Index: trunk/src/CuTest.c
===================================================================
--- trunk/src/CuTest.c	(revision 591)
+++ 	(revision )
@@ -1,350 +1,0 @@
-/*******************
-
-LICENSE
-
-Copyright (c) 2003 Asim Jalis
-
-This software is provided 'as-is', without any express or implied
-warranty. In no event will the authors be held liable for any damages
-arising from the use of this software.
-
-Permission is granted to anyone to use this software for any purpose,
-including commercial applications, and to alter it and redistribute it
-freely, subject to the following restrictions:
-
-1. The origin of this software must not be misrepresented; you must not
-claim that you wrote the original software. If you use this software in
-a product, an acknowledgment in the product documentation would be
-appreciated but is not required.
-
-2. Altered source versions must be plainly marked as such, and must not
-be misrepresented as being the original software.
-
-3. This notice may not be removed or altered from any source
-distribution.
-
-**********************/
-
-#include <assert.h>
-#include <setjmp.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <math.h>
-#include <unistd.h>
-
-#include "CuTest.h"
-
-/*-------------------------------------------------------------------------*
- * CuStr
- *-------------------------------------------------------------------------*/
-
-char* CuStrAlloc(int size)
-{
-	char* newStr = (char*) malloc( sizeof(char) * (size) );
-	return newStr;
-}
-
-char* CuStrCopy(const char* old)
-{
-	int len = strlen(old);
-	char* newStr = CuStrAlloc(len + 1);
-	strcpy(newStr, old);
-	return newStr;
-}
-
-/*-------------------------------------------------------------------------*
- * CuString
- *-------------------------------------------------------------------------*/
-
-void CuStringInit(CuString* str)
-{
-	str->length = 0;
-	str->size = STRING_MAX;
-	str->buffer = (char*) malloc(sizeof(char) * str->size);
-	if (str->buffer)
-	  str->buffer[0] = '\0';
-	else
-	  {
-	    perror("CuStringInit");
-	    _exit (EXIT_FAILURE);
-	  }
-}
-
-CuString* CuStringNew(void)
-{
-	CuString* str = (CuString*) malloc(sizeof(CuString));
-	str->length = 0;
-	str->size = STRING_MAX;
-	str->buffer = (char*) malloc(sizeof(char) * str->size);
-	if (str->buffer)
-	  str->buffer[0] = '\0';
-	else
-	  {
-	    perror("CuStringNew");
-	    _exit (EXIT_FAILURE);
-	  }
-	return str;
-}
-
-void CuStringResize(CuString* str, int newSize)
-{
-	str->buffer = (char*) realloc(str->buffer, sizeof(char) * newSize);
-	str->size = newSize;
-}
-
-void CuStringAppend(CuString* str, const char* text)
-{
-	int length;
-
-	if (text == NULL) {
-		text = "NULL";
-	}
-
-	length = strlen(text);
-	if (str->length + length + 1 >= str->size)
-		CuStringResize(str, str->length + length + 1 + STRING_INC);
-	str->length += length;
-	strcat(str->buffer, text);
-}
-
-void CuStringAppendChar(CuString* str, char ch)
-{
-	char text[2];
-	text[0] = ch;
-	text[1] = '\0';
-	CuStringAppend(str, text);
-}
-
-void CuStringAppendFormat(CuString* str, const char* format, ...)
-{
-	va_list argp;
-	char buf[2048];
-	va_start(argp, format);
-	vsnprintf(buf, sizeof(buf), format, argp);
-	va_end(argp);
-	CuStringAppend(str, buf);
-}
-
-void CuStringInsert(CuString* str, const char* text, int pos)
-{
-	int length = strlen(text);
-	if (pos > str->length)
-		pos = str->length;
-	if (str->length + length + 1 >= str->size)
-		CuStringResize(str, str->length + length + 1 + STRING_INC);
-	memmove(str->buffer + pos + length, str->buffer + pos, (str->length - pos) + 1);
-	str->length += length;
-	memcpy(str->buffer + pos, text, length);
-}
-
-/*-------------------------------------------------------------------------*
- * CuTest
- *-------------------------------------------------------------------------*/
-
-void CuTestInit(CuTest* t, const char* name, TestFunction function)
-{
-	t->name = CuStrCopy(name);
-	t->failed = 0;
-	t->ran = 0;
-	t->message = NULL;
-	t->function = function;
-	t->jumpBuf = NULL;
-}
-
-CuTest* CuTestNew(const char* name, TestFunction function)
-{
-	CuTest* tc = CU_ALLOC(CuTest);
-	CuTestInit(tc, name, function);
-	return tc;
-}
-
-void CuTestRun(CuTest* tc)
-{
-	jmp_buf buf;
-	tc->jumpBuf = &buf;
-	if (setjmp(buf) == 0)
-	{
-		tc->ran = 1;
-		(tc->function)(tc);
-	}
-	tc->jumpBuf = 0;
-}
-
-static void CuFailInternal(CuTest* tc, const char* file, int line, CuString* string)
-{
-	char buf[256];
-
-	snprintf(buf, sizeof(buf), "%s:%d: ", file, line);
-	CuStringInsert(string, buf, 0);
-
-	tc->failed = 1;
-	tc->message = string->buffer;
-	if (tc->jumpBuf != 0) longjmp(*(tc->jumpBuf), 0);
-}
-
-void CuFail_Line(CuTest* tc, const char* file, int line, const char* message2, const char* message)
-{
-	CuString string;
-
-	CuStringInit(&string);
-	if (message2 != NULL) 
-	{
-		CuStringAppend(&string, message2);
-		CuStringAppend(&string, ": ");
-	}
-	CuStringAppend(&string, message);
-	CuFailInternal(tc, file, line, &string);
-}
-
-void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, int condition)
-{
-	if (condition) return;
-	CuFail_Line(tc, file, line, NULL, message);
-}
-
-void CuAssertStrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, 
-	const char* expected, const char* actual)
-{
-	CuString string;
-	if ((expected == NULL && actual == NULL) ||
-	    (expected != NULL && actual != NULL &&
-	     strcmp(expected, actual) == 0))
-	{
-		return;
-	}
-
-	CuStringInit(&string);
-	if (message != NULL) 
-	{
-		CuStringAppend(&string, message);
-		CuStringAppend(&string, ": ");
-	}
-	CuStringAppend(&string, "expected <");
-	CuStringAppend(&string, expected);
-	CuStringAppend(&string, "> but was <");
-	CuStringAppend(&string, actual);
-	CuStringAppend(&string, ">");
-	CuFailInternal(tc, file, line, &string);
-}
-
-void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, 
-	int expected, int actual)
-{
-	char buf[STRING_MAX];
-	if (expected == actual) return;
-	sprintf(buf, "expected <%d> but was <%d>", expected, actual);
-	CuFail_Line(tc, file, line, message, buf);
-}
-
-void CuAssertDblEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, 
-	double expected, double actual, double delta)
-{
-	char buf[STRING_MAX];
-#define SH_FABS(a) (((a) < 0) ? -(a) : (a))
-        if (SH_FABS(expected - actual) <= delta) return;
-	sprintf(buf, "expected <%lf> but was <%lf>", expected, actual);
-	CuFail_Line(tc, file, line, message, buf);
-}
-
-void CuAssertPtrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, 
-	void* expected, void* actual)
-{
-	char buf[STRING_MAX];
-	if (expected == actual) return;
-	sprintf(buf, "expected pointer <0x%p> but was <0x%p>", expected, actual);
-	CuFail_Line(tc, file, line, message, buf);
-}
-
-
-/*-------------------------------------------------------------------------*
- * CuSuite
- *-------------------------------------------------------------------------*/
-
-void CuSuiteInit(CuSuite* testSuite)
-{
-	testSuite->count = 0;
-	testSuite->failCount = 0;
-}
-
-CuSuite* CuSuiteNew(void)
-{
-	CuSuite* testSuite = CU_ALLOC(CuSuite);
-	CuSuiteInit(testSuite);
-	return testSuite;
-}
-
-void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase)
-{
-	assert(testSuite->count < MAX_TEST_CASES);
-	testSuite->list[testSuite->count] = testCase;
-	testSuite->count++;
-}
-
-void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2)
-{
-	int i;
-	for (i = 0 ; i < testSuite2->count ; ++i)
-	{
-		CuTest* testCase = testSuite2->list[i];
-		CuSuiteAdd(testSuite, testCase);
-	}
-}
-
-void CuSuiteRun(CuSuite* testSuite)
-{
-	int i;
-	for (i = 0 ; i < testSuite->count ; ++i)
-	{
-		CuTest* testCase = testSuite->list[i];
-		CuTestRun(testCase);
-		if (testCase->failed) { testSuite->failCount += 1; }
-	}
-}
-
-void CuSuiteSummary(CuSuite* testSuite, CuString* summary)
-{
-	int i;
-	for (i = 0 ; i < testSuite->count ; ++i)
-	{
-		CuTest* testCase = testSuite->list[i];
-		CuStringAppend(summary, testCase->failed ? "F" : ".");
-	}
-	CuStringAppend(summary, "\n\n");
-}
-
-void CuSuiteDetails(CuSuite* testSuite, CuString* details)
-{
-	int i;
-	int failCount = 0;
-
-	if (testSuite->failCount == 0)
-	{
-		int passCount = testSuite->count - testSuite->failCount;
-		const char* testWord = passCount == 1 ? "test" : "tests";
-		CuStringAppendFormat(details, "OK (%d %s)\n", passCount, testWord);
-	}
-	else
-	{
-		if (testSuite->failCount == 1)
-			CuStringAppend(details, "There was 1 failure:\n");
-		else
-			CuStringAppendFormat(details, "There were %d failures:\n", testSuite->failCount);
-
-		for (i = 0 ; i < testSuite->count ; ++i)
-		{
-			CuTest* testCase = testSuite->list[i];
-			if (testCase->failed)
-			{
-				failCount++;
-				CuStringAppendFormat(details, "%d) %s: %s\n",
-					failCount, testCase->name, testCase->message);
-			}
-		}
-		CuStringAppend(details, "\n!!!FAILURES!!!\n");
-
-		CuStringAppendFormat(details, "Runs: %d ",   testSuite->count);
-		CuStringAppendFormat(details, "Passes: %d ", testSuite->count - testSuite->failCount);
-		CuStringAppendFormat(details, "Fails: %d\n",  testSuite->failCount);
-	}
-}
Index: trunk/src/bignum.c
===================================================================
--- trunk/src/bignum.c	(revision 591)
+++ trunk/src/bignum.c	(revision 1)
@@ -64,4 +64,9 @@
 #define ulong unsigned long
 
+/*
+extern void *malloc(size_t size);
+extern void free();
+*/
+
 char *last_string    = NULL;
 char *big_end_string = NULL;
@@ -175,5 +180,5 @@
     for (base = 2; base <= 36; base++)
     {
-	tmp = ((1U << (DIGIT_BITS - 1)) / base);
+	tmp = ((1 << (DIGIT_BITS - 1)) / base);
 	maxdigit = tmp * 2;
 	curdigit = 1;
@@ -209,7 +214,7 @@
   if (!dp) 
     return;
-  (void) count;
   free((char *)dp);
   dp = NULL;
+  count = 0;
 }
 #endif
@@ -227,5 +232,5 @@
     dgs_alloc += alloclen;
 #endif
-    digit_ptr = calloc(alloclen, sizeof(DIGIT));
+    digit_ptr = (DIGIT *) malloc(alloclen * sizeof(DIGIT));
     if (digit_ptr == NULL)
     {
@@ -425,5 +430,4 @@
 {
     DIGIT *a_ptr, *b_ptr;
-    int retval = 0;
 
     if (a->dgs_used  == b->dgs_used)
@@ -438,12 +442,10 @@
 	if (a_ptr < a->dp)
 	{
-	    return retval;
+	    return 0;
 	}
 	else
 	{
-	    if (retval == 0)
-	        retval = (*a_ptr > *b_ptr) ? 1 : -1;
-	}
-	return retval;
+	    return (*a_ptr > *b_ptr) ? 1 : -1;
+	}
     }
     return (a->dgs_used > b->dgs_used) ? 1 : -1;
@@ -623,5 +625,5 @@
     big_set_long((long)1, &big_one);
     length_last_string = 10;
-    if ((last_string = calloc(1,length_last_string)) == NULL)
+    if ((last_string = malloc(length_last_string)) == NULL)
     {
 	big_errno = BIG_MEMERR;
@@ -924,5 +926,5 @@
     if (a->sign == BIG_SIGN_MINUS)
     {
-      *n = -*n;
+	*n = -*n;;
     }
     return FALSE;
@@ -953,5 +955,5 @@
 	if (last_string != NULL)
 	  free(last_string);
-	if ((last_string = calloc(1,str_length)) == NULL)
+	if ((last_string = malloc(str_length)) == NULL)
 	{
 	    big_errno = BIG_MEMERR;
Index: trunk/src/caps.ac
===================================================================
--- trunk/src/caps.ac	(revision 1)
+++ trunk/src/caps.ac	(revision 1)
@@ -0,0 +1,23 @@
+        case "$host_os" in
+        *linux*)
+        if test x"${sh_use_lcaps}" = xyes
+        then
+            echo
+            echo "Using Linux capabilities to drop unnecessary root capabilities."
+            echo "Please make sure that directories used for writing have write permission"
+            echo "for root, because root will be a mere mortal in that respect:"
+            echo "  Baseline database in: ${mydataroot}"
+            echo "  Logfile in:           ${localstatedir}/log"
+            echo "  PID file in:          ${localstatedir}/run"
+            echo "(directories not existent yet will be created upon installation, root-owned"
+            echo "if make install is executed by root.)"
+            echo
+        fi
+        if test x"${sh_use_lcaps}" = xno
+        then
+            echo
+            echo "Not using Linux capabilities (capability.h or libcap not found)"
+            echo
+        fi
+        ;;
+        esac
Index: trunk/src/cutest_sh_hash.c
===================================================================
--- trunk/src/cutest_sh_hash.c	(revision 591)
+++ 	(revision )
@@ -1,121 +1,0 @@
-
-#include "config_xor.h"
-
-#include <string.h>
-#include "CuTest.h"
-
-extern char * quote_string   (const char * str, size_t len);
-extern char * unquote_string (const char * str, size_t len);
-
-void Test_quote_string_ok (CuTest *tc) {
-
-#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
-  char * ret = 0;
-
-  char   inp1[] = "foo\nba=r\ntest";
-  char   out1[] = "foo=0Aba=3Dr=0Atest";
-
-  char   inp2[] = "\n=foo\nba=r\ntest=\n";
-  char   out2[] = "=0A=3Dfoo=0Aba=3Dr=0Atest=3D=0A";
-
-  ret = quote_string(inp1, strlen(inp1));
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, out1, ret);
-
-  ret = quote_string(inp2,strlen(inp2));
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, out2, ret);
-#else
-  (void) tc; /* fix compiler warning */
-#endif
-  return;
-}
-
-void Test_unquote_string_ok (CuTest *tc) {
-#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
-  char * ret = 0;
-
-  char   out1[] = "foo\nba=r\ntes[t";
-  char   inp1[] = "foo=0Aba=3Dr=0Ates=5Bt";
-
-  char   out2[] = "\n=foo\nba=r\ntest=\n";
-  char   inp2[] = "=0A=3Dfoo=0Aba=3Dr=0Atest=3D=0A";
-
-  char   out3[] = ""; /* encoded '\0' at start */
-  char   inp3[] = "=00=3Dfoo=0Aba=3Dr=0Atest=3D=0A";
-
-  ret = unquote_string(inp1, strlen(inp1));
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, out1, ret);
-
-  ret = unquote_string(inp2, strlen(inp2));
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, out2, ret);
-
-  ret = unquote_string(inp3, strlen(inp3));
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, out3, ret);
-#else
-  (void) tc; /* fix compiler warning */
-#endif
-  return;
-}
-
-
-void Test_csv_escape_ok (CuTest *tc) {
-#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
-
-  extern char * csv_escape(const char * str);
-
-  char   test0[80];
-  char   expec[80];
-  char  *ret;
-
-  strcpy(test0, "foobar");
-  strcpy(expec, "\"foobar\"");
-  ret = csv_escape(test0);
-  CuAssertStrEquals(tc, expec, ret);
-
-  strcpy(test0, "\"foobar\"");
-  strcpy(expec, "\"\"\"foobar\"\"\"");
-  ret = csv_escape(test0);
-  CuAssertStrEquals(tc, expec, ret);
-
-  strcpy(test0, "foo,bar");
-  strcpy(expec, "\"foo,bar\"");
-  ret = csv_escape(test0);
-  CuAssertStrEquals(tc, expec, ret);
-
-  strcpy(test0, "foob,\"a\"r");
-  strcpy(expec, "\"foob,\"\"a\"\"r\"");
-  ret = csv_escape(test0);
-  CuAssertStrEquals(tc, expec, ret);
-
-  strcpy(test0, "\",\"foobar\",\"");
-  strcpy(expec, "\"\"\",\"\"foobar\"\",\"\"\"");
-  ret = csv_escape(test0);
-  CuAssertStrEquals(tc, expec, ret);
-
-  strcpy(test0, "");
-  strcpy(expec, "");
-  ret = csv_escape(test0);
-  CuAssertStrEquals(tc, expec, ret);
-
-  strcpy(test0, "a");
-  strcpy(expec, "\"a\"");
-  ret = csv_escape(test0);
-  CuAssertStrEquals(tc, expec, ret);
-
-  strcpy(test0, "foo\"bar");
-  strcpy(expec, "\"foo\"\"bar\"");
-  ret = csv_escape(test0);
-  CuAssertStrEquals(tc, expec, ret);
-
-#else
-  (void) tc; /* fix compiler warning */
-#endif
-  return;
-}
-
-
-
Index: trunk/src/cutest_sh_tiger0.c
===================================================================
--- trunk/src/cutest_sh_tiger0.c	(revision 591)
+++ 	(revision )
@@ -1,460 +1,0 @@
-
-#include "config_xor.h"
-
-#include <string.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include "CuTest.h"
-
-#include "sh_tiger.h"
-#include "sh_checksum.h"
-
-#if defined(HAVE_PTHREAD) && defined(SH_STEALTH)
-extern void sh_g_init(void);
-#endif
-
-
-static void init(void) {
-
-  extern unsigned char TcpFlag[8][PW_LEN+1];
-  extern UINT32  ErrFlag[2];
-  unsigned char * dez = NULL;
-  int i;
-
-#if defined(HAVE_PTHREAD) && defined(SH_STEALTH)
-  sh_g_init();
-#endif
-  skey = (sh_key_t *) malloc (sizeof(sh_key_t));
-  if (skey != NULL) 
-    {
-      skey->mlock_failed = S_FALSE;
-      skey->rngI         = BAD;
-      /* properly initialized later 
-       */
-      skey->rng0[0] = 0x03; skey->rng0[1] = 0x09; skey->rng0[2] = 0x17;
-      skey->rng1[0] = 0x03; skey->rng1[1] = 0x09; skey->rng1[2] = 0x17;
-      skey->rng2[0] = 0x03; skey->rng2[1] = 0x09; skey->rng2[2] = 0x17;
-      
-      for (i = 0; i < KEY_BYT; ++i)
-	skey->poolv[i] = '\0';
-      
-      skey->poolc        = 0;
-      
-      skey->ErrFlag[0]   = ErrFlag[0];
-      ErrFlag[0]         = 0;
-      skey->ErrFlag[1]   = ErrFlag[1];
-      ErrFlag[1]         = 0;
-      
-      dez = &(TcpFlag[POS_TF-1][0]);
-      for (i = 0; i < PW_LEN; ++i)
-	{ 
-	  skey->pw[i] = (char) (*dez); 
-	  (*dez)      = '\0';
-	  ++dez; 
-	}
-      
-      skey->sh_sockpass[0]  = '\0';
-      skey->sigkey_old[0]   = '\0';
-      skey->sigkey_new[0]   = '\0';
-      skey->mailkey_old[0]  = '\0';
-      skey->mailkey_new[0]  = '\0';
-      skey->crypt[0]        = '\0';
-      skey->session[0]      = '\0';
-      skey->vernam[0]       = '\0';
-    }
-  else
-    {
-      perror(_("sh_init"));
-      _exit (EXIT_FAILURE);
-    }
-
-}
-  
-void Test_tiger(CuTest *tc) {
-
-  char * input;
-  char * actual;
-  char * expected;
-  char hashbuf[KEYBUF_SIZE];
-
-#if defined(HAVE_PTHREAD) && defined(SH_STEALTH)
-  sh_g_init();
-#endif
-
-  input  = "";
-  actual = sh_tiger_hash(input, TIGER_DATA, strlen(input), hashbuf, sizeof(hashbuf));
-  expected = "24F0130C63AC933216166E76B1BB925FF373DE2D49584E7A";
-  CuAssertStrEquals(tc, expected, actual);
-
-  input  = "abc";
-  actual = sh_tiger_hash(input, TIGER_DATA, strlen(input), hashbuf, sizeof(hashbuf));
-  expected = "F258C1E88414AB2A527AB541FFC5B8BF935F7B951C132951";
-  CuAssertStrEquals(tc, expected, actual);
-  
-  input  = "Tiger";
-  actual = sh_tiger_hash(input, TIGER_DATA, strlen(input), hashbuf, sizeof(hashbuf));
-  expected = "9F00F599072300DD276ABB38C8EB6DEC37790C116F9D2BDF";
-  CuAssertStrEquals(tc, expected, actual);
-  
-  input  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-";
-  actual = sh_tiger_hash(input, TIGER_DATA, strlen(input), hashbuf, sizeof(hashbuf));
-  expected = "87FB2A9083851CF7470D2CF810E6DF9EB586445034A5A386";
-  CuAssertStrEquals(tc, expected, actual);
-  
-  input  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ=abcdefghijklmnopqrstuvwxyz+0123456789";
-  actual = sh_tiger_hash(input, TIGER_DATA, strlen(input), hashbuf, sizeof(hashbuf));
-  expected = "467DB80863EBCE488DF1CD1261655DE957896565975F9197";
-  CuAssertStrEquals(tc, expected, actual);
-  
-  input  = "Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham";
-  actual = sh_tiger_hash(input, TIGER_DATA, strlen(input), hashbuf, sizeof(hashbuf));
-  expected = "0C410A042968868A1671DA5A3FD29A725EC1E457D3CDB303";
-  CuAssertStrEquals(tc, expected, actual);
-  
-  input  = "Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham, proceedings of Fast Software Encryption 3, Cambridge.";
-  actual = sh_tiger_hash(input, TIGER_DATA, strlen(input), hashbuf, sizeof(hashbuf));
-  expected = "EBF591D5AFA655CE7F22894FF87F54AC89C811B6B0DA3193";
-  CuAssertStrEquals(tc, expected, actual);
-  
-  input  = "Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham, proceedings of Fast Software Encryption 3, Cambridge, 1996.";
-  actual = sh_tiger_hash(input, TIGER_DATA, strlen(input), hashbuf, sizeof(hashbuf));
-  expected = "3D9AEB03D1BD1A6357B2774DFD6D5B24DD68151D503974FC";
-  CuAssertStrEquals(tc, expected, actual);
-  
-  input  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-";
-  actual = sh_tiger_hash(input, TIGER_DATA, strlen(input), hashbuf, sizeof(hashbuf));
-  expected = "00B83EB4E53440C576AC6AAEE0A7485825FD15E70A59FFE4";
-  CuAssertStrEquals(tc, expected, actual);
-}
-
-void Test_tiger_file(CuTest *tc) {
-
-  SL_TICKET     rval_open;
-  FILE * fp;
-  int result;
-  char * actual;
-  char * expected;
-  char hashbuf[KEYBUF_SIZE];
-  char hexdigest[SHA256_DIGEST_STRING_LENGTH];
-  UINT64  length;
-
-  init();
-
-  fp = fopen("cutest_foo", "w");
-  CuAssertPtrNotNull(tc, fp);
-
-  result = fprintf(fp, "%s\n", 
-		   "ABCDEFGHIJKLMNOPQRSTUVWXYZ=abcdefghijklmnopqrstuvwxyz+0123456789");
-  CuAssertTrue(tc, result >= 0);
-
-  result = fclose(fp);
-  CuAssertTrue(tc, result == 0);
-  
-  result = sh_tiger_hashtype("TIGER192");
-  CuAssertTrue(tc, result == 0);
-
-  /* same result as GnuPG 1.0.6 (gpg --load-extension tiger --print-md TIGER192) 
-   */
-  length = TIGER_NOLIM;
-  actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "0E9321614C966A33608C2A15F156E0435CACFD1213B9F095";
-  CuAssertStrEquals(tc, expected, actual);
-
-  rval_open = sl_open_fastread (__FILE__, __LINE__, "cutest_foo", SL_YESPRIV);
-  CuAssertTrue(tc, rval_open >= 0);
-
-  length = TIGER_NOLIM;
-  actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "0E9321614C966A33608C2A15F156E0435CACFD1213B9F095";
-  CuAssertStrEquals(tc, expected, actual);
-
-  result = sl_close(rval_open);
-  CuAssertTrue(tc, result == 0);
-
-  result = sh_tiger_hashtype("MD5");
-  CuAssertTrue(tc, result == 0);
-
-  rval_open = sl_open_fastread (__FILE__, __LINE__, "cutest_foo", SL_YESPRIV);
-  CuAssertTrue(tc, rval_open >= 0);
-
-  /* same result as GNU md5sum 
-   */
-  length = TIGER_NOLIM;
-  actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "AEEC4DDA496BCFBA691F4E8863BA84C00000000000000000";
-  CuAssertStrEquals(tc, expected, actual);
-
-  result = sl_close(rval_open);
-  CuAssertTrue(tc, result == 0);
-
-  result = sh_tiger_hashtype("SHA1");
-  CuAssertTrue(tc, result == 0);
-
-  rval_open = sl_open_fastread (__FILE__, __LINE__, "cutest_foo", SL_YESPRIV);
-  CuAssertTrue(tc, rval_open >= 0);
-
-  /* same result as gpg --print-md SHA1 
-   */
-  length = TIGER_NOLIM;
-  actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "2FE65D1D995B8F8BC8B13F798C07E7E935A787ED00000000";
-  CuAssertStrEquals(tc, expected, actual);
-
-  result = sl_close(rval_open);
-  CuAssertTrue(tc, result == 0);
-
-  result = sh_tiger_hashtype("SHA256");
-  CuAssertTrue(tc, result == 0);
-
-  rval_open = sl_open_fastread (__FILE__, __LINE__, "cutest_foo", SL_YESPRIV);
-  CuAssertTrue(tc, rval_open >= 0);
-
-  /* same result as gpg --print-md SHA256
-   */
-  length = TIGER_NOLIM;
-  {
-    char * tmp = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
-    actual = SHA256_Base2Hex(tmp, hexdigest);
-  }
-  expected = "235790848f95e96b2c627f1bf58a2b8c05c535ada8c0a3326aac34ce1391ad40";
-  CuAssertStrEquals(tc, expected, actual);
-
-  result = sl_close(rval_open);
-  CuAssertTrue(tc, result == 0);
-
-  result = remove("cutest_foo");
-  CuAssertTrue(tc, result == 0);
-
-  /* --------------------------------------------------- */
-
-  fp = fopen("cutest_foo", "w");
-  CuAssertPtrNotNull(tc, fp);
-
-  result = fprintf(fp, "\n");
-  CuAssertTrue(tc, result >= 0);
-
-  result = fclose(fp);
-  CuAssertTrue(tc, result == 0);
-  
-  result = sh_tiger_hashtype("TIGER192");
-  CuAssertTrue(tc, result == 0);
-
-  /* same result as GnuPG 1.0.6 (gpg --load-extension tiger --print-md TIGER192) 
-   */
-  length = TIGER_NOLIM;
-  actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "F987845A0EA784367BF9E4DB09014995810F27C99C891734";
-  CuAssertStrEquals(tc, expected, actual);
-
-  result = remove("cutest_foo");
-  CuAssertTrue(tc, result == 0);
-
-  /* --------------------------------------------------- */
-
-  fp = fopen("cutest_foo", "w");
-  CuAssertPtrNotNull(tc, fp);
-
-  result = fprintf(fp, "Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham, proceedings of Fast Software Encryption 3, Cambridge, 1996.\n");
-  CuAssertTrue(tc, result >= 0);
-
-  result = fclose(fp);
-  CuAssertTrue(tc, result == 0);
-  
-  result = sh_tiger_hashtype("TIGER192");
-  CuAssertTrue(tc, result == 0);
-
-  /* same result as GnuPG 1.0.6 (gpg --load-extension tiger --print-md TIGER192) 
-   */
-  length = TIGER_NOLIM;
-  actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "75B98A7AE257A230189828A40792E30B4038D286479CC7B8";
-  CuAssertStrEquals(tc, expected, actual);
-
-  result = remove("cutest_foo");
-  CuAssertTrue(tc, result == 0);
-
-}  
-
-/* test checksum of file upto some given length
- */
-void Test_tiger_file_with_length(CuTest *tc) {
-
-  SL_TICKET     rval_open;
-  FILE * fp;
-  int result;
-  char * actual;
-  char * expected;
-  char hashbuf[KEYBUF_SIZE];
-  UINT64  length;
-
-  char * teststring = "Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham, proceedings of Fast Software Encryption 3, Cambridge, 1996.\n";
-  size_t    testlen = strlen(teststring);
-
-  init();
-
-  fp = fopen("cutest_foo", "w");
-  CuAssertPtrNotNull(tc, fp);
-
-  result = fprintf(fp, "%s", teststring);
-  CuAssertTrue(tc, result >= 0);
-  result = fprintf(fp, "%s", teststring);
-  CuAssertTrue(tc, result >= 0);
-
-  result = fclose(fp);
-  CuAssertTrue(tc, result == 0);
-  
-  result = sh_tiger_hashtype("TIGER192");
-  CuAssertTrue(tc, result == 0);
-
-  /* same as GnuPG 1.0.6 (gpg --load-extension tiger --print-md TIGER192) 
-   */
-  length = 0;
-  actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "24F0130C63AC933216166E76B1BB925FF373DE2D49584E7A";
-  CuAssertStrEquals(tc, expected, actual);
-  CuAssertTrue(tc, 0 == length);
-
-  length = testlen;
-  actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "75B98A7AE257A230189828A40792E30B4038D286479CC7B8";
-  CuAssertStrEquals(tc, expected, actual);
-  CuAssertTrue(tc, testlen == length);
-
-  length = 2*testlen;
-  actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "B5B4FB97B01ADB58794D87A6A01B2368852FA764BD93AB90";
-  CuAssertStrEquals(tc, expected, actual);
-  CuAssertTrue(tc, 2*testlen == length);
-
-  length = TIGER_NOLIM;
-  actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "B5B4FB97B01ADB58794D87A6A01B2368852FA764BD93AB90";
-  CuAssertStrEquals(tc, expected, actual);
-  CuAssertTrue(tc, 2*testlen == length);
-
-  fp = fopen("cutest_foo", "a");
-  CuAssertPtrNotNull(tc, fp);
-  result = fprintf(fp, "%s", teststring);
-  CuAssertTrue(tc, result >= 0);
-  result = fclose(fp);
-  CuAssertTrue(tc, result == 0);
-
-  length = testlen;
-  actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "75B98A7AE257A230189828A40792E30B4038D286479CC7B8";
-  CuAssertStrEquals(tc, expected, actual);
-  CuAssertTrue(tc, testlen == length);
-
-  length = 2*testlen;
-  actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "B5B4FB97B01ADB58794D87A6A01B2368852FA764BD93AB90";
-  CuAssertStrEquals(tc, expected, actual);
-  CuAssertTrue(tc, 2*testlen == length);
-
-  length = 3*testlen;
-  actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "D0EE1A9956CAB22D84B51A5E0C093B724828C6A1F9CBDB7F";
-  CuAssertStrEquals(tc, expected, actual);
-  CuAssertTrue(tc, 3*testlen == length);
-
-  length = TIGER_NOLIM;
-  actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "D0EE1A9956CAB22D84B51A5E0C093B724828C6A1F9CBDB7F";
-  CuAssertStrEquals(tc, expected, actual);
-  CuAssertTrue(tc, 3*testlen == length);
-
-  length = 5;
-  actual = sh_tiger_generic_hash("cutest_foo", TIGER_FILE, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "9F00F599072300DD276ABB38C8EB6DEC37790C116F9D2BDF";
-  CuAssertStrEquals(tc, expected, actual);
-  CuAssertTrue(tc, 5 == length);
-
-  /* same results as GNU md5sum */
-
-  result = sh_tiger_hashtype("MD5");
-  CuAssertTrue(tc, result == 0);
-
-  rval_open = sl_open_fastread (__FILE__, __LINE__, "cutest_foo", SL_YESPRIV);
-  CuAssertTrue(tc, rval_open >= 0);
-
-  length = testlen;
-  actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "11E7E7EA486136273606BEE57C71F34B0000000000000000";
-  CuAssertStrEquals(tc, expected, actual);
-  CuAssertTrue(tc, testlen == length);
-
-  result = sl_rewind(rval_open);
-  CuAssertTrue(tc, rval_open >= 0);
-
-  length = 2*testlen;
-  actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "D49DAD474095D467E2E5EFCB2DC23A770000000000000000";
-  CuAssertStrEquals(tc, expected, actual);
-  CuAssertTrue(tc, 2*testlen == length);
-
-  result = sl_rewind(rval_open);
-  CuAssertTrue(tc, rval_open >= 0);
-
-  length = 3*testlen;
-  actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "00A1F1C5EDDCCFC430D3862FDA94593E0000000000000000";
-  CuAssertStrEquals(tc, expected, actual);
-  CuAssertTrue(tc, 3*testlen == length);
-
-  result = sl_rewind(rval_open);
-  CuAssertTrue(tc, rval_open >= 0);
-
-  length = TIGER_NOLIM;
-  actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "00A1F1C5EDDCCFC430D3862FDA94593E0000000000000000";
-  CuAssertStrEquals(tc, expected, actual);
-  CuAssertTrue(tc, 3*testlen == length);
-
-  /* same result as gpg --print-md SHA1 
-   */
-
-  result = sh_tiger_hashtype("SHA1");
-  CuAssertTrue(tc, result == 0);
-
-  result = sl_rewind(rval_open);
-  CuAssertTrue(tc, rval_open >= 0);
-
-  length = testlen;
-  actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "F37DB4344CCD140EE315179E9A27512FB4704F0F00000000";
-  CuAssertStrEquals(tc, expected, actual);
-  CuAssertTrue(tc, testlen == length);
-
-  result = sl_rewind(rval_open);
-  CuAssertTrue(tc, rval_open >= 0);
-
-  length = 2*testlen;
-  actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "D2AD5FC366452D81400BAC31F96269DEEF314BC200000000";
-  CuAssertStrEquals(tc, expected, actual);
-  CuAssertTrue(tc, 2*testlen == length);
-
-  result = sl_rewind(rval_open);
-  CuAssertTrue(tc, rval_open >= 0);
-
-  length = 3*testlen;
-  actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "FAA937EF3389C7E786EB0F1006D049D7AEA7B7B600000000";
-  CuAssertStrEquals(tc, expected, actual);
-  CuAssertTrue(tc, 3*testlen == length);
-
-  result = sl_rewind(rval_open);
-  CuAssertTrue(tc, rval_open >= 0);
-
-  length = TIGER_NOLIM;
-  actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
-  expected = "FAA937EF3389C7E786EB0F1006D049D7AEA7B7B600000000";
-  CuAssertStrEquals(tc, expected, actual);
-  CuAssertTrue(tc, 3*testlen == length);
-
-  result = sl_close(rval_open);
-  CuAssertTrue(tc, result == 0);
-
-  result = remove("cutest_foo");
-  CuAssertTrue(tc, result == 0);
-}
Index: trunk/src/cutest_sh_tools.c
===================================================================
--- trunk/src/cutest_sh_tools.c	(revision 591)
+++ 	(revision )
@@ -1,138 +1,0 @@
-
-#include "config_xor.h"
-
-#include <string.h>
-#include "CuTest.h"
-#include "samhain.h"
-#include "sh_tools.h"
-#include "sh_ipvx.h"
-
-void Test_sh_tools_safe_name_01(CuTest *tc) {
-  /* xml specific */
-  char* input = strdup("hello<wo>rld\"foo&");
-  char* actual = sh_tools_safe_name(input, 1);
-#ifdef SH_USE_XML
-  char* expected = "hello=3cwo=3erld=22foo=26";
-#else
-  char* expected = "hello<wo>rld\"foo&";
-#endif
-  CuAssertStrEquals(tc, expected, actual);
-}
-
-void Test_sh_tools_safe_name_02(CuTest *tc) {
-  /* html entities */
-  char* input = strdup("hello&amp;&quot;&gt;&lt;");
-  char* actual = sh_tools_safe_name(input, 0);
-  char* expected = "hello=26=22=3e=3c";
-  CuAssertStrEquals(tc, expected, actual);
-}
-
-void Test_sh_tools_safe_name_03(CuTest *tc) {
-  char* input = strdup("\\\'hello\\");
-  char* actual = sh_tools_safe_name(input, 0);
-  char* expected = "=27hello";
-  CuAssertStrEquals(tc, expected, actual);
-
-  input = strdup("hello \"world\\\"");
-  actual = sh_tools_safe_name(input, 0);
-  expected = "hello \"world=22";
-  CuAssertStrEquals(tc, expected, actual);
-
-  input = strdup("hello\\\\");
-  actual = sh_tools_safe_name(input, 0);
-  expected = "hello=5c";
-  CuAssertStrEquals(tc, expected, actual);
-
-  input = strdup("hello\\n");
-  actual = sh_tools_safe_name(input, 0);
-  expected = "hello=0a";
-  CuAssertStrEquals(tc, expected, actual);
-}
-
-void Test_sh_tools_safe_name_04(CuTest *tc) {
-  /* invalid and valid octal code */
-  char* input = strdup("hello\\\n");
-  char* actual = sh_tools_safe_name(input, 0);
-  char* expected = "hello";
-  CuAssertStrEquals(tc, expected, actual);
-
-  input = strdup("hello\\100");
-  actual = sh_tools_safe_name(input, 0);
-  expected = "hello=40";
-  CuAssertStrEquals(tc, expected, actual);
-
-  input = strdup("h\\\"ello\\100a");
-  actual = sh_tools_safe_name(input, 0);
-  expected = "h=22ello=40a";
-  CuAssertStrEquals(tc, expected, actual);
-}
-
-void Test_sh_tools_safe_name_05(CuTest *tc) {
-  /* encoding of '=' */
-  char* input = strdup("he=llo=\"foo\"");
-  char* actual = sh_tools_safe_name(input, 0);
-  char* expected = "he=3dllo=\"foo\"";
-  CuAssertStrEquals(tc, expected, actual);
-
-  input = strdup("he=llo=<foo>");
-  actual = sh_tools_safe_name(input, 0);
-  expected = "he=3dllo=<foo>";
-  CuAssertStrEquals(tc, expected, actual);
-}
-
-void Test_sh_tools_safe_name_06(CuTest *tc) {
-  /* line break removal */
-  char* input = strdup("hello\nworld");
-  char* actual = sh_tools_safe_name(input, 0);
-  char* expected = "hello world";
-  CuAssertStrEquals(tc, expected, actual);
-}
-
-void Test_sh_tools_safe_name_07(CuTest *tc) {
-  /* non-printable chars */
-  char* input = strdup("hello world");
-  char* actual;
-  char* expected;
-
-  input[0]  = 0x01;
-  input[5]  = 0xFF;
-  input[10] = 0xF0;
-
-  actual   = sh_tools_safe_name(input, 0);
-  expected = "=01ello=ffworl=f0";
-  CuAssertStrEquals(tc, expected, actual);
-}
-
-void Test_is_numeric_01(CuTest *tc) {
-  char* input  = strdup("hello world");
-
-  CuAssertTrue(tc, !sh_ipvx_is_numeric(input));
-
-  input  = strdup("127.0.0.1");
-  CuAssertTrue(tc, sh_ipvx_is_numeric(input));
-  input  = strdup("127.0.0.de");
-  CuAssertTrue(tc, !sh_ipvx_is_numeric(input));
-  input  = strdup("127");
-  CuAssertTrue(tc, !sh_ipvx_is_numeric(input));
-#if defined(USE_IPVX)
-  input  = strdup("::1");
-  CuAssertTrue(tc, sh_ipvx_is_numeric(input));
-  input  = strdup("2002:c0a8:101::42");
-  CuAssertTrue(tc, sh_ipvx_is_numeric(input));
-  input  = strdup("2003:dead:beef:4dad:23:46:bb:101");
-  CuAssertTrue(tc, sh_ipvx_is_numeric(input));
-  input  = strdup("::192:168:0:1::");
-  CuAssertTrue(tc, sh_ipvx_is_numeric(input));
-  input  = strdup("1:1:192:168:0:1:1:1");
-  CuAssertTrue(tc, sh_ipvx_is_numeric(input));
-  input  = strdup("1:1:192:168:0:1:1:1:0");
-  CuAssertTrue(tc, !sh_ipvx_is_numeric(input));
-  input  = strdup("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
-  CuAssertTrue(tc, sh_ipvx_is_numeric(input));
-  input  = strdup("2001:db8:85a3:0:0:8a2e:370:7334");
-  CuAssertTrue(tc, sh_ipvx_is_numeric(input));
-  input  = strdup("2001:db8:85a3::8a2e:370:7334");
-  CuAssertTrue(tc, sh_ipvx_is_numeric(input));
-#endif
-}
-
Index: trunk/src/cutest_sh_unix.c
===================================================================
--- trunk/src/cutest_sh_unix.c	(revision 591)
+++ 	(revision )
@@ -1,201 +1,0 @@
-
-#include "config_xor.h"
-
-#include <string.h>
-#include "CuTest.h"
-#include "samhain.h"
-#include "sh_unix.h"
-
-int malloc_count = 0;
-
-void Test_dnmalloc (CuTest *tc) {
-
-  const int nalloc = 64 /* original dnmalloc 1.0-beta5 fails for >= 45 */;
-  int j, i;
-  int sum;
-#ifndef USE_SYSTEM_MALLOC
-  int i_malloc =  malloc_count;
-#endif
-
-  char * buf;
-  char * area[256];
-
-#if !defined(USE_SYSTEM_MALLOC) && !defined(__clang__)
-  /* test reuse of last freed chunk */
-  buf = malloc(1024);
-  CuAssertPtrNotNull(tc, buf);
-  free(buf);
-  area[0] = malloc(1024);
-  CuAssertTrue(tc, buf == area[0]);
-  free(area[0]);
-#endif
-  
-  /* test realloc */
-  buf = malloc(16);
-  CuAssertPtrNotNull(tc, buf);
-  strcpy(buf, "testing realloc");
-  buf = realloc(buf, 32);
-  strcat(buf, "testing realloc");
-  CuAssertStrEquals(tc, "testing realloctesting realloc", buf);
-
-#ifndef USE_SYSTEM_MALLOC
-  i_malloc = malloc_count;
-#endif
-
-  for (j = 0; j < 64; ++j)
-    {
-      buf = malloc((j+1) * 1024);
-      CuAssertPtrNotNull(tc, buf);
-#ifndef USE_SYSTEM_MALLOC
-#ifndef __clang__
-      CuAssertIntEquals (tc, malloc_count, (i_malloc + 1));
-#endif
-#endif
-      free(buf);
-#ifndef USE_SYSTEM_MALLOC
-      CuAssertIntEquals (tc, malloc_count, i_malloc);
-#endif
-    }
-
-  /* test realloc */
-  buf = malloc(16);
-  CuAssertPtrNotNull(tc, buf);
-  strcpy(buf, "testing realloc");
-  buf = realloc(buf, 32);
-  strcat(buf, "testing realloc");
-  CuAssertStrEquals(tc, "testing realloctesting realloc", buf);
-
-#ifndef USE_SYSTEM_MALLOC
-  i_malloc = malloc_count;
-#endif
-
-  for (j = 0; j < 64; ++j)
-    {
-      buf = calloc(1, (j+1) * 1024);
-      CuAssertPtrNotNull(tc, buf);
-#ifndef USE_SYSTEM_MALLOC
-      CuAssertIntEquals (tc, malloc_count, (i_malloc + 1));
-#endif
-      sum = 0;
-      for (i = 0; i < ((j+1) * 1024); ++i)
-	sum += buf[i];
-      CuAssertIntEquals (tc, 0, sum);
-      free(buf);
-#ifndef USE_SYSTEM_MALLOC
-      CuAssertIntEquals (tc, malloc_count, i_malloc);
-#endif
-    }
-
-  /* test realloc */
-  buf = malloc(16);
-  CuAssertPtrNotNull(tc, buf);
-  strcpy(buf, "testing realloc");
-  buf = realloc(buf, 32);
-  strcat(buf, "testing realloc");
-  CuAssertStrEquals(tc, "testing realloctesting realloc", buf);
-
-  for (j = 0; j < nalloc; ++j)
-    {
-      area[j] = malloc((j+1) * 1024);
-      CuAssertPtrNotNull(tc, area[j]);
-#ifndef USE_SYSTEM_MALLOC
-      /* CuAssertIntEquals (tc, malloc_count, (i_malloc + (j+1))); */
-#endif
-      memset(area[j], (unsigned char) ('a'+1), (j+1) * 1024);
-    }
-
-#ifndef USE_SYSTEM_MALLOC
-  i_malloc =  malloc_count;
-#endif
-
-  for (j = 0; j < nalloc; ++j)
-    {
-      sum = 0;
-      for (i = 0; i < ((j+1) * 1024); ++i)
-	sum +=  area[j][i];
-      CuAssertIntEquals (tc, sum, ((j+1) * 1024 * ((unsigned char) ('a'+1))));
-      free(area[j]);
-#ifndef USE_SYSTEM_MALLOC
-      CuAssertIntEquals (tc, malloc_count, i_malloc - (j+1));
-#endif
-    }
-
-  /* test realloc */
-  buf = malloc(16);
-  CuAssertPtrNotNull(tc, buf);
-  strcpy(buf, "testing realloc");
-  buf = realloc(buf, 32);
-  strcat(buf, "testing realloc");
-  CuAssertStrEquals(tc, "testing realloctesting realloc", buf);
-
-
-  for (j = 0; j < 32; ++j)
-    {
-#ifndef USE_SYSTEM_MALLOC
-      i_malloc =  malloc_count;
-#endif
-      buf = malloc((j+1) * 1024 * 1024);
-      CuAssertPtrNotNull(tc, buf);
-      for (i = 0; i < 32; ++i)
-	{
-	  area[i] = malloc((i+1) * 1024);
-	  CuAssertPtrNotNull(tc, area[i]);
-	}
-      free(buf);
-      for (i = 0; i < 32; ++i)
-	{
-	  free(area[i]);
-	}
-#ifndef USE_SYSTEM_MALLOC
-      CuAssertIntEquals (tc, malloc_count, i_malloc);
-#endif
-    }
-
-  /* test realloc */
-  buf = malloc(16);
-  CuAssertPtrNotNull(tc, buf);
-  strcpy(buf, "testing realloc");
-  buf = realloc(buf, 32);
-  strcat(buf, "testing realloc");
-  CuAssertStrEquals(tc, "testing realloctesting realloc", buf);
-}
-
-  
-void Test_sh_unix_lookup_page (CuTest *tc) {
-
-  long pagesize = sh_unix_pagesize();
-  
-  unsigned long base;
-  int          num_pages;
-
-  CuAssert (tc, "pagesize > 0", (pagesize > 0));
-
-  /* base = sh_unix_lookup_page(in_addr, len, *num_pages); */
-
-  base = sh_unix_lookup_page(0, pagesize, &num_pages);
-  CuAssert (tc, "base == 0", (base == 0));
-  CuAssertIntEquals (tc, num_pages, 1);
-
-  base = sh_unix_lookup_page(0, pagesize+1, &num_pages);
-  CuAssert (tc, "base == 0", (base == 0));
-  CuAssertIntEquals (tc, num_pages, 2);
-
-  base = sh_unix_lookup_page((void*)pagesize, pagesize, &num_pages);
-  CuAssert (tc, "base == 0", (base == (unsigned int)pagesize));
-  CuAssertIntEquals (tc, num_pages, 1);
-
-  base = sh_unix_lookup_page((void*)pagesize, pagesize+1, &num_pages);
-  CuAssert (tc, "base == 0", (base == (unsigned int)pagesize));
-  CuAssertIntEquals (tc, num_pages, 2);
-
-  base = sh_unix_lookup_page((void*)(pagesize-1), pagesize+1, &num_pages);
-  CuAssert (tc, "base == 0", (base == 0));
-  CuAssertIntEquals (tc, num_pages, 2);
-
-  base = sh_unix_lookup_page((void*)(pagesize-1), pagesize+2, &num_pages);
-  CuAssert (tc, "base == 0", (base == 0));
-  CuAssertIntEquals (tc, num_pages, 3);
-
-}
-
-  
Index: trunk/src/cutest_sh_utils.c
===================================================================
--- trunk/src/cutest_sh_utils.c	(revision 591)
+++ 	(revision )
@@ -1,532 +1,0 @@
-
-#include "config_xor.h"
-
-#include <string.h>
-#include "CuTest.h"
-#include "samhain.h"
-#include "sh_utils.h"
-
-void Test_sl_strlcpy (CuTest *tc) {
-  int ret;
-  char out[] = "aaaaaa";
-  char in[]  = "bbb";
-
-  ret = sl_strlcpy (NULL, NULL, 0);
-  CuAssertIntEquals(tc, ret, SL_ENONE);
-
-  ret = sl_strlcpy (NULL, in, 0);
-  CuAssertIntEquals(tc, ret, SL_ENULL);
-
-  ret = sl_strlcpy (out, NULL, 0);
-  CuAssertIntEquals(tc, ret, SL_ENONE);
-
-  ret = sl_strlcpy (out, in, 0);
-  CuAssertIntEquals(tc, ret, SL_ENONE);
-
-  ret = sl_strlcpy (out, NULL, 7);
-  CuAssertIntEquals(tc, ret, SL_ENONE);
-  CuAssertStrEquals(tc, "", out);
-
-  out[0] = 'a';
-  ret = sl_strlcpy (out, in, 4);
-  CuAssertIntEquals(tc, ret, SL_ENONE);
-  CuAssertStrEquals(tc, "bbb", out);
-  CuAssertStrEquals(tc, "aa", &out[4]);
-  
-  return;
-}
-
-void Test_sl_strlcat (CuTest *tc) {
-  int ret;
-  char out[16] = "aaaaaa";
-  char in[16]  = "bbb";
-
-  ret = sl_strlcat (NULL, NULL, 0);
-  CuAssertIntEquals(tc, ret, SL_ENONE);
-
-  ret = sl_strlcat (NULL, in, 0);
-  CuAssertIntEquals(tc, ret, SL_ENONE);
-
-  ret = sl_strlcat (out, NULL, 0);
-  CuAssertIntEquals(tc, ret, SL_ENONE);
-
-  ret = sl_strlcat (out, in, 0);
-  CuAssertIntEquals(tc, ret, SL_ENONE);
-
-  ret = sl_strlcat (out, NULL, sizeof(out));
-  CuAssertIntEquals(tc, ret, SL_ENONE);
-  CuAssertStrEquals(tc, "aaaaaa", out);
-
-  ret = sl_strlcat (out, in, 7);
-  CuAssertIntEquals(tc, ret, SL_ETRUNC);
-  CuAssertStrEquals(tc, "aaaaaa", out);
-
-  ret = sl_strlcat (out, in, 8);
-  CuAssertIntEquals(tc, ret, SL_ETRUNC);
-  CuAssertStrEquals(tc, "aaaaaab", out);
-
-  ret = sl_strlcat (out, in, sizeof(out));
-  CuAssertIntEquals(tc, ret, SL_ENONE);
-  CuAssertStrEquals(tc, "aaaaaabbbb", out);
-
-  CuAssertStrEquals(tc, "bbb", in);
-
-  return;
-}
-
-void Test_sh_util_acl_compact (CuTest *tc) {
-  char * ret = 0;
-  char   inp1[] = "user::r--\nuser:lisa:rwx\t\t#effective: r--\ngroup::r--\ngroup:toolies:rw-  #effective: r--\nmask::r--\nother::r--\n";
-  char   inp2[] = "use\n\nuser:lisa:rwx\t\t#effective: r--\ngroup::r--\ngroup:toolies:rw-  #effective: r--\nmask::r--\nother::r--\n";
-  char   inp3[] = "user:\177\145\177\122:r--\nuser:lisa:rwx\t\t#effective: r--\ngroup::r--\ngroup:toolies:rw-  #effective: r--\nmask::r--\nother::r--\n";
-  
-  ret = sh_util_acl_compact (inp1, strlen(inp1));
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, "u::r--,u:lisa:rwx,g::r--,g:toolies:rw-,m::r--,o::r--",
-		    ret); 
-
-  ret = sh_util_acl_compact (inp2, strlen(inp2));
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, "use,u:lisa:rwx,g::r--,g:toolies:rw-,m::r--,o::r--",
-		    ret); 
-
-  ret = sh_util_acl_compact (inp3, strlen(inp3));
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, "u:eR:r--,u:lisa:rwx,g::r--,g:toolies:rw-,m::r--,o::r--",
-		    ret); 
-
-  return;
-}
-
-void Test_sh_util_strdup_ok (CuTest *tc) {
-  char * ret = 0;
-  char   inp[] = "foobar";
-
-  ret = sh_util_strdup(inp);
-  CuAssertPtrNotNull(tc, ret);
-  CuAssert(tc, "expected inp != ret, but inp == ret", (inp != ret)); 
-  CuAssertStrEquals(tc, "foobar", ret);
-  return;
-}
-
-void Test_sh_util_strconcat_ok (CuTest *tc) {
-  char * ret = 0;
-
-  ret = sh_util_strconcat("foo", NULL);
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, "foo", ret);
-
-  ret = sh_util_strconcat("foo", "bar", NULL);
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, "foobar", ret);
-
-  ret = sh_util_strconcat("/", "foo", "/", "bar", NULL);
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, "/foo/bar", ret);
-
-  return;
-}
-
-void Test_sh_util_base64_enc_ok (CuTest *tc) {
-  unsigned char   out[64];
-  unsigned char   ou2[64];
-  int    ret;
-  unsigned char   inp0[64] = "";
-  unsigned char   inp1[64] = "A";
-  unsigned char   inp2[64] = "AB";
-  unsigned char   inp3[64] = "ABC";
-  unsigned char   inp4[64] = "ABCD";
-
-  ret = sh_util_base64_enc (out, inp0, strlen((char*)inp0));
-  CuAssertIntEquals(tc, 0, ret);
-  CuAssertStrEquals(tc, "", (char*)out);
-  ret = sh_util_base64_dec (ou2, out, strlen((char*)out));
-  CuAssertIntEquals(tc, ret, 0);
-  CuAssertStrEquals(tc, (char*)inp0, (char*)ou2);
-
-  ret = sh_util_base64_enc (out, inp1, strlen((char*)inp1));
-  CuAssertIntEquals(tc, ret, 4);
-  CuAssertStrEquals(tc, "QQ??", (char*)out);
-  ret = sh_util_base64_dec (ou2, out, strlen((char*)out));
-  CuAssertStrEquals(tc, (char*)inp1, (char*)ou2);
-  CuAssertIntEquals(tc, 1, ret);
-
-  ret = sh_util_base64_enc (out, inp2, strlen((char*)inp2));
-  CuAssertIntEquals(tc, ret, 4);
-  CuAssertStrEquals(tc, "QUI?", (char*)out);
-  ret = sh_util_base64_dec (ou2, out, strlen((char*)out));
-  CuAssertStrEquals(tc, (char*)inp2, (char*)ou2);
-  CuAssertIntEquals(tc, 2, ret);
-
-  ret = sh_util_base64_enc (out, inp3, strlen((char*)inp3));
-  CuAssertIntEquals(tc, ret, 4);
-  CuAssertStrEquals(tc, "QUJD", (char*)out);
-  ret = sh_util_base64_dec (ou2, out, strlen((char*)out));
-  CuAssertStrEquals(tc, (char*)inp3, (char*)ou2);
-  CuAssertIntEquals(tc, 3, ret);
-
-  ret = sh_util_base64_enc (out, inp4, strlen((char*)inp4));
-  CuAssertIntEquals(tc, ret, 8);
-  CuAssertStrEquals(tc, "QUJDRA??", (char*)out);
-  ret = sh_util_base64_dec (ou2, out, strlen((char*)out));
-  CuAssertStrEquals(tc, (char*)inp4, (char*)ou2);
-  CuAssertIntEquals(tc, 4, ret);
-
-
-  return;
-}
-
-void Test_sh_util_dirname_ok (CuTest *tc) {
-  char * ret = 0;
-
-  char input0[] = "/foo/bar";
-  char res0[] = "/foo";
-
-  char input1[] = "/foo/bar/";
-  char res1[] = "/foo";
-
-  char input2[] = "/foo";
-  char res2[] = "/";
-
-  char input3[] = "/";
-  char res3[] = "/";
-
-  char input4[] = "///foo//bar";
-  char res4[] = "///foo";
-
-  char input5[] = "//foo///bar///";
-  char res5[] = "//foo";
-
-  char input6[] = "///";
-  char res6[] = "///";
-
-  char input7[] = "//f///b///";
-  char res7[] = "//f";
-
-  char input8[] = "/f/b/";
-  char res8[] = "/f";
-
-  char input9[] = "/e/b";
-  char res9[] = "/e";
-
-  ret = sh_util_dirname(input0);
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, res0, ret);
-
-  ret = sh_util_dirname(input1);
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, res1, ret);
-
-  ret = sh_util_dirname(input2);
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, res2, ret);
-
-  ret = sh_util_dirname(input3);
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, res3, ret);
-
-  ret = sh_util_dirname(input4);
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, res4, ret);
-
-  ret = sh_util_dirname(input5);
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, res5, ret);
-
-  ret = sh_util_dirname(input6);
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, res6, ret);
-
-  ret = sh_util_dirname(input7);
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, res7, ret);
-
-  ret = sh_util_dirname(input8);
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, res8, ret);
-
-  ret = sh_util_dirname(input9);
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, res9, ret);
-  return;
-}
-
-void Test_sh_util_basename_ok (CuTest *tc) {
-  char * ret = 0;
-
-  char input0[] = "/foo/bar";
-  char res0[] = "bar";
-
-  char input1[] = "/foo/";
-  char res1[] = "foo";
-
-  char input2[] = "/foo";
-  char res2[] = "foo";
-
-  char input3[] = "/";
-  char res3[] = "/";
-
-  char input4[] = "/foo/bar/";
-  char res4[] = "bar";
-
-  char input5[] = "/foo///bar///";
-  char res5[] = "bar";
-
-  char input6[] = "//foo";
-  char res6[] = "foo";
-
-  ret = sh_util_basename(input0);
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, res0, ret);
-
-  ret = sh_util_basename(input1);
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, res1, ret);
-
-  ret = sh_util_basename(input2);
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, res2, ret);
-
-  ret = sh_util_basename(input3);
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, res3, ret);
-
-  ret = sh_util_basename(input4);
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, res4, ret);
-
-  ret = sh_util_basename(input5);
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, res5, ret);
-
-  ret = sh_util_basename(input6);
-  CuAssertPtrNotNull(tc, ret);
-  CuAssertStrEquals(tc, res6, ret);
-
-  return;
-}
-
-void Test_sh_util_utf8_ok (CuTest *tc) {
-  int ret = 0;
-#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
-  unsigned char seq[16];
-  unsigned char input[16] = "foobar";
-
-  seq[0] = 0x00;
-  ret = sh_util_valid_utf8(seq);
-  CuAssertIntEquals(tc, ret, S_TRUE);
-
-  seq[0] = 0xd7; seq[1] = 0x90; seq[2] = 0x00;
-  ret = sh_util_valid_utf8(seq);
-  CuAssertIntEquals(tc, ret, S_TRUE);
-
-  seq[0] = 0xed; seq[1] = 0x9f; seq[2] = 0xbf; seq[3] = 0x00;
-  ret = sh_util_valid_utf8(seq);
-  CuAssertIntEquals(tc, ret, S_TRUE);
-
-  seq[0] = 0xee; seq[1] = 0x80; seq[2] = 0x80; seq[3] = 0x00;
-  ret = sh_util_valid_utf8(seq);
-  CuAssertIntEquals(tc, ret, S_TRUE);
-
-  seq[0] = 0xef; seq[1] = 0xbf; seq[2] = 0xbd; seq[3] = 0x00;
-  ret = sh_util_valid_utf8(seq);
-  CuAssertIntEquals(tc, ret, S_TRUE);
-
-  seq[0] = 0xf4; seq[1] = 0x8f; seq[2] = 0xbf; seq[3] = 0xbf; seq[4] = 0x00;
-  ret = sh_util_valid_utf8(seq);
-  CuAssertIntEquals(tc, ret, S_TRUE);
-
-  seq[0] = 0xf4; seq[1] = 0x90; seq[2] = 0x80; seq[3] = 0x80; seq[4] = 0x00;
-  ret = sh_util_valid_utf8(seq);
-  CuAssertIntEquals(tc, ret, S_TRUE);
-
-  seq[0] = 0xd7; seq[1] = 0x90; seq[2] = 0xd7; seq[3] = 0x90; seq[4] = 0x00;
-  ret = sh_util_valid_utf8(seq);
-  CuAssertIntEquals(tc, ret, S_TRUE);
-
-  /* cont. char */
-
-  seq[0] = 0x80; seq[1] = 0x00; 
-  ret = sh_util_valid_utf8(seq);
-  CuAssertIntEquals(tc, ret, S_FALSE);
-
-  seq[0] = 0xbf; seq[1] = 0x00; 
-  ret = sh_util_valid_utf8(seq);
-  CuAssertIntEquals(tc, ret, S_FALSE);
-
-  /* overlong */
-
-  seq[0] = 0xc0; seq[1] = 0xaf; seq[2] = 0x00;  
-  ret = sh_util_valid_utf8(seq);
-  CuAssertIntEquals(tc, ret, S_FALSE);
-
-  seq[0] = 0xe0; seq[1] = 0x8f; seq[2] = 0xaf;  seq[3] = 0x00;  
-  ret = sh_util_valid_utf8(seq);
-  CuAssertIntEquals(tc, ret, S_FALSE);
-
-  seq[0] = 0xf0; seq[1] = 0x80; seq[2] = 0x80;  seq[3] = 0xaf; seq[4] = 0x00;  
-  ret = sh_util_valid_utf8(seq);
-  CuAssertIntEquals(tc, ret, S_FALSE);
-
-  /* overlong */
-
-  seq[0] = 0xc1; seq[1] = 0xbf; seq[2] = 0x00;  
-  ret = sh_util_valid_utf8(seq);
-  CuAssertIntEquals(tc, ret, S_FALSE);
-
-  seq[0] = 0xe0; seq[1] = 0x9f; seq[2] = 0xbf;  seq[3] = 0x00;  
-  ret = sh_util_valid_utf8(seq);
-  CuAssertIntEquals(tc, ret, S_FALSE);
-
-  seq[0] = 0xf0; seq[1] = 0x8f; seq[2] = 0xbf;  seq[3] = 0xbf; seq[4] = 0x00;  
-  ret = sh_util_valid_utf8(seq);
-  CuAssertIntEquals(tc, ret, S_FALSE);
-
-  /* overlong */
-
-  seq[0] = 0xc0; seq[1] = 0x80; seq[2] = 0x00;  
-  ret = sh_util_valid_utf8(seq);
-  CuAssertIntEquals(tc, ret, S_FALSE);
-
-  seq[0] = 0xe0; seq[1] = 0x80; seq[2] = 0x80;  seq[3] = 0x00;  
-  ret = sh_util_valid_utf8(seq);
-  CuAssertIntEquals(tc, ret, S_FALSE);
-
-  seq[0] = 0xf0; seq[1] = 0x80; seq[2] = 0x80;  seq[3] = 0x80; seq[4] = 0x00;  
-  ret = sh_util_valid_utf8(seq);
-  CuAssertIntEquals(tc, ret, S_FALSE);
-
-  /* cont missing */
-
-  seq[0] = 0xd7; seq[1] = 0x20; seq[3] = 0x00;
-  ret = sh_util_valid_utf8(seq);
-  CuAssertIntEquals(tc, ret, S_FALSE);
-
-  seq[0] = 0xee; seq[1] = 0x80; seq[2] = 0x20; seq[3] = 0x00;
-  ret = sh_util_valid_utf8(seq);
-  CuAssertIntEquals(tc, ret, S_FALSE);
-
-  seq[0] = 0xf4; seq[1] = 0x8f; seq[2] = 0xbf; seq[3] = 0x20; seq[4] = 0x00;
-  ret = sh_util_valid_utf8(seq);
-  CuAssertIntEquals(tc, ret, S_FALSE);
-
-  /* switch on utf8 checking for sh_util_obscurename() */
-
-  ret = sh_util_obscure_utf8("Y");
-  CuAssertIntEquals(tc, ret, 0);
-
-  ret = sh_util_obscure_ok ("0x01,0x02,0x03");
-  CuAssertIntEquals(tc, ret, 0);
-
-  ret = sh_util_valid_utf8 (input);
-  CuAssertIntEquals(tc, ret, S_TRUE);
-  ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
-  CuAssertIntEquals(tc, ret, 0);
-
-  input[0] = '\t';
-  ret = sh_util_valid_utf8 (input);
-  CuAssertIntEquals(tc, ret, S_FALSE);
-  ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
-  CuAssertIntEquals(tc, ret, -1);
-
-  input[0] = 0x01;
-  ret = sh_util_valid_utf8 (input);
-  CuAssertIntEquals(tc, ret, S_TRUE);
-  ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
-  CuAssertIntEquals(tc, ret, 0);
-
-  input[0] = 0x02;
-  ret = sh_util_valid_utf8 (input);
-  CuAssertIntEquals(tc, ret, S_TRUE);
-  ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
-  CuAssertIntEquals(tc, ret, 0);
-
-  input[0] = 0x03;
-  ret = sh_util_valid_utf8 (input);
-  CuAssertIntEquals(tc, ret, S_TRUE);
-  ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
-  CuAssertIntEquals(tc, ret, 0);
-
-  input[0] = 0x04;
-  ret = sh_util_valid_utf8 (input);
-  CuAssertIntEquals(tc, ret, S_FALSE);
-  ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
-  CuAssertIntEquals(tc, ret, -1);
-
-  input[0] = 'f';
-  ret = sh_util_valid_utf8 (input);
-  CuAssertIntEquals(tc, ret, S_TRUE);
-  ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
-  CuAssertIntEquals(tc, ret, 0);
-
-  input[5] = ' ';
-  ret = sh_util_valid_utf8 (input);
-  CuAssertIntEquals(tc, ret, S_FALSE);
-  ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
-  CuAssertIntEquals(tc, ret, -1);
-
-  input[5] = 'r'; input[3] = ' ';
-  ret = sh_util_valid_utf8 (input);
-  CuAssertIntEquals(tc, ret, S_TRUE);
-  ret = sh_util_obscurename (0, (char *)input, S_FALSE /* no log message */);
-  CuAssertIntEquals(tc, ret, 0);
-
-
-#else
-  CuAssertIntEquals(tc, ret, 0);
-#endif
-}
-
-void Test_sh_util_obscure_ok (CuTest *tc) {
-
-  int ret = 0;
-#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
-  char input[16] = "foobar";
-
-  /* switch off utf8 checking for sh_util_obscurename() */
-
-  ret = sh_util_obscure_utf8("N");
-  CuAssertIntEquals(tc, ret, 0);
-
-  ret = sh_util_obscure_ok ("0xA1,0xA2,0xA3");
-  CuAssertIntEquals(tc, ret, 0);
-
-  ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
-  CuAssertIntEquals(tc, ret, 0);
-
-  input[0] = '\t';
-  ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
-  CuAssertIntEquals(tc, ret, -1);
-
-  input[0] = 0xA1;
-  ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
-  CuAssertIntEquals(tc, ret, 0);
-
-  input[0] = 0xA2;
-  ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
-  CuAssertIntEquals(tc, ret, 0);
-
-  input[0] = 0xA3;
-  ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
-  CuAssertIntEquals(tc, ret, 0);
-
-  input[0] = 0xA4;
-  ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
-  CuAssertIntEquals(tc, ret, -1);
-
-  input[0] = 'f';
-  ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
-  CuAssertIntEquals(tc, ret, 0);
-
-  input[5] = ' ';
-  ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
-  CuAssertIntEquals(tc, ret, -1);
-
-  input[5] = 'r'; input[3] = ' ';
-  ret = sh_util_obscurename (0, input, S_FALSE /* no log message */);
-  CuAssertIntEquals(tc, ret, 0);
-#else
-  CuAssertIntEquals(tc, ret, 0);
-#endif
-}
-
Index: trunk/src/cutest_slib.c
===================================================================
--- trunk/src/cutest_slib.c	(revision 591)
+++ 	(revision )
@@ -1,135 +1,0 @@
-
-#include "config_xor.h"
-
-#include <string.h>
-#include "CuTest.h"
-#include "samhain.h"
-
-void Test_sl_stale (CuTest *tc) {
-
-  extern int get_the_fd (SL_TICKET ticket);
-
-  int       fd1, fd2, ret, line, val;
-  SL_TICKET tfd1, tfd2;
-  char *    err1;
-  char      err2[128];
-
-  line = __LINE__; tfd1 = sl_open_read(__FILE__, __LINE__, "/etc/group", SL_NOPRIV);
-  CuAssertTrue(tc, tfd1 > 0);
-
-  fd1 = get_the_fd(tfd1);
-  CuAssertTrue(tc, fd1 >= 0);
-
-  ret = close(fd1);
-  CuAssertTrue(tc, ret == 0);
-
-  tfd2 = sl_open_read(__FILE__, __LINE__, "/etc/group", SL_NOPRIV);
-  CuAssertTrue(tc, tfd2 > 0);
-  CuAssertTrue(tc, tfd2 != tfd1);
-
-  fd2 = get_the_fd(tfd2);
-  CuAssertIntEquals(tc, fd1, fd2);
-
-  err1 = sl_check_stale();
-  CuAssertTrue(tc, err1 != NULL);
-
-  sl_snprintf(err2, sizeof(err2), 
-	      "stale handle, %s, %d", __FILE__, line);
-  val = strcmp(err1, err2);
-  CuAssertIntEquals(tc, 0, val);
-}
-
-void Test_sl_snprintf (CuTest *tc) {
-
-  int ret = 0;
-  char input[16];
-
-  memset (&input, 'X', 16);
-  ret = sl_snprintf(input, 10, "%s\n", "01234567890123456789");
-  CuAssertIntEquals(tc, ret, 0);
-  CuAssertTrue(tc, input[9]  == '\0');
-  CuAssertTrue(tc, input[10] == 'X');
-
-  memset (&input, 'X', 16);
-  ret = sl_snprintf(input, 4, "%d\n", "012345");
-  CuAssertIntEquals(tc, ret, 0);
-  CuAssertTrue(tc, input[3] == '\0');
-  CuAssertTrue(tc, input[4] == 'X');
-}
-
-void Test_sl_ts_strncmp (CuTest *tc) {
-  char one[64], two[64];
-  int  res;
-
-  strcpy(one, "foo");
-  strcpy(two, "foo");
-  res = sl_ts_strncmp(one, two, 3);
-  CuAssertIntEquals(tc, 0, res);
-
-  strcpy(one, "fox");
-  strcpy(two, "foo");
-  res = sl_ts_strncmp(one, two, 2);
-  CuAssertIntEquals(tc, 0, res);
-  
-  strcpy(one, "f9o");
-  strcpy(two, "foo");
-  res = sl_ts_strncmp(one, two, 3);
-  CuAssertTrue(tc, 0 != res);
-
-}
-
-void Test_sl_strcasecmp (CuTest *tc) {
-  char one[64], two[64];
-  int  res;
-
-  strcpy(one, "foo");
-  strcpy(two, "foo");
-  res = sl_strcasecmp(one, two);
-  CuAssertIntEquals(tc, 0, res);
-
-  strcpy(one, "fo");
-  strcpy(two, "foo");
-  res = sl_strcasecmp(one, two);
-  CuAssertIntEquals(tc, -1, res);
-
-  strcpy(one, "foo");
-  strcpy(two, "fo");
-  res = sl_strcasecmp(one, two);
-  CuAssertIntEquals(tc, 1, res);
-
-  strcpy(one, "1234");
-  strcpy(two, "2345");
-  res = sl_strcasecmp(one, two);
-  CuAssertIntEquals(tc, -1, res);
-
-  strcpy(one, "234");
-  strcpy(two, "123");
-  res = sl_strcasecmp(one, two);
-  CuAssertIntEquals(tc, 1, res);
-
-  strcpy(one, "");
-  strcpy(two, "123");
-  res = sl_strcasecmp(one, two);
-  CuAssertIntEquals(tc, -1, res);
-
-  strcpy(one, "234");
-  strcpy(two, "");
-  res = sl_strcasecmp(one, two);
-  CuAssertIntEquals(tc, 1, res);
-
-  strcpy(one, "");
-  strcpy(two, "");
-  res = sl_strcasecmp(one, two);
-  CuAssertTrue(tc, res == 0);
-
-#ifndef SL_FAIL_ON_ERROR
-  res = sl_strcasecmp(NULL, two);
-  CuAssertIntEquals(tc, -1, res);
-
-  res = sl_strcasecmp(one, NULL);
-  CuAssertIntEquals(tc, 1, res);
-
-  res = sl_strcasecmp(NULL, NULL);
-  CuAssertTrue(tc, res != 0);
-#endif
-}
Index: trunk/src/cutest_zAVLTree.c
===================================================================
--- trunk/src/cutest_zAVLTree.c	(revision 591)
+++ 	(revision )
@@ -1,523 +1,0 @@
-
-#include "config_xor.h"
-
-#include <string.h>
-#include "CuTest.h"
-#include "zAVLTree.h"
-
-struct ztest {
-  char name[32];
-  int  iname;
-};
-
-static zAVLTree * ztest_tree   = NULL;
-
-static zAVLKey ztest_key (void const * arg)
-{
-  const struct ztest * sa = (const struct ztest *) arg;
-  return (zAVLKey) sa->name;
-}
-
-static zAVLKey ztest_intkey(void const *item)
-{
-  return (&((const struct ztest *)item)->iname);
-}
-
-
-static void free_node (void * inptr)
-{
-  struct ztest * ptr = (struct ztest *) inptr;
-  ptr->name[0] = '\0';
-  ptr->iname   = 0;
-}
-
-void Test_zAVLTree(CuTest *tc) {
-  zAVLCursor    cursor;
-
-  int result;
-  int counter = 0;
-
-  char * str;
-
-  struct ztest z1 = { "abc"  , 1 };
-  struct ztest z2 = { "aac"  , 2 };
-  struct ztest z3 = { "aaa1" , 3 };
-  struct ztest z4 = { "aaa3" , 4 };
-  struct ztest z5 = { "aaa2" , 5 };
-  struct ztest z6 = { "aaa6" , 6 };
-  struct ztest z7 = { "aaa5" , 7 };
-  struct ztest z8 = { "aaa4" , 8 };
-
-  struct ztest iz1 = { "aaa1" , 8 };
-  struct ztest iz2 = { "aaa2" , 7 };
-  struct ztest iz3 = { "aaa3" , 1 };
-  struct ztest iz4 = { "aaa4" , 3 };
-  struct ztest iz5 = { "aaa5" , 2 };
-  struct ztest iz6 = { "aaa5" , 6 };
-  struct ztest iz7 = { "aaa7" , 5 };
-  struct ztest iz8 = { "aaa8" , 4 };
-
-  struct ztest * ptr;
-
-  ptr = zAVLFirst(&cursor, ztest_tree);
-  CuAssertTrue(tc, NULL == ptr);
-
-  ztest_tree = zAVLAllocTree (ztest_key, zAVL_KEY_STRING);
-  CuAssertPtrNotNull(tc, ztest_tree);
-
-  do {
-
-  ++counter;
-
-  result = zAVLInsert(ztest_tree, &z1);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &z2);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &z3);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &z4);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &z5);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &z6);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &z7);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &z8);
-  CuAssertTrue(tc, 0 == result);
-
-  ptr = zAVLFirst(&cursor, ztest_tree);
-  CuAssertStrEquals(tc, ptr->name, z3.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, z5.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, z4.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, z8.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, z7.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, z6.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, z2.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, z1.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLInsert(ztest_tree, &z8);
-  CuAssertTrue(tc, 0 != result);
-  result = zAVLInsert(ztest_tree, &z7);
-  CuAssertTrue(tc, 0 != result);
-  result = zAVLInsert(ztest_tree, &z6);
-  CuAssertTrue(tc, 0 != result);
-  result = zAVLInsert(ztest_tree, &z5);
-  CuAssertTrue(tc, 0 != result);
-
-  ptr = zAVLSearch(ztest_tree, z1.name);
-  CuAssertStrEquals(tc, ptr->name, z1.name);
-  ptr = zAVLSearch(ztest_tree, z2.name);
-  CuAssertStrEquals(tc, ptr->name, z2.name);
-  ptr = zAVLSearch(ztest_tree, z3.name);
-  CuAssertStrEquals(tc, ptr->name, z3.name);
-  ptr = zAVLSearch(ztest_tree, z4.name);
-  CuAssertStrEquals(tc, ptr->name, z4.name);
-
-  ptr = zAVLFirst(&cursor, ztest_tree);
-  CuAssertStrEquals(tc, ptr->name, z3.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, z5.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, z4.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, z8.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, z7.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, z6.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, z2.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, z1.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertTrue(tc, NULL == ptr);
-
-
-  ptr = zAVLSearch(ztest_tree, z5.name);
-  CuAssertStrEquals(tc, ptr->name, z5.name);
-  ptr = zAVLSearch(ztest_tree, z6.name);
-  CuAssertStrEquals(tc, ptr->name, z6.name);
-  ptr = zAVLSearch(ztest_tree, z7.name);
-  CuAssertStrEquals(tc, ptr->name, z7.name);
-  ptr = zAVLSearch(ztest_tree, z8.name);
-  CuAssertStrEquals(tc, ptr->name, z8.name);
-  ptr = zAVLSearch(ztest_tree, "foobar");
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLDelete(ztest_tree, z8.name);
-  CuAssertTrue(tc, 0 == result);
-  ptr = zAVLSearch(ztest_tree, z8.name);
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLDelete(ztest_tree, z3.name);
-  CuAssertTrue(tc, 0 == result);
-  ptr = zAVLSearch(ztest_tree, z3.name);
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLDelete(ztest_tree, z1.name);
-  CuAssertTrue(tc, 0 == result);
-  ptr = zAVLSearch(ztest_tree, z1.name);
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLInsert(ztest_tree, &z1);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &z8);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &z3);
-  CuAssertTrue(tc, 0 == result);
-
-  ptr = zAVLFirst(&cursor, ztest_tree);
-  CuAssertStrEquals(tc, ptr->name, z3.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, z5.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, z4.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, z8.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, z7.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, z6.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, z2.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, z1.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLDelete(ztest_tree, z1.name);
-  CuAssertTrue(tc, 0 == result);
-  ptr = zAVLSearch(ztest_tree, z1.name);
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLDelete(ztest_tree, z2.name);
-  CuAssertTrue(tc, 0 == result);
-  ptr = zAVLSearch(ztest_tree, z2.name);
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLDelete(ztest_tree, z3.name);
-  CuAssertTrue(tc, 0 == result);
-  ptr = zAVLSearch(ztest_tree, z3.name);
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLDelete(ztest_tree, z4.name);
-  CuAssertTrue(tc, 0 == result);
-  ptr = zAVLSearch(ztest_tree, z4.name);
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLDelete(ztest_tree, z5.name);
-  CuAssertTrue(tc, 0 == result);
-  ptr = zAVLSearch(ztest_tree, z5.name);
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLDelete(ztest_tree, z6.name);
-  CuAssertTrue(tc, 0 == result);
-  ptr = zAVLSearch(ztest_tree, z6.name);
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLDelete(ztest_tree, z7.name);
-  CuAssertTrue(tc, 0 == result);
-  ptr = zAVLSearch(ztest_tree, z7.name);
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLDelete(ztest_tree, z8.name);
-  CuAssertTrue(tc, 0 == result);
-  ptr = zAVLSearch(ztest_tree, z8.name);
-  CuAssertTrue(tc, NULL == ptr);
-
-} while (counter < 100);
-
-  result = zAVLInsert(ztest_tree, &z1);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &z2);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &z3);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &z4);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &z5);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &z6);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &z7);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &z8);
-  CuAssertTrue(tc, 0 == result);
-
-  zAVLFreeTree (ztest_tree, free_node);
-  CuAssertTrue (tc, z1.name[0] == '\0');
-  CuAssertTrue (tc, z2.name[0] == '\0');
-  CuAssertTrue (tc, z3.name[0] == '\0');
-  CuAssertTrue (tc, z4.name[0] == '\0');
-  CuAssertTrue (tc, z5.name[0] == '\0');
-  CuAssertTrue (tc, z6.name[0] == '\0');
-  CuAssertTrue (tc, z7.name[0] == '\0');
-  CuAssertTrue (tc, z8.name[0] == '\0');
-
-
-  /* Numeric key here */
-
-  counter    = 0;
-  ztest_tree = NULL;
-
-  ptr = zAVLFirst(&cursor, ztest_tree);
-  CuAssertTrue(tc, NULL == ptr);
-
-  ztest_tree = zAVLAllocTree (ztest_intkey, zAVL_KEY_INT);
-  CuAssertPtrNotNull(tc, ztest_tree);
-
-  do {
-
-  ++counter;
-
-  result = zAVLInsert(ztest_tree, &iz1);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &iz2);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &iz3);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &iz4);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &iz5);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &iz6);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &iz7);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &iz8);
-  CuAssertTrue(tc, 0 == result);
-
-  ptr = zAVLFirst(&cursor, ztest_tree);
-  CuAssertStrEquals(tc, ptr->name, iz3.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, iz5.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, iz4.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, iz8.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, iz7.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, iz6.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, iz2.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, iz1.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLInsert(ztest_tree, &iz8);
-  CuAssertTrue(tc, 0 != result);
-  result = zAVLInsert(ztest_tree, &iz7);
-  CuAssertTrue(tc, 0 != result);
-  result = zAVLInsert(ztest_tree, &iz6);
-  CuAssertTrue(tc, 0 != result);
-  result = zAVLInsert(ztest_tree, &iz5);
-  CuAssertTrue(tc, 0 != result);
-
-  ptr = zAVLSearch(ztest_tree, &(iz1.iname));
-  CuAssertIntEquals(tc, ptr->iname, iz1.iname);
-  ptr = zAVLSearch(ztest_tree, &(iz2.iname));
-  CuAssertIntEquals(tc, ptr->iname, iz2.iname);
-  ptr = zAVLSearch(ztest_tree, &(iz3.iname));
-  CuAssertIntEquals(tc, ptr->iname, iz3.iname);
-  ptr = zAVLSearch(ztest_tree, &(iz6.iname));
-  CuAssertIntEquals(tc, ptr->iname, iz6.iname);
-  ptr = zAVLSearch(ztest_tree, &(iz4.iname));
-  CuAssertIntEquals(tc, ptr->iname, iz4.iname);
-
-  ptr = zAVLSearch(ztest_tree, &(iz2.iname));
-  CuAssertIntEquals(tc, ptr->iname, iz2.iname);
-  ptr = zAVLSearch(ztest_tree, &(iz3.iname));
-  CuAssertIntEquals(tc, ptr->iname, iz3.iname);
-  ptr = zAVLSearch(ztest_tree, &(iz7.iname));
-  CuAssertIntEquals(tc, ptr->iname, iz7.iname);
-
-  ptr = zAVLFirst(&cursor, ztest_tree);
-  CuAssertStrEquals(tc, ptr->name, iz3.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, iz5.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, iz4.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, iz8.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, iz7.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, iz6.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, iz2.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, iz1.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertTrue(tc, NULL == ptr);
-
-
-  ptr = zAVLSearch(ztest_tree, &(iz5.iname));
-  CuAssertStrEquals(tc, ptr->name, iz5.name);
-  ptr = zAVLSearch(ztest_tree, &(iz6.iname));
-  CuAssertStrEquals(tc, ptr->name, iz6.name);
-  ptr = zAVLSearch(ztest_tree, &(iz7.iname));
-  CuAssertStrEquals(tc, ptr->name, iz7.name);
-  ptr = zAVLSearch(ztest_tree, &(iz8.iname));
-  CuAssertStrEquals(tc, ptr->name, iz8.name);
-  ptr = zAVLSearch(ztest_tree, &(z1.iname)); /* been set to 0 */
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLDelete(ztest_tree, &(iz8.iname));
-  CuAssertTrue(tc, 0 == result);
-  ptr = zAVLSearch(ztest_tree, &(iz8.iname));
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLDelete(ztest_tree, &(iz3.iname));
-  CuAssertTrue(tc, 0 == result);
-  ptr = zAVLSearch(ztest_tree, &(iz3.iname));
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLDelete(ztest_tree, &(iz1.iname));
-  CuAssertTrue(tc, 0 == result);
-  ptr = zAVLSearch(ztest_tree, &(iz1.iname));
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLInsert(ztest_tree, &iz1);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &iz8);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &iz3);
-  CuAssertTrue(tc, 0 == result);
-
-  ptr = zAVLFirst(&cursor, ztest_tree);
-  CuAssertIntEquals(tc, ptr->iname, iz3.iname);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, iz5.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, iz4.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertIntEquals(tc, ptr->iname, iz8.iname);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, iz7.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, iz6.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertIntEquals(tc, ptr->iname, iz2.iname);
-  ptr = zAVLNext(&cursor);
-  CuAssertStrEquals(tc, ptr->name, iz1.name);
-  ptr = zAVLNext(&cursor);
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLDelete(ztest_tree, &(iz1.iname));
-  CuAssertTrue(tc, 0 == result);
-  ptr = zAVLSearch(ztest_tree, &(iz1.iname));
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLDelete(ztest_tree, &(iz2.iname));
-  CuAssertTrue(tc, 0 == result);
-  ptr = zAVLSearch(ztest_tree, &(iz2.iname));
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLDelete(ztest_tree, &(iz3.iname));
-  CuAssertTrue(tc, 0 == result);
-  ptr = zAVLSearch(ztest_tree, &(iz3.iname));
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLDelete(ztest_tree, &(iz4.iname));
-  CuAssertTrue(tc, 0 == result);
-  ptr = zAVLSearch(ztest_tree, &(iz4.iname));
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLDelete(ztest_tree, &(iz5.iname));
-  CuAssertTrue(tc, 0 == result);
-  ptr = zAVLSearch(ztest_tree, &(iz5.iname));
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLDelete(ztest_tree, &(iz6.iname));
-  CuAssertTrue(tc, 0 == result);
-  ptr = zAVLSearch(ztest_tree, &(iz6.iname));
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLDelete(ztest_tree, &(iz7.iname));
-  CuAssertTrue(tc, 0 == result);
-  ptr = zAVLSearch(ztest_tree, &(iz7.iname));
-  CuAssertTrue(tc, NULL == ptr);
-
-  result = zAVLDelete(ztest_tree, &(iz8.iname));
-  CuAssertTrue(tc, 0 == result);
-  ptr = zAVLSearch(ztest_tree, &(iz8.iname));
-  CuAssertTrue(tc, NULL == ptr);
-
-} while (counter < 100);
-
-  result = zAVLInsert(ztest_tree, &iz1);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &iz2);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &iz3);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &iz4);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &iz5);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &iz6);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &iz7);
-  CuAssertTrue(tc, 0 == result);
-  result = zAVLInsert(ztest_tree, &iz8);
-  CuAssertTrue(tc, 0 == result);
-
-  zAVLFreeTree (ztest_tree, free_node);
-  CuAssertTrue (tc, iz1.iname == 0);
-  CuAssertTrue (tc, iz2.iname == 0);
-  CuAssertTrue (tc, iz3.iname == 0);
-  CuAssertTrue (tc, iz4.iname == 0);
-  CuAssertTrue (tc, iz5.iname == 0);
-  CuAssertTrue (tc, iz6.iname == 0);
-  CuAssertTrue (tc, iz7.iname == 0);
-  CuAssertTrue (tc, iz8.iname == 0);
-
-  ztest_tree = NULL;
-  str = strdup("foo");
-  result = zAVL_string_set(&ztest_tree, str);
-  CuAssertTrue(tc, 0 == result);
-  CuAssertPtrNotNull(tc, ztest_tree);
-  CuAssertStrEquals(tc, "foo", zAVL_string_get(ztest_tree, "foo")); 
-
-  str = strdup("bar");
-  result = zAVL_string_set(&ztest_tree, str);
-  CuAssertTrue(tc, 0 == result);
-  CuAssertStrEquals(tc, "foo", zAVL_string_get(ztest_tree, "foo")); 
-  CuAssertStrEquals(tc, "bar", zAVL_string_get(ztest_tree, "bar")); 
-
-  str = strdup("balloon");
-  result = zAVL_string_set(&ztest_tree, str);
-  CuAssertTrue(tc, 0 == result);
-  CuAssertStrEquals(tc, "foo", zAVL_string_get(ztest_tree, "foo")); 
-  CuAssertStrEquals(tc, "bar", zAVL_string_get(ztest_tree, "bar")); 
-  CuAssertStrEquals(tc, "balloon", zAVL_string_get(ztest_tree, "balloon")); 
-
-  str = zAVL_string_get(ztest_tree, "foobar");
-  CuAssertTrue(tc, str == NULL);
-  str = zAVL_string_get(ztest_tree, "");
-  CuAssertTrue(tc, str == NULL);
-  str = zAVL_string_get(ztest_tree, NULL);
-  CuAssertTrue(tc, str == NULL);
-
-  zAVL_string_reset(ztest_tree);
-  ztest_tree = NULL;
-  str = zAVL_string_get(ztest_tree, "foo");
-  CuAssertTrue(tc, str == NULL);
-  str = zAVL_string_get(ztest_tree, "bar");
-  CuAssertTrue(tc, str == NULL);
-  str = zAVL_string_get(ztest_tree, "balloon");
-  CuAssertTrue(tc, str == NULL);
-
-}
Index: trunk/src/depend-gen.c
===================================================================
--- trunk/src/depend-gen.c	(revision 591)
+++ trunk/src/depend-gen.c	(revision 1)
@@ -197,8 +197,6 @@
   p = strrchr(name, '/');
   if (p)
-    {
-      ++p;
-      len = strlen(p);
-    }
+    ++p;
+  len = strlen(p);
 
   /* skip other dependencies
@@ -208,5 +206,5 @@
       if (NULL == fgets(line, 1023, fout))
 	break;
-      if (p && 0 == strncmp(line, p, len))
+      if (0 == strncmp(line, p, len))
 	break;
       fprintf(ftmp, "%s", line);
@@ -245,5 +243,5 @@
 	   *
 	   **************************************************/
-	  if (0 == strcmp(p, "sh_sig_chksum.h") ||
+	  if (0 == strcmp(p, "sh_gpg_chksum.h") ||
 	      0 == strcmp(p, "sh_gpg_fp.h"))
 	    {
Index: trunk/src/dnmalloc.c
===================================================================
--- trunk/src/dnmalloc.c	(revision 591)
+++ 	(revision )
@@ -1,5622 +1,0 @@
-/* DistriNet malloc (dnmalloc): a more secure memory allocator. 
-   Copyright (C) 2005, Yves Younan, Wouter Joosen, Frank Piessens 
-   and Rainer Wichmann
-
-   The authors can be contacted by:
-      Email: dnmalloc@fort-knox.org
-      Address:
-#if defined(WITH_TPT) 
-      	     Yves Younan
-      	     Celestijnenlaan 200A
-      	     B-3001 Heverlee
-      	     Belgium
-   
-   This library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2.1 of the License, or (at your option) any later version.
-
-   This library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
-   
-*/
-
-/* Current version: dnmalloc 1.0 */
-/* Includes arc4random from OpenBSD, which is under the BDS license     */
-
-/* Versions:
-   0.1-0.5:
-   Proof of concept implementation by Hans Van den Eynden and Yves Younan
-   0.6-0.7:
-   Bug fixes by Yves Younan
-   0.8-1.0.beta4:
-   Reimplementation from scratch by Yves Younan
-   1.0.beta4:
-   Public release
-   1.0.beta5:
-   Prev_chunkinfo speeded up, was really slow because of the way we did lookups
-   A freechunkinfo region is now freed when it is completely empty and 
-   not the current one
-
-   1.0 (Rainer Wichmann [support at la dash samhna dot org]):
-   ---------------------
-
-   Compiler warnings fixed
-   Define REALLOC_ZERO_BYTES_FREES because it's what GNU libc does
-       (and what the standard says)
-   Removed unused code
-   Fix       assert(aligned_OK(chunk(newp)));
-         ->  assert(aligned_OK(chunk(oldp)));
-   Fix statistics in sYSMALLOc
-   Fix overwrite of av->top in sYSMALLOc
-   Provide own assert(), glibc assert() doesn't work (calls malloc)
-   Fix bug in mEMALIGn(), put remainder in hashtable before calling fREe
-   Remove cfree, independent_cmalloc, independent_comalloc (untested
-       public functions not covered by any standard)
-   Provide posix_memalign (that one is in the standard)
-   Move the malloc_state struct to mmapped memory protected by guard pages 
-   Add arc4random function to initialize random canary on startup
-   Implement random canary at end of (re|m)alloced/memaligned buffer,
-       check at free/realloc
-   Remove code conditional on !HAVE_MMAP, since mmap is required anyway.
-   Use standard HAVE_foo macros (as generated by autoconf) instead of LACKS_foo
-
-   Profiling: Reorder branches in hashtable_add, next_chunkinfo, 
-                  prev_chunkinfo, hashtable_insert, mALLOc, fREe, request2size,
-	          checked_request2size (gcc predicts if{} branch to be taken).
-	      Use UNLIKELY macro (gcc __builtin_expect()) where branch
-                  reordering would make the code awkward.
-
-   Portability: Hashtable always covers full 32bit address space to
-                avoid assumptions about memory layout.
-   Portability: Try hard to enforce mapping of mmapped memory into
-                32bit address space, even on 64bit systems.
-   Portability: Provide a dnmalloc_pthread_init() function, since
-                pthread locking on HP-UX only works if initialized
-		after the application has entered main().
-   Portability: On *BSD, pthread_mutex_lock is unusable since it
-                calls malloc, use spinlocks instead.
-   Portability: Dynamically detect whether the heap is within
-                32bit address range (e.g. on Linux x86_64, it isn't).
-		Don't use sbrk() if the heap is mapped to an address 
-		outside the 32bit range, since this doesn't work with 
-		the hashtable. New macro morecore32bit.
-                
-   Success on: HP-UX 11.11/pthread, Linux/pthread (32/64 bit),
-               FreeBSD/pthread, and Solaris 10 i386/pthread.
-   Fail    on: OpenBSD/pthread (in  _thread_machdep_save_float_state),
-               might be related to OpenBSD pthread internals (??).
-	       Non-treaded version (#undef USE_MALLOC_LOCK) 
-	       works on OpenBSD.
-   
-   further to 1.0:
-   Valgrind client requests inserted (#define USE_VALGRIND)
-   Fix: malloc_consolidate (nextchunk->fd, nextchunk->bck may be NULL)
-   Portability: minsize = 32 bit on 64bit architecture
-   Minor cleanups
-   Fix: eliminate prototypes for memset, memcpy (they're in string.h)
-   Fix: one more malloc_consolidate segfault
-
-   There may be some bugs left in this version. please use with caution.
-*/
-
-
-
-/* Please read the following papers for documentation: 
-
-   Yves Younan, Wouter Joosen, and Frank Piessens, A Methodology for Designing
-   Countermeasures against Current and Future Code Injection Attacks,
-   Proceedings of the Third IEEE International Information Assurance
-   Workshop 2005 (IWIA2005), College Park, Maryland, U.S.A., March 2005,
-   IEEE, IEEE Press.
-   http://www.fort-knox.org/younany_countermeasures.pdf
-   
-   Yves Younan, Wouter Joosen and Frank Piessens and Hans Van den
-   Eynden. Security of Memory Allocators for C and C++. Technical Report
-   CW419, Departement Computerwetenschappen, Katholieke Universiteit
-   Leuven, July 2005. http://www.fort-knox.org/CW419.pdf
- 
- */
-
-/* Compile:
-   gcc -fPIC -rdynamic -c -Wall dnmalloc-portable.c
-   "Link": 
-   Dynamic:
-   gcc -shared -Wl,-soname,libdnmalloc.so.0 -o libdnmalloc.so.0.0 dnmalloc-portable.o -lc
-   Static:
-   ar -rv libdnmalloc.a dnmalloc-portable.o
-   
-*/
-
-/* 
-   dnmalloc is based on dlmalloc 2.7.2 (by Doug Lea (dl@cs.oswego.edu))
-   dlmalloc was released as public domain and contained the following license:
-   
-   "This is a version (aka dlmalloc) of malloc/free/realloc written by
-   Doug Lea and released to the public domain.  Use, modify, and
-   redistribute this code without permission or acknowledgement in any
-   way you wish.  Send questions, comments, complaints, performance
-   data, etc to dl@cs.oswego.edu
-   
-   * VERSION 2.7.2 Sat Aug 17 09:07:30 2002  Doug Lea  (dl at gee)
-   
-   Note: There may be an updated version of this malloc obtainable at
-   ftp://gee.cs.oswego.edu/pub/misc/malloc.c
-   Check before installing!"
-   
-*/
-
-/* The following preprocessor macros are tested, 
- *   and hence should have #define directives:
- *
- *   HAVE_CONFIG_H    Define to #include "config.h" (autoconf-generated)
- *
- *   HAVE_UNISTD_H    Define to #include <unistd.h>
- *
- *   HAVE_SYS_UIO_H   Define to #include <sys/uio.h> (for writev)
- *   HAVE_WRITEV      Define if the 'writev' function is available
- *
- *   HAVE_SYS_PARAM_H Define to #include <sys/param.h> (for pagesize)
- *
- *   HAVE_MALLOC_H    Define to #include <malloc.h> (for struct mallinfo2)
- *
- *   HAVE_FCNTL_H     Define to #include <fcntl.h>
- *
- *   HAVE_SYS_MMAN_H  Define to #include <sys/mman.h>
- *   HAVE_MMAP        Define if the 'mmap' function is available.
- *
- *   HAVE_SCHED_H     Define to #include <sched.h>
- *   HAVE_SCHED_YIELD Define id the 'sched_yield' function is available
- */
-
-
-/*
-  __STD_C should be nonzero if using ANSI-standard C compiler, a C++
-  compiler, or a C compiler sufficiently close to ANSI to get away
-  with it.
-*/
-
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#ifdef USE_VALGRIND
-#include <valgrind/memcheck.h>
-#else
-#define VALGRIND_FREELIKE_BLOCK(a,b)       ((void)0)
-#define VALGRIND_MALLOCLIKE_BLOCK(a,b,c,d) ((void)0)
-#define VALGRIND_CREATE_MEMPOOL(a,b,c)     ((void)0)
-#define VALGRIND_MEMPOOL_ALLOC(a,b,c)      ((void)0)
-#define VALGRIND_MEMPOOL_FREE(a,b)         ((void)0)
-#define VALGRIND_DESTROY_MEMPOOL(a)        ((void)0)
-#define VALGRIND_MAKE_MEM_DEFINED(a,b)     ((void)0)
-#define VALGRIND_MAKE_MEM_UNDEFINED(a,b)   ((void)0)
-#define VALGRIND_MAKE_MEM_NOACCESS(a,b)    ((void)0)
-#endif
-
-#if defined (__GNUC__) && __GNUC__ > 2
-# define LIKELY(expression) (__builtin_expect(!!(expression), 1))
-# define UNLIKELY(expression) (__builtin_expect(!!(expression), 0))
-# define __attribute_malloc__ __attribute__ ((__malloc__))
-#else
-# define LIKELY(x)       (x)
-# define UNLIKELY(x)     (x)
-# define __attribute_malloc__ /* Ignore */
-#endif
-
-/*
-  Define HAVE_MREMAP to make realloc() use mremap() to re-allocate
-  large blocks.  This is currently only possible on Linux with
-  kernel versions newer than 1.3.77.
-*/
-
-#ifndef HAVE_MREMAP
-#ifdef linux
-#define HAVE_MREMAP 1
-#define _GNU_SOURCE 1
-#else
-#define HAVE_MREMAP 0
-#endif
-#endif /* HAVE_MREMAP */
-
-
-
-#ifndef __STD_C
-#if defined(__STDC__) || defined(_cplusplus)
-#define __STD_C     1
-#else
-#define __STD_C     0
-#endif 
-#endif /*__STD_C*/
-
-
-/*
-  Void_t* is the pointer type that malloc should say it returns
-*/
-
-#ifndef Void_t
-#if (__STD_C || defined(WIN32))
-#define Void_t      void
-#else
-#define Void_t      char
-#endif
-#endif /*Void_t*/
-
-#if __STD_C
-#include <stddef.h>   /* for size_t */
-#else
-#include <sys/types.h>
-#endif
-
-#if !defined(USE_SYSTEM_MALLOC)
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* define HAVE_UNISTD_H if your system has a <unistd.h>. */
-
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-
-#ifdef HAVE_SYS_UIO_H
-#include <sys/uio.h>
-#endif
-
-#include <stdio.h>    /* needed for malloc_stats */
-#include <errno.h>    /* needed for optional MALLOC_FAILURE_ACTION */
-
-#include <string.h>
-#include <stdlib.h>
-
-#include <sys/resource.h>
-
-extern int errno;
-
-  /* 0: lazy, 
-   * 1: medium (assertions compiled in), 
-   * 2: high (guard pages at end of hash table and ciregions) 
-   * 3: paranoid (guards at end of each allocated chunk, check at free) 
-   */
-#ifndef PARANOIA
-#define PARANOIA 9
-#endif
-
-  /* Using assert() with multithreading will cause the code 
-   * to deadlock since glibc __assert_fail will call malloc().
-   * We need our very own assert().
-   */
-typedef void assert_handler_tp(const char * error, const char *file, int line);
-
-#if  PARANOIA > 0
-
-#ifdef NDEBUG
-#undef NDEBUG
-#endif
-
-static void default_assert_handler(const char *error, 
-				   const char *file, int line)
-{
-#ifdef HAVE_WRITEV
-  struct iovec iov[5];
-  char * i1 = "assertion failed (";
-  char * i3 = "): ";
-  char * i5 = "\n";
-  int    res = 0;
-  char   ifile[128];
-  char   ierr[128];
-
-  strncpy(ifile, file, sizeof(ifile)); ifile[sizeof(ifile)-1] = '\0';
-  strncpy(ierr, error, sizeof(ierr));  ierr[sizeof(ierr)-1]   = '\0';
-
-  iov[0].iov_base = i1;     iov[0].iov_len = strlen(i1); 
-  iov[1].iov_base = ifile;  iov[1].iov_len = strlen(ifile); 
-  iov[2].iov_base = i3;     iov[2].iov_len = strlen(i3); 
-  iov[3].iov_base = ierr;   iov[3].iov_len = strlen(ierr); 
-  iov[4].iov_base = i5;     iov[4].iov_len = strlen(i5);
-  do {
-    res = writev(STDERR_FILENO, iov, 5);
-  } while (res < 0 && errno == EINTR);  
-#else
-  fputs("assertion failed (", stderr);
-  fputs(file, stderr);
-  fputs("): ", stderr);
-  fputs(error, stderr);
-  fputc('\n', stderr);
-#endif
-  (void) line;
-  abort();
-}
-static assert_handler_tp *assert_handler = default_assert_handler;
-
-
-#define assert(x)                               \
-  do {		                                \
-    if (UNLIKELY(!(x))) {			\
-      assert_handler(#x, __FILE__, __LINE__);	\
-    }                                           \
-  } while (0)
-
-#else
-
-static assert_handler_tp *assert_handler = NULL;
-#ifndef NDEBUG
-#define NDEBUG
-#endif
-#define assert(x) ((void)0)
-
-#endif
-
-assert_handler_tp *dnmalloc_set_handler(assert_handler_tp *new)
-{
-  assert_handler_tp *old = assert_handler;
-  assert_handler = new;
-  return old;
-}
-
-
-#include <stdarg.h>
-
-  /* define for debugging */
-  /* #define DNMALLOC_DEBUG */ 
-
-  /* Do some extra checks? if not, covered by assrt()s */
-  /* #define DNMALLOC_CHECKS */
-
-  /*
-    The unsigned integer type used for comparing any two chunk sizes.
-    This should be at least as wide as size_t, but should not be signed.
-  */
-
-#ifndef CHUNK_SIZE_T
-#define CHUNK_SIZE_T unsigned long
-#endif
-
-/* 
-  The unsigned integer type used to hold addresses when they are are
-  manipulated as integers. Except that it is not defined on all
-  systems, intptr_t would suffice.
-*/
-#ifndef PTR_UINT
-#define PTR_UINT unsigned long
-#endif
-
-
-/*
-  INTERNAL_SIZE_T is the word-size used for internal bookkeeping
-  of chunk sizes.
-
-  The default version is the same as size_t.
-
-  While not strictly necessary, it is best to define this as an
-  unsigned type, even if size_t is a signed type. This may avoid some
-  artificial size limitations on some systems.
-
-  On a 64-bit machine, you may be able to reduce malloc overhead by
-  defining INTERNAL_SIZE_T to be a 32 bit `unsigned int' at the
-  expense of not being able to handle more than 2^32 of malloced
-  space. If this limitation is acceptable, you are encouraged to set
-  this unless you are on a platform requiring 16byte alignments. In
-  this case the alignment requirements turn out to negate any
-  potential advantages of decreasing size_t word size.
-
-  Implementors: Beware of the possible combinations of:
-     - INTERNAL_SIZE_T might be signed or unsigned, might be 32 or 64 bits,
-       and might be the same width as int or as long
-     - size_t might have different width and signedness as INTERNAL_SIZE_T
-     - int and long might be 32 or 64 bits, and might be the same width
-  To deal with this, most comparisons and difference computations
-  among INTERNAL_SIZE_Ts should cast them to CHUNK_SIZE_T, being
-  aware of the fact that casting an unsigned int to a wider long does
-  not sign-extend. (This also makes checking for negative numbers
-  awkward.) Some of these casts result in harmless compiler warnings
-  on some systems.
-*/
-
-#ifndef INTERNAL_SIZE_T
-#define INTERNAL_SIZE_T size_t
-#endif
-
-/* The corresponding word size */
-#define SIZE_SZ                (sizeof(INTERNAL_SIZE_T))
-
-
-
-/*
-  MALLOC_ALIGNMENT is the minimum alignment for malloc'ed chunks.
-  It must be a power of two at least 2 * SIZE_SZ, even on machines
-  for which smaller alignments would suffice. It may be defined as
-  larger than this though. Note however that code and data structures
-  are optimized for the case of 8-byte alignment.
-*/
-
-
-#ifndef MALLOC_ALIGNMENT
-#define MALLOC_ALIGNMENT       (2 * SIZE_SZ)
-#endif
-
-/* The corresponding bit mask value */
-#define MALLOC_ALIGN_MASK      (MALLOC_ALIGNMENT - 1)
-
-
-
-/*
-  REALLOC_ZERO_BYTES_FREES should be set if a call to
-  realloc with zero bytes should be the same as a call to free.
-  Some people think it should. Otherwise, since this malloc
-  returns a unique pointer for malloc(0), so does realloc(p, 0).
-*/
-
-#define REALLOC_ZERO_BYTES_FREES
-
-/*
-  TRIM_FASTBINS controls whether free() of a very small chunk can
-  immediately lead to trimming. Setting to true (1) can reduce memory
-  footprint, but will almost always slow down programs that use a lot
-  of small chunks.
-
-  Define this only if you are willing to give up some speed to more
-  aggressively reduce system-level memory footprint when releasing
-  memory in programs that use many small chunks.  You can get
-  essentially the same effect by setting MXFAST to 0, but this can
-  lead to even greater slowdowns in programs using many small chunks.
-  TRIM_FASTBINS is an in-between compile-time option, that disables
-  only those chunks bordering topmost memory from being placed in
-  fastbins.
-*/
-
-#ifndef TRIM_FASTBINS
-#define TRIM_FASTBINS  0
-#endif
-
-
-/*
-  USE_DL_PREFIX will prefix all public routines with the string 'dl'.
-  This is necessary when you only want to use this malloc in one part 
-  of a program, using your regular system malloc elsewhere.
-*/
-
-/* #define USE_DL_PREFIX */
-
-
-/*
-  USE_MALLOC_LOCK causes wrapper functions to surround each
-  callable routine with pthread mutex lock/unlock.
-
-  USE_MALLOC_LOCK forces USE_PUBLIC_MALLOC_WRAPPERS to be defined
-*/
-
-/* #define USE_MALLOC_LOCK */
-
-
-/*
-  If USE_PUBLIC_MALLOC_WRAPPERS is defined, every public routine is
-  actually a wrapper function that first calls MALLOC_PREACTION, then
-  calls the internal routine, and follows it with
-  MALLOC_POSTACTION. This is needed for locking, but you can also use
-  this, without USE_MALLOC_LOCK, for purposes of interception,
-  instrumentation, etc. It is a sad fact that using wrappers often
-  noticeably degrades performance of malloc-intensive programs.
-*/
-
-
-#ifdef USE_MALLOC_LOCK
-#define USE_PUBLIC_MALLOC_WRAPPERS
-#else
-/* #define USE_PUBLIC_MALLOC_WRAPPERS */
-#endif
-
-
-/* 
-   Two-phase name translation.
-   All of the actual routines are given mangled names.
-   When wrappers are used, they become the public callable versions.
-   When DL_PREFIX is used, the callable names are prefixed.
-*/
-
-#ifndef USE_PUBLIC_MALLOC_WRAPPERS
-#define cALLOc      public_cALLOc
-#define fREe        public_fREe
-#define mALLOc      public_mALLOc
-#define mEMALIGn    public_mEMALIGn
-#define posix_mEMALIGn    public_posix_mEMALIGn
-#define rEALLOc     public_rEALLOc
-#define vALLOc      public_vALLOc
-#define pVALLOc     public_pVALLOc
-#define mALLINFo2   public_mALLINFo2
-#define mALLOPt     public_mALLOPt
-#define mTRIm       public_mTRIm
-#define mSTATs      public_mSTATs
-#define mUSABLe     public_mUSABLe
-#endif
-
-#ifdef USE_DL_PREFIX
-#define public_cALLOc    dlcalloc
-#define public_fREe      dlfree
-#define public_mALLOc    dlmalloc
-#define public_mEMALIGn  dlmemalign
-#define public_posix_mEMALIGn  dlposix_memalign
-#define public_rEALLOc   dlrealloc
-#define public_vALLOc    dlvalloc
-#define public_pVALLOc   dlpvalloc
-#define public_mALLINFo2 dlmallinfo2
-#define public_mALLOPt   dlmallopt
-#define public_mTRIm     dlmalloc_trim
-#define public_mSTATs    dlmalloc_stats
-#define public_mUSABLe   dlmalloc_usable_size
-#else /* USE_DL_PREFIX */
-#define public_cALLOc    calloc
-#define public_fREe      free
-#define public_mALLOc    malloc
-#define public_mEMALIGn  memalign
-#define public_posix_mEMALIGn  posix_memalign
-#define public_rEALLOc   realloc
-#define public_vALLOc    valloc
-#define public_pVALLOc   pvalloc
-#define public_mALLINFo2 mallinfo2
-#define public_mALLOPt   mallopt
-#define public_mTRIm     malloc_trim
-#define public_mSTATs    malloc_stats
-#define public_mUSABLe   malloc_usable_size
-#endif /* USE_DL_PREFIX */
-
-
-/*
-  HAVE_MEMCPY should be defined if you are not otherwise using
-  ANSI STD C, but still have memcpy and memset in your C library
-  and want to use them in calloc and realloc. Otherwise simple
-  macro versions are defined below.
-
-  USE_MEMCPY should be defined as 1 if you actually want to
-  have memset and memcpy called. People report that the macro
-  versions are faster than libc versions on some systems.
-  
-  Even if USE_MEMCPY is set to 1, loops to copy/clear small chunks
-  (of <= 36 bytes) are manually unrolled in realloc and calloc.
-*/
-
-#ifndef HAVE_MEMCPY
-#define HAVE_MEMCPY
-#endif
-
-#ifndef USE_MEMCPY
-#ifdef HAVE_MEMCPY
-#define USE_MEMCPY 1
-#else
-#define USE_MEMCPY 0
-#endif
-#endif
-
-
-#if (__STD_C || defined(HAVE_MEMCPY))
-
-#ifdef WIN32
-  /* On Win32 memset and memcpy are already declared in windows.h */
-#else
-#if __STD_C
-  /* Defined in string.h */
-#else
-Void_t* memset();
-Void_t* memcpy();
-#endif
-#endif
-#endif
-
-/*
-  MALLOC_FAILURE_ACTION is the action to take before "return 0" when
-  malloc fails to be able to return memory, either because memory is
-  exhausted or because of illegal arguments.
-  
-  By default, sets errno if running on STD_C platform, else does nothing.  
-*/
-
-#ifndef MALLOC_FAILURE_ACTION
-#if __STD_C
-#define MALLOC_FAILURE_ACTION \
-   errno = ENOMEM;
-
-#else
-#define MALLOC_FAILURE_ACTION
-#endif
-#endif
-
-/*
-  MORECORE-related declarations. By default, rely on sbrk
-*/
-
-
-#if !defined(HAVE_UNISTD_H)
-#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
-#if __STD_C
-extern Void_t*     sbrk(ptrdiff_t);
-#else
-extern Void_t*     sbrk();
-#endif
-#endif
-#endif
-
-/*
-  MORECORE_FAILURE is the value returned upon failure of MORECORE
-  as well as mmap. Since it cannot be an otherwise valid memory address,
-  and must reflect values of standard sys calls, you probably ought not
-  try to redefine it.
-*/
-
-#ifndef MORECORE_FAILURE
-#define MORECORE_FAILURE ((void*)(-1UL))
-#endif
-
-/*
-  MORECORE is the name of the routine to call to obtain more memory
-  from the system.  See below for general guidance on writing
-  alternative MORECORE functions, as well as a version for WIN32 and a
-  sample version for pre-OSX macos.
-*/
-
-#ifndef MORECORE
-#define MORECORE sbrk
-#endif
-
-
-/*
-  If MORECORE_CONTIGUOUS is true, take advantage of fact that
-  consecutive calls to MORECORE with positive arguments always return
-  contiguous increasing addresses.  This is true of unix sbrk.  Even
-  if not defined, when regions happen to be contiguous, malloc will
-  permit allocations spanning regions obtained from different
-  calls. But defining this when applicable enables some stronger
-  consistency checks and space efficiencies. 
-*/
-
-#ifndef MORECORE_CONTIGUOUS
-#define MORECORE_CONTIGUOUS 1
-#endif
-
-/*
-  Define MORECORE_CANNOT_TRIM if your version of MORECORE
-  cannot release space back to the system when given negative
-  arguments. This is generally necessary only if you are using
-  a hand-crafted MORECORE function that cannot handle negative arguments.
-*/
-
-/* #define MORECORE_CANNOT_TRIM */
-
-
-/*
-  This malloc requires mmap for heap management data. It is an error
-  if mmap is not available.
-
-  Additionally, mmap will be used to satisfy large requests.
-*/
-
-#ifndef HAVE_MMAP
-#  error HAVE_MMAP not defined, has your operating system mmap?
-#endif
-
-/* 
-   Standard unix mmap using /dev/zero clears memory so calloc doesn't
-   need to.
-*/
-
-#ifndef MMAP_CLEARS
-#define MMAP_CLEARS 1
-#endif
-
-
-/* 
-   MMAP_AS_MORECORE_SIZE is the minimum mmap size argument to use if
-   sbrk fails, and mmap is used as a backup (which is done only if
-   HAVE_MMAP).  The value must be a multiple of page size.  This
-   backup strategy generally applies only when systems have "holes" in
-   address space, so sbrk cannot perform contiguous expansion, but
-   there is still space available on system.  On systems for which
-   this is known to be useful (i.e. most linux kernels), this occurs
-   only when programs allocate huge amounts of memory.  Between this,
-   and the fact that mmap regions tend to be limited, the size should
-   be large, to avoid too many mmap calls and thus avoid running out
-   of kernel resources.
-*/
-
-#ifndef MMAP_AS_MORECORE_SIZE
-#define MMAP_AS_MORECORE_SIZE (1024 * 1024)
-#endif
-
-
-/*
-  The system page size. To the extent possible, this malloc manages
-  memory from the system in page-size units.  Note that this value is
-  cached during initialization into a field of malloc_state. So even
-  if malloc_getpagesize is a function, it is only called once.
-
-  The following mechanics for getpagesize were adapted from bsd/gnu
-  getpagesize.h. If none of the system-probes here apply, a value of
-  4096 is used, which should be OK: If they don't apply, then using
-  the actual value probably doesn't impact performance.
-*/
-
-
-#ifndef malloc_getpagesize
-
-#  ifdef _SC_PAGESIZE         /* some SVR4 systems omit an underscore */
-#    ifndef _SC_PAGE_SIZE
-#      define _SC_PAGE_SIZE _SC_PAGESIZE
-#    endif
-#  endif
-
-#  ifdef _SC_PAGE_SIZE
-#    define malloc_getpagesize sysconf(_SC_PAGE_SIZE)
-#  else
-#    if defined(BSD) || defined(DGUX) || defined(HAVE_GETPAGESIZE)
-       extern size_t getpagesize();
-#      define malloc_getpagesize getpagesize()
-#    else
-#      ifdef WIN32 /* use supplied emulation of getpagesize */
-#        define malloc_getpagesize getpagesize() 
-#      else
-#        if defined(HAVE_SYS_PARAM_H)
-#          include <sys/param.h>
-#        endif
-#        ifdef EXEC_PAGESIZE
-#          define malloc_getpagesize EXEC_PAGESIZE
-#        else
-#          ifdef NBPG
-#            ifndef CLSIZE
-#              define malloc_getpagesize NBPG
-#            else
-#              define malloc_getpagesize (NBPG * CLSIZE)
-#            endif
-#          else
-#            ifdef NBPC
-#              define malloc_getpagesize NBPC
-#            else
-#              ifdef PAGESIZE
-#                define malloc_getpagesize PAGESIZE
-#              else /* just guess */
-#                define malloc_getpagesize (4096) 
-#              endif
-#            endif
-#          endif
-#        endif
-#      endif
-#    endif
-#  endif
-#endif
-
-/*
-  This version of malloc supports the standard SVID/XPG mallinfo2
-  routine that returns a struct containing usage properties and
-  statistics. It should work on any SVID/XPG compliant system that has
-  a /usr/include/malloc.h defining struct mallinfo2. (If you'd like to
-  install such a thing yourself, cut out the preliminary declarations
-  as described above and below and save them in a malloc.h file. But
-  there's no compelling reason to bother to do this.)
-
-  The main declaration needed is the mallinfo2 struct that is returned
-  (by-copy) by mallinfo2().  The SVID/XPG malloinfo2 struct contains a
-  bunch of fields that are not even meaningful in this version of
-  malloc.  These fields are are instead filled by mallinfo2() with
-  other numbers that might be of interest.
-
-  HAVE_MALLOC_H should be set if you have a
-  /usr/include/malloc.h file that includes a declaration of struct
-  mallinfo2.  If so, it is included; else an SVID2/XPG2 compliant
-  version is declared below.  These must be precisely the same for
-  mallinfo2() to work.  The original SVID version of this struct,
-  defined on most systems with mallinfo2, declares all fields as
-  size_2. But some others define as unsigned long. If your system
-  defines the fields using a type of different width than listed here,
-  you must #include your system version and #define
-  HAVE_MALLOC_H.
-*/
-
-/* #define HAVE_MALLOC_H */
-
-/* On *BSD, malloc.h is deprecated, and on some *BSD including 
- * it may actually raise an error.
- */
-#if defined(HAVE_MALLOC_H) && !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 33)
-#include <malloc.h>
-#else
-
-/* SVID2/XPG mallinfo2 structure */
-
-struct mallinfo2 {
-  size_t arena;    /* non-mmapped space allocated from system */
-  size_t ordblks;  /* number of free chunks */
-  size_t smblks;   /* number of fastbin blocks */
-  size_t hblks;    /* number of mmapped regions */
-  size_t hblkhd;   /* space in mmapped regions */
-  size_t usmblks;  /* maximum total allocated space */
-  size_t fsmblks;  /* space available in freed fastbin blocks */
-  size_t uordblks; /* total allocated space */
-  size_t fordblks; /* total free space */
-  size_t keepcost; /* top-most, releasable (via malloc_trim) space */
-};
-
-/*
-  SVID/XPG defines four standard parameter numbers for mallopt,
-  normally defined in malloc.h.  Only one of these (M_MXFAST) is used
-  in this malloc. The others (M_NLBLKS, M_GRAIN, M_KEEP) don't apply,
-  so setting them has no effect. But this malloc also supports other
-  options in mallopt described below.
-*/
-#endif
-
-
-/* ---------- description of public routines ------------ */
-
-/*
-  malloc(size_t n)
-  Returns a pointer to a newly allocated chunk of at least n bytes, or null
-  if no space is available. Additionally, on failure, errno is
-  set to ENOMEM on ANSI C systems.
-
-  If n is zero, malloc returns a minumum-sized chunk. (The minimum
-  size is 16 bytes on most 32bit systems, and 24 or 32 bytes on 64bit
-  systems.)  On most systems, size_t is an unsigned type, so calls
-  with negative arguments are interpreted as requests for huge amounts
-  of space, which will often fail. The maximum supported value of n
-  differs across systems, but is in all cases less than the maximum
-  representable value of a size_t.
-*/
-#if __STD_C
-Void_t*  public_mALLOc(size_t) __attribute_malloc__;
-#else
-Void_t*  public_mALLOc();
-#endif
-
-/*
-  free(Void_t* p)
-  Releases the chunk of memory pointed to by p, that had been previously
-  allocated using malloc or a related routine such as realloc.
-  It has no effect if p is null. It can have arbitrary (i.e., bad!)
-  effects if p has already been freed.
-
-  Unless disabled (using mallopt), freeing very large spaces will
-  when possible, automatically trigger operations that give
-  back unused memory to the system, thus reducing program footprint.
-*/
-#if __STD_C
-void     public_fREe(Void_t*);
-#else
-void     public_fREe();
-#endif
-
-/*
-  calloc(size_t n_elements, size_t element_size);
-  Returns a pointer to n_elements * element_size bytes, with all locations
-  set to zero.
-*/
-#if __STD_C
-Void_t*  public_cALLOc(size_t, size_t) __attribute_malloc__;
-#else
-Void_t*  public_cALLOc();
-#endif
-
-/*
-  realloc(Void_t* p, size_t n)
-  Returns a pointer to a chunk of size n that contains the same data
-  as does chunk p up to the minimum of (n, p's size) bytes, or null
-  if no space is available. 
-
-  The returned pointer may or may not be the same as p. The algorithm
-  prefers extending p when possible, otherwise it employs the
-  equivalent of a malloc-copy-free sequence.
-
-  If p is null, realloc is equivalent to malloc.  
-
-  If space is not available, realloc returns null, errno is set (if on
-  ANSI) and p is NOT freed.
-
-  if n is for fewer bytes than already held by p, the newly unused
-  space is lopped off and freed if possible.  Unless the #define
-  REALLOC_ZERO_BYTES_FREES is set, realloc with a size argument of
-  zero (re)allocates a minimum-sized chunk.
-
-  Large chunks that were internally obtained via mmap will always
-  be reallocated using malloc-copy-free sequences unless
-  the system supports MREMAP (currently only linux).
-
-  The old unix realloc convention of allowing the last-free'd chunk
-  to be used as an argument to realloc is not supported.
-*/
-#if __STD_C
-Void_t*  public_rEALLOc(Void_t*, size_t) __attribute_malloc__;
-#else
-Void_t*  public_rEALLOc();
-#endif
-
-/*
-  memalign(size_t alignment, size_t n);
-  Returns a pointer to a newly allocated chunk of n bytes, aligned
-  in accord with the alignment argument.
-
-  The alignment argument should be a power of two. If the argument is
-  not a power of two, the nearest greater power is used.
-  8-byte alignment is guaranteed by normal malloc calls, so don't
-  bother calling memalign with an argument of 8 or less.
-
-  Overreliance on memalign is a sure way to fragment space.
-*/
-#if __STD_C
-Void_t*  public_mEMALIGn(size_t, size_t) __attribute_malloc__;
-#else
-Void_t*  public_mEMALIGn();
-#endif
-
-/*
-  posix_memalign(void** memptr, size_t alignment, size_t n);
-  Sets *memptr to the address of a newly allocated chunk of n bytes, aligned
-  in accord with the alignment argument. Returns 0 on success, otherwise
-  an error (EINVAL for incorrect alignment, ENOMEM for out of memory).
-
-  The alignment must be a power of two, and a multiple of sizeof(void *).
-*/
-#if __STD_C
-int public_posix_mEMALIGn(Void_t**, size_t, size_t);
-#else
-int public_posix_mEMALIGn();
-#endif
-
-/*
-  valloc(size_t n);
-  Equivalent to memalign(pagesize, n), where pagesize is the page
-  size of the system. If the pagesize is unknown, 4096 is used.
-*/
-#if __STD_C
-Void_t*  public_vALLOc(size_t) __attribute_malloc__;
-#else
-Void_t*  public_vALLOc();
-#endif
-
-
-
-/*
-  mallopt(int parameter_number, int parameter_value)
-  Sets tunable parameters The format is to provide a
-  (parameter-number, parameter-value) pair.  mallopt then sets the
-  corresponding parameter to the argument value if it can (i.e., so
-  long as the value is meaningful), and returns 1 if successful else
-  0.  SVID/XPG/ANSI defines four standard param numbers for mallopt,
-  normally defined in malloc.h.  Only one of these (M_MXFAST) is used
-  in this malloc. The others (M_NLBLKS, M_GRAIN, M_KEEP) don't apply,
-  so setting them has no effect. But this malloc also supports four
-  other options in mallopt. See below for details.  Briefly, supported
-  parameters are as follows (listed defaults are for "typical"
-  configurations).
-
-  Symbol            param #   default    allowed param values
-  M_MXFAST          1         64         0-80  (0 disables fastbins)
-  M_TRIM_THRESHOLD -1         256*1024   any   (-1U disables trimming)
-  M_TOP_PAD        -2         0          any  
-  M_MMAP_THRESHOLD -3         256*1024   any   (or 0 if no MMAP support)
-  M_MMAP_MAX       -4         65536      any   (0 disables use of mmap)
-*/
-#if __STD_C
-int      public_mALLOPt(int, int);
-#else
-int      public_mALLOPt();
-#endif
-
-
-/*
-  mallinfo2()
-  Returns (by copy) a struct containing various summary statistics:
-
-  arena:     current total non-mmapped bytes allocated from system 
-  ordblks:   the number of free chunks 
-  smblks:    the number of fastbin blocks (i.e., small chunks that
-               have been freed but not use resused or consolidated)
-  hblks:     current number of mmapped regions 
-  hblkhd:    total bytes held in mmapped regions 
-  usmblks:   the maximum total allocated space. This will be greater
-                than current total if trimming has occurred.
-  fsmblks:   total bytes held in fastbin blocks 
-  uordblks:  current total allocated space (normal or mmapped)
-  fordblks:  total free space 
-  keepcost:  the maximum number of bytes that could ideally be released
-               back to system via malloc_trim. ("ideally" means that
-               it ignores page restrictions etc.)
-
-  Because these fields are ints, but internal bookkeeping may
-  be kept as longs, the reported values may wrap around zero and 
-  thus be inaccurate.
-*/
-#if __STD_C
-struct mallinfo2 public_mALLINFo2(void);
-#else
-struct mallinfo2 public_mALLINFo2();
-#endif
-
-/*
-  pvalloc(size_t n);
-  Equivalent to valloc(minimum-page-that-holds(n)), that is,
-  round up n to nearest pagesize.
- */
-#if __STD_C
-Void_t*  public_pVALLOc(size_t) __attribute_malloc__;
-#else
-Void_t*  public_pVALLOc();
-#endif
-
-/*
-  malloc_trim(size_t pad);
-
-  If possible, gives memory back to the system (via negative
-  arguments to sbrk) if there is unused memory at the `high' end of
-  the malloc pool. You can call this after freeing large blocks of
-  memory to potentially reduce the system-level memory requirements
-  of a program. However, it cannot guarantee to reduce memory. Under
-  some allocation patterns, some large free blocks of memory will be
-  locked between two used chunks, so they cannot be given back to
-  the system.
-  
-  The `pad' argument to malloc_trim represents the amount of free
-  trailing space to leave untrimmed. If this argument is zero,
-  only the minimum amount of memory to maintain internal data
-  structures will be left (one page or less). Non-zero arguments
-  can be supplied to maintain enough trailing space to service
-  future expected allocations without having to re-obtain memory
-  from the system.
-  
-  Malloc_trim returns 1 if it actually released any memory, else 0.
-  On systems that do not support "negative sbrks", it will always
-  rreturn 0.
-*/
-#if __STD_C
-int      public_mTRIm(size_t);
-#else
-int      public_mTRIm();
-#endif
-
-/*
-  malloc_usable_size(Void_t* p);
-
-  Returns the number of bytes you can actually use in
-  an allocated chunk, which may be more than you requested (although
-  often not) due to alignment and minimum size constraints.
-  You can use this many bytes without worrying about
-  overwriting other allocated objects. This is not a particularly great
-  programming practice. malloc_usable_size can be more useful in
-  debugging and assertions, for example:
-
-  p = malloc(n);
-  assert(malloc_usable_size(p) >= 256);
-
-*/
-#if __STD_C
-size_t   public_mUSABLe(Void_t*);
-#else
-size_t   public_mUSABLe();
-#endif
-
-/*
-  malloc_stats();
-  Prints on stderr the amount of space obtained from the system (both
-  via sbrk and mmap), the maximum amount (which may be more than
-  current if malloc_trim and/or munmap got called), and the current
-  number of bytes allocated via malloc (or realloc, etc) but not yet
-  freed. Note that this is the number of bytes allocated, not the
-  number requested. It will be larger than the number requested
-  because of alignment and bookkeeping overhead. Because it includes
-  alignment wastage as being in use, this figure may be greater than
-  zero even when no user-level chunks are allocated.
-
-  The reported current and maximum system memory can be inaccurate if
-  a program makes other calls to system memory allocation functions
-  (normally sbrk) outside of malloc.
-
-  malloc_stats prints only the most commonly interesting statistics.
-  More information can be obtained by calling mallinfo2.
-
-*/
-#if __STD_C
-void     public_mSTATs();
-#else
-void     public_mSTATs();
-#endif
-
-/* mallopt tuning options */
-
-/*
-  M_MXFAST is the maximum request size used for "fastbins", special bins
-  that hold returned chunks without consolidating their spaces. This
-  enables future requests for chunks of the same size to be handled
-  very quickly, but can increase fragmentation, and thus increase the
-  overall memory footprint of a program.
-
-  This malloc manages fastbins very conservatively yet still
-  efficiently, so fragmentation is rarely a problem for values less
-  than or equal to the default.  The maximum supported value of MXFAST
-  is 80. You wouldn't want it any higher than this anyway.  Fastbins
-  are designed especially for use with many small structs, objects or
-  strings -- the default handles structs/objects/arrays with sizes up
-  to 16 4byte fields, or small strings representing words, tokens,
-  etc. Using fastbins for larger objects normally worsens
-  fragmentation without improving speed.
-
-  M_MXFAST is set in REQUEST size units. It is internally used in
-  chunksize units, which adds padding and alignment.  You can reduce
-  M_MXFAST to 0 to disable all use of fastbins.  This causes the malloc
-  algorithm to be a closer approximation of fifo-best-fit in all cases,
-  not just for larger requests, but will generally cause it to be
-  slower.
-*/
-
-
-/* M_MXFAST is a standard SVID/XPG tuning option, usually listed in malloc.h */
-#ifndef M_MXFAST
-#define M_MXFAST            1    
-#endif
-
-#ifndef DEFAULT_MXFAST
-#define DEFAULT_MXFAST     64
-#endif
-
-
-/*
-  M_TRIM_THRESHOLD is the maximum amount of unused top-most memory
-  to keep before releasing via malloc_trim in free().
-
-  Automatic trimming is mainly useful in long-lived programs.
-  Because trimming via sbrk can be slow on some systems, and can
-  sometimes be wasteful (in cases where programs immediately
-  afterward allocate more large chunks) the value should be high
-  enough so that your overall system performance would improve by
-  releasing this much memory.
-
-  The trim threshold and the mmap control parameters (see below)
-  can be traded off with one another. Trimming and mmapping are
-  two different ways of releasing unused memory back to the
-  system. Between these two, it is often possible to keep
-  system-level demands of a long-lived program down to a bare
-  minimum. For example, in one test suite of sessions measuring
-  the XF86 X server on Linux, using a trim threshold of 128K and a
-  mmap threshold of 192K led to near-minimal long term resource
-  consumption.
-
-  If you are using this malloc in a long-lived program, it should
-  pay to experiment with these values.  As a rough guide, you
-  might set to a value close to the average size of a process
-  (program) running on your system.  Releasing this much memory
-  would allow such a process to run in memory.  Generally, it's
-  worth it to tune for trimming rather tham memory mapping when a
-  program undergoes phases where several large chunks are
-  allocated and released in ways that can reuse each other's
-  storage, perhaps mixed with phases where there are no such
-  chunks at all.  And in well-behaved long-lived programs,
-  controlling release of large blocks via trimming versus mapping
-  is usually faster.
-
-  However, in most programs, these parameters serve mainly as
-  protection against the system-level effects of carrying around
-  massive amounts of unneeded memory. Since frequent calls to
-  sbrk, mmap, and munmap otherwise degrade performance, the default
-  parameters are set to relatively high values that serve only as
-  safeguards.
-
-  The trim value must be greater than page size to have any useful
-  effect.  To disable trimming completely, you can set to 
-  (unsigned long)(-1)
-
-  Trim settings interact with fastbin (MXFAST) settings: Unless
-  TRIM_FASTBINS is defined, automatic trimming never takes place upon
-  freeing a chunk with size less than or equal to MXFAST. Trimming is
-  instead delayed until subsequent freeing of larger chunks. However,
-  you can still force an attempted trim by calling malloc_trim.
-
-  Also, trimming is not generally possible in cases where
-  the main arena is obtained via mmap.
-
-  Note that the trick some people use of mallocing a huge space and
-  then freeing it at program startup, in an attempt to reserve system
-  memory, doesn't have the intended effect under automatic trimming,
-  since that memory will immediately be returned to the system.
-*/
-
-#define M_TRIM_THRESHOLD       -1
-
-#ifndef DEFAULT_TRIM_THRESHOLD
-#define DEFAULT_TRIM_THRESHOLD (256 * 1024)
-#endif
-
-/*
-  M_TOP_PAD is the amount of extra `padding' space to allocate or
-  retain whenever sbrk is called. It is used in two ways internally:
-
-  * When sbrk is called to extend the top of the arena to satisfy
-  a new malloc request, this much padding is added to the sbrk
-  request.
-
-  * When malloc_trim is called automatically from free(),
-  it is used as the `pad' argument.
-
-  In both cases, the actual amount of padding is rounded
-  so that the end of the arena is always a system page boundary.
-
-  The main reason for using padding is to avoid calling sbrk so
-  often. Having even a small pad greatly reduces the likelihood
-  that nearly every malloc request during program start-up (or
-  after trimming) will invoke sbrk, which needlessly wastes
-  time.
-
-  Automatic rounding-up to page-size units is normally sufficient
-  to avoid measurable overhead, so the default is 0.  However, in
-  systems where sbrk is relatively slow, it can pay to increase
-  this value, at the expense of carrying around more memory than
-  the program needs.
-*/
-
-#define M_TOP_PAD              -2
-
-#ifndef DEFAULT_TOP_PAD
-#define DEFAULT_TOP_PAD        (0)
-#endif
-
-/*
-  M_MMAP_THRESHOLD is the request size threshold for using mmap()
-  to service a request. Requests of at least this size that cannot
-  be allocated using already-existing space will be serviced via mmap.
-  (If enough normal freed space already exists it is used instead.)
-
-  Using mmap segregates relatively large chunks of memory so that
-  they can be individually obtained and released from the host
-  system. A request serviced through mmap is never reused by any
-  other request (at least not directly; the system may just so
-  happen to remap successive requests to the same locations).
-
-  Segregating space in this way has the benefits that:
-
-   1. Mmapped space can ALWAYS be individually released back 
-      to the system, which helps keep the system level memory 
-      demands of a long-lived program low. 
-   2. Mapped memory can never become `locked' between
-      other chunks, as can happen with normally allocated chunks, which
-      means that even trimming via malloc_trim would not release them.
-   3. On some systems with "holes" in address spaces, mmap can obtain
-      memory that sbrk cannot.
-
-  However, it has the disadvantages that:
-
-   1. The space cannot be reclaimed, consolidated, and then
-      used to service later requests, as happens with normal chunks.
-   2. It can lead to more wastage because of mmap page alignment
-      requirements
-   3. It causes malloc performance to be more dependent on host
-      system memory management support routines which may vary in
-      implementation quality and may impose arbitrary
-      limitations. Generally, servicing a request via normal
-      malloc steps is faster than going through a system's mmap.
-
-  The advantages of mmap nearly always outweigh disadvantages for
-  "large" chunks, but the value of "large" varies across systems.  The
-  default is an empirically derived value that works well in most
-  systems.
-*/
-
-#define M_MMAP_THRESHOLD      -3
-
-#ifndef DEFAULT_MMAP_THRESHOLD
-#define DEFAULT_MMAP_THRESHOLD (256 * 1024)
-#endif
-
-/*
-  M_MMAP_MAX is the maximum number of requests to simultaneously
-  service using mmap. This parameter exists because
-. Some systems have a limited number of internal tables for
-  use by mmap, and using more than a few of them may degrade
-  performance.
-
-  The default is set to a value that serves only as a safeguard.
-  Setting to 0 disables use of mmap for servicing large requests.  If
-  HAVE_MMAP is not set, the default value is 0, and attempts to set it
-  to non-zero values in mallopt will fail.
-*/
-
-#define M_MMAP_MAX             -4
-
-#ifndef DEFAULT_MMAP_MAX
-#define DEFAULT_MMAP_MAX       (65536)
-#endif
-
-#ifdef __cplusplus
-};  /* end of extern "C" */
-#endif
-
-/* 
-  ========================================================================
-  To make a fully customizable malloc.h header file, cut everything
-  above this line, put into file malloc.h, edit to suit, and #include it 
-  on the next line, as well as in programs that use this malloc.
-  ========================================================================
-*/
-
-/* #include "malloc.h" */
-
-/* --------------------- public wrappers ---------------------- */
-
-#ifdef USE_PUBLIC_MALLOC_WRAPPERS
-
-/* DL_STATIC used to make functions (deep down) consistent
- * with prototypes (otherwise the prototypes are static
- * with USE_PUBLIC_MALLOC_WRAPPERS, but the functions aren't).
- * The gcc compiler doesn't care, but the HP-UX compiler does.
- */
-#define DL_STATIC static
-
-/* Declare all routines as internal */
-#if __STD_C
-static Void_t*  mALLOc(size_t) __attribute_malloc__;
-static void     fREe(Void_t*);
-static Void_t*  rEALLOc(Void_t*, size_t) __attribute_malloc__;
-static Void_t*  mEMALIGn(size_t, size_t) __attribute_malloc__;
-static int      posix_mEMALIGn(Void_t**, size_t, size_t);
-static Void_t*  vALLOc(size_t) __attribute_malloc__;
-static Void_t*  pVALLOc(size_t) __attribute_malloc__;
-static Void_t*  cALLOc(size_t, size_t) __attribute_malloc__;
-static int      mTRIm(size_t);
-static size_t   mUSABLe(Void_t*);
-static void     mSTATs();
-static int      mALLOPt(int, int);
-static struct mallinfo2 mALLINFo2(void);
-#else
-static Void_t*  mALLOc();
-static void     fREe();
-static Void_t*  rEALLOc();
-static Void_t*  mEMALIGn();
-static int      posix_mEMALIGn();
-static Void_t*  vALLOc();
-static Void_t*  pVALLOc();
-static Void_t*  cALLOc();
-static int      mTRIm();
-static size_t   mUSABLe();
-static void     mSTATs();
-static int      mALLOPt();
-static struct mallinfo2 mALLINFo2();
-#endif
-
-/*
-  MALLOC_PREACTION and MALLOC_POSTACTION should be
-  defined to return 0 on success, and nonzero on failure.
-  The return value of MALLOC_POSTACTION is currently ignored
-  in wrapper functions since there is no reasonable default
-  action to take on failure.
-*/
-
-
-#ifdef USE_MALLOC_LOCK
-
-# ifdef WIN32
-
-static int mALLOC_MUTEx;
-#define MALLOC_PREACTION   slwait(&mALLOC_MUTEx)
-#define MALLOC_POSTACTION  slrelease(&mALLOC_MUTEx)
-int dnmalloc_pthread_init(void) { return 0; }
-
-# elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
-
-#   if defined(__NetBSD__)
-#include <reentrant.h>
-extern int __isthreaded;
-static mutex_t thread_lock = MUTEX_INITIALIZER;
-#define _MALLOC_LOCK()   if (__isthreaded) mutex_lock(&thread_lock)
-#define _MALLOC_UNLOCK() if (__isthreaded) mutex_unlock(&thread_lock)
-void _malloc_prefork(void) {  _MALLOC_LOCK(); }
-void _malloc_postfork(void) { _MALLOC_UNLOCK(); }
-#   endif
-
-#   if defined(__OpenBSD__)
-extern int  __isthreaded;
-void   _thread_malloc_lock(void);
-void   _thread_malloc_unlock(void);
-#define _MALLOC_LOCK()           if (__isthreaded) _thread_malloc_lock()
-#define _MALLOC_UNLOCK()         if (__isthreaded) _thread_malloc_unlock()
-#   endif
-
-#   if defined(__FreeBSD__)
-extern int      __isthreaded;
-struct _spinlock {
-	volatile long	access_lock;
-	volatile long	lock_owner;
-	volatile char	*fname;
-	volatile int	lineno;
-};
-typedef struct _spinlock spinlock_t;
-#define	_SPINLOCK_INITIALIZER	{ 0, 0, 0, 0 }
-void	_spinlock(spinlock_t *);
-void	_spinunlock(spinlock_t *);
-/* # include "/usr/src/lib/libc/include/spinlock.h" */
-static spinlock_t thread_lock   = _SPINLOCK_INITIALIZER;
-spinlock_t *__malloc_lock       = &thread_lock;
-#define _MALLOC_LOCK()           if (__isthreaded) _spinlock(&thread_lock)
-#define _MALLOC_UNLOCK()         if (__isthreaded) _spinunlock(&thread_lock)
-#   endif
-
-/* Common for all three *BSD
- */
-static int malloc_active = 0;
-static int dnmalloc_mutex_lock()
-{
-  _MALLOC_LOCK();
-  if (!malloc_active)
-    {
-      ++malloc_active;
-      return 0;
-    }
-  assert(malloc_active == 0);
-  _MALLOC_UNLOCK();
-  errno = EDEADLK;
-  return 1;
-}
-static int dnmalloc_mutex_unlock()
-{
-  --malloc_active;
-  _MALLOC_UNLOCK();
-  return 0;
-}
-#define MALLOC_PREACTION   dnmalloc_mutex_lock()
-#define MALLOC_POSTACTION  dnmalloc_mutex_unlock()
-int dnmalloc_pthread_init(void) { return 0; }
-
-# else
-
-/* Wrapping malloc with pthread_mutex_lock/pthread_mutex_unlock
- *
- * Works fine on linux (no malloc in pthread_mutex_lock)
- * Works with on HP-UX if initialized after entering main()
- */ 
-#include <pthread.h>
-static int malloc_active      = 0;
-void dnmalloc_fork_prepare(void);
-void dnmalloc_fork_parent(void);
-void dnmalloc_fork_child(void);
-
-#if !defined(__linux__)
-
-static pthread_mutex_t mALLOC_MUTEx;
-pthread_once_t dnmalloc_once_control = PTHREAD_ONCE_INIT;
-static int dnmalloc_use_mutex = 0;
-static void dnmalloc_pthread_init_int(void)
-{
-  pthread_mutexattr_t   mta;
-  pthread_mutexattr_init(&mta);
-  pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE);
-  pthread_mutex_init(&(mALLOC_MUTEx), &mta);
-  pthread_mutexattr_destroy(&mta);       
-  pthread_atfork(dnmalloc_fork_prepare, 
-		 dnmalloc_fork_parent,
-		 dnmalloc_fork_child);
-  dnmalloc_use_mutex = 1;
-}
-int dnmalloc_pthread_init(void)
-{
-  return pthread_once(&dnmalloc_once_control, dnmalloc_pthread_init_int);
-}
-
-#else
-
-static pthread_mutex_t mALLOC_MUTEx = PTHREAD_MUTEX_INITIALIZER;
-static int dnmalloc_use_mutex = 1;
-int dnmalloc_pthread_init(void) { 
-  return pthread_atfork(dnmalloc_fork_prepare, 
-			dnmalloc_fork_parent,
-			dnmalloc_fork_child); 
-}
-#endif /* !defined(__linux__) */
-
-void dnmalloc_fork_prepare(void) { 
-  if (dnmalloc_use_mutex) 
-    pthread_mutex_lock(&mALLOC_MUTEx);
-}
-void dnmalloc_fork_parent(void) { 
-  if (dnmalloc_use_mutex)
-    pthread_mutex_unlock(&mALLOC_MUTEx); 
-}
-void dnmalloc_fork_child(void) {
-  int rc = 0;
-#ifdef __GLIBC__
-  if (dnmalloc_use_mutex)
-    {
-      pthread_mutex_unlock (&mALLOC_MUTEx);
-      pthread_mutex_destroy(&mALLOC_MUTEx);
-      rc = pthread_mutex_init(&mALLOC_MUTEx, NULL);
-    } 
-#else
-  if (dnmalloc_use_mutex)
-    rc = pthread_mutex_unlock(&mALLOC_MUTEx); 
-#endif
-  if (rc != 0)
-    {
-      fputs("fork_child failed", stderr);
-      _exit(EXIT_FAILURE);
-    }
-}
-static int dnmalloc_mutex_lock(pthread_mutex_t *mutex)
-{
-  if (dnmalloc_use_mutex)
-    {
-      int rc = pthread_mutex_lock(mutex);
-      if (rc == 0)
-	{
-	  if (!malloc_active)
-	    {
-	      ++malloc_active;
-	      return 0;
-	    }
-	  assert(malloc_active == 0);
-	  (void) pthread_mutex_unlock(mutex);
-	  errno = EDEADLK;
-	  return 1;
-	}
-      return rc;
-    }
-  return 0;
-}
-static int dnmalloc_mutex_unlock(pthread_mutex_t *mutex)
-{
-  if (dnmalloc_use_mutex)
-    {
-      --malloc_active;
-      return pthread_mutex_unlock(mutex);
-    }
-  return 0;
-}
-# define MALLOC_PREACTION   dnmalloc_mutex_lock(&mALLOC_MUTEx)
-# define MALLOC_POSTACTION  dnmalloc_mutex_unlock(&mALLOC_MUTEx)
-
-# endif
-
-#else
-
-/* Substitute anything you like for these */
-
-# define MALLOC_PREACTION   (0)
-# define MALLOC_POSTACTION  (0)
-int dnmalloc_pthread_init(void) { return 0; }
-
-#endif /* USE_MALLOC_LOCK */
-
-Void_t* public_mALLOc(size_t bytes) {
-  Void_t* m;
-  if (MALLOC_PREACTION == 0) {
-    m = mALLOc(bytes);
-    (void) MALLOC_POSTACTION;
-    return m;
-  }
-  return 0;
-}
-
-void public_fREe(Void_t* m) {
-  if (MALLOC_PREACTION == 0) {
-    fREe(m);
-    (void) MALLOC_POSTACTION;
-  }
-}
-
-Void_t* public_rEALLOc(Void_t* m, size_t bytes) {
-  if (MALLOC_PREACTION == 0) {
-    m = rEALLOc(m, bytes);
-    (void) MALLOC_POSTACTION;
-    return m;
-  }
-  return 0;
-}
-
-Void_t* public_mEMALIGn(size_t alignment, size_t bytes) {
-  Void_t* m;
-  if (MALLOC_PREACTION == 0) {
-    m = mEMALIGn(alignment, bytes);
-    (void) MALLOC_POSTACTION;
-    return m;
-  }
-  return 0;
-}
-
-int public_posix_mEMALIGn(Void_t**memptr, size_t alignment, size_t bytes) {
-  int m, ret;
-  if ((ret = MALLOC_PREACTION) == 0) {
-    m = posix_mEMALIGn(memptr, alignment, bytes);
-    (void) MALLOC_POSTACTION;
-    return m;
-  }
-  return ret;
-}
-
-Void_t* public_vALLOc(size_t bytes) {
-  Void_t* m;
-  if (MALLOC_PREACTION == 0) {
-    m = vALLOc(bytes);
-    (void) MALLOC_POSTACTION;
-    return m;
-  }
-  return 0;
-}
-
-Void_t* public_pVALLOc(size_t bytes) {
-  Void_t* m;
-  if (MALLOC_PREACTION == 0) {
-    m = pVALLOc(bytes);
-    (void) MALLOC_POSTACTION;
-    return m;
-  }
-  return 0;
-}
-
-Void_t* public_cALLOc(size_t n, size_t elem_size) {
-  Void_t* m;
-  if (MALLOC_PREACTION == 0) {
-    m = cALLOc(n, elem_size);
-    (void) MALLOC_POSTACTION;
-    return m;
-  }
-  return 0;
-}
-
-int public_mTRIm(size_t s) {
-  int result;
-  if (MALLOC_PREACTION == 0) {
-    result = mTRIm(s);
-    (void) MALLOC_POSTACTION;
-    return result;
-  }
-  return 0;
-}
-
-size_t public_mUSABLe(Void_t* m) {
-  size_t result;
-  if (MALLOC_PREACTION == 0) {
-    result = mUSABLe(m);
-    (void) MALLOC_POSTACTION;
-    return result;
-  }
-  return 0;
-}
-
-void public_mSTATs() {
-  if (MALLOC_PREACTION == 0) {
-    mSTATs();
-    (void) MALLOC_POSTACTION;
-  }
-}
-
-struct mallinfo2 public_mALLINFo2() {
-  struct mallinfo2 m;
-  if (MALLOC_PREACTION == 0) {
-    m = mALLINFo2();
-    (void) MALLOC_POSTACTION;
-    return m;
-  } else {
-    struct mallinfo2 nm = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
-    return nm;
-  }
-}
-
-int public_mALLOPt(int p, int v) {
-  int result;
-  if (MALLOC_PREACTION == 0) {
-    result = mALLOPt(p, v);
-    (void) MALLOC_POSTACTION;
-    return result;
-  }
-  return 0;
-}
-
-#else
-
-int dnmalloc_pthread_init(void) { return 0; }
-#define DL_STATIC
-
-#endif /* USE_PUBLIC_MALLOC_WRAPPERS */
-
-
-
-/* ------------- Optional versions of memcopy ---------------- */
-
-
-#if USE_MEMCPY
-
-/* 
-  Note: memcpy is ONLY invoked with non-overlapping regions,
-  so the (usually slower) memmove is not needed.
-*/
-
-#define MALLOC_COPY(dest, src, nbytes)  memcpy(dest, src, nbytes)
-#define MALLOC_ZERO(dest, nbytes)       memset(dest, 0,   nbytes)
-
-#else /* !USE_MEMCPY */
-
-/* Use Duff's device for good zeroing/copying performance. */
-
-#define MALLOC_ZERO(charp, nbytes)                                            \
-do {                                                                          \
-  INTERNAL_SIZE_T* mzp = (INTERNAL_SIZE_T*)(charp);                           \
-  CHUNK_SIZE_T  mctmp = (nbytes)/sizeof(INTERNAL_SIZE_T);                     \
-  long mcn;                                                                   \
-  if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp %= 8; }             \
-  switch (mctmp) {                                                            \
-    case 0: for(;;) { *mzp++ = 0;                                             \
-    case 7:           *mzp++ = 0;                                             \
-    case 6:           *mzp++ = 0;                                             \
-    case 5:           *mzp++ = 0;                                             \
-    case 4:           *mzp++ = 0;                                             \
-    case 3:           *mzp++ = 0;                                             \
-    case 2:           *mzp++ = 0;                                             \
-    case 1:           *mzp++ = 0; if(mcn <= 0) break; mcn--; }                \
-  }                                                                           \
-} while(0)
-
-#define MALLOC_COPY(dest,src,nbytes)                                          \
-do {                                                                          \
-  INTERNAL_SIZE_T* mcsrc = (INTERNAL_SIZE_T*) src;                            \
-  INTERNAL_SIZE_T* mcdst = (INTERNAL_SIZE_T*) dest;                           \
-  CHUNK_SIZE_T  mctmp = (nbytes)/sizeof(INTERNAL_SIZE_T);                     \
-  long mcn;                                                                   \
-  if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp %= 8; }             \
-  switch (mctmp) {                                                            \
-    case 0: for(;;) { *mcdst++ = *mcsrc++;                                    \
-    case 7:           *mcdst++ = *mcsrc++;                                    \
-    case 6:           *mcdst++ = *mcsrc++;                                    \
-    case 5:           *mcdst++ = *mcsrc++;                                    \
-    case 4:           *mcdst++ = *mcsrc++;                                    \
-    case 3:           *mcdst++ = *mcsrc++;                                    \
-    case 2:           *mcdst++ = *mcsrc++;                                    \
-    case 1:           *mcdst++ = *mcsrc++; if(mcn <= 0) break; mcn--; }       \
-  }                                                                           \
-} while(0)
-
-#endif
-
-/* ------------------ MMAP support ------------------  */
-
-
-#if defined(HAVE_FCNTL_H)
-#include <fcntl.h>
-#endif
-
-#if defined(HAVE_SYS_MMAN_H)
-#include <sys/mman.h>
-#endif
-
-#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
-#define MAP_ANONYMOUS MAP_ANON
-#endif
-
-/* 
-   Nearly all versions of mmap support MAP_ANONYMOUS, 
-   so the following is unlikely to be needed, but is
-   supplied just in case.
-*/
-
-#ifndef MAP_ANONYMOUS
-
-/* rw 19.05.2008 changed to avoid cached file descriptor, untested 
- */
-void * anon_mmap (void *addr, size_t length, int prot, int flags)
-{
-  void * retval   = NULL;
-  int dev_zero_fd = -1; /* File descriptor for /dev/zero. */
-
-  dev_zero_fd = open("/dev/zero", O_RDWR);
-  if (dev_zero_fd >= 0)
-    {
-      retval = mmap((addr), (size), (prot), (flags), dev_zero_fd, 0);
-      /* closing the file descriptor does not unmap the region */
-      close(dev_zero_fd); 
-    }
-  return retval;
-}
-  
-#define MMAP(addr, size, prot, flags) \
-  (anon_mmap((addr), (size), (prot), (flags)))
-
-
-#else /* have MAP_ANONYMOUS */
-
-#if !defined(MAP_32BIT) && defined(MAP_ADDR32)
-#define MAP_32BIT MAP_ADDR32
-#endif
-
-#if defined(MAP_32BIT)
-#define MMAP(addr, size, prot, flags) \
- (mmap((addr), (size), (prot), (flags)|MAP_ANONYMOUS|MAP_32BIT, -1, 0))
-#elif defined(__sun)
-/* 
- * Hint an address within 32bit address space
- */
-#define MMAP(addr, size, prot, flags) \
- (mmap((void*)0xC0000000, (size), (prot), (flags)|MAP_ANONYMOUS, -1, 0))
-#else
-/* *BSD */
-#define MMAP(addr, size, prot, flags) \
- (mmap((void*)0x80000000, (size), (prot), (flags)|MAP_ANONYMOUS, -1, 0))
-#endif
-
-#endif /* have MAP_ANONYMOUS */
-
-
-/*
-  -----------------------  Chunk representations -----------------------
-*/
-
-typedef void * mchunkptr;
-
-struct chunkinfo {
-   INTERNAL_SIZE_T      prev_size;  /* Size of previous in bytes          */
-   INTERNAL_SIZE_T      size;       /* Size in bytes, including overhead. */
-   INTERNAL_SIZE_T      req;        /* Original request size, for guard.  */
-   struct chunkinfo* hash_next;     /* contains a pointer to the next chunk 
-                                       in the linked list if the hash
-                                       value is the same as the chunk     */
-   struct chunkinfo* fd;	    /* double links -- used only if free. */
-   struct chunkinfo* bk;
-   mchunkptr chunk;
-};
-
-typedef struct chunkinfo* chunkinfoptr;
-
-struct cireginfo {
-	unsigned long position;
-	unsigned long *freebitmap;
-	struct cireginfo* next;
-	struct chunkinfo *freelist;
-	struct chunkinfo *begin; 
-	unsigned long freecounter; 
-};
-
-/*
-  ---------- Size and alignment checks and conversions ----------
-*/
-
-/* conversion from malloc headers to user pointers, and back */
-#define chunk(p) (p->chunk)
-
-
-#define chunk2mem(p)   (chunk(p))
-#define mem2chunk(mem) (hashtable_lookup(mem))
-
-/* The smallest possible chunk      */
-/* #define MIN_CHUNK_SIZE        16 */
-#if (SIZEOF_UNSIGNED_LONG == 8) || defined(__arch64__) || defined(__ia64__) || defined(__x86_64__) || defined(__LP64__) || defined(__64BIT__) || defined(_LP64) || defined(_M_IA64) || (defined(_MIPS_SZLONG) && (_MIPS_SZLONG == 64))
-#  define MIN_CHUNK_SIZE 32
-#else
-#  define MIN_CHUNK_SIZE 16
-#endif
-
-/* The smallest size we can malloc is an aligned minimal chunk */
-
-#define MINSIZE  \
-  (CHUNK_SIZE_T)(((MIN_CHUNK_SIZE+MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK))
-
-/* Check if m has acceptable alignment */
-
-#define aligned_OK(m)  (((PTR_UINT)((m)) & (MALLOC_ALIGN_MASK)) == 0)
-
-#define GUARD_SIZE 4
-
-/* 
-   Check if a request is so large that it would wrap around zero when
-   padded and aligned. To simplify some other code, the bound is made
-   low enough so that adding MINSIZE will also not wrap around zero.
-
-   Make it 4*MINSIZE.
-*/
-
-#define REQUEST_OUT_OF_RANGE(req)                                 \
-  ((CHUNK_SIZE_T)(req) >=                                         \
-   (CHUNK_SIZE_T)(INTERNAL_SIZE_T)(-4 * MINSIZE))    
-
-/* pad request bytes into a usable size -- internal version */
-
-#define request2size(req)                                         \
-  (((req) + GUARD_SIZE + MALLOC_ALIGN_MASK >= MINSIZE)  ?         \
-   ((req) + GUARD_SIZE + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK :\
-   MINSIZE)
-
-/*  Same, except also perform argument check */
-
-#define checked_request2size(req, sz)                             \
-  if (!REQUEST_OUT_OF_RANGE(req)) {                               \
-    (sz) = request2size(req);                                     \
-    assert((sz-req) >= GUARD_SIZE);                               \
-  } else {                                                        \
-    MALLOC_FAILURE_ACTION;                                        \
-    return 0;                                                     \
-  }
-
-#if PARANOIA > 2
-static char * guard_set_p;
-static char * guard_set_q;
-
-#define guard_set(guard, P, request, sz)			  \
-  assert((sz-request) >= GUARD_SIZE);                             \
-  guard_set_p = (char*)(chunk(P));                                \
-  guard_set_p += request;                                         \
-  VALGRIND_MAKE_MEM_UNDEFINED(guard_set_p,GUARD_SIZE);            \
-  guard_set_q = (char*)(guard);                                   \
-  *guard_set_p = *guard_set_q; ++guard_set_p; ++guard_set_q;      \
-  *guard_set_p = *guard_set_q; ++guard_set_p; ++guard_set_q;      \
-  *guard_set_p = *guard_set_q; ++guard_set_p; ++guard_set_q;      \
-  *guard_set_p = *guard_set_q;                                    \
-  VALGRIND_MAKE_MEM_NOACCESS((((char*)chunk(P))+request),GUARD_SIZE);	\
-  (P)->req = request
- 
-#define guard_check(guard, P)				          \
-  VALGRIND_MAKE_MEM_DEFINED((((char *)chunk(P))+(P)->req), GUARD_SIZE); \
-  assert(0 == memcmp((((char *)chunk(P))+(P)->req),(void*)(guard),GUARD_SIZE));\
-  VALGRIND_MAKE_MEM_NOACCESS((((char *)chunk(P))+(P)->req), GUARD_SIZE);
-
-#else
-#define guard_set(guard, P, request, sz) ((void)0)
-#define guard_check(guard, P) ((void)0)
-#endif /* PARANOIA > 2 */
-
-/* dnmalloc forward declarations */
-static char * dnmalloc_arc4random(void);
-static void dnmalloc_init (void);
-static void malloc_mmap_state(void);
-static void cireg_extend (void);
-static chunkinfoptr cireg_getfree (void);
-static void hashtable_add (chunkinfoptr ci);
-static void hashtable_insert (chunkinfoptr ci_orig, chunkinfoptr ci_insert);
-static void hashtable_remove (mchunkptr p);              
-static void hashtable_skiprm (chunkinfoptr ci_orig, chunkinfoptr ci_todelete);
-static chunkinfoptr hashtable_lookup (mchunkptr p);
-static chunkinfoptr next_chunkinfo (chunkinfoptr ci);          
-static chunkinfoptr prev_chunkinfo (chunkinfoptr ci);
-
-
-
-/*
-  --------------- Physical chunk operations ---------------
-*/
-
-
-/* size field is or'ed with PREV_INUSE when previous adjacent chunk in use */
-#define PREV_INUSE 0x1
-
-/* extract inuse bit of previous chunk */
-#define prev_inuse(p)       ((p)->size & PREV_INUSE)
-
-/* size field is or'ed with IS_MMAPPED if the chunk was obtained with mmap() */
-#define IS_MMAPPED 0x2
-
-/* check for mmap()'ed chunk */
-#define chunk_is_mmapped(p) ((p)->size & IS_MMAPPED)
-
-
-/* size field is or'ed when the chunk is in use */
-#define INUSE 0x4
-
-/* extract inuse bit of chunk */
-#define inuse(p)       ((p)->size & INUSE)
-
-/* 
-  Bits to mask off when extracting size 
-
-  Note: IS_MMAPPED is intentionally not masked off from size field in
-  macros for which mmapped chunks should never be seen. This should
-  cause helpful core dumps to occur if it is tried by accident by
-  people extending or adapting this malloc.
-*/
-#define SIZE_BITS (PREV_INUSE|IS_MMAPPED|INUSE)
-
-/* Bits to mask off when extracting size of chunks for macros which do not use mmap */
-#define SIZE_NOMMAP (PREV_INUSE|INUSE)
-
-/* Get size, ignoring use bits */
-#define chunksize(p)         ((p)->size & ~(SIZE_BITS))
-
-/* Ptr to chunkinfo of next physical malloc_chunk. */
-#define next_chunk(p) ((mchunkptr)( ((char*)(p)) + ((p)->size & SIZE_NOMMAP) ))
-
-/* Treat space at ptr + offset as a chunk */
-#define chunk_at_offset(p, s)  ((mchunkptr)(((char*)(p)) + (s)))
-
-/* set/clear chunk as being inuse without otherwise disturbing */
-#define set_inuse(p) ((p)->size |= INUSE)
-
-#define clear_inuse(p) ((p)->size &= ~(INUSE))
-
-#define set_previnuse(p) ((p)->size |= PREV_INUSE)
-
-#define clear_previnuse(p) ((p)->size &= ~(PREV_INUSE))
-
-static void set_previnuse_next (chunkinfoptr p)
-{
-   chunkinfoptr q;
-   q = next_chunkinfo (p);
-   if (q)
-      set_previnuse (q);
-}
-
-#define set_all_inuse(p) \
-set_inuse(p); \
-set_previnuse_next(p);
-
-
-/* Set size at head, without disturbing its use bit */
-#define set_head_size(p, s)  ((p)->size = (((p)->size & SIZE_NOMMAP) | (s)))
-
-/* Set size/use field */
-#define set_head(p, s)       ((p)->size = (s))
-
-/*
-  Bins
-
-    An array of bin headers for free chunks. Each bin is doubly
-    linked.  The bins are approximately proportionally (log) spaced.
-    There are a lot of these bins (128). This may look excessive, but
-    works very well in practice.  Most bins hold sizes that are
-    unusual as malloc request sizes, but are more usual for fragments
-    and consolidated sets of chunks, which is what these bins hold, so
-    they can be found quickly.  All procedures maintain the invariant
-    that no consolidated chunk physically borders another one, so each
-    chunk in a list is known to be preceeded and followed by either
-    inuse chunks or the ends of memory.
-
-    Chunks in bins are kept in size order, with ties going to the
-    approximately least recently used chunk. Ordering isn't needed
-    for the small bins, which all contain the same-sized chunks, but
-    facilitates best-fit allocation for larger chunks. These lists
-    are just sequential. Keeping them in order almost never requires
-    enough traversal to warrant using fancier ordered data
-    structures.  
-
-    Chunks of the same size are linked with the most
-    recently freed at the front, and allocations are taken from the
-    back.  This results in LRU (FIFO) allocation order, which tends
-    to give each chunk an equal opportunity to be consolidated with
-    adjacent freed chunks, resulting in larger free chunks and less
-    fragmentation.
-
-    To simplify use in double-linked lists, each bin header acts
-    as a malloc_chunk. This avoids special-casing for headers.
-    But to conserve space and improve locality, we allocate
-    only the fd/bk pointers of bins, and then use repositioning tricks
-    to treat these as the fields of a malloc_chunk*.  
-*/
-
-typedef struct chunkinfo* mbinptr;
-
-/* addressing -- note that bin_at(0) does not exist */
-#define bin_at(m, i) (&(m)->bins[i])
-
-/* analog of ++bin */
-#define next_bin(b)  (b+1)
-
-/* Reminders about list directionality within bins */
-#define first(b)     ((b)->fd)
-#define last(b)      ((b)->bk)
-
-/* Take a chunk off a bin list */
-#define unlink(P, BK, FD) {                                            \
-  FD = P->fd;                                                          \
-  BK = P->bk;                                                          \
-  FD->bk = BK;                                                         \
-  BK->fd = FD;                                                         \
-}
-
-/*
-  Indexing
-
-    Bins for sizes < 512 bytes contain chunks of all the same size, spaced
-    8 bytes apart. Larger bins are approximately logarithmically spaced:
-
-    64 bins of size       8
-    32 bins of size      64
-    16 bins of size     512
-     8 bins of size    4096
-     4 bins of size   32768
-     2 bins of size  262144
-     1 bin  of size what's left
-
-    The bins top out around 1MB because we expect to service large
-    requests via mmap.
-*/
-
-#define NBINS              96
-#define NSMALLBINS         32
-#define SMALLBIN_WIDTH      8
-#define MIN_LARGE_SIZE    256
-
-#define in_smallbin_range(sz)  \
-  ((CHUNK_SIZE_T)(sz) < (CHUNK_SIZE_T)MIN_LARGE_SIZE)
-
-#define smallbin_index(sz)     (((unsigned)(sz)) >> 3)
-
-/*
-  Compute index for size. We expect this to be inlined when
-  compiled with optimization, else not, which works out well.
-*/
-static int largebin_index(size_t sz) {
-
-  unsigned long  xx = sz >> SMALLBIN_WIDTH; 
-
-  if (xx < 0x10000) 
-    {
-      unsigned int  m;           /* bit position of highest set bit of m */
-      
-      /* On intel, use BSRL instruction to find highest bit */
-#if defined(__GNUC__) && defined(i386) && !defined(USE_UNO)
-
-      unsigned int  x = (unsigned int) xx;
-
-      __asm__("bsrl %1,%0\n\t"
-	      : "=r" (m) 
-	      : "rm"  (x));
-
-#elif defined(__GNUC__) && defined(x86_64) && !defined(USE_UNO)
-
-      __asm__("bsrq %1,%0\n\t"
-              : "=r" (m)
-              : "rm"  (xx));
-
-#else
-
-      /* Taken from Bit Twiddling Hacks
-       * http://graphics.stanford.edu/~seander/bithacks.html
-       * public domain
-       */
-      unsigned int  v  = (unsigned int) xx;
-      register unsigned int shift;
-      
-      m =     (v > 0xFFFF) << 4; v >>= m;
-      shift = (v > 0xFF  ) << 3; v >>= shift; m |= shift;
-      shift = (v > 0xF   ) << 2; v >>= shift; m |= shift;
-      shift = (v > 0x3   ) << 1; v >>= shift; m |= shift;
-      m |= (v >> 1);
-      
-#endif
-      
-      /* Use next 2 bits to create finer-granularity bins */
-      return NSMALLBINS + (m << 2) + ((sz >> (m + 6)) & 3);
-    }
-  else
-    {
-      return NBINS-1;
-    }
-}
-
-#define bin_index(sz) \
- ((in_smallbin_range(sz)) ? smallbin_index(sz) : largebin_index(sz))
-
-/*
-  FIRST_SORTED_BIN_SIZE is the chunk size corresponding to the
-  first bin that is maintained in sorted order. This must
-  be the smallest size corresponding to a given bin.
-
-  Normally, this should be MIN_LARGE_SIZE. But you can weaken
-  best fit guarantees to sometimes speed up malloc by increasing value.
-  Doing this means that malloc may choose a chunk that is 
-  non-best-fitting by up to the width of the bin.
-
-  Some useful cutoff values:
-      512 - all bins sorted
-     2560 - leaves bins <=     64 bytes wide unsorted  
-    12288 - leaves bins <=    512 bytes wide unsorted
-    65536 - leaves bins <=   4096 bytes wide unsorted
-   262144 - leaves bins <=  32768 bytes wide unsorted
-       -1 - no bins sorted (not recommended!)
-*/
-
-/* #define FIRST_SORTED_BIN_SIZE 65536 */
-
-#define FIRST_SORTED_BIN_SIZE MIN_LARGE_SIZE
-
-
-/*
-  Unsorted chunks
-
-    All remainders from chunk splits, as well as all returned chunks,
-    are first placed in the "unsorted" bin. They are then placed
-    in regular bins after malloc gives them ONE chance to be used before
-    binning. So, basically, the unsorted_chunks list acts as a queue,
-    with chunks being placed on it in free (and malloc_consolidate),
-    and taken off (to be either used or placed in bins) in malloc.
-*/
-
-/* The otherwise unindexable 1-bin is used to hold unsorted chunks. */
-#define unsorted_chunks(M)          (bin_at(M, 1))
-
-/*
-  Top
-
-    The top-most available chunk (i.e., the one bordering the end of
-    available memory) is treated specially. It is never included in
-    any bin, is used only if no other chunk is available, and is
-    released back to the system if it is very large (see
-    M_TRIM_THRESHOLD).  Because top initially
-    points to its own bin with initial zero size, thus forcing
-    extension on the first malloc request, we avoid having any special
-    code in malloc to check whether it even exists yet. But we still
-    need to do so when getting memory from system, so we make
-    initial_top treat the bin as a legal but unusable chunk during the
-    interval between initialization and the first call to
-    sYSMALLOc. (This is somewhat delicate, since it relies on
-    the 2 preceding words to be zero during this interval as well.)
-*/
-
-/* Conveniently, the unsorted bin can be used as dummy top on first call */
-#define initial_top(M)              (unsorted_chunks(M))
-
-/*
-  Binmap
-
-    To help compensate for the large number of bins, a one-level index
-    structure is used for bin-by-bin searching.  `binmap' is a
-    bitvector recording whether bins are definitely empty so they can
-    be skipped over during during traversals.  The bits are NOT always
-    cleared as soon as bins are empty, but instead only
-    when they are noticed to be empty during traversal in malloc.
-*/
-
-/* Conservatively use 32 bits per map word, even if on 64bit system */
-#define BINMAPSHIFT      5
-#define BITSPERMAP       (1U << BINMAPSHIFT)
-#define BINMAPSIZE       (NBINS / BITSPERMAP)
-
-#define idx2block(i)     ((i) >> BINMAPSHIFT)
-#define idx2bit(i)       ((1U << ((i) & ((1U << BINMAPSHIFT)-1))))
-
-#define mark_bin(m,i)    ((m)->binmap[idx2block(i)] |=  idx2bit(i))
-#define unmark_bin(m,i)  ((m)->binmap[idx2block(i)] &= ~(idx2bit(i)))
-#define get_binmap(m,i)  ((m)->binmap[idx2block(i)] &   idx2bit(i))
-
-/*
-  Fastbins
-
-    An array of lists holding recently freed small chunks.  Fastbins
-    are not doubly linked.  It is faster to single-link them, and
-    since chunks are never removed from the middles of these lists,
-    double linking is not necessary. Also, unlike regular bins, they
-    are not even processed in FIFO order (they use faster LIFO) since
-    ordering doesn't much matter in the transient contexts in which
-    fastbins are normally used.
-
-    Chunks in fastbins keep their inuse bit set, so they cannot
-    be consolidated with other free chunks. malloc_consolidate
-    releases all chunks in fastbins and consolidates them with
-    other free chunks. 
-*/
-
-typedef struct chunkinfo* mfastbinptr;
-
-/* offset 2 to use otherwise unindexable first 2 bins */
-#define fastbin_index(sz)        ((((unsigned int)(sz)) >> 3) - 2)
-
-/* The maximum fastbin request size we support */
-#define MAX_FAST_SIZE     80
-
-#define NFASTBINS  (fastbin_index(request2size(MAX_FAST_SIZE))+1)
-
-/*
-  FASTBIN_CONSOLIDATION_THRESHOLD is the size of a chunk in free()
-  that triggers automatic consolidation of possibly-surrounding
-  fastbin chunks. This is a heuristic, so the exact value should not
-  matter too much. It is defined at half the default trim threshold as a
-  compromise heuristic to only attempt consolidation if it is likely
-  to lead to trimming. However, it is not dynamically tunable, since
-  consolidation reduces fragmentation surrounding loarge chunks even 
-  if trimming is not used.
-*/
-
-#define FASTBIN_CONSOLIDATION_THRESHOLD  \
-  ((unsigned long)(DEFAULT_TRIM_THRESHOLD) >> 1)
-
-/*
-  Since the lowest 2 bits in max_fast don't matter in size comparisons, 
-  they are used as flags.
-*/
-
-/*
-  ANYCHUNKS_BIT held in max_fast indicates that there may be any
-  freed chunks at all. It is set true when entering a chunk into any
-  bin.
-*/
-
-#define ANYCHUNKS_BIT        (1U)
-
-#define have_anychunks(M)     (((M)->max_fast &  ANYCHUNKS_BIT))
-#define set_anychunks(M)      ((M)->max_fast |=  ANYCHUNKS_BIT)
-#define clear_anychunks(M)    ((M)->max_fast &= ~ANYCHUNKS_BIT)
-
-/*
-  FASTCHUNKS_BIT held in max_fast indicates that there are probably
-  some fastbin chunks. It is set true on entering a chunk into any
-  fastbin, and cleared only in malloc_consolidate.
-*/
-
-#define FASTCHUNKS_BIT        (2U)
-
-#define have_fastchunks(M)   (((M)->max_fast &  FASTCHUNKS_BIT))
-#define set_fastchunks(M)    ((M)->max_fast |=  (FASTCHUNKS_BIT|ANYCHUNKS_BIT))
-#define clear_fastchunks(M)  ((M)->max_fast &= ~(FASTCHUNKS_BIT))
-
-/* 
-   Set value of max_fast. 
-   Use impossibly small value if 0.
-*/
-
-#define set_max_fast(M, s) \
-  (M)->max_fast = (((s) == 0)? SMALLBIN_WIDTH: request2size(s)) | \
-  ((M)->max_fast &  (FASTCHUNKS_BIT|ANYCHUNKS_BIT))
-
-#define get_max_fast(M) \
-  ((M)->max_fast & ~(FASTCHUNKS_BIT | ANYCHUNKS_BIT))
-
-
-/*
-  morecore_properties is a status word holding dynamically discovered
-  or controlled properties of the morecore function
-*/
-
-#define MORECORE_CONTIGUOUS_BIT  (1U)
-
-#define contiguous(M) \
-        (((M)->morecore_properties &  MORECORE_CONTIGUOUS_BIT))
-#define noncontiguous(M) \
-        (((M)->morecore_properties &  MORECORE_CONTIGUOUS_BIT) == 0)
-#define set_contiguous(M) \
-        ((M)->morecore_properties |=  MORECORE_CONTIGUOUS_BIT)
-#define set_noncontiguous(M) \
-        ((M)->morecore_properties &= ~MORECORE_CONTIGUOUS_BIT)
-
-#define MORECORE_32BIT_BIT  (2U)
-
-#define morecore32bit(M) \
-        (((M)->morecore_properties &  MORECORE_32BIT_BIT))
-#define nonmorecore32bit(M) \
-        (((M)->morecore_properties &  MORECORE_32BIT_BIT) == 0)
-#define set_morecore32bit(M) \
-        ((M)->morecore_properties |=  MORECORE_32BIT_BIT)
-#define set_nonmorecore32bit(M) \
-        ((M)->morecore_properties &= ~MORECORE_32BIT_BIT)
-
-
-
-/* ----------------- dnmalloc -------------------- */
-
-/* size of pages */
-#define PGSIZE malloc_getpagesize
-/* pointer size */
-#define PTRSIZE sizeof(long)
-
-
-
-/* TODO: mmapped chunks are always multiples of pagesize -> we're wasting 
-   address space: the hashtable has granularity of 16*8, set it to something 
-   closer to pagesize for mmapped chunks (current waste: 32 positions/mmapped 
-   page) 
-*/
-
-/* The maximum heap size that dnmalloc can operate with
- * represented in hex to avoid annoying gcc warning
- *
- * Avoid integer overflow, cover complete 32bit address 
- * space for portability. With deferred allocation, the
- * hashtable size is a non-issue.
- */
-#define HEAPMAXSIZE_HALF 0x80000000UL
-
-/* How many elements are stored in the linked list */
-#define LINKEDLSTELS 8
-
-/* Minimum size of a chunk */
-
-#if (SIZEOF_UNSIGNED_LONG == 8) || defined(__arch64__) || defined(__ia64__) || defined(__x86_64__) || defined(__LP64__) || defined(__64BIT__) || defined(_LP64) || defined(_M_IA64) || (defined(_MIPS_SZLONG) && (_MIPS_SZLONG == 64))
-#  define MINCHUNKSIZE 32
-#else
-#  define MINCHUNKSIZE 16
-#endif
-
-
-/* The amount of hashtable entries for each page:
-Pagesize divded by the numer of elements in the linkedlists
-divided by the minimum chunk size
-*/
-#define CHUNKINFOPAGE (PGSIZE / LINKEDLSTELS / MINCHUNKSIZE)
-
-/* The amount of hashtable entries needed to manage the memory:
-Maximum heap size divided by page size multiplied by the amount
-of chunk info's per page 
-*/
-#define AMOUNTHASH ((HEAPMAXSIZE_HALF / PGSIZE) * CHUNKINFOPAGE * 2)
-
-/* Initial size of the map for the hashtable 
-Amount of entries muliplied by pointer size
-*/
-#define  HASHTABLESIZE (AMOUNTHASH * PTRSIZE)
-
-/* Amount of free chunks that the system should allocate at the start */
-#define NUMBER_FREE_CHUNKS 32768
-
-/* Initial size of the chunk info region, 
-also used when growing the region */
-#define CIREGSIZE (NUMBER_FREE_CHUNKS * sizeof(struct chunkinfo))
-
-/* Start address of the heap */
-char *startheap;
-
-/* pointer to the hashtable: struct chunkinfo **hashtable -> *hashtable[] */
-chunkinfoptr *hashtable;
-
-/* Current chunkinfo region */
-struct cireginfo *currciinfo = 0;
-struct cireginfo *firstciinfo = 0;
-
-unsigned long totalcictr = 0;
-
-
-/* Initialize the area for chunkinfos and the hashtable and protect 
- * it with non-writable pages 
- */
-static void
-dnmalloc_init ()
-{
-   void *hashtb;
-   int mprot;
-   int flags = MAP_PRIVATE;
-
-   /* Allocate the malloc_state struct */
-   malloc_mmap_state();
-
-   /* Use MAP_NORESERVE if available (Solaris, HP-UX; most other
-    * systems use defered allocation anyway.
-    */
-#ifdef MAP_NORESERVE
-   flags |= MAP_NORESERVE;
-#endif
-
-   /* Always start at 0, hashtable covers whole 32bit address space
-    */
-#define STARTHEAP_IS_ZERO
-   startheap = 0;
-
-   /* Map space for the hashtable */
-#if PARANOIA > 1
-   hashtb = MMAP(0, HASHTABLESIZE+(2*PGSIZE), PROT_READ|PROT_WRITE, flags);
-#else
-   hashtb = MMAP(0, HASHTABLESIZE+PGSIZE, PROT_READ|PROT_WRITE, flags);
-#endif
-
-#ifdef NDEBUG
-   if (hashtb == MAP_FAILED) {
-      fprintf (stderr, "Couldn't mmap hashtable: %s\n", strerror (errno));
-      abort ();
-   }
-#else
-   assert(hashtb != MAP_FAILED);
-#endif
-
-   /* Protect the hashtable with non-writable pages */
-   mprot = mprotect(hashtb, (size_t) PGSIZE, PROT_NONE);
-#ifdef NDEBUG
-   if (mprot == -1) {
-     fprintf (stderr, "Couldn't mprotect first non-rw page for hashtable: %s\n",
-	      strerror (errno));
-     abort ();
-   }
-#else
-   assert(mprot != -1);
-#endif
-
-   /* HP-UX: Cannot do arithmetic with pointers to objects of unknown size. */
-   hashtable = (chunkinfoptr *) (((char*)hashtb) + PGSIZE);
-
-   /* Protect the hashtable with non-writable pages */
-#if PARANOIA > 1
-   mprot = mprotect((void*)((char*)hashtb+HASHTABLESIZE+PGSIZE), (size_t) PGSIZE, PROT_NONE);
-#ifdef NDEBUG
-   if (mprot == -1) {
-     fprintf (stderr, "Couldn't mprotect last non-rw page for hashtable: %s\n",
-	      strerror (errno));
-     abort ();
-   }
-#else
-   assert(mprot != -1);
-#endif
-#endif
-}
-
-
-
-/* Extend the region for chunk infos by mapping more memory before the region */
-static void
-cireg_extend ()
-{
-   void *newcireg;
-   int mprot;
-   struct cireginfo *tempciinfo = 0;
-   
-#if PARANOIA > 1
-   newcireg = MMAP(0, CIREGSIZE+(2*PGSIZE), PROT_READ|PROT_WRITE, MAP_PRIVATE);
-#else
-   newcireg = MMAP(0, CIREGSIZE+PGSIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE);
-#endif
-
-#ifdef NDEBUG
-   if (newcireg == MAP_FAILED)
-   {
-	   fprintf (stderr, "Couldn't extend chunkinfo region: %s\n",
-		    strerror (errno));
-	   abort ();
-   }
-#else
-   assert(newcireg != MAP_FAILED);
-#endif
-   mprot = mprotect(newcireg, PGSIZE, PROT_NONE);
-#ifdef NDEBUG
-   if (mprot == -1) {
-	   fprintf (stderr, "Couldn't mprotect first non-rw page for extended region: %s\n",
-		    strerror (errno));
-	   abort ();
-   }
-#else
-   assert(mprot != -1);
-#endif
-   newcireg = ((char*)newcireg)+PGSIZE;
-   
-#if PARANOIA > 1
-   mprot = mprotect((void*)((char*)newcireg+CIREGSIZE), (size_t) PGSIZE, PROT_NONE);
-#ifdef NDEBUG
-   if (mprot == -1) {
-     fprintf (stderr, "Couldn't mprotect last non-rw page for extended region: %s\n",
-	      strerror (errno));
-     abort ();
-   }
-#else
-   assert(mprot != -1);
-#endif
-#endif
-
-   tempciinfo = currciinfo;
-   currciinfo = (struct cireginfo *) newcireg;
-   if (tempciinfo)
-	   tempciinfo->next = currciinfo;
-   currciinfo->position = 1;
-   currciinfo->freecounter = NUMBER_FREE_CHUNKS;
-   if (!firstciinfo)
-	   firstciinfo = currciinfo;
-   totalcictr++;
-   VALGRIND_CREATE_MEMPOOL(newcireg, 0, 0);
-}
-
-
-/* Get a free chunkinfo */
-static chunkinfoptr
-cireg_getfree ()
-{
-   chunkinfoptr freeci;
-   chunkinfoptr freelst = 0;
-   struct cireginfo *newciinfo = firstciinfo;
-   
-   if (newciinfo) {
-   	freelst = newciinfo->freelist;
-    
-	if (!freelst && newciinfo->next) {
-	  do {
-	    newciinfo = newciinfo->next;
-	    freelst = newciinfo->freelist;
-	  } while (!freelst && newciinfo->next);
-	}
-   }
-
-   /* Check if there are any free chunkinfos on the list of free chunkinfos */
-   if (freelst)
-   {
-     freeci = freelst;
-     newciinfo->freecounter--;
-     newciinfo->freelist = freelst->fd;
-     
-     VALGRIND_MEMPOOL_ALLOC((char*)currciinfo, (char*)freeci, 
-			    sizeof(struct chunkinfo));
-     
-     freeci->prev_size = 0;
-     freeci->size      = 0;
-     freeci->req       = 0;
-     freeci->hash_next = NULL;
-     freeci->fd        = NULL;
-     freeci->bk        = NULL;
-     freeci->chunk     = NULL;
-     return (freeci);
-   }
-   else
-   {
-     /* No free chunkinfos, check if chunkinfo region still has place 
-      * for a chunkinfo. If not, extend the region. 
-      */
-     if (UNLIKELY(!currciinfo || currciinfo->position == NUMBER_FREE_CHUNKS))
-       cireg_extend ();
-     /* Get a chunkinfo from the chunkinfo region */
-     freeci = (chunkinfoptr) currciinfo + currciinfo->position; 
-     currciinfo->freecounter--;
-     currciinfo->position++;
-
-     VALGRIND_MEMPOOL_ALLOC((char*)currciinfo, (char*)freeci, 
-			    sizeof(struct chunkinfo));
-
-     return (freeci);
-   }
-}
-
-static void freeciregion(struct cireginfo *freeme) {
-  /* free the chunkinfo region */
-  struct cireginfo *newciinfo = firstciinfo;
-  struct cireginfo *prevciinfo = firstciinfo;
-  void *unmapme;
-
-  while (newciinfo && newciinfo != freeme) {
-    prevciinfo = newciinfo;
-    newciinfo = newciinfo->next;
-  }
-  assert(freeme == newciinfo); /* rw */
-  assert(newciinfo != NULL);   /* rw */
-  if (newciinfo)
-    prevciinfo->next = newciinfo->next;
-  unmapme = (void *) ((char*)freeme - PGSIZE);
-  VALGRIND_DESTROY_MEMPOOL((char*)freeme);
-#if PARANOIA > 1
-  munmap(unmapme, CIREGSIZE+(2*PGSIZE));
-#else
-  munmap(unmapme, CIREGSIZE+PGSIZE);
-#endif
-}
-
-
-static void freecilst_add(chunkinfoptr p) {
-
-  struct cireginfo *newciinfo;
-
-  newciinfo = currciinfo;
-  if (((chunkinfoptr) newciinfo < p) && (p  <  (chunkinfoptr) (newciinfo+NUMBER_FREE_CHUNKS))) {
-    p->fd = newciinfo->freelist;
-    newciinfo->freelist = p;
-    newciinfo->freecounter++;
-    VALGRIND_MEMPOOL_FREE((char*)newciinfo, (char*)p);
-    VALGRIND_MAKE_MEM_DEFINED(p,sizeof(struct chunkinfo));
-    VALGRIND_MAKE_MEM_NOACCESS(p->size,     sizeof(INTERNAL_SIZE_T));
-    VALGRIND_MAKE_MEM_NOACCESS(p->req,      sizeof(INTERNAL_SIZE_T));
-    VALGRIND_MAKE_MEM_NOACCESS(p->bk,       sizeof(struct chunkinfo*));
-    VALGRIND_MAKE_MEM_NOACCESS(p->chunk,    sizeof(mchunkptr));
-  } else {
-    newciinfo = firstciinfo;
-    if (newciinfo) {
-      do {
-	if (((chunkinfoptr) newciinfo < p) && (p  <  (chunkinfoptr) (newciinfo+NUMBER_FREE_CHUNKS))) {
-	  p->fd = newciinfo->freelist;
-	  newciinfo->freelist = p;
-	  newciinfo->freecounter++;
-	  VALGRIND_MEMPOOL_FREE((char*)newciinfo, (char*)p);
-	  VALGRIND_MAKE_MEM_DEFINED(p,sizeof(struct chunkinfo));
-	  VALGRIND_MAKE_MEM_NOACCESS(p->size,     sizeof(INTERNAL_SIZE_T));
-	  VALGRIND_MAKE_MEM_NOACCESS(p->req,      sizeof(INTERNAL_SIZE_T));
-	  VALGRIND_MAKE_MEM_NOACCESS(p->bk,       sizeof(struct chunkinfo*));
-	  VALGRIND_MAKE_MEM_NOACCESS(p->chunk,    sizeof(mchunkptr));
-	  if (UNLIKELY(newciinfo->freecounter == NUMBER_FREE_CHUNKS))
-	    freeciregion(newciinfo);
-	  break;
-	}
-	newciinfo = newciinfo->next;
-      } while (newciinfo);
-    }
-  }
-}
-
-/* Calculate the hash table entry for a chunk */
-#ifdef STARTHEAP_IS_ZERO
-#define hash(p)  (((unsigned long) p) >> 7)
-#else
-#define hash(p)  (((unsigned long) p - (unsigned long) startheap) >> 7)
-#endif
-
-static void
-hashtable_add (chunkinfoptr ci)
-{
-   chunkinfoptr temp, next;
-   unsigned long hashval;
-   mchunkptr cic = chunk (ci);
-   
-   hashval = hash (cic);
-
-   if (hashval < AMOUNTHASH) {
-
-     temp = hashtable[hashval];
-
-#ifdef DNMALLOC_DEBUG
-     fprintf(stderr, "hashtable_add: %p, %lu\n", chunk(ci), hashval);
-#endif
-
-     /* If no pointer to a chunk info list is stored at this location 
-      * in the hashtable or if the chunk's address is smaller than the 
-      * one present, add the chunk to the front of the linked list
-      */
-     if (temp == 0 || chunk (temp) > cic)
-       {
-	 ci->hash_next = temp;
-	 hashtable[hashval] = ci;
-	 if (!temp) /* more likely case */
-	   goto out;
-	 temp->prev_size = chunksize(ci);
-	 return;
-       }
-     else
-       {
-	 /* We must place the chunk in the linked list for this hashentry
-	  * Loop to end of list or to a position where temp's chunk's address 
-	  * is larger than the new chunkinfo's chunk's address
-	  */
-	 if (!temp->hash_next || (chunk (temp->hash_next) > cic))
-	   {
-	     ci->hash_next = temp->hash_next;
-	     temp->hash_next = ci;
-	   }
-	 else
-	   {
-	     while ((temp->hash_next != 0) && (chunk (temp->hash_next) < cic))
-	       {
-		 temp = temp->hash_next;
-	       }
-	     /* Place in linked list if not already there */
-	     if (!temp->hash_next || !(chunk (temp->hash_next) == cic))
-	       {
-		 ci->hash_next = temp->hash_next;
-		 temp->hash_next = ci;
-	       }
-	   }
-       }
-   }
-   else {
-#ifdef DNMALLOC_CHECKS
-     if (hashval >= AMOUNTHASH) {
-       fprintf(stderr, "Dnmalloc error: trying to write outside of the bounds of the hashtable, this is definitely a bug, please email dnmalloc@fort-knox.org (hashval: %lu, AMOUNTHASH: %lu, HEAPMAXSIZE_HALF %lu PGSIZE %ld CHUNKINFOPAGE %ld chunk: %p, chunkinfo: %p, startheap: %p).\n", hashval, AMOUNTHASH, HEAPMAXSIZE_HALF, PGSIZE, CHUNKINFOPAGE, chunk(ci), ci, startheap);
-	   abort();
-     }
-#else
-     assert(hashval < AMOUNTHASH);
-#endif
-   }
-
- out:
-   next = next_chunkinfo(ci);
-   if (!next)
-     return;
-   next->prev_size = chunksize(ci);
-}
-
-static void
-hashtable_insert (chunkinfoptr ci_orig, chunkinfoptr ci_insert)
-{
-   chunkinfoptr next;
-
-#ifdef DNMALLOC_DEBUG
-   fprintf(stderr, "hashtable_ins: %p, %lu\n", chunk(ci_insert), 
-	   (unsigned long)hash(chunk(ci_insert));
-#endif
-
-   if (hash(chunk(ci_orig)) != hash(chunk(ci_insert))) {
-      hashtable_add(ci_insert);  
-   }
-   else {
-
-      ci_insert->hash_next = ci_orig->hash_next;
-      ci_orig->hash_next = ci_insert;
-
-      /* added for prevsize */
-      if (!(ci_insert->hash_next))
-	      next = next_chunkinfo(ci_insert);
-      else
-	      next = ci_insert->hash_next;
-
-      if (!next)
-	{
-	  ci_insert->prev_size = chunksize(ci_orig);
-	}
-      else
-	{
-	  next->prev_size = chunksize(ci_insert);
-	  ci_insert->prev_size = chunksize(ci_orig);
-	}
-   }
-}
-
-static void
-hashtable_remove (mchunkptr p) 
-{
-  chunkinfoptr prevtemp, temp;
-  unsigned long hashval;
-  
-  hashval = hash (p);
-#ifdef DNMALLOC_DEBUG
-  fprintf(stderr, "hashtable_rem: %p, %lu\n", p, hashval);
-#endif
-  assert(hashval < AMOUNTHASH); /* rw */
-  prevtemp = temp = hashtable[hashval];
-  if (chunk (temp) == p) {
-    hashtable[hashval] = temp->hash_next;
-  } 
-  else
-    {
-      if (temp && chunk (temp) != p) {
-	do
-	  {
-	    prevtemp = temp;
-	    temp = temp->hash_next;
-	  } while (temp && chunk (temp) != p);
-      }
-#ifdef DNMALLOC_CHECKS
-      if (!temp) {
-	fprintf (stderr,
-		 "Dnmalloc error (hash_rm): could not find a chunkinfo for the chunk %p in the hashtable at entry %lu\n This is definitely a bug, please report it to dnmalloc@fort-knox.org.\n",
-		 p, hashval);
-	abort();
-      }
-#else
-      assert(temp != NULL);
-#endif
-      if (temp) prevtemp->hash_next = temp->hash_next;
-    }
-}
-
-/* mmapped chunks are multiples of pagesize, no hash_nexts, 
- * just remove from the hashtable 
- */
-#define hashtable_remove_mmapped(p) hashtable[hash(p)] = 0;
-
-static void
-hashtable_skiprm (chunkinfoptr ci_orig, chunkinfoptr ci_todelete)
-{
-   unsigned long hashval;
-   chunkinfoptr next;
-   
-#ifdef DNMALLOC_DEBUG
-   fprintf(stderr, "hashtable_skiprm: %p, %lu\n", chunk(ci_todelete), hash(chunk(ci_todelete)));
-#endif
-
-   if (ci_orig->hash_next != ci_todelete) {
-     hashval = hash(chunk(ci_todelete));
-     assert(hashval < AMOUNTHASH); /* rw */
-#ifdef DNMALLOC_CHECKS
-     if (hashtable[hashval] != ci_todelete ) {
-	   fprintf(stderr, "Dnmalloc error: trying to delete wrong value (hash: %lu): ci_todelete: %p (%p), hashtable[hashval]: %p (%p)\n This is definitely a bug, please report it to dnmalloc@fort-knox.org.\n", hashval, ci_todelete, chunk(ci_todelete), hashtable[hashval], chunk(hashtable[hashval]));
-     }
-#else
-     assert(hashtable[hashval] == ci_todelete);
-#endif
-     hashtable[hashval] = ci_todelete->hash_next;
-   }
-
-   else {
-     ci_orig->hash_next = ci_todelete->hash_next;
-     if (!ci_orig->hash_next) {
-       next = next_chunkinfo(ci_orig);
-     } else {
-       next = ci_orig->hash_next;
-     }
-     if (next)
-       next->prev_size = chunksize(ci_orig);
-
-   } 
-}
-
-
-static chunkinfoptr
-hashtable_lookup (mchunkptr p)
-{
-   chunkinfoptr ci;
-   unsigned long hashval;
-   
-   /* if we were called wrongly
-    * if ((char *) p < startheap) return 0;
-    */
-   if ((char *) p >= startheap)
-     {
-       hashval = hash (p);
-       assert(hashval < AMOUNTHASH); /* rw */
-       ci = hashtable[hashval];
-       if (ci && chunk (ci) == p)
-	 return ci;
-
-       if (ci) {
-	 do {
-	   ci = ci->hash_next;
-	 } while (ci && chunk (ci) != p);
-       }
-#ifdef DNMALLOC_CHECKS
-       /* This should never occur but if it does, we'd like to know */
-       if (!ci) {
-	 fprintf (stderr,
-		  "Dnmalloc error: could not find a chunkinfo for the chunk %p in the hashtable at entry %lu\n This is definitely a bug, please report it to dnmalloc@fort-knox.org.\n",
-		  p, hashval);
-	 abort();
-       }
-#else
-       assert(ci != NULL);
-#endif
-       return ci;
-     }
-   return 0;
-}
-
-
-
-/*
-   ----------- Internal state representation and initialization -----------
-*/
-
-struct malloc_state {
-
-  /* The maximum chunk size to be eligible for fastbin */
-  INTERNAL_SIZE_T  max_fast;   /* low 2 bits used as flags */
-
-  /* Fastbins */
-  mfastbinptr      fastbins[NFASTBINS];
-
-  /* Base of the topmost chunk -- not otherwise kept in a bin */
-  chunkinfoptr        top;
-
-  /* The remainder from the most recent split of a small request */
-  chunkinfoptr        last_remainder;
-
-  /* Normal bins */
-  struct chunkinfo bins[NBINS];
-
-  /* Bitmap of bins. Trailing zero map handles cases of largest binned size */
-  unsigned int     binmap[BINMAPSIZE+1];
-
-  /* Tunable parameters */
-  CHUNK_SIZE_T     trim_threshold;
-  INTERNAL_SIZE_T  top_pad;
-  INTERNAL_SIZE_T  mmap_threshold;
-
-  /* Memory map support */
-  int              n_mmaps;
-  int              n_mmaps_max;
-  int              max_n_mmaps;
-
-  /* Cache malloc_getpagesize */
-  unsigned int     pagesize;  
-
-  /* Canary */
-  char             guard_stored[GUARD_SIZE];
-
-  /* Track properties of MORECORE */
-  unsigned int     morecore_properties;
-
-  /* Statistics */
-  INTERNAL_SIZE_T  mmapped_mem;
-  INTERNAL_SIZE_T  sbrked_mem;
-  INTERNAL_SIZE_T  max_sbrked_mem;
-  INTERNAL_SIZE_T  max_mmapped_mem;
-  INTERNAL_SIZE_T  max_total_mem;
-};
-
-typedef struct malloc_state *mstate;
-
-/* 
-   There is exactly one instance of this struct in this malloc.
-   If you are adapting this malloc in a way that does NOT use a static
-   malloc_state, you MUST explicitly zero-fill it before using. This
-   malloc relies on the property that malloc_state is initialized to
-   all zeroes (as is true of C statics).
-*/
-
-static struct malloc_state * av_ = NULL;  /* never directly referenced */
-
-/*
-   All uses of av_ are via get_malloc_state().
-   At most one "call" to get_malloc_state is made per invocation of
-   the public versions of malloc and free, but other routines
-   that in turn invoke malloc and/or free may call more then once. 
-   Also, it is called in check* routines if DEBUG is set.
-*/
-
-#define get_malloc_state() (av_)
-
-/*
-  Initialize a malloc_state struct.
-
-  This is called only from within malloc_consolidate, which needs
-  be called in the same contexts anyway.  It is never called directly
-  outside of malloc_consolidate because some optimizing compilers try
-  to inline it at all call points, which turns out not to be an
-  optimization at all. (Inlining it in malloc_consolidate is fine though.)
-*/
-
-#if __STD_C
-static void malloc_mmap_state(void)
-#else
-static void malloc_mmap_state()
-#endif
-{
-  int mprot;
-  unsigned long pagesize = malloc_getpagesize;
-  size_t size = (sizeof(struct malloc_state) + pagesize - 1) & ~(pagesize - 1);
-
-  void * foo = MMAP(0, size+(2*pagesize), PROT_READ|PROT_WRITE, MAP_PRIVATE);
-
-
-#ifdef NDEBUG
-   if (foo == MAP_FAILED) {
-      fprintf (stderr, "Couldn't mmap struct malloc_state: %s\n", strerror (errno));
-      abort ();
-   }
-#else
-   assert(foo != MAP_FAILED);
-#endif
-
-   mprot = mprotect(foo, pagesize, PROT_NONE);
-#ifdef NDEBUG
-   if (mprot == -1) {
-     fprintf (stderr, "Couldn't mprotect first non-rw page for struct malloc_state: %s\n",
-	      strerror (errno));
-     abort ();
-   }
-#else
-   assert(mprot != -1);
-#endif
-
-   av_ = (struct malloc_state *) ((char*)foo + pagesize);
-
-   MALLOC_ZERO(av_, sizeof(struct malloc_state));
-
-   mprot = mprotect((void*)((char*)foo + size + pagesize), (size_t) pagesize, PROT_NONE);
-#ifdef NDEBUG
-   if (mprot == -1) {
-     fprintf (stderr, 
-	      "Couldn't mprotect last non-rw page for struct malloc_state: %s\n",
-	      strerror (errno));
-     abort ();
-   }
-#else
-   assert(mprot != -1);
-#endif
-}
-
-#if __STD_C
-static void malloc_init_state(mstate av)
-#else
-static void malloc_init_state(av) mstate av;
-#endif
-{
-  int     i;
-  mbinptr bin;
-
-  void *  morecore_test = MORECORE(0);
-  unsigned long hashval;
-
-  /* Test morecore function 
-   */
-  set_morecore32bit(av);
-
-  if (morecore_test == MORECORE_FAILURE)
-    {
-      set_nonmorecore32bit(av);
-    }
-  else
-    {
-      /* On 64bit systems, the heap may be located above the
-       * 32bit address space. Since mmap() probably still can be
-       * convinced to map within 32bit, we don't use sbrk().
-       */
-      hashval = hash (morecore_test);
-      if (hashval >= AMOUNTHASH) 
-	{
-	  set_nonmorecore32bit(av);
-	}
-    }
-
-  
-  /* Establish circular links for normal bins */
-  for (i = 1; i < NBINS; ++i) { 
-    bin = bin_at(av,i);
-    bin->fd = bin->bk = bin;
-  }
-
-  av->top_pad        = DEFAULT_TOP_PAD;
-  av->n_mmaps_max    = DEFAULT_MMAP_MAX;
-  av->mmap_threshold = DEFAULT_MMAP_THRESHOLD;
-  av->trim_threshold = DEFAULT_TRIM_THRESHOLD;
-
-#if MORECORE_CONTIGUOUS
-  set_contiguous(av);
-#else
-  set_noncontiguous(av);
-#endif
-
-  set_max_fast(av, DEFAULT_MXFAST);
-
-  av->top = cireg_getfree ();
-  av->top->chunk     = (mchunkptr) startheap;
-  av->top->size      = 0;
-  set_previnuse(av->top);
-  clear_inuse(av->top);
-  hashtable[0]       = av->top;
-  av->pagesize       = malloc_getpagesize;
-
-  memcpy(av->guard_stored, dnmalloc_arc4random(), GUARD_SIZE);
-
-}
-
-/* 
-   Other internal utilities operating on mstates
-*/
-
-#if __STD_C
-static Void_t*  sYSMALLOc(INTERNAL_SIZE_T, mstate);
-static int      sYSTRIm(size_t, mstate);
-static void     malloc_consolidate(mstate);
-#else
-static Void_t*  sYSMALLOc();
-static int      sYSTRIm();
-static void     malloc_consolidate();
-#endif
-
-/* dnmalloc functions */
-/* needs mstate so moved here */
-
-static chunkinfoptr
-next_chunkinfo (chunkinfoptr ci)
-{
-   mchunkptr nextp;
-   unsigned long hashval;
-   chunkinfoptr cinfonextp;
-   mstate av = get_malloc_state();
-   
-   /* ci is not the last element in the linked list, just 
-      return the next chunkinfo from the list 
-   */
-   if (!ci->hash_next)
-     {
-       /* ci is the last element, find the next chunkinfo by 
-	* looking up the chunkinfo for the chunk that is after p's chunk 
-	*/
-       nextp = (mchunkptr) (((char *) (ci->chunk)) + chunksize (ci));
-
-       if (!(nextp == av->top->chunk)) 
-	 {
-	   hashval = hash (nextp);
-	   /* assert(hashval < AMOUNTHASH); *//* major bottleneck */
-	   cinfonextp = hashtable[hashval];
-	   if (cinfonextp && chunk (cinfonextp) == nextp)
-	     return cinfonextp; 
-	   
-#ifdef DNMALLOC_CHECKS_EXTRA
-	   /* This seems bogus; a chunkinfo may legally have no nextp if
-	    * it's the last one allocated (?)
-	    */
-	   else {
-	     if (cinfonextp)
-	       fprintf (stderr,
-			"Dnmalloc error: could not find a next chunkinfo for the chunk %p in the hashtable at entry %lu, cinfonextp: %p, chunk(cinfonextp): %p, nextp: %p\n This is definitely a bug, please report it to dnmalloc@fort-knox.org.\n",
-			chunk(ci), hashval, cinfonextp, chunk(cinfonextp), nextp);
-	     else
-	       fprintf (stderr,
-			"Dnmalloc error: could not find a next chunkinfo for the chunk %p in the hashtable at entry %lu, cinfonextp: %s, chunk(cinfonextp): %s, nextp: %p\n This is definitely a bug, please report it to dnmalloc@fort-knox.org.\n",
-			chunk(ci), hashval, "null", "null", nextp);
-	   }
-#endif
-	  
-	   return NULL;
-	 }
-       else
-	 {
-	   return av->top;
-	 }
-
-     }
-   else
-     {
-       return (ci->hash_next);
-     }
-}
-
-static int is_next_chunk(chunkinfoptr oldp, chunkinfoptr newp) {
-	mchunkptr nextp;
-	if (oldp->hash_next == newp)
-		return 1;
-	nextp = (mchunkptr) (((char *) (oldp->chunk)) + chunksize (oldp));
-	if (nextp == chunk(newp))
-		return 1;
-	return 0;
-}
-
-
-
-/* Get the chunkinfo of the physically previous chunk */
-/* Since we disposed of prev_size, we need this function to find the previous */
-
-static chunkinfoptr
-prev_chunkinfo (chunkinfoptr ci)
-{
-   unsigned int i;
-   chunkinfoptr prev;
-   mchunkptr prevchunk = 0;
-   /* chunkinfoptr temp; */
-   
-   /* Get the hashtable location of the chunkinfo */
-   i = hash (chunk (ci));
-   assert(i < AMOUNTHASH); /* rw */
-      
-   /* Get the first element of the linked list of chunkinfo's that contains p */
-   prev = hashtable[i];
-   
-   if (ci == prev) {
-     prevchunk = (mchunkptr) (((char *) (ci->chunk)) - (ci->prev_size));
-     i = hash(prevchunk);
-     assert(i < AMOUNTHASH); /* rw */
-     /* Loop over the linked list until we reach the last element */
-     for (prev = hashtable[i]; prev->hash_next != 0; prev = prev->hash_next) ;
-   } else {
-     /* p is not the first element in the linked list, we can just 
-	loop over the list and return the previous 
-     */
-     for (prev = hashtable[i]; prev->hash_next != ci; prev = prev->hash_next);
-   }
-
-   return prev;  
-}
-
-
-/*
-  Debugging support
-  Dnmalloc broke dlmallocs debugging functions, should fix them some 
-  time in the future, for now leave them undefined.
-*/
-
-#define check_chunk(P)
-#define check_free_chunk(P)
-#define check_inuse_chunk(P)
-#define check_remalloced_chunk(P,N)
-#define check_malloced_chunk(P,N)
-#define check_malloc_state()
-
-
-/* ----------- Routines dealing with system allocation -------------- */
-
-/*
-  sysmalloc handles malloc cases requiring more memory from the system.
-  On entry, it is assumed that av->top does not have enough
-  space to service request for nb bytes, thus requiring that av->top
-  be extended or replaced.
-*/
-
-#if __STD_C
-static Void_t* sYSMALLOc(INTERNAL_SIZE_T nb, mstate av)
-#else
-static Void_t* sYSMALLOc(nb, av) INTERNAL_SIZE_T nb; mstate av;
-#endif
-{
-  chunkinfoptr    old_top;        /* incoming value of av->top */
-  INTERNAL_SIZE_T old_size;       /* its size */
-  char*           old_end;        /* its end address */
-
-  long            size;           /* arg to first MORECORE or mmap call */
-  char*           brk;            /* return value from MORECORE */
-
-  long            correction;     /* arg to 2nd MORECORE call */
-  char*           snd_brk;        /* 2nd return val */
-
-  INTERNAL_SIZE_T front_misalign; /* unusable bytes at front of new space */
-  INTERNAL_SIZE_T end_misalign;   /* partial page left at end of new space */
-  char*           aligned_brk;    /* aligned offset into brk */
-
-  chunkinfoptr    p;              /* the allocated/returned chunk */
-  chunkinfoptr    remainder;      /* remainder from allocation */
-  chunkinfoptr    fencepost;      /* fencepost */
-  CHUNK_SIZE_T    remainder_size; /* its size */
-
-  CHUNK_SIZE_T    sum;            /* for updating stats */
-
-  size_t          pagemask  = av->pagesize - 1;
-
-#ifdef DNMALLOC_DEBUG
-  fprintf(stderr, "Enter sysmalloc\n");
-#endif
-  /*
-    If there is space available in fastbins, consolidate and retry
-    malloc from scratch rather than getting memory from system.  This
-    can occur only if nb is in smallbin range so we didn't consolidate
-    upon entry to malloc. It is much easier to handle this case here
-    than in malloc proper.
-  */
-
-
-  if (have_fastchunks(av)) {
-    Void_t * retval;
-    assert(in_smallbin_range(nb));
-    malloc_consolidate(av);
-#ifdef DNMALLOC_DEBUG
-    fprintf(stderr, "Return sysmalloc have_fastchunks\n");
-#endif
-    retval = mALLOc(nb - MALLOC_ALIGN_MASK);
-    VALGRIND_FREELIKE_BLOCK(retval, 0);
-    return retval;
-  }
-
-
-  /*
-    If have mmap, and the request size meets the mmap threshold, and
-    the system supports mmap, and there are few enough currently
-    allocated mmapped regions, try to directly map this request
-    rather than expanding top.
-  */
-
-  if (UNLIKELY((CHUNK_SIZE_T)(nb) >= (CHUNK_SIZE_T)(av->mmap_threshold) &&
-	       (av->n_mmaps < av->n_mmaps_max))) {
-
-    char* mm;             /* return value from mmap call*/
-
-    /*
-      Round up size to nearest page.  For mmapped chunks, the overhead
-      is one SIZE_SZ unit larger than for normal chunks, because there
-      is no following chunk whose prev_size field could be used.
-    */
-    size = (nb + MALLOC_ALIGN_MASK + pagemask) & ~pagemask;
-
-    /* Don't try if size wraps around 0 */
-    if ((CHUNK_SIZE_T)(size) > (CHUNK_SIZE_T)(nb)) {
-	    
-
-      mm = (char*)(MMAP(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE));
-      
-      if (mm != (char*)(MORECORE_FAILURE)) {
-        
-	VALGRIND_MAKE_MEM_NOACCESS(mm,size);
-
-        /*
-          The offset to the start of the mmapped region is stored
-          in the prev_size field of the chunk. This allows us to adjust
-          returned start address to meet alignment requirements here 
-          and in memalign(), and still be able to compute proper
-          address argument for later munmap in free() and realloc().
-        */
-        
-        front_misalign = (INTERNAL_SIZE_T) mm & MALLOC_ALIGN_MASK;
-	p = cireg_getfree();
-        
-        if (front_misalign > 0) {
-          correction = MALLOC_ALIGNMENT - front_misalign;
-          p->chunk = (mchunkptr)(mm + correction);
-          p->hash_next = (chunkinfoptr) correction;
-          set_head(p, (size - correction) |INUSE|IS_MMAPPED);
-        }
-        else {
-          p->chunk = (mchunkptr)mm;
-          p->hash_next = 0;
-          set_head(p, size|INUSE|IS_MMAPPED);
-        }
-        hashtable_add(p);
-        /* update statistics */
-        
-        if (++av->n_mmaps > av->max_n_mmaps) 
-          av->max_n_mmaps = av->n_mmaps;
-        
-        sum = av->mmapped_mem += size;
-        if (sum > (CHUNK_SIZE_T)(av->max_mmapped_mem)) 
-          av->max_mmapped_mem = sum;
-        sum += av->sbrked_mem;
-        if (sum > (CHUNK_SIZE_T)(av->max_total_mem)) 
-          av->max_total_mem = sum;
-
-        check_chunk(p);
-        
-#ifdef DNMALLOC_DEBUG
-	fprintf(stderr, "Return mmapped (%lu, total %lu)\n", 
-		size, (unsigned long)/* size_t */av->max_total_mem );
-#endif
-        return chunk(p);
-      }
-    }
-  }
-
-  /* Record incoming configuration of top */
-
-  old_top  = av->top;
-  old_size = chunksize(old_top);
-  old_end  = (char*)(chunk_at_offset(chunk(old_top), old_size));
-
-  brk = snd_brk = (char*)(MORECORE_FAILURE); 
-
-  /* 
-     If not the first time through, we require old_size to be
-     at least MINSIZE and to have prev_inuse set.
-  */
-
-  /* assert((old_top == initial_top(av) && old_size == 0) || 
-	 ((CHUNK_SIZE_T) (old_size) >= MINSIZE &&
-	 prev_inuse(old_top))); */
-
-  /* Precondition: not enough current space to satisfy nb request */
-  assert((CHUNK_SIZE_T)(old_size) < (CHUNK_SIZE_T)(nb + MINSIZE));
-
-  /* Precondition: all fastbins are consolidated */
-  assert(!have_fastchunks(av));
-
-  /* Request enough space for nb + pad + overhead */
-  size = nb + av->top_pad + MINSIZE;
-
-  /*
-    If contiguous, we can subtract out existing space that we hope to
-    combine with new space. We add it back later only if
-    we don't actually get contiguous space.
-  */
-  if (contiguous(av))
-    size -= old_size;
-
-  /*
-    Round to a multiple of page size.
-    If MORECORE is not contiguous, this ensures that we only call it
-    with whole-page arguments.  And if MORECORE is contiguous and
-    this is not first time through, this preserves page-alignment of
-    previous calls. Otherwise, we correct to page-align below.
-  */
-
-  size = (size + pagemask) & ~pagemask;
-
-  /*
-    Don't try to call MORECORE if argument is so big as to appear
-    negative. Note that since mmap takes size_t arg, it may succeed
-    below even if we cannot call MORECORE.
-  */
-  if (size > 0 && morecore32bit(av)) 
-    brk = (char*)(MORECORE(size));
-
-  /*
-    If have mmap, try using it as a backup when MORECORE fails or
-    cannot be used. This is worth doing on systems that have "holes" in
-    address space, so sbrk cannot extend to give contiguous space, but
-    space is available elsewhere.  Note that we ignore mmap max count
-    and threshold limits, since the space will not be used as a
-    segregated mmap region.
-  */
-  if (brk != (char*)(MORECORE_FAILURE)) {
-    av->sbrked_mem += size;
-    VALGRIND_MAKE_MEM_NOACCESS(brk,size);
-  }
-
-  else {
-
-#ifdef DNMALLOC_DEBUG
-    fprintf(stderr, "Morecore failure in sysmalloc\n");
-#endif
-
-    /* Cannot merge with old top, so add its size back in */
-    if (contiguous(av))
-      size = (size + old_size + pagemask) & ~pagemask;
-
-    /* If we are relying on mmap as backup, then use larger units */
-    if ((CHUNK_SIZE_T)(size) < (CHUNK_SIZE_T)(MMAP_AS_MORECORE_SIZE))
-      size = MMAP_AS_MORECORE_SIZE;
-
-    /* Don't try if size wraps around 0 */
-    if ((CHUNK_SIZE_T)(size) > (CHUNK_SIZE_T)(nb)) {
-
-#ifdef DNMALLOC_DEBUG
-      fprintf(stderr, "Try mmap in sysmalloc\n");
-#endif
-      brk = (char*)(MMAP(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE));
-      
-      if (brk != (char*)(MORECORE_FAILURE)) {
-        
-	VALGRIND_MAKE_MEM_NOACCESS(brk,size);
-
-	av->mmapped_mem += size;
-#ifdef DNMALLOC_DEBUG
-	fprintf(stderr, "Mmapped successfully in sysmalloc %p\n", brk);
-#endif
-
-        /* We do not need, and cannot use, another sbrk call to find end */
-        snd_brk = brk + size;
-        
-        /* 
-           Record that we no longer have a contiguous sbrk region. 
-           After the first time mmap is used as backup, we do not
-           ever rely on contiguous space since this could incorrectly
-           bridge regions.
-        */
-        set_noncontiguous(av);
-      }
-    }
-  }
-
-  if (brk != (char*)(MORECORE_FAILURE)) {
-#ifdef DNMALLOC_DEBUG
-    fprintf(stderr, "Success path %lu allocated, sbrked %lu\n", 
-	    size, (unsigned long)av->sbrked_mem);
-#endif
-    /* av->sbrked_mem += size; moved up */
-
-    /*
-      If MORECORE extends previous space, we can likewise extend top size.
-    */
-    
-    if (brk == old_end && snd_brk == (char*)(MORECORE_FAILURE)) {
-      set_head(old_top, (size + old_size) | PREV_INUSE);
-#ifdef DNMALLOC_DEBUG
-      fprintf(stderr, "Previous space extended\n");
-#endif
-    }
-
-    /*
-      Otherwise, make adjustments:
-      
-      * If the first time through or noncontiguous, we need to call sbrk
-        just to find out where the end of memory lies.
-
-      * We need to ensure that all returned chunks from malloc will meet
-        MALLOC_ALIGNMENT
-
-      * If there was an intervening foreign sbrk, we need to adjust sbrk
-        request size to account for fact that we will not be able to
-        combine new space with existing space in old_top.
-
-      * Almost all systems internally allocate whole pages at a time, in
-        which case we might as well use the whole last page of request.
-        So we allocate enough more memory to hit a page boundary now,
-        which in turn causes future contiguous calls to page-align.
-    */
-    
-    else {
-      /* front_misalign = 0; *//*superfluous */
-      /* end_misalign = 0; *//*superfluous */
-      correction = 0;
-      aligned_brk = brk;
-
-      /*
-        If MORECORE returns an address lower than we have seen before,
-        we know it isn't really contiguous.  This and some subsequent
-        checks help cope with non-conforming MORECORE functions and
-        the presence of "foreign" calls to MORECORE from outside of
-        malloc or by other threads.  We cannot guarantee to detect
-        these in all cases, but cope with the ones we do detect.
-      */
-      if (contiguous(av) && old_size != 0 && brk < old_end) {
-        set_noncontiguous(av);
-      }
-      
-      /* handle contiguous cases */
-      if (contiguous(av)) { 
-
-#ifdef DNMALLOC_DEBUG
-	fprintf(stderr, "Handle contiguous cases\n");
-#endif
-        /* 
-           We can tolerate forward non-contiguities here (usually due
-           to foreign calls) but treat them as part of our space for
-           stats reporting.
-        */
-        if (old_size != 0) 
-          av->sbrked_mem += brk - old_end;
-        
-        /* Guarantee alignment of first new chunk made from this space */
-
-        front_misalign = (INTERNAL_SIZE_T) brk & MALLOC_ALIGN_MASK;
-        if (front_misalign > 0) {
-
-          /*
-            Skip over some bytes to arrive at an aligned position.
-            We don't need to specially mark these wasted front bytes.
-            They will never be accessed anyway because
-            prev_inuse of av->top (and any chunk created from its start)
-            is always true after initialization.
-          */
-
-          correction = MALLOC_ALIGNMENT - front_misalign;
-          aligned_brk += correction;
-        }
-        
-        /*
-          If this isn't adjacent to existing space, then we will not
-          be able to merge with old_top space, so must add to 2nd request.
-        */
-        
-        correction += old_size;
-        
-        /* Extend the end address to hit a page boundary */
-        end_misalign = (INTERNAL_SIZE_T)(brk + size + correction);
-        correction += ((end_misalign + pagemask) & ~pagemask) - end_misalign;
-        
-        assert(correction >= 0);
-        snd_brk = (char*)(MORECORE(correction));
-        
-        if (snd_brk == (char*)(MORECORE_FAILURE)) {
-          /*
-            If can't allocate correction, try to at least find out current
-            brk.  It might be enough to proceed without failing.
-          */
-          correction = 0;
-          snd_brk = (char*)(MORECORE(0));
-        }
-        else if (snd_brk < brk) {
-          /*
-            If the second call gives noncontiguous space even though
-            it says it won't, the only course of action is to ignore
-            results of second call, and conservatively estimate where
-            the first call left us. Also set noncontiguous, so this
-            won't happen again, leaving at most one hole.
-            
-            Note that this check is intrinsically incomplete.  Because
-            MORECORE is allowed to give more space than we ask for,
-            there is no reliable way to detect a noncontiguity
-            producing a forward gap for the second call.
-          */
-          snd_brk = brk + size;
-          correction = 0;
-          set_noncontiguous(av);
-        }
-	else {
-	  VALGRIND_MAKE_MEM_NOACCESS(snd_brk,correction);
-	}
-
-      }
-      
-      /* handle non-contiguous cases */
-      else { 
-
-#ifdef DNMALLOC_DEBUG
-	fprintf(stderr, "Handle non-contiguous cases\n");
-#endif
-
-        /* MORECORE/mmap must correctly align */
-        assert(aligned_OK(brk));
-        
-        /* Find out current end of memory */
-        if (snd_brk == (char*)(MORECORE_FAILURE)) {
-          snd_brk = (char*)(MORECORE(0));
-          av->sbrked_mem += snd_brk - brk - size;
-        }
-#ifdef DNMALLOC_DEBUG
-	fprintf(stderr, "Sbrked now %lu\n", (unsigned long)av->sbrked_mem);
-#endif
-      }
-      
-      /* Adjust top based on results of second sbrk.
-       *
-       * If mmap() has been used as backup for failed morecore(),
-       * we end up in this branch as well.
-       */
-      if (snd_brk != (char*)(MORECORE_FAILURE)) {
-#ifdef DNMALLOC_DEBUG
-	fprintf(stderr, "Adjust top, correction %lu\n", correction);
-#endif
-        /* hashtable_remove(chunk(av->top)); *//* rw 19.05.2008 removed */
-	av->top =  cireg_getfree();
-        av->top->chunk = (mchunkptr)aligned_brk;
-        set_head(av->top, (snd_brk - aligned_brk + correction) | PREV_INUSE);
-#ifdef DNMALLOC_DEBUG
-	fprintf(stderr, "Adjust top, top %p size %lu\n", 
-		av->top, (unsigned long)chunksize(av->top));
-#endif
-        hashtable_add(av->top);
-        av->sbrked_mem += correction;
-     
-        /*
-          If not the first time through, we either have a
-          gap due to foreign sbrk or a non-contiguous region.  Insert a
-          double fencepost at old_top to prevent consolidation with space
-          we don't own. These fenceposts are artificial chunks that are
-          marked as inuse. Original dlmalloc had two of these but too 
-          small to use. To ensure that the linked lists contain a maximum 
-          of 8 elements we only use 1. Inuse is determined by the 
-          current rather than the next chunk anyway.
-        */
-   
-        if (old_size != 0) {
-#ifdef DNMALLOC_DEBUG
-	  fprintf(stderr, "Shrink old_top to insert fenceposts\n");
-#endif
-          /* 
-             Shrink old_top to insert fenceposts, keeping size a
-             multiple of MALLOC_ALIGNMENT. We know there is at least
-             enough space in old_top to do this.
-          */
-#ifdef DNMALLOC_DEBUG
-	  fprintf(stderr, "Adjust top, old_top %p old_size before %lu\n", 
-		  old_top, (unsigned long)old_size);
-#endif
-          old_size = (old_size - 4*SIZE_SZ) & ~MALLOC_ALIGN_MASK;
-          set_head(old_top, old_size | PREV_INUSE);
-#ifdef DNMALLOC_DEBUG
-	  fprintf(stderr, "Adjust top, old_size after %lu\n", 
-		  (unsigned long)old_size);
-#endif
-          
-          /*
-            Note that the following assignments completely overwrite
-            old_top when old_size was previously MINSIZE.  This is
-            intentional. We need the fencepost, even if old_top otherwise gets
-            lost.
-          */
-          /* dnmalloc, we need the fencepost to be 16 bytes, however since 
-	     it's marked inuse it will never be coalesced 
-	  */
-          fencepost = cireg_getfree();
-          fencepost->chunk = (mchunkptr) chunk_at_offset(chunk(old_top), 
-							 old_size);
-          fencepost->size = 16|INUSE|PREV_INUSE;
-          hashtable_add(fencepost);
-          /* 
-             If possible, release the rest, suppressing trimming.
-          */
-          if (old_size >= MINSIZE) {
-            INTERNAL_SIZE_T tt = av->trim_threshold;
-#ifdef DNMALLOC_DEBUG
-	    fprintf(stderr, "Release\n");
-#endif
-            av->trim_threshold = (INTERNAL_SIZE_T)(-1);
-	    set_head(old_top, old_size | PREV_INUSE | INUSE);
-	    guard_set(av->guard_stored, old_top, 0, old_size);
-	    VALGRIND_MALLOCLIKE_BLOCK(chunk(old_top), old_size, 0, 0);
-            fREe(chunk(old_top));
-            av->trim_threshold = tt;
-#ifdef DNMALLOC_DEBUG
-	    fprintf(stderr, "Release done\n");
-#endif
-          }
-
-#ifdef DNMALLOC_DEBUG
-	  fprintf(stderr, "Adjust top, size %lu\n", 
-		  (unsigned long)chunksize(av->top));
-#endif
-
-        } /* fenceposts */
-      } /* adjust top */
-    } /* not extended previous region */
-    
-    /* Update statistics */
-    sum = av->sbrked_mem;
-    if (sum > (CHUNK_SIZE_T)(av->max_sbrked_mem))
-      av->max_sbrked_mem = sum;
-    
-    sum += av->mmapped_mem;
-    if (sum > (CHUNK_SIZE_T)(av->max_total_mem))
-      av->max_total_mem = sum;
-
-    check_malloc_state();
-    
-    /* finally, do the allocation */
-
-    p = av->top;
-    size = chunksize(p);
-    
-#ifdef DNMALLOC_DEBUG
-    fprintf(stderr, "Size: %lu  nb+MINSIZE: %lu\n", 
-	    (CHUNK_SIZE_T)(size), (CHUNK_SIZE_T)(nb + MINSIZE));
-#endif
-
-    /* check that one of the above allocation paths succeeded */
-    if ((CHUNK_SIZE_T)(size) >= (CHUNK_SIZE_T)(nb + MINSIZE)) {
-      remainder_size = size - nb;
-      remainder = cireg_getfree();
-      remainder->chunk = chunk_at_offset(chunk(p), nb);
-      av->top = remainder;
-      set_head(p, nb | PREV_INUSE | INUSE);
-      set_head(remainder, remainder_size | PREV_INUSE);
-      hashtable_insert (p, av->top);
-      check_malloced_chunk(p, nb);
-#ifdef DNMALLOC_DEBUG
-      fprintf(stderr, "Return any (total %lu)\n", 
-	      (unsigned long)/* size_t */av->max_total_mem );
-#endif
-      return chunk(p);
-    }
-
-  }
-
-#ifdef DNMALLOC_DEBUG
-  fprintf(stderr, "Return failed (total %lu)\n", 
-	  (unsigned long)/* size_t */av->max_total_mem );
-#endif
-
-  /* catch all failure paths */
-  MALLOC_FAILURE_ACTION;
-  return 0;
-}
-
-
-
-
-/*
-  sYSTRIm is an inverse of sorts to sYSMALLOc.  It gives memory back
-  to the system (via negative arguments to sbrk) if there is unused
-  memory at the `high' end of the malloc pool. It is called
-  automatically by free() when top space exceeds the trim
-  threshold. It is also called by the public malloc_trim routine.  It
-  returns 1 if it actually released any memory, else 0.
-*/
-
-#if __STD_C
-static int sYSTRIm(size_t pad, mstate av)
-#else
-static int sYSTRIm(pad, av) size_t pad; mstate av;
-#endif
-{
-  long  top_size;        /* Amount of top-most memory */
-  long  extra;           /* Amount to release */
-  long  released;        /* Amount actually released */
-  char* current_brk;     /* address returned by pre-check sbrk call */
-  char* new_brk;         /* address returned by post-check sbrk call */
-  size_t pagesz;
-
-  pagesz = av->pagesize;
-  top_size = chunksize(av->top);
-  
-  /* Release in pagesize units, keeping at least one page */
-  extra = ((top_size - pad - MINSIZE + (pagesz-1)) / pagesz - 1) * pagesz;
-  
-  if (extra > 0) {
-    
-    /*
-      Only proceed if end of memory is where we last set it.
-      This avoids problems if there were foreign sbrk calls.
-    */
-    current_brk = (char*)(MORECORE(0));
-    if (current_brk == (char*)(av->top) + top_size) {
-      
-      /*
-        Attempt to release memory. We ignore MORECORE return value,
-        and instead call again to find out where new end of memory is.
-        This avoids problems if first call releases less than we asked,
-        of if failure somehow altered brk value. (We could still
-        encounter problems if it altered brk in some very bad way,
-        but the only thing we can do is adjust anyway, which will cause
-        some downstream failure.)
-      */
-      
-      MORECORE(-extra);
-      new_brk = (char*)(MORECORE(0));
-      
-      if (new_brk != (char*)MORECORE_FAILURE) {
-        released = (long)(current_brk - new_brk);
-        
-        if (released != 0) {
-          /* Success. Adjust top. */
-          av->sbrked_mem -= released;
-          set_head(av->top, (top_size - released) | PREV_INUSE);
-          check_malloc_state();
-          return 1;
-        }
-      }
-    }
-  }
-  return 0;
-}
-
-/*
-  ------------------------------ malloc ------------------------------
-*/
-
-
-#if __STD_C
-DL_STATIC Void_t* mALLOc(size_t bytes)
-#else
-DL_STATIC   Void_t* mALLOc(bytes) size_t bytes;
-#endif
-{
-  mstate av = get_malloc_state();
-
-  INTERNAL_SIZE_T nb;               /* normalized request size */
-  unsigned int    idx;              /* associated bin index */
-  mbinptr         bin;              /* associated bin */
-  mfastbinptr*    fb;               /* associated fastbin */
-
-  chunkinfoptr       victim;           /* inspected/selected chunk */
-  INTERNAL_SIZE_T size;             /* its size */
-  int             victim_index;     /* its bin index */
-
-  chunkinfoptr       remainder;        /* remainder from a split */
-  CHUNK_SIZE_T    remainder_size;   /* its size */
-
-  unsigned int    block;            /* bit map traverser */
-  unsigned int    bit;              /* bit map traverser */
-  unsigned int    map;              /* current word of binmap */
-
-  chunkinfoptr       fwd;              /* misc temp for linking */
-  chunkinfoptr       bck;              /* misc temp for linking */
-  
-  Void_t*         retval;
-
-  /* chunkinfoptr	  next; */
- 
-
-  /*
-    Convert request size to internal form by adding SIZE_SZ bytes
-    overhead plus possibly more to obtain necessary alignment and/or
-    to obtain a size of at least MINSIZE, the smallest allocatable
-    size. Also, checked_request2size traps (returning 0) request sizes
-    that are so large that they wrap around zero when padded and
-    aligned.
-  */
-#if defined(SH_CUTEST)
-  extern int malloc_count;
-  ++malloc_count;
-#endif
-
-  checked_request2size(bytes, nb);
-
-  /*
-    Bypass search if no frees yet
-   */
-  if (av && have_anychunks(av)) {
-    goto av_initialized;
-  }
-  else {
-    if (!av || av->max_fast == 0) { /* initialization check */
-      malloc_consolidate(av);
-      av = get_malloc_state();
-    }
-    goto use_top;
-  }
-
- av_initialized:
-
-  /*
-    If the size qualifies as a fastbin, first check corresponding bin.
-  */
-  if ((CHUNK_SIZE_T)(nb) <= (CHUNK_SIZE_T)(av->max_fast)) {
-    fb = &(av->fastbins[(fastbin_index(nb))]);
-    if ( (victim = *fb) != 0) {
-      *fb = victim->fd;
-      check_remalloced_chunk(victim, nb);
-      guard_set(av->guard_stored, victim, bytes, nb);
-      VALGRIND_MALLOCLIKE_BLOCK(chunk(victim), bytes, 0, 0);
-      return chunk(victim);
-    }
-  }
-
-  /*
-    If a small request, check regular bin.  Since these "smallbins"
-    hold one size each, no searching within bins is necessary.
-    (For a large request, we need to wait until unsorted chunks are
-    processed to find best fit. But for small ones, fits are exact
-    anyway, so we can check now, which is faster.)
-  */
-
-  if (in_smallbin_range(nb)) {
-    idx = smallbin_index(nb);
-    bin = bin_at(av,idx);
-
-    if ((victim = last(bin)) != bin) {
-      bck = victim->bk;
-      bin->bk = bck;
-      bck->fd = bin;
-
-      set_all_inuse(victim);
-            
-      check_malloced_chunk(victim, nb);
-      guard_set(av->guard_stored, victim, bytes, nb);
-      VALGRIND_MALLOCLIKE_BLOCK(chunk(victim), bytes, 0, 0);
-      return chunk(victim);
-    }
-  }
-
-  /* 
-     If this is a large request, consolidate fastbins before continuing.
-     While it might look excessive to kill all fastbins before
-     even seeing if there is space available, this avoids
-     fragmentation problems normally associated with fastbins.
-     Also, in practice, programs tend to have runs of either small or
-     large requests, but less often mixtures, so consolidation is not 
-     invoked all that often in most programs. And the programs that
-     it is called frequently in otherwise tend to fragment.
-  */
-
-  else {
-    idx = largebin_index(nb);
-    if (have_fastchunks(av)) 
-      malloc_consolidate(av);
-  }
-
-  /*
-    Process recently freed or remaindered chunks, taking one only if
-    it is exact fit, or, if this a small request, the chunk is remainder from
-    the most recent non-exact fit.  Place other traversed chunks in
-    bins.  Note that this step is the only place in any routine where
-    chunks are placed in bins.
-  */
-    
-  while ( (victim = unsorted_chunks(av)->bk) != unsorted_chunks(av)) {
-    bck = victim->bk;
-    size = chunksize(victim);
-    
-    /* 
-       If a small request, try to use last remainder if it is the
-       only chunk in unsorted bin.  This helps promote locality for
-       runs of consecutive small requests. This is the only
-       exception to best-fit, and applies only when there is
-       no exact fit for a small chunk.
-    */
-    
-    if (UNLIKELY(in_smallbin_range(nb) && 
-		 bck == unsorted_chunks(av) &&
-		 victim == av->last_remainder &&
-		 (CHUNK_SIZE_T)(size) > (CHUNK_SIZE_T)(nb + MINSIZE))) {
-      
-      /* split and reattach remainder */
-      remainder_size = size - nb;
-      remainder = cireg_getfree();
-      remainder->chunk = chunk_at_offset(chunk(victim), nb);
-      unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
-      av->last_remainder = remainder; 
-      remainder->bk = remainder->fd = unsorted_chunks(av);
-      
-      set_head(victim, nb | PREV_INUSE|INUSE);
-      set_head(remainder, remainder_size | PREV_INUSE);      
-      hashtable_insert(victim, remainder);
-
-      check_malloced_chunk(victim, nb);
-      guard_set(av->guard_stored, victim, bytes, nb);
-      VALGRIND_MALLOCLIKE_BLOCK(chunk(victim), bytes, 0, 0);
-      return chunk(victim);
-    }
-    
-    /* remove from unsorted list */
-    unsorted_chunks(av)->bk = bck;
-    bck->fd = unsorted_chunks(av);
-    
-    /* Take now instead of binning if exact fit */
-    
-    if (UNLIKELY(size == nb)) {
-      set_all_inuse(victim)
-      check_malloced_chunk(victim, nb);
-      guard_set(av->guard_stored, victim, bytes, nb);
-      VALGRIND_MALLOCLIKE_BLOCK(chunk(victim), bytes, 0, 0);
-      return chunk(victim);
-    }
-    
-    /* place chunk in bin */
-    
-    if (in_smallbin_range(size)) {
-
-      victim_index = smallbin_index(size);
-      bck = bin_at(av, victim_index);
-      fwd = bck->fd;
-    }
-    else {
-      victim_index = largebin_index(size);
-      bck = bin_at(av, victim_index);
-      fwd = bck->fd;
-      
-      if (UNLIKELY(fwd != bck)) {
-        /* if smaller than smallest, place first */
-        if ((CHUNK_SIZE_T)(size) < (CHUNK_SIZE_T)(bck->bk->size)) {
-          fwd = bck;
-          bck = bck->bk;
-        }
-        else if ((CHUNK_SIZE_T)(size) >= 
-                 (CHUNK_SIZE_T)(FIRST_SORTED_BIN_SIZE)) {
-          
-          /* maintain large bins in sorted order */
-          size |= PREV_INUSE|INUSE; /* Or with inuse bits to speed comparisons */
-          while ((CHUNK_SIZE_T)(size) < (CHUNK_SIZE_T)(fwd->size)) 
-            fwd = fwd->fd;
-          bck = fwd->bk;
-        }
-      }
-    }
-
-    mark_bin(av, victim_index);
-    victim->bk = bck;
-    victim->fd = fwd;
-    fwd->bk = victim;
-    bck->fd = victim;
-  }
-  
-  /*
-    If a large request, scan through the chunks of current bin to
-    find one that fits.  (This will be the smallest that fits unless
-    FIRST_SORTED_BIN_SIZE has been changed from default.)  This is
-    the only step where an unbounded number of chunks might be
-    scanned without doing anything useful with them. However the
-    lists tend to be short.
-  */
-
-  if (!in_smallbin_range(nb)) {
-    bin = bin_at(av, idx);
-    
-    victim = last(bin);
-
-    if (UNLIKELY(victim != bin)) {
-
-      do {
-	size = chunksize(victim);
-      
-	if ((CHUNK_SIZE_T)(size) >= (CHUNK_SIZE_T)(nb)) {
-	  remainder_size = size - nb;
-	  unlink(victim, bck, fwd);
-        
-	  /* Split */
-	  if (remainder_size >= MINSIZE) {
-	    remainder = cireg_getfree();
-	    remainder->chunk = chunk_at_offset(chunk(victim), nb);
-	    unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
-	    remainder->bk = remainder->fd = unsorted_chunks(av);
-	    set_head(victim, nb | PREV_INUSE | INUSE);
-	    set_head(remainder, remainder_size | PREV_INUSE);
-	    hashtable_insert(victim, remainder);
-	    check_malloced_chunk(victim, nb);
-	    guard_set(av->guard_stored, victim, bytes, nb);
-	    VALGRIND_MALLOCLIKE_BLOCK(chunk(victim), bytes, 0, 0);
-	    return chunk(victim);
-	  } 
-	  /* Exhaust */
-	  else  {
-	    set_all_inuse(victim);
-	    check_malloced_chunk(victim, nb);
-	    guard_set(av->guard_stored, victim, bytes, nb);
-	    VALGRIND_MALLOCLIKE_BLOCK(chunk(victim), bytes, 0, 0);
-	    return chunk(victim);
-	  }
-	}
-	victim = victim->bk;
-      } while(victim != bin);
-    }
-  }
-
-  /*
-    Search for a chunk by scanning bins, starting with next largest
-    bin. This search is strictly by best-fit; i.e., the smallest
-    (with ties going to approximately the least recently used) chunk
-    that fits is selected.
-    
-    The bitmap avoids needing to check that most blocks are nonempty.
-  */
-    
-
-  ++idx;
-  bin = bin_at(av,idx);
-  block = idx2block(idx);
-  map = av->binmap[block];
-  bit = idx2bit(idx);
-  
-  for (;;) {
-    
-    /* Skip rest of block if there are no more set bits in this block.  */
-    if (bit > map || bit == 0) {
-      do {
-        if (++block >= BINMAPSIZE)  /* out of bins */
-          goto use_top;
-      } while ( (map = av->binmap[block]) == 0);
-      
-      bin = bin_at(av, (block << BINMAPSHIFT));
-      bit = 1;
-    }
-    
-    /* Advance to bin with set bit. There must be one. */
-    while ((bit & map) == 0) {
-      bin = next_bin(bin);
-      bit <<= 1;
-      assert(bit != 0);
-    }
-    
-    /* Inspect the bin. It is likely to be non-empty */
-    victim = last(bin);
-    
-    /*  If a false alarm (empty bin), clear the bit. */
-    if (victim == bin) {
-      av->binmap[block] = map &= ~bit; /* Write through */
-      bin = next_bin(bin);
-      bit <<= 1;
-    }
-    
-    else {
-      size = chunksize(victim);
-      
-      /*  We know the first chunk in this bin is big enough to use. */
-      assert((CHUNK_SIZE_T)(size) >= (CHUNK_SIZE_T)(nb));
-      
-      remainder_size = size - nb;
-      
-      /* unlink */
-      bck = victim->bk;
-      bin->bk = bck;
-      bck->fd = bin;
-      
-      /* Split */
-      if (remainder_size >= MINSIZE) {
-        remainder = cireg_getfree();
-        remainder->chunk = chunk_at_offset(chunk(victim), nb);
-        
-        unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
-        remainder->bk = remainder->fd = unsorted_chunks(av);
-        /* advertise as last remainder */
-        if (in_smallbin_range(nb))
-	  av->last_remainder = remainder;
-        
-        set_head(victim, nb | PREV_INUSE | INUSE);
-        set_head(remainder, remainder_size | PREV_INUSE);
-        hashtable_insert(victim, remainder);
-        check_malloced_chunk(victim, nb);
-	guard_set(av->guard_stored, victim, bytes, nb);
-	VALGRIND_MALLOCLIKE_BLOCK(chunk(victim), bytes, 0, 0);
-        return chunk(victim);
-      }
-      /* Exhaust */
-      else {
-        set_all_inuse(victim);
-        check_malloced_chunk(victim, nb);
-	guard_set(av->guard_stored, victim, bytes, nb);
-	VALGRIND_MALLOCLIKE_BLOCK(chunk(victim), bytes, 0, 0);
-        return chunk(victim);
-      }
-      
-    }
-  }
-
-  use_top:
-   
-
-  /*
-    If large enough, split off the chunk bordering the end of memory
-    (held in av->top). Note that this is in accord with the best-fit
-    search rule.  In effect, av->top is treated as larger (and thus
-    less well fitting) than any other available chunk since it can
-    be extended to be as large as necessary (up to system
-    limitations).
-    
-    We require that av->top always exists (i.e., has size >=
-    MINSIZE) after initialization, so if it would otherwise be
-    exhuasted by current request, it is replenished. (The main
-    reason for ensuring it exists is that we may need MINSIZE space
-    to put in fenceposts in sysmalloc.)
-  */
-  
-  victim = av->top;
-  size = chunksize(victim);
-  
-  if ((CHUNK_SIZE_T)(size) >= (CHUNK_SIZE_T)(nb + MINSIZE)) {
-    remainder = cireg_getfree();
-    remainder_size = size - nb;
-    remainder->chunk = chunk_at_offset(chunk(victim), nb);
-    av->top = remainder;
-    set_head(victim, nb | PREV_INUSE | INUSE);
-    set_head(remainder, remainder_size | PREV_INUSE);
-    hashtable_insert(victim, remainder);
-    check_malloced_chunk(victim, nb);
-    guard_set(av->guard_stored, victim, bytes, nb);
-    VALGRIND_MALLOCLIKE_BLOCK(chunk(victim), bytes, 0, 0);
-    return chunk(victim);
-  }
-  
-  /* 
-     If no space in top, relay to handle system-dependent cases 
-  */
-  retval = sYSMALLOc(nb, av);
-  if (retval) {
-#if PARANOIA > 2
-    victim = mem2chunk(retval); /* is used in guard_set macro */
-#endif
-    guard_set(av->guard_stored, victim, bytes, nb);
-  }
-  VALGRIND_MALLOCLIKE_BLOCK(retval, bytes, 0, 0);
-  return retval;
-}
-
-/*
-  ------------------------------ free ------------------------------
-*/
-
-#if __STD_C
-DL_STATIC void fREe(Void_t* mem)
-#else
-DL_STATIC void fREe(mem) Void_t* mem;
-#endif
-{
-  mstate av = get_malloc_state();
-
-  chunkinfoptr       p;           /* chunk corresponding to mem */
-  INTERNAL_SIZE_T size;        /* its size */
-  mfastbinptr*    fb;          /* associated fastbin */
-  chunkinfoptr       prevchunk;   /* previous physical chunk */
-  chunkinfoptr       nextchunk;   /* next contiguous chunk */
-  INTERNAL_SIZE_T nextsize;    /* its size */
-  INTERNAL_SIZE_T prevsize;    /* size of previous contiguous chunk */
-  chunkinfoptr       bck;         /* misc temp for linking */
-  chunkinfoptr       fwd;         /* misc temp for linking */
-  chunkinfoptr	     next;
-#if defined(SH_CUTEST)
-  extern int malloc_count;
-  --malloc_count;
-#endif
-
-  /* free(0) has no effect */
-  if (mem != 0) {
-    p = hashtable_lookup(mem);
-    /* check that memory is managed by us 
-     * and is inuse 
-     */
-    if (UNLIKELY(!p || !inuse(p))) 
-      {
-#ifdef DNMALLOC_CHECKS
-	if (p) {
-	  fprintf(stderr, "Attempt to free memory not in use\n");
-	  abort();
-	} else {
-	  fprintf(stderr, "Attempt to free memory not allocated\n");
-	  abort();
-	}
-#endif
-	assert(p && inuse(p));
-	return;
-      }
-
-    VALGRIND_FREELIKE_BLOCK(mem, 0);
- 
-    guard_check(av->guard_stored, p);
-    
-    size = chunksize(p);
-
-    check_inuse_chunk(p);
-
-    /*
-      If eligible, place chunk on a fastbin so it can be found
-      and used quickly in malloc.
-    */
-
-    if ((CHUNK_SIZE_T)(size) <= (CHUNK_SIZE_T)(av->max_fast)
-
-#if TRIM_FASTBINS
-        /* 
-           If TRIM_FASTBINS set, don't place chunks
-           bordering top into fastbins
-        */
-        && (chunk_at_offset(chunk(p), size) != av->top)
-#endif
-        ) {
-
-      set_fastchunks(av);
-      fb = &(av->fastbins[fastbin_index(size)]);
-      p->fd = *fb;
-      *fb = p;
-    }
-
-    /*
-       Consolidate other non-mmapped chunks as they arrive.
-    */
-
-    else if (!chunk_is_mmapped(p)) {
-      set_anychunks(av);
-
-      nextchunk = next_chunkinfo(p);
-      if (nextchunk)
-	nextsize = chunksize(nextchunk);
-      else
-	nextsize = 0;/* gcc doesn't notice that it's only used if (nextchunk)*/
-
-      /* consolidate backward */
-      if (UNLIKELY(!prev_inuse(p))) {
-        prevchunk = prev_chunkinfo(p);
-        prevsize = chunksize(prevchunk);
-#ifdef DNMALLOC_CHECKS
-	if (inuse(prevchunk)) {
-		fprintf(stderr, "Dnmalloc error: trying to unlink an inuse chunk: %p (chunk: %p)\n This is definitely a bug, please report it to dnmalloc@fort-knox.org.\n", prevchunk, chunk(prevchunk));
-		abort();
-	}
-#else
-	assert(!inuse(prevchunk));
-#endif
-        size += prevsize;
-        unlink(prevchunk, bck, fwd);
-	set_head(p, size | PREV_INUSE);
-        hashtable_skiprm(prevchunk,p);
-        /* This chunk no longer exists in any form: release the chunkinfoptr 
-	 */
-        freecilst_add(p);
-        p = prevchunk;
-      }
-
-      if (nextchunk) {
-	if (nextchunk != av->top) {
-	  /* get and clear inuse bit */
-	  clear_previnuse(nextchunk);
-	  
-	  /* consolidate forward */
-	  if (!inuse(nextchunk)) {
-	    /* if mmap is used instead of sbrk, we may have a
-	     * chunk with !nextchunk->fd && !nextchunk->bk
-	     */
-	    if (nextchunk->fd && nextchunk->fd)
-	      {
-		unlink(nextchunk, bck, fwd);
-		size += nextsize;
-		set_head(p, size | PREV_INUSE);
-		hashtable_skiprm(p, nextchunk);
-		freecilst_add (nextchunk);
-	      }
-	  }
-	  
-	  set_head(p, size | PREV_INUSE);
-	  next = next_chunkinfo(p);
-	  if (next)
-	    next->prev_size = size;
-	  
-	  /*
-	    Place the chunk in unsorted chunk list. Chunks are
-	    not placed into regular bins until after they have
-	    been given one chance to be used in malloc.
-	  */
-	  
-	  bck = unsorted_chunks(av);
-	  fwd = bck->fd;
-	  p->bk = bck;
-	  p->fd = fwd;
-	  bck->fd = p;
-	  fwd->bk = p;
-	  
-	  nextchunk = next_chunkinfo(p);
-	  if (nextchunk)
-	    nextchunk->prev_size = chunksize(p);	
-	  
-	  check_free_chunk(p);
-	}
-	
-	/*
-	  If the chunk borders the current high end of memory,
-	  consolidate into top
-	*/
-	
-	else {
-	  size += nextsize;
-	  set_head(p, size | PREV_INUSE);
-	  hashtable_remove(chunk(av->top));
-	  freecilst_add(av->top);
-	  av->top = p;
-	  check_chunk(p);
-	}
-      } /* if (nextchunk) */
-
-      /*
-        If freeing a large space, consolidate possibly-surrounding
-        chunks. Then, if the total unused topmost memory exceeds trim
-        threshold, ask malloc_trim to reduce top.
-
-        Unless max_fast is 0, we don't know if there are fastbins
-        bordering top, so we cannot tell for sure whether threshold
-        has been reached unless fastbins are consolidated.  But we
-        don't want to consolidate on each free.  As a compromise,
-        consolidation is performed if FASTBIN_CONSOLIDATION_THRESHOLD
-        is reached.
-      */
-
-      if (UNLIKELY((CHUNK_SIZE_T)(size) >= FASTBIN_CONSOLIDATION_THRESHOLD)) { 
-        if (have_fastchunks(av)) 
-          malloc_consolidate(av);
-
-#ifndef MORECORE_CANNOT_TRIM        
-        if ((CHUNK_SIZE_T)(chunksize(av->top)) >= 
-            (CHUNK_SIZE_T)(av->trim_threshold))
-	  {
-	    if (morecore32bit(av))
-	      {
-#ifdef DNMALLOC_DEBUG
-		fprintf(stderr, "Calling systrim from free()\n");
-#endif
-		sYSTRIm(av->top_pad, av);
-#ifdef DNMALLOC_DEBUG
-		fprintf(stderr, "Systrim done\n");
-#endif
-	      }
-	  }
-#endif
-      }
-
-    }
-    /*
-      If the chunk was allocated via mmap, release via munmap()
-      Note that if HAVE_MMAP is false but chunk_is_mmapped is
-      true, then user must have overwritten memory. There's nothing
-      we can do to catch this error unless DEBUG is set, in which case
-      check_inuse_chunk (above) will have triggered error.
-    */
-
-    else {
-#if  PARANOIA > 0
-      int ret;
-#endif
-      INTERNAL_SIZE_T offset = (INTERNAL_SIZE_T) p->hash_next;
-      av->n_mmaps--;
-      av->mmapped_mem -= (size + offset);
-#if  PARANOIA > 0
-      ret = munmap((char*) chunk(p) - offset, size + offset);
-#else
-      munmap((char*) chunk(p) - offset, size + offset);
-#endif
-      hashtable_remove_mmapped(chunk(p));
-      freecilst_add(p);
-      /* munmap returns non-zero on failure */
-      assert(ret == 0);
-    }
-  }
-}
-
-/*
-  ------------------------- malloc_consolidate -------------------------
-
-  malloc_consolidate is a specialized version of free() that tears
-  down chunks held in fastbins.  Free itself cannot be used for this
-  purpose since, among other things, it might place chunks back onto
-  fastbins.  So, instead, we need to use a minor variant of the same
-  code.
-  
-  Also, because this routine needs to be called the first time through
-  malloc anyway, it turns out to be the perfect place to trigger
-  initialization code.
-*/
-
-#if __STD_C
-static void malloc_consolidate(mstate av)
-#else
-static void malloc_consolidate(av) mstate av;
-#endif
-{
-  mfastbinptr*    fb;                 /* current fastbin being consolidated */
-  mfastbinptr*    maxfb;              /* last fastbin (for loop control) */
-  chunkinfoptr       p;                  /* current chunk being consolidated */
-  chunkinfoptr       nextp;              /* next chunk to consolidate */
-  chunkinfoptr       prevp;
-  chunkinfoptr       unsorted_bin;       /* bin header */
-  chunkinfoptr       first_unsorted;     /* chunk to link to */
-
-  /* These have same use as in free() */
-  chunkinfoptr       nextchunk;
-  INTERNAL_SIZE_T size;
-  INTERNAL_SIZE_T nextsize;
-  INTERNAL_SIZE_T prevsize;
-  chunkinfoptr       bck;
-  chunkinfoptr       fwd;
-  chunkinfoptr	     next;
- 
-  /*
-    If max_fast is 0, we know that av hasn't
-    yet been initialized, in which case do so below
-  */
-  if (av && av->max_fast != 0) {
-
-    clear_fastchunks(av);
-
-    unsorted_bin = unsorted_chunks(av);
-
-    /*
-      Remove each chunk from fast bin and consolidate it, placing it
-      then in unsorted bin. Among other reasons for doing this,
-      placing in unsorted bin avoids needing to calculate actual bins
-      until malloc is sure that chunks aren't immediately going to be
-      reused anyway.
-    */
-    
-    maxfb = &(av->fastbins[fastbin_index(av->max_fast)]);
-    fb = &(av->fastbins[0]);
-    do {
-      if ( UNLIKELY((p = *fb) != 0)) {
-        *fb = 0;
-	do {
-          check_inuse_chunk(p);
-          nextp = p->fd;
-          
-          /*
-	   * Slightly streamlined version of consolidation code in free() 
-	   */
-
-          size = chunksize(p);
-          nextchunk = next_chunkinfo(p);
-
-	  /* gcc doesn't notice that it's only used if (nextchunk) */
-	  if (nextchunk)
-	    nextsize = chunksize(nextchunk);
-	  else
-	    nextsize = 0; 
-          
-	  if (!prev_inuse(p)) {
-             prevp = prev_chunkinfo(p);
-             prevsize = chunksize(prevp);
-             size += prevsize;
-#ifdef DNMALLOC_CHECKS
-	     if (inuse(prevp)) {
-		fprintf(stderr, "Dnmalloc error: trying to unlink an inuse chunk (2): %p (chunk: %p)\n This is definitely a bug, please report it to dnmalloc@fort-knox.org.\n", prevp, chunk(prevp));
-		     abort();
-	     }
-#else
-	     assert(!inuse(prevp));
-#endif
-             unlink(prevp, bck, fwd);
-             set_head(p, size | PREV_INUSE);	     
-             hashtable_skiprm(prevp,p);
-             freecilst_add(p);
-             p=prevp;
-          }
-          
-	  if (nextchunk) {
-	    if (nextchunk != av->top) {
-
-	      clear_previnuse(nextchunk);
-            
-	      /* if mmap is used instead of sbrk, we may have a
-	       * chunk with !nextchunk->fd && !nextchunk->bk
-	       */
-	      if (!inuse(nextchunk)) {
-		if( nextchunk->fd && nextchunk->bk) {
-		  size += nextsize;
-		  unlink(nextchunk, bck, fwd);
-		  set_head(p, size | PREV_INUSE);
-		  hashtable_skiprm(p,nextchunk);
-		  freecilst_add(nextchunk);
-		}
-	      }
-	      
-	      first_unsorted = unsorted_bin->fd;
-	      unsorted_bin->fd = p;
-	      first_unsorted->bk = p;
-	      
-	      set_head(p, size | PREV_INUSE);
-	      p->bk = unsorted_bin;
-	      p->fd = first_unsorted;
-	      next = next_chunkinfo(p);
-	      if (next)
-	    	next->prev_size = size;
-
-            
-	    }
-          
-	    else if (nextchunk == av->top) {
-	      size += nextsize;
-	      set_head(p, size | PREV_INUSE);
-	      hashtable_remove(chunk(av->top));
-	      freecilst_add(av->top);
-	      av->top = p;
-	    }
-	  } /* if (nextchunk) */
-          
-        } while ( (p = nextp) != 0);
-        
-      }
-    } while (fb++ != maxfb);
-  }
-  else {
-    /* Initialize dnmalloc */
-    dnmalloc_init();
-    malloc_init_state(get_malloc_state());
-    check_malloc_state();
-  }
-}
-
-/*
-  ------------------------------ realloc ------------------------------
-*/
-
-
-#if __STD_C
-DL_STATIC Void_t* rEALLOc(Void_t* oldmem, size_t bytes)
-#else
-DL_STATIC Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes;
-#endif
-{
-  mstate av = get_malloc_state();
-
-  INTERNAL_SIZE_T  nb;              /* padded request size */
-
-  chunkinfoptr        oldp;            /* chunk corresponding to oldmem */
-  INTERNAL_SIZE_T  oldsize;         /* its size */
-
-  chunkinfoptr        newp;            /* chunk to return */
-  INTERNAL_SIZE_T  newsize;         /* its size */
-  Void_t*          newmem;          /* corresponding user mem */
-
-  chunkinfoptr        next;            /* next contiguous chunk after oldp */
-
-  chunkinfoptr        remainder;       /* extra space at end of newp */
-  CHUNK_SIZE_T     remainder_size;  /* its size */
-
-  chunkinfoptr        bck;             /* misc temp for linking */
-  chunkinfoptr        fwd;             /* misc temp for linking */
-
-  CHUNK_SIZE_T     copysize;        /* bytes to copy */
-  unsigned int     ncopies;         /* INTERNAL_SIZE_T words to copy */
-  INTERNAL_SIZE_T* s;               /* copy source */ 
-  INTERNAL_SIZE_T* d;               /* copy destination */
-
-  
-#ifdef REALLOC_ZERO_BYTES_FREES
-  if (UNLIKELY(bytes == 0)) {
-    fREe(oldmem);
-    return 0;
-  }
-#endif
-
-  if (UNLIKELY(!av || av->max_fast == 0)) {
-    malloc_consolidate(av);
-    av = get_malloc_state();
-  }
-
-  /* realloc of null is supposed to be same as malloc */
-  if (UNLIKELY(oldmem == 0)) 
-    return mALLOc(bytes);
-
-  checked_request2size(bytes, nb);
-
-  oldp    = hashtable_lookup(oldmem);
-  
-  if (UNLIKELY(!oldp || !inuse(oldp))){ 
-     /* attempt to either realloc memory not managed by us 
-      * or memory that is not in use 
-      */
-#ifdef DNMALLOC_CHECKS
-    if (oldp) {
-      fprintf(stderr, "Attempt to free memory not in use\n");
-      abort();
-    } else {
-      fprintf(stderr, "Attempt to free memory not allocated\n");
-      abort();
-    }
-#endif
-    assert(oldp && inuse(oldp));
-    return 0;     
-  }
-
-  VALGRIND_FREELIKE_BLOCK(oldmem, 0);
-  guard_check(av->guard_stored, oldp);
-
-  oldsize = chunksize(oldp);
-
-  check_inuse_chunk(oldp);
-
-  if (!chunk_is_mmapped(oldp)) {
-
-    if (UNLIKELY((CHUNK_SIZE_T)(oldsize) >= (CHUNK_SIZE_T)(nb))) {
-      /* already big enough; split below */
-      newp    = oldp;
-      newsize = oldsize;
-    }
-
-    else {
-      next = next_chunkinfo(oldp);
-      if (next)
-      	next->prev_size = oldsize;
-      /* Try to expand forward into top */
-      if (next && next == av->top &&
-          (CHUNK_SIZE_T)(newsize = oldsize + chunksize(next)) >=
-          (CHUNK_SIZE_T)(nb + MINSIZE)) {
-         set_head_size(oldp, nb);
-         hashtable_remove(chunk(av->top));
-         av->top->chunk = chunk_at_offset(chunk(oldp), nb);
-         set_head(av->top, (newsize - nb) | PREV_INUSE);
-         /* av->top->chunk has been moved move in hashtable */
-         hashtable_insert(oldp, av->top);
-	 guard_set(av->guard_stored, oldp, bytes, nb);
-	 VALGRIND_MALLOCLIKE_BLOCK(chunk(oldp), bytes, 0, 0); 
-         return chunk(oldp);
-      }
-      
-      /* Try to expand forward into next chunk;  split off remainder below */
-      else if (next && next != av->top && 
-               !inuse(next) &&
-               (CHUNK_SIZE_T)(newsize = oldsize + chunksize(next)) >=
-               (CHUNK_SIZE_T)(nb)) {
-        newp = oldp;
-        unlink(next, bck, fwd);
-        hashtable_remove(chunk(next));
-        freecilst_add(next);
-	next = next_chunkinfo(oldp);
-	if (next)
-	  next->prev_size = newsize;
-      }
-
-      /* allocate, copy, free */
-      else {
-
-        newmem = mALLOc(nb - MALLOC_ALIGN_MASK);
-         if (newmem == 0)
-          return 0; /* propagate failure */
-
-        newp = hashtable_lookup(newmem);
-        newsize = chunksize(newp);
-	
- 
-        /* next = next_chunkinfo(oldp); *//* 'next' never used rw 19.05.2008 */
-        /*
-          Avoid copy if newp is next chunk after oldp.
-        */
-	if (UNLIKELY(is_next_chunk(oldp, newp))) {
-	  newsize += oldsize;
-	  set_head_size(oldp, newsize);
-	  hashtable_skiprm(oldp, newp);
-	  freecilst_add(newp);	  
-          newp = oldp;
-        }
-        else {
-          /*
-            Unroll copy of <= 40 bytes (80 if 8byte sizes)
-            We know that contents have an even number of
-            INTERNAL_SIZE_T-sized words; minimally 4 (2 on amd64).
-          */
-          
-	  VALGRIND_MALLOCLIKE_BLOCK(chunk(oldp), chunksize(oldp), 0, 0);
-
-          copysize = oldsize;
-          s = (INTERNAL_SIZE_T*)(oldmem);
-          d = (INTERNAL_SIZE_T*)(newmem);
-          ncopies = copysize / sizeof(INTERNAL_SIZE_T);
-          assert(ncopies >= 2);
-          
-          if (ncopies > 10)
-            MALLOC_COPY(d, s, copysize);
-          
-          else {
-            *(d+0) = *(s+0);
-            *(d+1) = *(s+1);
-	    if (ncopies > 2) {
-	      *(d+2) = *(s+2);
-	      *(d+3) = *(s+3);
-	      if (ncopies > 4) {
-		*(d+4) = *(s+4);
-		*(d+5) = *(s+5);
-		if (ncopies > 6) {
-		  *(d+6) = *(s+6);
-		  *(d+7) = *(s+7);
-		  if (ncopies > 8) {
-		    *(d+8) = *(s+8);
-		    *(d+9) = *(s+9);
-		  }
-                }
-              }
-            }
-          }
-          
-          fREe(oldmem);
-          check_inuse_chunk(newp);
-	  guard_set(av->guard_stored, newp, bytes, nb);
-          return chunk(newp);
-        }
-      }
-    }
-
-    /* If possible, free extra space in old or extended chunk */
-
-    assert((CHUNK_SIZE_T)(newsize) >= (CHUNK_SIZE_T)(nb));
-
-    remainder_size = newsize - nb;
-
-    if (remainder_size >= MINSIZE) { /* split remainder */
-      remainder = cireg_getfree();
-      remainder->chunk = chunk_at_offset(chunk(newp), nb);
-      set_head_size(newp, nb);
-      set_head(remainder, remainder_size | PREV_INUSE | INUSE);
-      remainder->prev_size = nb;
-      hashtable_insert(newp, remainder);
-      /* Mark remainder as inuse so free() won't complain */
-      set_all_inuse(remainder);
-      guard_set(av->guard_stored, remainder, 0, remainder_size);
-      VALGRIND_MALLOCLIKE_BLOCK(chunk(remainder), remainder_size, 0, 0);
-      fREe(chunk(remainder)); 
-    }
-    else { /* not enough extra to split off */
-      set_head_size(newp, newsize);
-      set_all_inuse(newp);
-    }
-
-    check_inuse_chunk(newp);
-    guard_set(av->guard_stored, newp, bytes, nb);
-    VALGRIND_MALLOCLIKE_BLOCK(chunk(newp), bytes, 0, 0);
-    return chunk(newp);
-  }
-
-  /*
-    Handle mmap cases
-  */
-
-  else {
-
-#if HAVE_MREMAP
-    INTERNAL_SIZE_T offset = (INTERNAL_SIZE_T) oldp->hash_next;
-    size_t pagemask = av->pagesize - 1;
-    char *cp;
-    CHUNK_SIZE_T  sum;
-    
-
-    /* Note the extra SIZE_SZ overhead */
-    /* newsize = (nb + offset + SIZE_SZ + pagemask) & ~pagemask; */
-    newsize = (nb + offset + pagemask) & ~pagemask;
-
-    /* don't need to remap if still within same page */
-    if (oldsize == newsize - offset)
-      {
-	guard_set(av->guard_stored, oldp, bytes, nb);
-	VALGRIND_FREELIKE_BLOCK(oldmem, 0);
-	VALGRIND_MALLOCLIKE_BLOCK(oldmem, bytes, 0, 0);
-	return oldmem;
-      }
-
-    cp = (char*)mremap((char*)chunk(oldp) - offset, oldsize + offset, newsize, 1);
-    
-    if (cp != (char*)MORECORE_FAILURE) {
-       
-      hashtable_remove_mmapped(chunk(oldp));
-       
-      oldp->chunk = (mchunkptr)(cp + offset);
-      set_head(oldp, (newsize - offset)|IS_MMAPPED|INUSE);
-      
-      hashtable_add(oldp);
-      
-      assert(aligned_OK(chunk(oldp))); /* rw fix: newp -> oldp */
-      assert(( ((INTERNAL_SIZE_T) oldp->hash_next) == offset));
-      
-      /* update statistics */
-      sum = av->mmapped_mem += newsize - oldsize;
-      if (sum > (CHUNK_SIZE_T)(av->max_mmapped_mem)) 
-        av->max_mmapped_mem = sum;
-      sum += av->sbrked_mem;
-      if (sum > (CHUNK_SIZE_T)(av->max_total_mem)) 
-        av->max_total_mem = sum;
-      
-      guard_set(av->guard_stored, oldp, bytes, nb);
-      VALGRIND_FREELIKE_BLOCK(oldmem, 0);
-      VALGRIND_MALLOCLIKE_BLOCK(chunk(oldp), bytes, 0, 0);
-      return chunk(oldp);
-    }
-#endif /* have MREMAP */
-
-    /* Note the extra SIZE_SZ overhead. */
-    if ((CHUNK_SIZE_T)(oldsize) >= (CHUNK_SIZE_T)(nb + SIZE_SZ)) 
-      newmem = oldmem; /* do nothing */
-    else {
-      /* Must alloc, copy, free. */
-      newmem = mALLOc(nb - MALLOC_ALIGN_MASK);
-      if (newmem != 0) {
-        MALLOC_COPY(newmem, oldmem, oldsize);
-        fREe(oldmem);
-      }
-    }
-    VALGRIND_MALLOCLIKE_BLOCK(newmem, bytes, 0, 0);
-    guard_set(av->guard_stored, mem2chunk(newmem), bytes, nb);
-    return newmem;
-  }
-}
-
-/*
-  ---------------------------posix_memalign ----------------------------
-*/
-
-#if __STD_C
-DL_STATIC int posix_mEMALIGn(Void_t** memptr, size_t alignment, size_t bytes)
-#else
-DL_STATIC int posix_mEMALIGn(memptr, alignment, bytes) Void_t** memptr; size_t alignment; size_t bytes;
-#endif
-{
-  mstate av;
-
-  if (alignment % sizeof(void *) != 0)
-    return EINVAL;
-  if ((alignment & (alignment - 1)) != 0)
-    return EINVAL;
-
-  av = get_malloc_state();
-  if (!av || av->max_fast == 0) malloc_consolidate(av);
-  *memptr =  mEMALIGn(alignment, bytes);
-
-  return (*memptr != NULL ? 0 : ENOMEM);
-}
-
-/*
-  ------------------------------ memalign ------------------------------
-*/
-
-#if __STD_C
-DL_STATIC Void_t* mEMALIGn(size_t alignment, size_t bytes)
-#else
-DL_STATIC Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes;
-#endif
-{
-  INTERNAL_SIZE_T nb;             /* padded  request size */
-  char*           m;              /* memory returned by malloc call */
-  chunkinfoptr       p;              /* corresponding chunk */
-  char*           brk;            /* alignment point within p */
-  chunkinfoptr       newp;           /* chunk to return */
-  INTERNAL_SIZE_T newsize;        /* its size */
-  INTERNAL_SIZE_T leadsize;       /* leading space before alignment point */
-  chunkinfoptr       remainder;      /* spare room at end to split off */
-  CHUNK_SIZE_T    remainder_size; /* its size */
-  INTERNAL_SIZE_T size;
-#if PARANOIA > 2
-  mstate          av;
-#endif
-
-  /* If need less alignment than we give anyway, just relay to malloc */
-
-  if (UNLIKELY(alignment <= MALLOC_ALIGNMENT)) return mALLOc(bytes);
-
-  /* Otherwise, ensure that it is at least a minimum chunk size */
-
-  if (alignment <  MINSIZE) alignment = MINSIZE;
-
-  /* Make sure alignment is power of 2 (in case MINSIZE is not).  */
-  if (UNLIKELY((alignment & (alignment - 1)) != 0)) {
-    size_t a = MALLOC_ALIGNMENT * 2;
-    while ((CHUNK_SIZE_T)a < (CHUNK_SIZE_T)alignment) a <<= 1;
-    alignment = a;
-  }
-
-  checked_request2size(bytes, nb);
-
-  /*
-    Strategy: find a spot within that chunk that meets the alignment
-    request, and then possibly free the leading and trailing space.
-  */
-
-
-  /* Call malloc with worst case padding to hit alignment. */
-
-  m  = (char*)(mALLOc(nb + alignment + MINSIZE));
-
-  if (m == 0) return 0; /* propagate failure */
-
-#if PARANOIA > 2
-  av = get_malloc_state();
-#endif
-
-  p = hashtable_lookup((mchunkptr) m);
-
-  if ((((PTR_UINT)(m)) % alignment) != 0) { /* misaligned */
-
-    /*
-      Find an aligned spot inside chunk.  Since we need to give back
-      leading space in a chunk of at least MINSIZE, if the first
-      calculation places us at a spot with less than MINSIZE leader,
-      we can move to the next aligned spot -- we've allocated enough
-      total room so that this is always possible.
-    */
-
-    brk = (char*) ((PTR_UINT)(((PTR_UINT)(m + alignment - 1)) &
-                           -((signed long) alignment)));
-    if ((CHUNK_SIZE_T)(brk - (char*)(chunk(p))) < MINSIZE)
-      brk += alignment;
-
-    newp = cireg_getfree();
-    newp->chunk = (mchunkptr)brk;
-    leadsize = brk - (char*)(chunk(p));
-    newsize = chunksize(p) - leadsize;
-
-    /* For mmapped chunks, just adjust offset */
-    if (UNLIKELY(chunk_is_mmapped(p))) {
-      newp->hash_next = (chunkinfoptr) (((INTERNAL_SIZE_T) p->hash_next) + leadsize);
-      set_head(newp, newsize|IS_MMAPPED|INUSE);
-      hashtable_remove_mmapped(chunk(p));
-      freecilst_add(p);
-      hashtable_add(newp);
-      guard_set(av->guard_stored, newp, bytes, nb);
-      return chunk(newp);
-    }
-
-    /* Otherwise, give back leader, use the rest */
-    set_head(newp, newsize | PREV_INUSE | INUSE);
-    set_head_size(p, leadsize);
-    set_all_inuse(newp);
-    hashtable_add(newp); /* 20.05.2008 rw */
-    guard_set(av->guard_stored, p, 0, leadsize);
-    fREe(chunk(p));
-    p = newp;
-
-    assert (newsize >= nb &&
-            (((PTR_UINT)(chunk(p))) % alignment) == 0);
-  }
-
-  /* Also give back spare room at the end */
-  if (!chunk_is_mmapped(p)) {
-    size = chunksize(p);
-    if ((CHUNK_SIZE_T)(size) > (CHUNK_SIZE_T)(nb + MINSIZE)) {
-       remainder = cireg_getfree();
-       remainder_size = size - nb;
-       remainder->chunk = chunk_at_offset(chunk(p), nb);
-       set_head(remainder, remainder_size | PREV_INUSE | INUSE);
-       set_head_size(p, nb);
-       hashtable_add(remainder); /* 20.05.2008 rw */
-       guard_set(av->guard_stored, remainder, 0, remainder_size);
-       fREe(chunk(remainder));
-    }
-  }
-
-  check_inuse_chunk(p);
-  guard_set(av->guard_stored, p, bytes, nb);
-  return chunk(p);
-}
-
-/*
-  ------------------------------ calloc ------------------------------
-*/
-
-#if __STD_C
-DL_STATIC Void_t* cALLOc(size_t n_elements, size_t elem_size)
-#else
-DL_STATIC Void_t* cALLOc(n_elements, elem_size) size_t n_elements; size_t elem_size;
-#endif
-{
-  chunkinfoptr p;
-  CHUNK_SIZE_T  clearsize;
-  CHUNK_SIZE_T  nclears;
-  INTERNAL_SIZE_T* d;
-  Void_t* mem;
- 
-  
-  mem = mALLOc(n_elements * elem_size);
-
-  if (mem != 0) {
-    p = hashtable_lookup(mem);
-
-    if (!chunk_is_mmapped(p))
-    {  
-      /*
-        Unroll clear of <= 40 bytes (80 if 8byte sizes)
-        We know that contents have an even number of
-        INTERNAL_SIZE_T-sized words; minimally 4 (2 on amd64).
-      */
-
-      d = (INTERNAL_SIZE_T*)mem;
-      clearsize = chunksize(p);
-      nclears = clearsize / sizeof(INTERNAL_SIZE_T);
-      assert(nclears >= 2);
-
-      if (nclears > 10) {
-        MALLOC_ZERO(d, clearsize);
-      }
-
-      else {
-        *(d+0) = 0;
-        *(d+1) = 0;
-	if (nclears > 2) {
-	  *(d+2) = 0;
-	  *(d+3) = 0;
-	  if (nclears > 4) {
-	    *(d+4) = 0;
-	    *(d+5) = 0;
-	    if (nclears > 6) {
-	      *(d+6) = 0;
-	      *(d+7) = 0;
-	      if (nclears > 8) {
-		*(d+8) = 0;
-		*(d+9) = 0;
-	      }
-            }
-          }
-        }
-      }
-    }
-#if ! MMAP_CLEARS
-    else
-    {
-      d = (INTERNAL_SIZE_T*)mem;
-      clearsize = chunksize(p);
-      MALLOC_ZERO(d, clearsize);
-    }
-#endif
-    /* Set guard again, since we just cleared it
-     */
-    guard_set(get_malloc_state()->guard_stored, p, (n_elements * elem_size), p->size);
-  }
-
-  return mem;
-}
-
-/*
-  ------------------------------ valloc ------------------------------
-*/
-
-#if __STD_C
-DL_STATIC Void_t* vALLOc(size_t bytes)
-#else
-DL_STATIC Void_t* vALLOc(bytes) size_t bytes;
-#endif
-{
-  /* Ensure initialization */
-  mstate av = get_malloc_state();
-  if (!av || av->max_fast == 0) {
-    malloc_consolidate(av);
-    av = get_malloc_state();
-  }
-  return mEMALIGn(av->pagesize, bytes);
-}
-
-/*
-  ------------------------------ pvalloc ------------------------------
-*/
-
-
-#if __STD_C
-DL_STATIC Void_t* pVALLOc(size_t bytes)
-#else
-DL_STATIC Void_t* pVALLOc(bytes) size_t bytes;
-#endif
-{
-  mstate av = get_malloc_state();
-  size_t pagesz;
-
-  /* Ensure initialization */
-  if (!av || av->max_fast == 0) {
-    malloc_consolidate(av);
-    av = get_malloc_state();
-  }
-  pagesz = av->pagesize;
-  return mEMALIGn(pagesz, (bytes + pagesz - 1) & ~(pagesz - 1));
-}
-   
-
-/*
-  ------------------------------ malloc_trim ------------------------------
-*/
-
-#if __STD_C
-DL_STATIC int mTRIm(size_t pad)
-#else
-DL_STATIC int mTRIm(pad) size_t pad;
-#endif
-{
-  mstate av = get_malloc_state();
-  /* Ensure initialization/consolidation */
-  malloc_consolidate(av);
-  av = get_malloc_state();
-#ifndef MORECORE_CANNOT_TRIM
-  if (morecore32bit(av))
-    return sYSTRIm(pad, av);
-  else
-    return 0;
-#else
-  return 0;
-#endif
-}
-
-
-
-/*
-  ------------------------- malloc_usable_size -------------------------
-*/
-
-#if __STD_C
-DL_STATIC size_t mUSABLe(Void_t* mem)
-#else
-DL_STATIC size_t mUSABLe(mem) Void_t* mem;
-#endif
-{
-  chunkinfoptr p;
-  if (mem != 0) {
-    p = hashtable_lookup(mem);
-    if (p && inuse(p)) return chunksize(p);
-  }
-  return 0;
-}
-
-/*
-  ------------------------------ mallinfo ------------------------------
-*/
-
-DL_STATIC struct mallinfo2 mALLINFo2()
-{
-  mstate av = get_malloc_state();
-  static struct mallinfo2 mi;
-  unsigned int i;
-  mbinptr b;
-  chunkinfoptr p;
-  INTERNAL_SIZE_T avail;
-  INTERNAL_SIZE_T fastavail;
-  int nblocks;
-  int nfastblocks;
-
-  /* Ensure initialization */
-  if (!av || av->top == 0) {
-    malloc_consolidate(av);
-    av = get_malloc_state();
-  }
-  check_malloc_state();
-
-  if (!av || av->top == 0) {
-    return mi;
-  }
-  
-  /* Account for top */
-  avail = chunksize(av->top);
-  nblocks = 1;  /* top always exists */
-
-  /* traverse fastbins */
-  nfastblocks = 0;
-  fastavail = 0;
-
-  for (i = 0; i < NFASTBINS; ++i) {
-    for (p = av->fastbins[i]; p != 0; p = p->fd) {
-      ++nfastblocks;
-      fastavail += chunksize(p);
-    }
-  }
-
-  avail += fastavail;
-
-  /* traverse regular bins */
-  for (i = 1; i < NBINS; ++i) {
-    b = bin_at(av, i);
-    for (p = last(b); p != b; p = p->bk) {
-      ++nblocks;
-      avail += chunksize(p);
-    }
-  }
-
-  mi.smblks = nfastblocks;
-  mi.ordblks = nblocks;
-  mi.fordblks = avail;
-  mi.uordblks = av->sbrked_mem - avail;
-  mi.arena = av->sbrked_mem;
-  mi.hblks = av->n_mmaps;
-  mi.hblkhd = av->mmapped_mem;
-  mi.fsmblks = fastavail;
-  mi.keepcost = chunksize(av->top);
-  mi.usmblks = av->max_total_mem;
-  return mi;
-}
-
-/*
-  ------------------------------ malloc_stats ------------------------------
-*/
-
-DL_STATIC void mSTATs()
-{
-  struct mallinfo2 mi = mALLINFo2();
-
-  fprintf(stderr, "hashtable = %10lu MB\n", 
-	  (CHUNK_SIZE_T)(HASHTABLESIZE / (1024*1024)));
-  fprintf(stderr, "max system bytes = %10lu\n",
-	  (CHUNK_SIZE_T)(mi.usmblks));
-  fprintf(stderr, "system bytes     = %10lu  (%10lu sbrked, %10lu mmaped)\n",
-	  (CHUNK_SIZE_T)(mi.arena + mi.hblkhd),
-	  (CHUNK_SIZE_T)(mi.arena),
-	  (CHUNK_SIZE_T)(mi.hblkhd));
-  fprintf(stderr, "in use bytes     = %10lu\n",
-	  (CHUNK_SIZE_T)(mi.uordblks + mi.hblkhd));
-}
-
-
-/*
-  ------------------------------ mallopt ------------------------------
-*/
-
-#if __STD_C
-DL_STATIC int mALLOPt(int param_number, int value)
-#else
-DL_STATIC int mALLOPt(param_number, value) int param_number; int value;
-#endif
-{
-  mstate av = get_malloc_state();
-  /* Ensure initialization/consolidation */
-  malloc_consolidate(av);
-  av = get_malloc_state();
-
-  switch(param_number) {
-  case M_MXFAST:
-    if (value >= 0 && value <= MAX_FAST_SIZE) {
-      set_max_fast(av, value);
-      return 1;
-    }
-    else
-      return 0;
-
-  case M_TRIM_THRESHOLD:
-    av->trim_threshold = value;
-    return 1;
-
-  case M_TOP_PAD:
-    av->top_pad = value;
-    return 1;
-
-  case M_MMAP_THRESHOLD:
-    av->mmap_threshold = value;
-    return 1;
-
-  case M_MMAP_MAX:
-    if (value != 0)
-      return 0;
-    av->n_mmaps_max = value;
-    return 1;
-
-  default:
-    return 0;
-  }
-}
-
-
-/*	$OpenBSD: arc4random.c,v 1.19 2008/06/04 00:50:23 djm Exp $	*/
-
-/*
- * Copyright (c) 1996, David Mazieres <dm@uun.org>
- * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-/*
- * Arc4 random number generator for OpenBSD.
- *
- * This code is derived from section 17.1 of Applied Cryptography,
- * second edition, which describes a stream cipher allegedly
- * compatible with RSA Labs "RC4" cipher (the actual description of
- * which is a trade secret).  The same algorithm is used as a stream
- * cipher called "arcfour" in Tatu Ylonen's ssh package.
- *
- * Here the stream cipher has been modified always to include the time
- * when initializing the state.  That makes it impossible to
- * regenerate the same random sequence twice, so this can't be used
- * for encryption, but will generate good random numbers.
- *
- * RC4 is a registered trademark of RSA Laboratories.
- */
-
-/* Moved u_int8_t -> unsigned char (portability)
- * Eliminated unneeded functions, added read from /dev/urandom taken from:
- $MirOS: contrib/code/Snippets/arc4random.c,v 1.3 2008-03-04 22:53:14 tg Exp $
- * Modified by Robert Connolly from OpenBSD lib/libc/crypt/arc4random.c v1.11.
- * This is arc4random(3) using urandom.
- */
-
-#include <fcntl.h>
-#include <limits.h>
-#include <stdlib.h>
-#include <sys/param.h>
-#include <sys/time.h>
-
-struct arc4_stream {
-	unsigned char i;
-	unsigned char j;
-	unsigned char s[256];
-};
-
-static int rs_initialized;
-static struct arc4_stream rs;
-static pid_t arc4_stir_pid;
-static int arc4_count;
-
-static unsigned char arc4_getbyte(void);
-
-static void
-arc4_init(void)
-{
-	int     n;
-
-	for (n = 0; n < 256; n++)
-		rs.s[n] = n;
-	rs.i = 0;
-	rs.j = 0;
-}
-
-static inline void
-arc4_addrandom(unsigned char *dat, int datlen)
-{
-	int     n;
-	unsigned char si;
-
-	rs.i--;
-	for (n = 0; n < 256; n++) {
-		rs.i = (rs.i + 1);
-		si = rs.s[rs.i];
-		rs.j = (rs.j + si + dat[n % datlen]);
-		rs.s[rs.i] = rs.s[rs.j];
-		rs.s[rs.j] = si;
-	}
-	rs.j = rs.i;
-}
-
-#ifdef HAVE_SCHED_H
-#include <sched.h>
-#endif
-
-static void
-arc4_stir(void)
-{
-	int     i;
-        struct {
-                struct timeval tv1;
-                struct timeval tv2;
-                unsigned char rnd[(128 - 2*sizeof(struct timeval)) / sizeof(unsigned char)];
-        } rdat;
-#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
-        ssize_t sz = 0;
-	int    fd;
-#endif
- 
-        gettimeofday(&rdat.tv1, NULL);
-
-
-	if (!rs_initialized) {
-		arc4_init();
-		rs_initialized = 1;
-	}
-
-#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
-
-#ifdef HAVE_SCHED_YIELD
-	/* Yield the processor to introduce some random delay. */
-	(void) sched_yield();
-#endif
-
-	/*
-	 * Pthread problem in multithreaded code on *BSD.
-	 */
-        fd = open("/dev/urandom", O_RDONLY);
-        if (fd != -1) {
-	        sz = (size_t)read(fd, rdat.rnd, sizeof (rdat.rnd));
-		/* 
-		 * gcc complains if we ignore the return value of read(), and
-		 * the llvm/clang analyzer complains if we don't use it...
-		 */
-		if (sz > (-256)) /* always true */
-		  close(fd);
-        }
-	/*
-        if (sz > sizeof (rdat.rnd))
-                sz = 0;
-	*/
- #endif
-
-	arc4_stir_pid = getpid();
-        gettimeofday(&rdat.tv2, NULL);
-
-        arc4_addrandom((void *)&rdat, sizeof(rdat));
-
-	/*
-	 * Discard early keystream, as per recommendations in:
-	 * http://www.wisdom.weizmann.ac.il/~itsik/RC4/Papers/Rc4_ksa.ps
-	 */
-	for (i = 0; i < 256; i++)
-		(void)arc4_getbyte();
-	arc4_count = 1600000;
-}
-
-static unsigned char
-arc4_getbyte(void)
-{
-	unsigned char si, sj;
-
-	rs.i = (rs.i + 1);
-	si = rs.s[rs.i];
-	rs.j = (rs.j + si);
-	sj = rs.s[rs.j];
-	rs.s[rs.i] = sj;
-	rs.s[rs.j] = si;
-	return (rs.s[(si + sj) & 0xff]);
-}
-
-
- /* Changed to return char* */
-static char *
-dnmalloc_arc4random(void)
-{
-	static char val[4];
-	
-	/* We only call this once, hence no need for locking. */
-
-	/* _ARC4_LOCK(); */
-	arc4_count -= 4;
-	if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != getpid())
-		arc4_stir();
-
-	val[0] = (char) arc4_getbyte();
-	val[1] = (char) arc4_getbyte();
-	val[2] = (char) arc4_getbyte();
-	val[3] = (char) arc4_getbyte();
-
-	arc4_stir();
-	/* _ARC4_UNLOCK(); */
-	return val;
-}
-
-#else
-int dnmalloc_pthread_init() { return 0; }
-#endif /* ! USE_SYSTEM_MALLOC */
Index: trunk/src/exepack.c
===================================================================
--- trunk/src/exepack.c	(revision 591)
+++ trunk/src/exepack.c	(revision 1)
@@ -84,5 +84,4 @@
 {
   int file;
-  long result;
 
   unsigned long i    = argc; /* dummy use of argc to fix compiler warning */
@@ -130,5 +129,4 @@
   out_len = (unsigned long) programlen_compressed_0;
   len     = (unsigned long) programlen_0;
-  in_len  = len;
 
   outbuf  = program_0;
@@ -262,15 +260,11 @@
     } 
 
-  result = (long) write(file, inbuf, in_len);
-  if (result < 0 || in_len != (lzo_uint) result)
+  write(file, inbuf, in_len);
+
+#if defined(__linux__)
+
+  if ( 0 != fstat(file, &sbuf))
     {
       return (5);
-    }
-
-#if defined(__linux__)
-
-  if ( 0 != fstat(file, &sbuf))
-    {
-      return (6);
     } 
   
@@ -282,5 +276,5 @@
   if ( 0 != fstat(file, &fbuf))
     {
-      return (7);
+      return (6);
     } 
 
@@ -294,5 +288,5 @@
     {
       close  ( file );
-      return ( 8 );
+      return ( 6 );
     }
   
@@ -318,5 +312,5 @@
       fcntl  (file, F_SETFD, FD_CLOEXEC);
       execve (pname, argv, environ); 
-      return (9);
+      return (8);
     }
 #endif
@@ -331,5 +325,5 @@
       execve (fname, argv, environ);
       unlink (fname);
-      return (10);
+      return (9);
     } 
   else if (i == 0)
Index: trunk/src/exepack_fill.c
===================================================================
--- trunk/src/exepack_fill.c	(revision 591)
+++ trunk/src/exepack_fill.c	(revision 1)
@@ -169,5 +169,5 @@
 {
   FILE * fd;
-  unsigned long   clen;
+  long   clen;
   char * data;
   struct stat sbuf;
@@ -222,6 +222,5 @@
     return (-1);
 
-  if (clen != fread  (data, 1, clen, fd))
-    return (-1);
+  fread  (data, 1, clen, fd);
   fclose (fd);
 
@@ -338,5 +337,5 @@
       return (8);
     }
-  status = replaceData (data, clen, "CONTAINER", (char *) outbuf, out_len);
+  status = replaceData (data, clen, "CONTAINER", outbuf, out_len);
   if (status < 0)
     {
Index: trunk/src/kern_head.c
===================================================================
--- trunk/src/kern_head.c	(revision 1)
+++ trunk/src/kern_head.c	(revision 1)
@@ -0,0 +1,907 @@
+/* 
+ * need to #define SH_USE_KERN
+ *
+ */
+#define SH_SYSCALL_CODE
+
+#include "config.h"
+
+#ifdef HOST_IS_I86LINUX
+#define SH_IDT_TABLE
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#if defined(SH_USE_KERN) 
+
+#undef _
+#define _(string)  string
+#undef N_
+#define N_(string) string
+
+void usage(int flag)
+{
+  printf("\n");
+  printf("Usage: kern_head [-v | --verbose]\n");
+  printf("       kern_head [-h | --help]\n");
+  printf("\n");
+  /*
+   * printf("       You need superuser privileges to use this program,\n");
+   * printf("       because only the superuser can read from /dev/kmem.\n");
+   * printf("\n");
+   */
+  exit(flag);
+}
+
+#if defined(HOST_IS_LINUX)
+
+
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <limits.h>
+#include <sys/utsname.h>
+
+/* number of system calls */
+#define SH_MAXCALLS 512
+
+#include "kern_head.h"
+
+static int verbose = 0;
+
+typedef struct _smap_entry {
+#ifdef SH_SYSCALL_CODE
+  unsigned int  code[2];  /* 8 bytes */
+#endif
+  unsigned long addr;
+  char          name[64];
+} smap_entry;
+
+union {
+  unsigned long addr_sys_call_table;
+  unsigned char str_sys_call_table[sizeof(unsigned long)];
+} sh_sys_call;
+
+#define SYS_CODE_SIZE 1024
+
+static unsigned long addr_system_call;
+static unsigned char system_call_code[SYS_CODE_SIZE];
+
+int read_kcode (unsigned long addr, unsigned char * buf, int len)
+{
+  int fd;
+
+  if (addr == 0UL)
+    {
+      perror("read_kcode: invalid input");
+      return -1;
+    }
+
+  fd = open ("/dev/kmem", O_RDONLY);
+  if (fd < 0)
+    {
+      perror("read_kcode: open");
+      return -1;
+    }
+  if (lseek(fd, addr, SEEK_SET) == (off_t) (-1))
+    {
+      perror("read_kcode: lseek");
+      return -1;
+    }
+  if (read(fd, buf, len) < 0)
+    {
+      perror("read_kcode: read");
+      return -1;
+    }
+  return 0;
+}
+
+int get_dispatch (int * qq)
+{
+  int i;
+
+  if (addr_system_call == 0L || sh_sys_call.addr_sys_call_table == 0L)
+    {
+      fprintf(stderr, "get_dispatch: invalid data\n");
+      return -1;
+    }
+
+  if (0 != read_kcode (addr_system_call, system_call_code, SYS_CODE_SIZE))
+    {
+      fprintf(stderr, "get_dispatch: could not read system_call code\n");
+      return -1;
+    }
+
+  for (i = 0; i < (SYS_CODE_SIZE - 4); ++i)
+    {
+      if (system_call_code[i]   == sh_sys_call.str_sys_call_table[0] &&
+	  system_call_code[i+1] == sh_sys_call.str_sys_call_table[1] &&
+	  system_call_code[i+2] == sh_sys_call.str_sys_call_table[2] &&
+	  system_call_code[i+3] == sh_sys_call.str_sys_call_table[3])
+	{
+	  /*
+	    fprintf(stderr, "INFO: get_dispatch: found sys_call_table in "\
+		    "system_call code at %d\n", i);
+	  */
+	  *qq = i;
+	  return 0;
+	}
+    }
+  fprintf(stderr, 
+	  "get_dispatch: did not find sys_call_table in system_call code\n");
+  fprintf(stderr, 
+	  "** This indicates that either your System.map does not match\n");
+  fprintf(stderr,
+	  "** the currently running kernel, or that your System.map does\n");
+  fprintf(stderr,
+	  "** not provide the required information, and thus use of\n");
+  fprintf(stderr,
+	  "** the --with-kcheck option is not possible\n");
+  return -1;
+}
+
+unsigned long get_symbol_from_systemmap (char * systemmap, 
+                                         char * symbol, char flag)
+{
+  FILE * fp;
+  char buf[512], addr[16], * p;
+  unsigned long retval = 0;
+
+  fp = fopen (systemmap, "r");
+
+  if (!fp)
+    {
+      fprintf(stderr, "error opening <%s>\n", systemmap);
+      perror("get_symbol_from_systemmap: fopen");
+      return -1;
+    }
+  while (fgets(buf, 512, fp) != NULL)
+    {
+      if (buf[9] != flag)
+        continue;
+
+      p = strchr(buf, '\n');
+      if (p != NULL)
+        *p = '\0';
+
+      if (0 != strcmp(&buf[11], symbol))
+        continue;
+
+      addr[0] = '0'; addr[1] = 'x'; addr[2] = '\0';
+      strncat(&addr[2], buf, 8);
+
+      retval = strtoul(addr, NULL, 0);
+      if (retval == ULONG_MAX)
+        {
+          perror("get_symbol_from_systemmap");
+          return -1;
+        }
+    }
+  fclose(fp);
+  return retval;
+}
+
+
+/* returns the number N of entries in syscall table
+ * (0 .. N-1) with valid syscalls
+ */
+int fill_smap(smap_entry * sh_smap, int num)
+{
+  FILE * fp;
+  char buf[512], addr[16], name[128];
+  int  i, j, count = 0, maxcall = 0;
+
+  fp = fopen (SYSTEMMAP, "r");
+
+  if (!fp)
+    {
+      perror("fill_smap: fopen");
+      fprintf(stderr, "fill_smap: error opening <%s>\n", SYSTEMMAP);
+      return -1;
+    }
+
+  /* initialize
+   */
+  sh_sys_call.addr_sys_call_table = 0L;
+
+  while (fgets(buf, 512, fp) != NULL)
+    {
+      
+      if ( ( (buf[9] == 'D') || (buf[9] == 'd') || 
+	     (buf[9] == 'R') || (buf[9] == 'r')) && 
+	   0 == strncmp("sys_call_table", &buf[11], 14))
+	{
+	  printf("/* found sys_call_table */\n");
+	  /* --- copy symbol address ---
+	   */
+	  addr[0] = '0'; addr[1] = 'x'; addr[2] = '\0';
+	  strncat(&addr[2], buf, 8);
+	  addr[10] = '\0';
+	  sh_sys_call.addr_sys_call_table = strtoul(addr, NULL, 0);
+	  if (sh_sys_call.addr_sys_call_table == ULONG_MAX)
+	    {
+	      perror("fill_smap");
+	      return -1;
+	    }
+	  else
+	    {
+	      printf("#define SH_SYS_CALL_TABLE %s\n", addr);
+	    }
+	}
+
+      if (buf[9] != 'T')
+	continue;
+
+      if (0 == strncmp("system_call", &buf[11], 11))
+	{
+	  printf("/* found system_call */\n");
+	  /* --- copy symbol address ---
+	   */
+	  addr[0] = '0'; addr[1] = 'x'; addr[2] = '\0';
+	  strncat(&addr[2], buf, 8);
+	  addr[10] = '\0';
+	  addr_system_call = strtoul(addr, NULL, 0);
+	  if (addr_system_call == ULONG_MAX)
+	    {
+	      perror("fill_smap");
+	      return -1;
+	    }
+	}
+
+
+      if ( (buf[11]!='s' || buf[12]!='y' || buf[13]!='s' || buf[14]!='_') &&
+	   (buf[11]!='o' || buf[12]!='l' || buf[13]!='d' || buf[14]!='_'))
+	continue;
+
+      for (i = 0; i < num; ++i)
+	{
+	  for (j = 0; j < 128; ++j)
+	    {
+	      if (buf[11+j] == '\n' || buf[11+j] == '\0')
+		{
+		  name[j] = '\0';
+		  break;
+		}
+	      name[j] = buf[11+j];
+	    }
+
+
+	  if (0 == strcmp(name, sh_smap[i].name)) 
+	    {
+	      
+	      /* --- copy symbol address ---
+	       */
+	      addr[0] = '0'; addr[1] = 'x'; addr[2] = '\0';
+	      strncat(&addr[2], buf, 8);
+	      addr[10] = '\0';
+	      sh_smap[i].addr = strtoul(addr, NULL, 0);
+	      if (sh_smap[i].addr == ULONG_MAX)
+		{
+		  perror("fill_smap");
+		  return -1;
+		}
+	      ++count;
+	      if (i > maxcall) maxcall = i;
+	      /* printf("maxcall = %d\n", maxcall); */
+	      /* break; */
+      	    }
+	}
+    }
+  fclose(fp);
+  if ((count > 0) && (maxcall > 0))
+    return maxcall+1;
+  else
+    return count;
+}
+
+
+struct inode_operations {
+  int (*create) (int *,int *,int);
+  int * (*lookup) (int *,int *);
+  int (*link) (int *,int *,int *);
+  int (*unlink) (int *,int *);
+  int (*symlink) (int *,int *,const char *);
+  int (*mkdir) (int *,int *,int);
+  int (*rmdir) (int *,int *);
+  int (*mknod) (int *,int *,int,int);
+  int (*rename) (int *, int *,
+                 int *, int *);
+  int (*readlink) (int *, char *,int);
+  int (*follow_link) (int *, int *);
+  void (*truncate) (int *);
+  int (*permission) (int *, int);
+  int (*revalidate) (int *);
+  /*
+    int (*setattr) (int *, int *);
+    int (*getattr) (int *, int *);
+    int (*setxattr) (int *, const char *, void *, size_t, int);
+    ssize_t (*getxattr) (int *, const char *, void *, size_t);
+    ssize_t (*listxattr) (int *, char *, size_t);
+    int (*removexattr) (int *, const char *);
+  */
+};
+
+struct file_operations {
+  int (*create) (int *,int *,int);
+};
+
+struct proc_dir_entry {
+  unsigned short low_ino;
+  unsigned short namelen;
+  const char * name;
+  mode_t mode;
+  nlink_t nlink;
+  uid_t uid;
+  gid_t gid;
+  unsigned long size; 
+  struct inode_operations * proc_iops;
+  struct file_operations * proc_fops;
+  /*
+  get_info_t *get_info;
+  struct module *owner;
+  struct proc_dir_entry *next, *parent, *subdir;
+  void *data;
+  read_proc_t *read_proc;
+  write_proc_t *write_proc;
+  atomic_t count;         
+  int deleted;  
+  */          
+};
+
+
+
+int main(int argc, char * argv[])
+{
+  int i, count, maxcall, qq;
+  int which = 4;
+  smap_entry sh_smap[SH_MAXCALLS];
+  struct utsname utbuf;
+  char *p = NULL;
+
+  unsigned long proc_root;
+  unsigned long proc_root_iops;
+  unsigned long proc_root_lookup;
+
+  unsigned long addr_ni_syscall = 0;
+
+  if (argc > 1)
+    {
+      if (strcmp(argv[1], "-h") == 0 ||  strcmp(argv[1], "--help") == 0)
+	usage(EXIT_SUCCESS);
+      else if (strcmp(argv[1], "-v") == 0 ||
+	       strcmp(argv[1], "--verbose") == 0)
+	verbose = 1;
+    }
+
+  if (0 != uname(&utbuf))
+    {
+      perror("kern_head: uname");
+      exit (EXIT_FAILURE);
+    }
+
+  if (strncmp(utbuf.release, SH_KERNEL_VERSION, 3) != 0)
+    {
+      fprintf(stderr, "kern_head: current kernel version %s does not match\n",
+	      utbuf.release);
+      fprintf(stderr, "kern_head: %s from config.h\n", SH_KERNEL_VERSION);
+      fprintf(stderr, "kern_head: continuing with %s\n", SH_KERNEL_VERSION);
+
+      p = SH_KERNEL_VERSION;
+    } else {
+      p = utbuf.release;
+    }
+
+  if      (strncmp(p, "2.2", 3) == 0)
+    which = 2;
+  else if (strncmp(p, "2.4", 3) == 0)
+    which = 4;
+  else if (strncmp(p, "2.6", 3) == 0)
+    which = 6;
+  else
+    {
+      fprintf(stderr, "kern_head: kernel %s not supported\n", p);
+      exit (EXIT_FAILURE);
+    }
+
+  
+  if (utbuf.machine[0] != 'i' || utbuf.machine[2] != '8' || 
+      utbuf.machine[3] != '6')
+    {
+      fprintf(stderr, "kern_head: machine %s not supported\n", utbuf.machine);
+      exit (EXIT_FAILURE);
+    }
+
+  if (0 != getuid())
+    {
+      fprintf(stderr, "\n");
+      
+      fprintf(stderr, "NOTE:  kern_head: must run as 'root' (need to read from /dev/kmem)\n");
+      fprintf(stderr, "       If you get this message, then proceed as follows:\n");
+      fprintf(stderr, "       $ su\n");
+      fprintf(stderr, "       $ ./kern_head > sh_ks.h\n");
+      fprintf(stderr, "       $ exit\n");
+      fprintf(stderr, "       $ make\n\n");
+      exit (EXIT_FAILURE);
+    }
+      
+  printf("#ifndef SH_KERN_CALLS_H\n");
+  printf("#define SH_KERN_CALLS_H\n\n");
+
+  printf("\n/* Kernel %s, machine %s -- use table %s */\n\n", 
+	 p, utbuf.machine,
+	 (which == 2) ? "callz_2p2" : "callz_2p4");
+      
+
+  /* initiate the system call table 
+   */
+  for (i = 0; i < SH_MAXCALLS; ++i)
+    {
+      if (which == 2)
+	{
+	  if (callz_2p2[i] == NULL)
+	    break;
+	  strcpy(sh_smap[i].name, callz_2p2[i]);
+	}
+      else
+	{
+	  if (callz_2p4[i] == NULL)
+	    break;
+	  strcpy(sh_smap[i].name, callz_2p4[i]);
+	}
+      sh_smap[i].addr    = 0UL;
+    }
+
+  if (which == 6) /* fix syscall map for 2.6 */
+    {
+      strcpy(sh_smap[0].name,   "sys_restart_syscall");
+      strcpy(sh_smap[180].name, "sys_pread64");
+      strcpy(sh_smap[181].name, "sys_pwrite64");
+    }
+  count = i;
+
+  /* get the actual number of the highest syscalls and use no more.
+   * get sys_call_table and system_call
+   */
+  maxcall = fill_smap(sh_smap, count);
+  if ( maxcall < 0)
+    {
+      printf("#endif\n");
+      fprintf(stderr, "kern_head: fill_smap failed\n");
+      exit (EXIT_FAILURE);
+    }
+  if (addr_system_call == 0L) 
+    {
+      printf("#endif\n");
+      fprintf(stderr, 
+	      "kern_head: address of system_call not found in System.map\n");
+      fprintf(stderr, 
+	      "** This indicates that your System.map does not provide\n");
+      fprintf(stderr, 
+	      "** the required information, and thus use of the\n");
+      fprintf(stderr, 
+	      "** --with-kcheck option is not possible\n");
+      exit (EXIT_FAILURE);
+    }
+
+  for (i = 0; i < maxcall; ++i)
+    {
+      if (0 == strcmp(sh_smap[i].name, "sys_ni_syscall"))
+        {
+          addr_ni_syscall = sh_smap[i].addr;
+          break;
+        }
+    }
+  if (which < 6)
+    {
+      maxcall = (maxcall > 256) ? 256 : maxcall;
+    }
+
+  for (i = 0; i < maxcall; ++i)
+    {
+      if (sh_smap[i].addr == 0UL)
+        {
+          if (verbose > 0)
+            fprintf(stderr, "** unknown syscall **: [%s]\n", sh_smap[i].name);
+          strcpy(sh_smap[i].name, "sys_ni_syscall");
+          sh_smap[i].addr = addr_ni_syscall;
+        }
+    }
+
+
+  /* get the location of the syscall table address within system_call
+   */
+  if ( get_dispatch (&qq) < 0)
+    {
+      printf("#endif\n");
+      fprintf(stderr, "kern_head: get_dispatch failed\n");
+      exit (EXIT_FAILURE);
+    }
+
+  if (qq <= 252)
+    printf("#define SYS_CALL_LOC  %d\n", qq);
+  else
+    {
+      printf("#endif\n");
+      fprintf(stderr, "kern_head: SYS_CALL_LOC (%d) too large\n", qq);
+      exit(EXIT_FAILURE);
+    }
+  printf("#define SH_SYS_CALL_ADDR %#lx\n\n", addr_system_call);
+
+  printf("static unsigned char system_call_code[256] = { 0 };\n");
+
+  printf("#define SH_MAXCALLS %d\n\n", maxcall);
+
+#ifdef SH_IDT_TABLE
+  printf("static unsigned char idt_table[2048] = { 0 };\n");
+#endif
+
+  printf("typedef struct _sh_syscall_t {\n");
+#ifdef SH_SYSCALL_CODE
+  printf("  unsigned int  code[2];  /* 8 bytes */\n");
+#endif
+  printf("  unsigned long addr;\n");
+  printf("  char *        name;\n");
+  printf("} sh_syscall_t;\n\n");
+
+  printf("static sh_syscall_t sh_syscalls[] = {\n");
+
+  for (i = 0; i < maxcall; ++i) 
+    {
+#ifdef SH_SYSCALL_CODE
+      printf(" /* %03d */   { { 0, 0 }, 0, N_(%c%s%c) },\n", 
+	     i, '"', sh_smap[i].name, '"');
+#else
+      printf(" /* %03d */   { 0, N_(%c%s%c) },\n", 
+	     i, '"', sh_smap[i].name, '"');
+#endif
+    }
+#ifdef SH_SYSCALL_CODE
+  printf(" /* eof */   { { 0x00000000, 0x00000000 }, 0x00000000,  NULL }\n");
+#else
+  printf(" /* eof */   { 0x00000000,  NULL }\n");
+#endif
+  printf("};\n\n");
+
+
+  /* get proc addresses
+   */
+  proc_root =  get_symbol_from_systemmap (SYSTEMMAP, 
+                                          "proc_root", 'D');
+  if (proc_root == 0) 
+    {
+      proc_root =  get_symbol_from_systemmap (SYSTEMMAP, 
+                                              "proc_root", 'd');
+    }
+  if (proc_root == 0) 
+    {
+      proc_root =  get_symbol_from_systemmap (SYSTEMMAP, 
+                                              "proc_root", 'R');
+    }
+  if (proc_root != 0) {
+    printf("#define PROC_ROOT_LOC %#lx\n\n", proc_root);
+  }
+
+  proc_root_lookup =  get_symbol_from_systemmap (SYSTEMMAP, 
+                                                 "proc_root_lookup", 't');
+  if (proc_root_lookup == 0) 
+    {
+      proc_root_lookup =  get_symbol_from_systemmap (SYSTEMMAP, 
+                                                     "proc_root_lookup", 'T');
+    }
+  if (proc_root_lookup != 0) {
+    printf("#define PROC_ROOT_LOOKUP_LOC %#lx\n\n", proc_root_lookup);
+  }
+
+  proc_root_iops =  get_symbol_from_systemmap (SYSTEMMAP, 
+                                               "proc_root_inode_operations", 
+                                               'd');
+  if (proc_root_iops == 0) 
+    {
+      proc_root_iops = get_symbol_from_systemmap (SYSTEMMAP, 
+                                                  "proc_root_inode_operations",
+                                                  'D');
+    }
+  if (proc_root_iops == 0) 
+    {
+      proc_root_iops = get_symbol_from_systemmap (SYSTEMMAP, 
+                                                  "proc_root_inode_operations",
+                                                  'R');
+    }
+  if (proc_root_iops != 0) {
+    printf("#define PROC_ROOT_IOPS_LOC %#lx\n\n", proc_root_iops);
+  }
+
+  printf("#endif\n");
+
+  exit (EXIT_SUCCESS);
+}
+
+/* if defined(HOST_IS_LINUX) */
+#endif
+
+/************************************************************
+ *
+ *
+ *  FreeBSD Implementation
+ *
+ ************************************************************/
+
+#if defined(HOST_IS_FREEBSD) || defined(__OpenBSD__)
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <err.h>
+#include <kvm.h>
+#include <fcntl.h>
+#include <nlist.h>
+#include <limits.h>
+#include <sys/types.h>
+#include <sys/utsname.h>
+
+#ifdef __FreeBSD__
+#include <sys/sysent.h>
+#endif
+
+#include <sys/syscall.h>
+
+#ifndef  SYS_MAXSYSCALL
+#define  SYS_MAXSYSCALL	512
+#endif
+
+/* number of system calls */
+#define SH_MAXCALLS 512
+#include "kern_head.h"
+static int verbose = 0;
+
+#ifdef __OpenBSD__
+struct proc;
+struct sysent {
+	short sy_narg;
+	short sy_argsize;
+	int   (*sy_call)(struct proc *, void *, register_t *);
+};
+#endif
+
+typedef struct _smap_entry {
+  unsigned int  code[2];  /* 8 bytes */
+  unsigned long addr;
+  char          name[64];
+} smap_entry;
+
+union {
+  unsigned long addr_sys_call_table;
+  unsigned char str_sys_call_table[sizeof(unsigned long)];
+} sh_sys_call;
+
+struct nlist sys_list[SYS_MAXSYSCALL+1];
+
+struct  nlist   list[2];
+
+
+int main(int argc, char * argv[])
+{
+  int i, count, which;
+  smap_entry sh_smap[SYS_MAXSYSCALL];
+  struct utsname utbuf;
+  char errbuf[_POSIX2_LINE_MAX];
+
+  struct sysent  sy;
+  unsigned long offset = 0L;
+  kvm_t *kd;
+
+  list[0].n_name = "_sysent";
+  list[1].n_name = NULL;
+
+  if (argc > 1)
+    {
+      if (strcmp(argv[1], "-h") == 0 ||  strcmp(argv[1], "--help") == 0)
+	usage(EXIT_SUCCESS);
+      else if (strcmp(argv[1], "-v") == 0 ||
+	       strcmp(argv[1], "--verbose") == 0)
+	verbose = 1;
+    }
+
+  if (0 != uname(&utbuf))
+    {
+      perror("kern_head: uname");
+      exit (EXIT_FAILURE);
+    }
+
+  if      (utbuf.release[0] == '4')
+    which = 4;
+  else if (utbuf.release[0] == '5')
+    which = 5;
+  else
+    {
+      fprintf(stderr, "kern_head: kernel %s not supported\n", utbuf.release);
+      exit (EXIT_FAILURE);
+    }
+
+  if (utbuf.machine[0] != 'i' || utbuf.machine[2] != '8' || 
+      utbuf.machine[3] != '6')
+    {
+      fprintf(stderr, "kern_head: machine %s not supported\n", utbuf.machine);
+      exit (EXIT_FAILURE);
+    }
+
+  if (0 != getuid())
+    {
+      fprintf(stderr, "\n");
+      fprintf(stderr, "NOTE:  kern_head: must run as 'root' ");
+      fprintf(stderr, "(need to read from /dev/kmem)\n");
+      fprintf(stderr, "       If you get this message, then proceed ");
+      fprintf(stderr, "as follows:\n");
+      fprintf(stderr, "       $ su\n");
+      fprintf(stderr, "       $ ./kern_head > sh_ks.h\n");
+      fprintf(stderr, "       $ exit\n");
+      fprintf(stderr, "       $ make\n\n");
+      exit (EXIT_FAILURE);
+    }
+
+  kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
+  if (!kd)
+    {
+      fprintf(stderr, "check_sysent: kvm_openfiles: %s\n", errbuf);
+      exit(EXIT_FAILURE);
+    }
+
+  i = kvm_nlist(kd, list);
+  if (i == -1)
+    {
+      fprintf(stderr, "check_sysent: kvm_nlist: %s\n", kvm_geterr(kd));
+      exit(EXIT_FAILURE);
+    }
+
+  if (which == 4)
+    printf("\n/* Kernel %s, machine %s -- use table %s */\n\n", 
+        	 utbuf.release, utbuf.machine, "callz_fbsd");
+  if (which == 5)
+    printf("\n/* Kernel %s, machine %s -- use table %s */\n\n",
+                 utbuf.release, utbuf.machine, "callz_fbsd5");
+      
+  i = 0;
+  if (which == 4) {
+    while ((callz_fbsd[i] != NULL) && (i < SYS_MAXSYSCALL))
+      {
+	sys_list[i].n_name = callz_fbsd[i];
+	/* fprintf(stderr, "sys_list[%d] = %s\n", i, sys_list[i].n_name); */
+	++i;
+      }
+    if ((utbuf.release[1] == '.') && (utbuf.release[2] == '1') && 
+	(utbuf.release[3] == '0'))
+      {
+	sys_list[336].n_name = callz_fbsd[290]; /* sendfile -> nosys */
+      }
+  } else {
+    while ((callz_fbsd5[i] != NULL) && (i < SYS_MAXSYSCALL))
+      {
+	sys_list[i].n_name = callz_fbsd5[i];
+	/* fprintf(stderr, "sys_list[%d] = %s\n", i, sys_list[i].n_name); */
+	++i;
+      }
+  }
+  count = i;
+  sys_list[i].n_name = NULL;
+   
+  i = kvm_nlist(kd, sys_list);
+  if (i == -1)
+    {
+      fprintf(stderr, "check_sysent: kvm_nlist: %s\n", kvm_geterr(kd));
+      /* exit(EXIT_FAILURE); */
+    }
+  else if (i != 0 && verbose != 0)
+     {
+	fprintf(stderr, "check_sysent: kvm_nlist: %d out of %d invalid.\n",
+		i, count);
+	fprintf(stderr, "              Probably callz_fbsd in kern_head.c\n");
+	fprintf(stderr, "              is not for your kernel version.\n");
+	fprintf(stderr, "              (No reason to worry, kcheck will "\
+                                       "work anyway)\n\n");
+     }
+
+  for (i = 0; i < count /* SYS_MAXSYSCALL */; i++) 
+    {
+       if (NULL == sys_list[i].n_name)
+	 break;
+       if (!sys_list[i].n_value && 0 != strcmp(sys_list[i].n_name, "_nosys")
+	   && verbose != 0)
+	{
+	  fprintf(stderr,"check_sysent: not found: slot %03d [%s]\n", 
+		  i, sys_list[i].n_name);
+	  /* exit(EXIT_FAILURE); */
+	}
+      offset = list[0].n_value + (i*sizeof(struct sysent));
+      if (kvm_read(kd, offset, &sy, sizeof(struct sysent)) < 0)
+	{
+	  fprintf(stderr,"check_sysent: kvm_read: %s\n", kvm_geterr(kd));
+	  exit(EXIT_FAILURE);
+	}
+
+      if (verbose > 0)
+	fprintf(stderr, "(kvm_nlist) %#lx   %#lx (sysent[%03d])  %03d [%s]\n",
+		(unsigned long) sys_list[i].n_value,
+		(unsigned long) sy.sy_call,
+		i, i, sys_list[i].n_name);
+
+      if((unsigned long)sy.sy_call != sys_list[i].n_value && 
+	 sys_list[i].n_value != 0 &&
+	 0 != strcmp(sys_list[i].n_name, "_nosys") &&
+	 (unsigned long)sy.sy_call != sys_list[290].n_value)  
+	{
+          fprintf(stderr,
+                  "WARNING: (kvm_nlist) %#lx != %#lx (sysent[%03d])  %03d [%s]\n",
+		  (unsigned long) sys_list[i].n_value,
+		  (unsigned long) sy.sy_call,
+		  i, i, sys_list[i].n_name);
+	}
+      sh_smap[i].addr = (unsigned long) sy.sy_call;
+      strncpy(sh_smap[i].name, sys_list[i].n_name, 64);
+      if(kvm_read(kd, (unsigned int) sy.sy_call, &(sh_smap[i].code[0]), 
+		  2 * sizeof(unsigned int)) < 0)
+	{
+	  fprintf(stderr,"check_sysent: kvm_read: %s\n", kvm_geterr(kd));
+	  exit(EXIT_FAILURE);
+	}
+    }
+
+  if(kvm_close(kd) < 0) 
+    {
+      fprintf(stderr,"check_sysent: kvm_nlist: %s\n", kvm_geterr(kd));
+      exit(EXIT_FAILURE);
+    }
+ 
+  printf("#ifndef SH_KERN_CALLS_H\n");
+  printf("#define SH_KERN_CALLS_H\n\n");
+
+  printf("#define SH_MAXCALLS %d\n\n", count);
+
+  printf("typedef struct _sh_syscall_t {\n");
+  printf("  unsigned int  code[2];  /* 8 bytes */\n");
+  printf("  unsigned long addr;\n");
+  printf("  char *        name;\n");
+  printf("} sh_syscall_t;\n\n");
+
+  printf("static sh_syscall_t sh_syscalls[] = {\n");
+  for (i = 0; i < count; ++i) {
+    printf(" /* %03d */ {{ 0x%-8.8x, 0x%-8.8x }, 0x%-8.8lx, N_(%c%s%c) },\n", 
+	   i, sh_smap[i].code[0], sh_smap[i].code[1], 
+	   sh_smap[i].addr, '"', sh_smap[i].name, '"');
+  }
+  printf(" /* eof */   { { 0x00000000, 0x00000000 }, 0x00000000,  NULL }\n");
+  printf("};\n\n");
+  printf("#endif\n");
+  return 0;
+}
+/* if defined(HOST_IS_FREEBSD) */
+#endif
+
+/* #if defined(SH_USE_KERN) */
+#else
+
+#include <stdio.h>
+#include <stdlib.h>
+
+int main()
+{
+  printf("#ifndef SH_KERN_CALLS_H\n");
+  printf("#define SH_KERN_CALLS_H\n\n");
+
+  printf("/* Dummy header. */\n\n");
+
+  printf("typedef struct _sh_syscall_t {\n");
+  printf("  unsigned long addr;\n");
+  printf("  char *        name;\n");
+  printf("} sh_syscall_t;\n\n");
+
+  printf("#endif\n");
+
+  return (EXIT_SUCCESS);
+}
+
+/* #ifdef SH_USE_KERN */
+#endif
Index: trunk/src/make-tests.sh
===================================================================
--- trunk/src/make-tests.sh	(revision 591)
+++ 	(revision )
@@ -1,75 +1,0 @@
-#!/bin/sh
-
-# Auto generate single AllTests file for CuTest.
-# Searches through all *.c files in the current directory.
-# Prints to stdout.
-# Author: Asim Jalis
-# Date: 01/08/2003
-
-# Modified to return non-zero if any test has failed
-# Rainer Wichmann, 29. Jan 2006
-# ...and to print to stderr if any test has failed
-# Rainer Wichmann, 31. Jan 2006
-
-if test $# -eq 0 ; then FILES=*.c ; else FILES=$* ; fi
-
-echo '
-
-/* This is auto-generated code. Edit at your own peril. */
-
-#include "config.h"
-#include <stdio.h>
-#include "CuTest.h"
-'
-
-cat $FILES | grep '^void Test' | 
-    sed -e 's/(.*$//' \
-        -e 's/$/(CuTest*);/' \
-        -e 's/^/extern /'
-
-echo \
-'
-
-int RunAllTests(void) 
-{
-    CuString *output = CuStringNew();
-    CuSuite* suite = CuSuiteNew();
-
-'
-cat $FILES | grep '^void Test' | 
-    sed -e 's/^void //' \
-        -e 's/(.*$//' \
-        -e 's/^/    SUITE_ADD_TEST(suite, /' \
-        -e 's/$/);/'
-
-echo \
-'
-    CuSuiteRun(suite);
-    CuSuiteSummary(suite, output);
-    CuSuiteDetails(suite, output);
-    if (suite->failCount > 0)
-      fprintf(stderr, "%s%c", output->buffer, 0x0A);
-    else
-      fprintf(stdout, "%s%c", output->buffer, 0x0A);
-    return suite->failCount;
-}
-
-int main(void)
-{
-#if !defined(USE_SYSTEM_MALLOC)
-    typedef void assert_handler_tp(const char * error, const char *file, int line);
-    extern assert_handler_tp *dnmalloc_set_handler(assert_handler_tp *new);
-    extern void safe_fatal  (const char * details, const char *f, int l);
-#endif
-#if !defined(USE_SYSTEM_MALLOC) && defined(USE_MALLOC_LOCK)
-    extern int dnmalloc_pthread_init(void);
-    dnmalloc_pthread_init();
-#endif
-#if !defined(USE_SYSTEM_MALLOC)
-    (void) dnmalloc_set_handler(safe_fatal);
-#endif
-    int retval;
-    retval = RunAllTests();
-    return (retval == 0) ? 0 : 1;
-}
-'
Index: trunk/src/minilzo.c
===================================================================
--- trunk/src/minilzo.c	(revision 591)
+++ trunk/src/minilzo.c	(revision 1)
@@ -82,8 +82,12 @@
 #else
 #  include <sys/types.h>
-#  if defined(__STDC__)
+#  if defined(STDC_HEADERS)
 #    include <string.h>
 #    include <stdlib.h>
+#  endif
+#  if defined(HAVE_STDDEF_H)
 #    include <stddef.h>
+#  endif
+#  if defined(HAVE_MEMORY_H)
 #    include <memory.h>
 #  endif
@@ -2395,6 +2399,6 @@
 #  endif
 #  if (LZO_TEST_DECOMPRESS_OVERRUN_INPUT >= 2)
-#    define NEED_IP(x, y) \
-    if ( ((y > 0) && (x > (LZO_UINT_MAX - y))) || ((lzo_uint)(ip_end - ip) < (lzo_uint)((x)+(y))) )  goto input_overrun
+#    define NEED_IP(x) \
+	    if ((lzo_uint)(ip_end - ip) < (lzo_uint)(x))  goto input_overrun
 #  endif
 #endif
@@ -2406,6 +2410,6 @@
 #  if (LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT >= 2)
 #    undef TEST_OP
-#    define NEED_OP(x,y) \
-    if (  ((y > 0) && (x > (LZO_UINT_MAX - y))) || ((lzo_uint)(op_end - op) < (lzo_uint)((x)+(y))) )  goto output_overrun
+#    define NEED_OP(x) \
+	    if ((lzo_uint)(op_end - op) < (lzo_uint)(x))  goto output_overrun
 #  endif
 #endif
@@ -2435,12 +2439,10 @@
 #  define HAVE_NEED_IP
 #else
-    /* #  define NEED_IP(x)            ((void) 0) */
-#error "no need_ip"
+#  define NEED_IP(x)            ((void) 0)
 #endif
 #if defined(NEED_OP)
 #  define HAVE_NEED_OP
 #else
-    /* #  define NEED_OP(x)            ((void) 0) */
-#error "no need_op"
+#  define NEED_OP(x)            ((void) 0)
 #endif
 
@@ -2511,5 +2513,5 @@
 	if (t < 4)
 	    goto match_next;
-	assert(t > 0); NEED_OP(t, 0); NEED_IP(t, 1);
+	assert(t > 0); NEED_OP(t); NEED_IP(t+1);
 	do *op++ = *ip++; while (--t > 0);
 	goto first_literal_run;
@@ -2523,14 +2525,14 @@
 	if (t == 0)
 	{
-	  NEED_IP(1, 0);
+	    NEED_IP(1);
 	    while (*ip == 0)
 	    {
 		t += 255;
 		ip++;
-		NEED_IP(1, 0);
+		NEED_IP(1);
 	    }
 	    t += 15 + *ip++;
 	}
-	assert(t > 0); NEED_OP(t,3); NEED_IP(t,4);
+	assert(t > 0); NEED_OP(t+3); NEED_IP(t+4);
 #if defined(LZO_UNALIGNED_OK_4) || defined(LZO_ALIGNED_OK_4)
 #if !defined(LZO_UNALIGNED_OK_4)
@@ -2589,5 +2591,5 @@
 	m_pos -= *ip++ << 2;
 #endif
-	TEST_LOOKBEHIND(m_pos,out); NEED_OP(3,0);
+	TEST_LOOKBEHIND(m_pos,out); NEED_OP(3);
 	*op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos;
 #endif
@@ -2646,5 +2648,5 @@
 		t = (t >> 5) - 1;
 #endif
-		TEST_LOOKBEHIND(m_pos,out); assert(t > 0); NEED_OP(t,(3-1));
+		TEST_LOOKBEHIND(m_pos,out); assert(t > 0); NEED_OP(t+3-1);
 		goto copy_match;
 #endif
@@ -2655,10 +2657,10 @@
 		if (t == 0)
 		{
-		  NEED_IP(1,0);
+		    NEED_IP(1);
 		    while (*ip == 0)
 		    {
 			t += 255;
 			ip++;
-			NEED_IP(1,0);
+			NEED_IP(1);
 		    }
 		    t += 31 + *ip++;
@@ -2699,10 +2701,10 @@
 		if (t == 0)
 		{
-		  NEED_IP(1,0);
+		    NEED_IP(1);
 		    while (*ip == 0)
 		    {
 			t += 255;
 			ip++;
-			NEED_IP(1,0);
+			NEED_IP(1);
 		    }
 		    t += 7 + *ip++;
@@ -2747,5 +2749,5 @@
 		m_off = 1 + (t >> 2) + (*ip++ << 2);
 #endif
-		NEED_OP(2,0);
+		NEED_OP(2);
 		t = 2; COPY_DICT(t,m_off)
 #else
@@ -2759,5 +2761,5 @@
 		m_pos -= *ip++ << 2;
 #endif
-		TEST_LOOKBEHIND(m_pos,out); NEED_OP(2,0);
+		TEST_LOOKBEHIND(m_pos,out); NEED_OP(2);
 		*op++ = *m_pos++; *op++ = *m_pos;
 #endif
@@ -2767,10 +2769,10 @@
 #if defined(COPY_DICT)
 
-	    NEED_OP(t,(3-1));
+	    NEED_OP(t+3-1);
 	    t += 3-1; COPY_DICT(t,m_off)
 
 #else
 
-	    TEST_LOOKBEHIND(m_pos,out); assert(t > 0); NEED_OP(t,(3-1));
+	    TEST_LOOKBEHIND(m_pos,out); assert(t > 0); NEED_OP(t+3-1);
 #if defined(LZO_UNALIGNED_OK_4) || defined(LZO_ALIGNED_OK_4)
 #if !defined(LZO_UNALIGNED_OK_4)
@@ -2810,5 +2812,5 @@
 
 match_next:
-	    assert(t > 0); NEED_OP(t,0); NEED_IP(t,1);
+	    assert(t > 0); NEED_OP(t); NEED_IP(t+1);
 	    do *op++ = *ip++; while (--t > 0);
 	    t = *ip++;
Index: trunk/src/rijndael-alg-fst.c
===================================================================
--- trunk/src/rijndael-alg-fst.c	(revision 591)
+++ trunk/src/rijndael-alg-fst.c	(revision 1)
@@ -1,1235 +1,280 @@
-/*	$NetBSD: rijndael-alg-fst.c,v 1.7 2005/12/11 12:20:52 christos Exp $	*/
-/*	$KAME: rijndael-alg-fst.c,v 1.10 2003/07/15 10:47:16 itojun Exp $	*/
+/*
+ * rijndael-alg-fst.c   v2.3   April '2000
+ *
+ * Optimised ANSI C code
+ *
+ * authors: v1.0: Antoon Bosselaers
+ *          v2.0: Vincent Rijmen
+ *          v2.3: Paulo Barreto
+ *
+ * This code is placed in the public domain.
+ */
+
+#include "config_xor.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#ifdef SH_ENCRYPT
+
+#include "rijndael-alg-fst.h"
+
+#include "rijndael-boxes-fst.h"
+
+int rijndaelKeySched(word8 k[MAXKC][4], word8 W[MAXROUNDS+1][4][4], int ROUNDS) {
+	/* Calculate the necessary round keys
+	 * The number of calculations depends on keyBits and blockBits
+	 */ 
+	int j, r, t, rconpointer = 0;
+	word8 tk[MAXKC][4];
+	int KC = ROUNDS - 6;
+
+	for (j = KC-1; j >= 0; j--) {
+		*((word32*)tk[j]) = *((word32*)k[j]);
+	}
+	r = 0;
+	t = 0;
+	/* copy values into round key array */
+	for (j = 0; (j < KC) && (r < ROUNDS + 1); ) {
+		for (; (j < KC) && (t < 4); j++, t++) {
+			*((word32*)W[r][t]) = *((word32*)tk[j]);
+		}
+		if (t == 4) {
+			r++;
+			t = 0;
+		}
+	}
+		
+	while (r < ROUNDS + 1) { /* while not enough round key material calculated */
+		/* calculate new values */
+		tk[0][0] ^= S[tk[KC-1][1]];
+		tk[0][1] ^= S[tk[KC-1][2]];
+		tk[0][2] ^= S[tk[KC-1][3]];
+		tk[0][3] ^= S[tk[KC-1][0]];
+		tk[0][0] ^= rcon[rconpointer++];
+
+		if (KC != 8) {
+			for (j = 1; j < KC; j++) {
+				*((word32*)tk[j]) ^= *((word32*)tk[j-1]);
+			}
+		} else {
+			for (j = 1; j < KC/2; j++) {
+				*((word32*)tk[j]) ^= *((word32*)tk[j-1]);
+			}
+			tk[KC/2][0] ^= S[tk[KC/2 - 1][0]];
+			tk[KC/2][1] ^= S[tk[KC/2 - 1][1]];
+			tk[KC/2][2] ^= S[tk[KC/2 - 1][2]];
+			tk[KC/2][3] ^= S[tk[KC/2 - 1][3]];
+			for (j = KC/2 + 1; j < KC; j++) {
+				*((word32*)tk[j]) ^= *((word32*)tk[j-1]);
+			}
+		}
+		/* copy values into round key array */
+		for (j = 0; (j < KC) && (r < ROUNDS + 1); ) {
+			for (; (j < KC) && (t < 4); j++, t++) {
+				*((word32*)W[r][t]) = *((word32*)tk[j]);
+			}
+			if (t == 4) {
+				r++;
+				t = 0;
+			}
+		}
+	}		
+	return 0;
+}
+
+int rijndaelKeyEncToDec(word8 W[MAXROUNDS+1][4][4], int ROUNDS) {
+	int r;
+	word8 *w;
+
+	for (r = 1; r < ROUNDS; r++) {
+		w = W[r][0];
+		*((word32*)w) =
+			  *((word32*)U1[w[0]])
+			^ *((word32*)U2[w[1]])
+			^ *((word32*)U3[w[2]])
+			^ *((word32*)U4[w[3]]);
+
+		w = W[r][1];
+		*((word32*)w) =
+			  *((word32*)U1[w[0]])
+			^ *((word32*)U2[w[1]])
+			^ *((word32*)U3[w[2]])
+			^ *((word32*)U4[w[3]]);
+
+		w = W[r][2];
+		*((word32*)w) =
+			  *((word32*)U1[w[0]])
+			^ *((word32*)U2[w[1]])
+			^ *((word32*)U3[w[2]])
+			^ *((word32*)U4[w[3]]);
+
+		w = W[r][3];
+		*((word32*)w) =
+			  *((word32*)U1[w[0]])
+			^ *((word32*)U2[w[1]])
+			^ *((word32*)U3[w[2]])
+			^ *((word32*)U4[w[3]]);
+	}
+	return 0;
+}	
+
 /**
- * rijndael-alg-fst.c
- *
- * @version 3.0 (December 2000)
- *
- * Optimised ANSI C code for the Rijndael cipher (now AES)
- *
- * @author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
- * @author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>
- * @author Paulo Barreto <paulo.barreto@terra.com.br>
- *
- * This code is hereby placed in the public domain.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
- * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * Encrypt a single block. 
  */
-
-#include "config_xor.h"
-
-#include <sys/types.h>
-#include <string.h>
-
-#ifdef SH_ENCRYPT
-
-typedef unsigned char u8;
-#if defined(HAVE_INT_32)
-typedef unsigned int u32;
-#elif defined(HAVE_LONG_32)
-typedef unsigned long u32;
-#elif defined(HAVE_SHORT_32)
-typedef unsigned short u32;
-#else
-#error "No 32 bit integer type found"
+int rijndaelEncrypt(word8 a[16], word8 b[16], word8 rk[MAXROUNDS+1][4][4], int ROUNDS) {
+	int r;
+	word8 temp[4][4];
+
+    *((word32*)temp[0]) = *((word32*)(a   )) ^ *((word32*)rk[0][0]);
+    *((word32*)temp[1]) = *((word32*)(a+ 4)) ^ *((word32*)rk[0][1]);
+    *((word32*)temp[2]) = *((word32*)(a+ 8)) ^ *((word32*)rk[0][2]);
+    *((word32*)temp[3]) = *((word32*)(a+12)) ^ *((word32*)rk[0][3]);
+    *((word32*)(b    )) = *((word32*)T1[temp[0][0]])
+						^ *((word32*)T2[temp[1][1]])
+						^ *((word32*)T3[temp[2][2]]) 
+						^ *((word32*)T4[temp[3][3]]);
+    *((word32*)(b + 4)) = *((word32*)T1[temp[1][0]])
+						^ *((word32*)T2[temp[2][1]])
+						^ *((word32*)T3[temp[3][2]]) 
+						^ *((word32*)T4[temp[0][3]]);
+    *((word32*)(b + 8)) = *((word32*)T1[temp[2][0]])
+						^ *((word32*)T2[temp[3][1]])
+						^ *((word32*)T3[temp[0][2]]) 
+						^ *((word32*)T4[temp[1][3]]);
+    *((word32*)(b +12)) = *((word32*)T1[temp[3][0]])
+						^ *((word32*)T2[temp[0][1]])
+						^ *((word32*)T3[temp[1][2]]) 
+						^ *((word32*)T4[temp[2][3]]);
+	for (r = 1; r < ROUNDS-1; r++) {
+		*((word32*)temp[0]) = *((word32*)(b   )) ^ *((word32*)rk[r][0]);
+		*((word32*)temp[1]) = *((word32*)(b+ 4)) ^ *((word32*)rk[r][1]);
+		*((word32*)temp[2]) = *((word32*)(b+ 8)) ^ *((word32*)rk[r][2]);
+		*((word32*)temp[3]) = *((word32*)(b+12)) ^ *((word32*)rk[r][3]);
+
+		*((word32*)(b    )) = *((word32*)T1[temp[0][0]])
+							^ *((word32*)T2[temp[1][1]])
+							^ *((word32*)T3[temp[2][2]]) 
+							^ *((word32*)T4[temp[3][3]]);
+		*((word32*)(b + 4)) = *((word32*)T1[temp[1][0]])
+							^ *((word32*)T2[temp[2][1]])
+							^ *((word32*)T3[temp[3][2]]) 
+							^ *((word32*)T4[temp[0][3]]);
+		*((word32*)(b + 8)) = *((word32*)T1[temp[2][0]])
+							^ *((word32*)T2[temp[3][1]])
+							^ *((word32*)T3[temp[0][2]]) 
+							^ *((word32*)T4[temp[1][3]]);
+		*((word32*)(b +12)) = *((word32*)T1[temp[3][0]])
+							^ *((word32*)T2[temp[0][1]])
+							^ *((word32*)T3[temp[1][2]]) 
+							^ *((word32*)T4[temp[2][3]]);
+	}
+	/* last round is special */   
+	*((word32*)temp[0]) = *((word32*)(b   )) ^ *((word32*)rk[ROUNDS-1][0]);
+	*((word32*)temp[1]) = *((word32*)(b+ 4)) ^ *((word32*)rk[ROUNDS-1][1]);
+	*((word32*)temp[2]) = *((word32*)(b+ 8)) ^ *((word32*)rk[ROUNDS-1][2]);
+	*((word32*)temp[3]) = *((word32*)(b+12)) ^ *((word32*)rk[ROUNDS-1][3]);
+	b[ 0] = T1[temp[0][0]][1];
+	b[ 1] = T1[temp[1][1]][1];
+	b[ 2] = T1[temp[2][2]][1];
+	b[ 3] = T1[temp[3][3]][1];
+	b[ 4] = T1[temp[1][0]][1];
+	b[ 5] = T1[temp[2][1]][1];
+	b[ 6] = T1[temp[3][2]][1];
+	b[ 7] = T1[temp[0][3]][1];
+	b[ 8] = T1[temp[2][0]][1];
+	b[ 9] = T1[temp[3][1]][1];
+	b[10] = T1[temp[0][2]][1];
+	b[11] = T1[temp[1][3]][1];
+	b[12] = T1[temp[3][0]][1];
+	b[13] = T1[temp[0][1]][1];
+	b[14] = T1[temp[1][2]][1];
+	b[15] = T1[temp[2][3]][1];
+	*((word32*)(b   )) ^= *((word32*)rk[ROUNDS][0]);
+	*((word32*)(b+ 4)) ^= *((word32*)rk[ROUNDS][1]);
+	*((word32*)(b+ 8)) ^= *((word32*)rk[ROUNDS][2]);
+	*((word32*)(b+12)) ^= *((word32*)rk[ROUNDS][3]);
+
+	return 0;
+}
+
+/**
+ * Decrypt a single block.
+ */
+int rijndaelDecrypt(word8 a[16], word8 b[16], word8 rk[MAXROUNDS+1][4][4], int ROUNDS) {
+	int r;
+	word8 temp[4][4];
+	
+    *((word32*)temp[0]) = *((word32*)(a   )) ^ *((word32*)rk[ROUNDS][0]);
+    *((word32*)temp[1]) = *((word32*)(a+ 4)) ^ *((word32*)rk[ROUNDS][1]);
+    *((word32*)temp[2]) = *((word32*)(a+ 8)) ^ *((word32*)rk[ROUNDS][2]);
+    *((word32*)temp[3]) = *((word32*)(a+12)) ^ *((word32*)rk[ROUNDS][3]);
+
+    *((word32*)(b   )) = *((word32*)T5[temp[0][0]])
+           ^ *((word32*)T6[temp[3][1]])
+           ^ *((word32*)T7[temp[2][2]]) 
+           ^ *((word32*)T8[temp[1][3]]);
+	*((word32*)(b+ 4)) = *((word32*)T5[temp[1][0]])
+           ^ *((word32*)T6[temp[0][1]])
+           ^ *((word32*)T7[temp[3][2]]) 
+           ^ *((word32*)T8[temp[2][3]]);
+	*((word32*)(b+ 8)) = *((word32*)T5[temp[2][0]])
+           ^ *((word32*)T6[temp[1][1]])
+           ^ *((word32*)T7[temp[0][2]]) 
+           ^ *((word32*)T8[temp[3][3]]);
+	*((word32*)(b+12)) = *((word32*)T5[temp[3][0]])
+           ^ *((word32*)T6[temp[2][1]])
+           ^ *((word32*)T7[temp[1][2]]) 
+           ^ *((word32*)T8[temp[0][3]]);
+	for (r = ROUNDS-1; r > 1; r--) {
+		*((word32*)temp[0]) = *((word32*)(b   )) ^ *((word32*)rk[r][0]);
+		*((word32*)temp[1]) = *((word32*)(b+ 4)) ^ *((word32*)rk[r][1]);
+		*((word32*)temp[2]) = *((word32*)(b+ 8)) ^ *((word32*)rk[r][2]);
+		*((word32*)temp[3]) = *((word32*)(b+12)) ^ *((word32*)rk[r][3]);
+		*((word32*)(b   )) = *((word32*)T5[temp[0][0]])
+           ^ *((word32*)T6[temp[3][1]])
+           ^ *((word32*)T7[temp[2][2]]) 
+           ^ *((word32*)T8[temp[1][3]]);
+		*((word32*)(b+ 4)) = *((word32*)T5[temp[1][0]])
+           ^ *((word32*)T6[temp[0][1]])
+           ^ *((word32*)T7[temp[3][2]]) 
+           ^ *((word32*)T8[temp[2][3]]);
+		*((word32*)(b+ 8)) = *((word32*)T5[temp[2][0]])
+           ^ *((word32*)T6[temp[1][1]])
+           ^ *((word32*)T7[temp[0][2]]) 
+           ^ *((word32*)T8[temp[3][3]]);
+		*((word32*)(b+12)) = *((word32*)T5[temp[3][0]])
+           ^ *((word32*)T6[temp[2][1]])
+           ^ *((word32*)T7[temp[1][2]]) 
+           ^ *((word32*)T8[temp[0][3]]);
+	}
+	/* last round is special */   
+	*((word32*)temp[0]) = *((word32*)(b   )) ^ *((word32*)rk[1][0]);
+	*((word32*)temp[1]) = *((word32*)(b+ 4)) ^ *((word32*)rk[1][1]);
+	*((word32*)temp[2]) = *((word32*)(b+ 8)) ^ *((word32*)rk[1][2]);
+	*((word32*)temp[3]) = *((word32*)(b+12)) ^ *((word32*)rk[1][3]);
+	b[ 0] = S5[temp[0][0]];
+	b[ 1] = S5[temp[3][1]];
+	b[ 2] = S5[temp[2][2]];
+	b[ 3] = S5[temp[1][3]];
+	b[ 4] = S5[temp[1][0]];
+	b[ 5] = S5[temp[0][1]];
+	b[ 6] = S5[temp[3][2]];
+	b[ 7] = S5[temp[2][3]];
+	b[ 8] = S5[temp[2][0]];
+	b[ 9] = S5[temp[1][1]];
+	b[10] = S5[temp[0][2]];
+	b[11] = S5[temp[3][3]];
+	b[12] = S5[temp[3][0]];
+	b[13] = S5[temp[2][1]];
+	b[14] = S5[temp[1][2]];
+	b[15] = S5[temp[0][3]];
+	*((word32*)(b   )) ^= *((word32*)rk[0][0]);
+	*((word32*)(b+ 4)) ^= *((word32*)rk[0][1]);
+	*((word32*)(b+ 8)) ^= *((word32*)rk[0][2]);
+	*((word32*)(b+12)) ^= *((word32*)rk[0][3]);
+
+	return 0;
+}
+
 #endif
-
-#include "rijndael-alg-fst.h"
-
-#define FULL_UNROLL 1
-/*
-Te0[x] = S [x].[02, 01, 01, 03];
-Te1[x] = S [x].[03, 02, 01, 01];
-Te2[x] = S [x].[01, 03, 02, 01];
-Te3[x] = S [x].[01, 01, 03, 02];
-Te4[x] = S [x].[01, 01, 01, 01];
-
-Td0[x] = Si[x].[0e, 09, 0d, 0b];
-Td1[x] = Si[x].[0b, 0e, 09, 0d];
-Td2[x] = Si[x].[0d, 0b, 0e, 09];
-Td3[x] = Si[x].[09, 0d, 0b, 0e];
-Td4[x] = Si[x].[01, 01, 01, 01];
-*/
-
-static const u32 Te0[256] = {
-    0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU,
-    0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U,
-    0x60303050U, 0x02010103U, 0xce6767a9U, 0x562b2b7dU,
-    0xe7fefe19U, 0xb5d7d762U, 0x4dababe6U, 0xec76769aU,
-    0x8fcaca45U, 0x1f82829dU, 0x89c9c940U, 0xfa7d7d87U,
-    0xeffafa15U, 0xb25959ebU, 0x8e4747c9U, 0xfbf0f00bU,
-    0x41adadecU, 0xb3d4d467U, 0x5fa2a2fdU, 0x45afafeaU,
-    0x239c9cbfU, 0x53a4a4f7U, 0xe4727296U, 0x9bc0c05bU,
-    0x75b7b7c2U, 0xe1fdfd1cU, 0x3d9393aeU, 0x4c26266aU,
-    0x6c36365aU, 0x7e3f3f41U, 0xf5f7f702U, 0x83cccc4fU,
-    0x6834345cU, 0x51a5a5f4U, 0xd1e5e534U, 0xf9f1f108U,
-    0xe2717193U, 0xabd8d873U, 0x62313153U, 0x2a15153fU,
-    0x0804040cU, 0x95c7c752U, 0x46232365U, 0x9dc3c35eU,
-    0x30181828U, 0x379696a1U, 0x0a05050fU, 0x2f9a9ab5U,
-    0x0e070709U, 0x24121236U, 0x1b80809bU, 0xdfe2e23dU,
-    0xcdebeb26U, 0x4e272769U, 0x7fb2b2cdU, 0xea75759fU,
-    0x1209091bU, 0x1d83839eU, 0x582c2c74U, 0x341a1a2eU,
-    0x361b1b2dU, 0xdc6e6eb2U, 0xb45a5aeeU, 0x5ba0a0fbU,
-    0xa45252f6U, 0x763b3b4dU, 0xb7d6d661U, 0x7db3b3ceU,
-    0x5229297bU, 0xdde3e33eU, 0x5e2f2f71U, 0x13848497U,
-    0xa65353f5U, 0xb9d1d168U, 0x00000000U, 0xc1eded2cU,
-    0x40202060U, 0xe3fcfc1fU, 0x79b1b1c8U, 0xb65b5bedU,
-    0xd46a6abeU, 0x8dcbcb46U, 0x67bebed9U, 0x7239394bU,
-    0x944a4adeU, 0x984c4cd4U, 0xb05858e8U, 0x85cfcf4aU,
-    0xbbd0d06bU, 0xc5efef2aU, 0x4faaaae5U, 0xedfbfb16U,
-    0x864343c5U, 0x9a4d4dd7U, 0x66333355U, 0x11858594U,
-    0x8a4545cfU, 0xe9f9f910U, 0x04020206U, 0xfe7f7f81U,
-    0xa05050f0U, 0x783c3c44U, 0x259f9fbaU, 0x4ba8a8e3U,
-    0xa25151f3U, 0x5da3a3feU, 0x804040c0U, 0x058f8f8aU,
-    0x3f9292adU, 0x219d9dbcU, 0x70383848U, 0xf1f5f504U,
-    0x63bcbcdfU, 0x77b6b6c1U, 0xafdada75U, 0x42212163U,
-    0x20101030U, 0xe5ffff1aU, 0xfdf3f30eU, 0xbfd2d26dU,
-    0x81cdcd4cU, 0x180c0c14U, 0x26131335U, 0xc3ecec2fU,
-    0xbe5f5fe1U, 0x359797a2U, 0x884444ccU, 0x2e171739U,
-    0x93c4c457U, 0x55a7a7f2U, 0xfc7e7e82U, 0x7a3d3d47U,
-    0xc86464acU, 0xba5d5de7U, 0x3219192bU, 0xe6737395U,
-    0xc06060a0U, 0x19818198U, 0x9e4f4fd1U, 0xa3dcdc7fU,
-    0x44222266U, 0x542a2a7eU, 0x3b9090abU, 0x0b888883U,
-    0x8c4646caU, 0xc7eeee29U, 0x6bb8b8d3U, 0x2814143cU,
-    0xa7dede79U, 0xbc5e5ee2U, 0x160b0b1dU, 0xaddbdb76U,
-    0xdbe0e03bU, 0x64323256U, 0x743a3a4eU, 0x140a0a1eU,
-    0x924949dbU, 0x0c06060aU, 0x4824246cU, 0xb85c5ce4U,
-    0x9fc2c25dU, 0xbdd3d36eU, 0x43acacefU, 0xc46262a6U,
-    0x399191a8U, 0x319595a4U, 0xd3e4e437U, 0xf279798bU,
-    0xd5e7e732U, 0x8bc8c843U, 0x6e373759U, 0xda6d6db7U,
-    0x018d8d8cU, 0xb1d5d564U, 0x9c4e4ed2U, 0x49a9a9e0U,
-    0xd86c6cb4U, 0xac5656faU, 0xf3f4f407U, 0xcfeaea25U,
-    0xca6565afU, 0xf47a7a8eU, 0x47aeaee9U, 0x10080818U,
-    0x6fbabad5U, 0xf0787888U, 0x4a25256fU, 0x5c2e2e72U,
-    0x381c1c24U, 0x57a6a6f1U, 0x73b4b4c7U, 0x97c6c651U,
-    0xcbe8e823U, 0xa1dddd7cU, 0xe874749cU, 0x3e1f1f21U,
-    0x964b4bddU, 0x61bdbddcU, 0x0d8b8b86U, 0x0f8a8a85U,
-    0xe0707090U, 0x7c3e3e42U, 0x71b5b5c4U, 0xcc6666aaU,
-    0x904848d8U, 0x06030305U, 0xf7f6f601U, 0x1c0e0e12U,
-    0xc26161a3U, 0x6a35355fU, 0xae5757f9U, 0x69b9b9d0U,
-    0x17868691U, 0x99c1c158U, 0x3a1d1d27U, 0x279e9eb9U,
-    0xd9e1e138U, 0xebf8f813U, 0x2b9898b3U, 0x22111133U,
-    0xd26969bbU, 0xa9d9d970U, 0x078e8e89U, 0x339494a7U,
-    0x2d9b9bb6U, 0x3c1e1e22U, 0x15878792U, 0xc9e9e920U,
-    0x87cece49U, 0xaa5555ffU, 0x50282878U, 0xa5dfdf7aU,
-    0x038c8c8fU, 0x59a1a1f8U, 0x09898980U, 0x1a0d0d17U,
-    0x65bfbfdaU, 0xd7e6e631U, 0x844242c6U, 0xd06868b8U,
-    0x824141c3U, 0x299999b0U, 0x5a2d2d77U, 0x1e0f0f11U,
-    0x7bb0b0cbU, 0xa85454fcU, 0x6dbbbbd6U, 0x2c16163aU,
-};
-static const u32 Te1[256] = {
-    0xa5c66363U, 0x84f87c7cU, 0x99ee7777U, 0x8df67b7bU,
-    0x0dfff2f2U, 0xbdd66b6bU, 0xb1de6f6fU, 0x5491c5c5U,
-    0x50603030U, 0x03020101U, 0xa9ce6767U, 0x7d562b2bU,
-    0x19e7fefeU, 0x62b5d7d7U, 0xe64dababU, 0x9aec7676U,
-    0x458fcacaU, 0x9d1f8282U, 0x4089c9c9U, 0x87fa7d7dU,
-    0x15effafaU, 0xebb25959U, 0xc98e4747U, 0x0bfbf0f0U,
-    0xec41adadU, 0x67b3d4d4U, 0xfd5fa2a2U, 0xea45afafU,
-    0xbf239c9cU, 0xf753a4a4U, 0x96e47272U, 0x5b9bc0c0U,
-    0xc275b7b7U, 0x1ce1fdfdU, 0xae3d9393U, 0x6a4c2626U,
-    0x5a6c3636U, 0x417e3f3fU, 0x02f5f7f7U, 0x4f83ccccU,
-    0x5c683434U, 0xf451a5a5U, 0x34d1e5e5U, 0x08f9f1f1U,
-    0x93e27171U, 0x73abd8d8U, 0x53623131U, 0x3f2a1515U,
-    0x0c080404U, 0x5295c7c7U, 0x65462323U, 0x5e9dc3c3U,
-    0x28301818U, 0xa1379696U, 0x0f0a0505U, 0xb52f9a9aU,
-    0x090e0707U, 0x36241212U, 0x9b1b8080U, 0x3ddfe2e2U,
-    0x26cdebebU, 0x694e2727U, 0xcd7fb2b2U, 0x9fea7575U,
-    0x1b120909U, 0x9e1d8383U, 0x74582c2cU, 0x2e341a1aU,
-    0x2d361b1bU, 0xb2dc6e6eU, 0xeeb45a5aU, 0xfb5ba0a0U,
-    0xf6a45252U, 0x4d763b3bU, 0x61b7d6d6U, 0xce7db3b3U,
-    0x7b522929U, 0x3edde3e3U, 0x715e2f2fU, 0x97138484U,
-    0xf5a65353U, 0x68b9d1d1U, 0x00000000U, 0x2cc1ededU,
-    0x60402020U, 0x1fe3fcfcU, 0xc879b1b1U, 0xedb65b5bU,
-    0xbed46a6aU, 0x468dcbcbU, 0xd967bebeU, 0x4b723939U,
-    0xde944a4aU, 0xd4984c4cU, 0xe8b05858U, 0x4a85cfcfU,
-    0x6bbbd0d0U, 0x2ac5efefU, 0xe54faaaaU, 0x16edfbfbU,
-    0xc5864343U, 0xd79a4d4dU, 0x55663333U, 0x94118585U,
-    0xcf8a4545U, 0x10e9f9f9U, 0x06040202U, 0x81fe7f7fU,
-    0xf0a05050U, 0x44783c3cU, 0xba259f9fU, 0xe34ba8a8U,
-    0xf3a25151U, 0xfe5da3a3U, 0xc0804040U, 0x8a058f8fU,
-    0xad3f9292U, 0xbc219d9dU, 0x48703838U, 0x04f1f5f5U,
-    0xdf63bcbcU, 0xc177b6b6U, 0x75afdadaU, 0x63422121U,
-    0x30201010U, 0x1ae5ffffU, 0x0efdf3f3U, 0x6dbfd2d2U,
-    0x4c81cdcdU, 0x14180c0cU, 0x35261313U, 0x2fc3ececU,
-    0xe1be5f5fU, 0xa2359797U, 0xcc884444U, 0x392e1717U,
-    0x5793c4c4U, 0xf255a7a7U, 0x82fc7e7eU, 0x477a3d3dU,
-    0xacc86464U, 0xe7ba5d5dU, 0x2b321919U, 0x95e67373U,
-    0xa0c06060U, 0x98198181U, 0xd19e4f4fU, 0x7fa3dcdcU,
-    0x66442222U, 0x7e542a2aU, 0xab3b9090U, 0x830b8888U,
-    0xca8c4646U, 0x29c7eeeeU, 0xd36bb8b8U, 0x3c281414U,
-    0x79a7dedeU, 0xe2bc5e5eU, 0x1d160b0bU, 0x76addbdbU,
-    0x3bdbe0e0U, 0x56643232U, 0x4e743a3aU, 0x1e140a0aU,
-    0xdb924949U, 0x0a0c0606U, 0x6c482424U, 0xe4b85c5cU,
-    0x5d9fc2c2U, 0x6ebdd3d3U, 0xef43acacU, 0xa6c46262U,
-    0xa8399191U, 0xa4319595U, 0x37d3e4e4U, 0x8bf27979U,
-    0x32d5e7e7U, 0x438bc8c8U, 0x596e3737U, 0xb7da6d6dU,
-    0x8c018d8dU, 0x64b1d5d5U, 0xd29c4e4eU, 0xe049a9a9U,
-    0xb4d86c6cU, 0xfaac5656U, 0x07f3f4f4U, 0x25cfeaeaU,
-    0xafca6565U, 0x8ef47a7aU, 0xe947aeaeU, 0x18100808U,
-    0xd56fbabaU, 0x88f07878U, 0x6f4a2525U, 0x725c2e2eU,
-    0x24381c1cU, 0xf157a6a6U, 0xc773b4b4U, 0x5197c6c6U,
-    0x23cbe8e8U, 0x7ca1ddddU, 0x9ce87474U, 0x213e1f1fU,
-    0xdd964b4bU, 0xdc61bdbdU, 0x860d8b8bU, 0x850f8a8aU,
-    0x90e07070U, 0x427c3e3eU, 0xc471b5b5U, 0xaacc6666U,
-    0xd8904848U, 0x05060303U, 0x01f7f6f6U, 0x121c0e0eU,
-    0xa3c26161U, 0x5f6a3535U, 0xf9ae5757U, 0xd069b9b9U,
-    0x91178686U, 0x5899c1c1U, 0x273a1d1dU, 0xb9279e9eU,
-    0x38d9e1e1U, 0x13ebf8f8U, 0xb32b9898U, 0x33221111U,
-    0xbbd26969U, 0x70a9d9d9U, 0x89078e8eU, 0xa7339494U,
-    0xb62d9b9bU, 0x223c1e1eU, 0x92158787U, 0x20c9e9e9U,
-    0x4987ceceU, 0xffaa5555U, 0x78502828U, 0x7aa5dfdfU,
-    0x8f038c8cU, 0xf859a1a1U, 0x80098989U, 0x171a0d0dU,
-    0xda65bfbfU, 0x31d7e6e6U, 0xc6844242U, 0xb8d06868U,
-    0xc3824141U, 0xb0299999U, 0x775a2d2dU, 0x111e0f0fU,
-    0xcb7bb0b0U, 0xfca85454U, 0xd66dbbbbU, 0x3a2c1616U,
-};
-static const u32 Te2[256] = {
-    0x63a5c663U, 0x7c84f87cU, 0x7799ee77U, 0x7b8df67bU,
-    0xf20dfff2U, 0x6bbdd66bU, 0x6fb1de6fU, 0xc55491c5U,
-    0x30506030U, 0x01030201U, 0x67a9ce67U, 0x2b7d562bU,
-    0xfe19e7feU, 0xd762b5d7U, 0xabe64dabU, 0x769aec76U,
-    0xca458fcaU, 0x829d1f82U, 0xc94089c9U, 0x7d87fa7dU,
-    0xfa15effaU, 0x59ebb259U, 0x47c98e47U, 0xf00bfbf0U,
-    0xadec41adU, 0xd467b3d4U, 0xa2fd5fa2U, 0xafea45afU,
-    0x9cbf239cU, 0xa4f753a4U, 0x7296e472U, 0xc05b9bc0U,
-    0xb7c275b7U, 0xfd1ce1fdU, 0x93ae3d93U, 0x266a4c26U,
-    0x365a6c36U, 0x3f417e3fU, 0xf702f5f7U, 0xcc4f83ccU,
-    0x345c6834U, 0xa5f451a5U, 0xe534d1e5U, 0xf108f9f1U,
-    0x7193e271U, 0xd873abd8U, 0x31536231U, 0x153f2a15U,
-    0x040c0804U, 0xc75295c7U, 0x23654623U, 0xc35e9dc3U,
-    0x18283018U, 0x96a13796U, 0x050f0a05U, 0x9ab52f9aU,
-    0x07090e07U, 0x12362412U, 0x809b1b80U, 0xe23ddfe2U,
-    0xeb26cdebU, 0x27694e27U, 0xb2cd7fb2U, 0x759fea75U,
-    0x091b1209U, 0x839e1d83U, 0x2c74582cU, 0x1a2e341aU,
-    0x1b2d361bU, 0x6eb2dc6eU, 0x5aeeb45aU, 0xa0fb5ba0U,
-    0x52f6a452U, 0x3b4d763bU, 0xd661b7d6U, 0xb3ce7db3U,
-    0x297b5229U, 0xe33edde3U, 0x2f715e2fU, 0x84971384U,
-    0x53f5a653U, 0xd168b9d1U, 0x00000000U, 0xed2cc1edU,
-    0x20604020U, 0xfc1fe3fcU, 0xb1c879b1U, 0x5bedb65bU,
-    0x6abed46aU, 0xcb468dcbU, 0xbed967beU, 0x394b7239U,
-    0x4ade944aU, 0x4cd4984cU, 0x58e8b058U, 0xcf4a85cfU,
-    0xd06bbbd0U, 0xef2ac5efU, 0xaae54faaU, 0xfb16edfbU,
-    0x43c58643U, 0x4dd79a4dU, 0x33556633U, 0x85941185U,
-    0x45cf8a45U, 0xf910e9f9U, 0x02060402U, 0x7f81fe7fU,
-    0x50f0a050U, 0x3c44783cU, 0x9fba259fU, 0xa8e34ba8U,
-    0x51f3a251U, 0xa3fe5da3U, 0x40c08040U, 0x8f8a058fU,
-    0x92ad3f92U, 0x9dbc219dU, 0x38487038U, 0xf504f1f5U,
-    0xbcdf63bcU, 0xb6c177b6U, 0xda75afdaU, 0x21634221U,
-    0x10302010U, 0xff1ae5ffU, 0xf30efdf3U, 0xd26dbfd2U,
-    0xcd4c81cdU, 0x0c14180cU, 0x13352613U, 0xec2fc3ecU,
-    0x5fe1be5fU, 0x97a23597U, 0x44cc8844U, 0x17392e17U,
-    0xc45793c4U, 0xa7f255a7U, 0x7e82fc7eU, 0x3d477a3dU,
-    0x64acc864U, 0x5de7ba5dU, 0x192b3219U, 0x7395e673U,
-    0x60a0c060U, 0x81981981U, 0x4fd19e4fU, 0xdc7fa3dcU,
-    0x22664422U, 0x2a7e542aU, 0x90ab3b90U, 0x88830b88U,
-    0x46ca8c46U, 0xee29c7eeU, 0xb8d36bb8U, 0x143c2814U,
-    0xde79a7deU, 0x5ee2bc5eU, 0x0b1d160bU, 0xdb76addbU,
-    0xe03bdbe0U, 0x32566432U, 0x3a4e743aU, 0x0a1e140aU,
-    0x49db9249U, 0x060a0c06U, 0x246c4824U, 0x5ce4b85cU,
-    0xc25d9fc2U, 0xd36ebdd3U, 0xacef43acU, 0x62a6c462U,
-    0x91a83991U, 0x95a43195U, 0xe437d3e4U, 0x798bf279U,
-    0xe732d5e7U, 0xc8438bc8U, 0x37596e37U, 0x6db7da6dU,
-    0x8d8c018dU, 0xd564b1d5U, 0x4ed29c4eU, 0xa9e049a9U,
-    0x6cb4d86cU, 0x56faac56U, 0xf407f3f4U, 0xea25cfeaU,
-    0x65afca65U, 0x7a8ef47aU, 0xaee947aeU, 0x08181008U,
-    0xbad56fbaU, 0x7888f078U, 0x256f4a25U, 0x2e725c2eU,
-    0x1c24381cU, 0xa6f157a6U, 0xb4c773b4U, 0xc65197c6U,
-    0xe823cbe8U, 0xdd7ca1ddU, 0x749ce874U, 0x1f213e1fU,
-    0x4bdd964bU, 0xbddc61bdU, 0x8b860d8bU, 0x8a850f8aU,
-    0x7090e070U, 0x3e427c3eU, 0xb5c471b5U, 0x66aacc66U,
-    0x48d89048U, 0x03050603U, 0xf601f7f6U, 0x0e121c0eU,
-    0x61a3c261U, 0x355f6a35U, 0x57f9ae57U, 0xb9d069b9U,
-    0x86911786U, 0xc15899c1U, 0x1d273a1dU, 0x9eb9279eU,
-    0xe138d9e1U, 0xf813ebf8U, 0x98b32b98U, 0x11332211U,
-    0x69bbd269U, 0xd970a9d9U, 0x8e89078eU, 0x94a73394U,
-    0x9bb62d9bU, 0x1e223c1eU, 0x87921587U, 0xe920c9e9U,
-    0xce4987ceU, 0x55ffaa55U, 0x28785028U, 0xdf7aa5dfU,
-    0x8c8f038cU, 0xa1f859a1U, 0x89800989U, 0x0d171a0dU,
-    0xbfda65bfU, 0xe631d7e6U, 0x42c68442U, 0x68b8d068U,
-    0x41c38241U, 0x99b02999U, 0x2d775a2dU, 0x0f111e0fU,
-    0xb0cb7bb0U, 0x54fca854U, 0xbbd66dbbU, 0x163a2c16U,
-};
-static const u32 Te3[256] = {
-
-    0x6363a5c6U, 0x7c7c84f8U, 0x777799eeU, 0x7b7b8df6U,
-    0xf2f20dffU, 0x6b6bbdd6U, 0x6f6fb1deU, 0xc5c55491U,
-    0x30305060U, 0x01010302U, 0x6767a9ceU, 0x2b2b7d56U,
-    0xfefe19e7U, 0xd7d762b5U, 0xababe64dU, 0x76769aecU,
-    0xcaca458fU, 0x82829d1fU, 0xc9c94089U, 0x7d7d87faU,
-    0xfafa15efU, 0x5959ebb2U, 0x4747c98eU, 0xf0f00bfbU,
-    0xadadec41U, 0xd4d467b3U, 0xa2a2fd5fU, 0xafafea45U,
-    0x9c9cbf23U, 0xa4a4f753U, 0x727296e4U, 0xc0c05b9bU,
-    0xb7b7c275U, 0xfdfd1ce1U, 0x9393ae3dU, 0x26266a4cU,
-    0x36365a6cU, 0x3f3f417eU, 0xf7f702f5U, 0xcccc4f83U,
-    0x34345c68U, 0xa5a5f451U, 0xe5e534d1U, 0xf1f108f9U,
-    0x717193e2U, 0xd8d873abU, 0x31315362U, 0x15153f2aU,
-    0x04040c08U, 0xc7c75295U, 0x23236546U, 0xc3c35e9dU,
-    0x18182830U, 0x9696a137U, 0x05050f0aU, 0x9a9ab52fU,
-    0x0707090eU, 0x12123624U, 0x80809b1bU, 0xe2e23ddfU,
-    0xebeb26cdU, 0x2727694eU, 0xb2b2cd7fU, 0x75759feaU,
-    0x09091b12U, 0x83839e1dU, 0x2c2c7458U, 0x1a1a2e34U,
-    0x1b1b2d36U, 0x6e6eb2dcU, 0x5a5aeeb4U, 0xa0a0fb5bU,
-    0x5252f6a4U, 0x3b3b4d76U, 0xd6d661b7U, 0xb3b3ce7dU,
-    0x29297b52U, 0xe3e33eddU, 0x2f2f715eU, 0x84849713U,
-    0x5353f5a6U, 0xd1d168b9U, 0x00000000U, 0xeded2cc1U,
-    0x20206040U, 0xfcfc1fe3U, 0xb1b1c879U, 0x5b5bedb6U,
-    0x6a6abed4U, 0xcbcb468dU, 0xbebed967U, 0x39394b72U,
-    0x4a4ade94U, 0x4c4cd498U, 0x5858e8b0U, 0xcfcf4a85U,
-    0xd0d06bbbU, 0xefef2ac5U, 0xaaaae54fU, 0xfbfb16edU,
-    0x4343c586U, 0x4d4dd79aU, 0x33335566U, 0x85859411U,
-    0x4545cf8aU, 0xf9f910e9U, 0x02020604U, 0x7f7f81feU,
-    0x5050f0a0U, 0x3c3c4478U, 0x9f9fba25U, 0xa8a8e34bU,
-    0x5151f3a2U, 0xa3a3fe5dU, 0x4040c080U, 0x8f8f8a05U,
-    0x9292ad3fU, 0x9d9dbc21U, 0x38384870U, 0xf5f504f1U,
-    0xbcbcdf63U, 0xb6b6c177U, 0xdada75afU, 0x21216342U,
-    0x10103020U, 0xffff1ae5U, 0xf3f30efdU, 0xd2d26dbfU,
-    0xcdcd4c81U, 0x0c0c1418U, 0x13133526U, 0xecec2fc3U,
-    0x5f5fe1beU, 0x9797a235U, 0x4444cc88U, 0x1717392eU,
-    0xc4c45793U, 0xa7a7f255U, 0x7e7e82fcU, 0x3d3d477aU,
-    0x6464acc8U, 0x5d5de7baU, 0x19192b32U, 0x737395e6U,
-    0x6060a0c0U, 0x81819819U, 0x4f4fd19eU, 0xdcdc7fa3U,
-    0x22226644U, 0x2a2a7e54U, 0x9090ab3bU, 0x8888830bU,
-    0x4646ca8cU, 0xeeee29c7U, 0xb8b8d36bU, 0x14143c28U,
-    0xdede79a7U, 0x5e5ee2bcU, 0x0b0b1d16U, 0xdbdb76adU,
-    0xe0e03bdbU, 0x32325664U, 0x3a3a4e74U, 0x0a0a1e14U,
-    0x4949db92U, 0x06060a0cU, 0x24246c48U, 0x5c5ce4b8U,
-    0xc2c25d9fU, 0xd3d36ebdU, 0xacacef43U, 0x6262a6c4U,
-    0x9191a839U, 0x9595a431U, 0xe4e437d3U, 0x79798bf2U,
-    0xe7e732d5U, 0xc8c8438bU, 0x3737596eU, 0x6d6db7daU,
-    0x8d8d8c01U, 0xd5d564b1U, 0x4e4ed29cU, 0xa9a9e049U,
-    0x6c6cb4d8U, 0x5656faacU, 0xf4f407f3U, 0xeaea25cfU,
-    0x6565afcaU, 0x7a7a8ef4U, 0xaeaee947U, 0x08081810U,
-    0xbabad56fU, 0x787888f0U, 0x25256f4aU, 0x2e2e725cU,
-    0x1c1c2438U, 0xa6a6f157U, 0xb4b4c773U, 0xc6c65197U,
-    0xe8e823cbU, 0xdddd7ca1U, 0x74749ce8U, 0x1f1f213eU,
-    0x4b4bdd96U, 0xbdbddc61U, 0x8b8b860dU, 0x8a8a850fU,
-    0x707090e0U, 0x3e3e427cU, 0xb5b5c471U, 0x6666aaccU,
-    0x4848d890U, 0x03030506U, 0xf6f601f7U, 0x0e0e121cU,
-    0x6161a3c2U, 0x35355f6aU, 0x5757f9aeU, 0xb9b9d069U,
-    0x86869117U, 0xc1c15899U, 0x1d1d273aU, 0x9e9eb927U,
-    0xe1e138d9U, 0xf8f813ebU, 0x9898b32bU, 0x11113322U,
-    0x6969bbd2U, 0xd9d970a9U, 0x8e8e8907U, 0x9494a733U,
-    0x9b9bb62dU, 0x1e1e223cU, 0x87879215U, 0xe9e920c9U,
-    0xcece4987U, 0x5555ffaaU, 0x28287850U, 0xdfdf7aa5U,
-    0x8c8c8f03U, 0xa1a1f859U, 0x89898009U, 0x0d0d171aU,
-    0xbfbfda65U, 0xe6e631d7U, 0x4242c684U, 0x6868b8d0U,
-    0x4141c382U, 0x9999b029U, 0x2d2d775aU, 0x0f0f111eU,
-    0xb0b0cb7bU, 0x5454fca8U, 0xbbbbd66dU, 0x16163a2cU,
-};
-static const u32 Te4[256] = {
-    0x63636363U, 0x7c7c7c7cU, 0x77777777U, 0x7b7b7b7bU,
-    0xf2f2f2f2U, 0x6b6b6b6bU, 0x6f6f6f6fU, 0xc5c5c5c5U,
-    0x30303030U, 0x01010101U, 0x67676767U, 0x2b2b2b2bU,
-    0xfefefefeU, 0xd7d7d7d7U, 0xababababU, 0x76767676U,
-    0xcacacacaU, 0x82828282U, 0xc9c9c9c9U, 0x7d7d7d7dU,
-    0xfafafafaU, 0x59595959U, 0x47474747U, 0xf0f0f0f0U,
-    0xadadadadU, 0xd4d4d4d4U, 0xa2a2a2a2U, 0xafafafafU,
-    0x9c9c9c9cU, 0xa4a4a4a4U, 0x72727272U, 0xc0c0c0c0U,
-    0xb7b7b7b7U, 0xfdfdfdfdU, 0x93939393U, 0x26262626U,
-    0x36363636U, 0x3f3f3f3fU, 0xf7f7f7f7U, 0xccccccccU,
-    0x34343434U, 0xa5a5a5a5U, 0xe5e5e5e5U, 0xf1f1f1f1U,
-    0x71717171U, 0xd8d8d8d8U, 0x31313131U, 0x15151515U,
-    0x04040404U, 0xc7c7c7c7U, 0x23232323U, 0xc3c3c3c3U,
-    0x18181818U, 0x96969696U, 0x05050505U, 0x9a9a9a9aU,
-    0x07070707U, 0x12121212U, 0x80808080U, 0xe2e2e2e2U,
-    0xebebebebU, 0x27272727U, 0xb2b2b2b2U, 0x75757575U,
-    0x09090909U, 0x83838383U, 0x2c2c2c2cU, 0x1a1a1a1aU,
-    0x1b1b1b1bU, 0x6e6e6e6eU, 0x5a5a5a5aU, 0xa0a0a0a0U,
-    0x52525252U, 0x3b3b3b3bU, 0xd6d6d6d6U, 0xb3b3b3b3U,
-    0x29292929U, 0xe3e3e3e3U, 0x2f2f2f2fU, 0x84848484U,
-    0x53535353U, 0xd1d1d1d1U, 0x00000000U, 0xededededU,
-    0x20202020U, 0xfcfcfcfcU, 0xb1b1b1b1U, 0x5b5b5b5bU,
-    0x6a6a6a6aU, 0xcbcbcbcbU, 0xbebebebeU, 0x39393939U,
-    0x4a4a4a4aU, 0x4c4c4c4cU, 0x58585858U, 0xcfcfcfcfU,
-    0xd0d0d0d0U, 0xefefefefU, 0xaaaaaaaaU, 0xfbfbfbfbU,
-    0x43434343U, 0x4d4d4d4dU, 0x33333333U, 0x85858585U,
-    0x45454545U, 0xf9f9f9f9U, 0x02020202U, 0x7f7f7f7fU,
-    0x50505050U, 0x3c3c3c3cU, 0x9f9f9f9fU, 0xa8a8a8a8U,
-    0x51515151U, 0xa3a3a3a3U, 0x40404040U, 0x8f8f8f8fU,
-    0x92929292U, 0x9d9d9d9dU, 0x38383838U, 0xf5f5f5f5U,
-    0xbcbcbcbcU, 0xb6b6b6b6U, 0xdadadadaU, 0x21212121U,
-    0x10101010U, 0xffffffffU, 0xf3f3f3f3U, 0xd2d2d2d2U,
-    0xcdcdcdcdU, 0x0c0c0c0cU, 0x13131313U, 0xececececU,
-    0x5f5f5f5fU, 0x97979797U, 0x44444444U, 0x17171717U,
-    0xc4c4c4c4U, 0xa7a7a7a7U, 0x7e7e7e7eU, 0x3d3d3d3dU,
-    0x64646464U, 0x5d5d5d5dU, 0x19191919U, 0x73737373U,
-    0x60606060U, 0x81818181U, 0x4f4f4f4fU, 0xdcdcdcdcU,
-    0x22222222U, 0x2a2a2a2aU, 0x90909090U, 0x88888888U,
-    0x46464646U, 0xeeeeeeeeU, 0xb8b8b8b8U, 0x14141414U,
-    0xdedededeU, 0x5e5e5e5eU, 0x0b0b0b0bU, 0xdbdbdbdbU,
-    0xe0e0e0e0U, 0x32323232U, 0x3a3a3a3aU, 0x0a0a0a0aU,
-    0x49494949U, 0x06060606U, 0x24242424U, 0x5c5c5c5cU,
-    0xc2c2c2c2U, 0xd3d3d3d3U, 0xacacacacU, 0x62626262U,
-    0x91919191U, 0x95959595U, 0xe4e4e4e4U, 0x79797979U,
-    0xe7e7e7e7U, 0xc8c8c8c8U, 0x37373737U, 0x6d6d6d6dU,
-    0x8d8d8d8dU, 0xd5d5d5d5U, 0x4e4e4e4eU, 0xa9a9a9a9U,
-    0x6c6c6c6cU, 0x56565656U, 0xf4f4f4f4U, 0xeaeaeaeaU,
-    0x65656565U, 0x7a7a7a7aU, 0xaeaeaeaeU, 0x08080808U,
-    0xbabababaU, 0x78787878U, 0x25252525U, 0x2e2e2e2eU,
-    0x1c1c1c1cU, 0xa6a6a6a6U, 0xb4b4b4b4U, 0xc6c6c6c6U,
-    0xe8e8e8e8U, 0xddddddddU, 0x74747474U, 0x1f1f1f1fU,
-    0x4b4b4b4bU, 0xbdbdbdbdU, 0x8b8b8b8bU, 0x8a8a8a8aU,
-    0x70707070U, 0x3e3e3e3eU, 0xb5b5b5b5U, 0x66666666U,
-    0x48484848U, 0x03030303U, 0xf6f6f6f6U, 0x0e0e0e0eU,
-    0x61616161U, 0x35353535U, 0x57575757U, 0xb9b9b9b9U,
-    0x86868686U, 0xc1c1c1c1U, 0x1d1d1d1dU, 0x9e9e9e9eU,
-    0xe1e1e1e1U, 0xf8f8f8f8U, 0x98989898U, 0x11111111U,
-    0x69696969U, 0xd9d9d9d9U, 0x8e8e8e8eU, 0x94949494U,
-    0x9b9b9b9bU, 0x1e1e1e1eU, 0x87878787U, 0xe9e9e9e9U,
-    0xcecececeU, 0x55555555U, 0x28282828U, 0xdfdfdfdfU,
-    0x8c8c8c8cU, 0xa1a1a1a1U, 0x89898989U, 0x0d0d0d0dU,
-    0xbfbfbfbfU, 0xe6e6e6e6U, 0x42424242U, 0x68686868U,
-    0x41414141U, 0x99999999U, 0x2d2d2d2dU, 0x0f0f0f0fU,
-    0xb0b0b0b0U, 0x54545454U, 0xbbbbbbbbU, 0x16161616U,
-};
-static const u32 Td0[256] = {
-    0x51f4a750U, 0x7e416553U, 0x1a17a4c3U, 0x3a275e96U,
-    0x3bab6bcbU, 0x1f9d45f1U, 0xacfa58abU, 0x4be30393U,
-    0x2030fa55U, 0xad766df6U, 0x88cc7691U, 0xf5024c25U,
-    0x4fe5d7fcU, 0xc52acbd7U, 0x26354480U, 0xb562a38fU,
-    0xdeb15a49U, 0x25ba1b67U, 0x45ea0e98U, 0x5dfec0e1U,
-    0xc32f7502U, 0x814cf012U, 0x8d4697a3U, 0x6bd3f9c6U,
-    0x038f5fe7U, 0x15929c95U, 0xbf6d7aebU, 0x955259daU,
-    0xd4be832dU, 0x587421d3U, 0x49e06929U, 0x8ec9c844U,
-    0x75c2896aU, 0xf48e7978U, 0x99583e6bU, 0x27b971ddU,
-    0xbee14fb6U, 0xf088ad17U, 0xc920ac66U, 0x7dce3ab4U,
-    0x63df4a18U, 0xe51a3182U, 0x97513360U, 0x62537f45U,
-    0xb16477e0U, 0xbb6bae84U, 0xfe81a01cU, 0xf9082b94U,
-    0x70486858U, 0x8f45fd19U, 0x94de6c87U, 0x527bf8b7U,
-    0xab73d323U, 0x724b02e2U, 0xe31f8f57U, 0x6655ab2aU,
-    0xb2eb2807U, 0x2fb5c203U, 0x86c57b9aU, 0xd33708a5U,
-    0x302887f2U, 0x23bfa5b2U, 0x02036abaU, 0xed16825cU,
-    0x8acf1c2bU, 0xa779b492U, 0xf307f2f0U, 0x4e69e2a1U,
-    0x65daf4cdU, 0x0605bed5U, 0xd134621fU, 0xc4a6fe8aU,
-    0x342e539dU, 0xa2f355a0U, 0x058ae132U, 0xa4f6eb75U,
-    0x0b83ec39U, 0x4060efaaU, 0x5e719f06U, 0xbd6e1051U,
-    0x3e218af9U, 0x96dd063dU, 0xdd3e05aeU, 0x4de6bd46U,
-    0x91548db5U, 0x71c45d05U, 0x0406d46fU, 0x605015ffU,
-    0x1998fb24U, 0xd6bde997U, 0x894043ccU, 0x67d99e77U,
-    0xb0e842bdU, 0x07898b88U, 0xe7195b38U, 0x79c8eedbU,
-    0xa17c0a47U, 0x7c420fe9U, 0xf8841ec9U, 0x00000000U,
-    0x09808683U, 0x322bed48U, 0x1e1170acU, 0x6c5a724eU,
-    0xfd0efffbU, 0x0f853856U, 0x3daed51eU, 0x362d3927U,
-    0x0a0fd964U, 0x685ca621U, 0x9b5b54d1U, 0x24362e3aU,
-    0x0c0a67b1U, 0x9357e70fU, 0xb4ee96d2U, 0x1b9b919eU,
-    0x80c0c54fU, 0x61dc20a2U, 0x5a774b69U, 0x1c121a16U,
-    0xe293ba0aU, 0xc0a02ae5U, 0x3c22e043U, 0x121b171dU,
-    0x0e090d0bU, 0xf28bc7adU, 0x2db6a8b9U, 0x141ea9c8U,
-    0x57f11985U, 0xaf75074cU, 0xee99ddbbU, 0xa37f60fdU,
-    0xf701269fU, 0x5c72f5bcU, 0x44663bc5U, 0x5bfb7e34U,
-    0x8b432976U, 0xcb23c6dcU, 0xb6edfc68U, 0xb8e4f163U,
-    0xd731dccaU, 0x42638510U, 0x13972240U, 0x84c61120U,
-    0x854a247dU, 0xd2bb3df8U, 0xaef93211U, 0xc729a16dU,
-    0x1d9e2f4bU, 0xdcb230f3U, 0x0d8652ecU, 0x77c1e3d0U,
-    0x2bb3166cU, 0xa970b999U, 0x119448faU, 0x47e96422U,
-    0xa8fc8cc4U, 0xa0f03f1aU, 0x567d2cd8U, 0x223390efU,
-    0x87494ec7U, 0xd938d1c1U, 0x8ccaa2feU, 0x98d40b36U,
-    0xa6f581cfU, 0xa57ade28U, 0xdab78e26U, 0x3fadbfa4U,
-    0x2c3a9de4U, 0x5078920dU, 0x6a5fcc9bU, 0x547e4662U,
-    0xf68d13c2U, 0x90d8b8e8U, 0x2e39f75eU, 0x82c3aff5U,
-    0x9f5d80beU, 0x69d0937cU, 0x6fd52da9U, 0xcf2512b3U,
-    0xc8ac993bU, 0x10187da7U, 0xe89c636eU, 0xdb3bbb7bU,
-    0xcd267809U, 0x6e5918f4U, 0xec9ab701U, 0x834f9aa8U,
-    0xe6956e65U, 0xaaffe67eU, 0x21bccf08U, 0xef15e8e6U,
-    0xbae79bd9U, 0x4a6f36ceU, 0xea9f09d4U, 0x29b07cd6U,
-    0x31a4b2afU, 0x2a3f2331U, 0xc6a59430U, 0x35a266c0U,
-    0x744ebc37U, 0xfc82caa6U, 0xe090d0b0U, 0x33a7d815U,
-    0xf104984aU, 0x41ecdaf7U, 0x7fcd500eU, 0x1791f62fU,
-    0x764dd68dU, 0x43efb04dU, 0xccaa4d54U, 0xe49604dfU,
-    0x9ed1b5e3U, 0x4c6a881bU, 0xc12c1fb8U, 0x4665517fU,
-    0x9d5eea04U, 0x018c355dU, 0xfa877473U, 0xfb0b412eU,
-    0xb3671d5aU, 0x92dbd252U, 0xe9105633U, 0x6dd64713U,
-    0x9ad7618cU, 0x37a10c7aU, 0x59f8148eU, 0xeb133c89U,
-    0xcea927eeU, 0xb761c935U, 0xe11ce5edU, 0x7a47b13cU,
-    0x9cd2df59U, 0x55f2733fU, 0x1814ce79U, 0x73c737bfU,
-    0x53f7cdeaU, 0x5ffdaa5bU, 0xdf3d6f14U, 0x7844db86U,
-    0xcaaff381U, 0xb968c43eU, 0x3824342cU, 0xc2a3405fU,
-    0x161dc372U, 0xbce2250cU, 0x283c498bU, 0xff0d9541U,
-    0x39a80171U, 0x080cb3deU, 0xd8b4e49cU, 0x6456c190U,
-    0x7bcb8461U, 0xd532b670U, 0x486c5c74U, 0xd0b85742U,
-};
-static const u32 Td1[256] = {
-    0x5051f4a7U, 0x537e4165U, 0xc31a17a4U, 0x963a275eU,
-    0xcb3bab6bU, 0xf11f9d45U, 0xabacfa58U, 0x934be303U,
-    0x552030faU, 0xf6ad766dU, 0x9188cc76U, 0x25f5024cU,
-    0xfc4fe5d7U, 0xd7c52acbU, 0x80263544U, 0x8fb562a3U,
-    0x49deb15aU, 0x6725ba1bU, 0x9845ea0eU, 0xe15dfec0U,
-    0x02c32f75U, 0x12814cf0U, 0xa38d4697U, 0xc66bd3f9U,
-    0xe7038f5fU, 0x9515929cU, 0xebbf6d7aU, 0xda955259U,
-    0x2dd4be83U, 0xd3587421U, 0x2949e069U, 0x448ec9c8U,
-    0x6a75c289U, 0x78f48e79U, 0x6b99583eU, 0xdd27b971U,
-    0xb6bee14fU, 0x17f088adU, 0x66c920acU, 0xb47dce3aU,
-    0x1863df4aU, 0x82e51a31U, 0x60975133U, 0x4562537fU,
-    0xe0b16477U, 0x84bb6baeU, 0x1cfe81a0U, 0x94f9082bU,
-    0x58704868U, 0x198f45fdU, 0x8794de6cU, 0xb7527bf8U,
-    0x23ab73d3U, 0xe2724b02U, 0x57e31f8fU, 0x2a6655abU,
-    0x07b2eb28U, 0x032fb5c2U, 0x9a86c57bU, 0xa5d33708U,
-    0xf2302887U, 0xb223bfa5U, 0xba02036aU, 0x5ced1682U,
-    0x2b8acf1cU, 0x92a779b4U, 0xf0f307f2U, 0xa14e69e2U,
-    0xcd65daf4U, 0xd50605beU, 0x1fd13462U, 0x8ac4a6feU,
-    0x9d342e53U, 0xa0a2f355U, 0x32058ae1U, 0x75a4f6ebU,
-    0x390b83ecU, 0xaa4060efU, 0x065e719fU, 0x51bd6e10U,
-    0xf93e218aU, 0x3d96dd06U, 0xaedd3e05U, 0x464de6bdU,
-    0xb591548dU, 0x0571c45dU, 0x6f0406d4U, 0xff605015U,
-    0x241998fbU, 0x97d6bde9U, 0xcc894043U, 0x7767d99eU,
-    0xbdb0e842U, 0x8807898bU, 0x38e7195bU, 0xdb79c8eeU,
-    0x47a17c0aU, 0xe97c420fU, 0xc9f8841eU, 0x00000000U,
-    0x83098086U, 0x48322bedU, 0xac1e1170U, 0x4e6c5a72U,
-    0xfbfd0effU, 0x560f8538U, 0x1e3daed5U, 0x27362d39U,
-    0x640a0fd9U, 0x21685ca6U, 0xd19b5b54U, 0x3a24362eU,
-    0xb10c0a67U, 0x0f9357e7U, 0xd2b4ee96U, 0x9e1b9b91U,
-    0x4f80c0c5U, 0xa261dc20U, 0x695a774bU, 0x161c121aU,
-    0x0ae293baU, 0xe5c0a02aU, 0x433c22e0U, 0x1d121b17U,
-    0x0b0e090dU, 0xadf28bc7U, 0xb92db6a8U, 0xc8141ea9U,
-    0x8557f119U, 0x4caf7507U, 0xbbee99ddU, 0xfda37f60U,
-    0x9ff70126U, 0xbc5c72f5U, 0xc544663bU, 0x345bfb7eU,
-    0x768b4329U, 0xdccb23c6U, 0x68b6edfcU, 0x63b8e4f1U,
-    0xcad731dcU, 0x10426385U, 0x40139722U, 0x2084c611U,
-    0x7d854a24U, 0xf8d2bb3dU, 0x11aef932U, 0x6dc729a1U,
-    0x4b1d9e2fU, 0xf3dcb230U, 0xec0d8652U, 0xd077c1e3U,
-    0x6c2bb316U, 0x99a970b9U, 0xfa119448U, 0x2247e964U,
-    0xc4a8fc8cU, 0x1aa0f03fU, 0xd8567d2cU, 0xef223390U,
-    0xc787494eU, 0xc1d938d1U, 0xfe8ccaa2U, 0x3698d40bU,
-    0xcfa6f581U, 0x28a57adeU, 0x26dab78eU, 0xa43fadbfU,
-    0xe42c3a9dU, 0x0d507892U, 0x9b6a5fccU, 0x62547e46U,
-    0xc2f68d13U, 0xe890d8b8U, 0x5e2e39f7U, 0xf582c3afU,
-    0xbe9f5d80U, 0x7c69d093U, 0xa96fd52dU, 0xb3cf2512U,
-    0x3bc8ac99U, 0xa710187dU, 0x6ee89c63U, 0x7bdb3bbbU,
-    0x09cd2678U, 0xf46e5918U, 0x01ec9ab7U, 0xa8834f9aU,
-    0x65e6956eU, 0x7eaaffe6U, 0x0821bccfU, 0xe6ef15e8U,
-    0xd9bae79bU, 0xce4a6f36U, 0xd4ea9f09U, 0xd629b07cU,
-    0xaf31a4b2U, 0x312a3f23U, 0x30c6a594U, 0xc035a266U,
-    0x37744ebcU, 0xa6fc82caU, 0xb0e090d0U, 0x1533a7d8U,
-    0x4af10498U, 0xf741ecdaU, 0x0e7fcd50U, 0x2f1791f6U,
-    0x8d764dd6U, 0x4d43efb0U, 0x54ccaa4dU, 0xdfe49604U,
-    0xe39ed1b5U, 0x1b4c6a88U, 0xb8c12c1fU, 0x7f466551U,
-    0x049d5eeaU, 0x5d018c35U, 0x73fa8774U, 0x2efb0b41U,
-    0x5ab3671dU, 0x5292dbd2U, 0x33e91056U, 0x136dd647U,
-    0x8c9ad761U, 0x7a37a10cU, 0x8e59f814U, 0x89eb133cU,
-    0xeecea927U, 0x35b761c9U, 0xede11ce5U, 0x3c7a47b1U,
-    0x599cd2dfU, 0x3f55f273U, 0x791814ceU, 0xbf73c737U,
-    0xea53f7cdU, 0x5b5ffdaaU, 0x14df3d6fU, 0x867844dbU,
-    0x81caaff3U, 0x3eb968c4U, 0x2c382434U, 0x5fc2a340U,
-    0x72161dc3U, 0x0cbce225U, 0x8b283c49U, 0x41ff0d95U,
-    0x7139a801U, 0xde080cb3U, 0x9cd8b4e4U, 0x906456c1U,
-    0x617bcb84U, 0x70d532b6U, 0x74486c5cU, 0x42d0b857U,
-};
-static const u32 Td2[256] = {
-    0xa75051f4U, 0x65537e41U, 0xa4c31a17U, 0x5e963a27U,
-    0x6bcb3babU, 0x45f11f9dU, 0x58abacfaU, 0x03934be3U,
-    0xfa552030U, 0x6df6ad76U, 0x769188ccU, 0x4c25f502U,
-    0xd7fc4fe5U, 0xcbd7c52aU, 0x44802635U, 0xa38fb562U,
-    0x5a49deb1U, 0x1b6725baU, 0x0e9845eaU, 0xc0e15dfeU,
-    0x7502c32fU, 0xf012814cU, 0x97a38d46U, 0xf9c66bd3U,
-    0x5fe7038fU, 0x9c951592U, 0x7aebbf6dU, 0x59da9552U,
-    0x832dd4beU, 0x21d35874U, 0x692949e0U, 0xc8448ec9U,
-    0x896a75c2U, 0x7978f48eU, 0x3e6b9958U, 0x71dd27b9U,
-    0x4fb6bee1U, 0xad17f088U, 0xac66c920U, 0x3ab47dceU,
-    0x4a1863dfU, 0x3182e51aU, 0x33609751U, 0x7f456253U,
-    0x77e0b164U, 0xae84bb6bU, 0xa01cfe81U, 0x2b94f908U,
-    0x68587048U, 0xfd198f45U, 0x6c8794deU, 0xf8b7527bU,
-    0xd323ab73U, 0x02e2724bU, 0x8f57e31fU, 0xab2a6655U,
-    0x2807b2ebU, 0xc2032fb5U, 0x7b9a86c5U, 0x08a5d337U,
-    0x87f23028U, 0xa5b223bfU, 0x6aba0203U, 0x825ced16U,
-    0x1c2b8acfU, 0xb492a779U, 0xf2f0f307U, 0xe2a14e69U,
-    0xf4cd65daU, 0xbed50605U, 0x621fd134U, 0xfe8ac4a6U,
-    0x539d342eU, 0x55a0a2f3U, 0xe132058aU, 0xeb75a4f6U,
-    0xec390b83U, 0xefaa4060U, 0x9f065e71U, 0x1051bd6eU,
-
-    0x8af93e21U, 0x063d96ddU, 0x05aedd3eU, 0xbd464de6U,
-    0x8db59154U, 0x5d0571c4U, 0xd46f0406U, 0x15ff6050U,
-    0xfb241998U, 0xe997d6bdU, 0x43cc8940U, 0x9e7767d9U,
-    0x42bdb0e8U, 0x8b880789U, 0x5b38e719U, 0xeedb79c8U,
-    0x0a47a17cU, 0x0fe97c42U, 0x1ec9f884U, 0x00000000U,
-    0x86830980U, 0xed48322bU, 0x70ac1e11U, 0x724e6c5aU,
-    0xfffbfd0eU, 0x38560f85U, 0xd51e3daeU, 0x3927362dU,
-    0xd9640a0fU, 0xa621685cU, 0x54d19b5bU, 0x2e3a2436U,
-    0x67b10c0aU, 0xe70f9357U, 0x96d2b4eeU, 0x919e1b9bU,
-    0xc54f80c0U, 0x20a261dcU, 0x4b695a77U, 0x1a161c12U,
-    0xba0ae293U, 0x2ae5c0a0U, 0xe0433c22U, 0x171d121bU,
-    0x0d0b0e09U, 0xc7adf28bU, 0xa8b92db6U, 0xa9c8141eU,
-    0x198557f1U, 0x074caf75U, 0xddbbee99U, 0x60fda37fU,
-    0x269ff701U, 0xf5bc5c72U, 0x3bc54466U, 0x7e345bfbU,
-    0x29768b43U, 0xc6dccb23U, 0xfc68b6edU, 0xf163b8e4U,
-    0xdccad731U, 0x85104263U, 0x22401397U, 0x112084c6U,
-    0x247d854aU, 0x3df8d2bbU, 0x3211aef9U, 0xa16dc729U,
-    0x2f4b1d9eU, 0x30f3dcb2U, 0x52ec0d86U, 0xe3d077c1U,
-    0x166c2bb3U, 0xb999a970U, 0x48fa1194U, 0x642247e9U,
-    0x8cc4a8fcU, 0x3f1aa0f0U, 0x2cd8567dU, 0x90ef2233U,
-    0x4ec78749U, 0xd1c1d938U, 0xa2fe8ccaU, 0x0b3698d4U,
-    0x81cfa6f5U, 0xde28a57aU, 0x8e26dab7U, 0xbfa43fadU,
-    0x9de42c3aU, 0x920d5078U, 0xcc9b6a5fU, 0x4662547eU,
-    0x13c2f68dU, 0xb8e890d8U, 0xf75e2e39U, 0xaff582c3U,
-    0x80be9f5dU, 0x937c69d0U, 0x2da96fd5U, 0x12b3cf25U,
-    0x993bc8acU, 0x7da71018U, 0x636ee89cU, 0xbb7bdb3bU,
-    0x7809cd26U, 0x18f46e59U, 0xb701ec9aU, 0x9aa8834fU,
-    0x6e65e695U, 0xe67eaaffU, 0xcf0821bcU, 0xe8e6ef15U,
-    0x9bd9bae7U, 0x36ce4a6fU, 0x09d4ea9fU, 0x7cd629b0U,
-    0xb2af31a4U, 0x23312a3fU, 0x9430c6a5U, 0x66c035a2U,
-    0xbc37744eU, 0xcaa6fc82U, 0xd0b0e090U, 0xd81533a7U,
-    0x984af104U, 0xdaf741ecU, 0x500e7fcdU, 0xf62f1791U,
-    0xd68d764dU, 0xb04d43efU, 0x4d54ccaaU, 0x04dfe496U,
-    0xb5e39ed1U, 0x881b4c6aU, 0x1fb8c12cU, 0x517f4665U,
-    0xea049d5eU, 0x355d018cU, 0x7473fa87U, 0x412efb0bU,
-    0x1d5ab367U, 0xd25292dbU, 0x5633e910U, 0x47136dd6U,
-    0x618c9ad7U, 0x0c7a37a1U, 0x148e59f8U, 0x3c89eb13U,
-    0x27eecea9U, 0xc935b761U, 0xe5ede11cU, 0xb13c7a47U,
-    0xdf599cd2U, 0x733f55f2U, 0xce791814U, 0x37bf73c7U,
-    0xcdea53f7U, 0xaa5b5ffdU, 0x6f14df3dU, 0xdb867844U,
-    0xf381caafU, 0xc43eb968U, 0x342c3824U, 0x405fc2a3U,
-    0xc372161dU, 0x250cbce2U, 0x498b283cU, 0x9541ff0dU,
-    0x017139a8U, 0xb3de080cU, 0xe49cd8b4U, 0xc1906456U,
-    0x84617bcbU, 0xb670d532U, 0x5c74486cU, 0x5742d0b8U,
-};
-static const u32 Td3[256] = {
-    0xf4a75051U, 0x4165537eU, 0x17a4c31aU, 0x275e963aU,
-    0xab6bcb3bU, 0x9d45f11fU, 0xfa58abacU, 0xe303934bU,
-    0x30fa5520U, 0x766df6adU, 0xcc769188U, 0x024c25f5U,
-    0xe5d7fc4fU, 0x2acbd7c5U, 0x35448026U, 0x62a38fb5U,
-    0xb15a49deU, 0xba1b6725U, 0xea0e9845U, 0xfec0e15dU,
-    0x2f7502c3U, 0x4cf01281U, 0x4697a38dU, 0xd3f9c66bU,
-    0x8f5fe703U, 0x929c9515U, 0x6d7aebbfU, 0x5259da95U,
-    0xbe832dd4U, 0x7421d358U, 0xe0692949U, 0xc9c8448eU,
-    0xc2896a75U, 0x8e7978f4U, 0x583e6b99U, 0xb971dd27U,
-    0xe14fb6beU, 0x88ad17f0U, 0x20ac66c9U, 0xce3ab47dU,
-    0xdf4a1863U, 0x1a3182e5U, 0x51336097U, 0x537f4562U,
-    0x6477e0b1U, 0x6bae84bbU, 0x81a01cfeU, 0x082b94f9U,
-    0x48685870U, 0x45fd198fU, 0xde6c8794U, 0x7bf8b752U,
-    0x73d323abU, 0x4b02e272U, 0x1f8f57e3U, 0x55ab2a66U,
-    0xeb2807b2U, 0xb5c2032fU, 0xc57b9a86U, 0x3708a5d3U,
-    0x2887f230U, 0xbfa5b223U, 0x036aba02U, 0x16825cedU,
-    0xcf1c2b8aU, 0x79b492a7U, 0x07f2f0f3U, 0x69e2a14eU,
-    0xdaf4cd65U, 0x05bed506U, 0x34621fd1U, 0xa6fe8ac4U,
-    0x2e539d34U, 0xf355a0a2U, 0x8ae13205U, 0xf6eb75a4U,
-    0x83ec390bU, 0x60efaa40U, 0x719f065eU, 0x6e1051bdU,
-    0x218af93eU, 0xdd063d96U, 0x3e05aeddU, 0xe6bd464dU,
-    0x548db591U, 0xc45d0571U, 0x06d46f04U, 0x5015ff60U,
-    0x98fb2419U, 0xbde997d6U, 0x4043cc89U, 0xd99e7767U,
-    0xe842bdb0U, 0x898b8807U, 0x195b38e7U, 0xc8eedb79U,
-    0x7c0a47a1U, 0x420fe97cU, 0x841ec9f8U, 0x00000000U,
-    0x80868309U, 0x2bed4832U, 0x1170ac1eU, 0x5a724e6cU,
-    0x0efffbfdU, 0x8538560fU, 0xaed51e3dU, 0x2d392736U,
-    0x0fd9640aU, 0x5ca62168U, 0x5b54d19bU, 0x362e3a24U,
-    0x0a67b10cU, 0x57e70f93U, 0xee96d2b4U, 0x9b919e1bU,
-    0xc0c54f80U, 0xdc20a261U, 0x774b695aU, 0x121a161cU,
-    0x93ba0ae2U, 0xa02ae5c0U, 0x22e0433cU, 0x1b171d12U,
-    0x090d0b0eU, 0x8bc7adf2U, 0xb6a8b92dU, 0x1ea9c814U,
-    0xf1198557U, 0x75074cafU, 0x99ddbbeeU, 0x7f60fda3U,
-    0x01269ff7U, 0x72f5bc5cU, 0x663bc544U, 0xfb7e345bU,
-    0x4329768bU, 0x23c6dccbU, 0xedfc68b6U, 0xe4f163b8U,
-    0x31dccad7U, 0x63851042U, 0x97224013U, 0xc6112084U,
-    0x4a247d85U, 0xbb3df8d2U, 0xf93211aeU, 0x29a16dc7U,
-    0x9e2f4b1dU, 0xb230f3dcU, 0x8652ec0dU, 0xc1e3d077U,
-    0xb3166c2bU, 0x70b999a9U, 0x9448fa11U, 0xe9642247U,
-    0xfc8cc4a8U, 0xf03f1aa0U, 0x7d2cd856U, 0x3390ef22U,
-    0x494ec787U, 0x38d1c1d9U, 0xcaa2fe8cU, 0xd40b3698U,
-    0xf581cfa6U, 0x7ade28a5U, 0xb78e26daU, 0xadbfa43fU,
-    0x3a9de42cU, 0x78920d50U, 0x5fcc9b6aU, 0x7e466254U,
-    0x8d13c2f6U, 0xd8b8e890U, 0x39f75e2eU, 0xc3aff582U,
-    0x5d80be9fU, 0xd0937c69U, 0xd52da96fU, 0x2512b3cfU,
-    0xac993bc8U, 0x187da710U, 0x9c636ee8U, 0x3bbb7bdbU,
-    0x267809cdU, 0x5918f46eU, 0x9ab701ecU, 0x4f9aa883U,
-    0x956e65e6U, 0xffe67eaaU, 0xbccf0821U, 0x15e8e6efU,
-    0xe79bd9baU, 0x6f36ce4aU, 0x9f09d4eaU, 0xb07cd629U,
-    0xa4b2af31U, 0x3f23312aU, 0xa59430c6U, 0xa266c035U,
-    0x4ebc3774U, 0x82caa6fcU, 0x90d0b0e0U, 0xa7d81533U,
-    0x04984af1U, 0xecdaf741U, 0xcd500e7fU, 0x91f62f17U,
-    0x4dd68d76U, 0xefb04d43U, 0xaa4d54ccU, 0x9604dfe4U,
-    0xd1b5e39eU, 0x6a881b4cU, 0x2c1fb8c1U, 0x65517f46U,
-    0x5eea049dU, 0x8c355d01U, 0x877473faU, 0x0b412efbU,
-    0x671d5ab3U, 0xdbd25292U, 0x105633e9U, 0xd647136dU,
-    0xd7618c9aU, 0xa10c7a37U, 0xf8148e59U, 0x133c89ebU,
-    0xa927eeceU, 0x61c935b7U, 0x1ce5ede1U, 0x47b13c7aU,
-    0xd2df599cU, 0xf2733f55U, 0x14ce7918U, 0xc737bf73U,
-    0xf7cdea53U, 0xfdaa5b5fU, 0x3d6f14dfU, 0x44db8678U,
-    0xaff381caU, 0x68c43eb9U, 0x24342c38U, 0xa3405fc2U,
-    0x1dc37216U, 0xe2250cbcU, 0x3c498b28U, 0x0d9541ffU,
-    0xa8017139U, 0x0cb3de08U, 0xb4e49cd8U, 0x56c19064U,
-    0xcb84617bU, 0x32b670d5U, 0x6c5c7448U, 0xb85742d0U,
-};
-static const u32 Td4[256] = {
-    0x52525252U, 0x09090909U, 0x6a6a6a6aU, 0xd5d5d5d5U,
-    0x30303030U, 0x36363636U, 0xa5a5a5a5U, 0x38383838U,
-    0xbfbfbfbfU, 0x40404040U, 0xa3a3a3a3U, 0x9e9e9e9eU,
-    0x81818181U, 0xf3f3f3f3U, 0xd7d7d7d7U, 0xfbfbfbfbU,
-    0x7c7c7c7cU, 0xe3e3e3e3U, 0x39393939U, 0x82828282U,
-    0x9b9b9b9bU, 0x2f2f2f2fU, 0xffffffffU, 0x87878787U,
-    0x34343434U, 0x8e8e8e8eU, 0x43434343U, 0x44444444U,
-    0xc4c4c4c4U, 0xdedededeU, 0xe9e9e9e9U, 0xcbcbcbcbU,
-    0x54545454U, 0x7b7b7b7bU, 0x94949494U, 0x32323232U,
-    0xa6a6a6a6U, 0xc2c2c2c2U, 0x23232323U, 0x3d3d3d3dU,
-    0xeeeeeeeeU, 0x4c4c4c4cU, 0x95959595U, 0x0b0b0b0bU,
-    0x42424242U, 0xfafafafaU, 0xc3c3c3c3U, 0x4e4e4e4eU,
-    0x08080808U, 0x2e2e2e2eU, 0xa1a1a1a1U, 0x66666666U,
-    0x28282828U, 0xd9d9d9d9U, 0x24242424U, 0xb2b2b2b2U,
-    0x76767676U, 0x5b5b5b5bU, 0xa2a2a2a2U, 0x49494949U,
-    0x6d6d6d6dU, 0x8b8b8b8bU, 0xd1d1d1d1U, 0x25252525U,
-    0x72727272U, 0xf8f8f8f8U, 0xf6f6f6f6U, 0x64646464U,
-    0x86868686U, 0x68686868U, 0x98989898U, 0x16161616U,
-    0xd4d4d4d4U, 0xa4a4a4a4U, 0x5c5c5c5cU, 0xccccccccU,
-    0x5d5d5d5dU, 0x65656565U, 0xb6b6b6b6U, 0x92929292U,
-    0x6c6c6c6cU, 0x70707070U, 0x48484848U, 0x50505050U,
-    0xfdfdfdfdU, 0xededededU, 0xb9b9b9b9U, 0xdadadadaU,
-    0x5e5e5e5eU, 0x15151515U, 0x46464646U, 0x57575757U,
-    0xa7a7a7a7U, 0x8d8d8d8dU, 0x9d9d9d9dU, 0x84848484U,
-    0x90909090U, 0xd8d8d8d8U, 0xababababU, 0x00000000U,
-    0x8c8c8c8cU, 0xbcbcbcbcU, 0xd3d3d3d3U, 0x0a0a0a0aU,
-    0xf7f7f7f7U, 0xe4e4e4e4U, 0x58585858U, 0x05050505U,
-    0xb8b8b8b8U, 0xb3b3b3b3U, 0x45454545U, 0x06060606U,
-    0xd0d0d0d0U, 0x2c2c2c2cU, 0x1e1e1e1eU, 0x8f8f8f8fU,
-    0xcacacacaU, 0x3f3f3f3fU, 0x0f0f0f0fU, 0x02020202U,
-    0xc1c1c1c1U, 0xafafafafU, 0xbdbdbdbdU, 0x03030303U,
-    0x01010101U, 0x13131313U, 0x8a8a8a8aU, 0x6b6b6b6bU,
-    0x3a3a3a3aU, 0x91919191U, 0x11111111U, 0x41414141U,
-    0x4f4f4f4fU, 0x67676767U, 0xdcdcdcdcU, 0xeaeaeaeaU,
-    0x97979797U, 0xf2f2f2f2U, 0xcfcfcfcfU, 0xcecececeU,
-    0xf0f0f0f0U, 0xb4b4b4b4U, 0xe6e6e6e6U, 0x73737373U,
-    0x96969696U, 0xacacacacU, 0x74747474U, 0x22222222U,
-    0xe7e7e7e7U, 0xadadadadU, 0x35353535U, 0x85858585U,
-    0xe2e2e2e2U, 0xf9f9f9f9U, 0x37373737U, 0xe8e8e8e8U,
-    0x1c1c1c1cU, 0x75757575U, 0xdfdfdfdfU, 0x6e6e6e6eU,
-    0x47474747U, 0xf1f1f1f1U, 0x1a1a1a1aU, 0x71717171U,
-    0x1d1d1d1dU, 0x29292929U, 0xc5c5c5c5U, 0x89898989U,
-    0x6f6f6f6fU, 0xb7b7b7b7U, 0x62626262U, 0x0e0e0e0eU,
-    0xaaaaaaaaU, 0x18181818U, 0xbebebebeU, 0x1b1b1b1bU,
-    0xfcfcfcfcU, 0x56565656U, 0x3e3e3e3eU, 0x4b4b4b4bU,
-    0xc6c6c6c6U, 0xd2d2d2d2U, 0x79797979U, 0x20202020U,
-    0x9a9a9a9aU, 0xdbdbdbdbU, 0xc0c0c0c0U, 0xfefefefeU,
-    0x78787878U, 0xcdcdcdcdU, 0x5a5a5a5aU, 0xf4f4f4f4U,
-    0x1f1f1f1fU, 0xddddddddU, 0xa8a8a8a8U, 0x33333333U,
-    0x88888888U, 0x07070707U, 0xc7c7c7c7U, 0x31313131U,
-    0xb1b1b1b1U, 0x12121212U, 0x10101010U, 0x59595959U,
-    0x27272727U, 0x80808080U, 0xececececU, 0x5f5f5f5fU,
-    0x60606060U, 0x51515151U, 0x7f7f7f7fU, 0xa9a9a9a9U,
-    0x19191919U, 0xb5b5b5b5U, 0x4a4a4a4aU, 0x0d0d0d0dU,
-    0x2d2d2d2dU, 0xe5e5e5e5U, 0x7a7a7a7aU, 0x9f9f9f9fU,
-    0x93939393U, 0xc9c9c9c9U, 0x9c9c9c9cU, 0xefefefefU,
-    0xa0a0a0a0U, 0xe0e0e0e0U, 0x3b3b3b3bU, 0x4d4d4d4dU,
-    0xaeaeaeaeU, 0x2a2a2a2aU, 0xf5f5f5f5U, 0xb0b0b0b0U,
-    0xc8c8c8c8U, 0xebebebebU, 0xbbbbbbbbU, 0x3c3c3c3cU,
-    0x83838383U, 0x53535353U, 0x99999999U, 0x61616161U,
-    0x17171717U, 0x2b2b2b2bU, 0x04040404U, 0x7e7e7e7eU,
-    0xbabababaU, 0x77777777U, 0xd6d6d6d6U, 0x26262626U,
-    0xe1e1e1e1U, 0x69696969U, 0x14141414U, 0x63636363U,
-    0x55555555U, 0x21212121U, 0x0c0c0c0cU, 0x7d7d7d7dU,
-};
-static const u32 rcon[] = {
-	0x01000000, 0x02000000, 0x04000000, 0x08000000,
-	0x10000000, 0x20000000, 0x40000000, 0x80000000,
-	0x1B000000, 0x36000000, /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */
-};
-
-
-#ifdef _MSC_VER
-#define SWAP(x) (_lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00)
-#define GETU32(p) SWAP(*((u32 *)(p)))
-#define PUTU32(ct, st) { *((u32 *)(ct)) = SWAP((st)); }
-#else
-#define GETU32(pt) (((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ ((u32)(pt)[2] <<  8) ^ ((u32)(pt)[3]))
-#define PUTU32(ct, st) { (ct)[0] = (u8)((st) >> 24); (ct)[1] = (u8)((st) >> 16); (ct)[2] = (u8)((st) >>  8); (ct)[3] = (u8)(st); }
-#endif
-
-/**
- * Expand the cipher key into the encryption key schedule.
- *
- * @return	the number of rounds for the given cipher key size.
- */
-int rijndaelKeySetupEnc(u32 rk[/*4*(Nr + 1)*/], const u8 cipherKey[], int keyBits) {
-   	int i = 0;
-	u32 temp;
-
-	rk[0] = GETU32(cipherKey     );
-	rk[1] = GETU32(cipherKey +  4);
-	rk[2] = GETU32(cipherKey +  8);
-	rk[3] = GETU32(cipherKey + 12);
-	if (keyBits == 128) {
-		for (;;) {
-			temp  = rk[3];
-			rk[4] = rk[0] ^
-				(Te4[(temp >> 16) & 0xff] & 0xff000000) ^
-				(Te4[(temp >>  8) & 0xff] & 0x00ff0000) ^
-				(Te4[(temp      ) & 0xff] & 0x0000ff00) ^
-				(Te4[(temp >> 24)       ] & 0x000000ff) ^
-				rcon[i];
-			rk[5] = rk[1] ^ rk[4];
-			rk[6] = rk[2] ^ rk[5];
-			rk[7] = rk[3] ^ rk[6];
-			if (++i == 10) {
-				return 10;
-			}
-			rk += 4;
-		}
-	}
-	rk[4] = GETU32(cipherKey + 16);
-	rk[5] = GETU32(cipherKey + 20);
-	if (keyBits == 192) {
-		for (;;) {
-			temp = rk[ 5];
-			rk[ 6] = rk[ 0] ^
-				(Te4[(temp >> 16) & 0xff] & 0xff000000) ^
-				(Te4[(temp >>  8) & 0xff] & 0x00ff0000) ^
-				(Te4[(temp      ) & 0xff] & 0x0000ff00) ^
-				(Te4[(temp >> 24)       ] & 0x000000ff) ^
-				rcon[i];
-			rk[ 7] = rk[ 1] ^ rk[ 6];
-			rk[ 8] = rk[ 2] ^ rk[ 7];
-			rk[ 9] = rk[ 3] ^ rk[ 8];
-			if (++i == 8) {
-				return 12;
-			}
-			rk[10] = rk[ 4] ^ rk[ 9];
-			rk[11] = rk[ 5] ^ rk[10];
-			rk += 6;
-		}
-	}
-	rk[6] = GETU32(cipherKey + 24);
-	rk[7] = GETU32(cipherKey + 28);
-	if (keyBits == 256) {
-        for (;;) {
-        	temp = rk[ 7];
-        	rk[ 8] = rk[ 0] ^
-        		(Te4[(temp >> 16) & 0xff] & 0xff000000) ^
-        		(Te4[(temp >>  8) & 0xff] & 0x00ff0000) ^
-        		(Te4[(temp      ) & 0xff] & 0x0000ff00) ^
-        		(Te4[(temp >> 24)       ] & 0x000000ff) ^
-        		rcon[i];
-        	rk[ 9] = rk[ 1] ^ rk[ 8];
-        	rk[10] = rk[ 2] ^ rk[ 9];
-        	rk[11] = rk[ 3] ^ rk[10];
-			if (++i == 7) {
-				return 14;
-			}
-        	temp = rk[11];
-        	rk[12] = rk[ 4] ^
-        		(Te4[(temp >> 24)       ] & 0xff000000) ^
-        		(Te4[(temp >> 16) & 0xff] & 0x00ff0000) ^
-        		(Te4[(temp >>  8) & 0xff] & 0x0000ff00) ^
-        		(Te4[(temp      ) & 0xff] & 0x000000ff);
-        	rk[13] = rk[ 5] ^ rk[12];
-        	rk[14] = rk[ 6] ^ rk[13];
-        	rk[15] = rk[ 7] ^ rk[14];
-
-			rk += 8;
-        }
-	}
-	return 0;
-}
-
-/**
- * Expand the cipher key into the decryption key schedule.
- *
- * @return	the number of rounds for the given cipher key size.
- */
-int rijndaelKeySetupDec(u32 rk[/*4*(Nr + 1)*/], const u8 cipherKey[], int keyBits) {
-	int Nr, i, j;
-	u32 temp;
-
-	/* expand the cipher key: */
-	Nr = rijndaelKeySetupEnc(rk, cipherKey, keyBits);
-	/* invert the order of the round keys: */
-	for (i = 0, j = 4*Nr; i < j; i += 4, j -= 4) {
-		temp = rk[i    ]; rk[i    ] = rk[j    ]; rk[j    ] = temp;
-		temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp;
-		temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp;
-		temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp;
-	}
-	/* apply the inverse MixColumn transform to all round keys but the first and the last: */
-	for (i = 1; i < Nr; i++) {
-		rk += 4;
-		rk[0] =
-			Td0[Te4[(rk[0] >> 24)       ] & 0xff] ^
-			Td1[Te4[(rk[0] >> 16) & 0xff] & 0xff] ^
-			Td2[Te4[(rk[0] >>  8) & 0xff] & 0xff] ^
-			Td3[Te4[(rk[0]      ) & 0xff] & 0xff];
-		rk[1] =
-			Td0[Te4[(rk[1] >> 24)       ] & 0xff] ^
-			Td1[Te4[(rk[1] >> 16) & 0xff] & 0xff] ^
-			Td2[Te4[(rk[1] >>  8) & 0xff] & 0xff] ^
-			Td3[Te4[(rk[1]      ) & 0xff] & 0xff];
-		rk[2] =
-			Td0[Te4[(rk[2] >> 24)       ] & 0xff] ^
-			Td1[Te4[(rk[2] >> 16) & 0xff] & 0xff] ^
-			Td2[Te4[(rk[2] >>  8) & 0xff] & 0xff] ^
-			Td3[Te4[(rk[2]      ) & 0xff] & 0xff];
-		rk[3] =
-			Td0[Te4[(rk[3] >> 24)       ] & 0xff] ^
-			Td1[Te4[(rk[3] >> 16) & 0xff] & 0xff] ^
-			Td2[Te4[(rk[3] >>  8) & 0xff] & 0xff] ^
-			Td3[Te4[(rk[3]      ) & 0xff] & 0xff];
-	}
-	return Nr;
-}
-
-void rijndaelEncrypt(const u32 rk[/*4*(Nr + 1)*/], int Nr, const u8 pt[16], u8 ct[16]) {
-	u32 s0, s1, s2, s3, t0, t1, t2, t3;
-#ifndef FULL_UNROLL
-    int r;
-#endif /* ?FULL_UNROLL */
-
-    /*
-	 * map byte array block to cipher state
-	 * and add initial round key:
-	 */
-	s0 = GETU32(pt     ) ^ rk[0];
-	s1 = GETU32(pt +  4) ^ rk[1];
-	s2 = GETU32(pt +  8) ^ rk[2];
-	s3 = GETU32(pt + 12) ^ rk[3];
-#ifdef FULL_UNROLL
-    /* round 1: */
-   	t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >>  8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[ 4];
-   	t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >>  8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[ 5];
-   	t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >>  8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[ 6];
-   	t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >>  8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[ 7];
-   	/* round 2: */
-   	s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >>  8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[ 8];
-   	s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >>  8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[ 9];
-   	s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >>  8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[10];
-   	s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >>  8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[11];
-    /* round 3: */
-   	t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >>  8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[12];
-   	t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >>  8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[13];
-   	t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >>  8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[14];
-   	t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >>  8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[15];
-   	/* round 4: */
-   	s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >>  8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[16];
-   	s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >>  8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[17];
-   	s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >>  8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[18];
-   	s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >>  8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[19];
-    /* round 5: */
-   	t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >>  8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[20];
-   	t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >>  8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[21];
-   	t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >>  8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[22];
-   	t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >>  8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[23];
-   	/* round 6: */
-   	s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >>  8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[24];
-   	s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >>  8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[25];
-   	s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >>  8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[26];
-   	s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >>  8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[27];
-    /* round 7: */
-   	t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >>  8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[28];
-   	t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >>  8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[29];
-   	t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >>  8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[30];
-   	t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >>  8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[31];
-   	/* round 8: */
-   	s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >>  8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[32];
-   	s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >>  8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[33];
-   	s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >>  8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[34];
-   	s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >>  8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[35];
-    /* round 9: */
-   	t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >>  8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[36];
-   	t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >>  8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[37];
-   	t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >>  8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[38];
-   	t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >>  8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[39];
-    if (Nr > 10) {
-        /* round 10: */
-        s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >>  8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[40];
-        s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >>  8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[41];
-        s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >>  8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[42];
-        s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >>  8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[43];
-        /* round 11: */
-        t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >>  8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[44];
-        t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >>  8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[45];
-        t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >>  8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[46];
-        t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >>  8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[47];
-        if (Nr > 12) {
-            /* round 12: */
-            s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >>  8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[48];
-            s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >>  8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[49];
-            s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >>  8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[50];
-            s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >>  8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[51];
-            /* round 13: */
-            t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >>  8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[52];
-            t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >>  8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[53];
-            t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >>  8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[54];
-            t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >>  8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[55];
-        }
-    }
-    rk += Nr << 2;
-#else  /* !FULL_UNROLL */
-    /*
-	 * Nr - 1 full rounds:
-	 */
-    r = Nr >> 1;
-    for (;;) {
-        t0 =
-            Te0[(s0 >> 24)       ] ^
-            Te1[(s1 >> 16) & 0xff] ^
-            Te2[(s2 >>  8) & 0xff] ^
-            Te3[(s3      ) & 0xff] ^
-            rk[4];
-        t1 =
-            Te0[(s1 >> 24)       ] ^
-            Te1[(s2 >> 16) & 0xff] ^
-            Te2[(s3 >>  8) & 0xff] ^
-            Te3[(s0      ) & 0xff] ^
-            rk[5];
-        t2 =
-            Te0[(s2 >> 24)       ] ^
-            Te1[(s3 >> 16) & 0xff] ^
-            Te2[(s0 >>  8) & 0xff] ^
-            Te3[(s1      ) & 0xff] ^
-            rk[6];
-        t3 =
-            Te0[(s3 >> 24)       ] ^
-            Te1[(s0 >> 16) & 0xff] ^
-            Te2[(s1 >>  8) & 0xff] ^
-            Te3[(s2      ) & 0xff] ^
-            rk[7];
-
-        rk += 8;
-        if (--r == 0) {
-            break;
-        }
-
-        s0 =
-            Te0[(t0 >> 24)       ] ^
-            Te1[(t1 >> 16) & 0xff] ^
-            Te2[(t2 >>  8) & 0xff] ^
-            Te3[(t3      ) & 0xff] ^
-            rk[0];
-        s1 =
-            Te0[(t1 >> 24)       ] ^
-            Te1[(t2 >> 16) & 0xff] ^
-            Te2[(t3 >>  8) & 0xff] ^
-            Te3[(t0      ) & 0xff] ^
-            rk[1];
-        s2 =
-            Te0[(t2 >> 24)       ] ^
-            Te1[(t3 >> 16) & 0xff] ^
-            Te2[(t0 >>  8) & 0xff] ^
-            Te3[(t1      ) & 0xff] ^
-            rk[2];
-        s3 =
-            Te0[(t3 >> 24)       ] ^
-            Te1[(t0 >> 16) & 0xff] ^
-            Te2[(t1 >>  8) & 0xff] ^
-            Te3[(t2      ) & 0xff] ^
-            rk[3];
-    }
-#endif /* ?FULL_UNROLL */
-    /*
-	 * apply last round and
-	 * map cipher state to byte array block:
-	 */
-	s0 =
-		(Te4[(t0 >> 24)       ] & 0xff000000) ^
-		(Te4[(t1 >> 16) & 0xff] & 0x00ff0000) ^
-		(Te4[(t2 >>  8) & 0xff] & 0x0000ff00) ^
-		(Te4[(t3      ) & 0xff] & 0x000000ff) ^
-		rk[0];
-	PUTU32(ct     , s0);
-	s1 =
-		(Te4[(t1 >> 24)       ] & 0xff000000) ^
-		(Te4[(t2 >> 16) & 0xff] & 0x00ff0000) ^
-		(Te4[(t3 >>  8) & 0xff] & 0x0000ff00) ^
-		(Te4[(t0      ) & 0xff] & 0x000000ff) ^
-		rk[1];
-	PUTU32(ct +  4, s1);
-	s2 =
-		(Te4[(t2 >> 24)       ] & 0xff000000) ^
-		(Te4[(t3 >> 16) & 0xff] & 0x00ff0000) ^
-		(Te4[(t0 >>  8) & 0xff] & 0x0000ff00) ^
-		(Te4[(t1      ) & 0xff] & 0x000000ff) ^
-		rk[2];
-	PUTU32(ct +  8, s2);
-	s3 =
-		(Te4[(t3 >> 24)       ] & 0xff000000) ^
-		(Te4[(t0 >> 16) & 0xff] & 0x00ff0000) ^
-		(Te4[(t1 >>  8) & 0xff] & 0x0000ff00) ^
-		(Te4[(t2      ) & 0xff] & 0x000000ff) ^
-		rk[3];
-	PUTU32(ct + 12, s3);
-}
-
-void rijndaelDecrypt(const u32 rk[/*4*(Nr + 1)*/], int Nr, const u8 ct[16], u8 pt[16]) {
-	u32 s0, s1, s2, s3, t0, t1, t2, t3;
-#ifndef FULL_UNROLL
-    int r;
-#endif /* ?FULL_UNROLL */
-
-    /*
-	 * map byte array block to cipher state
-	 * and add initial round key:
-	 */
-    s0 = GETU32(ct     ) ^ rk[0];
-    s1 = GETU32(ct +  4) ^ rk[1];
-    s2 = GETU32(ct +  8) ^ rk[2];
-    s3 = GETU32(ct + 12) ^ rk[3];
-#ifdef FULL_UNROLL
-    /* round 1: */
-    t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >>  8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[ 4];
-    t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >>  8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[ 5];
-    t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >>  8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[ 6];
-    t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >>  8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[ 7];
-    /* round 2: */
-    s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >>  8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[ 8];
-    s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >>  8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[ 9];
-    s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >>  8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[10];
-    s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >>  8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[11];
-    /* round 3: */
-    t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >>  8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[12];
-    t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >>  8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[13];
-    t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >>  8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[14];
-    t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >>  8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[15];
-    /* round 4: */
-    s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >>  8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[16];
-    s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >>  8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[17];
-    s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >>  8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[18];
-    s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >>  8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[19];
-    /* round 5: */
-    t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >>  8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[20];
-    t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >>  8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[21];
-    t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >>  8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[22];
-    t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >>  8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[23];
-    /* round 6: */
-    s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >>  8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[24];
-    s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >>  8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[25];
-    s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >>  8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[26];
-    s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >>  8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[27];
-    /* round 7: */
-    t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >>  8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[28];
-    t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >>  8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[29];
-    t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >>  8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[30];
-    t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >>  8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[31];
-    /* round 8: */
-    s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >>  8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[32];
-    s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >>  8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[33];
-    s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >>  8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[34];
-    s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >>  8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[35];
-    /* round 9: */
-    t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >>  8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[36];
-    t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >>  8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[37];
-    t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >>  8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[38];
-    t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >>  8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[39];
-    if (Nr > 10) {
-        /* round 10: */
-        s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >>  8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[40];
-        s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >>  8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[41];
-        s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >>  8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[42];
-        s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >>  8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[43];
-        /* round 11: */
-        t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >>  8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[44];
-        t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >>  8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[45];
-        t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >>  8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[46];
-        t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >>  8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[47];
-        if (Nr > 12) {
-            /* round 12: */
-            s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >>  8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[48];
-            s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >>  8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[49];
-            s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >>  8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[50];
-            s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >>  8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[51];
-            /* round 13: */
-            t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >>  8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[52];
-            t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >>  8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[53];
-            t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >>  8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[54];
-            t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >>  8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[55];
-        }
-    }
-	rk += Nr << 2;
-#else  /* !FULL_UNROLL */
-    /*
-     * Nr - 1 full rounds:
-     */
-    r = Nr >> 1;
-    for (;;) {
-        t0 =
-            Td0[(s0 >> 24)       ] ^
-            Td1[(s3 >> 16) & 0xff] ^
-            Td2[(s2 >>  8) & 0xff] ^
-            Td3[(s1      ) & 0xff] ^
-            rk[4];
-        t1 =
-            Td0[(s1 >> 24)       ] ^
-            Td1[(s0 >> 16) & 0xff] ^
-            Td2[(s3 >>  8) & 0xff] ^
-            Td3[(s2      ) & 0xff] ^
-            rk[5];
-        t2 =
-            Td0[(s2 >> 24)       ] ^
-            Td1[(s1 >> 16) & 0xff] ^
-            Td2[(s0 >>  8) & 0xff] ^
-            Td3[(s3      ) & 0xff] ^
-            rk[6];
-        t3 =
-            Td0[(s3 >> 24)       ] ^
-            Td1[(s2 >> 16) & 0xff] ^
-            Td2[(s1 >>  8) & 0xff] ^
-            Td3[(s0      ) & 0xff] ^
-            rk[7];
-
-        rk += 8;
-        if (--r == 0) {
-            break;
-        }
-
-        s0 =
-            Td0[(t0 >> 24)       ] ^
-            Td1[(t3 >> 16) & 0xff] ^
-            Td2[(t2 >>  8) & 0xff] ^
-            Td3[(t1      ) & 0xff] ^
-            rk[0];
-        s1 =
-            Td0[(t1 >> 24)       ] ^
-            Td1[(t0 >> 16) & 0xff] ^
-            Td2[(t3 >>  8) & 0xff] ^
-            Td3[(t2      ) & 0xff] ^
-            rk[1];
-        s2 =
-            Td0[(t2 >> 24)       ] ^
-            Td1[(t1 >> 16) & 0xff] ^
-            Td2[(t0 >>  8) & 0xff] ^
-            Td3[(t3      ) & 0xff] ^
-            rk[2];
-        s3 =
-            Td0[(t3 >> 24)       ] ^
-            Td1[(t2 >> 16) & 0xff] ^
-            Td2[(t1 >>  8) & 0xff] ^
-            Td3[(t0      ) & 0xff] ^
-            rk[3];
-    }
-#endif /* ?FULL_UNROLL */
-    /*
-	 * apply last round and
-	 * map cipher state to byte array block:
-	 */
-   	s0 =
-   		(Td4[(t0 >> 24)       ] & 0xff000000) ^
-   		(Td4[(t3 >> 16) & 0xff] & 0x00ff0000) ^
-   		(Td4[(t2 >>  8) & 0xff] & 0x0000ff00) ^
-   		(Td4[(t1      ) & 0xff] & 0x000000ff) ^
-   		rk[0];
-	PUTU32(pt     , s0);
-   	s1 =
-   		(Td4[(t1 >> 24)       ] & 0xff000000) ^
-   		(Td4[(t0 >> 16) & 0xff] & 0x00ff0000) ^
-   		(Td4[(t3 >>  8) & 0xff] & 0x0000ff00) ^
-   		(Td4[(t2      ) & 0xff] & 0x000000ff) ^
-   		rk[1];
-	PUTU32(pt +  4, s1);
-   	s2 =
-   		(Td4[(t2 >> 24)       ] & 0xff000000) ^
-   		(Td4[(t1 >> 16) & 0xff] & 0x00ff0000) ^
-   		(Td4[(t0 >>  8) & 0xff] & 0x0000ff00) ^
-   		(Td4[(t3      ) & 0xff] & 0x000000ff) ^
-   		rk[2];
-	PUTU32(pt +  8, s2);
-   	s3 =
-   		(Td4[(t3 >> 24)       ] & 0xff000000) ^
-   		(Td4[(t2 >> 16) & 0xff] & 0x00ff0000) ^
-   		(Td4[(t1 >>  8) & 0xff] & 0x0000ff00) ^
-   		(Td4[(t0      ) & 0xff] & 0x000000ff) ^
-   		rk[3];
-	PUTU32(pt + 12, s3);
-}
-
-#endif
Index: trunk/src/rijndael-api-fst.c
===================================================================
--- trunk/src/rijndael-api-fst.c	(revision 591)
+++ trunk/src/rijndael-api-fst.c	(revision 1)
@@ -1,408 +1,319 @@
-/*	$NetBSD: rijndael-api-fst.c,v 1.24 2011/05/14 16:46:55 jmmv Exp $	*/
-
-/**
- * rijndael-api-fst.c
+/*
+ * rijndael-api-fst.c   v2.3   April '2000
  *
- * @version 2.9 (December 2000)
+ * Optimised ANSI C code
  *
- * Optimised ANSI C code for the Rijndael cipher (now AES)
+ * authors: v1.0: Antoon Bosselaers
+ *          v2.0: Vincent Rijmen
+ *          v2.1: Vincent Rijmen
+ *          v2.2: Vincent Rijmen
+ *          v2.3: Paulo Barreto
+ *          v2.4: Vincent Rijmen
  *
- * @author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
- * @author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>
- * @author Paulo Barreto <paulo.barreto@terra.com.br>
- *
- * This code is hereby placed in the public domain.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
- * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * Acknowledgements:
- *
- * We are deeply indebted to the following people for their bug reports,
- * fixes, and improvement suggestions to this implementation. Though we
- * tried to list all contributions, we apologise in advance for any
- * missing reference.
- *
- * Andrew Bales <Andrew.Bales@Honeywell.com>
- * Markus Friedl <markus.friedl@informatik.uni-erlangen.de>
- * John Skodon <skodonj@webquill.com>
+ * This code is placed in the public domain.
  */
+
 #include "config_xor.h"
 
+#include <stdio.h>
+#include <assert.h>
 #include <stdlib.h>
 #include <string.h>
 
-
 #ifdef SH_ENCRYPT
 
+#include "rijndael-alg-fst.h"
 #include "rijndael-api-fst.h"
 
-static void xor16(u8 *d, const u8 *a, const u8* b)
-{
-  size_t i;
-
-	for (i = 0; i < 4; i++) {
-		*d++ = *a++ ^ *b++;
-		*d++ = *a++ ^ *b++;
-		*d++ = *a++ ^ *b++;
-		*d++ = *a++ ^ *b++;
-	}
+int makeKey(keyInstance *key, RIJ_BYTE direction, int keyLen, char *keyMaterial) {
+  word8 k[MAXKC][4];
+  int i;
+  char *keyMat;
+  
+  if (key == NULL) {
+    return BAD_KEY_INSTANCE;
+  }
+  
+  if ((direction == DIR_ENCRYPT) || (direction == DIR_DECRYPT)) {
+    key->direction = direction;
+  } else {
+    return BAD_KEY_DIR;
+  }
+  
+  if ((keyLen == 128) || (keyLen == 192) || (keyLen == 256)) { 
+    key->keyLen = keyLen;
+  } else {
+    return BAD_KEY_MAT;
+  }
+  
+  if (keyMaterial != NULL) {
+    strncpy(key->keyMaterial, keyMaterial, keyLen/4);
+  }
+  
+  key->ROUNDS = keyLen/32 + 6;
+  
+  /* initialize key schedule: */
+  keyMat = key->keyMaterial;
+#ifndef BINARY_KEY_MATERIAL
+  for (i = 0; i < key->keyLen/8; i++) {
+    int t, j;
+    
+    t = *keyMat++;
+    if ((t >= '0') && (t <= '9')) j = (t - '0') << 4;
+    else if ((t >= 'a') && (t <= 'f')) j = (t - 'a' + 10) << 4; 
+    else if ((t >= 'A') && (t <= 'F')) j = (t - 'A' + 10) << 4; 
+    else return BAD_KEY_MAT;
+    
+    t = *keyMat++;
+    if ((t >= '0') && (t <= '9')) j ^= (t - '0');
+    else if ((t >= 'a') && (t <= 'f')) j ^= (t - 'a' + 10); 
+    else if ((t >= 'A') && (t <= 'F')) j ^= (t - 'A' + 10); 
+    else return BAD_KEY_MAT;
+    
+    k[i >> 2][i & 3] = (word8)j; 
+  }
+#else
+  for (i = 0; i < key->keyLen/8; i++) {
+    k[i >> 2][i & 3] = (word8)keyMat[i]; 
+  }
+#endif /* ?BINARY_KEY_MATERIAL */
+  rijndaelKeySched(k, key->keySched, key->ROUNDS);
+  if (direction == DIR_DECRYPT) {
+    rijndaelKeyEncToDec(key->keySched, key->ROUNDS);
+  }
+  
+  return TRUE;
 }
 
-int
-rijndael_makeKey(keyInstance *key, BYTE direction, int keyLen,
-    const char *keyMaterial)
-{
-	u8 cipherKey[RIJNDAEL_MAXKB];
-	int i;
-
-	if (key == NULL) {
-		return BAD_KEY_INSTANCE;
-	}
-
-	if ((direction == DIR_ENCRYPT) || (direction == DIR_DECRYPT)) {
-		key->direction = direction;
-	} else {
-		return BAD_KEY_DIR;
-	}
-
-	if ((keyLen == 128) || (keyLen == 192) || (keyLen == 256)) {
-		key->keyLen = keyLen;
-	} else {
-		return BAD_KEY_MAT;
-	}
-
-	if (keyMaterial != NULL) {
-	        char temp[RIJNDAEL_MAX_KEY_SIZE];
-		for (i = 0; i < key->keyLen/8; i++) {
-		  int t, j;
-		  
-		  t = *keyMaterial++;
-		  if ((t >= '0') && (t <= '9')) j = (t - '0') << 4;
-		  else if ((t >= 'a') && (t <= 'f')) j = (t - 'a' + 10) << 4; 
-		  else if ((t >= 'A') && (t <= 'F')) j = (t - 'A' + 10) << 4; 
-		  else return BAD_KEY_MAT;
-		  
-		  t = *keyMaterial++;
-		  if ((t >= '0') && (t <= '9')) j ^= (t - '0');
-		  else if ((t >= 'a') && (t <= 'f')) j ^= (t - 'a' + 10); 
-		  else if ((t >= 'A') && (t <= 'F')) j ^= (t - 'A' + 10); 
-		  else return BAD_KEY_MAT;
-		  
-		  temp[i] = (u8)j; 
-		}
-
-		/* memcpy(key->keyMaterial, keyMaterial, keyLen/8); */
-		/* cppcheck-suppress uninitvar */
-		memcpy(key->keyMaterial, temp, keyLen/8);
-	}
-
-	/* initialize key schedule: */
-	memcpy(cipherKey, key->keyMaterial, keyLen/8);
-	if (direction == DIR_ENCRYPT) {
-		key->Nr = rijndaelKeySetupEnc(key->rk, cipherKey, keyLen);
-	} else {
-		key->Nr = rijndaelKeySetupDec(key->rk, cipherKey, keyLen);
-	}
-	rijndaelKeySetupEnc(key->ek, cipherKey, keyLen);
-	return TRUE;
+int cipherInit(cipherInstance *cipher, RIJ_BYTE mode, char *IV) {
+  if ((mode == MODE_ECB) || (mode == MODE_CBC) || (mode == MODE_CFB1)) {
+    cipher->mode = mode;
+  } else {
+    return BAD_CIPHER_MODE;
+  }
+  if (IV != NULL) {
+#ifndef BINARY_KEY_MATERIAL
+    int i;
+    for (i = 0; i < MAX_IV_SIZE; i++) {
+      int t, j;
+      
+      t = IV[2*i];
+      if ((t >= '0') && (t <= '9')) j = (t - '0') << 4;
+      else if ((t >= 'a') && (t <= 'f')) j = (t - 'a' + 10) << 4; 
+      else if ((t >= 'A') && (t <= 'F')) j = (t - 'A' + 10) << 4; 
+      else return BAD_CIPHER_INSTANCE;
+      
+      t = IV[2*i+1];
+      if ((t >= '0') && (t <= '9')) j ^= (t - '0');
+      else if ((t >= 'a') && (t <= 'f')) j ^= (t - 'a' + 10); 
+      else if ((t >= 'A') && (t <= 'F')) j ^= (t - 'A' + 10); 
+      else return BAD_CIPHER_INSTANCE;
+      
+      cipher->IV[i] = (word8)j;
+    }
+#else
+    memcpy(cipher->IV, IV, MAX_IV_SIZE);
+#endif /* ?BINARY_KEY_MATERIAL */
+  } else {
+    memset(cipher->IV, 0, MAX_IV_SIZE);
+  }
+  return TRUE;
 }
 
-int
-rijndael_cipherInit(cipherInstance *cipher, BYTE mode, const char *IV)
-{
-	if ((mode == MODE_ECB) || (mode == MODE_CBC) || (mode == MODE_CFB1)) {
-		cipher->mode = mode;
-	} else {
-		return BAD_CIPHER_MODE;
-	}
-	if (IV != NULL) {
-		memcpy(cipher->IV, IV, RIJNDAEL_MAX_IV_SIZE);
-	} else {
-		memset(cipher->IV, 0, RIJNDAEL_MAX_IV_SIZE);
-	}
-	return TRUE;
+int blockEncrypt(cipherInstance *cipher, keyInstance *key,
+		 RIJ_BYTE *input, int inputLen, RIJ_BYTE *outBuffer) {
+  int i, k, numBlocks;
+  word8 block[16], iv[4][4];
+  
+  if (cipher == NULL ||
+      key == NULL ||
+      key->direction == DIR_DECRYPT) {
+    return BAD_CIPHER_STATE;
+  }
+  if (input == NULL || inputLen <= 0) {
+    return 0; /* nothing to do */
+  }
+  
+  numBlocks = inputLen/128;
+  
+  switch (cipher->mode) {
+  case MODE_ECB: 
+    for (i = numBlocks; i > 0; i--) {
+      rijndaelEncrypt(input, outBuffer, key->keySched, key->ROUNDS);
+      input += 16;
+      outBuffer += 16;
+    }
+    break;
+    
+  case MODE_CBC:
+    /* fix the memory alignment for HP-UX 10.20 
+     * R. Wichmann  Mon Jun 18 22:36:55 CEST 2001
+     */
+#if STRICT_ALIGN 
+    memcpy(iv, cipher->IV, 16); 
+    ((word32*)block)[0] = ((word32*)iv)[0] ^ ((word32*)input)[0];
+    ((word32*)block)[1] = ((word32*)iv)[1] ^ ((word32*)input)[1];
+    ((word32*)block)[2] = ((word32*)iv)[2] ^ ((word32*)input)[2];
+    ((word32*)block)[3] = ((word32*)iv)[3] ^ ((word32*)input)[3];
+#else  /* !STRICT_ALIGN */
+    ((word32*)block)[0] = ((word32*)cipher->IV)[0] ^ ((word32*)input)[0];
+    ((word32*)block)[1] = ((word32*)cipher->IV)[1] ^ ((word32*)input)[1];
+    ((word32*)block)[2] = ((word32*)cipher->IV)[2] ^ ((word32*)input)[2];
+    ((word32*)block)[3] = ((word32*)cipher->IV)[3] ^ ((word32*)input)[3];
+#endif /* ?STRICT_ALIGN */
+    rijndaelEncrypt(block, outBuffer, key->keySched, key->ROUNDS);
+    input += 16;
+    for (i = numBlocks - 1; i > 0; i--) {
+      ((word32*)block)[0] = ((word32*)outBuffer)[0] ^ ((word32*)input)[0];
+      ((word32*)block)[1] = ((word32*)outBuffer)[1] ^ ((word32*)input)[1];
+      ((word32*)block)[2] = ((word32*)outBuffer)[2] ^ ((word32*)input)[2];
+      ((word32*)block)[3] = ((word32*)outBuffer)[3] ^ ((word32*)input)[3];
+      outBuffer += 16;
+      rijndaelEncrypt(block, outBuffer, key->keySched, key->ROUNDS);
+      input += 16;
+    }
+    break;
+    
+  case MODE_CFB1:
+#if STRICT_ALIGN 
+    memcpy(iv, cipher->IV, 16); 
+#else  /* !STRICT_ALIGN */
+    *((word32*)iv[0]) = *((word32*)(cipher->IV   ));
+    *((word32*)iv[1]) = *((word32*)(cipher->IV+ 4));
+    *((word32*)iv[2]) = *((word32*)(cipher->IV+ 8));
+    *((word32*)iv[3]) = *((word32*)(cipher->IV+12));
+#endif /* ?STRICT_ALIGN */
+    for (i = numBlocks; i > 0; i--) {
+      for (k = 0; k < 128; k++) {
+	*((word32*) block    ) = *((word32*)iv[0]);
+	*((word32*)(block+ 4)) = *((word32*)iv[1]);
+	*((word32*)(block+ 8)) = *((word32*)iv[2]);
+	*((word32*)(block+12)) = *((word32*)iv[3]);
+	rijndaelEncrypt(block, block, key->keySched, key->ROUNDS);
+	outBuffer[k/8] ^= (block[0] & 0x80) >> (k & 7);
+	iv[0][0] = (iv[0][0] << 1) | (iv[0][1] >> 7);
+	iv[0][1] = (iv[0][1] << 1) | (iv[0][2] >> 7);
+	iv[0][2] = (iv[0][2] << 1) | (iv[0][3] >> 7);
+	iv[0][3] = (iv[0][3] << 1) | (iv[1][0] >> 7);
+	iv[1][0] = (iv[1][0] << 1) | (iv[1][1] >> 7);
+	iv[1][1] = (iv[1][1] << 1) | (iv[1][2] >> 7);
+	iv[1][2] = (iv[1][2] << 1) | (iv[1][3] >> 7);
+	iv[1][3] = (iv[1][3] << 1) | (iv[2][0] >> 7);
+	iv[2][0] = (iv[2][0] << 1) | (iv[2][1] >> 7);
+	iv[2][1] = (iv[2][1] << 1) | (iv[2][2] >> 7);
+	iv[2][2] = (iv[2][2] << 1) | (iv[2][3] >> 7);
+	iv[2][3] = (iv[2][3] << 1) | (iv[3][0] >> 7);
+	iv[3][0] = (iv[3][0] << 1) | (iv[3][1] >> 7);
+	iv[3][1] = (iv[3][1] << 1) | (iv[3][2] >> 7);
+	iv[3][2] = (iv[3][2] << 1) | (iv[3][3] >> 7);
+	iv[3][3] = (iv[3][3] << 1) | ((outBuffer[k/8] >> (7-(k&7))) & 1);
+      }
+    }
+    break;
+    
+  default:
+    return BAD_CIPHER_STATE;
+  }
+  
+  return 128*numBlocks;
 }
 
-int
-rijndael_blockEncrypt(cipherInstance *cipher, keyInstance *key,
-    const BYTE *input, int inputLen, BYTE *outBuffer)
-{
-	int i, k, t, numBlocks;
-	u8 block[16], *iv;
-
-	if (cipher == NULL ||
-		key == NULL ||
-		key->direction == DIR_DECRYPT) {
-		return BAD_CIPHER_STATE;
-	}
-	if (input == NULL || inputLen <= 0) {
-		return 0; /* nothing to do */
-	}
-
-	numBlocks = inputLen/128;
-
-	switch (cipher->mode) {
-	case MODE_ECB:
-		for (i = numBlocks; i > 0; i--) {
-			rijndaelEncrypt(key->rk, key->Nr, input, outBuffer);
-			input += 16;
-			outBuffer += 16;
-		}
-		break;
-
-	case MODE_CBC:
-		iv = (u8 *)cipher->IV;
-		for (i = numBlocks; i > 0; i--) {
-			xor16(block, input, iv);
-			rijndaelEncrypt(key->rk, key->Nr, block, outBuffer);
-			iv = outBuffer;
-			input += 16;
-			outBuffer += 16;
-		}
-		break;
-
-	case MODE_CFB1:
-		iv = (u8 *)cipher->IV;
-		for (i = numBlocks; i > 0; i--) {
-			memcpy(outBuffer, input, 16);
-			for (k = 0; k < 128; k++) {
-				rijndaelEncrypt(key->ek, key->Nr, iv, block);
-				outBuffer[k >> 3] ^=
-				    (block[0] & 0x80U) >> (k & 7);
-				for (t = 0; t < 15; t++) {
-					iv[t] = (iv[t] << 1) | (iv[t + 1] >> 7);
-				}
-				iv[15] = (iv[15] << 1) |
-				    ((outBuffer[k >> 3] >> (7 - (k & 7))) & 1);
-			}
-			outBuffer += 16;
-			input += 16;
-		}
-		break;
-
-	default:
-		return BAD_CIPHER_STATE;
-	}
-
-	return 128 * numBlocks;
+int blockDecrypt(cipherInstance *cipher, keyInstance *key,
+		 RIJ_BYTE *input, int inputLen, RIJ_BYTE *outBuffer) {
+  int i, k, numBlocks;
+  word8 block[16], iv[4][4];
+  
+  if (cipher == NULL ||
+      key == NULL ||
+      ((cipher->mode != MODE_CFB1) && (key->direction == DIR_ENCRYPT))) {
+    return BAD_CIPHER_STATE;
+  }
+  if (input == NULL || inputLen <= 0) {
+    return 0; /* nothing to do */
+  }
+  
+  numBlocks = inputLen/128;
+  
+  switch (cipher->mode) {
+  case MODE_ECB: 
+    for (i = numBlocks; i > 0; i--) { 
+      rijndaelDecrypt(input, outBuffer, key->keySched, key->ROUNDS);
+      input += 16;
+      outBuffer += 16;
+    }
+    break;
+    
+  case MODE_CBC:
+#if STRICT_ALIGN 
+    memcpy(iv, cipher->IV, 16); 
+#else
+    *((word32*)iv[0]) = *((word32*)(cipher->IV   ));
+    *((word32*)iv[1]) = *((word32*)(cipher->IV+ 4));
+    *((word32*)iv[2]) = *((word32*)(cipher->IV+ 8));
+    *((word32*)iv[3]) = *((word32*)(cipher->IV+12));
+#endif
+    for (i = numBlocks; i > 0; i--) {
+      rijndaelDecrypt(input, block, key->keySched, key->ROUNDS);
+      ((word32*)block)[0] ^= *((word32*)iv[0]);
+      ((word32*)block)[1] ^= *((word32*)iv[1]);
+      ((word32*)block)[2] ^= *((word32*)iv[2]);
+      ((word32*)block)[3] ^= *((word32*)iv[3]);
+#if STRICT_ALIGN
+      memcpy(iv, input, 16);
+      memcpy(outBuffer, block, 16);
+#else
+      *((word32*)iv[0]) = ((word32*)input)[0]; ((word32*)outBuffer)[0] = ((word32*)block)[0];
+      *((word32*)iv[1]) = ((word32*)input)[1]; ((word32*)outBuffer)[1] = ((word32*)block)[1];
+      *((word32*)iv[2]) = ((word32*)input)[2]; ((word32*)outBuffer)[2] = ((word32*)block)[2];
+      *((word32*)iv[3]) = ((word32*)input)[3]; ((word32*)outBuffer)[3] = ((word32*)block)[3];
+#endif
+      input += 16;
+      outBuffer += 16;
+    }
+    break;
+    
+  case MODE_CFB1:
+#if STRICT_ALIGN 
+    memcpy(iv, cipher->IV, 16); 
+#else
+    *((word32*)iv[0]) = *((word32*)(cipher->IV));
+    *((word32*)iv[1]) = *((word32*)(cipher->IV+ 4));
+    *((word32*)iv[2]) = *((word32*)(cipher->IV+ 8));
+    *((word32*)iv[3]) = *((word32*)(cipher->IV+12));
+#endif
+    for (i = numBlocks; i > 0; i--) {
+      for (k = 0; k < 128; k++) {
+	*((word32*) block    ) = *((word32*)iv[0]);
+	*((word32*)(block+ 4)) = *((word32*)iv[1]);
+	*((word32*)(block+ 8)) = *((word32*)iv[2]);
+	*((word32*)(block+12)) = *((word32*)iv[3]);
+	rijndaelEncrypt(block, block, key->keySched, key->ROUNDS);
+	iv[0][0] = (iv[0][0] << 1) | (iv[0][1] >> 7);
+	iv[0][1] = (iv[0][1] << 1) | (iv[0][2] >> 7);
+	iv[0][2] = (iv[0][2] << 1) | (iv[0][3] >> 7);
+	iv[0][3] = (iv[0][3] << 1) | (iv[1][0] >> 7);
+	iv[1][0] = (iv[1][0] << 1) | (iv[1][1] >> 7);
+	iv[1][1] = (iv[1][1] << 1) | (iv[1][2] >> 7);
+	iv[1][2] = (iv[1][2] << 1) | (iv[1][3] >> 7);
+	iv[1][3] = (iv[1][3] << 1) | (iv[2][0] >> 7);
+	iv[2][0] = (iv[2][0] << 1) | (iv[2][1] >> 7);
+	iv[2][1] = (iv[2][1] << 1) | (iv[2][2] >> 7);
+	iv[2][2] = (iv[2][2] << 1) | (iv[2][3] >> 7);
+	iv[2][3] = (iv[2][3] << 1) | (iv[3][0] >> 7);
+	iv[3][0] = (iv[3][0] << 1) | (iv[3][1] >> 7);
+	iv[3][1] = (iv[3][1] << 1) | (iv[3][2] >> 7);
+	iv[3][2] = (iv[3][2] << 1) | (iv[3][3] >> 7);
+	iv[3][3] = (iv[3][3] << 1) | ((input[k/8] >> (7-(k&7))) & 1);
+	outBuffer[k/8] ^= (block[0] & 0x80) >> (k & 7);
+      }
+    }
+    break;
+    
+  default:
+    return BAD_CIPHER_STATE;
+  }
+  
+  return 128*numBlocks;
 }
 
-/**
- * Encrypt data partitioned in octets, using RFC 2040-like padding.
- *
- * @param   input           data to be encrypted (octet sequence)
- * @param   inputOctets		input length in octets (not bits)
- * @param   outBuffer       encrypted output data
- *
- * @return	length in octets (not bits) of the encrypted output buffer.
- */
-int
-rijndael_padEncrypt(cipherInstance *cipher, keyInstance *key,
-    const BYTE *input, int inputOctets, BYTE *outBuffer)
-{
-	int i, numBlocks, padLen;
-	u8 block[16], *iv;
-
-	if (cipher == NULL ||
-		key == NULL ||
-		key->direction == DIR_DECRYPT) {
-		return BAD_CIPHER_STATE;
-	}
-	if (input == NULL || inputOctets <= 0) {
-		return 0; /* nothing to do */
-	}
-
-	numBlocks = inputOctets / 16;
-
-	switch (cipher->mode) {
-	case MODE_ECB:
-		for (i = numBlocks; i > 0; i--) {
-			rijndaelEncrypt(key->rk, key->Nr, input, outBuffer);
-			input += 16;
-			outBuffer += 16;
-		}
-		padLen = 16 - (inputOctets - 16*numBlocks);
-		memcpy(block, input, 16 - padLen);
-		memset(block + 16 - padLen, padLen, padLen);
-		rijndaelEncrypt(key->rk, key->Nr, block, outBuffer);
-		break;
-
-	case MODE_CBC:
-		iv = (u8 *)cipher->IV;
-		for (i = numBlocks; i > 0; i--) {
-			xor16(block, input, iv);
-			rijndaelEncrypt(key->rk, key->Nr, block, outBuffer);
-			iv = outBuffer;
-			input += 16;
-			outBuffer += 16;
-		}
-		padLen = 16 - (inputOctets - 16*numBlocks);
-		for (i = 0; i < 16 - padLen; i++) {
-			block[i] = input[i] ^ iv[i];
-		}
-		for (i = 16 - padLen; i < 16; i++) {
-			block[i] = (BYTE)padLen ^ iv[i];
-		}
-		rijndaelEncrypt(key->rk, key->Nr, block, outBuffer);
-		break;
-
-	default:
-		return BAD_CIPHER_STATE;
-	}
-
-	return 16 * (numBlocks + 1);
-}
-
-int
-rijndael_blockDecrypt(cipherInstance *cipher, keyInstance *key,
-    const BYTE *input, int inputLen, BYTE *outBuffer)
-{
-	int i, k, t, numBlocks;
-	u8 block[16], *iv;
-
-	if (cipher == NULL ||
-		key == NULL ||
-		(cipher->mode != MODE_CFB1 && key->direction == DIR_ENCRYPT)) {
-		return BAD_CIPHER_STATE;
-	}
-	if (input == NULL || inputLen <= 0) {
-		return 0; /* nothing to do */
-	}
-
-	numBlocks = inputLen/128;
-
-	switch (cipher->mode) {
-	case MODE_ECB:
-		for (i = numBlocks; i > 0; i--) {
-			rijndaelDecrypt(key->rk, key->Nr, input, outBuffer);
-			input += 16;
-			outBuffer += 16;
-		}
-		break;
-
-	case MODE_CBC:
-		iv = (u8 *)cipher->IV;
-		for (i = numBlocks; i > 0; i--) {
-			rijndaelDecrypt(key->rk, key->Nr, input, block);
-			xor16(block, block, iv);
-			if (numBlocks > 1)
-			  memcpy(cipher->IV, input, 16);
-			memcpy(outBuffer, block, 16);
-			input += 16;
-			outBuffer += 16;
-		}
-		break;
-
-    case MODE_CFB1:
-		iv = (u8 *)cipher->IV;
-		for (i = numBlocks; i > 0; i--) {
-			memcpy(outBuffer, input, 16);
-			for (k = 0; k < 128; k++) {
-				rijndaelEncrypt(key->ek, key->Nr, iv, block);
-				for (t = 0; t < 15; t++) {
-					iv[t] = (iv[t] << 1) | (iv[t + 1] >> 7);
-				}
-				iv[15] = (iv[15] << 1) |
-				    ((input[k >> 3] >> (7 - (k & 7))) & 1);
-				outBuffer[k >> 3] ^= (block[0] & 0x80U) >>
-				    (k & 7);
-			}
-			outBuffer += 16;
-			input += 16;
-		}
-		break;
-
-	default:
-		return BAD_CIPHER_STATE;
-	}
-
-	return 128 * numBlocks;
-}
-
-int
-rijndael_padDecrypt(cipherInstance *cipher, keyInstance *key,
-    const BYTE *input, int inputOctets, BYTE *outBuffer)
-{
-	int i, numBlocks, padLen;
-	u8 block[16], *iv;
-
-	if (cipher == NULL ||
-		key == NULL ||
-		key->direction == DIR_ENCRYPT) {
-		return BAD_CIPHER_STATE;
-	}
-	if (input == NULL || inputOctets <= 0) {
-		return 0; /* nothing to do */
-	}
-	if (inputOctets % 16 != 0) {
-		return BAD_DATA;
-	}
-
-	numBlocks = inputOctets/16;
-
-	switch (cipher->mode) {
-	case MODE_ECB:
-		/* all blocks but last */
-		for (i = numBlocks - 1; i > 0; i--) {
-			rijndaelDecrypt(key->rk, key->Nr, input, outBuffer);
-			input += 16;
-			outBuffer += 16;
-		}
-		/* last block */
-		rijndaelDecrypt(key->rk, key->Nr, input, block);
-		padLen = block[15];
-		if (padLen >= 16) {
-			return BAD_DATA;
-		}
-		for (i = 16 - padLen; i < 16; i++) {
-			if (block[i] != padLen) {
-				return BAD_DATA;
-			}
-		}
-		memcpy(outBuffer, block, 16 - padLen);
-		break;
-
-	case MODE_CBC:
-		iv = (u8 *)cipher->IV;
-		/* all blocks but last */
-		for (i = numBlocks - 1; i > 0; i--) {
-			rijndaelDecrypt(key->rk, key->Nr, input, block);
-			xor16(block, block, iv);
-			memcpy(cipher->IV, input, 16);
-			memcpy(outBuffer, block, 16);
-			input += 16;
-			outBuffer += 16;
-		}
-		/* last block */
-		rijndaelDecrypt(key->rk, key->Nr, input, block);
-		xor16(block, block, iv);
-		padLen = block[15];
-		if (padLen <= 0 || padLen > 16) {
-			return BAD_DATA;
-		}
-		for (i = 16 - padLen; i < 16; i++) {
-			if (block[i] != padLen) {
-				return BAD_DATA;
-			}
-		}
-		memcpy(outBuffer, block, 16 - padLen);
-		break;
-
-	default:
-		return BAD_CIPHER_STATE;
-	}
-
-	return 16 * numBlocks - padLen;
-}
-
 #endif
Index: trunk/src/samhain.c
===================================================================
--- trunk/src/samhain.c	(revision 591)
+++ trunk/src/samhain.c	(revision 1)
@@ -34,8 +34,15 @@
 #include <errno.h>
 
+
+#if TIME_WITH_SYS_TIME
+#include <sys/time.h>
+#include <time.h>
+#else
 #if HAVE_SYS_TIME_H
 #include <sys/time.h>
-#endif
+#else
 #include <time.h>
+#endif
+#endif
 
 #ifdef HAVE_MEMORY_H
@@ -56,21 +63,18 @@
 
 #include "samhain.h"
-#include "sh_pthread.h"
+#include "sh_files.h"
 #include "sh_utils.h"
 #include "sh_error.h"
 #include "sh_unix.h"
-#include "sh_files.h"
 #include "sh_getopt.h"
 #include "sh_readconf.h"
 #include "sh_hash.h"
-#include "sh_dbIO.h"
-#include "sh_restrict.h"
-
-#include "sh_nmail.h"
+
+#include "sh_mail.h"
 
 #include "sh_tiger.h"
-#include "sh_sig.h"
+#include "sh_gpg.h"
 #include "sh_mem.h"
-#include "sh_xfer.h"
+#include "sh_forward.h"
 #include "sh_tools.h"
 #include "sh_hash.h"
@@ -82,5 +86,4 @@
 #include "sh_ignore.h"
 #include "sh_prelink.h"
-#include "sh_sem.h"
 #endif
 
@@ -110,12 +113,8 @@
 volatile  int      sh_global_suspend_flag;
 volatile  int      sig_fresh_trail;        /* SIGIOT  */
-volatile  int      sh_thread_pause_flag = S_FALSE;
 volatile  int      sig_config_read_again;  /* SIGHUP  */
 volatile  int      sig_terminate;          /* SIGQUIT */
 volatile  int      sig_termfast;           /* SIGTERM */
 volatile  int      sig_force_check;        /* SIGTTOU */
-volatile  int      sig_force_silent;       /* SIGTSTP */
-volatile  int      sh_global_check_silent;
-volatile  int      sh_load_delta_flag;
 long int           eintr__result;
 char               sh_sig_msg[SH_MINIBUF];
@@ -138,60 +137,4 @@
 #endif
 
-#ifdef HAVE_PTHREAD
-struct gt {
-  size_t g_count;
-  char * g_glob;
-};
-
-pthread_key_t g_key;
-
-int sh_g_thread()
-{
-  struct gt * ptr = calloc(1,sizeof(struct gt));
-  if (!ptr)
-    return -1;
-  ptr->g_count    = 0;
-  ptr->g_glob     = calloc(1, SH_MAX_GLOBS * (GLOB_LEN+1));
-  if (!(ptr->g_glob))
-    {
-      free(ptr);
-      return -1;
-    }
-  return pthread_setspecific(g_key, ptr);
-}
-
-void sh_g_destroy(void * data)
-{
-  struct gt * ptr = (struct gt *) data;
-  free(ptr->g_glob);
-  free(ptr);
-  return;
-}
-
-void sh_g_init(void)
-{
-#if !defined(USE_SYSTEM_MALLOC) && defined(USE_MALLOC_LOCK)
-  extern int dnmalloc_pthread_init(void);
-  dnmalloc_pthread_init();
-#endif
-
-  if (0 != pthread_key_create(&g_key, sh_g_destroy))
-    {
-      perror("1");
-      exit(EXIT_FAILURE);
-    }
-
-  if (0 != sh_g_thread())
-    {
-      perror("2");
-      exit(EXIT_FAILURE);
-    }
-  return;
-}
-#define SH_G_INIT sh_g_init()
-#else
-#define SH_G_INIT ((void)0)
-#endif
-
 char * globber(const char * str)
 {
@@ -199,24 +142,14 @@
   size_t j;
 
-#ifndef HAVE_PTHREAD
+  static   size_t  items = 0;
   static   size_t  count = 0;
   static   char glob[SH_MAX_GLOBS * (GLOB_LEN+1)];
-#else
-  struct gt * ptr = pthread_getspecific(g_key);
-  size_t count;
-  char *  glob;
-
-  if (ptr) {
-    count = ptr->g_count;
-    glob  = ptr->g_glob;
-  } else {
+
+  if (str == NULL)
     return NULL;
-  }
-#endif
-
-  if (str != NULL)
+  else
     j = strlen(str);
-  else
-    return NULL;
+
+  ++items;
 
   ASSERT((j <= GLOB_LEN), _("j <= GLOB_LEN"))
@@ -230,4 +163,5 @@
     {
       count = 0;
+      items = 0;
     }
 
@@ -242,9 +176,5 @@
 
   i     = count;
-#ifdef HAVE_PTHREAD
-  ptr->g_count = count + j + 1;
-#else
   count = count + j + 1;
-#endif
   return &glob[i];
 }
@@ -263,7 +193,4 @@
 }
 
-#else
-/* not stealth */
-#define SH_G_INIT ((void)0)
 #endif
 
@@ -311,7 +238,4 @@
   ErrFlag[0] |= (1 << 11);
 #endif
-
-  sh.delayload = 0;
-
 #ifdef MKA_13
   ErrFlag[0] |= (1 << 12);
@@ -338,12 +262,5 @@
   sig_termfast           = 0;           /* SIGTERM */
   sig_force_check        = 0;           /* SIGTTOU */
-  sig_force_silent       = 0;           /* SIGTSTP */
-  sh_global_check_silent = 0;
-  sh_load_delta_flag     = 0;
-  sh_sig_msg[4] = '\0';
-  sh_sig_msg[3] = 'e';
-  sh_sig_msg[2] = 'n';
-  sh_sig_msg[1] = 'o';
-  sh_sig_msg[0] = 'N';
+  strcpy ( sh_sig_msg, _("None"));
 
 #ifdef MKB_01
@@ -380,8 +297,4 @@
 #endif
 
-  sh.pid = (UINT64) getpid();
-
-  sh.outpath = NULL;
-
   /* The flags.
    */
@@ -390,5 +303,4 @@
   sh.flag.update          = S_FALSE;
   sh.flag.opts            = S_FALSE;
-  sh.flag.started         = S_FALSE;
   if (is_samhainctl_init == S_FALSE)
     sh.flag.isdaemon        = S_FALSE;
@@ -406,5 +318,4 @@
   sh.flag.hidefile        = S_FALSE;
   sh.flag.loop            = S_FALSE;
-  sh.flag.inotify         = 0;
 
 #ifdef MKB_09
@@ -435,10 +346,6 @@
   /* The stats.
    */
-  sh.statistics.bytes_speed   = 0;
-  sh.statistics.bytes_hashed  = 0;
-  sh.statistics.files_report  = 0;
-  sh.statistics.files_error   = 0;
-  sh.statistics.files_nodir   = 0;
-
+  sh.statistics.bytes_speed  = 0;
+  sh.statistics.bytes_hashed = 0;
   sh.statistics.mail_success = 0;
   sh.statistics.mail_failed  = 0;
@@ -475,5 +382,5 @@
    */
   (void) sl_strlcpy (sh.host.name,  _("localhost"),  SH_MINIBUF);
-  sh.host.system[0]     = '\0'; /* flawfinder: ignore *//* ff bug */
+  sh.host.system[0]     = '\0';
   sh.host.release[0]    = '\0';
   sh.host.machine[0]    = '\0';
@@ -541,26 +448,14 @@
    */
 #if defined(SH_WITH_MAIL)
-  if (0 != strcmp (DEFAULT_MAILADDRESS, _("NULL")))
-    {
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
-      char * saveptr;
-      (void) sl_strncpy(q, DEFAULT_MAILADDRESS, SH_PATHBUF);
-      p = strtok_r (q, ", \t", &saveptr);
-      if (p)
-	{
-	  (void) sh_nmail_add_compiled_recipient (p);
-	  while (NULL != (p = strtok_r (NULL, ", \t", &saveptr)))
-	    (void) sh_nmail_add_compiled_recipient (p);
-	}
-#else
+  if (0 == strcmp (DEFAULT_MAILADDRESS, _("NULL")))
+    {
       (void) sl_strncpy(q, DEFAULT_MAILADDRESS, SH_PATHBUF);
       p = strtok (q, ", \t");
       if (p)
 	{
-	  (void) sh_nmail_add_compiled_recipient (p);
+	  (void) sh_mail_setaddress_int (p);
 	  while (NULL != (p = strtok (NULL, ", \t")))
-	    (void) sh_nmail_add_compiled_recipient (p);
-	}
-#endif
+	    (void) sh_mail_setaddress_int (p);
+	}
     }
 #endif
@@ -647,54 +542,48 @@
   sh.looptime     = 60;
 
-#ifdef SCREW_IT_UP
-  sh.sigtrap_max_duration = 500000; /* 500ms */
-#endif
-
   /* The struct to hold privileged information.
    */
-  skey = calloc(1,sizeof(sh_key_t));
-  if (skey != NULL)
-    {
-
-      skey->mlock_failed = S_FALSE;
-      skey->rngI         = BAD;
-      /* properly initialized later 
-       */
-      skey->rng0[0] = 0x03; skey->rng0[1] = 0x09; skey->rng0[2] = 0x17;
-      skey->rng1[0] = 0x03; skey->rng1[1] = 0x09; skey->rng1[2] = 0x17;
-      skey->rng2[0] = 0x03; skey->rng2[1] = 0x09; skey->rng2[2] = 0x17;
-      
-      for (i = 0; i < KEY_BYT; ++i)
-	skey->poolv[i] = '\0';
-      
-      skey->poolc        = 0;
-      
-      skey->ErrFlag[0]   = ErrFlag[0];
-      ErrFlag[0]         = 0;
-      skey->ErrFlag[1]   = ErrFlag[1];
-      ErrFlag[1]         = 0;
-      
-      dez = &(TcpFlag[POS_TF-1][0]);
-      for (i = 0; i < PW_LEN; ++i)
-	{ 
-	  skey->pw[i] = (char) (*dez); 
-	  (*dez)      = '\0';
-	  ++dez; 
-	}
-      
-      skey->sh_sockpass[0]  = '\0';
-      skey->sigkey_old[0]   = '\0';
-      skey->sigkey_new[0]   = '\0';
-      skey->mailkey_old[0]  = '\0';
-      skey->mailkey_new[0]  = '\0';
-      skey->crypt[0]        = '\0'; /* flawfinder: ignore *//* ff bug */
-      skey->session[0]      = '\0';
-      skey->vernam[0]       = '\0';
-    }
-  else
+  skey = (sh_key_t *) malloc (sizeof(sh_key_t));
+  if (skey == NULL) 
     {
       perror(_("sh_init"));
       _exit (EXIT_FAILURE);
     }
+
+  skey->mlock_failed = SL_FALSE;
+  skey->rngI         = BAD;
+  /* properly initialized later 
+   */
+  skey->rng0[0] = 0x03; skey->rng0[1] = 0x09; skey->rng0[2] = 0x17;
+  skey->rng1[0] = 0x03; skey->rng1[1] = 0x09; skey->rng1[2] = 0x17;
+  skey->rng2[0] = 0x03; skey->rng2[1] = 0x09; skey->rng2[2] = 0x17;
+
+  for (i = 0; i < KEY_BYT; ++i)
+    skey->poolv[i] = '\0';
+
+  skey->poolc        = 0;
+
+  skey->ErrFlag[0]   = ErrFlag[0];
+  ErrFlag[0]         = 0;
+  skey->ErrFlag[1]   = ErrFlag[1];
+  ErrFlag[1]         = 0;
+
+  dez = &(TcpFlag[POS_TF-1][0]);
+  for (i = 0; i < PW_LEN; ++i)
+    { 
+       skey->pw[i] = (char) (*dez); 
+      (*dez)      = '\0';
+      ++dez; 
+    }
+
+  skey->sh_sockpass[0]  = '\0';
+  skey->sigkey_old[0]   = '\0';
+  skey->sigkey_new[0]   = '\0';
+  skey->mailkey_old[0]  = '\0';
+  skey->mailkey_new[0]  = '\0';
+  skey->crypt[0]        = '\0';
+  skey->session[0]      = '\0';
+  skey->vernam[0]       = '\0';
+
 
   sh_unix_memlock();
@@ -724,7 +613,8 @@
 #endif
 #if defined(SH_WITH_SERVER)
-  extern int sh_socket_remove (void);
-  extern int sh_html_zero();
-#endif
+  extern int sh_socket_remove ();
+#endif
+
+  SL_ENTER(_("exit_handler"));
 
 #if defined(SH_WITH_SERVER)
@@ -735,10 +625,7 @@
   for (modnum = 0; modList[modnum].name != NULL; ++modnum) 
     {
-      if (modList[modnum].initval == SH_MOD_ACTIVE)
+      if (modList[modnum].initval == GOOD)
 	(void) modList[modnum].mod_cleanup();
     }
-#ifdef HAVE_PTHREAD
-  sh_pthread_cancel_all();
-#endif
 #endif
 
@@ -748,5 +635,5 @@
   if (sh.mailNum.alarm_last > 0) 
     {
-      (void) sh_nmail_flush ();
+      (void) sh_mail_msg (NULL);
     }
 #endif
@@ -755,8 +642,5 @@
    */
 #if defined(SH_WITH_SERVER)
-  /* zero out the status file at exit, such that the status
-   * of client becomes unknown in the beltane interface
-   */
-  sh_html_zero();
+  sh_forward_html_write();
 #endif
 
@@ -767,27 +651,10 @@
   sh_files_deldirstack ();
   sh_files_delfilestack ();
-  sh_files_delglobstack ();
   sh_hash_hashdelete();
-  sh_files_hle_reg (NULL);
-  (void) sh_ignore_clean ();
-  /*
-   * Only flush on exit if running as deamon.
-   * Otherwise we couldn't run another instance
-   * while the deamon is running (would leave the
-   * deamon with flushed ruleset).
-   */
-  if (sh.flag.isdaemon == S_TRUE)
-    {
-      sh_audit_delete_all ();
-    }
 #endif
 #if defined(SH_WITH_SERVER)
-  sh_xfer_free_all ();
-#endif
-#if defined(SH_WITH_MAIL)
-  sh_nmail_free();
+  sh_forward_free_all ();
 #endif
   delete_cache();
-  sh_userid_destroy ();
   sh_mem_stat();
 #endif
@@ -799,19 +666,9 @@
   /* --- Checksum of executable. ---
    */
-  {
-    volatile  int sig_store = sig_termfast;
-
-    sig_termfast = 0;
-    (void) sh_unix_self_check();
-    sig_termfast = sig_store;
-  }
+  (void) sh_unix_self_check();
 
 
   /* --- Exit Message. ---
    */
-  if (sh.flag.exit == EXIT_FAILURE && !strcmp(sh_sig_msg, _("None")))
-    sl_strlcpy(sh_sig_msg, _("exit_failure"), SH_MINIBUF);
-  if (sh.flag.exit == EXIT_SUCCESS && !strcmp(sh_sig_msg, _("None")))
-    sl_strlcpy(sh_sig_msg, _("exit_success"), SH_MINIBUF);
   sh_error_handle ((-1), FIL__, __LINE__, sh.flag.exit, MSG_EXIT_NORMAL, 
 		   sh.prg_name, sh_sig_msg);
@@ -823,14 +680,16 @@
   /* --- Restrict error logging to stderr. ---
    */
+#ifdef WITH_MESSAGE_QUEUE
   close_ipc ();
+#endif
   sh_error_only_stderr (S_TRUE);
 
+
   /* --- Remove lock, delete critical information. ---
    */
   (void) sh_unix_rm_lock_file (sh.srvlog.name);
-  if (sh.flag.isdaemon == S_TRUE)
-    (void) sh_unix_rm_pid_file ();
+  (void) sh_unix_rm_pid_file ();
   if (skey != NULL)
-    memset (skey, 0, sizeof(sh_key_t));
+    memset (skey, (int) '\0', sizeof(sh_key_t));
   
   /* --- Exit. ---
@@ -854,38 +713,26 @@
 #if defined(__linux__) || defined(sun) || defined(__sun) || defined(__sun__)
 #include <dirent.h>
-static pid_t * procdirSamhain (void)
+static pid_t * procdirSamhain ()
 {
-  pid_t        * pidlist;
+  pid_t        * pidlist = malloc(sizeof(pid_t) * 65535);
   struct dirent * d;
   DIR *        dp;
   long         ino;
   struct stat  buf;
-  int          i;
+  int          i = 0;
   pid_t        pid, mypid = getpid();
   char       * tail;
   char         exef[128];
 
+  for (i = 0; i < 65535; ++i) pidlist[i] = 0;
+  i = 0;
+
   if (0 != stat(SH_INSTALL_PATH, &buf))
-    {
-      return NULL;
-    }
+    return NULL;
 
   ino = (long) buf.st_ino;
     
-  if (NULL == (dp = opendir(_("/proc"))))
-    {
-      /* cppcheck-suppress resourceLeak */
-      return NULL;
-    }
-
-  SH_MUTEX_LOCK(mutex_readdir);
-
-  pidlist =  calloc(1, sizeof(pid_t) * 65535);
-  if (!pidlist)
-    goto unlock_and_out;
-
-  for (i = 0; i < 65535; ++i) pidlist[i] = 0;
-
-  i = 0;
+  if (NULL == (dp = opendir("/proc")))
+    return NULL;
   while (NULL != (d = readdir(dp)) && i < 65535)
     {
@@ -908,14 +755,9 @@
 	}
     }
-
- unlock_and_out:
-  ;
-  SH_MUTEX_UNLOCK(mutex_readdir);
-
   closedir(dp);
   return pidlist;
 }
 #else
-static pid_t * procdirSamhain (void)
+static pid_t * procdirSamhain ()
 {
   return NULL;
@@ -966,7 +808,7 @@
   if (!fp)
     { if (errno != ENOENT) perror(_("fopen")); return 0; }
-  if (NULL == fgets(line, sizeof(line), fp))
-    { perror(_("fgets")); (void) sl_fclose(FIL__, __LINE__, fp); return 0; }
-  (void) sl_fclose(FIL__, __LINE__, fp); 
+  if (NULL == fgets(line, 255, fp))
+    { perror(_("fgets")); (void) fclose(fp); return 0; }
+  (void) fclose(fp); 
   p = line; 
   while (*p == ' '  || *p == '\f' || *p == '\n' || 
@@ -1016,17 +858,18 @@
   pid_t       * pidlist;
   int         i;
-#ifdef WCONTINUED
-      int wflags = WNOHANG|WUNTRACED|WCONTINUED;
-#else
-      int wflags = WNOHANG|WUNTRACED;
-#endif
-
-  fullpath = strdup (SH_INSTALL_PATH);
+
+
+  fullpath = malloc(strlen(SH_INSTALL_PATH)+1);
   if (fullpath == NULL)
-    { perror(_("strdup")); exit (1); }
-
-  argp[0]  = strdup (SH_INSTALL_PATH);
+    { perror(_("malloc")); exit (1); }
+  else
+    strcpy(fullpath, SH_INSTALL_PATH);                 /* known to fit  */
+
+  argp[0] = malloc(strlen(SH_INSTALL_PATH)+1);
   if (argp[0] == NULL)
-    { perror(_("strdup")); exit (1); }
+    { perror(_("malloc")); exit (1); }
+  else
+    strcpy(argp[0], SH_INSTALL_PATH);                  /* known to fit  */
+
 
   for (times = 1; times < 32; ++times)  argp[times] = NULL;
@@ -1036,7 +879,9 @@
   for (times = 2; times < res; ++times)  
     {
-      argp[times-1] = strdup (argv[times]);
+      argp[times-1] = malloc(strlen(argv[times])+1);
       if (argp[times-1] == NULL)
-	{ perror(_("strdup")); exit (1); }
+	{ perror(_("malloc")); exit (1); }
+      else
+	strcpy(argp[times-1], argv[times]);              /* known to fit  */
     }
 
@@ -1054,9 +899,9 @@
 	exit (1);
       case  0:
-	if (0 != sl_close_fd (FIL__, __LINE__, 0))
+	if (0 != close (0))
 	  {
 	    _exit(4);
 	  }
-	(void) execv(fullpath, argp); /* flawfinder: ignore *//* wtf? */
+	(void) execv(fullpath, argp); 
 	if (errno == EPERM)
 	  _exit(4);
@@ -1066,6 +911,6 @@
       default:
 	times = 0;
-	while (times < 300) {
-	  respid = waitpid(pid, &status, wflags);
+	while (times < 64) {
+	  respid = waitpid(pid, &status, WNOHANG|WUNTRACED);
 	  if ((pid_t)-1 == respid)
 	    {
@@ -1075,5 +920,4 @@
 	  else if (pid == respid)
 	    {
-#ifndef USE_UNO
 	      if (0 != WIFEXITED(status))
 		{
@@ -1083,12 +927,9 @@
 	      else
 		exit (1);
-#else
-	      exit (1);
-#endif
 	    }
 	  ++times;
 	  (void) retry_msleep(1, 0);
 	}
-	exit (0); /* assume that it runs ok */
+	exit (1);
       }
     }
@@ -1100,8 +941,5 @@
       pidlist = procdirSamhain ();
       if (pid == 0 && NULL == pidlist) /* pid file not found */ 
-	{
-	  free(fullpath);
-	  return (0);
-	}
+	return (0);
 	  
       status = 0;
@@ -1118,5 +956,4 @@
 	    }
 	}
-      free(fullpath);
       if (status == 7)
 	return 0;
@@ -1155,5 +992,5 @@
 	}
     }
-  free(fullpath); /* silence smatch false positive */
+
   exit (1); /* no exit handler installed yet */
   /*@notreached@*/
@@ -1185,5 +1022,5 @@
 /* Add a new schedule to the linked list of schedules
  */
-static sh_schedule_t * sh_set_schedule_int (const char * str, 
+static sh_schedule_t * sh_set_schedule_int (char * str, 
 					    sh_schedule_t * FileSchedIn, 
 					    /*@out@*/ int * status)
@@ -1198,5 +1035,5 @@
       FileSchedIn = NULL;
       *status = 0;
-      SL_RETURN(NULL, _("sh_set_schedule_int"));
+      return 0;
     }
 
@@ -1209,7 +1046,5 @@
       SL_RETURN(FileSchedIn , _("sh_set_schedule_int"));
     }
-  else {
-    FileSched->next = FileSchedIn;
-  }
+  FileSched->next = FileSchedIn;
   SL_RETURN(FileSched , _("sh_set_schedule_int"));
 }
@@ -1217,5 +1052,5 @@
 /* Add a new schedule to the linked list FileSchedOne
  */
-int sh_set_schedule_one (const char * str)
+int sh_set_schedule_one (char * str)
 {
   int status;
@@ -1226,5 +1061,5 @@
 /* Add a new schedule to the linked list FileSchedTwo
  */
-int sh_set_schedule_two (const char * str)
+int sh_set_schedule_two (char * str)
 {
   int status;
@@ -1233,285 +1068,4 @@
 }
 
-static int sh_flag_silent = S_FALSE;
-
-int sh_set_silent_full (const char * str)
-{
-  int status = sh_util_flagval(str, &sh_flag_silent);
-  return status;
-}
-
-
-void do_reconf()
-{
-  int status, modnum;
-
-  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_RECONF);
-  
-  sh_thread_pause_flag = S_TRUE;
-
-  for (modnum = 0; modList[modnum].name != NULL; ++modnum) 
-    {
-      if (0 != (SH_MODFL_NEEDPAUSED & modList[modnum].flags) &&
-	  modList[modnum].initval == SH_MOD_THREAD)
-	{
-	  int count = 50;
-	  while (count && 0 == (SH_MODFL_ISPAUSED & modList[modnum].flags))
-	    retry_msleep(0, 100), --count;
-	}
-    }
-  
-#if defined(WITH_EXTERNAL)
-  /* delete list of external tasks
-   */
-  (void) sh_ext_cleanup();
-#endif
-#if defined(SH_WITH_MAIL)
-  sh_nmail_free();
-#endif
-  
-  /* delete the file list, make all database
-   * entries visible (allignore = FALSE)
-   */
-  (void) sh_files_deldirstack ();
-  (void) sh_files_delfilestack ();
-  (void) sh_files_delglobstack ();
-  (void) sh_ignore_clean ();
-  (void) hash_full_tree ();
-  sh_audit_delete_all ();
-  
-  
-#if defined(SH_WITH_CLIENT)
-  reset_count_dev_server();
-#endif
-#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
-  sh_restrict_purge ();
-  
-  
-  FileSchedOne = free_sched(FileSchedOne);
-  FileSchedTwo = free_sched(FileSchedTwo);
-  
-  for (modnum = 0; modList[modnum].name != NULL; ++modnum) 
-    {
-      /* sh_thread_pause_flag is true, and we block in lock
-       * until check has returned, so we are sure check will
-       * not run until sh_thread_pause_flag is set to false
-       */
-      /* if (modList[modnum].initval >= SH_MOD_ACTIVE) */
-      (void) modList[modnum].mod_reconf();
-    }
-#endif
-  
-  reset_count_dev_console();
-  reset_count_dev_time();
-  
-  (void) sh_unix_maskreset();
-  
-#ifdef RELOAD_DATABASE
-  sh_hash_hashdelete();
-
-  if (0 != sl_strcmp(file_path('D', 'R'), _("REQ_FROM_SERVER")))
-    {
-      char hashbuf[KEYBUF_SIZE];
-      (void) sl_strlcpy(sh.data.hash,
-			sh_tiger_hash (file_path('D', 'R'), 
-				       TIGER_FILE, TIGER_NOLIM, 
-				       hashbuf, sizeof(hashbuf)), 
-			KEY_LEN+1);
-    }
-#endif
-  (void) sl_trust_purge_user();
-  (void) sh_files_hle_reg (NULL);
-  (void) sh_prelink_run (NULL, NULL, 0, 0);
-  
-  /* --------------------------
-   * --- READ CONFIGURATION ---
-   * --------------------------
-   */
-  (void) sh_readconf_read ();
-  sig_config_read_again = 0;
-  (void) sh_files_setrec();
-  (void) sh_files_test_setup();
-  sh_audit_commit ();
-  
-  if (0 != sh.flag.nice)
-    {
-#ifdef HAVE_SETPRIORITY
-      setpriority(PRIO_PROCESS, 0, sh.flag.nice);
-#else
-      nice(sh.flag.nice);
-#endif
-    }
-  
-  if (sh.flag.checkSum == SH_CHECK_INIT)
-    {
-      sh.flag.isdaemon = S_FALSE;
-      sh.flag.loop     = S_FALSE;
-    }
-  
-  
-  /* --- Initialize modules. ---
-   */
-  TPT((0, FIL__, __LINE__, _("msg=<Initialize modules.>\n")));
-  for (modnum = 0; modList[modnum].name != NULL; ++modnum) 
-    {
-      status = modList[modnum].mod_init(&(modList[modnum]));
-      
-      if (status < 0)
-	{
-	  if (status == (-1)) {
-	    sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__, 
-			     status, MSG_MOD_FAIL,
-			     _(modList[modnum].name),
-			     status+SH_MOD_OFFSET);
-	  } else {
-	    sh_error_handle ((-1), FIL__, __LINE__, 
-			     status, MSG_MOD_FAIL,
-			     _(modList[modnum].name),
-			     status+SH_MOD_OFFSET);
-	  }
-	  modList[modnum].initval = SH_MOD_FAILED;
-	}
-      else
-	{
-	  sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_OK,
-			   _(modList[modnum].name));
-	  modList[modnum].initval = status;
-	}
-    }
-  
-  /* module is properly set up now
-   */
-  sh_thread_pause_flag = S_FALSE;
-
-  return;
-}
-
-static void check_signals (volatile int *flag_check_1, 
-			   volatile int * flag_check_2)
-{
-  if (sig_raised > 0) 
-    {
-      
-      TPT((0, FIL__, __LINE__, _("msg=<Process a signal.>\n")))
-	
-	if (sig_termfast == 1)  /* SIGTERM */
-	  {
-	    TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
-	    --sig_raised; --sig_urgent;
-	    aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
-	  }
-      
-      if (sig_force_check == 1) /* SIGTTOU / SIGTSTP */
-	{
-	  TPT((0, FIL__, __LINE__, _("msg=<Check run triggered.>\n")));
-	  *flag_check_1 = 1; *flag_check_2 = 1;
-	  sig_force_check = 0; --sig_raised;
-	  if (sig_force_silent)
-	    {
-	      sig_force_silent       = 0;
-	      sh_global_check_silent = (sh_flag_silent == S_FALSE) ? SH_SILENT_STD : SH_SILENT_FULL;
-	    }
-	  sh_sem_trylock();
-	}
-      
-      if (sig_config_read_again == 1 && /* SIGHUP */
-	  sh_global_suspend_flag == 0)
-	{
-	  TPT((0, FIL__, __LINE__, _("msg=<Re-read configuration.>\n")));
-	  do_reconf();
-	  --sig_raised;
-	}
-      
-      if (sig_fresh_trail == 1) /* SIGIOT */
-	{
-	  if (sh_global_suspend_flag == 0)
-	    {
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      
-	      /* Logfile access 
-	       */
-#ifdef SH_USE_XML
-	      (void) sh_log_file (NULL, NULL);
-#endif
-	      TPT((0, FIL__, __LINE__, _("msg=<Logfile stop/restart.>\n")));
-	      sh_error_only_stderr (S_TRUE);
-	      (void) sh_unix_rm_lock_file(sh.srvlog.name);
-	      (void) retry_msleep(3, 0);
-	      sh.flag.log_start = S_TRUE;
-	      sh_error_only_stderr (S_FALSE);
-	      sh_thread_pause_flag = S_FALSE;
-	      sig_fresh_trail       = 0;
-	      --sig_raised;
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	    }
-	}
-      
-      if (sig_terminate == 1)  /* SIGQUIT */
-	{
-	  TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
-	  strncpy (sh_sig_msg, _("Quit"), 20);
-	  --sig_raised;
-	  aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
-	}
-	  
-      if (sig_debug_switch == 1)  /* SIGUSR1 */
-	{
-	  TPT((0, FIL__, __LINE__, _("msg=<Debug switch.>\n")));
-	  sh_error_dbg_switch();
-	  sig_debug_switch = 0;
-	  --sig_raised;
-	}
-	  
-      if (sig_suspend_switch > 0)  /* SIGUSR2 */
-	{
-	  TPT((0, FIL__, __LINE__, _("msg=<Suspend switch.>\n")));
-	  if (sh_global_suspend_flag != 1) {
-	    SH_MUTEX_LOCK_UNSAFE(mutex_thread_nolog);
-	    sh_global_suspend_flag = 1;
-	    sh_error_handle((-1), FIL__, __LINE__, 0, MSG_SUSPEND, 
-			    sh.prg_name);
-	  } else {
-	    sh_global_suspend_flag = 0;
-	    SH_MUTEX_UNLOCK_UNSAFE(mutex_thread_nolog);
-	  }
-	  --sig_suspend_switch;
-	  --sig_raised; --sig_urgent;
-	}
-
-      if (sh_load_delta_flag > 0 &&
-	  sh_global_suspend_flag == 0)  /* DELTA Command */
-	{
-	  if (0 == sh_dbIO_load_delta())
-	    {
-	      --sh_load_delta_flag; --sig_raised;
-	    } 
-	}
-
-      sig_raised = (sig_raised < 0) ? 0 : sig_raised;
-      sig_urgent = (sig_urgent < 0) ? 0 : sig_urgent;
-      TPT((0, FIL__, __LINE__, _("msg=<End signal processing.>\n")));
-    }
-  return;
-}
-
-void check_for_delta_db()
-{
-  /* Need to contact the server.
-   */
-  if (sh_global_check_silent < SH_SILENT_FULL)
-    sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_2);
-
-  if (sig_raised > 0 && sh_load_delta_flag > 0)  /* DELTA Command */
-    {
-      /* There was a DELTA Command
-       */
-      if (0 == sh_dbIO_load_delta())
-	{
-	  --sh_load_delta_flag; --sig_raised;
-	} 
-    }
-  return;
-}
-
 #endif
 
@@ -1521,15 +1075,11 @@
  *
  *******************************************************/
-#if !defined(SH_CUTEST)
 int main(int argc, char * argv[])
-#else
-int undef_main(int argc, char * argv[])
-#endif
 {
 #if defined(INET_SYSLOG)
-  extern int    sh_xfer_create_syslog_socket (int flag);
+  extern int    create_syslog_socket (int flag);
 #endif
 #if defined(SH_WITH_SERVER)
-  extern int    sh_create_tcp_socket(void);
+  extern int    sh_create_tcp_socket();
 #endif
 
@@ -1539,14 +1089,14 @@
   float         st_1, st_2;
   int           status;
-  volatile long          cct = 0; /* main loop iterations */
-
-  volatile int           flag_check_1 = 0;
-  volatile int           flag_check_2 = 0;
+  long          cct = 0; /* main loop iterations */
+
+  int           flag_check_1 = 0;
+  int           flag_check_2 = 0;
 
   int           check_done   = 0;
 #endif
 
-  volatile time_t        told;
-  volatile time_t        tcurrent;
+  time_t        told;
+  time_t        tcurrent;
   size_t        tzlen;
   char *        tzptr;
@@ -1554,23 +1104,10 @@
 
 #if defined (SH_STEALTH_NOCL)
-  char    command_line[256];
+  char  * command_line;
   int     my_argc = 0;
   char  * my_argv[32];
 #endif
 
-#if !defined(USE_SYSTEM_MALLOC)
-  typedef void assert_handler_tp(const char * error, const char *file, int line);
-  extern assert_handler_tp *dnmalloc_set_handler(assert_handler_tp *new);
-  (void) dnmalloc_set_handler(safe_fatal);
-#endif
-
-  SH_G_INIT; /* Must precede any use of _() */
-
   SL_ENTER(_("main"));
-
-  /* --- Close all but first three file descriptors. ---
-   */
-  sh_unix_closeall(3, -1, S_FALSE); /* at program start */
-
 
   if (argc >= 2 && 0 != getuid() &&
@@ -1673,15 +1210,10 @@
   /* Save the timezone.
    */
-  if (NULL != (tzptr = getenv("TZ"))) /* flawfinder: ignore */
+  if ((tzptr = getenv("TZ")) != NULL)
     {
       tzlen       = strlen(tzptr);
-      if (tzlen < 1024)
-	{
-	  sh.timezone = calloc(1, tzlen + 1);
-	  if (sh.timezone != NULL)
-	    (void) sl_strlcpy (sh.timezone, tzptr, tzlen + 1);
-	}
-      else
-	sh.timezone = NULL;
+       sh.timezone = malloc (tzlen + 1);
+      if (sh.timezone != NULL)
+	 (void) sl_strlcpy (sh.timezone, tzptr, tzlen + 1);
     }
   else
@@ -1691,9 +1223,11 @@
   /* --------  INIT  --------    
    */
-  sh_unix_ign_sigpipe();
 
   /* Restrict error logging to stderr.
    */
   sh_error_only_stderr (S_TRUE);
+
+  BREAKEXIT(sh_derr);
+  (void) sh_derr();
 
   /* Check that first three descriptors are open.
@@ -1714,10 +1248,4 @@
 #endif
 
-  /* --- First check for an attached debugger (after setting
-         sh.sigtrap_max_duration which has to be done before). ---
-   */
-  BREAKEXIT(sh_derr);
-  (void) sh_derr();
-
   /* --- Get local hostname. ---
    */
@@ -1730,6 +1258,4 @@
 
 #if !defined(SH_STEALTH_NOCL)
-  sh_argc_store = argc;
-  sh_argv_store = argv;
   (void) sh_getopt_get (argc, argv);
 #else
@@ -1737,22 +1263,14 @@
       strlen(argv[1]) > 0 && strlen(NOCL_CODE) > 0)
     {
-      if ( 0 == strcmp(argv[1], NOCL_CODE) )
-	{
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
-	  char * saveptr;
-#endif
+      if ( 0 == strcmp(argv[1], NOCL_CODE) &&
+	   NULL != (command_line = (char *) SH_ALLOC(256 * sizeof(char))))
+	{
 	  my_argv[0] = argv[0]; ++my_argc;  
 	  command_line[0] = '\0';
-	  if (NULL != fgets (command_line, sizeof(command_line), stdin))
-	    command_line[sizeof(command_line)-1] = '\0';
-
+	  (void*) fgets (command_line, 255, stdin);
+	  command_line[255] = '\0';
 	  do {
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
 	    my_argv[my_argc] = 
-	      strtok_r( (my_argc == 1) ? command_line : NULL, " \n", &saveptr);
-#else
-	    my_argv[my_argc] = 
-	      strtok( (my_argc == 1) ? command_line : NULL, " \n");
-#endif 
+	      strtok( (my_argc == 1) ? command_line : NULL, " \n"); 
 	    if (my_argv[my_argc] != NULL) {
 	      ++my_argc;
@@ -1761,9 +1279,6 @@
 	    }
 	  } while (my_argc < 32);
-
-	  sh_argc_store = my_argc;
-	  sh_argv_store = my_argv;
-
 	  (void) sh_getopt_get (my_argc, my_argv);
+	  SH_FREE (command_line);
 	}
       else
@@ -1776,4 +1291,9 @@
   sh.flag.opts = S_FALSE;
   
+
+  /* close all other files
+   */
+  sh_unix_closeall(3, -1); /* after processing CL options */
+
 
   /* --- Get user info. ---
@@ -1797,6 +1317,4 @@
   BREAKEXIT(sh_readconf_read);
   (void) sh_readconf_read ();
-
-  sh_calls_enable_sub();
 
 #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
@@ -1832,13 +1350,9 @@
     }
 
-  /* --- load database; checksum of database
-   */
-#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
-  if (0 == sh.delayload)
-    sh_hash_init_and_checksum();
-#endif
-
-  /* --- initialize signal handling etc.; fork daemon
-   */
+  /* initialize signal handling etc
+   */
+  if (sh.flag.isdaemon == S_TRUE)
+    sh_error_only_stderr (BAD);
+
   if (sh_unix_init(sh.flag.isdaemon) == -1) 
     {
@@ -1848,12 +1362,4 @@
     }
 
-#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
-  if (sh.delayload)
-    {
-      sleep(sh.delayload);
-      sh_hash_init_and_checksum();
-    }
-#endif
-
   /* --- drop privileges eventually ---
    */
@@ -1861,5 +1367,5 @@
   sh_create_tcp_socket ();
 #if defined(INET_SYSLOG)
-  sh_xfer_create_syslog_socket (S_TRUE);
+  create_syslog_socket (S_TRUE);
 #endif
   SL_REQUIRE(sl_policy_get_real(DEFAULT_IDENT) == SL_ENONE, 
@@ -1883,5 +1389,29 @@
    */
 #if defined(INET_SYSLOG) && defined(SH_WITH_SERVER)
-  sh_xfer_create_syslog_socket (S_FALSE);
+  create_syslog_socket (S_FALSE);
+#endif
+
+
+  /* checksum of database
+   */
+#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) 
+  TPT((0, FIL__, __LINE__, _("msg=<Get checksum of the database.>\n")))
+  if (sh.flag.checkSum == SH_CHECK_CHECK) 
+    {
+      if (0 == sl_strcmp(file_path('D', 'R'), _("REQ_FROM_SERVER")))
+	{
+	  /* fetch the file from server to get checksum
+	   */
+	  sh_hash_init ();
+	  /* sh_hash_hashdelete (); */
+	}
+      else
+	{
+	  (void) sl_strlcpy(sh.data.hash,
+			    sh_tiger_hash (file_path('D', 'R'), 
+					   TIGER_FILE, 0), 
+			    KEY_LEN+1);
+	}
+    }
 #endif
 
@@ -1890,6 +1420,4 @@
    */
   sh_error_only_stderr (S_FALSE);
-
-  sh.flag.started = S_TRUE;
 
   /****************************************************
@@ -1901,7 +1429,9 @@
 #if defined(SH_WITH_SERVER) && !defined(SH_WITH_CLIENT)
 
-#if defined(WITH_GPG)
-  /* log startup */
-  sh_sig_log_startup ();
+#if (defined(WITH_GPG) || defined(WITH_PGP))
+  /* do nothing -- we exit earlier if error 
+  if (0 != sh_gpg_check_sign (1)) 
+    aud_exit(FIL__, __LINE__, EXIT_FAILURE);
+  */
 #else
   sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_1H,
@@ -1924,7 +1454,10 @@
   if (sh.flag.checkSum == SH_CHECK_CHECK) 
     {
-#if defined(WITH_GPG)
-      /* log startup */
-      sh_sig_log_startup ();
+#if (defined(WITH_GPG) || defined(WITH_PGP))
+      /* do nothing -- we exit earlier if error 
+	 if (0 != sh_gpg_check_sign (2)) 
+	 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
+      */
+      ;
 #else
       sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_2H,
@@ -1936,7 +1469,10 @@
   else
     {
-#if defined(WITH_GPG)
-      /* log startup */
-      sh_sig_log_startup ();
+#if (defined(WITH_GPG) || defined(WITH_PGP))
+      /* do nothing -- we exit earlier if error 
+      if (0 != sh_gpg_check_sign (1)) 
+	aud_exit(FIL__, __LINE__, EXIT_FAILURE);
+      */
+      ;
 #else
       sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_1H,
@@ -1948,5 +1484,5 @@
 
  
-  if ((skey == NULL) || (skey->mlock_failed == S_TRUE))
+  if ((skey == NULL) || (skey->mlock_failed == SL_TRUE))
     sh_error_handle ((-1), FIL__, __LINE__, EPERM, MSG_MLOCK);
 
@@ -1970,10 +1506,10 @@
   if (sh.flag.isserver == S_TRUE)
     { 
-      sh_xfer_start_server();
+      sh_receive();
       TPT((0, FIL__, __LINE__, _("msg=<End server.>\n")))
       aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
     }
 #else
-  sh_xfer_start_server();
+  sh_receive();
   TPT((0, FIL__, __LINE__, _("msg=<End server.>\n")))
   aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
@@ -1995,6 +1531,5 @@
   for (modnum = 0; modList[modnum].name != NULL; ++modnum) 
     {
-      status = modList[modnum].mod_init(&(modList[modnum]));
-      if ( status < 0 )
+      if (0 != (status = modList[modnum].mod_init()))
 	{
 	  if (status == (-1)) {
@@ -2002,11 +1537,11 @@
 			     MSG_MOD_FAIL,
 			     _(modList[modnum].name),
-			     status+SH_MOD_OFFSET);
+			     status);
 	  } else {
 	    sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_FAIL,
 			     _(modList[modnum].name),
-			     status+SH_MOD_OFFSET);
+			     status);
 	  }
-	  modList[modnum].initval = SH_MOD_FAILED;
+	  modList[modnum].initval = S_FALSE;
 	}
       else
@@ -2014,5 +1549,5 @@
 	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,
 			   _(modList[modnum].name));
-	  modList[modnum].initval = status;
+	  modList[modnum].initval = S_TRUE;
 	}
     }
@@ -2022,5 +1557,5 @@
   (void) sh_files_setrec();
   (void) sh_files_test_setup();
-  sh_audit_commit ();
+
 
   /* --------  NICE LEVEL   ---------
@@ -2037,15 +1572,8 @@
     }
 
-  /*  --------  CREATE SEMAPHORE  ---------
-   */
-  sh_sem_open();
-
   /*  --------  MAIN LOOP  ---------
    */
-  sh.statistics.bytes_speed   = 0;
-  sh.statistics.bytes_hashed  = 0;
-  sh.statistics.files_report  = 0;
-  sh.statistics.files_error   = 0;
-  sh.statistics.files_nodir   = 0;
+  sh.statistics.bytes_speed  = 0;
+  sh.statistics.bytes_hashed = 0;
 
   while (1 == 1) 
@@ -2059,16 +1587,192 @@
       tcurrent = time (NULL);
 
-      do {
-	check_signals(&flag_check_1, &flag_check_2);
-	if (sh_global_suspend_flag == 1)
+      if (sig_raised > 0) 
+	{
+
+	  TPT((0, FIL__, __LINE__, _("msg=<Process a signal.>\n")))
+
+	  if (sig_termfast == 1)  /* SIGTERM */
+	    {
+	      TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
+	      /* strncpy (sh_sig_msg, _("SIGTERM"), 20); */
+	      --sig_raised; --sig_urgent;
+	      aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
+	    }
+
+	  if (sig_force_check == 1) /* SIGTTOU */
+	    {
+	      TPT((0, FIL__, __LINE__, _("msg=<Check run triggered.>\n")));
+	      flag_check_1 = 1;
+	      flag_check_2 = 1;
+	      sig_force_check = 0;
+	      --sig_raised; 
+	    }
+	  
+	  if (sig_config_read_again == 1) /* SIGHUP */
+	    {
+	      TPT((0, FIL__, __LINE__, _("msg=<Re-read configuration.>\n")))
+	      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_RECONF);
+
+#if defined(WITH_EXTERNAL)
+	      /* delete list of external tasks
+	       */
+	      (void) sh_ext_cleanup();
+#endif
+	      /* delete the file list, make all database
+	       * entries visible (allignore = FALSE)
+	       */
+	      (void) sh_files_deldirstack ();
+	      (void) sh_files_delfilestack ();
+	      (void) sh_ignore_clean ();
+	      (void) hash_full_tree ();
+
+#if defined(SH_WITH_CLIENT)
+	      reset_count_dev_server();
+#endif
+#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
+
+
+	      FileSchedOne = free_sched(FileSchedOne);
+	      FileSchedTwo = free_sched(FileSchedTwo);
+
+	      for (modnum = 0; modList[modnum].name != NULL; ++modnum) 
+		{
+		  if (modList[modnum].initval == GOOD)
+		    (void) modList[modnum].mod_reconf();
+		}
+#endif
+
+#if defined(SH_WITH_MAIL)
+	      reset_count_dev_mail();
+#endif
+	      reset_count_dev_console();
+	      reset_count_dev_time();
+
+	      (void) sh_unix_maskreset();
+ 
+	      /* Should this be included ??? 
+	       * (i.e. should we reload the database ?)
+	       */
+#ifdef RELOAD_DATABASE
+	      sh_hash_hashdelete();
+#endif
+	      (void) sl_trust_purge_user();
+	      (void) sh_files_hle_reg (NULL);
+	      (void) sh_prelink_run (NULL, NULL, 0);
+
+	      /* --------------------------
+	       * --- READ CONFIGURATION ---
+	       * --------------------------
+	       */
+	      (void) sh_readconf_read ();
+	      sig_config_read_again = 0;
+	      (void) sh_files_setrec();
+	      (void) sh_files_test_setup();
+	      if (0 != sh.flag.nice)
+		{
+#ifdef HAVE_SETPRIORITY
+		  setpriority(PRIO_PROCESS, 0, sh.flag.nice);
+#else
+		  nice(sh.flag.nice);
+#endif
+		}
+
+	      if (sh.flag.checkSum == SH_CHECK_INIT)
+		{
+		  sh.flag.isdaemon = S_FALSE;
+		  sh.flag.loop     = S_FALSE;
+		}
+
+	      /* --- Initialize modules. ---
+	       */
+	      TPT((0, FIL__, __LINE__, _("msg=<Initialize modules.>\n")));
+	      for (modnum = 0; modList[modnum].name != NULL; ++modnum) 
+		{
+		  if (0 != (status = modList[modnum].mod_init()))
+		    {
+		      if (status == (-1)) {
+			sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__, 
+					 status, MSG_MOD_FAIL,
+					 _(modList[modnum].name),
+					 status);
+		      } else {
+			sh_error_handle ((-1), FIL__, __LINE__, 
+					 status, MSG_MOD_FAIL,
+					 _(modList[modnum].name),
+					 status);
+		      }
+		      modList[modnum].initval = S_FALSE;
+		    }
+		  else
+		    {
+		      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,
+				       _(modList[modnum].name));
+		      modList[modnum].initval = S_TRUE;
+		    }
+		}
+	      
+	      --sig_raised;
+	    }
+	  
+	  if (sig_fresh_trail == 1) /* SIGIOT */
+	    {
+	      /* Logfile access 
+	       */
+#ifdef SH_USE_XML
+	      (void) sh_log_file (NULL, NULL);
+#endif
+	      TPT((0, FIL__, __LINE__, _("msg=<Logfile stop/restart.>\n")));
+	      sh_error_only_stderr (S_TRUE);
+	      (void) sh_unix_rm_lock_file(sh.srvlog.name);
+	      (void) retry_msleep(3, 0);
+	      sh.flag.log_start = S_TRUE;
+	      sh_error_only_stderr (S_FALSE);
+	      sig_fresh_trail       = 0;
+	      --sig_raised;
+	    }
+	  
+	  if (sig_terminate == 1)  /* SIGQUIT */
+	    {
+	      TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
+	      strncpy (sh_sig_msg, _("Quit"), 20);
+	      --sig_raised; --sig_urgent;
+	      aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
+	    }
+	  
+	  if (sig_debug_switch == 1)  /* SIGUSR1 */
+	    {
+	      TPT((0, FIL__, __LINE__, _("msg=<Debug switch.>\n")));
+	      sh_error_dbg_switch();
+	      sig_debug_switch = 0;
+	      --sig_raised;
+	    }
+	  
+	  if (sig_suspend_switch == 1)  /* SIGUSR2 */
+	    {
+	      TPT((0, FIL__, __LINE__, _("msg=<Suspend switch.>\n")));
+	      if (sh_global_suspend_flag == 1) {
+		sh_global_suspend_flag = 0;
+	      } else {
+		sh_error_handle((-1), FIL__, __LINE__, 0, MSG_SUSPEND, 
+				sh.prg_name);
+		sh_global_suspend_flag = 1;
+	      }
+	      sig_suspend_switch = 0;
+	      --sig_raised; --sig_urgent;
+	    }
+	  sig_raised = (sig_raised < 0) ? 0 : sig_raised;
+	  sig_urgent = (sig_urgent < 0) ? 0 : sig_urgent;
+	  TPT((0, FIL__, __LINE__, _("msg=<End signal processing.>\n")));
+	}
+      
+      if (sh_global_suspend_flag == 1)
+	{
 	  (void) retry_msleep (1, 0);
-      } while (sh_global_suspend_flag == 1);
+	  continue;
+	}
       
       /* see whether its time to check files
        */
-      if      (sh.flag.checkSum == SH_CHECK_INIT ||
-	       (sh.flag.inotify & SH_INOTIFY_DOSCAN) != 0 ||
-	       (sh.flag.checkSum == SH_CHECK_CHECK &&
-		(sh.flag.isdaemon == S_FALSE && sh.flag.loop == S_FALSE)))
+      if      (sh.flag.checkSum == SH_CHECK_INIT)
 	{
 	  flag_check_1 = 1;
@@ -2103,17 +1807,4 @@
 	  (flag_check_1 == 1 || flag_check_2 == 1))
 	{
-	  sh_sem_trylock();
-
-	  /* Starting a check now, so make sure to fetch delta DB
-	   * if there is one to download.
-	   */
-	  check_for_delta_db();
-
-	  SH_INOTIFY_IFUSED( sh.flag.inotify |= SH_INOTIFY_INSCAN; );
-	  /* Refresh list files matching glob patterns.
-	   */
-	  if (sh.flag.checkSum != SH_CHECK_INIT)
-	    sh_files_check_globPatterns();
-
 	  /* 
 	   * check directories and files
@@ -2124,7 +1815,4 @@
 	  sh.statistics.dirs_checked   = 0;
 	  sh.statistics.files_checked  = 0;
-	  sh.statistics.files_report   = 0;
-	  sh.statistics.files_error    = 0;
-	  sh.statistics.files_nodir    = 0;
 
 	  TPT((0, FIL__, __LINE__, _("msg=<Check directories.>\n")))
@@ -2134,5 +1822,5 @@
 	      (void) sh_dirs_chk  (1);
 #ifndef SH_PROFILE
-	      (void) retry_aud_chdir (FIL__, __LINE__, "/");
+	      (void) chdir ("/");
 #endif
 	    }
@@ -2141,5 +1829,5 @@
 	      (void) sh_dirs_chk  (2); 
 #ifndef SH_PROFILE
-	      (void) retry_aud_chdir (FIL__, __LINE__, "/");
+	      (void) chdir ("/");
 #endif
 	    }
@@ -2149,5 +1837,7 @@
 	    (void) sh_files_chk ();
 
-	  if (sig_urgent > 0) { sh_sem_unlock(sh.statistics.files_report); sh_global_check_silent = 0; continue; }
+	  if (sig_urgent > 0)
+	    continue;
+
 	  /*
 	   * check for files not visited
@@ -2159,5 +1849,6 @@
 	    }
 
-	  if (sig_urgent > 0) { sh_sem_unlock(sh.statistics.files_report); sh_global_check_silent = 0; continue; }
+	  if (sig_urgent > 0)
+	    continue;
 
 	  /* reset
@@ -2165,20 +1856,16 @@
 	  TPT((0, FIL__, __LINE__, _("msg=<Reset status.>\n")))
 	  sh_dirs_reset  ();
-
-	  if (sig_urgent > 0) { sh_sem_unlock(sh.statistics.files_report); sh_global_check_silent = 0; continue; }
+	  if (sig_urgent > 0)
+	    continue;
 
 	  sh_files_reset ();
-	  check_done   = 1;
-
 	  flag_check_1 = 0;
 	  flag_check_2 = 0;
-	  sh_sem_unlock(sh.statistics.files_report);
-
-	  SH_INOTIFY_IFUSED( sh.flag.inotify &= ~SH_INOTIFY_INSCAN; );
-	  SH_INOTIFY_IFUSED( sh.flag.inotify &= ~SH_INOTIFY_DOSCAN; );
-
-	  (void) sh_prelink_run (NULL, NULL, 0, 0);
-
-	  if (sig_urgent > 0) { sh_global_check_silent = 0; continue; }
+	  check_done   = 1;
+
+	  (void) sh_prelink_run (NULL, NULL, 0);
+
+	  if (sig_urgent > 0)
+	    continue;
 
 	  runtim = time(NULL) - sh.statistics.time_start;
@@ -2186,6 +1873,5 @@
 	
 	  if ((sh.statistics.dirs_checked == 0) && 
-	      (sh.statistics.files_checked == 0) &&
-	      (sh_global_check_silent < SH_SILENT_FULL))
+	      (sh.statistics.files_checked == 0))
 	    sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_0);
 
@@ -2205,19 +1891,12 @@
 	      sh.statistics.bytes_speed = (unsigned long) st_1;
 
-	      if (sh_global_check_silent < SH_SILENT_FULL)
-		sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_1,
-				 (long) runtim, 
-				 0.001 * st_1);
-
-	      if (sh.flag.checkSum != SH_CHECK_INIT)
-		sh_efile_report();
+	      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_1,
+			       (long) runtim, 
+			       0.001 * st_1);
 	    }
-	  
-	  if (0 == sh_global_check_silent)
-	    sh.fileCheck.alarm_last = time (NULL);
-
-	  sh_global_check_silent = 0;
-	  
-	  if (sig_urgent > 0) continue;
+	  sh.fileCheck.alarm_last = time (NULL);
+
+	  if (sig_urgent > 0)
+	    continue;
 
 	  /*
@@ -2226,9 +1905,10 @@
 #if defined(SH_WITH_MAIL)
 	  TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
-	  (void) sh_nmail_flush ();
+	  (void) sh_mail_msg (NULL);
 #endif
 	}
       
-      if (sig_urgent > 0) continue;
+      if (sig_urgent > 0)
+	continue;
       
       /* execute modules
@@ -2237,9 +1917,9 @@
       for (modnum = 0; modList[modnum].name != NULL; ++modnum) 
 	{
-	  if (modList[modnum].initval == SH_MOD_ACTIVE &&
+	  if (modList[modnum].initval == GOOD &&
 	      0 != modList[modnum].mod_timer(tcurrent))
 	    if (0 != (status = modList[modnum].mod_check()))
 	      sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_EXEC,
-			       _(modList[modnum].name), (long) (status+SH_MOD_OFFSET));
+			       _(modList[modnum].name), (long) status);
 	}
       
@@ -2248,5 +1928,5 @@
        */ 
       if      (sh.flag.checkSum == SH_CHECK_INIT)
-	sh_dbIO_data_write (NULL, NULL);
+	sh_hash_pushdata (NULL, NULL);
 
       /* write out database
@@ -2255,34 +1935,12 @@
 	  sh.flag.update == S_TRUE && 
 	  check_done == 1)
-	sh_dbIO_writeout_update ();
-
-      /* no-op unless MEM_LOG is defined in sh_mem.c
-       */
-#ifdef MEM_DEBUG
-      sh_mem_dump ();
-#endif
-
-      {
-	char * stale;
-
-	stale = sl_check_stale();
-	if (stale)
-	  {
-	    sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-			    stale, _("sl_check_stale"));
-	  }
-
-	stale = sl_check_badfd();
-	if (stale)
-	  {
-	    sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-			    stale, _("sl_check_stale"));
-	  }
-      }
+	sh_hash_writeout ();
 
       /* no loop if not daemon
        */
-      if (sh.flag.isdaemon != S_TRUE && sh.flag.loop == S_FALSE) break;
-      if (sig_urgent > 0) continue;
+      if (sh.flag.isdaemon != S_TRUE && sh.flag.loop == S_FALSE)
+	break; 
+      if (sig_urgent > 0)
+	continue;
 
       /* see whether its time to send mail
@@ -2292,9 +1950,10 @@
 	{
 	  TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
-	  (void) sh_nmail_flush ();
+	  (void) sh_mail_msg (NULL);
 	  sh.mailTime.alarm_last = time (NULL);
 	}
 #endif
-      if (sig_urgent > 0) continue;
+      if (sig_urgent > 0)
+	continue;
             
       /* log the timestamp
@@ -2316,10 +1975,7 @@
       (void) taus_seed();
       
-      if (sig_urgent > 0) continue;
+      if (sig_urgent > 0)
+	continue;
       
-      /* reset cache
-       */
-      sh_userid_destroy();
-
       /* go to sleep
        */
@@ -2343,5 +1999,5 @@
 #if defined(SH_WITH_MAIL)
   if (sh.mailNum.alarm_last > 0) 
-    (void)sh_nmail_flush ();
+    (void)sh_mail_msg (NULL);
 #endif
 
Index: trunk/src/samhain_erase.c
===================================================================
--- trunk/src/samhain_erase.c	(revision 1)
+++ trunk/src/samhain_erase.c	(revision 1)
@@ -0,0 +1,140 @@
+/***************************************************************************
+ *
+ * Purpose:
+ * -------
+ *   Hide loaded kernel modules with names including the string MAGIC_HIDE 
+
+ *
+ * Configuration:
+ * -------------
+ *   If not building within the samhain system, you may remove the 
+ *   line '#include "config.h"' and in the line
+ *   '#define MAGIC_HIDE SH_MAGIC_HIDE', replace SH_MAGIC_HIDE with
+ *   "someString" (in quotes !).
+ */
+
+
+#include "config.h" 
+
+#define MAGIC_HIDE SH_MAGIC_HIDE
+
+/*  #define MAGIC_HIDE "someString"              */
+
+/* define this if you have a modversioned kernel */
+/*  #define MODVERSIONS                           */
+
+/*
+ * Install:
+ * -------
+ *   gcc -Wall -O2 -c samhain_erase.c
+ *   mv samhain_hide.o  /lib/modules/KERNEL_VERSION/misc/
+ *   
+ *   (Replace KERNEL_VERSION with your kernel's version.)
+ *
+ * Usage:
+ * -----
+ *   To load the module:
+ *    insmod samhain_hide (for improved safety: 'sync && insmod samhain_hide')
+ *
+ *   To unload the module 
+ *    rmmod samhain_hide  (for improved safety: 'sync && rmmod samhain_hide')
+ * 
+ * 
+ * Tested on:
+ * ---------
+ *   Linux 2.2
+ *
+ * Copyright:
+ * ---------
+ *   Copyright (C) 2001 Rainer Wichmann (http://la-samhna.de)
+ *
+ * License: 
+ * -------
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License, version 2, as
+ *   published by the Free Software Foundation.
+ *                                                                         
+ *   This program is distributed in the hope that it will be useful,        
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of         
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          
+ *   GNU General Public License for more details.                           
+ *                                                                         
+ *   You should have received a copy of the GNU General Public License      
+ *   along with this program; if not, write to the Free Software            
+ *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              
+ *
+ ***************************************************************************/
+
+#define __KERNEL__
+#define MODULE
+
+/* The configure options (#defines) for the Kernel
+ */
+#include <linux/config.h>
+
+#ifdef CONFIG_MODVERSIONS
+#include <linux/modversions.h>
+#endif
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/string.h>
+
+#define N_(string) string
+#include "config.h"
+
+#ifdef MODULE_LICENSE
+MODULE_LICENSE("GPL");  
+#endif
+
+#undef  NULL
+#define NULL ((void *)0)
+
+
+int init_module()
+{
+  struct module * ptr;
+  struct module * prev;
+  int             found  = 0;
+
+  ptr  = &(__this_module);
+  prev = &(__this_module);
+
+  /* skip this module to allow 'rmmod'
+   */
+  ptr  = ptr->next;
+
+  while (ptr)
+    {
+      found = 0;
+
+      if (ptr->name && ptr->name[0] != '\0')
+	{
+	  /* printk("%s <%s>\n", ptr->name, SH_MAGIC_HIDE); */
+	  if (NULL != strstr(ptr->name, SH_MAGIC_HIDE))
+	    {
+	      prev->next = ptr->next;
+	      /* printk("-->HIDE\n"); */
+	      found = 1;
+	    }
+	} 
+
+      if (ptr->next)
+	{
+	  if (found == 0)
+	    prev = ptr;
+	  ptr = ptr->next;
+	}
+      else
+	break;
+    }
+
+  return 0;
+}
+
+void cleanup_module()
+{
+	return;
+}
+
+
Index: trunk/src/samhain_hide.c
===================================================================
--- trunk/src/samhain_hide.c	(revision 1)
+++ trunk/src/samhain_hide.c	(revision 1)
@@ -0,0 +1,780 @@
+/***************************************************************************
+ *
+ * Purpose:
+ * -------
+ *   (1) Hide files with the string MAGIC_HIDE in filename,
+ *       where MAGIC_HIDE is defined below. 
+ *       By default,  MAGIC_HIDE is defined as "samhain".
+ *
+ *   (2) Hide all processes, if the executable has the string MAGIC_HIDE 
+ *       in its name.
+ *
+ *
+ * Configuration:
+ * -------------
+ *   If not building within the samhain system, you may remove the 
+ *   line '#include "config.h"' and in the line
+ *   '#define MAGIC_HIDE SH_MAGIC_HIDE', replace SH_MAGIC_HIDE with
+ *   "someString" (in quotes !).
+ */
+
+/* #define _(string) string */
+#include "config.h" 
+
+#undef _
+#define _(string) string
+
+/* define if this is a 2.6 kernel                 */
+/* #define LINUX26                                */
+
+#define MAGIC_HIDE SH_MAGIC_HIDE
+
+/*  #define MAGIC_HIDE "someString"               */
+
+/* define this if you have a modversioned kernel  */
+/*  #define MODVERSIONS                           */
+
+/* the address of the sys_call_table (not exported in 2.5 kernels) */
+#define MAGIC_ADDRESS SH_SYSCALLTABLE
+
+/*
+ * Install:
+ * -------
+ *   gcc -Wall -O2 -c samhain_hide.c
+ *   mv samhain_hide.o  /lib/modules/KERNEL_VERSION/misc/
+ *   
+ *   (Replace KERNEL_VERSION with your kernel's version.)
+ *
+ * Usage:
+ * -----
+ *   To load the module:
+ *    insmod samhain_hide (for improved safety: 'sync && insmod samhain_hide')
+ *
+ *   To unload the module 
+ *    rmmod samhain_hide  (for improved safety: 'sync && rmmod samhain_hide')
+ * 
+ *
+ * Details:
+ * -------
+ *   The following kernel syscalls are replaced:
+ *     sys_getdents     [hide files/directories/processes (/proc/PID)]
+ * 
+ * Tested on:
+ * ---------
+ *   Linux 2.2, 2.4, 2.6
+ *
+ * Copyright:
+ * ---------
+ *   Copyright (C) 2001, 2002 Rainer Wichmann (http://la-samhna.de)
+ *
+ * License: 
+ * -------
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License, version 2, as
+ *   published by the Free Software Foundation.
+ *                                                                         
+ *   This program is distributed in the hope that it will be useful,        
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of         
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          
+ *   GNU General Public License for more details.                           
+ *                                                                         
+ *   You should have received a copy of the GNU General Public License      
+ *   along with this program; if not, write to the Free Software            
+ *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              
+ *
+ ***************************************************************************/
+
+
+
+/*****************************************************
+ *
+ *  The defines:
+ *
+ *****************************************************/
+
+/* This is a Linux Loadable Kernel Module.
+ */
+
+#ifndef LINUX26
+#define __KERNEL__
+#define MODULE
+#endif
+#define LINUX
+
+/* Define for debugging.   
+ */
+/* #define HIDE_DEBUG  */   /* query_module */
+/* #define FILE_DEBUG  */   /* getdents     */
+/* #define READ_DEBUG  */   /* read         */
+/* #define PROC_DEBUG  */   /* procfs       */
+
+
+/*****************************************************
+ *
+ *  The include files:
+ *
+ *****************************************************/
+
+
+/* The configure options (#defines) for the Kernel
+ */
+#include <linux/config.h>
+
+#ifndef LINUX26
+#ifdef CONFIG_MODVERSIONS
+#include <linux/modversions.h>
+#endif
+#endif
+
+
+#ifdef LINUX26
+#include <linux/init.h>
+#endif
+
+#include <linux/module.h>
+
+/* File tables structures. If directory caching is used,
+ * <linux/dcache.h> will be included here, and __LINUX_DCACHE_H
+ * will thus be defined.
+ */
+#include <linux/fs.h>
+#include <linux/proc_fs.h>
+
+/* Include the SYS_syscall defines.
+ */
+#ifndef LINUX26
+#include <sys/syscall.h>
+#else
+#define SYS_getdents 141
+#define SYS_getdents64 220
+#endif
+
+
+/* Includes for 'getdents' per the manpage.
+ */
+#include <linux/types.h>
+#include <linux/dirent.h>
+#include <linux/unistd.h>
+
+/* To access userspace memory.
+ */
+#include <asm/uaccess.h>
+
+/* Include for lock_kernel().
+ */
+#include <linux/smp_lock.h>
+
+/* Include for fget().
+ */
+#include <linux/file.h>
+
+/*****************************************************
+ *
+ *  The global variables:
+ *
+ *****************************************************/
+
+/* The kernel syscall table. Not exported anymore in 2.5 ff., and also
+ * not in the RedHat 2.4 kernel.
+ */
+
+#if 0
+extern void * sys_call_table[];
+#define sh_sys_call_table sys_call_table
+#endif
+
+unsigned long * sh_sys_call_table = (unsigned long *) MAGIC_ADDRESS;
+
+/* The old address of the sys_getdents syscall.
+ */
+int (*old_getdents)(unsigned int, struct dirent *, unsigned int);
+#ifdef __NR_getdents64
+long (*old_getdents64)(unsigned int, struct dirent64 *, unsigned int);
+#endif
+
+char hidden[] = MAGIC_HIDE;
+ 
+
+/*****************************************************
+ *
+ *  The functions:
+ *
+ *****************************************************/
+
+
+MODULE_AUTHOR("Rainer Wichmann");
+MODULE_DESCRIPTION("Hide files/processes/modules with MAGIC_HIDE in name.");
+#if defined(MODULE_LICENSE) || defined(LINUX26)
+MODULE_LICENSE("GPL");  
+#endif
+
+#ifdef LINUX26
+/* Default is to hide ourselves.
+ */
+static int removeme = 1;
+
+MODULE_PARM (removeme, "i");
+#endif
+
+
+/* 
+ *  struct task_struct is defined in linux/sched.h
+ *
+ *  as of 2.4.20, the vanilla kernel holds (among others):
+ *        struct task_struct *next_task, *prev_task;
+ *
+ *  Redhat kernel seems to have a different scheduler.
+ *  use:
+ *        struct task_struct * find_task_by_pid (int pid);
+ */
+
+#if defined(SH_VANILLA_KERNEL) && !defined(LINUX26)
+/*
+ * Fetch the task struct for a given PID.
+ */
+struct task_struct * fetch_task_struct (int pid)
+{
+  struct task_struct * task_ptr;
+
+#ifdef PROC_DEBUG
+  printk("FETCH TASK %d\n", pid);
+#endif
+
+  task_ptr = current;
+
+  do 
+    {
+      if (task_ptr->pid == (pid_t) pid )
+	return (task_ptr);
+      task_ptr = task_ptr->next_task;
+    } 
+  while (task_ptr != current);
+
+#ifdef PROC_DEBUG
+  printk("FETCH TASK: NOT FOUND !!!\n");
+#endif
+
+  return (NULL);
+}
+
+#else
+/*
+ *  RedHat 2.4.20 kernel
+ */
+struct task_struct * fetch_task_struct (int pid)
+{
+  struct task_struct * task_ptr = NULL;
+  task_ptr = find_task_by_pid (pid);
+  return (task_ptr);
+}
+#endif
+
+/* Convert a string to an int. 
+ * Does not recognize integers with a sign (+/-) in front.
+ */
+int my_atoi(char * in_str)
+{
+  int i      = 0;
+  int retval = 0;
+  int conv   = 0;
+
+  if (in_str == NULL)
+    return (-1);
+
+  while(in_str[i] != '\0')
+    {
+      /* Break if not numeric.
+       */
+      if (in_str[i] < '0' || in_str[i] > '9')
+	break;
+
+      ++conv;
+      
+      /* Leading zeroes (should not happen in /proc)
+       */
+      if (retval == 0 && in_str[i] == '0')
+	retval = retval;
+      else
+	retval = retval * 10;
+
+      retval = retval + (in_str[i] - '0');
+
+      i++;
+    }
+      
+  if (conv == 0)
+    return (-1);
+  else
+    return (retval); 
+}
+
+/* Purpose:
+ * 
+ *   Hide all files/dirs that include the string MAGIC_HIDE in their
+ *   name. 
+ */
+int new_getdents (unsigned int fd, struct dirent *dirp, unsigned int count)
+{
+  int                  status = 0;    /* Return value from original getdents */
+  struct inode       * dir_inode;
+  int                  dir_is_proc = 0;
+
+  struct dirent      * dirp_prev;
+  struct dirent      * dirp_new;
+  struct dirent      * dirp_current;
+
+  int                  dir_table_bytes;
+  int                  forward_bytes;
+  struct task_struct * task_ptr;
+  int                  hide_it = 0;
+  long                 dirp_offset;
+
+  lock_kernel();
+
+  status = (*old_getdents)(fd, dirp, count);
+
+#ifdef FILE_DEBUG
+  printk("STATUS %d\n", status);
+#endif
+
+  /*  0: end of directory.
+   * -1: some error
+   */
+  if (status <= 0)
+    {
+      unlock_kernel();
+      return (status);
+    }
+
+  /* Handle directory caching. dir_inode is the inode of the directory.
+   */
+#if defined(__LINUX_DCACHE_H)
+  dir_inode  = current->files->fd[fd]->f_dentry->d_inode;
+#else
+  dir_inode  = current->files->fd[fd]->f_inode;
+#endif
+
+  /* Check for the /proc directory
+   */
+  if (dir_inode->i_ino == PROC_ROOT_INO 
+#ifndef LINUX26
+      && !MAJOR(dir_inode->i_dev) && 
+      MINOR(dir_inode->i_dev) == 1
+#endif
+      )
+    dir_is_proc = 1;
+
+  /* Allocate space for new dirent table. Can't use GFP_KERNEL 
+   * (kernel oops)
+   */
+  dirp_new = (struct dirent *) kmalloc (status, GFP_ATOMIC);
+
+  if (dirp_new == NULL)
+    {
+      unlock_kernel();
+      return (status);
+    }
+
+  /* Copy the dirp table to kernel space.
+   */
+  copy_from_user(dirp_new, dirp, status);
+
+#ifdef FILE_DEBUG
+  printk("COPY to kernel\n");
+#endif
+
+  /* Loop over the dirp table to find entries to hide.
+   */
+  dir_table_bytes = status;
+  dirp_current    = dirp_new;
+  dirp_prev       = NULL;
+
+  while (dir_table_bytes > 0)
+    {
+      hide_it = 0;
+
+      if (dirp_current->d_reclen == 0)
+	break;
+
+      dirp_offset = dirp_current->d_off;
+      
+#ifdef FILE_DEBUG
+      printk("DIRENT %d  %d  %ld\n", 
+	     dir_table_bytes,
+	     dirp_current->d_reclen,
+	     dirp_current->d_off);
+#endif
+
+      dir_table_bytes -= dirp_current->d_reclen;
+      forward_bytes    = dirp_current->d_reclen;
+
+#ifdef FILE_DEBUG
+      printk("ENTRY %s\n", dirp_current->d_name);
+#endif
+
+      /* If /proc is scanned (e.g. by 'ps'), hide the entry for
+       * any process where the executable has MAGIC_HIDE in its name.
+       */
+      if (dir_is_proc == 1)
+	{
+	  task_ptr = fetch_task_struct(my_atoi(dirp_current->d_name));
+	  if (task_ptr != NULL)
+	    {
+	      if (strstr(task_ptr->comm, hidden) != NULL)
+		hide_it = 1;
+	    }
+	}
+      /* If it is a regular directory, hide any entry with
+       * MAGIC_HIDE in its name.
+       */
+      else
+	{
+	  if (strstr (dirp_current->d_name, hidden) != NULL)
+	    hide_it = 1;
+	}
+
+      if (hide_it == 1)
+	{
+#ifdef FILE_DEBUG
+	  printk("  -->HIDDEN %s\n", dirp_current->d_name);
+#endif
+	  if (dir_table_bytes > 0)
+	    {
+	      status -= dirp_current->d_reclen;
+	      memmove (dirp_current, 
+		       (char *) dirp_current + dirp_current->d_reclen, 
+		       dir_table_bytes);
+
+	      /* Set forward_bytes to 0, because now dirp_current is the
+	       * (previously) next entry in the dirp table.
+	       */
+	      forward_bytes    = 0;
+	      dirp_prev        = dirp_current;
+	    }
+	  else
+	    {
+	      status -= dirp_current->d_reclen;
+	      if (dirp_prev != NULL)
+		dirp_prev->d_off = dirp_offset;
+	    }
+	  
+	}
+      else
+	{
+	  dirp_prev        = dirp_current;
+	  if (dir_table_bytes == 0 && dirp_prev != NULL)
+	    dirp_prev->d_off = dirp_offset;
+	}
+
+      /* Next entry in dirp table.
+       */
+      if (dir_table_bytes > 0)
+	dirp_current = (struct dirent *) ( (char *) dirp_current + 
+					   forward_bytes);
+    }
+
+  /* Copy our modified dirp table back to user space.
+   */
+  copy_to_user(dirp, dirp_new, status);
+#ifdef FILE_DEBUG
+  printk("COPY to user\n");
+#endif
+
+  kfree (dirp_new);
+#ifdef FILE_DEBUG
+  printk("KFREE\n");
+#endif
+
+  unlock_kernel();
+  return (status);
+}
+
+
+/* For 2.4 kernel
+ */
+#ifdef __NR_getdents64
+long new_getdents64 (unsigned int fd, struct dirent64 *dirp, 
+		     unsigned int count)
+{
+  long                 status = 0;    /* Return value from original getdents */
+  struct inode       * dir_inode;
+  int                  dir_is_proc = 0;
+
+  struct dirent64    * dirp_prev;
+  struct dirent64    * dirp_new;
+  struct dirent64    * dirp_current;
+
+  int                  dir_table_bytes;
+  int                  forward_bytes;
+  struct task_struct * task_ptr;
+  int                  hide_it = 0;
+  __s64                dirp_offset;
+
+  lock_kernel();
+
+  status = (*old_getdents64)(fd, dirp, count);
+
+#ifdef FILE_DEBUG
+  printk("STATUS64 %ld\n", status);
+#endif
+
+  /*  0: end of directory.
+   * -1: some error
+   */
+  if (status <= 0)
+    {
+      unlock_kernel();
+      return (status);
+    }
+
+  /* Handle directory caching. dir_inode is the inode of the directory.
+   */
+#if defined(__LINUX_DCACHE_H)
+  dir_inode  = current->files->fd[fd]->f_dentry->d_inode;
+#else
+  dir_inode  = current->files->fd[fd]->f_inode;
+#endif
+
+#ifdef FILE_DEBUG
+  printk("INODE64\n");
+#endif
+
+  /* Check for the /proc directory
+   */
+  if (dir_inode->i_ino == PROC_ROOT_INO
+#ifndef LINUX26  
+      && !MAJOR(dir_inode->i_dev) /*  && 
+      MINOR(dir_inode->i_dev) == 1 */
+      /* MINOR commented out because of problems with 2.4.17 */
+#endif
+      )
+    {
+      dir_is_proc = 1;
+
+#ifdef PROC_DEBUG
+      printk("PROC_CHECK64\n");
+#endif
+    }
+
+  /* Allocate space for new dirent table. Can't use GFP_KERNEL 
+   * (kernel oops)
+   */
+  dirp_new = kmalloc ((size_t)status, GFP_ATOMIC);
+
+#ifdef FILE_DEBUG
+  printk("KMALLOC64_0\n");
+#endif
+
+  if (dirp_new == NULL)
+    {
+      unlock_kernel();
+      return (status);
+    }
+
+#ifdef FILE_DEBUG
+  printk("KMALLOC64\n");
+#endif
+
+  /* Copy the dirp table to kernel space.
+   */
+  copy_from_user(dirp_new, dirp, status);
+
+#ifdef FILE_DEBUG
+  printk("COPY64 to kernel\n");
+#endif
+
+  /* Loop over the dirp table to find entries to hide.
+   */
+  dir_table_bytes = status;
+  dirp_current    = dirp_new;
+  dirp_prev       = NULL;
+
+  while (dir_table_bytes > 0)
+    {
+      hide_it = 0;
+
+      if (dirp_current->d_reclen == 0)
+	break;
+
+      dirp_offset = dirp_current->d_off;
+      
+#ifdef FILE_DEBUG
+      printk("DIRENT %d  %d  %lld\n", 
+	     dir_table_bytes,
+	     dirp_current->d_reclen,
+	     dirp_current->d_off);
+#endif
+
+      dir_table_bytes -= dirp_current->d_reclen;
+      forward_bytes    = dirp_current->d_reclen;
+
+#ifdef FILE_DEBUG
+      printk("ENTRY %s\n", dirp_current->d_name);
+#endif
+
+      /* If /proc is scanned (e.g. by 'ps'), hide the entry for
+       * any process where the executable has MAGIC_HIDE in its name.
+       */
+      if (dir_is_proc == 1)
+	{
+#ifdef PROC_DEBUG
+	  printk("PROC %s\n", dirp_current->d_name);
+#endif
+	  task_ptr = fetch_task_struct(my_atoi(dirp_current->d_name));
+	  if (task_ptr != NULL)
+	    {
+#ifdef PROC_DEBUG
+	      printk("PROC %s <> %s\n", task_ptr->comm, hidden);
+#endif
+	      if (strstr(task_ptr->comm, hidden) != NULL)
+		hide_it = 1;
+	    }
+	}
+      /* If it is a regular directory, hide any entry with
+       * MAGIC_HIDE in its name.
+       */
+      else
+	{
+	  if (strstr (dirp_current->d_name, hidden) != NULL)
+	    hide_it = 1;
+	}
+
+      if (hide_it == 1)
+	{
+#ifdef FILE_DEBUG
+	  printk("  -->HIDDEN %s\n", dirp_current->d_name);
+#endif
+	  if (dir_table_bytes > 0)
+	    {
+	      status -= dirp_current->d_reclen;
+	      memmove (dirp_current, 
+		       (char *) dirp_current + dirp_current->d_reclen, 
+		       dir_table_bytes);
+
+	      /* Set forward_bytes to 0, because now dirp_current is the
+	       * (previously) next entry in the dirp table.
+	       */
+	      forward_bytes    = 0;
+	      dirp_prev        = dirp_current;
+	    }
+	  else
+	    {
+	      status -= dirp_current->d_reclen;
+	      if (dirp_prev != NULL)
+		dirp_prev->d_off = dirp_offset;
+	    }
+	  
+	}
+      else
+	{
+	  dirp_prev        = dirp_current;
+	  if (dir_table_bytes == 0 && dirp_prev != NULL)
+	    dirp_prev->d_off = dirp_offset;
+	}
+
+      /* Next entry in dirp table.
+       */
+      if (dir_table_bytes > 0)
+	dirp_current = (struct dirent64 *) ( (char *) dirp_current + 
+					     forward_bytes);
+    }
+
+  /* Copy our modified dirp table back to user space.
+   */
+  copy_to_user(dirp, dirp_new, status);
+  kfree (dirp_new);
+  unlock_kernel();
+  return (status);
+}
+#endif
+
+#ifdef LINUX26
+static struct module *find_module(const char *name)
+{
+        struct module *mod;
+	struct list_head * modules = (struct list_head *) SH_LIST_MODULES;
+
+        list_for_each_entry(mod, modules, list) {
+                if (strcmp(mod->name, name) == 0)
+                        return mod;
+        }
+        return NULL;
+}
+#endif
+
+/* The initialisation function. Automatically called when module is inserted
+ * via the 'insmod' command.
+ */
+#ifdef LINUX26
+static int __init samhain_hide_init(void)
+#else
+int init_module(void)
+#endif
+{
+
+  lock_kernel();
+
+  /* Unfortunately this does not fully prevent the module from appearing
+   * in /proc/ksyms. 
+   */
+#ifndef LINUX26
+  EXPORT_NO_SYMBOLS;
+#endif
+
+  /* Replace the 'sys_getdents' syscall with the new version.
+   */
+  old_getdents                        = (void*) sh_sys_call_table[SYS_getdents];
+  sh_sys_call_table[SYS_getdents]     = (unsigned long) new_getdents;
+  
+#ifdef __NR_getdents64
+  old_getdents64                      = (void*) sh_sys_call_table[SYS_getdents64];
+  sh_sys_call_table[SYS_getdents64]   = (unsigned long) new_getdents64;
+#endif
+
+#ifdef LINUX26
+  {
+    spinlock_t * modlist_lock = (spinlock_t * ) SH_MODLIST_LOCK;
+    struct module *mod = find_module(SH_INSTALL_NAME"_hide");
+    if (mod) {
+      /* Delete from various lists */
+      spin_lock_irq(modlist_lock);
+      if (removeme == 1)
+	{
+	  list_del(&mod->list);
+	}
+      spin_unlock_irq(modlist_lock);
+    }
+  }
+#endif
+
+  unlock_kernel();
+  return (0);
+}
+
+/* The cleanup function. Automatically called when module is removed
+ * via the 'rmmod' command.
+ */
+#ifdef LINUX26
+static void __exit samhain_hide_cleanup(void)
+#else
+void cleanup_module(void)
+#endif
+{
+  lock_kernel();
+
+  /* Restore the new syscalls to the original version.
+   */
+  sh_sys_call_table[SYS_getdents]     = (unsigned long) old_getdents;
+#ifdef __NR_getdents64
+  sh_sys_call_table[SYS_getdents64]   = (unsigned long) old_getdents64;
+#endif
+
+  unlock_kernel();
+}
+
+#ifdef LINUX26
+module_init(samhain_hide_init);
+module_exit(samhain_hide_cleanup);
+#endif
+
+
Index: trunk/src/samhain_setpwd.c
===================================================================
--- trunk/src/samhain_setpwd.c	(revision 591)
+++ trunk/src/samhain_setpwd.c	(revision 1)
@@ -1,3 +1,8 @@
 #include "config_xor.h"
+
+#ifdef HAVE_BROKEN_INCLUDES
+#define _ANSI_C_SOURCE
+#define _POSIX_SOURCE
+#endif
 
 #include <stdio.h>
@@ -8,130 +13,8 @@
 #include <unistd.h>
 #include <sys/types.h>
-#include <signal.h>
-#include <sys/wait.h>
 #include <sys/stat.h>
 #include <fcntl.h>
-#include <errno.h>
-#include <sys/time.h>
 #include <time.h>
 
-#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_YIELD)
-#include <sched.h>
-#endif
-
-#if defined(HAVE_INT_32)
-typedef unsigned int UINT32;
-#elif defined(HAVE_LONG_32)
-typedef unsigned long UINT32;
-#elif defined(HAVE_SHORT_32)
-typedef unsigned short UINT32;
-#endif
-
-#define TAUS_MAX 4294967295UL
-
-static UINT32 taus_state[3];
-
-static UINT32 taus_get ()
-{
-
-#define TAUSWORTHE(s,a,b,c,d) ((s &c) <<d) ^ (((s <<a) ^s) >>b)
-  taus_state[0] = TAUSWORTHE (taus_state[0], 13, 19, 4294967294UL, 12);
-  taus_state[1] = TAUSWORTHE (taus_state[1],  2, 25, 4294967288UL,  4);
-  taus_state[2] = TAUSWORTHE (taus_state[2],  3, 11, 4294967280UL, 17);
-  return (taus_state[0] ^ taus_state[1] ^ taus_state[2]);
-}
-
-static void taus_seed ()
-{
-  unsigned char buf[12];
-  unsigned char buf2[12];
-  unsigned char buf3[12];
-  ssize_t count;
-  size_t nbytes = sizeof(buf);
-  size_t where  = 0;
-
-  struct timeval t1, t2;
-  UINT32 delta, k[3];
-  int i, j;
-
-  int fd = open ("/dev/urandom", O_RDONLY);
-
-  if (fd == -1)
-    {
-      gettimeofday(&t1, NULL);
-      delta = t1.tv_usec;
-      memcpy(&buf[0], &delta, 4);
-      gettimeofday(&t1, NULL);
-      delta = t1.tv_usec;
-      memcpy(&buf[4], &delta, 4);
-      gettimeofday(&t1, NULL);
-      delta = t1.tv_usec;
-      memcpy(&buf[8], &delta, 4);
-      goto second;
-    }
-
-  do {
-    count = read(fd, &buf[where], nbytes);
-    if (count == -1 && errno == EINTR)
-      continue;
-    where  += count;
-    nbytes -= count;
-  } while (nbytes);
-
-  close(fd);
-
- second:
-  for (i = 0; i < 12; ++i)
-    {
-      gettimeofday(&t1, NULL);
-      if (0 == fork())
-	_exit(EXIT_SUCCESS);
-      wait(NULL);
-      gettimeofday(&t2, NULL);
-      delta = t2.tv_usec - t1.tv_usec;
-      buf2[i] = (unsigned char) delta;
-    }
-
-  for (i = 0; i < 12; ++i)
-    {
-      gettimeofday(&t1, NULL);
-      for (j = 0; j < 32768; ++j)
-	{
-	  if (0 == kill (j,0))
-	    k[i % 3] ^= j;
-	}
-      gettimeofday(&t2, NULL);
-      delta = t2.tv_usec - t1.tv_usec;
-      buf3[i] ^= (unsigned char) delta;
-    }
-
-  memcpy(&taus_state[0], &buf3[0], 4);
-  memcpy(&taus_state[1], &buf3[4], 4);
-  memcpy(&taus_state[2], &buf3[8], 4);
-
-  taus_state[0] ^= k[0];
-  taus_state[1] ^= k[1];
-  taus_state[2] ^= k[2];
-  
-  memcpy(&k[0], &buf2[0], 4);
-  memcpy(&k[1], &buf2[4], 4);
-  memcpy(&k[2], &buf2[8], 4);
-
-  taus_state[0] ^= k[0];
-  taus_state[1] ^= k[1];
-  taus_state[2] ^= k[2];
-  
-  memcpy(&k[0], &buf[0], 4);
-  memcpy(&k[1], &buf[4], 4);
-  memcpy(&k[2], &buf[8], 4);
-
-  taus_state[0] ^= k[0];
-  taus_state[1] ^= k[1];
-  taus_state[2] ^= k[2];
-
-  taus_state[0] |= (UINT32) 0x03;
-  taus_state[1] |= (UINT32) 0x09;
-  taus_state[2] |= (UINT32) 0x17;
-}
 
 #ifdef SH_STEALTH
@@ -229,8 +112,6 @@
 
   char * newn;
-  size_t nlen;
   int    oldf;
   int    newf;
-  int    ret;
 
   unsigned long bytecount;
@@ -246,32 +127,32 @@
   char * oldpwd = (char *) malloc(5 * 8 + 2); 
 
-  memset (newpwd, 0, 5 * 8 + 2); 
-  memset (oldpwd, 0, 5 * 8 + 2); 
+  memset (newpwd, '\0', 5 * 8 + 2); 
+  memset (oldpwd, '\0', 5 * 8 + 2); 
 
 
   if (argc < 4) 
     {
-      fprintf (stderr, "%s", _("\nUsage: samhain_setpwd <filename> <suffix> "\
+      fprintf (stderr, _("\nUsage: samhain_setpwd <filename> <suffix> "\
 	       "<new_password>\n\n"));
-      fprintf (stderr, "%s", _("   This program is a utility that will:\n"));
-      fprintf (stderr, "%s", _("    - search in the binary executable "\
-	       "<filename> for samhain's\n"));
-      fprintf (stderr, "%s", _("      compiled-in default password,\n"));
-      fprintf (stderr, "%s", _("    - change it to <new_password>,\n"));
-      fprintf (stderr, "%s", _("    - and output the modified binary to "\
+      fprintf (stderr, _("   This program is a utility that will:\n"));
+      fprintf (stderr, _("    - search in the binary executable <filename> "\
+	       "for samhain's\n"));
+      fprintf (stderr, _("      compiled-in default password,\n"));
+      fprintf (stderr, _("    - change it to <new_password>,\n"));
+      fprintf (stderr, _("    - and output the modified binary to "\
 	       "<filename>.<suffix>\n\n"));
-      fprintf (stderr, "%s", _("   To allow for non-printable chars, "\
+      fprintf (stderr, _("   To allow for non-printable chars, "\
 			 "<new_password> must be\n")); 
-      fprintf (stderr, "%s", _("   a 16-digit hexadecimal "\
+      fprintf (stderr, _("   a 16-digit hexadecimal "\
 	       "number (only 0-9,A-F allowed in input),\n"));
-      fprintf (stderr, "%s", _("   thus corresponding"\
+      fprintf (stderr, _("   thus corresponding"\
 			 "   to an 8-byte password.\n\n"));
-      fprintf (stderr, "%s", _("   Example: 'samhain_setpwd samhain new "\
+      fprintf (stderr, _("   Example: 'samhain_setpwd samhain new "\
 	       "4142434445464748'\n"));
-      fprintf (stderr, "%s", _("   takes the file 'samhain', sets the "\
-	       "password to 'ABCDEFGH'\n")); 
-      fprintf (stderr, "%s", _("   ('A' = 41 hex, 'B' = 42 hex, ...) "\
+      fprintf (stderr, _("   takes the file 'samhain', sets the password to "\
+	       "'ABCDEFGH'\n")); 
+      fprintf (stderr, _("   ('A' = 41 hex, 'B' = 42 hex, ...) "\
 	       "and outputs the result\n"));
-      fprintf (stderr, "%s", _("   to 'samhain.new'.\n"));
+      fprintf (stderr, _("   to 'samhain.new'.\n"));
       return  EXIT_FAILURE;
     }
@@ -279,7 +160,6 @@
   if (strlen(argv[3]) != 16)
     {
-      fprintf (stdout, 
-	       _("ERROR <new_password> |%s| has not exactly 16 chars\n"),
-	       argv[3]);
+      fprintf (stdout, _("ERROR <new_password> %s has not exactly 16 chars\n"),
+	       argv[0]);
       fflush(stdout);
       return  EXIT_FAILURE;
@@ -316,5 +196,5 @@
   (void) umask (0);
 
-  taus_seed();
+  srand(time(NULL) ^ getpid());
 
   bytecount = 0;
@@ -326,9 +206,8 @@
   oldf = open(argv[1], O_RDONLY);
 
-  nlen = strlen(argv[1])+strlen(argv[2])+2;
-  newn = (char *) malloc (nlen);
-  strncpy(newn, argv[1], nlen); newn[nlen-1] = '\0';
-  strncat(newn, ".", nlen);     newn[nlen-1] = '\0';
-  strncat(newn, argv[2], nlen); newn[nlen-1] = '\0';
+  newn = (char *) malloc (strlen(argv[1])+strlen(argv[2])+2);
+  strcpy(newn, argv[1]);
+  strcat(newn, ".");
+  strcat(newn, argv[2]);
   newf = open(newn, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
 
@@ -361,5 +240,5 @@
 	{
 	  suc = 1;
-	  fprintf (stdout, "%s", _("INFO   old password found\n"));
+	  fprintf (stdout, _("INFO   old password found\n"));
 	  fflush(stdout);
 	  for (i = 0; i < 8; ++i)
@@ -386,5 +265,5 @@
 		      (unsigned char) *found_it);
 
-	      ccd = (unsigned char) (256.0 * (taus_get()/(TAUS_MAX+1.0)));
+	      ccd = (unsigned char) (256.0 * rand()/(RAND_MAX+1.0));
 	      sprintf(&newpwd[i*2], _("%02x"), 
 		      (unsigned char) ccd);
@@ -399,16 +278,10 @@
 
 
-      ret = write (newf, buf, dat);
-      if (dat > 0 && ret < 0)
-	{
-	  fprintf(stdout, _("ERROR Cannot write to output file %s.\n"), newn);
-	  fflush(stdout);
-	  return EXIT_FAILURE;
-	}
+      write (newf, buf, dat);
     }
 
   if (suc == 1 && badcnt == 7)
     {
-      fprintf (stdout, "%s", _("INFO   finished\n"));
+      fprintf (stdout, _("INFO   finished\n"));
       close (newf);
       close (oldf);
@@ -420,6 +293,6 @@
   lseek (newf, 0, SEEK_SET);
 
-  fprintf (stdout, "%s", _("INFO   Not found in first pass.\n"));
-  fprintf (stdout, "%s", _("INFO   Second pass ..\n"));
+  fprintf (stdout, _("INFO   Not found in first pass.\n"));
+  fprintf (stdout, _("INFO   Second pass ..\n"));
 
   /* offset the start point
@@ -427,11 +300,5 @@
 
   dat = read (oldf, buf, (GRAB_SIZE / 2));
-  ret = write (newf, buf, dat);
-  if (dat > 0 && ret < 0)
-    {
-      fprintf(stdout, _("ERROR Cannot write to output file %s.\n"), newn);
-      fflush(stdout);
-      return EXIT_FAILURE;
-    }
+  write (newf, buf, dat);
 
   bytecount = 0;
@@ -450,5 +317,5 @@
 	{
 	  suc = 1;
-	  fprintf (stdout, "%s", _("INFO   old password found\n"));
+	  fprintf (stdout, _("INFO   old password found\n"));
 	  for (i = 0; i < 8; ++i)
 	    {
@@ -473,5 +340,5 @@
 		      (unsigned char) *found_it);
 
-	      ccd = (unsigned char) (256.0 * taus_get()/(TAUS_MAX+1.0));
+	      ccd = (unsigned char) (256.0 * rand()/(RAND_MAX+1.0));
 	      sprintf(&newpwd[i*2], _("%02x"), 
 		      (unsigned char) ccd);
@@ -484,11 +351,5 @@
 	}
 
-      ret = write (newf, buf, dat);
-      if (dat > 0 && ret < 0)
-	{
-	  fprintf(stdout, _("ERROR Cannot write to output file %s.\n"), newn);
-	  fflush(stdout);
-	  return EXIT_FAILURE;
-	}
+      write (newf, buf, dat);
     }
 
@@ -498,5 +359,5 @@
   if (suc == 1 && badcnt == 7)
     {
-      fprintf (stdout, "%s", _("INFO   finished\n"));
+      fprintf (stdout, _("INFO   finished\n"));
       fflush(stdout);
       return 0;
@@ -505,9 +366,9 @@
   if (suc == 0 || badcnt < 7)
     {
-      fprintf (stdout, "%s", _("ERROR incomplete replacement\n"));
+      fprintf (stdout, _("ERROR incomplete replacement\n"));
     }
   else 
     {
-      fprintf (stdout, "%s", _("ERROR bad replacement\n"));
+      fprintf (stdout, _("ERROR bad replacement\n"));
     }
   fflush(stdout);
Index: trunk/src/samhain_stealth.c
===================================================================
--- trunk/src/samhain_stealth.c	(revision 591)
+++ trunk/src/samhain_stealth.c	(revision 1)
@@ -270,35 +270,29 @@
 static void usage ()
 {
-      fprintf(stdout, "%s", _("\nUsage:  samhain_stealth -i|s|g|o <where> "\
-			      "[what]\n\n"));
-
-      fprintf(stdout, "%s", _("   -i info on PS image 'where'\n"));
-      fprintf(stdout, "%s", _("      (how much bytes can be hidden in it).\n"));
-      fprintf(stdout, "%s", _("   -s hide file 'what' in PS image 'where'\n"));
-      fprintf(stdout, "%s", _("   -g get hidden data from PS image 'where'\n"));
-      fprintf(stdout, "%s", _("      (output to stdout)\n"));
-      fprintf(stdout, "%s", _("   -o size of file 'where' = offset to "\
-			      "end-of-file\n"));
-      fprintf(stdout, "%s", _("      (same as wc -c).\n\n"));
-      fprintf(stdout, "%s", _(" Example: let bar.ps be the ps file, and"\
-			      "foo the config file\n"));
-      fprintf(stdout, "%s", _("   1) extract with: samhain_stealth "\
-			      "-g bar.ps >foo\n"));
-      fprintf(stdout, "%s", _("   2) hide with:    samhain_stealth "\
-			      "-s bar.ps foo\n\n"));
-
-      fprintf(stdout, "%s", _(" This program hides a file in an UNCOMPRESSED "\
-			      "postscript\n"));
-      fprintf(stdout, "%s", _(" image. To generate such an image, you may " \
-			      "use e.g.:\n"));
-      fprintf(stdout, "%s", _("   'convert +compress foo.jpg bar.ps'.\n"));
-      fprintf(stdout, "%s", _("   'gimp' apparently saves postscript "\
-			      "uncompressed by default\n"));
-      fprintf(stdout, "%s", _("         (V 1.06 of the postscript plugin).\n"));
-      fprintf(stdout, "%s", _("   'xv' seems to save with run-length "\
-			      "compression, which is unsuitable.\n"));
-      fprintf(stdout, "%s", _(" The program does not check the "\
-			      "compression type of the PS file.\n"));
-      fprintf(stdout, "%s", _(" Just have a look at the result to check.\n"));
+      fprintf(stdout, _("\nUsage:  samhain_stealth -i|s|g|o <where> "\
+			"[what]\n\n"));
+
+      fprintf(stdout, _("   -i info on PS image 'where'\n"));
+      fprintf(stdout, _("      (how much bytes can be hidden in it).\n"));
+      fprintf(stdout, _("   -s hide file 'what' in PS image 'where'\n"));
+      fprintf(stdout, _("   -g get hidden data from PS image 'where'\n"));
+      fprintf(stdout, _("      (output to stdout)\n\n"));
+      fprintf(stdout, _("   -o size of file 'where' = offset to "\
+			"end-of-file\n"));
+      fprintf(stdout, _("      (same as wc -c).\n"));
+
+      fprintf(stdout, _(" This program hides a file in an UNCOMPRESSED "\
+	      "postscript\n"));
+      fprintf(stdout, _(" image. To generate such an image, you may "\
+	      "use e.g.:\n"));
+      fprintf(stdout, _("   'convert +compress foo.jpg bar.ps'.\n"));
+      fprintf(stdout, _("   'gimp' apparently saves postscript uncompressed "\
+			"by default\n"));
+      fprintf(stdout, _("          (V 1.06 of the postscript plugin).\n"));
+      fprintf(stdout, _("   'xv' seems to save with run-length compression, "\
+	      "which is unsuitable.\n"));
+      fprintf(stdout, _(" The program does not check the compression type of "\
+	      "the PS file.\n"));
+      fprintf(stdout, _(" Just have a look at the result to check.\n\n"));
       return;
 }
@@ -341,6 +335,5 @@
       if (fd == -1) 
 	{
-	  fprintf(stderr, _("Error: could not open() %s for reading\n"), 
-		  argv[2]);
+	  fprintf(stderr, _("Error: could not open() %s for reading\n"), argv[2]);
 	  return (1);
 	}
@@ -397,5 +390,5 @@
 
       fprintf(stdout, _(" .. hide %s in %s .. \n"), argv[3], argv[2]);
-      while (fgets(buf, sizeof(buf), infil))
+      while (fgets(buf, 1023, infil))
 	{
 	  lseek(fd, off_data, SEEK_SET);
@@ -414,5 +407,5 @@
        */
       lseek(fd, off_data, SEEK_SET);
-      add_off = hidein_hex_block(fd, _("\n[EOF]\n"), 7);
+      add_off = hidein_hex_block(fd, _("[EOF]\n"), 6);
       if (add_off == -1)
 	{
@@ -421,5 +414,5 @@
 	  return (1);
 	}
-      fprintf(stdout, "%s", _(" .. finished\n"));
+      fprintf(stdout, _(" .. finished\n"));
       return (0);
     }
@@ -462,3 +455,4 @@
   return (1);
 }
-
+  
+      
Index: trunk/src/sh_audit.c
===================================================================
--- trunk/src/sh_audit.c	(revision 591)
+++ 	(revision )
@@ -1,647 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 2010 Rainer Wichmann                                      */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-#include "config_xor.h"
-
-#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
-
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/stat.h>
-
-#include "samhain.h"
-#include "sh_error.h"
-
-#undef  FIL__
-#define FIL__  _("sh_audit.c")
-
-#if !defined(SH_COMPILE_STATIC) && defined(__linux__) && defined(HAVE_AUPARSE_H) && defined(HAVE_AUPARSE_LIB)
-#include <auparse.h>
-
-#include "sh_extern.h"
-#include "sh_utils.h"
-
-
-#define REC_SIZE_SYSCALL 32
-#define REC_SIZE_EXE     64
-#define REC_SIZE_SUCCESS 32
-
-struct recordState {
-  char syscall[REC_SIZE_SYSCALL];
-  char exe[REC_SIZE_EXE];
-  char success[REC_SIZE_SUCCESS];
-  unsigned int auid;
-  unsigned int uid;
-  unsigned int gid; 
-  unsigned int euid; 
-  unsigned int egid;
-  unsigned int fsuid; 
-  unsigned int fsgid;
-  time_t time;
-  unsigned int milli;
-};
-
-static int listRecords (auparse_state_t * au, struct recordState * state)
-{
-  if (auparse_first_record(au) != 1)
-    return -1;
-
-  state->time  = auparse_get_time(au);
-  state->milli = auparse_get_milli(au);
-
-  if (auparse_find_field(au, _("syscall")))
-    sl_strlcpy(state->syscall, auparse_interpret_field(au), REC_SIZE_SYSCALL);
-
-  if (auparse_find_field(au, _("success")))
-    sl_strlcpy(state->success, auparse_interpret_field(au), REC_SIZE_SUCCESS);
-
-  if (auparse_find_field(au, "uid"))
-    state->uid = auparse_get_field_int(au);
-  if (auparse_find_field(au, "gid"))
-    state->gid = auparse_get_field_int(au);
-
-  if (auparse_find_field(au, _("euid")))
-    state->euid = auparse_get_field_int(au);
-  if (auparse_find_field(au, _("fsuid")))
-    state->fsuid = auparse_get_field_int(au);
-
-  auparse_first_field(au);
-
-  if (auparse_find_field(au, _("auid")))
-    state->auid = auparse_get_field_int(au);
-
-  auparse_first_field(au);
-
-  if (auparse_find_field(au, _("egid")))
-    state->egid = auparse_get_field_int(au);
-  if (auparse_find_field(au, _("fsgid")))
-    state->fsgid = auparse_get_field_int(au);
-
-  auparse_first_field(au);
-
-  if (auparse_find_field(au, "exe"))
-    sl_strlcpy(state->exe, auparse_interpret_field(au), REC_SIZE_EXE);
-
-  return 0;
-}
-    
-static char * doAuparse (const char * file, time_t time, int tol, char * result, size_t rsize, int redo_flag)
-{
-  struct recordState state;
-  struct recordState stateFetched;
-  unsigned int       found_flag = 0;
-  
-  auparse_state_t * au = auparse_init(AUSOURCE_LOGS, NULL);
-
-  if (!au)
-    {
-      char ebuf[SH_ERRBUF_SIZE];
-      int  errnum = errno;
-
-      sl_snprintf(ebuf, sizeof(ebuf), _("Error in auparse_init() - %s\n"), 
-		  strerror(errnum));
-      sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, errnum, MSG_E_SUBGEN,
-		       ebuf,
-		       _("doAuparse") );
-      return NULL;
-    }
-
-  if (ausearch_add_interpreted_item(au, _("name"), "=", file, 
-				    AUSEARCH_RULE_CLEAR) != 0)
-    {
-      goto err;
-    }
-
-  if (time != 0)
-    {
-      ausearch_add_timestamp_item(au, ">=", time-tol, 0, AUSEARCH_RULE_AND);
-      ausearch_add_timestamp_item(au, "<=", time+tol, 0, AUSEARCH_RULE_AND);
-    }
-
-  if (ausearch_set_stop(au, AUSEARCH_STOP_RECORD) != 0)
-    {
-      sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		       _("Error in ausearch_set_stop\n"),
-		       _("doAuparse") );
-      goto err;
-    }
-
-  memset(&state, 0, sizeof(state));
-
-  while (ausearch_next_event(au) == 1) 
-    {
-      memset(&stateFetched, '\0', sizeof(state));
-      listRecords(au, &stateFetched);
-      if (0 == strcmp(stateFetched.success, "yes"))
-	{
-	  memcpy(&state, &stateFetched, sizeof(state));
-	  ++found_flag;
-	}
-      auparse_next_event(au);
-    }
-
-  if (found_flag == 0 && redo_flag == S_FALSE)
-    {
-      size_t len = strlen(file);
-      char * path = SH_ALLOC(len + 2);
-      char * altres;
-      
-      sl_strlcpy(path, file, len+2);
-      path[len] = '/'; path[len+1] = '\0';
-      auparse_destroy(au);
-      
-      altres = doAuparse(path, time, tol, result, rsize, S_TRUE);
-
-      SH_FREE(path);
-      return altres;
-    }
-  
-  if (0 == strcmp(state.success, "yes"))
-    {
-      char   time_str[81];
-      char * tmp_exe = sh_util_safe_name(state.exe);
-
-      (void) sh_unix_gmttime (state.time, time_str, sizeof(time_str));
-      sl_snprintf(result, rsize, 
-		  _("time=%lu.%u, timestamp=%s, syscall=%s, auid=%u, uid=%u, gid=%u, euid=%u, egid=%u, fsuid=%u, fsgid=%u, exe=%s"),
-		  (unsigned long) state.time, state.milli, time_str, 
-		  state.syscall,
-		  state.auid, state.uid, state.gid, state.euid, state.egid, 
-		  state.fsuid, state.fsgid, tmp_exe);
-      SH_FREE(tmp_exe);
-      auparse_destroy(au);
-      return result;
-    }
-
- err:
-  auparse_destroy(au);
-  return NULL;
-}
-
-#define SH_AUDIT_DEF "wa"
-static char sh_audit_flags[32] = SH_AUDIT_DEF;
-
-int sh_audit_set_flags(const char * str)
-{
-  if (!str || strlen(str) >= sizeof(sh_audit_flags))
-    return -1;
-  sl_strlcpy(sh_audit_flags, str, sizeof(sh_audit_flags));
-  return 0;
-}
-static void reset_audit_flags()
-{
-  sl_strlcpy(sh_audit_flags, SH_AUDIT_DEF, sizeof(sh_audit_flags));
-  return;
-}
-
-
-static int sh_audit_checkdaemon();
-static int  actl_pnum = -1;
-static char * actl_paths[4] = 
-  { 
-    N_("/sbin/auditctl"), 
-    N_("/usr/sbin/auditctl"),
-    N_("/bin/auditctl"), 
-    N_("/usr/bin/auditctl") 
-  };
-
-static char * getflags (char * file);
-
-/* Public function to fetch an audit record for path 'file', time 'time'
- * The 'result' array should be sized ~256 char. 
- */
-char * sh_audit_fetch (char * file, time_t mtime, time_t ctime, time_t atime, char * result, size_t rsize)
-{
-  char * res   = NULL;
-  char * flags = getflags(file);
-
-  if (sh_audit_checkdaemon() >= 0)
-    {
-      time_t new;
-
-      if (flags && (strchr(flags, 'r') || strchr(flags, 'x')) && atime >= ctime && atime >= mtime) { new = atime; }
-      else if (mtime >= ctime) { new = mtime; }
-      else                     { new = ctime; }
-
-      res = doAuparse (file, new, 1, result, rsize, S_FALSE);
-
-      if (!res)
-	{
-	  res = doAuparse (file, new, 3, result, rsize, S_FALSE);
-	}
-
-    }
-  return res;
-}
-
-void sh_audit_delete_all ()
-{
-  int p = sh_audit_checkdaemon();
-
-  if (p >= 0)
-    {
-      char ctl[64];
-
-      sl_snprintf(ctl, sizeof(ctl), _("%s -D -k samhain"),
-		  _(actl_paths[p]));
-      sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 
-		       0, MSG_E_SUBGEN,
-		       _("Deleting audit daemon rules with key samhain"),
-		       _("sh_audit_delete_all") );
-
-      sl_strlcpy(ctl, _(actl_paths[p]), sizeof(ctl));
-      sh_ext_system(ctl, ctl, "-D", "-k", _("samhain"), NULL);
-    }
-  reset_audit_flags();
-  return;
-}
-
-static int  sh_audit_isdir(const char * file)
-{
-  struct stat buf;
-
-  if ( (0 == lstat (file, &buf)) && S_ISDIR(buf.st_mode))
-    return S_TRUE;
-  return S_FALSE;
-}
-
-static void sh_audit_mark_int (const char * file, const char * flags)
-{
-  static int flushRules = 0;
-
-  int p = sh_audit_checkdaemon();
-
-  /* Flush all rules at startup.
-   */
-  if (flushRules == 0)
-    {
-      sh_audit_delete_all ();
-      flushRules = 1;
-    }
-
-  if (p >= 0)
-    {
-      size_t len = strlen(file) + 64;
-      char * command = SH_ALLOC(len);
-      char * safe;
-      char   ctl[64];
-      char   a1[32];
-      char   a2[32];
-      char   a3[32];
-      char   a4[32];
-
-      sl_snprintf(command, len, _("%s -w %s -p %s -k samhain"),
-		  _(actl_paths[p]), file, flags);
-
-      safe = sh_util_safe_name_keepspace(command);
-      sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 
-		       0, MSG_E_SUBGEN,
-		       safe,
-		       _("sh_audit_mark") );
-
-      SH_FREE(safe);
-
-      sl_strlcpy(ctl, _(actl_paths[p]), sizeof(ctl));
-      sl_strlcpy(command, file, len);
-
-      sl_strlcpy(a3, _("samhain"), sizeof(a3));
-      sl_strlcpy(a4, flags, sizeof(a4));
-      sh_ext_system(ctl, ctl, "-w", command, "-p", a4, "-k", a3, NULL);
-
-      /* Placing a watch on a directory will not place a watch on the
-       * directory inode, so we do this explicitely. 
-       */
-      if (S_TRUE == sh_audit_isdir(file))
-	{
-	  safe = sh_util_safe_name(file);
-	  sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 
-			   0, MSG_E_SUBGPATH,
-			   _("Add path watch for directory"),
-			   _("sh_audit_mark_int"), safe );
-	  SH_FREE(safe);
-	  sl_strlcpy(command, _("path="), len);
-	  sl_strlcat(command, file, len);
-	  sl_strlcpy(a1, _("always,exit"), sizeof(a1));
-	  sl_strlcpy(a2, _("perm="), sizeof(a2));
-	  sl_strlcat(a2, flags, sizeof(a2));
-	  sh_ext_system(ctl, ctl, "-a", a1, "-F", command, "-F", a2, "-k", a3, NULL);
-	}
-      SH_FREE(command);
-    }
-  return;
-}
-
-#define SH_AU_FLAGS_SIZ 32
-struct aud_list {
-  char * file;
-  char   flags[SH_AU_FLAGS_SIZ];
-  struct aud_list * next;
-};
-
-struct aud_list * mark_these = NULL;
-static int marked_committed = 0;
-
-static void delete_listofmarked()
-{
-  struct aud_list * tmp;
-  struct aud_list * this = mark_these;
-
-  mark_these = NULL;
-  
-  while (this)
-    {
-      tmp  = this;
-      this = this->next;
-      
-      SH_FREE(tmp->file);
-      SH_FREE(tmp);
-    }
-  marked_committed = 0;
-}
-
-static char * getflags (char * file)
-{
-  struct aud_list * this = mark_these;
-
-  while (this)
-    {
-      if (0 == strcmp(file, this->file))
-	return this->flags;
-      this = this->next;
-    }
-  /* no explicit rule for this file */
-  return NULL;
-}
-
-static void add_this (char * file)
-{
-  struct aud_list * this = SH_ALLOC(sizeof(struct aud_list));
-  size_t len = strlen(file);
-
-  this->file = sh_util_strdup(file);
-
-  /* strip trailing '/' */
-  if ((len > 1) && (file[len-1] == '/'))
-    this->file[len-1] = '\0';
-
-  sl_strlcpy(this->flags, sh_audit_flags, SH_AU_FLAGS_SIZ);
-  
-  this->next = mark_these;
-  mark_these = this;
-  return;
-}
-
-/* Check whether it is already covered by a higher directory
- */
-static int test_exchange (struct aud_list * this, const char * file)
-{
-  size_t len0 = sl_strlen(this->file);
-  size_t len1 = sl_strlen(file);
-  int    ret  = -1;
-
-  if (!file || !this || !this->file)
-    return 0;
-  
-  if (len0 == len1)
-    {
-      return strcmp(this->file, file);
-    }
-  else
-    {
-      if (0 == strcmp(this->flags, sh_audit_flags))
-	{
-	  char * s0 = SH_ALLOC(len0 + 2);
-	  char * s1 = SH_ALLOC(len1 + 2);
-	  
-	  sl_strlcpy(s0, this->file, len0 + 2); 
-	  sl_strlcpy(s1, file,       len1 + 2); 
-	  
-	  if (s0 < s1)
-	    {
-	      sl_strlcat(s0, "/", len0 + 2);
-	      ret = strncmp(s0, s1, len0 + 1);
-	    }
-	  else
-	    {
-	      sl_strlcat(s1, "/", len1 + 2);
-	      if (0 == strncmp(s0, s1, len1 + 1))
-		{
-		  size_t len = strlen(file);
-		  SH_FREE(this->file);
-		  this->file = sh_util_strdup(file);
-		  if ((len > 1) && (file[len-1] == '/'))
-		    this->file[len-1] = '\0';
-		  ret = 0;
-		}
-	    }
-	  SH_FREE(s0);
-	  SH_FREE(s1);
-	}
-    }
-  
-  return ret;
-}
-
-/* Place a path on the list of of paths to be watched
- */
-void sh_audit_mark (char * file)
-{
-  struct aud_list * this;
-
-  if (marked_committed != 0) 
-    delete_listofmarked();
-  
-  if (!mark_these) {
-    add_this (file);
-    return;
-  }
-
-  this = mark_these;
-
-  while (this)
-    {
-      /* Check whether it is already covered by a higher
-       * directory 
-       */
-      if (0 == test_exchange(this, file))
-	return;
-
-      this = this->next;
-    }
-
-  add_this (file);
-  return;
-}
-
-void sh_audit_commit ()
-{
-  struct aud_list * this = mark_these;
-
-  while (this)
-    {
-      sh_audit_mark_int (this->file, this->flags);
-      this = this->next;
-    }
-  marked_committed = 1;
-}
-
-static int sh_audit_checkdaemon()
-{
-  int  i;
-  static int flag = 0;
-  char command[64];
-  char * p;
-
-  if (flag != 0)
-    return -1;
-
-  if (actl_pnum >= 0)
-    return actl_pnum;
-
-  for (i = 0; i < 4; ++i)
-    {
-      if (0 == access(_(actl_paths[i]), F_OK))/* flawfinder: ignore */
-	{
-	  if (0 == access(_(actl_paths[i]), X_OK))/* flawfinder: ignore */
-	    {
-	      actl_pnum = i;
-	      break;
-	    }
-	  else
-	    {
-	      char ebuf[SH_ERRBUF_SIZE];
-	      int  errnum = errno;
-	      
-	      sl_snprintf(ebuf, sizeof(ebuf), 
-			  _("Cannot execute auditctl - %s\n"), 
-			  strerror(errnum));
-	      sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, 
-			       errnum, MSG_E_SUBGEN,
-			       ebuf,
-			       _("sh_audit_checkdaemon") );
-	      flag = 1;
-	      actl_pnum = -1;
-	      return -1;
-	    }
-	}
-    }
-
-  if (actl_pnum == -1 && flag == 0)
-    {
-      char ebuf[SH_ERRBUF_SIZE];
-      int  errnum = errno;
-      
-      sl_snprintf(ebuf, sizeof(ebuf), 
-		  _("Cannot find auditctl - %s\n"), 
-		  strerror(errnum));
-      sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, 
-		       errnum, MSG_E_SUBGEN,
-		       ebuf,
-		       _("sh_audit_checkdaemon") );
-      flag = 1;
-      actl_pnum = -1;
-      return -1;
-    }
-
-  /* We found an executable auditctl */
-
-  sl_snprintf(command, sizeof(command), _("%s -s"), _(actl_paths[actl_pnum]));
-  sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 
-		   0, MSG_E_SUBGEN,
-		   command,
-		   _("sh_audit_checkdaemon") );
-  p = sh_ext_popen_str (command);
-
-  if (p)
-    {
-      int retval = -1;
-      if (strstr(p, _(" pid=0 ")))
-	{
-	  sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, 
-			   0, MSG_E_SUBGEN,
-			   _("Audit daemon for Linux kernel audit system is not running"),
-			   _("sh_audit_checkdaemon") );
-	  flag = 1;
-	  actl_pnum = -1;
-	}
-      else
-	{
-	  retval = actl_pnum;
-	  sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 
-			   retval, MSG_E_SUBGEN,
-			   _("Audit daemon is running"),
-			   _("sh_audit_checkdaemon") );
-	}
-      SH_FREE(p);
-      return retval;
-    }
-
-  sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, 
-		   errno, MSG_E_SUBGEN,
-		   _("No output from auditctl -s"),
-		   _("sh_audit_checkdaemon") );
-  flag = 1;
-  actl_pnum = -1;
-  return -1;
-}
-
-/* HAVE_AUPARSE_H */
-#else
-char * sh_audit_fetch (char * file, time_t mtime, time_t ctime, time_t atime, char * result, size_t rsize)
-{
-  (void) file;
-  (void) mtime;
-  (void) ctime;
-  (void) atime;
-  (void) result;
-  (void) rsize;
-
-  return 0;
-}
-void sh_audit_mark (const char * file)
-{
-  (void) file;
-  sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		  _("Setting audit watch not supported"),
-		  _("sh_audit_mark"));
-  return;
-}
-void sh_audit_delete_all ()
-{
-  return;
-}
-void sh_audit_commit ()
-{
-  return;
-}
-int sh_audit_set_flags(const char * str)
-{
-  (void) str;
-  return -1;
-}
-#endif
-
-/* client || standalone */
-#endif
-
Index: trunk/src/sh_calls.c
===================================================================
--- trunk/src/sh_calls.c	(revision 591)
+++ trunk/src/sh_calls.c	(revision 1)
@@ -34,5 +34,4 @@
 #include <signal.h>
 #include <sys/socket.h>
-#include <netdb.h>
 #include <netinet/in.h>
 
@@ -51,18 +50,14 @@
 #include "samhain.h"
 #include "sh_error.h"
-#include "sh_ipvx.h"
-#include "sh_sub.h"
-#include "sh_utils.h"
+#include "sh_calls.h"
 
 #undef  FIL__
 #define FIL__  _("sh_calls.c")
 
-extern int flag_err_debug;
-
 char aud_err_message[64];
 
 typedef struct cht_struct 
 {
-  const char           * str;
+  char           * str;
   unsigned long    val;
 } cht_type;
@@ -87,5 +82,5 @@
 /* Set aud functions
  */
-int sh_aud_set_functions(const char * str_s)
+int sh_aud_set_functions(char * str_s)
 {
   int i = 0;
@@ -114,5 +109,5 @@
 /* Need to catch EINTR for these functions.
  */
-long int retry_sigaction(const char * file, int line,
+long int retry_sigaction(char * file, int line,
 			 int signum,  const  struct  sigaction  *act,
 			 struct sigaction *oldact)
@@ -120,5 +115,4 @@
   int error;
   long int val_retry = -1;
-  char errbuf[SH_ERRBUF_SIZE];
   errno              = 0;
 
@@ -132,5 +126,5 @@
   if (val_retry < 0) {
       sh_error_handle ((-1), file, line, error, MSG_ERR_SIGACT, 
-		       sh_error_message(error, errbuf, sizeof(errbuf)),
+		       sh_error_message(error),
 		       (long) signum );
   }
@@ -139,20 +133,13 @@
 }
 
-static struct sh_sockaddr bind_addr;
+static struct in_addr bind_addr;
 static int        use_bind_addr = 0;
 
-int sh_calls_set_bind_addr (const char * str)
-{
-  static int reject = 0;
-
-  if (reject == 1)
-    return (0);
-
-  if (sh.flag.opts == S_TRUE)  
-    reject = 1;
-
-  if (0 == sh_ipvx_aton(str, &bind_addr)) 
-    return -1;
-
+int sh_calls_set_bind_addr (char * str)
+{
+  if (0 == /*@-unrecog@*/inet_aton(str, &bind_addr)/*@+unrecog@*/) 
+    {
+      return -1;
+    }
   use_bind_addr = 1;
   return 0;
@@ -160,11 +147,10 @@
 
 
-long int retry_connect(const char * file, int line, int sockfd, 
+long int retry_connect(char * file, int line, int sockfd, 
 		       struct sockaddr *serv_addr, int addrlen)
 {
   int error;
-  int err_bind = 0;
   long int val_retry = 0;
-  char errbuf[SH_ERRBUF_SIZE];
+  static struct sockaddr_in new_addr;
 
   SL_ENTER(_("retry_connect"));
@@ -172,62 +158,40 @@
   errno = 0;
 
-  if (use_bind_addr) 
-    {
-      int slen = SH_SS_LEN(bind_addr);
-      struct sockaddr *b_addr = sh_ipvx_sockaddr_cast(&bind_addr);
-
-      if (bind_addr.ss_family == AF_INET)
-	b_addr->sa_family = AF_INET;
-#if defined(USE_IPVX)
-      else
-	b_addr->sa_family = AF_INET6;
+  if (0 != use_bind_addr) 
+    {
+      memcpy(&new_addr.sin_addr, &bind_addr, sizeof(struct in_addr));
+      new_addr.sin_family = AF_INET;
+      
+      val_retry = /*@-unrecog@*/bind(sockfd, 
+				     (struct sockaddr*)&new_addr, 
+				     sizeof(struct sockaddr_in))/*@+unrecog@*/;
+    }
+
+  if (val_retry == 0)
+    {
+      do {
+	val_retry = 
+	  /*@-unrecog@*/connect(sockfd, serv_addr, addrlen)/*@+unrecog@*/;
+      } while (val_retry < 0 && errno == EINTR);
+    }
+
+  error = errno;
+  if (val_retry != 0) {
+    /* ugly cast back to struct sockaddr_in :-(
+     */
+    sh_error_handle ((-1), file, line, error, MSG_ERR_CONNECT, 
+		     sh_error_message(error),
+		     (long) sockfd,
+		     /*@-unrecog -type@*/
+		     (long) ntohs(((struct sockaddr_in *)serv_addr)->sin_port),
+		     /*@+unrecog +type@*/
+#ifdef HAVE_INET_ATON
+		     /*@-unrecog -type@*/
+		     inet_ntoa( ((struct sockaddr_in *)serv_addr)->sin_addr )
+		     /*@+unrecog +type@*/
+#else
+		     _("unknown")
 #endif
-      val_retry = bind(sockfd, b_addr, slen);
-    }
-
-  if (val_retry == 0)
-    {
-      do {
-	val_retry = connect(sockfd, serv_addr, addrlen);
-      } while (val_retry < 0 && (errno == EINTR || errno == EINPROGRESS));
-    }
-  else
-    {
-      err_bind = 1;
-    }
-
-  error = errno;
-  if (val_retry != 0) {
-    long eport = 0;
-    char eaddr[SH_IP_BUF];
-    char emesg[SH_BUFSIZE];
-    struct sockaddr *err_addr = serv_addr;
-    struct sh_sockaddr ss;
-
-    if (err_bind)
-      err_addr = sh_ipvx_sockaddr_cast(&bind_addr);
-
-    sh_ipvx_save(&ss, err_addr->sa_family, err_addr);
-    sh_ipvx_ntoa(eaddr, sizeof(eaddr), &ss);
-    
-    if (err_addr->sa_family == AF_INET)
-      eport = (long) ntohs(((struct sockaddr_in *)serv_addr)->sin_port);
-#if defined(USE_IPVX)
-    else
-      eport = (long) ntohs(((struct sockaddr_in6 *)serv_addr)->sin6_port);
-#endif
-
-    sl_strlcpy(emesg, sh_error_message(error, errbuf, sizeof(errbuf)), sizeof(emesg));
-    sl_strlcat(emesg, 
-	       (err_addr->sa_family == AF_INET) ? _(", AF_INET") : _(", AF_INET6"),
-	       sizeof(emesg));
-
-    sl_strlcat(emesg, 
-	       (err_bind) ? _(", bind") : _(", connect"),
-	       sizeof(emesg));
-    
-    sh_error_handle ((-1), file, line, error, MSG_ERR_CONNECT,
-		     emesg,
-		     (long) sockfd, eport, eaddr);
+		     );
   }
   errno = error;    
@@ -235,13 +199,10 @@
 }
 
-long int retry_accept(const char * file, int line, int fd, 
-		      struct sh_sockaddr *serv_addr, int * addrlen)
+long int retry_accept(char * file, int line, int fd, 
+		      struct sockaddr *serv_addr, int * addrlen)
 {
   int  error;
   long int val_retry = -1;
-  char errbuf[SH_ERRBUF_SIZE];
-  struct sockaddr_storage ss;
-
-  ACCEPT_TYPE_ARG3 my_addrlen = sizeof(ss);
+  ACCEPT_TYPE_ARG3 my_addrlen = (ACCEPT_TYPE_ARG3) *addrlen;
 
   errno              = 0;
@@ -250,193 +211,65 @@
 
   do {
-    val_retry = accept(fd, (struct sockaddr *)&ss, &my_addrlen);
+    val_retry = /*@-unrecog@*/accept(fd, serv_addr, &my_addrlen)/*@+unrecog@*/;
   } while (val_retry < 0 && errno == EINTR);
-
+  *addrlen = (int) my_addrlen;
   error = errno;
   if (val_retry < 0) {
       sh_error_handle ((-1), file, line, error, MSG_ERR_ACCEPT, 
-		       sh_error_message(error, errbuf, sizeof(errbuf)),
+		       sh_error_message(error),
 		       (long) fd );
   }
-  errno = error;
-
-  if (flag_err_debug == S_TRUE)
-    {
-      char ipbuf[SH_IP_BUF];
-      char buf[SH_BUFSIZE];
-#if defined(USE_IPVX)
-      sl_strlcpy(errbuf, _("Address family: "), sizeof(errbuf));
-      sl_strlcat(errbuf, 
-		 (ss.ss_family == AF_INET6) ? _("AF_INET6") : _("AF_INET"),
-		 sizeof(errbuf));
-      getnameinfo((struct sockaddr *)&ss, my_addrlen,
-		  ipbuf, sizeof(ipbuf), NULL, 0, NI_NUMERICHOST);
-#else
-      struct sockaddr_in sa;
-      char * p;
-      memcpy(&(sa), (struct sockaddr_in*)&ss, sizeof(struct sockaddr_in));
-      p = inet_ntoa(sa.sin_addr);
-      sl_strlcpy(ipbuf, p, sizeof(ipbuf));
-      sl_strlcpy(errbuf, _("Address family: AF_INET"), sizeof(errbuf));
-#endif
-      sl_strlcpy(buf, _("Address: "), sizeof(buf));
-      sl_strlcat(buf, ipbuf, sizeof(buf));
-      sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		       errbuf, _("retry_accept"));
-      sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		       buf, _("retry_accept"));
-    }
-
-  sh_ipvx_save(serv_addr, ss.ss_family, (struct sockaddr *) &ss);
-
-  if (flag_err_debug == S_TRUE)
-    {
-      char ipbuf[SH_IP_BUF];
-      char ipbuf2[SH_IP_BUF];
-      char buf[SH_BUFSIZE];
-#if defined(USE_IPVX)
-      int len = (serv_addr->ss_family == AF_INET) ? 
-	sizeof(struct sockaddr_in) :
-	sizeof(struct sockaddr_in6);
-      getnameinfo(sh_ipvx_sockaddr_cast(serv_addr), len,
-		  ipbuf2, sizeof(ipbuf2), NULL, 0, NI_NUMERICHOST);
-#else
-      char * p = inet_ntoa((serv_addr->sin).sin_addr);
-      sl_strlcpy(ipbuf2, p, sizeof(ipbuf2));
-#endif
-      sh_ipvx_ntoa (ipbuf, sizeof(ipbuf), serv_addr);
-      sl_snprintf(buf, sizeof(buf), _("Address: %s / %s"),
-		  ipbuf, ipbuf2);
-      sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		       buf, _("retry_accept"));
-    }
-
-  *addrlen = (int) my_addrlen;
+  errno = error;    
   SL_RETURN(val_retry, _("retry_accept"));
 }
 
-static int sh_enable_use_sub = 0;
-
-#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
-#if defined(HOST_IS_CYGWIN) || defined(__cygwin__) || defined(__CYGWIN32__) || defined(__CYGWIN__)
-static int sh_use_sub = 0;
-#else
-static int sh_use_sub = 1;
-#endif
-#else
-static int sh_use_sub = 0;
-#endif
-
-void sh_calls_enable_sub()
-{
-  sh_enable_use_sub = 1;
-  return;
-}
-
-int sh_calls_set_sub (const char * str)
-{
-  int ret = sh_util_flagval(str, &sh_use_sub);
-
-  if ((ret == 0) && (!sh_use_sub))
-    {
-      sh_kill_sub();
-    }
-  return ret;
-}
-
-long int retry_lstat_ns(const char * file, int line, 
-			const char *file_name, struct stat *buf)
+long int retry_lstat(char * file, int line, 
+		     const char *file_name, struct stat *buf)
 {
   int error;
   long int val_retry = -1;
-  char errbuf[SH_ERRBUF_SIZE];
  
-  SL_ENTER(_("retry_lstat_ns"));
+  SL_ENTER(_("retry_lstat"));
 
   do {
     val_retry = /*@-unrecog@*/lstat (file_name, buf)/*@+unrecog@*/;
   } while (val_retry < 0 && errno == EINTR);
-
   error = errno;
   if (val_retry < 0) {
-      (void) sh_error_message(error, aud_err_message, 64);
+      (void) sl_strlcpy(aud_err_message, sh_error_message(error), 64);
       sh_error_handle ((-1), file, line, error, MSG_ERR_LSTAT, 
-		       sh_error_message(error, errbuf, sizeof(errbuf)),
+		       sh_error_message(error),
 		       file_name );
   }
   errno = error;    
-
-  SL_RETURN(val_retry, _("retry_lstat_ns"));
-}
-
-long int retry_lstat(const char * file, int line, 
-		     const char *file_name, struct stat *buf)
+  SL_RETURN(val_retry, _("retry_lstat"));
+}
+
+long int retry_stat(char * file, int line, 
+		    const char *file_name, struct stat *buf)
 {
   int error;
   long int val_retry = -1;
-  char errbuf[SH_ERRBUF_SIZE];
- 
-  SL_ENTER(_("retry_lstat"));
-
-  if (sh_use_sub && sh_enable_use_sub)
-    {
-      val_retry = sh_sub_lstat (file_name, buf);
-    }
-  else
-    {
-      do {
-	val_retry = /*@-unrecog@*/lstat (file_name, buf)/*@+unrecog@*/;
-      } while (val_retry < 0 && errno == EINTR);
-    }
-
-  error = errno;
-  if (val_retry < 0) {
-      (void) sh_error_message(error, aud_err_message, 64);
-      sh_error_handle ((-1), file, line, error, MSG_ERR_LSTAT, 
-		       sh_error_message(error, errbuf, sizeof(errbuf)),
-		       file_name );
-  }
-  errno = error;    
-
-  SL_RETURN(val_retry, _("retry_lstat"));
-}
-
-long int retry_stat(const char * file, int line, 
-		    const char *file_name, struct stat *buf)
-{
-  int error;
-  long int val_retry = -1;
-  char errbuf[SH_ERRBUF_SIZE];
  
   SL_ENTER(_("retry_stat"));
 
-  if (sh_use_sub && sh_enable_use_sub)
-    {
-      val_retry = sh_sub_stat (file_name, buf);
-    }
-  else
-    {
-      do {
-	val_retry = stat (file_name, buf);
-      } while (val_retry < 0 && errno == EINTR);
-    }
-
+  do {
+    val_retry = stat (file_name, buf);
+  } while (val_retry < 0 && errno == EINTR);
   error = errno;
   if (val_retry < 0) {
-      (void) sh_error_message(error, aud_err_message, 64);
+      (void) sl_strlcpy(aud_err_message, sh_error_message(error), 64);
       sh_error_handle ((-1), file, line, error, MSG_ERR_STAT, 
-		       sh_error_message(error, errbuf, sizeof(errbuf)),
+		       sh_error_message(error),
 		       file_name );
   }
   errno = error;    
-
   SL_RETURN(val_retry, _("retry_stat"));
 }
 
-long int retry_fstat(const char * file, int line, int filed, struct stat *buf)
+long int retry_fstat(char * file, int line, int filed, struct stat *buf)
 {
   int error;
   long int val_retry = -1;
-  char errbuf[SH_ERRBUF_SIZE];
  
   SL_ENTER(_("retry_fstat"));
@@ -448,5 +281,5 @@
   if (val_retry < 0) {
       sh_error_handle ((-1), file, line, error, MSG_ERR_FSTAT, 
-		       sh_error_message(error, errbuf, sizeof(errbuf)),
+		       sh_error_message(error),
 		       (long) filed );
   }
@@ -455,9 +288,8 @@
 }
 
-long int retry_fcntl(const char * file, int line, int fd, int cmd, long arg)
+long int retry_fcntl(char * file, int line, int fd, int cmd, long arg)
 {
   int error;
   long int val_retry = -1;
-  char errbuf[SH_ERRBUF_SIZE];
   errno              = 0;
 
@@ -479,5 +311,5 @@
   if (val_retry < 0) {
       sh_error_handle ((-1), file, line, error, MSG_ERR_FCNTL, 
-		       sh_error_message(error, errbuf, sizeof(errbuf)),
+		       sh_error_message(error),
 		       (long) fd, (long) cmd, arg );
   }
@@ -492,4 +324,6 @@
   struct timespec req, rem;
 #endif
+
+  SL_ENTER(_("retry_fcntl"));
 
   errno  = 0;
@@ -512,16 +346,23 @@
 #else
   if (sec > 0)
-    sleep (sec); /* nanosleep not available */
-  else {
+    {
+      sleep (sec);
+    }
+  else
+    {
 #ifdef HAVE_USLEEP
-    if (millisec > 0)
-      usleep(1000 * millisec);
+      if (millisec > 0)
+	{
+	  usleep(1000 * millisec);
+	}
 #else
-    if (millisec > 0)
-      sleep (1);
+      if (millisec > 0)
+	{
+	  sleep (1);
+	}
 #endif
-  }
+    }
 #endif
-  return result;
+  SL_RETURN(result, _("retry_msleep"));
 }
 
@@ -532,7 +373,7 @@
  ***************************************************/
 
-long int retry_aud_execve  (const char * file, int line, 
-			    const  char *dateiname, char *const argv[],
-			    char *const envp[])
+long int retry_aud_execve  (char * file, int line, 
+			    const  char *dateiname, char * argv[],
+			    char * envp[])
 {
   uid_t a = geteuid();
@@ -540,5 +381,4 @@
   int   i;
   int   error;
-  char errbuf[SH_ERRBUF_SIZE];
 
   SL_ENTER(_("retry_aud_execve"));
@@ -553,6 +393,5 @@
   error = errno;
   if (i < 0) {
-      sh_error_handle ((-1), file, line, error, MSG_ERR_EXEC, 
-		       sh_error_message(error, errbuf, sizeof(errbuf)),
+      sh_error_handle ((-1), file, line, error, MSG_ERR_EXEC, sh_error_message(error),
 		       dateiname, (long) a, (long) b );
   }
@@ -562,10 +401,9 @@
 
 
-long int retry_aud_utime (const char * file, int line, 
-			  char * path, struct utimbuf *buf)
+long int retry_aud_utime (char * file, int line, 
+			   char * path, struct utimbuf *buf)
 {
   long int val_return;
   int  error;
-  char errbuf[SH_ERRBUF_SIZE];
   errno      = 0;
 
@@ -584,5 +422,5 @@
   if (val_return < 0) {
       sh_error_handle ((-1), file, line, error, MSG_ERR_UTIME, 
-		       sh_error_message(error, errbuf, sizeof(errbuf)),
+		       sh_error_message(error),
 		       path, 
 		       (unsigned long) buf->actime, 
@@ -593,10 +431,9 @@
 }
 
-long int retry_aud_unlink (const char * file, int line, 
+long int retry_aud_unlink (char * file, int line, 
 			   char * path)
 {
   long int val_return;
   int error;
-  char errbuf[SH_ERRBUF_SIZE];
   errno      = 0;
 
@@ -612,6 +449,5 @@
 		     path);
   if (val_return < 0) {
-      sh_error_handle ((-1), file, line, error, MSG_ERR_UNLINK, 
-		       sh_error_message(error, errbuf, sizeof(errbuf)),
+      sh_error_handle ((-1), file, line, error, MSG_ERR_UNLINK, sh_error_message(error),
 		       path);
   }
@@ -620,10 +456,9 @@
 }
 
-long int retry_aud_dup2 (const char * file, int line, 
+long int retry_aud_dup2 (char * file, int line, 
 			 int fd, int fd2)
 {
   long int val_return;
   int error;
-  char errbuf[SH_ERRBUF_SIZE];
   errno      = 0;
 
@@ -640,5 +475,5 @@
   if (val_return < 0) {
       sh_error_handle ((-1), file, line, error, MSG_ERR_DUP, 
-		       sh_error_message(error, errbuf, sizeof(errbuf)),
+		       sh_error_message(error),
 		       (long) fd, val_return);
   }
@@ -647,10 +482,9 @@
 }
 
-long int retry_aud_dup (const char * file, int line, 
+long int retry_aud_dup (char * file, int line, 
 			int fd)
 {
   long int val_return;
   int error;
-  char errbuf[SH_ERRBUF_SIZE];
   errno      = 0;
 
@@ -666,5 +500,5 @@
   if (val_return < 0) {
       sh_error_handle ((-1), file, line, error, MSG_ERR_DUP, 
-		       sh_error_message(error, errbuf, sizeof(errbuf)),
+		       sh_error_message(error),
 		       (long) fd, val_return);
   }
@@ -675,10 +509,9 @@
 
   
-long int retry_aud_chdir (const char * file, int line, 
+long int retry_aud_chdir (char * file, int line, 
 			  const char *path)
 {
   long int val_return;
   int      error      = 0;
-  char errbuf[SH_ERRBUF_SIZE];
   errno      = 0;
 
@@ -694,6 +527,5 @@
 		     path);
   if (val_return < 0) {
-      sh_error_handle ((-1), file, line, error, MSG_ERR_CHDIR, 
-		       sh_error_message(error, errbuf, sizeof(errbuf)),
+      sh_error_handle ((-1), file, line, error, MSG_ERR_CHDIR, sh_error_message(error),
 		       path);
   }
@@ -703,5 +535,5 @@
 
 
-long int aud_open_noatime (const char * file, int line, int privs,
+long int aud_open_noatime (char * file, int line, int privs,
 			   const char *pathname, int flags, mode_t mode,
 			   int * o_noatime)
@@ -709,41 +541,22 @@
   long int val_return;
   int error;
-  char errbuf[SH_ERRBUF_SIZE];
 
   SL_ENTER(_("aud_open"));
 
-#ifdef USE_SUID
-  if (0 == strcmp(pathname, "/usr/bin/sudo"))
-    {
-      uid_t ruid; uid_t euid; uid_t suid;
-      getresuid(&ruid, &euid, &suid);
-    }
-  if (privs == SL_YESPRIV)
-    sl_set_suid();
-#else
+  val_return = open (pathname, *o_noatime|flags, mode);
+  if ((val_return < 0) && (*o_noatime != 0))
+    {
+      val_return = open (pathname, flags, mode);
+      if (val_return >= 0)
+	*o_noatime = 0;
+    }
+  error = errno;
   /*@-noeffect@*/
   (void) privs; /* fix compiler warning */
   /*@+noeffect@*/
-#endif
-
-  val_return = open (pathname, *o_noatime|flags, mode);
-
-#ifdef USE_SUID
-  if (privs == SL_YESPRIV)
-    sl_unset_suid();
-#endif
-
-  if ((val_return < 0) && (*o_noatime != 0))
-    {
-      /* cppcheck-suppress resourceLeak */
-      val_return = open (pathname, flags, mode);
-      if (val_return >= 0)
-	*o_noatime = 0;
-    }
-  error = errno;
 
   if (val_return < 0)
     {
-      (void) sh_error_message(error, aud_err_message, 64);
+      (void) sl_strlcpy(aud_err_message, sh_error_message(error), 64);
     }
 
@@ -755,5 +568,5 @@
   if (val_return < 0) {
     sh_error_handle ((-1), file, line, error, MSG_ERR_OPEN, 
-		     sh_error_message(error, errbuf, sizeof(errbuf)),
+		     sh_error_message(error),
 		     pathname, (long) flags, (long) mode, val_return);
   }
@@ -762,34 +575,21 @@
 }
 
-long int aud_open (const char * file, int line, int privs,
+long int aud_open (char * file, int line, int privs,
 		   const char *pathname, int flags, mode_t mode)
 {
   long int val_return;
   int error;
-  char errbuf[SH_ERRBUF_SIZE];
 
   SL_ENTER(_("aud_open"));
 
-#ifdef USE_SUID
-  if (privs == SL_YESPRIV)
-    sl_set_suid();
-#else
+  val_return = open (pathname, flags, mode);
+  error = errno;
   /*@-noeffect@*/
   (void) privs; /* fix compiler warning */
   /*@+noeffect@*/
-#endif
-
-  val_return = open (pathname, flags, mode);
-
-#ifdef USE_SUID
-  if (privs == SL_YESPRIV)
-    sl_unset_suid();
-#endif
-
-  error = errno;
 
   if (val_return < 0)
     {
-      (void) sh_error_message(error, aud_err_message, 64);
+      (void) sl_strlcpy(aud_err_message, sh_error_message(error), 64);
     }
 
@@ -801,5 +601,5 @@
   if (val_return < 0) {
     sh_error_handle ((-1), file, line, error, MSG_ERR_OPEN, 
-		     sh_error_message(error, errbuf, sizeof(errbuf)),
+		     sh_error_message(error),
 		     pathname, (long) flags, (long) mode, val_return);
   }
@@ -808,9 +608,8 @@
 }
   
-long int aud_kill (const char * file, int line, pid_t pid, int sig)
+long int aud_kill (char * file, int line, pid_t pid, int sig)
 {
   int  myerror;
   long int val_return = kill (pid, sig);
-  char errbuf[SH_ERRBUF_SIZE];
   myerror = errno;
 
@@ -822,5 +621,5 @@
   if (val_return < 0) {
       sh_error_handle ((-1), file, line, myerror, MSG_ERR_KILL, 
-		       sh_error_message(myerror, errbuf, sizeof(errbuf)),
+		       sh_error_message(myerror),
 		       (long) pid, (long) sig);
   }
@@ -830,34 +629,33 @@
   
 /*@noreturn@*/
-void aud_exit (const char * file, int line, int exitval)
+void aud_exit (char * file, int line, int fd)
 {
   if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_EXIT) != 0)
     sh_error_handle ((-1), file, line, 0, MSG_AUD_EXIT,
-		      (long) exitval);
+		      (long) fd);
 
   SL_ENTER(_("aud_exit"));
 
-  sh.flag.exit = exitval;
-  exit(exitval);
+  sh.flag.exit = fd;
+  exit(fd);
 }
 
 /*@noreturn@*/
-void aud__exit (const char * file, int line, int exitval)
+void aud__exit (char * file, int line, int fd)
 {
   if (sh.flag.audit != 0 && (sh.flag.aud_mask & AUD_EXIT) != 0)
     sh_error_handle ((-1), file, line, 0, MSG_AUD_EXIT,
-		      (long) exitval);
+		      (long) fd);
 
   SL_ENTER(_("aud__exit"));
 
-  sh.flag.exit = exitval;
-  _exit(exitval);
-}
-
-pid_t aud_fork (const char * file, int line)
+  sh.flag.exit = fd;
+  _exit(fd);
+}
+
+pid_t aud_fork (char * file, int line)
 {
   int error;
   pid_t i = fork();
-  char errbuf[SH_ERRBUF_SIZE];
 
   error = errno;
@@ -869,5 +667,5 @@
   if (i == (pid_t) -1) {
     sh_error_handle ((-1), file, line, error, MSG_ERR_FORK, 
-		     sh_error_message(error, errbuf, sizeof(errbuf)),
+		     sh_error_message(error),
 		     (long) i);
   }
@@ -876,9 +674,8 @@
 }
 
-int aud_setuid (const char * file, int line, uid_t uid)
+int aud_setuid (char * file, int line, uid_t uid)
 {
   int error = 0;
   int i = 0;
-  char errbuf[SH_ERRBUF_SIZE];
 
   SL_ENTER(_("aud_setuid"));
@@ -897,5 +694,5 @@
   if (i < 0) {
     sh_error_handle ((-1), file, line, error, MSG_ERR_SETUID, 
-		     sh_error_message(error, errbuf, sizeof(errbuf)),
+		     sh_error_message(error),
 		     (long) uid);
   }
@@ -904,9 +701,8 @@
 }
 
-int aud_setgid (const char * file, int line, gid_t gid)
+int aud_setgid (char * file, int line, gid_t gid)
 {
   int error = 0;
   int i = 0;
-  char errbuf[SH_ERRBUF_SIZE];
 
   SL_ENTER(_("aud_setgid"));
@@ -926,5 +722,5 @@
   if (i < 0) {
     sh_error_handle ((-1), file, line, error, MSG_ERR_SETGID, 
-		     sh_error_message(error, errbuf, sizeof(errbuf)),
+		     sh_error_message(error),
 		     (long) gid);
   }
@@ -933,9 +729,8 @@
 }
 
-int aud_pipe (const char * file, int line, int modus[2])
+int aud_pipe (char * file, int line, int * modus)
 {
   int error;
   int i = pipe (modus);
-  char errbuf[SH_ERRBUF_SIZE];
 
   SL_ENTER(_("aud_pipe"));
@@ -954,9 +749,9 @@
     if (i < 0)
       sh_error_handle ((-1), file, line, error, MSG_ERR_PIPE, 
-		       sh_error_message(error, errbuf, sizeof(errbuf)),
+		       sh_error_message(error),
 		       (long) 0, (long) 0);
     else
       sh_error_handle ((-1), file, line, error, MSG_ERR_PIPE, 
-		       sh_error_message(error, errbuf, sizeof(errbuf)),
+		       sh_error_message(error),
 		       (long) modus[0], (long) modus[1]);
   }
Index: trunk/src/sh_cat.c
===================================================================
--- trunk/src/sh_cat.c	(revision 591)
+++ trunk/src/sh_cat.c	(revision 1)
@@ -8,5 +8,5 @@
 /*@-nullassign@*/
 
-const char * class_cat[] = {
+char * class_cat[] = {
   N_("AUD"),     /*  0 */
   N_("PANIC"),   /*  1 */
@@ -40,5 +40,5 @@
   { MSG_FI_CSUM,     SH_ERR_ALL,     FIL,   N_("msg=\"Checksum\" chk=\"%s\" path=\"%s\"")},
   { MSG_FI_DSUM,     SH_ERR_INFO,    FIL,   N_("msg=\"d: %3ld, -: %3ld, l: %3ld, |: %3ld, s: %3ld, c: %3ld, b: %3ld\"")},
-  { MSG_FI_CHK,      SH_ERR_INFO,    FIL,   N_("msg=\"Checking %16s\" path=\"%s\"")},
+  { MSG_FI_CHK,      SH_ERR_INFO,    FIL,   N_("msg=\"Checking\" path=\"%s\"")},
 #endif
 
@@ -70,13 +70,9 @@
   { MSG_CHECK_0,     SH_ERR_WARN,    RUN,   N_("msg=\"No files or directories defined for checking\"")},
   { MSG_CHECK_1,     SH_ERR_STAMP,   STAMP, N_("msg=\"File check completed.\" time=\"%ld\" kBps=\"%f\"")},
-  { MSG_CHECK_2,     SH_ERR_STAMP,   STAMP, N_("msg=\"File check starting.\"")},
   { MSG_STAMP,       SH_ERR_STAMP,   STAMP, N_("msg=\"---- TIMESTAMP ----\"")},
-  { MSG_DCLOSE,      SH_ERR_NOTICE,  RUN,   N_("msg=\"Finished writing baseline database.\"")},
-  
+
   { MSG_D_START,     SH_ERR_INFO,    RUN,   N_("msg=\"Downloading configuration file\"")},
   { MSG_D_DSTART,    SH_ERR_INFO,    RUN,   N_("msg=\"Downloading database file\"")},
   { MSG_D_FAIL,      SH_ERR_INFO,    RUN,   N_("msg=\"No file from server, trying local file\"")},
-  { MSG_D_DELTAOK,   SH_ERR_SEVERE,  RUN,   N_("msg=\"Delta database downloaded\", path=\"%s\"")},
-  { MSG_D_DELTAFAIL, SH_ERR_SEVERE,  RUN,   N_("msg=\"Delta database download failed\", path=\"%s\"")},
 
 
@@ -97,4 +93,17 @@
   { MSG_SUID_QREPORT,SH_ERR_SEVERE,  EVENT, N_("msg=\"Quarantine report: %s\" path=\"%s\"") },
   { MSG_SUID_ERROR,  SH_ERR_SEVERE,  EVENT, N_("msg=\"Quarantine error: %s\"") },
+#endif
+
+#ifdef SH_USE_KERN
+  /* FreeBSD */
+  { MSG_KERN_POLICY, SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY KERNEL BSD syscall table: new: %#lx old: %#lx\" syscall=\"%03d %s\"") },
+  { MSG_KERN_POL_CO, SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY KERNEL BSD syscall code: new: %#x,%#x old: %#x,%#x\" syscall=\"%03d %s\"") },
+
+  /* Linux */
+  { MSG_KERN_SYSCALL,SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [Kernel] SYSCALL modified syscall\" syscall=\"%03d %s\" %s") },
+  { MSG_KERN_PROC,   SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [Kernel] PROC modified proc filesystem: %s\"") },
+  { MSG_KERN_IDT,    SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [Kernel] IDT modified interrupt %03d: new: 0x%-8.8lx %-9s %3d %c old: 0x%-8.8lx %-9s %3d %c\" %s") },
+  { MSG_KERN_GATE,   SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [Kernel] SYS_GATE modified system_call: new: %#x,%#x old: %#x,%#x\" syscall=\"%03d %s\" %s") },
+
 #endif
 
@@ -113,54 +122,15 @@
   { MSG_UT_ROT,      SH_ERR_WARN,    RUN,   N_("msg=\"Logfile size decreased\" path=\"%s\"")},
 
-  { MSG_UT_BAD,      SH_ERR_SEVERE,  EVENT, N_("msg=\"Login at disallowed time\" userid=\"%s\" host=\"%s\" time=\"%s\"")},
-  { MSG_UT_FIRST,    SH_ERR_SEVERE,  EVENT, N_("msg=\"First login from this host\" userid=\"%s\" host=\"%s\" time=\"%s\"")},
-  { MSG_UT_OUTLIER,  SH_ERR_SEVERE,  EVENT, N_("msg=\"Login time outlier\" userid=\"%s\" host=\"%s\" time=\"%s\"")},
-
-#endif
-
-#ifdef SH_USE_PROCESSCHECK
-  { MSG_PCK_CHECK,   SH_ERR_INFO,    RUN,   N_("msg=\"Checking processes in pid interval [%ld,%ld]\"")},
-  { MSG_PCK_OK,      SH_ERR_ALL,     RUN,   N_("msg=\"PID %ld found with tests %s\"")},
-  { MSG_PCK_P_HIDDEN,SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [Process] Hidden pid: %ld tests: %s\" path=\"%s\" userid=\"%s\"")},
-  { MSG_PCK_HIDDEN,  SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [Process] Hidden pid: %ld tests: %s\"")},
-  { MSG_PCK_FAKE,    SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [Process] Fake pid: %ld tests: %s\"")},
-  { MSG_PCK_MISS,    SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [Process] Missing: %s\"")},
-#endif
-
-#ifdef SH_USE_PORTCHECK
-  { MSG_PORT_MISS,   SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [ServiceMissing] %s\"")},
-  { MSG_PORT_NEW,    SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [ServiceNew] %s\" path=\"%s\"  pid=\"%lu\" userid=\"%s\"")},
-  { MSG_PORT_RESTART,SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [ServiceRestarted] %s\" path=\"%s\" pid=\"%lu\" userid=\"%s\"")},
-  { MSG_PORT_NEWPORT,SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [ServicePortSwitch] %s\" path=\"%s\" pid=\"%lu\" userid=\"%s\"")},
 #endif
 
 #ifdef SH_USE_MOUNTS
-  { MSG_MNT_CHECK,   SH_ERR_INFO,    RUN,   N_("msg=\"Checking mounts\"")},
+  { MSG_MNT_CHECK,   SH_ERR_NOTICE,  RUN,   N_("msg=\"Checking mounts\"")},
   { MSG_MNT_MEMLIST, SH_ERR_ERR,     RUN,   N_("msg=\"Cannot read mount list from memory\"")},
-  { MSG_MNT_MNTMISS, SH_ERR_WARN,    EVENT, N_("msg=\"POLICY [Mounts] Mount missing\" path=\"%s\"")},
-  { MSG_MNT_OPTMISS, SH_ERR_WARN,    EVENT, N_("msg=\"POLICY [Mounts] Mount option missing\" path=\"%s\" option=\"%s\"")},
+  { MSG_MNT_MNTMISS, SH_ERR_WARN,    EVENT, N_("msg=\"Mount missing\" path=\"%s\"")},
+  { MSG_MNT_OPTMISS, SH_ERR_WARN,    EVENT, N_("msg=\"Mount option missing\" path=\"%s\" option=\"%s\"")},
 #endif
 
 #ifdef SH_USE_USERFILES
   { MSG_USERFILES_SUMMARY,SH_ERR_INFO,    RUN,   N_("msg=\"Checked for users files\"") },
-#endif
-
-#ifdef USE_LOGFILE_MONITOR
-  { MSG_LOGMON_CHKS, SH_ERR_INFO,    RUN,   N_("msg=\"Checking logfile %s\"") },
-  { MSG_LOGMON_CHKE, SH_ERR_INFO,    RUN,   N_("msg=\"Finished logfile %s, %lu new records processed\"") },
-  { MSG_LOGMON_MISS, SH_ERR_ERR,     RUN,   N_("msg=\"Missing logfile %s\"") },
-  { MSG_LOGMON_EOPEN,SH_ERR_ERR,     RUN,   N_("msg=\"Cannot open logfile %s\"") },
-  { MSG_LOGMON_EREAD,SH_ERR_ERR,     RUN,   N_("msg=\"Error while reading logfile %s\"") },
-  { MSG_LOGMON_REP,  SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [Logfile] %s\" time=\"%s\" host=\"%s\" path=\"%s\"") },
-  { MSG_LOGMON_SUM,  SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [Logfile] %s\" host=\"%s\" path=\"%s\"") },
-  { MSG_LOGMON_COR,  SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [Logfile] Correlation event %s occured %d time(s)\"") },
-  { MSG_LOGMON_MARK, SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [Logfile] Event %s missing for %lu seconds\"") },
-  { MSG_LOGMON_BURST, SH_ERR_SEVERE, EVENT, N_("msg=\"POLICY [Logfile] Repeated %d times: %s\" host=\"%s\"") },
-#endif
-
-#ifdef USE_REGISTRY_CHECK
-  { MSG_REG_MISS,   SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [RegistryKeyMissing] %s\" path=\"%s\" %s")},
-  { MSG_REG_NEW,    SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [RegistryKeyNew] %s\" path=\"%s\" %s")},
-  { MSG_REG_CHANGE, SH_ERR_SEVERE,  EVENT, N_("msg=\"POLICY [RegistryKeyChanged] %s\" path=\"%s\" %s")},
 #endif
 
@@ -172,5 +142,5 @@
   { MSG_FI_CSUM,     SH_ERR_ALL,     FIL,   N_("msg=\"Checksum\" chk=\"%s\" path=\"%s\"")},
   { MSG_FI_DSUM,     SH_ERR_INFO,    FIL,   N_("msg=\"d: %3ld, -: %3ld, l: %3ld, |: %3ld, s: %3ld, c: %3ld, b: %3ld\"")},
-  { MSG_FI_CHK,      SH_ERR_INFO,    FIL,   N_("msg=\"Checking %16s\" path=\"%s\"")},
+  { MSG_FI_CHK,      SH_ERR_INFO,    FIL,   N_("msg=\"Checking\" path=\"%s\"")},
 #endif
 
@@ -187,5 +157,5 @@
   { MSG_FI_NOGRP,    SH_ERR_ERR,     FIL,   N_("interface=\"getgrgid\" msg=\"No such group\" group=\"%ld\" path=\"%s\"")},
   { MSG_FI_NOUSR,    SH_ERR_ERR,     FIL,   N_("interface=\"getpwuid\" msg=\"No such user\" userid=\"%ld\" path=\"%s\"")},
-  { MSG_FI_STAT,     SH_ERR_ERR,     FIL,   N_("interface=\"%s\" msg=\"%s\" userid=\"%ld\" path=\"%s\"")},
+  { MSG_FI_LSTAT,    SH_ERR_ERR,     FIL,   N_("interface=\"lstat\" msg=\"%s\" path=\"%s\"")},
   { MSG_FI_OBSC,     SH_ERR_ERR,     FIL,   N_("msg=\"Weird filename\" path=\"%s\"")},
   { MSG_FI_OBSC2,    SH_ERR_ERR,     FIL,   N_("msg=\"Weird filename\" path=\"%s/%s\"")},
@@ -211,5 +181,5 @@
   { MSG_TCP_MISMATCH,SH_ERR_ERR,     TCP,   N_("msg=\"Protocol mismatch\"")},
   { MSG_TCP_MISENC,  SH_ERR_ERR,     TCP,   N_("msg=\"Encryption mismatch in %s: server: %s client: %s\"")},
-  { MSG_TCP_NONAME,  SH_ERR_ERR,     TCP,   N_("msg=\"No server name known\"")},
+  { MSG_TCP_NONAME,  SH_ERR_ERR,     TCP,   N_("msg=\"No server name available\"")},
   { MSG_TCP_UNEXP,   SH_ERR_ERR,     TCP,   N_("msg=\"Unexpected reply\"")},
   { MSG_TCP_EFIL,    SH_ERR_ERR,     TCP,   N_("msg=\"Could not open temporary file\"")},
@@ -225,5 +195,4 @@
 
   { MSG_TCP_CREG,    SH_ERR_ALL,     TCP,   N_("msg=\"Registered %s, salt %s, verifier %s\"")},
-  { MSG_TCP_AREG,    SH_ERR_ALL,     TCP,   N_("msg=\"Registered %s, hostname %s\"")},
   { MSG_TCP_FAUTH,   SH_ERR_INFO,    TCP,   N_("msg=\"Force authentication\" host=\"%s\"")},
 
@@ -246,5 +215,5 @@
   { MSG_TCP_ILL,     SH_ERR_SEVERE,  TCP,   N_("msg=\"Restart without prior exit\" host=\"%s\"")},
   { MSG_TCP_SYNC,    SH_ERR_SEVERE,  TCP,   N_("msg=\"Out of sync\" host=\"%s\"")},
-  { MSG_TCP_RESET,   SH_ERR_NOTICE,  TCP,   N_("msg=\"Connection reset by peer\" host=\"%s\"")},
+  { MSG_TCP_RESET,   SH_ERR_SEVERE,  TCP,   N_("msg=\"Connection reset by peer\" host=\"%s\"")},
   { MSG_TCP_CNEW,    SH_ERR_INFO,    TCP,   N_("msg=\"New connection\" socket_id=\"%d\"")},
   { MSG_E_HTML,      SH_ERR_ERR,     ERR,   N_("msg=\"Error writing HTML status\"")},
@@ -265,5 +234,5 @@
   { MSG_MSTAMP,      SH_ERR_STAMP,   STAMP, N_("msg=\"Memory used:  max.=%lu, current=%lu\"")},
   { MSG_MSTAMP2,     SH_ERR_STAMP,   STAMP, N_("msg=\"Blocks: %d allocated, %d freed, %d maximum\"")},
-  { MSG_E_MNULL,     SH_ERR_ERR,     ERR,   N_("msg=\"Dereferenced NULL pointer allocated in %s, line %d\" source_file=\"%s\" source_line=\"%d\"")},
+  { MSG_E_MNULL,     SH_ERR_ERR,     ERR,   N_("msg=\"Dereferenced NULL pointer\" source_file=\"%s\" source_line=\"%d\"")},
   { MSG_E_MMEM,      SH_ERR_ERR,     ERR,   N_("msg=\"Out of memory\" source_file=\"%s\" source_line=\"%d\"")},
   { MSG_E_MREC,      SH_ERR_ERR,     ERR,   N_("msg=\"Free() on unrecorded block\" source_file=\"%s\" source_line=\"%d\"")},
@@ -276,8 +245,7 @@
   { MSG_E_HASH,      SH_ERR_ERR,     ERR,   N_("msg=\"Incorrect checksum\" path=\"%s\"")},
   { MSG_E_ACCESS,    SH_ERR_ERR,     ERR,   N_("msg=\"File not accessible\" userid=\"%ld\" path=\"%s\"")},
-  { MSG_E_READ,      SH_ERR_ERR,     ERR,   N_("msg=\"Not accessible or not a regular file (%s / %s)\" path=\"%s\"")},
-  { MSG_E_NOTREG,    SH_ERR_ERR,     ERR,   N_("msg=\"Not a regular file\" path=\"%s\"")},
+  { MSG_E_READ,      SH_ERR_ERR,     ERR,   N_("msg=\"Not accessible or not a regular file\" path=\"%s\"")},
   { MSG_E_TIMEOUT,   SH_ERR_ERR,     ERR,   N_("msg=\"Timeout (%d sec) while checksumming file\" path=\"%s\"")},
-  { MSG_NODEV,       SH_ERR_ERR,     ERR,   N_("msg=\"Device not available or timeout during read attempt\" userid=\"%ld\" path=\"%s\"")},
+  { MSG_NODEV,       SH_ERR_ERR,     ERR,   N_("msg=\"Device not available\" userid=\"%ld\" path=\"%s\"")},
   { MSG_LOCKED,      SH_ERR_ERR,     ERR,   N_("msg=\"File lock error\" userid=\"%ld\" path=\"%s\" obj=\"%s\"")},
   { MSG_PIDFILE,      SH_ERR_ERR,     ERR,   N_("msg=\"Could not write PID file\" userid=\"%ld\" path=\"%s\"")},
@@ -374,5 +342,5 @@
   { MSG_FI_CSUM,     SH_ERR_ALL,     FIL,   N_("msg=<Checksum>, chk=<%s>, path=<%s>")},
   { MSG_FI_DSUM,     SH_ERR_INFO,    FIL,   N_("msg=<d: %3ld, -: %3ld, l: %3ld, |: %3ld, s: %3ld, c: %3ld, b: %3ld>")},
-  { MSG_FI_CHK,      SH_ERR_INFO,    FIL,   N_("msg=<Checking %16s>, path=<%s>")},
+  { MSG_FI_CHK,      SH_ERR_INFO,    FIL,   N_("msg=<Checking>, path=<%s>")},
 #endif
 
@@ -403,13 +371,9 @@
   { MSG_CHECK_0,     SH_ERR_WARN,    RUN,   N_("msg=<No files or directories defined for checking>")},
   { MSG_CHECK_1,     SH_ERR_STAMP,   STAMP, N_("msg=<File check completed.>, time=<%ld>, kBps=<%f>")},
-  { MSG_CHECK_2,     SH_ERR_STAMP,   STAMP, N_("msg=<File check starting.>")},
   { MSG_STAMP,       SH_ERR_STAMP,   STAMP, N_("msg=<---- TIMESTAMP ---->")},
-  { MSG_DCLOSE,      SH_ERR_NOTICE,  RUN,   N_("msg=<Finished writing baseline database.>")},
 
   { MSG_D_START,     SH_ERR_INFO,    RUN,   N_("msg=<Downloading configuration file>")},
   { MSG_D_DSTART,    SH_ERR_INFO,    RUN,   N_("msg=<Downloading database file>")},
   { MSG_D_FAIL,      SH_ERR_INFO,    RUN,   N_("msg=<No file from server, trying local file>")},
-  { MSG_D_DELTAOK,   SH_ERR_SEVERE,  RUN,   N_("msg=<Delta database downloaded>, path=<%s>")},
-  { MSG_D_DELTAFAIL, SH_ERR_SEVERE,  RUN,   N_("msg=<Delta database download failed>, path=<%s>")},
 
 
@@ -433,6 +397,6 @@
 
 #ifdef SH_USE_KERN
-  { MSG_KERN_POLICY, SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [Kernel] BSD syscall table: new: %#lx old: %#lx>, syscall=<%03d %s>") },
-  { MSG_KERN_POL_CO, SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [Kernel] BSD syscall code: new: %#x,%#x old: %#x,%#x>, syscall=<%03d %s>") },
+  { MSG_KERN_POLICY, SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY KERNEL BSD syscall table: new: %#lx old: %#lx>, syscall=<%03d %s>") },
+  { MSG_KERN_POL_CO, SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY KERNEL BSD syscall code: new: %#x,%#x old: %#x,%#x>, syscall=<%03d %s>") },
 
   { MSG_KERN_SYSCALL,SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [Kernel] SYSCALL modified> syscall=<%03d %s>, %s") },
@@ -460,23 +424,4 @@
   { MSG_UT_ROT,      SH_ERR_WARN,    RUN,   N_("msg=<Logfile size decreased>, path=<%s>")},
 
-  { MSG_UT_BAD,      SH_ERR_SEVERE,  EVENT, N_("msg=<Login at disallowed time> userid=<%s> host=<%s> time=<%s>")},
-  { MSG_UT_FIRST,    SH_ERR_SEVERE,  EVENT, N_("msg=<First login from this host> userid=<%s> host=<%s> time=<%s>")},
-  { MSG_UT_OUTLIER,  SH_ERR_SEVERE,  EVENT, N_("msg=<Login time outlier> userid=<%s> host=<%s> time=<%s>")},
-#endif
-
-#ifdef SH_USE_PROCESSCHECK
-  { MSG_PCK_CHECK,   SH_ERR_INFO,    RUN,   N_("msg=<Checking processes in pid interval [%ld,%ld]>")},
-  { MSG_PCK_OK,      SH_ERR_ALL,     RUN,   N_("msg=<PID %ld found with tests %s>")},
-  { MSG_PCK_P_HIDDEN,SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [Process] Hidden pid: %ld tests: %s> path=<%s> userid=<%s>")},
-  { MSG_PCK_HIDDEN,  SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [Process] Hidden pid: %ld tests: %s>")},
-  { MSG_PCK_FAKE,    SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [Process] Fake pid: %ld tests: %s>")},
-  { MSG_PCK_MISS,    SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [Process] Missing: %s>")},
-#endif
-
-#ifdef SH_USE_PORTCHECK
-  { MSG_PORT_MISS,   SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [ServiceMissing] %s>")},
-  { MSG_PORT_NEW,    SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [ServiceNew] %s> path=<%s> pid=<%lu> userid=<%s>")},
-  { MSG_PORT_RESTART,SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [ServiceRestarted] %s> path=<%s> pid=<%lu> userid=<%s>")},
-  { MSG_PORT_NEWPORT,SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [ServicePortSwitch] %s> path=<%s> pid=<%lu> userid=<%s>")},
 #endif
 
@@ -484,29 +429,10 @@
   { MSG_MNT_CHECK,   SH_ERR_INFO,    RUN,   N_("msg=<Checking mounts>")},
   { MSG_MNT_MEMLIST, SH_ERR_ERR,     RUN,   N_("msg=<Cannot read mount list from memory>")},
-  { MSG_MNT_MNTMISS, SH_ERR_WARN,    EVENT, N_("msg=<POLICY [Mounts] Mount missing>, path=<%s>")},
-  { MSG_MNT_OPTMISS, SH_ERR_WARN,    EVENT, N_("msg=<POLICY [Mounts] Mount option missing>, path=<%s>, option=<%s>")},
+  { MSG_MNT_MNTMISS, SH_ERR_WARN,    EVENT, N_("msg=<Mount missing>, path=<%s>")},
+  { MSG_MNT_OPTMISS, SH_ERR_WARN,    EVENT, N_("msg=<Mount option missing>, path=<%s>, option=<%s>")},
 #endif
 
 #ifdef SH_USE_USERFILES
   { MSG_USERFILES_SUMMARY,SH_ERR_INFO,    RUN,   N_("msg=<Checked for users files>") },
-#endif
-
-#ifdef USE_LOGFILE_MONITOR
-  { MSG_LOGMON_CHKS, SH_ERR_INFO,    RUN,   N_("msg=<Checking logfile %s>") },
-  { MSG_LOGMON_CHKE, SH_ERR_INFO,    RUN,   N_("msg=<Finished logfile %s, %lu new records processed>") },
-  { MSG_LOGMON_MISS, SH_ERR_ERR,     RUN,   N_("msg=<Missing logfile %s>") },
-  { MSG_LOGMON_EOPEN,SH_ERR_ERR,     RUN,   N_("msg=<Cannot open logfile %s>") },
-  { MSG_LOGMON_EREAD,SH_ERR_ERR,     RUN,   N_("msg=<Error while reading logfile %s>") },
-  { MSG_LOGMON_REP,  SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [Logfile] %s> time=<%s>, host=<%s>, path=<%s>") },
-  { MSG_LOGMON_SUM,  SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [Logfile] %s> host=<%s> path=<%s>") },
-  { MSG_LOGMON_COR,  SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [Logfile] Correlation event %s occured %d time(s)>") },
-  { MSG_LOGMON_MARK, SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [Logfile] Event %s missing for %lu seconds>") },
-  { MSG_LOGMON_BURST, SH_ERR_SEVERE, EVENT, N_("msg=<POLICY [Logfile] Repeated %d times: %s>, host=<%s> ") },
-#endif
-
-#ifdef USE_REGISTRY_CHECK
-  { MSG_REG_MISS,   SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [RegistryKeyMissing] %s>, path=<%s>, %s")},
-  { MSG_REG_NEW,    SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [RegistryKeyNew] %s>, path=<%s>, %s")},
-  { MSG_REG_CHANGE, SH_ERR_SEVERE,  EVENT, N_("msg=<POLICY [RegistryKeyChanged] %s>, path=<%s>, %s")},
 #endif
 
@@ -518,5 +444,5 @@
   { MSG_FI_CSUM,     SH_ERR_ALL,     FIL,   N_("msg=<Checksum>, chk=<%s>, path=<%s>")},
   { MSG_FI_DSUM,     SH_ERR_INFO,    FIL,   N_("msg=<d: %3ld, -: %3ld, l: %3ld, |: %3ld, s: %3ld, c: %3ld, b: %3ld>")},
-  { MSG_FI_CHK,      SH_ERR_INFO,    FIL,   N_("msg=<Checking %16s>, path=<%s>")},
+  { MSG_FI_CHK,      SH_ERR_INFO,    FIL,   N_("msg=<Checking>, path=<%s>")},
 #endif
 
@@ -533,5 +459,5 @@
   { MSG_FI_NOGRP,    SH_ERR_ERR,     FIL,   N_("msg=<No such group>, interface=<getgrgid>, group=<%ld>, path=<%s>")},
   { MSG_FI_NOUSR,    SH_ERR_ERR,     FIL,   N_("msg=<No such user>, interface=<getpwuid>, userid=<%ld>, path=<%s>")},
-  { MSG_FI_STAT,     SH_ERR_ERR,     FIL,   N_("interface=<%s>, msg=<%s>, userid=<%ld>, path=<%s>")},
+  { MSG_FI_LSTAT,    SH_ERR_ERR,     FIL,   N_("msg=<%s>, interface=<lstat>, path=<%s>")},
   { MSG_FI_OBSC,     SH_ERR_ERR,     FIL,   N_("msg=<Weird filename>, path=<%s>")},
   { MSG_FI_OBSC2,    SH_ERR_ERR,     FIL,   N_("msg=<Weird filename>, path=<%s/%s>")},
@@ -570,5 +496,4 @@
 
   { MSG_TCP_CREG,    SH_ERR_ALL,     TCP,   N_("msg=<Registered %s, salt %s, verifier %s>")},
-  { MSG_TCP_AREG,    SH_ERR_ALL,     TCP,   N_("msg=<Registered %s, hostname %s>")},
   { MSG_TCP_FAUTH,   SH_ERR_INFO,    TCP,   N_("msg=<Force authentication>, client=<%s>")},
 
@@ -591,5 +516,5 @@
   { MSG_TCP_ILL,     SH_ERR_SEVERE,  TCP,   N_("msg=<Restart without prior exit>, client=<%s>")},
   { MSG_TCP_SYNC,    SH_ERR_SEVERE,  TCP,   N_("msg=<Out of sync>, client=<%s>")},
-  { MSG_TCP_RESET,   SH_ERR_NOTICE,  TCP,   N_("msg=<Connection reset by peer>, client=<%s>")},
+  { MSG_TCP_RESET,   SH_ERR_SEVERE,  TCP,   N_("msg=<Connection reset by peer>, client=<%s>")},
   { MSG_TCP_CNEW,    SH_ERR_INFO,    TCP,   N_("msg=<New connection>, socket_id=<%d>")},
   { MSG_E_HTML,      SH_ERR_ERR,     ERR,   N_("msg=<Error writing HTML status>")},
@@ -621,8 +546,7 @@
   { MSG_E_HASH,      SH_ERR_ERR,     ERR,   N_("msg=<Incorrect checksum>, path=<%s>")},
   { MSG_E_ACCESS,    SH_ERR_ERR,     ERR,   N_("msg=<File not accessible>, userid=<%ld>, path=<%s>")},
-  { MSG_E_READ,      SH_ERR_ERR,     ERR,   N_("msg=<Not accessible or not a regular file (%s / %s)>, path=<%s>")},
-  { MSG_E_NOTREG,    SH_ERR_ERR,     ERR,   N_("msg=<Not a regular file>, path=<%s>")},
+  { MSG_E_READ,      SH_ERR_ERR,     ERR,   N_("msg=<Not accessible or not a regular file>, path=<%s>")},
   { MSG_E_TIMEOUT,   SH_ERR_ERR,     ERR,   N_("msg=<Timeout (%d sec) while checksumming file>, path=<%s>")},
-  { MSG_NODEV,       SH_ERR_ERR,     ERR,   N_("msg=<Device not available or timeout during read attempt>, userid=<%ld>, path=<%s>")},
+  { MSG_NODEV,       SH_ERR_ERR,     ERR,   N_("msg=<Device not available>, userid=<%ld>, path=<%s>")},
   { MSG_LOCKED,      SH_ERR_ERR,     ERR,   N_("msg=<File lock error>, userid=<%ld>, path=<%s>, obj=<%s>")},
   { MSG_PIDFILE,      SH_ERR_ERR,     ERR,   N_("msg=<Could not write PID file>, userid=<%ld>, path=<%s>")},
Index: trunk/src/sh_checksum.c
===================================================================
--- trunk/src/sh_checksum.c	(revision 591)
+++ 	(revision )
@@ -1,638 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 2013 Rainer Wichmann                                      */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-#include "config_xor.h"
-#include "samhain.h"
-#include "sh_checksum.h"
-#include <string.h>
-
-#undef  FIL__
-#define FIL__  _("sh_checksum.c")
-
-/*
- * sha2.c
- *
- * Version 1.0.0beta1
- *
- * Written by Aaron D. Gifford <me@aarongifford.com>
- *
- * Copyright 2000 Aaron D. Gifford.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the copyright holder nor the names of contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- * 
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTOR(S) ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- */
-
-/* Modified for use in samhain by R. Wichmann */
-
-#if WORDS_BIGENDIAN
-#define SHA2_BIG_ENDIAN    4321
-#define SHA2_BYTE_ORDER SHA2_BIG_ENDIAN
-#else
-#define SHA2_LITTLE_ENDIAN 1234
-#define SHA2_BYTE_ORDER SHA2_LITTLE_ENDIAN
-#endif
-
-#if SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN
-#define REVERSE32(w,x)  { \
-        sha2_word32 tmp = (w); \
-        tmp = (tmp >> 16) | (tmp << 16); \
-        (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
-}
-#define REVERSE64(w,x)  { \
-        sha2_word64 tmp = (w); \
-        tmp = (tmp >> 32) | (tmp << 32); \
-        tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \
-              ((tmp & 0x00ff00ff00ff00ffULL) << 8); \
-        (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \
-              ((tmp & 0x0000ffff0000ffffULL) << 16); \
-}
-#endif
-
-/*
- * Macro for incrementally adding the unsigned 64-bit integer n to the
- * unsigned 128-bit integer (represented using a two-element array of
- * 64-bit words):
- */
-#define ADDINC128(w,n)  { \
-        (w)[0] += (sha2_word64)(n); \
-        if ((w)[0] < (n)) { \
-                (w)[1]++; \
-        } \
-}
-
-/*** THE SIX LOGICAL FUNCTIONS ****************************************/
-/*
- * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
- *
- *   NOTE:  The naming of R and S appears backwards here (R is a SHIFT and
- *   S is a ROTATION) because the SHA-256/384/512 description document
- *   (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
- *   same "backwards" definition.
- */
-/* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
-#define R(b,x)          ((x) >> (b))
-/* 32-bit Rotate-right (used in SHA-256): */
-#define S32(b,x)        (((x) >> (b)) | ((x) << (32 - (b))))
-/* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
-#define S64(b,x)        (((x) >> (b)) | ((x) << (64 - (b))))
-
-/* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
-#define Ch(x,y,z)       (((x) & (y)) ^ ((~(x)) & (z)))
-#define Maj(x,y,z)      (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
-
-/* Four of six logical functions used in SHA-256: */
-#define Sigma0_256(x)   (S32(2,  (x)) ^ S32(13, (x)) ^ S32(22, (x)))
-#define Sigma1_256(x)   (S32(6,  (x)) ^ S32(11, (x)) ^ S32(25, (x)))
-#define sigma0_256(x)   (S32(7,  (x)) ^ S32(18, (x)) ^ R(3 ,   (x)))
-#define sigma1_256(x)   (S32(17, (x)) ^ S32(19, (x)) ^ R(10,   (x)))
-
-/*** INTERNAL FUNCTION PROTOTYPES *************************************/
-/* NOTE: These should not be accessed directly from outside this
- * library -- they are intended for private internal visibility/use
- * only.
- */
-void SHA256_Transform(SHA256_CTX*, const sha2_word32*);
-
-/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
-/* Hash constant words K for SHA-256: */
-static const sha2_word32 K256[64] = {
-        0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
-        0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
-        0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
-        0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
-        0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
-        0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
-        0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
-        0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
-        0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
-        0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
-        0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
-        0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
-        0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
-        0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
-        0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
-        0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
-};
-
-/* Initial hash value H for SHA-256: */
-static const sha2_word32 sha256_initial_hash_value[8] = {
-        0x6a09e667UL,
-        0xbb67ae85UL,
-        0x3c6ef372UL,
-        0xa54ff53aUL,
-        0x510e527fUL,
-        0x9b05688cUL,
-        0x1f83d9abUL,
-        0x5be0cd19UL
-};
-
-/*
- * Constant used by SHA256/384/512_End() functions for converting the
- * digest to a readable hexadecimal character string:
- */
-static const char *sha2_hex_digits = "0123456789abcdef";
-
-/*** SHA-256: *********************************************************/
-void SHA256_Init(SHA256_CTX* context) {
-  if (context == (SHA256_CTX*)0) {
-    return;
-  }
-  memcpy(context->state, sha256_initial_hash_value, SHA256_DIGEST_LENGTH);
-  /* bcopy(sha256_initial_hash_value, context->state, SHA256_DIGEST_LENGTH); */
-  memset(context->buffer, 0, SHA256_BLOCK_LENGTH);
-  /* bzero(context->buffer, SHA256_BLOCK_LENGTH); */
-  
-  context->bitcount = 0;
-}
-
-#ifdef SHA2_UNROLL_TRANSFORM
-
-/* Unrolled SHA-256 round macros: */
-
-#if SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN
-
-#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h)       \
-        REVERSE32(*data++, W256[j]); \
-        T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
-             K256[j] + W256[j]; \
-        (d) += T1; \
-        (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
-        j++
-
-#else /* SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN */
-
-#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h)       \
-        T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
-             K256[j] + (W256[j] = *data++); \
-        (d) += T1; \
-        (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
-        j++
-
-#endif /* SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN */
-
-#define ROUND256(a,b,c,d,e,f,g,h)       \
-        s0 = W256[(j+1)&0x0f]; \
-        s0 = sigma0_256(s0); \
-        s1 = W256[(j+14)&0x0f]; \
-        s1 = sigma1_256(s1); \
-        T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \
-             (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \
-        (d) += T1; \
-        (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
-        j++
-
-void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
-  sha2_word32     a, b, c, d, e, f, g, h, s0, s1;
-  sha2_word32     T1, *W256;
-  int             j;
-  
-  W256 = (sha2_word32*)context->buffer;
-  
-  /* Initialize registers with the prev. intermediate value */
-  a = context->state[0];
-  b = context->state[1];
-  c = context->state[2];
-  d = context->state[3];
-  e = context->state[4];
-  f = context->state[5];
-  g = context->state[6];
-  h = context->state[7];
-  
-  j = 0;
-  do {
-    /* Rounds 0 to 15 (unrolled): */
-    ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
-    ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
-    ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
-    ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
-    ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
-    ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
-    ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
-    ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
-  } while (j < 16);
-  
-  /* Now for the remaining rounds to 64: */
-  do {
-    ROUND256(a,b,c,d,e,f,g,h);
-    ROUND256(h,a,b,c,d,e,f,g);
-    ROUND256(g,h,a,b,c,d,e,f);
-    ROUND256(f,g,h,a,b,c,d,e);
-    ROUND256(e,f,g,h,a,b,c,d);
-    ROUND256(d,e,f,g,h,a,b,c);
-    ROUND256(c,d,e,f,g,h,a,b);
-    ROUND256(b,c,d,e,f,g,h,a);
-  } while (j < 64);
-  
-  /* Compute the current intermediate hash value */
-  context->state[0] += a;
-  context->state[1] += b;
-  context->state[2] += c;
-  context->state[3] += d;
-  context->state[4] += e;
-  context->state[5] += f;
-  context->state[6] += g;
-  context->state[7] += h;
-  
-  /* Clean up */
-  a = b = c = d = e = f = g = h = T1 = 0;
-}
-
-#else /* SHA2_UNROLL_TRANSFORM */
-
-void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
-  sha2_word32     a, b, c, d, e, f, g, h, s0, s1;
-  sha2_word32     T1, T2, *W256;
-  int             j;
-  
-  W256 = (sha2_word32*)context->buffer;
-  
-  /* Initialize registers with the prev. intermediate value */
-  a = context->state[0];
-  b = context->state[1];
-  c = context->state[2];
-  d = context->state[3];
-  e = context->state[4];
-  f = context->state[5];
-  g = context->state[6];
-  h = context->state[7];
-  
-  j = 0;
-  do {
-#if SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN
-    /* Copy data while converting to host byte order */
-    REVERSE32(*data++,W256[j]);
-    /* Apply the SHA-256 compression function to update a..h */
-    T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
-#else /* SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN */
-    /* Apply the SHA-256 compression function to update a..h with copy */
-    T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
-#endif /* SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN */
-    T2 = Sigma0_256(a) + Maj(a, b, c);
-    h = g;
-    g = f;
-    f = e;
-    e = d + T1;
-    d = c;
-    c = b;
-    b = a;
-    a = T1 + T2;
-    
-    j++;
-  } while (j < 16);
-  
-  do {
-    /* Part of the message block expansion: */
-    s0 = W256[(j+1)&0x0f];
-    s0 = sigma0_256(s0);
-    s1 = W256[(j+14)&0x0f]; 
-    s1 = sigma1_256(s1);
-    
-    /* Apply the SHA-256 compression function to update a..h */
-    T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + 
-      (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
-    T2 = Sigma0_256(a) + Maj(a, b, c);
-    h = g;
-    g = f;
-    f = e;
-    e = d + T1;
-    d = c;
-    c = b;
-    b = a;
-    a = T1 + T2;
-    
-    j++;
-  } while (j < 64);
-  
-  /* Compute the current intermediate hash value */
-  context->state[0] += a;
-  context->state[1] += b;
-  context->state[2] += c;
-  context->state[3] += d;
-  context->state[4] += e;
-  context->state[5] += f;
-  context->state[6] += g;
-  context->state[7] += h;
-  
-  /* Clean up */
-  a = b = c = d = e = f = g = h = T1 = T2 = 0;
-}
-
-#endif /* SHA2_UNROLL_TRANSFORM */
-
-void SHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len) {
-  unsigned int    freespace, usedspace;
-  
-  if (len == 0) {
-    /* Calling with no data is valid - we do nothing */
-    return;
-  }
-  
-  usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
-  
-  if (usedspace > 0) {
-    /* Calculate how much free space is available in the buffer */
-    freespace = SHA256_BLOCK_LENGTH - usedspace;
-    
-    if (len >= freespace) {
-      /* Fill the buffer completely and process it */
-      memcpy(&context->buffer[usedspace], data, freespace);
-      /* bcopy(data, &context->buffer[usedspace], freespace); */
-      context->bitcount += freespace << 3;
-      len -= freespace;
-      data += freespace;
-      SHA256_Transform(context, (sha2_word32*)context->buffer);
-    } else {
-      /* The buffer is not yet full */
-      memcpy(&context->buffer[usedspace], data, len);
-      /* bcopy(data, &context->buffer[usedspace], len); */
-      context->bitcount += len << 3;
-      
-      /* Clean up: */
-      usedspace = freespace = 0;
-      return;
-    }
-  }
-  while (len >= SHA256_BLOCK_LENGTH) {
-    /* Process as many complete blocks as we can */
-    SHA256_Transform(context, (const sha2_word32*)data);
-    context->bitcount += SHA256_BLOCK_LENGTH << 3;
-    len -= SHA256_BLOCK_LENGTH;
-    data += SHA256_BLOCK_LENGTH;
-  }
-  if (len > 0) {
-    /* There's left-overs, so save 'em */
-    memcpy(context->buffer, data, len);
-    /* bcopy(data, context->buffer, len); */
-    context->bitcount += len << 3;
-  }
-  /* Clean up: */
-  usedspace = freespace = 0;
-}
-
-void SHA256_Final(sha2_byte digest[SHA256_DIGEST_LENGTH], SHA256_CTX* context) 
-{
-  sha2_word32     *d = (sha2_word32*)digest;
-  unsigned int    usedspace;
-  union {
-    sha2_word64     bitcount;
-    sha2_byte       buffer[sizeof(sha2_word64)];
-  } sha2_union;
-  
-  /* If no digest buffer is passed, we don't bother doing this: */
-  if (digest != (sha2_byte*)0) {
-    
-    usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
-    
-#if SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN
-    /* Convert FROM host byte order */
-    REVERSE64(context->bitcount,context->bitcount);
-#endif
-    if (usedspace > 0) {
-      /* Begin padding with a 1 bit: */
-      context->buffer[usedspace++] = 0x80;
-      
-      if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
-	/* Set-up for the last transform: */
-	memset(&context->buffer[usedspace], 0, SHA256_SHORT_BLOCK_LENGTH - usedspace);
-      } else {
-	if (usedspace < SHA256_BLOCK_LENGTH) {
-	  memset(&context->buffer[usedspace], 0, SHA256_BLOCK_LENGTH - usedspace);
-	}
-	/* Do second-to-last transform: */
-	SHA256_Transform(context, (sha2_word32*)context->buffer);
-	
-	/* And set-up for the last transform: */
-	memset(context->buffer, 0, SHA256_SHORT_BLOCK_LENGTH);
-      }
-    } else {
-      /* Set-up for the last transform: */
-      memset(context->buffer, 0, SHA256_SHORT_BLOCK_LENGTH);
-      
-      /* Begin padding with a 1 bit: */
-      *context->buffer = 0x80;
-    }
-
-    /* Set the bit count (with fix for gcc type-punning warning): */
-    sha2_union.bitcount = context->bitcount;
-    memcpy (&context->buffer[SHA256_SHORT_BLOCK_LENGTH], sha2_union.buffer, sizeof(sha2_word64));
-    /* *(sha2_word64*) &context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount; */
-    
-    /* Final transform: */
-    SHA256_Transform(context, (sha2_word32*)context->buffer);
-    
-#if SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN
-    {
-      /* Convert TO host byte order */
-      int     j;
-      for (j = 0; j < 8; j++) {
-	REVERSE32(context->state[j],context->state[j]);
-	*d++ = context->state[j];
-      }
-    }
-#else
-    memset(d, context->state, SHA256_DIGEST_LENGTH);
-    /* bcopy(context->state, d, SHA256_DIGEST_LENGTH); */
-#endif
-  }
-  
-  /* Clean up state data: */
-  memset(context, 0, sizeof(SHA256_CTX));
-  usedspace = 0;
-}
-
-#include "sh_utils.h"
-
-/* If buffer is of length KEYBUF_SIZE, the digest will fit */
-char *SHA256_End(SHA256_CTX* context, char buffer[KEYBUF_SIZE]) 
-{
-  sha2_byte       digest[SHA256_DIGEST_LENGTH];
-  
-  if (buffer != (char*)0) {
-    SHA256_Final(digest, context);
-    sh_util_base64_enc ((unsigned char *)buffer, digest,  SHA256_DIGEST_LENGTH);
-  } else {
-    memset(context, 0, sizeof(SHA256_CTX));
-  }
-  memset(digest, 0, SHA256_DIGEST_LENGTH);
-  return buffer;
-}
-
-char* SHA256_Data(const sha2_byte* data, size_t len, char digest[KEYBUF_SIZE]) 
-{
-  SHA256_CTX      context;
-  
-  SHA256_Init(&context);
-  SHA256_Update(&context, data, len);
-  return SHA256_End(&context, digest);
-}
-
-char* SHA256_Base2Hex(char * b64digest, char * hexdigest) 
-{
-  int        i;
-  sha2_byte data[512];
-  sha2_byte *d;
-  size_t     len;
-  char * buffer;
-
-  len = strlen(b64digest);
-  sh_util_base64_dec ((unsigned char*) data, (unsigned char *)b64digest, len);
-  d = data;
-
-  buffer = hexdigest;
-  for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
-    *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
-    *buffer++ = sha2_hex_digits[*d & 0x0f];
-    d++;
-  }
-  *buffer = (char)0;
-
-  return hexdigest;
-}
-
-char * SHA256_ReplaceBaseByHex(const char * str, char * before, char after)
-{
-  char   keybuf[KEYBUF_SIZE];
-  char * s   = strstr(str, before);
-
-  if (s)
-    {
-      char * p;
-
-      s += strlen(before);
-      memcpy(keybuf, s, sizeof(keybuf));
-      keybuf[sizeof(keybuf)-1] = '\0';
-      p = strchr(keybuf, after);
-
-      if (p)
-	{
-	  char   hexbuf[SHA256_DIGEST_STRING_LENGTH];
-	  char * ret = SH_ALLOC(strlen(str) + 1 + sizeof(keybuf)); 
-	  char * r   = ret;
-
-	  *p = '\0';
-	  SHA256_Base2Hex(keybuf, hexbuf);
-
-	  memcpy(ret, str, (s - str));
-	  r += (int)(s - str); *r = '\0';
-	  strcpy(r, hexbuf); /* flawfinder: ignore */
-	  r += strlen(hexbuf);
-	  p = strchr(s, after);
-	  strcpy(r, p);      /* flawfinder: ignore */
-
-	  return ret;
-	}
-    }
-  return NULL;
-}
-
-
-#ifdef SH_CUTEST
-#include <stdlib.h>
-#include "CuTest.h"
-
-void Test_sha256 (CuTest *tc) {
-
-  char hexdigest[SHA256_DIGEST_STRING_LENGTH];
-  char b64digest[KEYBUF_SIZE];
-  char * b64;
-  char * buffer;
-  size_t len;
-  sha2_byte data[512];
-  sha2_byte *d;
-  int        i;
-
-  data[0] = '\0'; len = 0;
-  b64 = SHA256_Data(data, len, b64digest);
-  CuAssertPtrNotNull(tc, b64);
-
-  len = strlen((char*)b64);
-  sh_util_base64_dec (data, (unsigned char*)b64, len);
-  d = data;
-  buffer = hexdigest;
-  for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
-    *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
-    *buffer++ = sha2_hex_digits[*d & 0x0f];
-    d++;
-  }
-  *buffer = (char)0;
-  CuAssertStrEquals(tc, hexdigest, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
-
-  memset(hexdigest, 0, sizeof(hexdigest));
-  buffer = SHA256_Base2Hex(b64digest, hexdigest);
-  CuAssertPtrNotNull(tc, buffer);
-  CuAssertStrEquals(tc, hexdigest, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
-  CuAssertStrEquals(tc,    buffer, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
-
-  strcpy((char*)data, "The quick brown fox jumps over the lazy dog"); len = strlen((char*)data);
-  b64 = SHA256_Data(data, len, b64digest);
-  CuAssertPtrNotNull(tc, b64);
-
-  len = strlen((char*)b64);
-  sh_util_base64_dec (data, (unsigned char*)b64, len);
-  d = data;
-  buffer = hexdigest;
-  for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
-    *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
-    *buffer++ = sha2_hex_digits[*d & 0x0f];
-    d++;
-  }
-  *buffer = (char)0;
-  CuAssertStrEquals(tc, hexdigest, "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592");
-
-  strcpy((char*)data, "The quick brown fox jumps over the lazy dog."); len = strlen((char*)data);
-  b64 = SHA256_Data(data, len, b64digest);
-  CuAssertPtrNotNull(tc, b64);
-
-  len = strlen((char*)b64);
-  sh_util_base64_dec (data, (unsigned char*)b64, len);
-  d = data;
-  buffer = hexdigest;
-  for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
-    *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
-    *buffer++ = sha2_hex_digits[*d & 0x0f];
-    d++;
-  }
-  *buffer = (char)0;
-  CuAssertStrEquals(tc, hexdigest, "ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c");
-
-}
-
-#endif
Index: trunk/src/sh_database.c
===================================================================
--- trunk/src/sh_database.c	(revision 591)
+++ trunk/src/sh_database.c	(revision 1)
@@ -33,6 +33,4 @@
 /* #define DB_DEBUG  */
 
-#define SH_REAL_SET
-
 #include "samhain.h"
 
@@ -51,5 +49,4 @@
   int    val;
   int    size;
-  int    alen;
   size_t off;
 } my_attr;
@@ -61,5 +58,5 @@
   char            msg[1024];
   char            sev[8];
-  char            path[MAX_PATH_STORE+1];
+  char            path[12288];
   char            user[9];
   char            group[9];
@@ -75,5 +72,5 @@
   char            module[8];
   char            syscall[16];
-  char            ip[SH_IP_BUF];     
+  char            ip[16];     
   char            tty[16];    
   char            peer[64];
@@ -82,6 +79,6 @@
   char            interface[64];   
   char            ltime[64];   
-  char            dir[MAX_PATH_STORE+1];   
-  char            linked_path[MAX_PATH_STORE+1]; 
+  char            dir[1024];   
+  char            linked_path[1024]; 
   char            service[64];   
   char            facility[32];   
@@ -107,10 +104,8 @@
   char            chksum_old[50];
   char            chksum_new[50];
-  char            link_old[MAX_PATH_STORE+1];
-  char            link_new[MAX_PATH_STORE+1];
-  char            acl_old[1024];
-  char            acl_new[1024];
-
-  unsigned long   ulong_data[20];
+  char            link_old[1024];
+  char            link_new[1024];
+
+  long            long_data[20];
 
   /*
@@ -125,105 +120,90 @@
 } dbins;
 
-static my_attr * attr_tab_srch = NULL;
-static int       attr_tab_srch_siz = 0;
-
 static my_attr attr_tab[] = {
-  { NULL, N_("sev"),         0,   1,    8, 0, offsetof(struct dbins_, sev) },
-  { NULL, N_("tstamp"),      0,   2,   16, 0, offsetof(struct dbins_, time) },
-  { NULL, N_("remote_host"), 0,   3,   64, 0, offsetof(struct dbins_, host) },
-  { NULL, N_("msg"),         0,   4, 1024, 0, offsetof(struct dbins_, msg) },
-
-  { NULL, N_("path"),        0,   5,MAX_PATH_STORE+1, 0, offsetof(struct dbins_, path)  },
+  { NULL, N_("sev"),         0,   1,    8, offsetof(struct dbins_, sev) },
+  { NULL, N_("tstamp"),      0,   2,   16, offsetof(struct dbins_, time) },
+  { NULL, N_("remote_host"), 0,   3,   64, offsetof(struct dbins_, host) },
+  { NULL, N_("msg"),         0,   4, 1024, offsetof(struct dbins_, msg) },
+
+  { NULL, N_("path"),        0,   5,12288, offsetof(struct dbins_, path)  },
   /* username -> userid; replace (long) 'userid' - below - by 'dummy' */ 
-  { NULL, N_("userid"),      0,   6,    9, 0, offsetof(struct dbins_, user)  },
-  { NULL, N_("group"),       0,   7,    9, 0, offsetof(struct dbins_, group)  },
-  { NULL, N_("program"),     0,   8,    8, 0, offsetof(struct dbins_, program)  },
-  { NULL, N_("subroutine"),  0,   9,   16, 0, offsetof(struct dbins_, subroutine)},
-  { NULL, N_("status"),      0,  10,   12, 0, offsetof(struct dbins_, status)  },
-  { NULL, N_("hash"),        0,  11,   50, 0, offsetof(struct dbins_, hash)  },
-  { NULL, N_("path_data"),   0,  12, 1024, 0, offsetof(struct dbins_, path_data)  },
-  { NULL, N_("hash_data"),   0,  13,   50, 0, offsetof(struct dbins_, hash_data)  },
-  { NULL, N_("key_uid"),     0,  14,   64, 0, offsetof(struct dbins_, key_uid)  },
-  { NULL, N_("key_uid_data"),0,  15,   64, 0, offsetof(struct dbins_, key_uid_data)},
-  { NULL, N_("key_id"),      0,  16,   16, 0, offsetof(struct dbins_, key_id)  },
-  { NULL, N_("module"),      0,  17,    8, 0, offsetof(struct dbins_, module)  },
-  { NULL, N_("syscall"),     0,  19,   16, 0, offsetof(struct dbins_, syscall)  },
-  { NULL, N_("ip"),          0,  20,SH_IP_BUF, 0, offsetof(struct dbins_, ip)  },
-  { NULL, N_("tty"),         0,  21,   16, 0, offsetof(struct dbins_, tty)  },
-  { NULL, N_("peer"),        0,  22,   64, 0, offsetof(struct dbins_, peer)  },
-  { NULL, N_("obj"),         0,  23, 1024, 0, offsetof(struct dbins_, obj)  },
-  { NULL, N_("interface"),   0,  24,   64, 0, offsetof(struct dbins_, interface)},
-  { NULL, N_("time"),        0,  25,   64, 0, offsetof(struct dbins_, ltime)  },
-  { NULL, N_("dir"),         0,  26, MAX_PATH_STORE+1, 0, offsetof(struct dbins_, dir)  },
-  { NULL, N_("linked_path"), 0,  27, MAX_PATH_STORE+1, 0, offsetof(struct dbins_, linked_path)},
-  { NULL, N_("service"),     0,  29,   64, 0, offsetof(struct dbins_, service)},
-  { NULL, N_("facility"),    0,  30,   32, 0, offsetof(struct dbins_, facility) },
-  { NULL, N_("priority"),    0,  31,   32, 0, offsetof(struct dbins_, priority) },
-  { NULL, N_("syslog_msg"),  0,  32, 1024, 0, offsetof(struct dbins_, syslog_msg)  },
-
-  { NULL, N_("mode_old"),    0,  33,   16, 0, offsetof(struct dbins_, mode_old) },
-  { NULL, N_("mode_new"),    0,  34,   16, 0, offsetof(struct dbins_, mode_new) },
-  { NULL, N_("device_old"),  0,  35,   16, 0, offsetof(struct dbins_, device_old)}, 
-  { NULL, N_("device_new"),  0,  36,   16, 0, offsetof(struct dbins_, device_new)},
-  { NULL, N_("owner_old"),   0,  37,    9, 0, offsetof(struct dbins_, owner_old)},
-  { NULL, N_("owner_new"),   0,  38,    9, 0, offsetof(struct dbins_, owner_new)},
-  { NULL, N_("group_old"),   0,  39,    9, 0, offsetof(struct dbins_, group_old)},
-  { NULL, N_("group_new"),   0,  40,    9, 0, offsetof(struct dbins_, group_new)},
-  { NULL, N_("ctime_old"),   0,  41,   20, 0, offsetof(struct dbins_, ctime_old)},
-  { NULL, N_("ctime_new"),   0,  42,   20, 0, offsetof(struct dbins_, ctime_new)},
-  { NULL, N_("atime_old"),   0,  43,   20, 0, offsetof(struct dbins_, atime_old)},
-  { NULL, N_("atime_new"),   0,  44,   20, 0, offsetof(struct dbins_, atime_new)},
-  { NULL, N_("mtime_old"),   0,  45,   20, 0, offsetof(struct dbins_, mtime_old)},
-  { NULL, N_("mtime_new"),   0,  46,   20, 0, offsetof(struct dbins_, mtime_new)},
-  { NULL, N_("chksum_old"),  0,  47,   50, 0, offsetof(struct dbins_, chksum_old)},
-  { NULL, N_("chksum_new"),  0,  48,   50, 0, offsetof(struct dbins_, chksum_new)},
-  { NULL, N_("link_old"),    0,  49, MAX_PATH_STORE+1, 0, offsetof(struct dbins_, link_old)},
-  { NULL, N_("link_new"),    0,  50, MAX_PATH_STORE+1, 0, offsetof(struct dbins_, link_new)},
-
-  /* These go into  dbins.ulong_data[n - START_SEC_LONGS] */             				    
-  { NULL, N_("size_old"),     0,  51,    0, 0, 0  },
-  { NULL, N_("size_new"),     0,  52,    0, 0, 0  },
-  { NULL, N_("hardlinks_old"),0,  53,    0, 0, 0  },
-  { NULL, N_("hardlinks_new"),0,  54,    0, 0, 0  },
-  { NULL, N_("inode_old"),    0,  55,    0, 0, 0  }, 
-  { NULL, N_("inode_new"),    0,  56,    0, 0, 0  }, 
-					    
-  { NULL, N_("imode_old"),    0,  57,    0, 0, 0  },
-  { NULL, N_("imode_new"),    0,  58,    0, 0, 0  },
-  { NULL, N_("iattr_old"),    0,  59,    0, 0, 0  },
-  { NULL, N_("iattr_new"),    0,  60,    0, 0, 0  },
-  { NULL, N_("idevice_old"),  0,  61,    0, 0, 0  }, 
-  { NULL, N_("idevice_new"),  0,  62,    0, 0, 0  }, 
-  { NULL, N_("iowner_old"),   0,  63,    0, 0, 0  },
-  { NULL, N_("iowner_new"),   0,  64,    0, 0, 0  },
-  { NULL, N_("igroup_old"),   0,  65,    0, 0, 0  },
-  { NULL, N_("igroup_new"),   0,  66,    0, 0, 0  },
-
-  { NULL, N_("port"),         0,  67,    0, 0, 0  },
-  { NULL, N_("return_code"),  0,  68,    0, 0, 0  },
-
-  { NULL, N_("checkflags_old"), 0,  69,    0, 0, 0  },
-  { NULL, N_("checkflags_new"), 0,  70,    0, 0, 0  },
-
-  /* END_SEC_LONGS                                         */
-
-  { NULL, N_("host"),         0,  71,   64, 0, offsetof(struct dbins_, fromhost)},
-  { NULL, N_("attr_old"),     0,  72,   16, 0, offsetof(struct dbins_, attr_old)},
-  { NULL, N_("attr_new"),     0,  73,   16, 0, offsetof(struct dbins_, attr_new)},
-  { NULL, N_("acl_old"),      0,  74, 1024, 0, offsetof(struct dbins_, acl_old)},
-  { NULL, N_("acl_new"),      0,  75, 1024, 0, offsetof(struct dbins_, acl_new)},
-
-  { NULL, NULL,      0,  0, 0, 0, 0 }
+  { NULL, N_("userid"),      0,   6,    9, offsetof(struct dbins_, user)  },
+  { NULL, N_("group"),       0,   7,    9, offsetof(struct dbins_, group)  },
+  { NULL, N_("program"),     0,   8,    8, offsetof(struct dbins_, program)  },
+  { NULL, N_("subroutine"),  0,   9,   16, offsetof(struct dbins_, subroutine)},
+  { NULL, N_("status"),      0,  10,   12, offsetof(struct dbins_, status)  },
+  { NULL, N_("hash"),        0,  11,   50, offsetof(struct dbins_, hash)  },
+  { NULL, N_("path_data"),   0,  12, 1024, offsetof(struct dbins_, path_data)  },
+  { NULL, N_("hash_data"),   0,  13,   50, offsetof(struct dbins_, hash_data)  },
+  { NULL, N_("key_uid"),     0,  14,   64, offsetof(struct dbins_, key_uid)  },
+  { NULL, N_("key_uid_data"),0,  15,   64, offsetof(struct dbins_, key_uid_data)},
+  { NULL, N_("key_id"),      0,  16,   16, offsetof(struct dbins_, key_id)  },
+  { NULL, N_("module"),      0,  17,    8, offsetof(struct dbins_, module)  },
+  { NULL, N_("syscall"),     0,  19,   16, offsetof(struct dbins_, syscall)  },
+  { NULL, N_("ip"),          0,  20,   16, offsetof(struct dbins_, ip)  },
+  { NULL, N_("tty"),         0,  21,   16, offsetof(struct dbins_, tty)  },
+  { NULL, N_("peer"),        0,  22,   64, offsetof(struct dbins_, peer)  },
+  { NULL, N_("obj"),         0,  23, 1024, offsetof(struct dbins_, obj)  },
+  { NULL, N_("interface"),   0,  24,   64, offsetof(struct dbins_, interface)},
+  { NULL, N_("time"),        0,  25,   64, offsetof(struct dbins_, ltime)  },
+  { NULL, N_("dir"),         0,  26, 1024, offsetof(struct dbins_, dir)  },
+  { NULL, N_("linked_path"), 0,  27, 1024, offsetof(struct dbins_, linked_path)},
+  { NULL, N_("service"),     0,  29,   64, offsetof(struct dbins_, service)},
+  { NULL, N_("facility"),    0,  30,   32, offsetof(struct dbins_, facility) },
+  { NULL, N_("priority"),    0,  31,   32, offsetof(struct dbins_, priority) },
+  { NULL, N_("syslog_msg"),  0,  32, 1024, offsetof(struct dbins_, syslog_msg)  },
+
+  { NULL, N_("mode_old"),    0,  33,   16, offsetof(struct dbins_, mode_old) },
+  { NULL, N_("mode_new"),    0,  34,   16, offsetof(struct dbins_, mode_new) },
+  { NULL, N_("device_old"),  0,  35,   16, offsetof(struct dbins_, device_old)}, 
+  { NULL, N_("device_new"),  0,  36,   16, offsetof(struct dbins_, device_new)},
+  { NULL, N_("owner_old"),   0,  37,    9, offsetof(struct dbins_, owner_old)},
+  { NULL, N_("owner_new"),   0,  38,    9, offsetof(struct dbins_, owner_new)},
+  { NULL, N_("group_old"),   0,  39,    9, offsetof(struct dbins_, group_old)},
+  { NULL, N_("group_new"),   0,  40,    9, offsetof(struct dbins_, group_new)},
+  { NULL, N_("ctime_old"),   0,  41,   20, offsetof(struct dbins_, ctime_old)},
+  { NULL, N_("ctime_new"),   0,  42,   20, offsetof(struct dbins_, ctime_new)},
+  { NULL, N_("atime_old"),   0,  43,   20, offsetof(struct dbins_, atime_old)},
+  { NULL, N_("atime_new"),   0,  44,   20, offsetof(struct dbins_, atime_new)},
+  { NULL, N_("mtime_old"),   0,  45,   20, offsetof(struct dbins_, mtime_old)},
+  { NULL, N_("mtime_new"),   0,  46,   20, offsetof(struct dbins_, mtime_new)},
+  { NULL, N_("chksum_old"),  0,  47,   50, offsetof(struct dbins_, chksum_old)},
+  { NULL, N_("chksum_new"),  0,  48,   50, offsetof(struct dbins_, chksum_new)},
+  { NULL, N_("link_old"),    0,  49, 1024, offsetof(struct dbins_, link_old)},
+  { NULL, N_("link_new"),    0,  50, 1024, offsetof(struct dbins_, link_new)},
+               
+  { NULL, N_("size_old"),     0,  51,    0, 0  },
+  { NULL, N_("size_new"),     0,  52,    0, 0  },
+  { NULL, N_("hardlinks_old"),0,  53,    0, 0  },
+  { NULL, N_("hardlinks_new"),0,  54,    0, 0  },
+  { NULL, N_("inode_old"),    0,  55,    0, 0  }, 
+  { NULL, N_("inode_new"),    0,  56,    0, 0  }, 
+
+  { NULL, N_("imode_old"),    0,  57,    0, 0  },
+  { NULL, N_("imode_new"),    0,  58,    0, 0  },
+  { NULL, N_("iattr_old"),    0,  59,    0, 0  },
+  { NULL, N_("iattr_new"),    0,  60,    0, 0  },
+  { NULL, N_("idevice_old"),  0,  61,    0, 0  }, 
+  { NULL, N_("idevice_new"),  0,  62,    0, 0  }, 
+  { NULL, N_("iowner_old"),   0,  63,    0, 0  },
+  { NULL, N_("iowner_new"),   0,  64,    0, 0  },
+  { NULL, N_("igroup_old"),   0,  65,    0, 0  },
+  { NULL, N_("igroup_new"),   0,  66,    0, 0  },
+
+  { NULL, N_("port"),         0,  67,    0, 0  },
+  { NULL, N_("return_code"),  0,  68,    0, 0  },
+  /* { NULL, N_("userid"),        0,  69,    0, 0  }, old 'userid', 1.8.1 */
+
+  { NULL, N_("host"),         0,  70,   64, offsetof(struct dbins_, fromhost)},
+  { NULL, N_("attr_old"),     0,  71,   16, offsetof(struct dbins_, attr_old)},
+  { NULL, N_("attr_new"),     0,  72,   16, offsetof(struct dbins_, attr_new)},
+
+  { NULL, NULL,      0,  0, 0, 0 }
 };
 
-#define SH_SLOT_CHECKFLAGS 69
-
-/* need special attention b/o reserved SQL words    */
-#define SH_SLOT_HOST    71
+#define SH_SLOT_HOST    70
 #define SH_SLOT_GROUP    7
-
-/* these go into dbins.ulong_data[n-START_SEC_LONGS */
 #define START_SEC_LONGS 51
-#define END_SEC_LONGS   70
+#define END_SEC_LONGS   68
 
 #if defined(HAVE_INT_32)
@@ -239,15 +219,13 @@
 typedef unsigned char uint8;
 
-typedef struct md5_ctx
-{
-  uint32 A;
-  uint32 B;
-  uint32 C;
-  uint32 D;
-
-  uint32 total[2];
-  uint32 buflen;
-  char buffer[128];
+typedef struct
+{
+        uint32 h[4];
+        uint32 data[16];
+        uint8  offset;
+        uint32  nblocks;
+        int  count;
 } md5Param;
+
 
 
@@ -267,10 +245,10 @@
 static int  sh_persistent_dbconn = S_TRUE;
 
-int sh_database_use_persistent (const char * str)
+int sh_database_use_persistent (char * str)
 {
   return sh_util_flagval (str, &sh_persistent_dbconn);
 }
 
-static int insert_value (char * ptr, const char * str)
+static int insert_value (char * ptr, char * str)
 {
   if (!ptr || !str)
@@ -284,5 +262,5 @@
 static void init_db_entry (dbins * ptr)
 {
-  memset (ptr, 0, sizeof(dbins));
+  memset (ptr, (int) '\0', sizeof(dbins));
   ptr->next = NULL;
   return;
@@ -290,21 +268,21 @@
   
 
-int sh_database_set_database (const char * str)
+int sh_database_set_database (char * str)
 {
   return insert_value (db_name, str);
 }
-int sh_database_set_table (const char * str)
+int sh_database_set_table (char * str)
 {
   return insert_value (db_table, str);
 }
-int sh_database_set_host (const char * str)
+int sh_database_set_host (char * str)
 {
   return insert_value (db_host, str);
 }
-int sh_database_set_user (const char * str)
+int sh_database_set_user (char * str)
 {
   return insert_value (db_user, str);
 }
-int sh_database_set_password (const char * str)
+int sh_database_set_password (char * str)
 {
   return insert_value (db_password, str);
@@ -511,5 +489,4 @@
 static    OCIError  * o_error = NULL;
 static    OCIStmt   * o_statement;
-static    OCIBind   * o_bind = (OCIBind *) 0;
 static    text        o_errormsg[512];
 static    sb4         o_errorcode;
@@ -537,5 +514,5 @@
     {
       if (str[len-1] == '\n')
-	str[len-1] = '\0';
+	str[len-1] == '\0';
     }
   return str;
@@ -549,6 +526,4 @@
   char         row_query[128];
   int          retry     = 0;
-  static SH_TIMEOUT sh_timer = { 0, 3600, S_TRUE };
-
 
   SL_ENTER(_("sh_database_query"));
@@ -585,5 +560,5 @@
  oracle_doconnect:
 
-  if (!getenv("ORACLE_HOME")) /* flawfinder: ignore */
+  if (!getenv("ORACLE_HOME")) 
     {
       sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
@@ -648,39 +623,27 @@
 	       (OraText*) db_password, sl_strlen(db_password), 
 	       (OraText*) db_name,     sl_strlen(db_name))) 
-      {
-   
-	connected = 0;
-
-	sh_timer.flag_ok = S_FALSE;
-
-	if (S_TRUE == sh_util_timeout_check(&sh_timer))
-	  {
-	    OCIErrorGet(o_error, 1, NULL, &o_errorcode, 
-			o_errormsg, sizeof(o_errormsg), OCI_HTYPE_ERROR);
-	    sh_stripnl (o_errormsg);
-	    sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
-			    o_errormsg, 
-			    _("sh_database_query"));
-	    sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
-			    _("check database is listed in tnsnames.ora"), 
-			    _("sh_database_query"));
-	    sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
-			    _("check tnsnames.ora readable"), 
-			    _("sh_database_query"));
-	    sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
-			    _("check database accessible with sqlplus"), 
-			    _("sh_database_query"));
-	    sl_snprintf(row_query, 127, 
-			_("OCILogon: Connection to database '%s' failed"), 
-			db_name); 
-	    sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-			    row_query, _("sh_database_query")); 
-
-	    goto err_out;
-	  }
-	else
-	  {
-	    SL_RETURN(0, _("sh_database_query"));
-	  }
+      {   
+	OCIErrorGet(o_error, 1, NULL, &o_errorcode, 
+		    o_errormsg, sizeof(o_errormsg), OCI_HTYPE_ERROR);
+	sh_stripnl (o_errormsg);
+	sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
+			o_errormsg, 
+			_("sh_database_query"));
+	sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
+			_("check database is listed in tnsnames.ora"), 
+			_("sh_database_query"));
+	sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
+			_("check tnsnames.ora readable"), 
+			_("sh_database_query"));
+	sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
+			_("check database accessible with sqlplus"), 
+			_("sh_database_query"));
+	sl_snprintf(row_query, 127, 
+		    _("OCILogon: Connection to database '%s' failed"), 
+		    db_name); 
+	sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
+			row_query, _("sh_database_query")); 
+	bad_init = 1;
+	SL_RETURN(-1, _("sh_database_query")); 
       }
  
@@ -695,16 +658,15 @@
  oracle_connected:
 
-  /* Get row index
-   */
-  sl_strlcpy (row_query, _("SELECT log_log_index_seq.NEXTVAL FROM dual"), 128);
-
+  /* 
+   * Insert
+   */
 #ifdef DB_DEBUG
   sh_error_handle(SH_ERR_NOTICE, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		  row_query, 
+		  query, 
 		  _("sh_database_query"));
 #endif
 
   if (OCIStmtPrepare(o_statement, o_error, 
-		     (OraText*) row_query, sl_strlen(row_query), 
+		     (OraText*) query, sl_strlen(query), 
 		     OCI_NTV_SYNTAX, OCI_DEFAULT))
     {
@@ -717,5 +679,5 @@
 		      _("sh_database_query"));
       if (retry == 0 && 
-	  (3114 == o_errorcode || 0 == strncmp(o_errormsg, _("ORA-03114"), 9))) 
+	  (3114 == o_errorcode || 0 == strncmp(o_errormsg, _("ORA-03114"), 9)) 
 	  { 
 	    ++retry; sh_database_reset(); goto oracle_doconnect; 
@@ -723,7 +685,8 @@
       goto err_out;
     }
-
-  if (OCIStmtExecute(o_servicecontext, o_statement, o_error, 
-		     0, 0, NULL, NULL, OCI_DEFAULT))
+ 
+   if (OCIStmtExecute(o_servicecontext, 
+		      o_statement, o_error, 1,  0, 
+		      NULL, NULL, OCI_COMMIT_ON_SUCCESS))
     {
       OCIErrorGet(o_error, 1, NULL, 
@@ -735,5 +698,5 @@
 		      _("sh_database_query"));
       if (retry == 0 && 
-	  (3114 == o_errorcode || 0 == strncmp(o_errormsg, _("ORA-03114"), 9))) 
+	  (3114 == o_errorcode || 0 == strncmp(o_errormsg, _("ORA-03114"), 9)) 
 	  { 
 	    ++retry; sh_database_reset(); goto oracle_doconnect; 
@@ -742,7 +705,24 @@
     }
 
-  if (OCIDefineByPos (o_statement, &o_define, o_error, 1, 
-		      &result, sizeof(result), 
-		      SQLT_INT, 0, 0, 0, OCI_DEFAULT))
+#ifdef DB_DEBUG
+  sh_error_handle(SH_ERR_NOTICE, FIL__, __LINE__, 0, MSG_E_SUBGEN,
+		  _("No error on insert"), 
+		  _("sh_database_query"));
+#endif
+
+  /* Get row index
+   */
+  sl_strlcpy (row_query, _("SELECT MAX(log_index) FROM "), 128);
+  sl_strlcat (row_query, db_table, 128);
+
+#ifdef DB_DEBUG
+  sh_error_handle(SH_ERR_NOTICE, FIL__, __LINE__, 0, MSG_E_SUBGEN,
+		  row_query, 
+		  _("sh_database_query"));
+#endif
+
+  if (OCIStmtPrepare(o_statement, o_error, 
+		     (OraText*) row_query, sl_strlen(row_query), 
+		     OCI_NTV_SYNTAX, OCI_DEFAULT))
     {
       OCIErrorGet(o_error, 1, NULL, 
@@ -754,5 +734,5 @@
 		      _("sh_database_query"));
       if (retry == 0 && 
-	  (3114 == o_errorcode || 0 == strncmp(o_errormsg, _("ORA-03114"), 9))) 
+	  (3114 == o_errorcode || 0 == strncmp(o_errormsg, _("ORA-03114"), 9)) 
 	  { 
 	    ++retry; sh_database_reset(); goto oracle_doconnect; 
@@ -760,5 +740,7 @@
       goto err_out;
     }
-  if (OCIStmtFetch (o_statement, o_error, 1, OCI_FETCH_NEXT, OCI_DEFAULT))
+
+  if (OCIStmtExecute(o_servicecontext, o_statement, o_error, 
+		     0, 0, NULL, NULL, OCI_DEFAULT))
     {
       OCIErrorGet(o_error, 1, NULL, 
@@ -770,5 +752,5 @@
 		      _("sh_database_query"));
       if (retry == 0 && 
-	  (3114 == o_errorcode || 0 == strncmp(o_errormsg, _("ORA-03114"), 9))) 
+	  (3114 == o_errorcode || 0 == strncmp(o_errormsg, _("ORA-03114"), 9)) 
 	  { 
 	    ++retry; sh_database_reset(); goto oracle_doconnect; 
@@ -776,25 +758,8 @@
       goto err_out;
     }
-  
-#ifdef DB_DEBUG
-  sl_snprintf(row_query, 127, _("Returned value: %d"), result); 
-  sh_error_handle(SH_ERR_NOTICE, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		  row_query, 
-		  _("sh_database_query"));
-#endif
-
-  *id = result;
-
-  /* do the insert
-   */
-#ifdef DB_DEBUG
-  sh_error_handle(SH_ERR_NOTICE, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		  query, 
-		  _("sh_database_query"));
-#endif
-
-  if (OCIStmtPrepare(o_statement, o_error, 
-		     (OraText*) query, sl_strlen(query), 
-		     OCI_NTV_SYNTAX, OCI_DEFAULT))
+
+  if (OCIDefineByPos (o_statement, &o_define, o_error, 1, 
+		      &result, sizeof(result), 
+		      SQLT_INT, 0, 0, 0, OCI_DEFAULT))
     {
       OCIErrorGet(o_error, 1, NULL, 
@@ -806,14 +771,11 @@
 		      _("sh_database_query"));
       if (retry == 0 && 
-	  (3114 == o_errorcode || 0 == strncmp(o_errormsg, _("ORA-03114"), 9))) 
-	{ 
-	  ++retry; sh_database_reset(); goto oracle_doconnect; 
-	}
+	  (3114 == o_errorcode || 0 == strncmp(o_errormsg, _("ORA-03114"), 9)) 
+	  { 
+	    ++retry; sh_database_reset(); goto oracle_doconnect; 
+	  }
       goto err_out;
     }
- 
-  if (OCIBindByPos(o_statement, &o_bind, o_error, 1,
-		   (dvoid *) &result, (sword) sizeof(result), SQLT_INT, 
-		   (dvoid *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT))
+  if (OCIStmtFetch (o_statement, o_error, 1, OCI_FETCH_NEXT, OCI_DEFAULT))
     {
       OCIErrorGet(o_error, 1, NULL, 
@@ -825,5 +787,5 @@
 		      _("sh_database_query"));
       if (retry == 0 && 
-	  (3114 == o_errorcode || 0 == strncmp(o_errormsg, _("ORA-03114"), 9))) 
+	  (3114 == o_errorcode || 0 == strncmp(o_errormsg, _("ORA-03114"), 9)) 
 	  { 
 	    ++retry; sh_database_reset(); goto oracle_doconnect; 
@@ -831,29 +793,13 @@
       goto err_out;
     }
-
-   if (OCIStmtExecute(o_servicecontext, 
-		      o_statement, o_error, 1,  0, 
-		      NULL, NULL, OCI_COMMIT_ON_SUCCESS))
-    {
-      OCIErrorGet(o_error, 1, NULL, 
-		  &o_errorcode, o_errormsg, sizeof(o_errormsg), 
-		  OCI_HTYPE_ERROR);
-      sh_stripnl (o_errormsg);
-      sh_error_handle((-1), FIL__, __LINE__, (long) o_errorcode, MSG_E_SUBGEN,
-		      o_errormsg, 
-		      _("sh_database_query"));
-      if (retry == 0 && 
-	  (3114 == o_errorcode || 0 == strncmp(o_errormsg, _("ORA-03114"), 9))) 
-	  { 
-	    ++retry; sh_database_reset(); goto oracle_doconnect; 
-	  }
-      goto err_out;
-    }
-
+  
 #ifdef DB_DEBUG
+  sl_snprintf(row_query, 127, _("Returned value: %d"), result); 
   sh_error_handle(SH_ERR_NOTICE, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		  _("No error on insert"), 
+		  row_query, 
 		  _("sh_database_query"));
 #endif
+
+  *id = result;
 
   if (sh_persistent_dbconn == S_FALSE)
@@ -872,6 +818,13 @@
    * Error
    */
-  sh_database_reset();
-
+  if (sh_persistent_dbconn == S_FALSE)
+    {
+      OCILogoff(o_servicecontext, o_error);
+      OCIHandleFree((dvoid *) o_statement,      OCI_HTYPE_STMT);
+      OCIHandleFree((dvoid *) o_servicecontext, OCI_HTYPE_SVCCTX);
+      OCIHandleFree((dvoid *) o_error,          OCI_HTYPE_ERROR);
+      o_error = NULL;
+      connected = 0;
+    }
   SL_RETURN(-1, _("sh_database_query"));
 }
@@ -887,14 +840,8 @@
  ******************************************************************/
 
-#if defined(HAVE_PGSQL_LIBPQ_FE_H)
+#ifdef HAVE_PGSQL_LIBPQ_FE_H
 #include <pgsql/libpq-fe.h>
-#elif defined(HAVE_POSTGRESQL_LIBPQ_FE_H)
-#include <postgresql/libpq-fe.h>
 #else
-#if !defined(USE_UNO)
 #include <libpq-fe.h>
-#else
-#include <postgresql/libpq-fe.h>
-#endif
 #endif
 
@@ -915,6 +862,4 @@
   PGresult        * res;
   unsigned int      i;
-  const char      * params[1];
-  char              id_param[32];
   static SH_TIMEOUT sh_timer = { 0, 3600, S_TRUE };
 
@@ -987,29 +932,26 @@
 
 
-  /* get the unique row index
-   */
-  res = PQexec(conn, _("SELECT NEXTVAL('log_log_index_seq')"));
-  if (PQresultStatus(res) != PGRES_TUPLES_OK) 
+  /* do the insert
+   */
+  res = PQexec(conn, query);
+  if (PQresultStatus(res) != PGRES_COMMAND_OK) 
     {
       PQclear(res);
       goto err_out;
     }
-
-  *id = atoi (PQgetvalue(res, 0, 0)); 
   PQclear(res);
 
-  sl_snprintf(id_param, 32, "%ld", *id);
-  params[0] = id_param;
-
-  /* do the insert
-   */
-  res = PQexecParams(conn, query, 1, NULL, params, NULL, NULL, 1);
-  if (PQresultStatus(res) != PGRES_COMMAND_OK) 
+  /* get the unique row index
+   */
+  res = PQexec(conn, _("SELECT last_value FROM log_log_index_seq"));
+  if (PQresultStatus(res) != PGRES_TUPLES_OK) 
     {
       PQclear(res);
       goto err_out;
     }
+
+  *id = atoi (PQgetvalue(res, 0, 0)); 
+
   PQclear(res);
-
   if (S_FALSE == sh_persistent_dbconn)
     {
@@ -1057,5 +999,5 @@
 static int        connection_status = S_FALSE;
 
-void sh_database_reset(void)
+void sh_database_reset()
 {
   connection_status = S_FALSE;
@@ -1067,5 +1009,5 @@
 {
   int               status = 0;
-  const char      * p;
+  char            * p;
   static MYSQL    * db_conn = NULL;
   static SH_TIMEOUT sh_timer = { 0, 3600, S_TRUE };
@@ -1114,15 +1056,4 @@
 	}
 
-      /* Read in defaults from /etc/my.cnf and associated files,
-       * suggested by arjones at simultan dyndns org
-       * see: - http://dev.mysql.com/doc/refman/5.0/en/option-files.html
-       *        for the my.cnf format,
-       *      - http://dev.mysql.com/doc/refman/5.0/en/mysql-options.html
-       *        for possible options
-       * We don't check the return value because it's useless (failure due 
-       * to lack of access permission is not reported).
-       */
-      mysql_options(db_conn, MYSQL_READ_DEFAULT_GROUP, _("samhain"));
-
       status = 0;
   
@@ -1144,5 +1075,4 @@
 	    }
 	}
-      
       connection_status = S_TRUE;
     }
@@ -1169,5 +1099,5 @@
     }
 
-  if (flag_err_debug == S_TRUE)
+  if (flag_err_debug == SL_TRUE)
     {
       p = mysql_info (db_conn);
@@ -1221,30 +1151,32 @@
   long len;
 
-  if (!((end == NULL) || (val == NULL) || (size == NULL)))
-    {
-      if (val[0] != '\0')
-	{
-	  if (*size > 1)
-	    {
-	      *end = ','; ++end; (*size) -= 1;
-	      if (flag == 1) { *end = '\''; ++end; (*size) -= 1; }
-	      *end = '\0';
-	    }
-	  len = (long) strlen(val);
-	  if ((long) *size > (len+1))
-	    {
-	      (void) sl_strlcat(end, val, (size_t) *size);
-	      end   += len; (*size) -= len;
-	      if (flag == 1) { *end = '\''; ++end;  (*size) -= 1; }
-	      *end = '\0'; 
-	    }
-	}
-    }
-  
+  if (!end    || !val   || !size)
+    return end;
+
+  if (val[0] == '\0')
+    {
+      return end;
+    }
+  else
+    {
+      if (*size > 1)
+	{
+	  *end = ','; ++end; (*size) -= 1;
+	  if (flag == 1) { *end = '\''; ++end; (*size) -= 1; }
+	  *end = '\0';
+	}
+      len = (long) strlen(val);
+      if ((long) *size > (len+1))
+	{
+	  (void) sl_strlcat(end, val, (size_t) *size);
+	  end   += len; (*size) -= len;
+	  if (flag == 1) { *end = '\''; ++end;  (*size) -= 1; }
+	  *end = '\0'; 
+	}
+    }
   return end;
 }
 
-#define SH_QUERY_MAX SH_MSG_BUF
-/* define SH_QUERY_MAX 16383 */
+#define SH_QUERY_MAX 16383
 
 static
@@ -1257,4 +1189,5 @@
   char * values;
 
+  int    status;
   long   the_id;
   int    size;
@@ -1271,7 +1204,4 @@
   int  cnt;
 
-  size_t len_val;
-  size_t len_col;
-
   SL_ENTER(_("sh_database_entry"));
 
@@ -1294,10 +1224,10 @@
   /*@+bufferoverflowhigh@*/
 
-#if defined(WITH_ORACLE)
+#ifdef WITH_ORACLE
   /* Oracle needs some help for the time format (fix by Michael Somers)
    */
   (void)
   sl_snprintf (values, SH_QUERY_MAX,  
-	       _("(:1,%s,%c%s%c,to_date(%c%s%c,'YYYY-MM-DD HH24:MI:SS'),%c%s%c,%c%s%c"),
+	       _("(%s,%c%s%c,to_date(%c%s%c,'YYYY-MM-DD HH24:MI:SS'),%c%s%c,%c%s%c"),
                id >= 0 ? num : _("NULL"),
                '\'', db_entry->host,'\'', 
@@ -1307,21 +1237,4 @@
                (db_entry->msg[0] == '\0' ? _("NULL") : db_entry->msg),
                '\'');
-  (void) sl_snprintf (columns, 1023, 
-		      _("(log_index,log_ref,log_host,log_time,log_sev,log_msg"));
-#elif defined(WITH_POSTGRES)
-  /* Prepare query for PQexecParams
-   */
-  (void)
-  sl_snprintf (values, SH_QUERY_MAX, 
-	       _("($1,%s,%c%s%c,%c%s%c,%c%s%c,%c%s%c"),
-	       id >= 0 ? num : _("NULL"),
-	       '\'', db_entry->host,'\'', 
-	       '\'', db_entry->time,'\'', 
-	       '\'', db_entry->sev, '\'',
-	       '\'', 
-	       (db_entry->msg[0] == '\0' ? _("NULL") : db_entry->msg), 
-	       '\'');
-  (void) sl_snprintf (columns, 1023, 
-		      _("(log_index,log_ref,log_host,log_time,log_sev,log_msg"));
 #else
   (void)
@@ -1334,8 +1247,8 @@
 	       (db_entry->msg[0] == '\0' ? _("NULL") : db_entry->msg), 
 	       '\'');
+#endif
+
   (void) sl_snprintf (columns, 1023, 
 		      _("(log_ref,log_host,log_time,log_sev,log_msg"));
-#endif
-
 
   /*@-type@*//* byte* versus char[..] */
@@ -1354,11 +1267,8 @@
   /*@+type@*/
 
-  len_val = strlen(values);
-  size    =  (int) (SH_QUERY_MAX - len_val);
-  end     =  values + len_val;
-
-  len_col = strlen(columns);
-  c_size  =  1023   - (int) len_col; /* sizeof(colums) == 1024 */
-  c_end   =  columns + len_col;
+  size   =  (int) (SH_QUERY_MAX - strlen(values));
+  end    =  values + strlen(values);
+  c_size =  1023   - (int) strlen(columns); /* sizeof(colums) == 1024 */
+  c_end  =  columns + strlen(columns);
 
   i = 4;
@@ -1366,10 +1276,4 @@
   while (attr_tab[i].attr != NULL)
     {
-      if (SH_SLOT_CHECKFLAGS == attr_tab[i].val) {
-	if (db_entry->ulong_data[attr_tab[i].val-START_SEC_LONGS] == 0 &&
-	    db_entry->ulong_data[attr_tab[i+1].val-START_SEC_LONGS] == 0)
-	  { i+= 2; continue; } 
-      }
-
       if (attr_tab[i].size != 0)
 	{
@@ -1386,26 +1290,20 @@
 	  if (p != end)
 	    {
-	      if ((attr_tab[i].val != SH_SLOT_HOST) &&
-		  (attr_tab[i].val != SH_SLOT_GROUP))
-		{
-		  c_end = null_or_val (c_end, attr_tab[i].attr, &c_size,0);
-		}
+	      /* 
+	       * 'host' is a reserved word in SQL
+	       */
+	      if (attr_tab[i].val == SH_SLOT_HOST)
+		c_end = null_or_val (c_end, _("fromhost"), &c_size,0);
+	      /* 
+	       * 'group' is a reserved word in SQL
+	       */
+	      else if (attr_tab[i].val == SH_SLOT_GROUP)
+		c_end = null_or_val (c_end, _("grp"), &c_size,0);
 	      else
-		{
-		  /* 
-		   * 'host' is a reserved word in SQL
-		   */
-		  if (attr_tab[i].val == SH_SLOT_HOST)
-		    c_end = null_or_val (c_end, _("fromhost"), &c_size,0);
-		  /* 
-		   * 'group' is a reserved word in SQL
-		   */
-		  else /* if (attr_tab[i].val == SH_SLOT_GROUP) */
-		    c_end = null_or_val (c_end, _("grp"), &c_size,0);
-		}
+		c_end = null_or_val (c_end, attr_tab[i].attr, &c_size,0);
 	    }
 	  /*@-type@*//* byte* versus char[..] */
 	  if (attr_tab[i].inHash == 1 && 
-	      *((char *)(db_entry)+attr_tab[i].off) != '\0')
+	      ((char *)(db_entry)+attr_tab[i].off) != '\0')
 	    {
 	      (void)md5Update(&crc, 
@@ -1419,6 +1317,6 @@
 	{
 	  (void)
-	  sl_snprintf(end, (size_t)(size-1), _(",\'%lu\'"), 
-		      db_entry->ulong_data[attr_tab[i].val-START_SEC_LONGS]);
+	  sl_snprintf(end, (size_t)(size-1), _(",\'%ld\'"), 
+		      db_entry->long_data[attr_tab[i].val-START_SEC_LONGS]);
 	  while (*end != '\0') { ++end; --size; }
 	  (void) sl_snprintf(c_end, (size_t)(c_size-1), 
@@ -1430,5 +1328,5 @@
 	      (void)
 	      md5Update(&crc,
-			(sh_byte *) db_entry->ulong_data[attr_tab[i].val-START_SEC_LONGS], 
+			(sh_byte *) db_entry->long_data[attr_tab[i].val-START_SEC_LONGS], 
 			sizeof(long));
 	      /*@+type@*/
@@ -1463,5 +1361,5 @@
 		      db_table, columns, values);
 
-  sh_database_query (query, &the_id);
+  status = sh_database_query (query, &the_id);
 
   /*@-usedef@*//* no, 'values' is allocated here */
@@ -1473,66 +1371,45 @@
 }
  
-static int sh_database_comp_attr (const void *m1, const void *m2) 
-{
-  const my_attr *mi1 = (const my_attr *) m1;
-  const my_attr *mi2 = (const my_attr *) m2;
-  return strcmp(mi1->attr, mi2->attr);
-}
-
-
-static void init_attr_table(void)
+
+static void init_attr_table()
 {
   static  int first = S_TRUE;
-  int         i, j;
+  int         i;
 
 #ifdef SH_STEALTH
-  int     k;
-
-  if (first == S_FALSE)
-    return;
-
-  i = 0;
-  while (attr_tab[i].attr_o != NULL)
-    {
-      j = strlen(attr_tab[i].attr_o);
-      attr_tab[i].attr = calloc(1, j+1); /* only once */
-      if (NULL == attr_tab[i].attr)
-	return;
-      for (k = 0; k < j; ++k)
-	attr_tab[i].attr[k] = attr_tab[i].attr_o[k] ^ XOR_CODE;
-      attr_tab[i].attr[j] = '\0';
-      attr_tab[i].alen = strlen(attr_tab[i].attr_o);
-      ++i;
-    }
-  first = S_FALSE;
-
+  int     j, k;
+
+  if (first == S_TRUE)
+    {
+      i = 0;
+      while (attr_tab[i].attr_o != NULL)
+	{
+	  j = strlen(attr_tab[i].attr_o);
+	  attr_tab[i].attr = malloc (j+1); /* only once */
+	  if (NULL == attr_tab[i].attr)
+	    SL_RETURN (NULL, _("sh_database_parse"));
+	  for (k = 0; k < j; ++k)
+	    attr_tab[i].attr[k] = attr_tab[i].attr_o[k] ^ XOR_CODE;
+	  attr_tab[i].attr[j] = '\0';
+	  ++i;
+	}
+      first = S_FALSE;
+    }
 #else
-
-  if (first == S_FALSE)
-    return;
-
-  i = 0;
-  while (attr_tab[i].attr_o != NULL)
-    {
-      attr_tab[i].attr = attr_tab[i].attr_o;
-      attr_tab[i].alen = strlen(attr_tab[i].attr_o);
-      ++i;
-    }
-  first = S_FALSE;
-
+  if (first == S_TRUE)
+    {
+      i = 0;
+      while (attr_tab[i].attr_o != NULL)
+	{
+	  attr_tab[i].attr = attr_tab[i].attr_o;
+	  ++i;
+	}
+      first = S_FALSE;
+    }
 #endif
-
-  /* create a sorted table for binary search
-   */
-  attr_tab_srch = SH_ALLOC(i * sizeof(my_attr));
-  for (j=0; j<i; ++j)
-    memcpy(&attr_tab_srch[j], &attr_tab[j], sizeof(my_attr));
-  qsort(attr_tab_srch, i, sizeof(my_attr), sh_database_comp_attr);
-  attr_tab_srch_siz = i;
-
   return;
 }
 
-int sh_database_add_to_hash  (const char * str)
+int sh_database_add_to_hash  (char * str)
 {
   int i;
@@ -1555,53 +1432,26 @@
 }
 
-static int is_escaped(char * p_in) {
+static int is_escaped(char * p) {
 
   int    escp = 0;
   int    retv = S_TRUE;
-  unsigned char * p = (unsigned char *) p_in;
-
-  if (*p != '\0')
-    {
-      do 
-	{
-	  if (*p <=  126 && *p >= 32)
-	    {
-	      if (escp == 0)
-		{
-		  if      (!((*p == '\'') || (*p == '\"') || (*p == '\\'))) 
-		    /* do nothing */;
-		  else if (*p == '\\') 
-		    {
-#ifndef WITH_MYSQL
-		      if (p[1] == '\'')
-			{
-			  *p = '\'';
-			}
-#endif
-		      escp = 1;
-		    }
-		  else  
-		    retv = S_FALSE; /* (*p == '\'' || *p == '\"') */
-		}
-	      else /* escp == 1 */
-		{
-		  escp = 0;
-		}
-	    }
-	  else /* *p > 126 || *p < 32 */
-	    {
-	      retv = S_FALSE;
-	    }
-	  
-	  ++p;
-	  
-	} 
-      while (*p != '\0');
-    }
-
-  if (escp == 0)
-    return retv;
-  else
-    return S_FALSE;
+
+  while (*p != '\0') 
+    {
+      if (*p == '\\')
+	{
+	  escp = (escp == 1) ? 0 : 1;
+	}
+      else if (*p == '\'' && escp == 0)
+	{
+	  retv = S_FALSE;
+	}
+      else 
+	{
+	  escp = 0;
+	}
+      ++p;
+    }
+  return retv;
 }
 
@@ -1619,7 +1469,4 @@
   int     i;
   size_t  j;
-  my_attr * res;
-  my_attr key;
-  char    key_str[64];
 
   SL_ENTER(_("sh_database_parse"));
@@ -1638,6 +1485,11 @@
     SL_RETURN (NULL, _("sh_database_parse"));
 
-  while ((*p != '\0') && (*p != '>'))
-    {
+  while ((p != NULL) && (*p != '\0') && (*p != '>'))
+    {
+      if (p[0] == '/' && p[1] == '>')
+	SL_RETURN (&p[2], _("sh_database_parse"));
+      if (p[0] == '/' && p[1] == 'l' && p[2] == 'o' && 
+	  p[3] == 'g' && p[4] == '>')
+	SL_RETURN (&p[5], _("sh_database_parse"));
       if (p[0] == 'l' && p[1] == 'o' && p[2] == 'g' &&
 	  (p[3] == ' ' || p[3] == '>'))
@@ -1646,9 +1498,4 @@
 	  goto parse;
 	}
-      else if (p[0] == '/' && p[1] == '>')
-	SL_RETURN (&p[2], _("sh_database_parse"));
-      else if (p[0] == '/' && p[1] == 'l' && p[2] == 'o' && 
-	  p[3] == 'g' && p[4] == '>')
-	SL_RETURN (&p[5], _("sh_database_parse"));
       ++p;
     }
@@ -1657,19 +1504,13 @@
  parse:
 
-  while (*p == ' ' || *p == '>')
+  while ((p != NULL) && (*p == ' ' || *p == '>'))
     ++p;
 
-  if (*p == '\0')
+  if (!p || *p == '\0')
     SL_RETURN(NULL, _("sh_database_parse"));
-
-  if (*p != '<' && *p != '/')
-    goto par2;
 
   if (p[0] == '<' && p[1] == 'l' &&
       p[2] == 'o' && p[3] == 'g')
     {
-      /* 
-       * recursive call 
-       */
       new       = SH_ALLOC(sizeof(dbins));
       init_db_entry(new);
@@ -1685,83 +1526,62 @@
     SL_RETURN (&p[5], _("sh_database_parse"));
 
- par2:
 
   /* non-whitespace 
    */
-  for (i=0; i < 64; ++i)
-    {
-      if (p[i] != '=')
-	{
-	  key_str[i] = p[i];
-	}
-      else
-	{
-	  key_str[i] = '\0';
-	  break;
-	}
-    }
-  key_str[63] = '\0';
-  key.attr = &key_str[0];
-
-  res = bsearch(&key, attr_tab_srch, attr_tab_srch_siz,
-		sizeof(my_attr), sh_database_comp_attr);
-
-  if (res != NULL)
-    {
-      j = res->alen; /* strlen(attr_tab[i].attr); */
-      if (p[j] == '=' && p[j+1] == '"')
+  i = 0;
+  while (attr_tab[i].attr != NULL)
+    {
+      j = strlen(attr_tab[i].attr);
+      if (0 == strncmp(p, attr_tab[i].attr, j) && 
+	  p[j] == '=' && p[j+1] == '"')
 	{
 	  q = strchr(&p[j+2], '"');
-	  if (q)
-	    {
-	      *q = '\0';
-
-	      if (S_TRUE == is_escaped(&p[j+2])) {
-
-		if      (res->val == 1)
-		  (void) sl_strlcpy(db_entry->sev, &p[j+2], 
-				    (size_t)res->size);
-		else if (res->val == 2)
-		  {
-		    z = strchr(&p[j+2], 'T');
-		    if (z) *z = ' ';
-		    (void) sl_strlcpy(db_entry->time, &p[j+2],  20);
-		  }
-		else if (res->val == 3)
-		  (void) sl_strlcpy(db_entry->host, &p[j+2], 
-				    (size_t) res->size);
-		else if (res->val == 4)
-		  (void) sl_strlcpy(db_entry->msg,  &p[j+2], 
-				    (size_t) res->size);
-		else if (res->size != 0)
-		  {
-		    (void) sl_strlcpy( (((char *)(db_entry))+ res->off),
-				       &p[j+2], 
-				       (size_t) res->size);
-		  }
-		else if (res->val >= START_SEC_LONGS && res->val <= END_SEC_LONGS)
-		  {
-		    db_entry->ulong_data[res->val-START_SEC_LONGS]
-		      = strtoul(&p[j+2], (char **) NULL, 0);
-		  }
-
-		*q = '"';
-		p  = q; 
-		++p;
-
-		goto parse;
-	      }
-	      else { /* S_FALSE == is_escaped(&p[j+2]) */
-		sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
-				_("Message not properly escaped"), 
-				_("sh_database_parse"));
-		SL_RETURN(NULL, _("sh_database_parse"));
-	      }
-	    }
-	  else /* q == NULL */
+	  if (!q)
 	    {
 	      SL_RETURN(NULL, _("sh_database_parse"));
 	    }
-	}
+	  else
+	    {
+	      *q = '\0';
+
+	      if (S_FALSE == is_escaped(&p[j+2])) {
+		SL_RETURN(NULL, _("sh_database_parse"));
+	      }
+
+	      if      (attr_tab[i].val == 1)
+		(void) sl_strlcpy(db_entry->sev, &p[j+2], 
+				  (size_t)attr_tab[i].size);
+	      else if (attr_tab[i].val == 2)
+		{
+		  z = strchr(&p[j+2], 'T');
+		  if (z) *z = ' ';
+		  (void) sl_strlcpy(db_entry->time, &p[j+2],  20);
+		}
+	      else if (attr_tab[i].val == 3)
+		(void) sl_strlcpy(db_entry->host, &p[j+2], 
+				  (size_t) attr_tab[i].size);
+	      else if (attr_tab[i].val == 4)
+		(void) sl_strlcpy(db_entry->msg,  &p[j+2], 
+				  (size_t) attr_tab[i].size);
+	      else if (attr_tab[i].size != 0)
+		{
+		  (void) sl_strlcpy( (((char *)(db_entry))+ attr_tab[i].off),
+				     &p[j+2], 
+				     (size_t) attr_tab[i].size);
+		}
+	      else if (attr_tab[i].val >= START_SEC_LONGS)
+		{
+		  db_entry->long_data[attr_tab[i].val-START_SEC_LONGS]
+		    = atol(&p[j+2]);
+		}
+
+	      *q = '"';
+	      p  = q; 
+	      ++p;
+
+	      goto parse;
+	    }
+	}
+      ++i;
     }
 
@@ -1776,58 +1596,18 @@
 static int enter_wrapper = 1;
 
-int set_enter_wrapper (const char * str)
+int set_enter_wrapper (char * str)
 {
   return sh_util_flagval(str, &enter_wrapper);
 }
 
-/* recursively enter linked list of messages into database, last first
- * - last is client (if this is a client message received by client)
- */
-long sh_database_insert_rec (dbins * curr, int depth, char * host)
-{
-  unsigned long    id = 0;
-
-  SL_ENTER(_("sh_database_insert_rec"));
-
-  if (curr->next)
-    {
-      /*
-      prev = curr->next;
-      sl_strlcpy(prev->host, curr->host, 64);
-      id = sh_database_insert_rec (curr->next, (depth + 1));
-      */
-      ++depth;
-      id = sh_database_insert_rec (curr->next, depth, curr->host);
-    }
-
-  if (host) 
-    sl_strlcpy(curr->host, host, 64);
-
-  if (id != 0)                       /* this is a server wrapper          */
-    {
-      if (enter_wrapper != 0)
-	{
-	  id = sh_database_entry (curr, id);
-	}
-    }
-  else
-    {
-      /*
-       * id = -1 is the client message; log_ref will be NULL 
-       */
-      if (depth > 0)                  /* this is a client message         */
-	id = sh_database_entry (curr, -1);
-      else                            /* this is a generic server message */
-	id = sh_database_entry (curr, 0);
-    }
-
-  SH_FREE(curr);
-
-  SL_RETURN(id, _("sh_database_insert_rec"));
-}
-
 int sh_database_insert (char * message)
 {
   dbins * db_entry;
+  dbins * prev;
+  dbins * curr;
+  long    id = 0;
+#ifdef HOST_SWITCH
+  char  * temp[64];
+#endif
 
   SL_ENTER(_("sh_database_insert"));
@@ -1840,7 +1620,32 @@
   (void) sh_database_parse (message, db_entry);
 
-  /* recursively enter the linked list into the database
-   */
-  (void) sh_database_insert_rec (db_entry, 0, NULL);
+  /* Enter the list into the database. Actually, the list can only have
+   * two entries at most.
+   */
+  curr = db_entry;
+  if (curr->next)
+    {
+      prev = curr->next;
+#ifdef HOST_SWITCH
+      strncpy(temp, prev->host,       64);
+#endif
+      strncpy(prev->host, curr->host, 64);
+#ifdef HOST_SWITCH
+      strncpy(curr->host, temp,       64);
+#endif
+      id = sh_database_entry (prev, -1);
+      SH_FREE(prev);
+    }
+
+  if (id != 0)                       /* this is a server wrapper          */
+    {
+      if (enter_wrapper != 0)
+	(void) sh_database_entry (curr, id);
+    }
+  else                                /* this is a generic server message */
+    {
+      (void) sh_database_entry (curr, 0);
+    }
+  SH_FREE(curr);
 
   SL_RETURN(0, _("sh_database_insert"));
Index: trunk/src/sh_dbCheck.c
===================================================================
--- trunk/src/sh_dbCheck.c	(revision 591)
+++ 	(revision )
@@ -1,122 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 2015 Rainer Wichmann                                      */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-#include "config_xor.h"
-
-#include <string.h>
-
-#include "samhain.h"
-#include "sh_unix.h"
-#include "sh_utils.h"
-#include "sh_hash.h"
-#include "sh_files.h"
-#include "sh_tiger.h"
-
-#include "sh_dbIO.h"
-#include "sh_dbIO_int.h"
-#include "sh_pthread.h"
-
-#undef  FIL__
-#define FIL__  _("sh_dbCheck.c")
-
-#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE) 
-
-static void file_verify(sh_file_t * p)
-{
-  int reported = 0;
-  unsigned long check_flags;
-  char * dir_name;
-  char * file_name;
-
-  if (p->next != NULL)
-    file_verify(p->next);
-  if (p->fullpath == NULL || p->fullpath[0] != '/')
-    return;
-
-  check_flags = p->theFile.checkflags;
-
-  if (!MODI_INITIALIZED(check_flags)) {
-    MODI_SET(check_flags, MODI_INIT|MASK_READONLY_);
-    sh_tiger_get_mask_hashtype(&check_flags);
-  }
-
-  dir_name   = sh_util_dirname(p->fullpath);
-  file_name  = sh_util_basename(p->fullpath);
-
-  if (SH_FILE_UNKNOWN == sh_files_filecheck (SH_LEVEL_READONLY, check_flags,
-					     dir_name, file_name,
-					     &reported, 0))
-    ++sh.statistics.files_report;
-
-  SH_FREE(dir_name);
-  SH_FREE(file_name);
-  return;
-}
-
-static void dbCheck_setup()
-{
-  sh_hash_set_initialized();
-  sh.flag.isdaemon = S_FALSE; 
-  sh.flag.loop     = S_FALSE;
-  sh.flag.update   = S_FALSE;
-  sh.flag.checkSum = SH_CHECK_CHECK;
-  
-  sh.statistics.files_report  = 0;
-  ShDFLevel[SH_ERR_T_FILE]    = SH_ERR_SEVERE;
-  ShDFLevel[SH_ERR_T_RO]      = SH_ERR_SEVERE;
-  ShDFLevel[SH_ERR_T_NAME]    = SH_ERR_SEVERE;
-
-  return;
-}
-#include <stddef.h>
-int sh_dbCheck_verify (const char * db_file)
-{
-  unsigned int i;
-  sh_file_t ** mtab = get_default_data_table();
-  
-  sh_error_only_stderr (S_TRUE);
-  sh_error_setprint(_("none"));
-
-  /* for sh.effective.home in open_tmp() */
-  sh_unix_getUser (); 
-
-  if (sh_dbIO_load_db_file(mtab, db_file) < 0)
-    aud_exit (FIL__, __LINE__, EXIT_FAILURE);
-
-  dbCheck_setup();
-
-  /* Don't lock because:
-   * (a) we are single-treaded, thus it's not required
-   * (b) it will lead to deadlocking
-   */
-  for (i = 0; i < TABSIZE; ++i)
-    {
-      if (mtab[i] != NULL) 
-	file_verify(mtab[i]);
-    }
-  
-  sh_hash_unvisited (SH_ERR_INFO);
-  
-  if (0 != sh.statistics.files_report)
-    aud_exit (FIL__, __LINE__, EXIT_FAILURE);
-  aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
-  return 0;
-}
-
-#endif
Index: trunk/src/sh_dbCreate.c
===================================================================
--- trunk/src/sh_dbCreate.c	(revision 591)
+++ 	(revision )
@@ -1,227 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 2015 Rainer Wichmann                                      */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-#include "config_xor.h"
-
-#include <stdio.h>
-#include <string.h>
-#include <ctype.h>
-
-#include "samhain.h"
-#include "sh_utils.h"
-#include "sh_hash.h"
-#include "sh_files.h"
-
-#include "sh_dbIO.h"
-#include "sh_dbIO_int.h"
-#include "sh_pthread.h"
-#include "sh_guid.h"
-
-#undef  FIL__
-#define FIL__  _("sh_dbCreate.c")
-
-#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE) 
-
-static int dbCreate_writeout()
-{
-  char   uuid[SH_UUID_BUF];
-  char * path;
-  int    retval;
-
-  if (sh.outpath == NULL || sh.outpath[0] == '\0')
-    {
-      sh_uuid_generate_random(uuid, sizeof(uuid));
-      path = sh_util_strconcat(_("file."), sh.host.name, ".", uuid, NULL);
-    }
-  else
-    path = sh_util_strdup(sh.outpath);
-
-  retval = sh_dbIO_writeout_to_path(path);
-  SH_FREE(path);
-  return retval;
-}
-
-static void  dbCreate_run_filecheck(unsigned long add_mask, char * str)
-{
-  int status;
-
-  int reported = 0;
-  unsigned long check_flags = (MASK_READONLY_ | MODI_INIT | add_mask);
-  char * dir_name   = sh_util_dirname(str);
-  char * file_name  = sh_util_basename(str);
-
-  status = sh_files_filecheck (SH_LEVEL_READONLY, check_flags,
-			       dir_name, file_name, &reported, 0);
-
-  if (status == SH_FILE_UNKNOWN)
-    {
-      sh_hash_insert_null(str);
-    }
-
-  return;
-}
-
-static int dbCreate_filecheck(char * str)
-{
-  unsigned long add_mask = 0;
-
-  if (*str == '+')
-    {
-      add_mask = MODI_TXT;
-      ++str; while (isspace((int)*str)) ++str;
-    }
-  if (*str != '/')
-    {
-      char * tmp  = sh_util_safe_name (str);
-      sh_error_handle((-1), FIL__, __LINE__, EINVAL, MSG_E_SUBGPATH,
-		      _("Not an absolute path"), 
-		      _("dbCreate_filecheck"), tmp);
-      SH_FREE(tmp);
-      return -1;
-    }
-  dbCreate_run_filecheck(add_mask, str);
-  return 0;
-}
-
-char * rtrim(char * str)
-{
-  size_t len;
-
-  if (!str)
-    return str;
-
-  len = strlen(str);
-  while (len > 0)
-    {
-      --len;
-      if (str[len] == '\n' || str[len] == '\r')
-        str[len] = '\0';
-      else
-        break;
-    }
-    
-  return str;
-}
-
-static int dbCreate_loop(FILE * fd)
-{
-  int  status, retval = 0;
-  size_t linesize = MAX_PATH_STORE+2;
-  char * line = SH_ALLOC(linesize);
-
-  do {
-    status = sh_dbIO_getline(fd, line, linesize);
-
-    if (status > 0)
-      {
-	char * str = rtrim(line);   
-	while (isspace((int)*str)) ++str;
-	if (*str != '#')
-	  {
-	    int    fstatus = -1;
-	    size_t len     = 0;
-	    char * p       = sh_files_parse_input(str, &len);
-
-	    if (p)
-	      {
-		fstatus = dbCreate_filecheck(p);
-		SH_FREE(p);
-	      }
-	    if (fstatus != 0)
-	      retval = -1;
-	  }
-      }
-  } while (status != -1);
-
-  SH_FREE(line);
-  return retval;
-}
-
-static FILE * dbCreate_open (const char * path)
-{
-  FILE * fd = fopen(path, "r");
-  if (!fd)
-    {
-      int error = errno;
-      char * tmp  = sh_util_safe_name (path);
-      sh_error_handle((-1), FIL__, __LINE__, error, MSG_E_SUBGPATH,
-		      _("Cannot open file for read"), 
-		      _("dbCreate_open"), tmp);
-      SH_FREE(tmp);
-      aud_exit (FIL__, __LINE__, EXIT_FAILURE);
-    }
-  return fd;
-}
-
-static void dbCreate_setup()
-{
-  sh_hash_set_initialized();
-  sh.flag.isdaemon = S_FALSE; 
-  sh.flag.loop     = S_FALSE;
-  sh.flag.update   = S_FALSE;
-  sh.flag.checkSum = SH_CHECK_CHECK;
-  
-  sh.statistics.files_report  = 0;
-  ShDFLevel[SH_ERR_T_FILE]    = SH_ERR_SEVERE;
-  ShDFLevel[SH_ERR_T_RO]      = SH_ERR_SEVERE;
-  ShDFLevel[SH_ERR_T_NAME]    = SH_ERR_SEVERE;
-
-  sh_error_only_stderr (S_TRUE);
-  sh_error_setprint(_("none"));
-
-  return;
-}
-
-
-int sh_dbCreate (const char * path)
-{
-  FILE * fd;
-
-  /* Initialize application status
-   */
-  dbCreate_setup();
-
-  /* Open file list
-   */
-  fd = dbCreate_open(path);
-
-  /* Load the database
-   */
-  sh_hash_init_and_checksum();
-
-  /* Loop over file list to check files.
-   */
-  dbCreate_loop(fd);
-
-  /* Close file list
-   */
-  fclose(fd);
-
-  /* Write out database
-   */
-  if (0 != dbCreate_writeout())
-    aud_exit(FIL__, __LINE__, EXIT_FAILURE);
-
-  /* Exit on success.
-   */
-  aud_exit(FIL__, __LINE__, EXIT_SUCCESS);
-  return 0;
-}
-    
-#endif
Index: trunk/src/sh_dbIO.c
===================================================================
--- trunk/src/sh_dbIO.c	(revision 591)
+++ 	(revision )
@@ -1,1902 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 2015 Rainer Wichmann                                      */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-#include "config_xor.h"
-
-#include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-
-#include "samhain.h"
-#include "sh_utils.h"
-#include "sh_dbIO_int.h"
-#include "sh_hash.h"
-#include "sh_dbIO.h"
-#include "sh_sig.h"
-#include "sh_tiger.h"
-#include "sh_xfer.h"
-#include "sh_pthread.h"
-#include "sh_socket.h"
-#include "sh_files.h"
-
-#undef  FIL__
-#define FIL__  _("sh_dbIO.c")
-
-#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE) 
-
-/* external prototypes                                                     */
-
-extern int get_the_fd (SL_TICKET ticket);
-
-SH_MUTEX_EXTERN(mutex_hash);
-
-/******************************************************************
- *
- * Get a single line
- *
- ******************************************************************/
-static FILE * sh_fin_fd = NULL;
-
-int sh_dbIO_getline (FILE * fd, char * line, const size_t sizeofline)
-{
-  size_t  n = 0;
-
-  SL_REQUIRE(sizeofline >= SH_MINIBUF, _("sizeofline >= SH_MINIBUF"));
-
-  if (NULL != fgets(line, sizeofline, fd))
-    {
-      n = strlen(line);
-      if (n > 0 && line[n-1] == '\n') {
-	n--; line[n] = '\0';
-      }
-    } 
-  else {
-    line[0] = '\0';
-    return -1;
-  }
-
-  return n;
-}
-
-/******************************************************************
- *
- * Fast forward to start of data
- *
- ******************************************************************/
-
-static void reopen_fin_fd(SL_TICKET fd)
-{
-  if (sh_fin_fd != NULL)
-    {
-      sl_fclose (FIL__, __LINE__, sh_fin_fd);
-      sh_fin_fd = NULL;
-    }
-
-  sh_fin_fd = fdopen(dup(get_the_fd(fd)), "rb");
-  return;
-}
-
-
-static int seek_sof(FILE * fd, char * line, int size, const char * file)
-{
-  long i;
-
-  while (1) 
-    {
-      i =  sh_dbIO_getline (fd, line, size);
-      if (i < 0 ) 
-	{
-	  SH_FREE(line);
-	  dlog(1, FIL__, __LINE__, 
-	       _("The file signature database: %s does not\ncontain any data, or the start-of-file marker is missing (unlikely,\nunless modified by hand).\n"),
-	       (NULL == file) ? _("(null)") : file);
-	       
-	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_P_NODATA,
-			   ( (NULL == file) ? _("(null)") : file)
-			   );
-	  return -1;
-	}
-
-#if defined(SH_STEALTH)
-      if (0 == sl_strncmp (line, N_("[SOF]"), 5)) 
-#else
-      if (0 == sl_strncmp (line, _("[SOF]"),  5)) 
-#endif
-	break;
-    }
-  fflush(fd);
-  return 0;
-}
-
-static int sh_dbIO_setdataent (SL_TICKET fd, char * line, int size, 
-			       const char * file)
-{
-  int retval;
-
-  SL_ENTER(_("sh_dbIO_setdataent"));
-
-  sl_rewind (fd);
-  reopen_fin_fd(fd);
-
-  if (!sh_fin_fd)
-    {
-      dlog(1, FIL__, __LINE__, 
-	   _("The file signature database: %s is not readable.\n"),
-	   (NULL == file) ? _("(null)") : file);
-      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_P_NODATA,
-		       ( (NULL == file) ? _("(null)") : file)
-		       );
-      SL_RETURN( -1, _("sh_dbIO_setdataent"));
-    }
-
-  retval = seek_sof(sh_fin_fd, line, size, file);
-  SL_RETURN( retval, _("sh_dbIO_setdataent"));
-}
-
-/* Seek to [SOF] and truncate remainder of file
- */
-static int sh_dbIO_setdataent_old (SL_TICKET fd, char * line, int size, 
-				   const char * file)
-{
-  FILE * fdp;
-  
-  SL_ENTER(_("sh_dbIO_setdataent_old"));
-
-  sl_rewind (fd);
-  fdp = sl_stream(fd, "r+");
-  if (0 != seek_sof(fdp, line, size, file))
-    SL_RETURN( SL_EREAD, _("sh_dbIO_setdataent_old"));
-
-  lseek(fileno(fdp), ftello(fdp), SEEK_SET);
-
-  if (0 != ftruncate(fileno(fdp), ftello(fdp)))
-    {
-      char ebuf[SH_ERRBUF_SIZE];
-      int errnum = errno;
-      sh_error_message(errnum, ebuf, sizeof(ebuf));
-      sh_error_handle ((-1), FIL__, __LINE__, errnum, MSG_E_SUBGEN,
-		       ebuf, _("sh_dbIO_setdataent_old") );
-      SL_RETURN( SL_EWRITE, _("sh_dbIO_setdataent_old"));
-    }
-  SL_RETURN( 0, _("sh_dbIO_setdataent_old"));
-}
-
-/******************************************************************
- *
- * IO helper functions
- *
- ******************************************************************/
-
-
-static UINT32 * swap_32 (UINT32 * iptr)
-{
-#ifdef WORDS_BIGENDIAN
-  unsigned char swap;
-  unsigned char * ii = (unsigned char *) iptr;
-  swap = ii[0]; ii[0] = ii[3]; ii[3] = swap;
-  swap = ii[1]; ii[1] = ii[2]; ii[2] = swap;
-  return iptr;
-#else
-  return iptr;
-#endif
-}
-
-static UINT64 *  swap_64 (UINT64 * iptr)
-{
-#ifdef WORDS_BIGENDIAN
-#ifdef UINT64_IS_32
-  swap_32 ((UINT32*) iptr);
-#else
-  unsigned char swap;
-  unsigned char * ii = (unsigned char *) iptr;
-  swap = ii[0]; ii[0] = ii[7]; ii[7] = swap;
-  swap = ii[1]; ii[1] = ii[6]; ii[6] = swap;
-  swap = ii[2]; ii[2] = ii[5]; ii[5] = swap;
-  swap = ii[3]; ii[3] = ii[4]; ii[4] = swap;
-#endif
-  return iptr;
-#else
-  return iptr;
-#endif
-}
-
-static unsigned short *  swap_short (unsigned short * iptr)
-{
-#ifdef WORDS_BIGENDIAN
-  if (sizeof(short) == 4)
-    swap_32 ((UINT32*) iptr);
-  else
-    {
-      /* alignment problem */
-      static unsigned short ooop = *iptr;
-      unsigned short hi   = (ooop & 0xff00);
-      unsigned short lo   = (ooop & 0xff);
-      ooop = (lo << 8) | (hi >> 8);
-      return &ooop;
-    }
-  return iptr;
-#else
-  return iptr;
-#endif
-}
-
-static void swap_data(sh_filestore_t * ft)
-{
-  swap_32(&(ft->mode));
-  swap_32(&(ft->linkmode));
-  swap_64(&(ft->dev));
-  swap_64(&(ft->rdev));
-  swap_32(&(ft->hardlinks));
-  swap_32(&(ft->ino));
-  swap_64(&(ft->size));
-  swap_64(&(ft->atime));
-  swap_64(&(ft->mtime));
-  swap_64(&(ft->ctime));
-  swap_32(&(ft->owner));
-  swap_32(&(ft->group));
-  swap_32(&(ft->checkflags));
-#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
-  swap_32(&(ft->attributes));
-#endif
-  ft->mark = *(swap_short(&(ft->mark)));
-  return;
-}
-
-#define QUOTE_CHAR '='
-
-char * unquote_string (const char * str, size_t len)
-{
-  int    i = 0, t1, t2;
-  char * tmp = NULL;
-  size_t l2, j, k = 0;
-
-  SL_ENTER(_("unquote_string"));
-
-  if (str != NULL)
-    {
-      l2  = len - 2;
-      tmp = SH_ALLOC(len + 1);
-
-      for (j = 0; j <= len; ++j)
-	{
-	  if (str[j] != QUOTE_CHAR)
-	    {
-	      tmp[k] = str[j];
-	    }
-	  else if (str[j] == QUOTE_CHAR && j < l2)
-	    {
-	      t1 = sh_util_hexchar(str[j+1]);
-	      t2 = sh_util_hexchar(str[j+2]);
-	      if ((t1|t2) >= 0)
-		{
-		  i = 16 * t1 + t2;
-		  tmp[k] = i; 
-		  j += 2;
-		}
-	      else
-		{
-		  tmp[k] = str[j];
-		}
-	    }
-	  else
-	    tmp[k] = str[j];
-	  ++k;
-	}
-    }
-  SL_RETURN(tmp, _("unquote_string"));
-}
-
-static char * int2hex (unsigned char i, char * i2h)
-{
-  static char hexchars[] = "0123456789ABCDEF";
-
-  i2h[0] = hexchars[(((i) & 0xF0) >> 4)]; /* high */
-  i2h[1] = hexchars[((i) & 0x0F)];        /* low  */
-
-  return i2h;
-}
-
-char * quote_string (const char * str, size_t len)
-{
-  char * tmp;
-  char * tmp2;
-  size_t l2, j, i = 0, k = 0;
-  char   i2h[2];
-
-  SL_ENTER(_("quote_string"));
-
-  if (str == NULL)
-    {
-      SL_RETURN(NULL, _("quote_string"));
-    }
-
-  for (j = 0; j < len; ++j)
-    if (str[j] == '\n' || str[j] == QUOTE_CHAR) ++i;
-
-  l2 = len + 1;
-  if (sl_ok_muls(3, i) && sl_ok_adds(l2, (3*i)))
-    {
-      tmp = SH_ALLOC(len + 1 + 3*i);
-    }
-  else
-    {
-      sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
-		      _("integer overflow"), 
-		      _("quote_string"));
-      SL_RETURN(NULL, _("quote_string"));
-    }
-
-  for (j = 0; j <= len; ++j)
-    {
-      if (str[j] == '\n')
-	{
-	  tmp2 = int2hex((unsigned char) '\n', i2h);
-	  tmp[k] = QUOTE_CHAR; ++k;
-	  tmp[k] = tmp2[0];    ++k;
-	  tmp[k] = tmp2[1];
-	}
-      else if (str[j] == QUOTE_CHAR)
-	{
-	  tmp2 = int2hex((unsigned char) QUOTE_CHAR, i2h);
-	  tmp[k] = QUOTE_CHAR; ++k;
-	  tmp[k] = tmp2[0];    ++k;
-	  tmp[k] = tmp2[1];
-	}
-      else
-	{
-	  tmp[k] = str[j];
-	}
-      ++k;
-    }
-  SL_RETURN(tmp, _("quote_string"));
-}
-
-static char * unquote_path(char * line, long i)
-{
-  char * tmp  = unquote_string (line, i);
-  size_t len  = sl_strlen(tmp)+1;
-  char * path = SH_ALLOC(len);
-
-  (void) sl_strlcpy (path, tmp, len);
-  if (tmp)
-    SH_FREE(tmp);
-  /* do not strip newline twice...
-  if (len > 1) {
-    if (path[len-2] == '\n')
-      path[len-2] = '\0';
-  }
-  ****/
-  return path;
-}
-
-/******************************************************************
- *
- * Use different init rootfs (patch by Kemal H.)
- *
- ******************************************************************/
-
-static char * sh_dbIO_rootfs = NULL;
-static size_t sh_dbIO_rootfs_len = 0;
-
-int sh_dbIO_init_rootfs (const char * rootfs)
-{
-  if (NULL == sh_dbIO_rootfs)
-    {
-      sh_dbIO_rootfs     = sh_util_strdup (rootfs);
-      sh_dbIO_rootfs_len = sl_strlen(sh_dbIO_rootfs);
-      return 0;
-    }
-  return -1;
-}
-
-size_t sh_dbIO_get_rootfs_len()
-{
-  return sh_dbIO_rootfs_len;
-}
-
-/* Prepend rootfs when reading from config file ('path' must be allocated with sufficient space).
- */
-char * sh_dbIO_rootfs_prepend(char * path)
-{
-  if (0 == sh_dbIO_rootfs_len)
-    return path;
-  
-  memmove (path + sh_dbIO_rootfs_len, path, sl_strlen(path) + 1);
-  memcpy  (path, sh_dbIO_rootfs, sh_dbIO_rootfs_len);
-
-  return path;
-}
-
-
-/* Strip rootfs when writing to database file.
- */
-char * sh_dbIO_rootfs_strip(char * path)
-{
-  if (sh_dbIO_rootfs_len == 0)
-    {
-      return path;
-    }
-  else
-    {
-      size_t len = sl_strlen(path);
-
-      memmove (path, path + sh_dbIO_rootfs_len, len + 1 - sh_dbIO_rootfs_len);
-      if(path[0] != '/')
-	{
-	  path[0]='/';
-	  path[1]='\0';
-	}
-    }
-
-  return path;
-}
-
-char * sh_dbIO_rootfs_strip_link(char * path)
-{
-  if (sh_dbIO_rootfs_len == 0)
-    return path;
-  if (strstr(path, sh_dbIO_rootfs) == path)
-    {
-      size_t len = sl_strlen(path);
-
-      memmove (path, path + sh_dbIO_rootfs_len, len + 1 - sh_dbIO_rootfs_len);
-    }
-  return path;
-}
-
-/******************************************************************
- *
- * Read next record and return it
- *
- ******************************************************************/
-
-static void corrupt_record(char * file, int line, const char * filepath)
-{
-  dlog(1, file, line, 
-       _("There is a corrupt record in the file signature database: %s\n"),
-       (NULL == filepath)? _("(null)") : filepath);
-  sh_error_handle ((-1), file, line, 0, MSG_E_SUBGPATH,
-		   _("Corrupt record in file signature database"), 
-		   _("sh_dbIO_getdataent"),
-		   ( (NULL == filepath) ? _("(null)") : filepath) );
-  return;
-}
-
-static void wrong_version(char * file, int line, const char * filepath)
-{
-  dlog(1, file, line, 
-       _("There is a record with a bad version number in the file signature database: %s\nThis may be caused by using '-t init' repeatedly to initialise the database, without (re)moving the database file.\n"),
-       (NULL == filepath) ? _("(null)") : filepath);
-  sh_error_handle((-1), file, line, 0, MSG_E_SUBGPATH,
-		  _("Record with bad version number in file signature database"), 
-		  _("sh_dbIO_getdataent"),
-		  (NULL == filepath) ? _("(null)") : filepath);
-  return;
-}
-
-static void hexdump(unsigned char * data, size_t size)
-{
-  unsigned int count =0;
-  char ith[3];
-
-  do {
-    int2hex(data[count], ith); ith[2] = '\0';
-    printf("%2s", ith);
-    ++count;
-    if (count % 40 == 0) putc('\n', stdout);
-  } while (count < size);
-}
-
-static size_t dbIO_fread_struct (sh_filestore_t * ptr, FILE *stream, 
-				 const char * path, int * errflag)
-{
-  sh_filestore_old_t old_struct;
-  fpos_t position;
-  static int oldflag = -1;
-
- start:
-  if (oldflag != -1) /* 'initialized' case first */
-    {
-      if (oldflag == 0)
-	return fread (ptr, sizeof(sh_filestore_t), 1, stream);
-
-      else
-	{
-	  unsigned short mark;
-	  if (1 != fread (&old_struct, sizeof(old_struct), 1, stream))
-	    return 0;
-
-	  /* set mark to current version */
-	  mark = old_struct.mark;
-	  mark = *(swap_short(&(mark)));
-	  if ((mark & ~REC_FLAGS_MASK) != OLD_REC_MAGIC)
-	    {
-	      sh_filestore_old_t try_struct;
-	      char try[5];
-
-	      if (1 == 0)
-		hexdump((unsigned char *)&old_struct, sizeof(old_struct));
-	      memset(&try_struct, 0, sizeof(try_struct));
-	      if (!memcmp(&old_struct, &try_struct, sizeof(try_struct)))
-		return 0; /* NULL read */
-	      if (1 != fread (try, sizeof(try), 1, stream))
-		return 0;
-	      if (feof(stream))
-		return 0;
-
-	      wrong_version(FIL__, __LINE__, path);
-	      *errflag = -1;
-	      return 0;
-	    }
-	  if ((mark & REC_FLAGS_ATTR) != 0)
-	    mark = REC_MAGIC|REC_FLAGS_ATTR;
-	  else
-	    mark = REC_MAGIC;
-	  mark = *(swap_short(&(mark)));
-	  old_struct.mark = mark;
-
-	  /* copy into current struct version */
-	  memcpy(ptr, &old_struct, sizeof(old_struct));
-	  ptr->checkflags = 0;
-	  return 1;
-	}
-    }
-  else /* not initialized yet, test DB version */
-    {
-      if (0 == fgetpos(stream, &position))
-	{
-	  unsigned short mark;
-
-	  if (1 != fread (&old_struct, sizeof(old_struct), 1, stream))
-	    return 0;
-
-	  mark = old_struct.mark;
-	  mark = *(swap_short(&(mark)));
-	  if ((mark & ~REC_FLAGS_MASK) == REC_MAGIC)
-	    oldflag = 0;
-	  else if ((mark & ~REC_FLAGS_MASK) == OLD_REC_MAGIC)
-	    oldflag = 1;
-	  else
-	    {
-	      wrong_version(FIL__, __LINE__, path);
-	      *errflag = -1;
-	      return 0;
-	    }
-
-	  /* return to previous position and read data */
-	  if (0 != fsetpos(stream, &position))
-	    return 0;
-	  goto start;
-	}
-      return 0;
-    }
-}
-
-int sig_end_detected (void * ft)
-{
-  char * str = (char *) ft;
-  char cmp[SH_MINIBUF];
-
-  sl_strlcpy(cmp, _("-----BEGIN PGP SIGNATURE-----"), sizeof(cmp));
-
-  if ( 0 == memcmp(str, cmp, strlen(cmp)) ) 
-    return S_TRUE;
-  return S_FALSE;
-}
-
-static sh_file_t *  sh_dbIO_getdataent (char * line, int size, 
-					const char * filepath, int * errflag)
-{
-  sh_file_t * p;
-  sh_filestore_t ft;
-  long i;
-  char * fullpath;
-  char * linkpath;
-  char * attr_string = NULL;
-
-  SL_ENTER(_("sh_dbIO_getdataent"));
-
-  *errflag = 0;
-
-  p = SH_ALLOC(sizeof(sh_file_t));
-
-  /* Read next record -- Part One 
-   */
-  if (1 != dbIO_fread_struct (&ft, sh_fin_fd, filepath, errflag))
-    {
-      SH_FREE(p);
-      SL_RETURN( NULL, _("sh_dbIO_getdataent"));
-    }
-
-  ft.mark = *(swap_short(&(ft.mark)));
-
-  if ((ft.mark & ~REC_FLAGS_MASK) != REC_MAGIC)
-    {
-      if (sig_end_detected(&ft))
-	{
-	  SH_FREE(p);
-	  SL_RETURN( NULL, _("sh_dbIO_getdataent"));
-	}
-      SH_FREE(p);
-      wrong_version(FIL__, __LINE__, filepath);
-      *errflag = -1;
-      SL_RETURN( NULL, _("sh_dbIO_getdataent"));
-    }
-
-  ft.mark = *(swap_short(&(ft.mark)));
-  swap_data(&ft);
-
-  /* Read next record -- Part Two -- Fullpath
-   */
-  i = sh_dbIO_getline (sh_fin_fd, line, size);
-
-  if (i <= 0 ) 
-    {
-      SH_FREE(p);
-      corrupt_record(FIL__, __LINE__, filepath);
-      *errflag = -1;
-      SL_RETURN( NULL, _("sh_dbIO_getdataent"));
-    }
-
-  fullpath = unquote_path(line, i);
-  
-  /* Read next record -- Part Three -- Linkpath
-   */
-  i =  sh_dbIO_getline (sh_fin_fd, line, size);
-
-  if (i <= 0 ) 
-    {
-      SH_FREE(fullpath); SH_FREE(p);
-      corrupt_record(FIL__, __LINE__, filepath);
-      *errflag = -1;
-      SL_RETURN( NULL, _("sh_dbIO_getdataent"));
-    }
-
-  linkpath = unquote_path(line, i);
-
-  /* Read next record -- Part Four -- attr_string
-   */
-  if ((ft.mark & REC_FLAGS_ATTR) != 0)
-    {
-      i =  sh_dbIO_getline (sh_fin_fd, line, size);
-      if (i <= 0 ) 
-	{
-	  SH_FREE(fullpath); SH_FREE(linkpath); SH_FREE(p);
-	  corrupt_record(FIL__, __LINE__, filepath);
-	  *errflag = -1;
-	  SL_RETURN( NULL, _("sh_dbIO_getdataent"));
-	}
-
-      attr_string = unquote_path(line, i);
-    }
-
-  /* Read next record -- Part Four -- Decode
-   */
-#if defined(SH_STEALTH)
-  sh_do_decode(fullpath,    sl_strlen(fullpath));
-#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
-  sh_do_decode(ft.c_attributes,   sl_strlen(ft.c_attributes));
-#endif
-  sh_do_decode(ft.c_mode,   sl_strlen(ft.c_mode));
-  sh_do_decode(ft.c_owner,  sl_strlen(ft.c_owner));
-  sh_do_decode(ft.c_group,  sl_strlen(ft.c_group));
-  sh_do_decode(ft.checksum, sl_strlen(ft.checksum));  
-  /* 
-   * TXT entries are c_mode[0] != 'l' and do not get decoded 
-   */
-  if (ft.c_mode[0] == 'l' && linkpath[0] != '-')
-    {  
-      sh_do_decode(linkpath, sl_strlen(linkpath));
-    }
-  if ((ft.mark & REC_FLAGS_ATTR) != 0)
-    {  
-      sh_do_decode(attr_string, sl_strlen(attr_string));
-    }
-#endif
-
-  memcpy( &(*p).theFile, &ft, sizeof(sh_filestore_t) );
-
-  /* init fflags, such that suid files in 
-   * database are recognized as such 
-   */
-  {
-    mode_t mode = (mode_t) ft.mode;
-
-    if (S_ISREG(mode) &&
-	(0 !=(S_ISUID & mode) ||
-#if defined(HOST_IS_LINUX)
-	 (0 !=(S_ISGID & mode) && 
-	  0 !=(S_IXGRP & mode)) 
-#else  
-	 0 !=(S_ISGID & mode)
-#endif
-	 )
-	)
-      p->fflags = SH_FFLAG_SUIDCHK;
-
-    else
-      p->fflags = 0;
-  }
-
-  p->modi_mask   = ft.checkflags;
-  if (MODI_ISSET(ft.checkflags, MODI_ALLIGNORE))
-    SET_SH_FFLAG_ALLIGNORE(p->fflags);
-  p->fullpath    = fullpath;
-  p->linkpath    = linkpath;
-  p->attr_string = attr_string;
-
-  /* set to an invalid value 
-   */
-  ft.mark = (REC_MAGIC + 5);
-
-  SL_REQUIRE((*errflag == 0), _("errflag not set correctly"));
-  SL_RETURN( p, _("sh_dbIO_getdataent"));
-}
-
-/******************************************************************
- *
- * Data loading routines
- *
- ******************************************************************/
-static SL_TICKET load_data_from_server(const char * uuid)
-{
-  SL_TICKET fd = -1;
-
-#if defined(SH_WITH_CLIENT)
-  char hashbuf[KEYBUF_SIZE];
-
-  /* Data file from Server
-   */
-  if (0 != sl_strcmp(file_path('D', 'R'), _("REQ_FROM_SERVER")))
-    return -1;
-
-  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_D_DSTART);
-  fd = sh_xfer_request_file((!uuid) ? _("DATA") : uuid);
-
-  if (SL_ISERROR(fd))
-    {
-      if (!uuid)
-	{
-	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FBAD);
-	  dlog(1, FIL__, __LINE__, 
-	       _("Could not retrieve the file signature database from the server(errnum = %ld).\nPossible reasons include:\n - the server is not running,\n - session key negotiation failed (see the manual for proper setup), or\n - the server cannot access the file.\n"), fd);  
-	}
-      else
-	sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_TCP_FBAD);
-      return fd;
-    }
-  sl_rewind (fd);
-
-  if (!uuid)
-    {
-      sl_strlcpy (sh.data.hash, 
-		  sh_tiger_hash (file_path('D', 'R'),  
-				 fd, TIGER_NOLIM, hashbuf, sizeof(hashbuf)),
-		  KEY_LEN+1);
-      sl_rewind (fd);
-    }
-#else
-  (void) uuid;
-#endif
-  return fd;
-}
-
-static SL_TICKET load_data_from_disk(const char * filepath)
-{
-  char hashbuf[KEYBUF_SIZE];
-  SL_TICKET fd = -1;
-
-  /* Local data file
-   */
-  if ( SL_ISERROR(fd = sl_open_read(FIL__, __LINE__, filepath, SL_YESPRIV)) )
-    {
-      TPT(( 0, FIL__, __LINE__, _("msg=<Error opening: %s>\n"), filepath));
-      dlog(1, FIL__, __LINE__, 
-    _("Could not open the local file signature database for reading because\nof the following error: %s (errnum = %ld)\nIf this is a permission problem, you need to change file permissions\nto make the file readable for the effective UID: %d\n"), 
-	   sl_get_errmsg(), fd, (int) sl_ret_euid());
-      sh_error_handle ((-1), FIL__, __LINE__, fd, MSG_EXIT_ABORT1, 
-		       sh.prg_name);
-      return -1;
-    }
-  
-  TPT(( 0, FIL__, __LINE__, _("msg=<Opened database: %s>\n"), 
-	filepath));
-
-  if (sh.data.hash[0] == '\0')
-    {
-      char hashbuf[KEYBUF_SIZE];
-      sl_strlcpy(sh.data.hash, 
-		 sh_tiger_hash (filepath, TIGER_FILE, TIGER_NOLIM, hashbuf, sizeof(hashbuf)), 
-		 KEY_LEN+1);
-    }
-  else
-    {
-      if (0 != sl_strncmp(sh.data.hash, 
-			  sh_tiger_hash (filepath, fd, TIGER_NOLIM, 
-					 hashbuf, sizeof(hashbuf)),
-			  KEY_LEN)
-	  && sh.flag.checkSum != SH_CHECK_INIT) 
-	{
-	  dlog(1, FIL__, __LINE__, 
-	       _("The checksum of the file signature database has changed since startup: %s -> %s\n"),
-	       sh.data.hash, sh_tiger_hash (filepath, fd, TIGER_NOLIM, 
-					    hashbuf, sizeof(hashbuf)));
-	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_AUTH,
-			   ( (NULL == filepath) ? _("(null)") :
-			     filepath )
-			   );
-	}
-    }
-  sl_rewind (fd);
-  return fd;
-}
-
-static SL_TICKET verify_data (SL_TICKET fd)
-{
-#if defined(WITH_SIG)
-  SL_TICKET fdTmp;
-
-  /* extract the data and copy to temporary file
-   */
-  fdTmp = sh_sig_extract_signed(fd);
-
-  if (sig_termfast == 1)  /* SIGTERM */
-    {
-      TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
-      --sig_raised; --sig_urgent;
-      return -1;
-    }
-
-  sl_close(fd);
-  fd = fdTmp;
-
-  /* Validate signature of open file.
-   */
-  if (0 != sh_sig_check_signature (fd, SIG_DATA))
-    {
-      sl_close(fd);
-      return -1;
-    }
-  sl_rewind (fd);
-
-  fdTmp = sh_sig_extract_signed_data(fd);
-  sl_close(fd);
-  fd = fdTmp;  
-#endif
-
-  return fd;
-}
-
-static int read_data(SL_TICKET fd, sh_file_t * tab[TABSIZE], 
-		     const char * filepath)
-{
-  sh_file_t * p;
-  int errflag = 0;
-  char * line = SH_ALLOC(MAX_PATH_STORE+2);
-
-  /* fast forward to start of data
-   */
-  if (0 != sh_dbIO_setdataent(fd, line, MAX_PATH_STORE+1, filepath))
-    return -1;
-
-  while (1) 
-    {
-      if (sig_termfast == 1)  /* SIGTERM */
-	{
-	  TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
-	  --sig_raised; --sig_urgent;
-	  SH_FREE(line);
-	  return -1;
-	}
-
-      p = sh_dbIO_getdataent (line, MAX_PATH_STORE+1, filepath, &errflag);
-      if (p != NULL)
-	{
-	  if (!sh_hash_is_null_record(&(p->theFile)))
-	    hashinsert (tab, p);
-	  else
-	    sh_hash_remove_unconditional (p->fullpath);
-	}
-      else
-	break;
-    }
-
-  if (line != NULL)
-    SH_FREE(line);
-
-  /* Always keep db in memory, so we have no open file
-   */
-  sl_close (fd);
-
-  sl_fclose (FIL__, __LINE__, sh_fin_fd);
-  sh_fin_fd = NULL;
-
-  return errflag;
-}
-
-
-static int sh_dbIO_load_db_int(sh_file_t * tab[TABSIZE], 
-			       const char * filepath, const char * uuid)
-{
-#define FGETS_BUF 16384
-
-  SL_TICKET fd = -1;
-
-  if (uuid)
-    {
-      fd = load_data_from_server(uuid);
-      if (SL_ISERROR(fd))
-	return -1;
-    }
-  else if (!filepath)
-    {
-      char * dbpath = file_path('D', 'R');
-
-      fd = load_data_from_server(NULL);
-
-      if (SL_ISERROR(fd))
-	{
-	  if (*dbpath == '/')
-	    fd = load_data_from_disk(dbpath);
-	}
-    }
-  else
-    {
-      fd = load_data_from_disk(filepath);
-    }
-
-  if (SL_ISERROR(fd))
-    return -1;
-
-  if (sig_termfast == 1)  /* SIGTERM */
-    {
-      TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
-      --sig_raised; --sig_urgent;
-      aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
-    }
-
-  fd = verify_data(fd);
-  if (SL_ISERROR(fd))
-    return -1;
-
-  if (!uuid) { int i; for (i = 0; i < TABSIZE; ++i) tab[i] = NULL; }
-
-  return read_data (fd, tab, filepath);
-}
-
-
-int sh_dbIO_load_db(sh_file_t * tab[TABSIZE])
-{
-  return sh_dbIO_load_db_int(tab, NULL, NULL);
-}
-int sh_dbIO_load_db_file(sh_file_t * tab[TABSIZE], const char * filepath)
-{
-  return sh_dbIO_load_db_int(tab, filepath, NULL);
-}
-
-int sh_dbIO_load_delta()
-{
-  int    status = 0;
-#if defined(SH_WITH_CLIENT)
-  sh_file_t ** mtab = get_default_data_table();
-  int errflag = 0;
-  unsigned int count;
-  time_t last;
-
-  if ( sh.flag.checkSum != SH_CHECK_INIT )
-    {
-      if (sh_hash_get_initialized() != 0)
-	{
-	  char * uuid = sh_socket_get_uuid(&errflag, &count, &last);
-
-	  if (!uuid) 
-	    return errflag;
-
-	  if (count > 0)
-	    sh_error_handle(SH_ERR_NOTICE, FIL__, __LINE__, count, MSG_E_SUBGEN,
-			  _("Retrying download of delta DB"), 
-			  _("sh_dbIO_load_delta"));
-
-	  status = sh_dbIO_load_db_int(mtab, NULL, uuid);
-	  if (status < 0)
-	    {
-	      /* Return status < 0 indicates that max_try is exceeded
-	       */
-	      if (sh_socket_return_uuid(uuid, count, last) < 0)
-		sh_error_handle((-1), FIL__, __LINE__, -1, MSG_D_DELTAFAIL, uuid);
-	    }
-	  else
-	    {
-	      sh_error_handle((-1), FIL__, __LINE__, -1, MSG_D_DELTAOK, uuid);
-	    }
-	  SH_FREE(uuid);
-	}
-      else
-	{
-	  /* not initialized yet */
-	  sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, -1, MSG_E_SUBGEN,
-			  _("Download of delta DB skipped, not initialized yet"), 
-			  _("sh_dbIO_load_delta"));
-	  return -1;
-	}
-    }
-#endif
-  return status;
-}
-
-/******************************************************************
- *
- * Writing out a file to the database.
- *
- ******************************************************************/ 
-static int       pushdata_isfirst =  1;
-static SL_TICKET pushdata_fd      = -1;
-
-static int       pushdata_stdout  =  S_FALSE;
-
-static char * sh_db_version_string = NULL;
-
-int sh_dbIO_writeout_stdout (const char * str)
-{
-  if (!str)
-    { pushdata_stdout  =  S_TRUE; return 0; }
-  return -1;
-}
-
-int sh_dbIO_version_string(const char * str)
-{
-  if (str)
-    {
-      if (sh_db_version_string != NULL) {
-	SH_FREE(sh_db_version_string);
-      }
-      if (0 == sl_strncmp(str, _("NULL"), 4))
-	{
-	  sh_db_version_string = NULL;
-	  return 0;
-	}
-      sh_db_version_string = sh_util_strdup(str);
-      return 0;
-    }
-  return -1;
-}
-
-void do_writeout_checks(const char * outpath)
-{
-  if ((pushdata_stdout == S_TRUE) && (sh.flag.update == S_TRUE))
-    {
-      dlog(1, FIL__, __LINE__, 
-	   _("You cannot write the database to stdout when you use update rather than init.\n"));
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORTS,
-		      _("Writing database to stdout with update"), 
-		      sh.prg_name, 
-		      _("sh_dbIO_data_write_int"));
-      aud_exit(FIL__, __LINE__, EXIT_FAILURE);
-    }
-
-  if ((pushdata_stdout == S_TRUE) && (sl_is_suid()))
-    {
-      dlog(1, FIL__, __LINE__, 
-	   _("You cannot write the database to stdout when running with suid privileges.\n"));
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORTS,
-		      _("Writing database to stdout when suid"), 
-		      sh.prg_name, 
-		      _("sh_dbIO_data_write_int"));
-      aud_exit(FIL__, __LINE__, EXIT_FAILURE);
-    }
-
-
-  if ( (pushdata_isfirst == 1) && (pushdata_stdout == S_FALSE) && 
-       ( (NULL == outpath) || (0 == sl_strcmp(outpath, _("REQ_FROM_SERVER"))) ) )
-    {
-      dlog(1, FIL__, __LINE__, 
-	   _("You need to configure a local path for initializing the database\nlike ./configure --with-data-file=REQ_FROM_SERVER/some/local/path\n"));
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORTS,
-		      _("No local path for database specified"), 
-		      sh.prg_name, 
-		      _("sh_dbIO_data_write_int"));
-      aud_exit(FIL__, __LINE__, EXIT_FAILURE);
-    }
-
-  if ((pushdata_isfirst == 1) && (pushdata_stdout == S_FALSE))  
-    {
-      /* Warn that file already exists; file_path != NULL here because
-       * checked above
-       */
-      struct stat sbuf;
-
-      if (0 == retry_lstat(FIL__, __LINE__, outpath, &sbuf))
-	{
-	  if (sh.flag.update == S_FALSE)
-	    {
-	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_FI_DBEX,
-			      file_path('D', 'W'));
-	    }
-	}
-    }
-
-  return;
-}
-
-static SL_TICKET open_writeout_data_truncate(const char * path)
-{
-  int status;
-  SL_TICKET fd;
-
-  if ( SL_ISERROR(fd = sl_open_rdwr_trunc(FIL__, __LINE__, path, SL_YESPRIV))) 
-    {
-      sh_error_handle((-1), FIL__, __LINE__, fd, MSG_E_ACCESS,
-		      geteuid(), path);
-      aud_exit(FIL__, __LINE__, EXIT_FAILURE);
-    }
-
-  if (SL_ISERROR(status = sl_lock (fd)))
-    {
-      sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGPATH,
-		      _("Failed to lock baseline database"), _("sh_dbIO_data_write_int"),
-		      path);
-      aud_exit(FIL__, __LINE__, EXIT_FAILURE);
-    }
-  return fd;
-}
-
-static SL_TICKET open_writeout_data(const char * path)
-{
-  int status;
-  SL_TICKET fd;
-
-  if ( SL_ISERROR(fd = sl_open_rdwr(FIL__, __LINE__, path, SL_YESPRIV))) 
-    {
-      sh_error_handle((-1), FIL__, __LINE__, fd, MSG_E_ACCESS,
-		      geteuid(), path);
-      aud_exit(FIL__, __LINE__, EXIT_FAILURE);
-    }
-
-  if (SL_ISERROR(status = sl_lock (fd)))
-    {
-      sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGPATH,
-		      _("Failed to lock baseline database"), _("sh_dbIO_data_write_int"),
-		      path);
-      aud_exit(FIL__, __LINE__, EXIT_FAILURE);
-    }
-  return fd;
-}
-
-static void seek_writeout_data(SL_TICKET fd, const char * path)
-{
-  int status;
-
-  if ( SL_ISERROR(status = sl_forward(fd))) 
-    {
-      sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGPATH,
-		      _("Failed to seek to end of baseline database"),
-		      _("seek_writeout_data"),
-		      path);
-      aud_exit(FIL__, __LINE__, EXIT_FAILURE);
-    }
-  return;
-}
-
-/* Seek to [SOF] and truncate remainder
- */
-static int seek_writeout_data_old(SL_TICKET fd, const char * path)
-{
-  char * line = SH_ALLOC(MAX_PATH_STORE+1);
-
-  /* This will do an ftruncate() after the sof marker
-   */
-  if (SL_ISERROR(sh_dbIO_setdataent_old (fd, line, MAX_PATH_STORE, path)))
-    {
-      SH_FREE(line);
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGPATH,
-		      _("Failed to seek to start of baseline database"),
-		      _("seek_writeout_data_old"),
-		      path);
-      aud_exit(FIL__, __LINE__, EXIT_FAILURE);
-    }
-  SH_FREE(line);
-  return 0;
-}
-
-char * prep_path(char * path, int flag)
-{
-  size_t old_len = sl_strlen(path);
-  char * tmp;
-  size_t tmp_len;
-  size_t path_len;
-  char * outpath = NULL;
-#if !defined(SH_STEALTH)
-  (void) flag;
-#endif
-
-#if defined(SH_STEALTH)
-  if (flag == S_TRUE)
-    sh_do_encode(path, old_len);
-#endif
-  tmp = quote_string(path, old_len);
-  tmp_len = sl_strlen(tmp);
-#if defined(SH_STEALTH)
-  if (flag == S_TRUE)
-    sh_do_decode(path, old_len);
-#endif
-
-  if (tmp && tmp_len <= MAX_PATH_STORE) 
-    {
-      outpath = sh_util_strdup(path);
-    } 
-  else 
-    {
-      char hashbuf[KEYBUF_SIZE];
-      
-      outpath = sh_util_strdup(sh_tiger_hash (path,
-					      TIGER_DATA, old_len, 
-					      hashbuf, sizeof(hashbuf)));
-    }
-  if (tmp) 
-    SH_FREE(tmp);
-
-  path_len = sl_strlen(outpath);
-#if defined(SH_STEALTH)
-  if (flag == S_TRUE)
-    sh_do_encode(outpath, path_len);
-#endif
-  
-  tmp = quote_string(outpath, path_len);
-  if (tmp) {
-    SH_FREE(outpath);
-    outpath = tmp;
-  }
-  return outpath;
-}
-
-static char * prep_attr(char * attr_str)
-{
-  char * tmp;
-  char * outstr = NULL;
-  size_t old_len = sl_strlen(attr_str);
-
-#if defined(SH_STEALTH)
-  sh_do_encode(attr_str, old_len);
-#endif
-
-  tmp = quote_string(attr_str, old_len);
-  if (tmp)
-    {
-      outstr = tmp;
-    }
-
-#if defined(SH_STEALTH)
-  sh_do_decode(attr_str, old_len);
-#endif
-  return outstr;
-}
-
-static void prep_encode(sh_filestore_t * p)
-{
-#if defined(SH_STEALTH)
-  sh_do_encode(p->c_mode,   sl_strlen(p->c_mode));
-  sh_do_encode(p->c_owner,  sl_strlen(p->c_owner));
-  sh_do_encode(p->c_group,  sl_strlen(p->c_group));
-  sh_do_encode(p->checksum, sl_strlen(p->checksum));
-  sh_do_encode(p->c_attributes,   sl_strlen(p->c_attributes));
-#else
-  (void) p;
-#endif
-  return;
-}
-
-static void prep_struct(sh_filestore_t * p, file_type * buf, char * fileHash)
-{
-#if !defined(__linux__) && !defined(HAVE_STAT_FLAGS)
-  int    i;
-#endif
-  p->mark = REC_MAGIC;
-  sl_strlcpy(p->c_mode,   buf->c_mode,   CMODE_SIZE);
-  sl_strlcpy(p->c_group,  buf->c_group,  GROUP_MAX+1);
-  sl_strlcpy(p->c_owner,  buf->c_owner,  USER_MAX+1);
-  if (fileHash) {
-    sl_strlcpy(p->checksum, fileHash,      KEY_LEN+1);
-  }
-#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
-  sl_strlcpy(p->c_attributes, buf->c_attributes, ATTRBUF_SIZE);
-#else
-  for (i = 0; i < ATTRBUF_USED; ++i) p->c_attributes[i] = '-';
-  p->c_attributes[ATTRBUF_USED] = '\0';
-#endif
- 
-  prep_encode(p);
-  
-#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
-  p->attributes  = (UINT32) buf->attributes;
-#else
-  p->attributes  = 0;
-#endif
-  p->linkmode    = (UINT32) buf->linkmode;
-  p->hardlinks   = (UINT32) buf->hardlinks;
-  p->dev   = (UINT64) buf->dev;
-  p->rdev  = (UINT64) buf->rdev;
-  p->mode  = (UINT32) buf->mode;
-  p->ino   = (UINT32) buf->ino;
-  p->size  = (UINT64) buf->size;
-  p->mtime = (UINT64) buf->mtime;
-  p->atime = (UINT64) buf->atime;
-  p->ctime = (UINT64) buf->ctime;
-  p->owner = (UINT32) buf->owner;
-  p->group = (UINT32) buf->group;
-
-  p->checkflags = (UINT32) buf->check_flags; 
-  
-  return;
-}
-
-
-static void write_start_header(SL_TICKET fd)
-{
-  char   timestring[81];
-
-  if (pushdata_stdout == S_FALSE)
-    {
-      sl_write (fd, _("\n#Host "), 7);
-      sl_write (fd, sh.host.name, 
-		sl_strlen(sh.host.name));
-      sl_write (fd, _(" Version "), 9);
-      sl_write (fd, sh_db_version_string, 
-		sl_strlen(sh_db_version_string));
-      sl_write (fd, _(" Date "), 6);
-      (void) sh_unix_time(0, timestring, sizeof(timestring));
-      sl_write (fd, timestring, strlen(timestring));
-      sl_write (fd,        "\n", 1);
-    } 
-  else 
-    {
-      printf ("%s",_("\n#Host "));
-      printf ("%s", sh.host.name);
-      printf ("%s",_(" Version "));
-      printf ("%s", sh_db_version_string);
-      printf ("%s",_(" Date "));
-      (void) sh_unix_time(0, timestring, sizeof(timestring));
-      printf ("%s\n", timestring);
-    }
-}
-
-static void write_start_marker(SL_TICKET fd)
-{
-  if (sh_db_version_string != NULL)
-    {
-      write_start_header(fd);
-    }
-  
-  if (pushdata_stdout == S_FALSE)
-    {
-#if defined(SH_STEALTH)
-      sl_write      (fd,        "\n", 1);
-      sl_write_line (fd, N_("[SOF]"), 5);
-#else
-      sl_write_line (fd, _("\n[SOF]"),  6);
-#endif
-    }
-  else 
-    {
-#if defined(SH_STEALTH)
-      puts (N_("[SOF]"));
-#else
-      puts (_("\n[SOF]"));
-#endif
-    }
-}
-
-static void   write_record(SL_TICKET fd, sh_filestore_t * p, 
-			   char * fullpath, char * linkpath, char * attr_string)
-{
-  static char ll[2] = { '-', '\0' };
-  char * lpath;
-
-  if (!linkpath || 0 == sl_strlen(linkpath))
-    lpath = ll;
-  else
-    /* cppcheck-suppress uninitvar */
-    lpath = linkpath;
-
-  if (pushdata_stdout == S_FALSE)
-    {
-      if (SL_ENONE != sl_write (fd,        p, sizeof(sh_filestore_t)))
-	{
-	  char * tmp = sh_util_safe_name(fullpath);
-	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGPATH,
-			  _("Failed to write record to baseline database"),
-			  _("write_record"),
-			  tmp);
-	  SH_FREE(tmp);
-	  aud_exit(FIL__, __LINE__,  EXIT_FAILURE );
-	}
-      if (SL_ENONE != sl_write_line_fast (fd, fullpath, sl_strlen(fullpath)))
-	{
-	  char * tmp = sh_util_safe_name(fullpath);
-	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGPATH,
-			  _("Failed to write path to baseline database"),
-			  _("write_record"),
-			  tmp);
-	  SH_FREE(tmp);
-	  aud_exit(FIL__, __LINE__,  EXIT_FAILURE );
-	}
-      if (SL_ENONE != sl_write_line_fast (fd,    lpath, sl_strlen(lpath)))
-	{
-	  char * tmp = sh_util_safe_name(fullpath);
-	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGPATH,
-			  _("Failed to write lpath to baseline database"),
-			  _("write_record"),
-			  tmp);
-	  SH_FREE(tmp);
-	  aud_exit(FIL__, __LINE__,  EXIT_FAILURE );
-	}
-      if (attr_string)
-	sl_write_line_fast (fd, attr_string, sl_strlen(attr_string));
-    } 
-  else 
-    {
-      if (fwrite (p, sizeof(sh_filestore_t), 1, stdout))
-	{
-	  puts (fullpath);
-	  puts (lpath);
-	  if (attr_string)
-	    puts (attr_string);
-	}
-      else
-	{
-	  perror(_("Error writing database"));
-	  aud_exit (FIL__, __LINE__, EXIT_FAILURE);
-	}
-    }
-
-  SH_FREE(fullpath);
-  if (linkpath)
-    SH_FREE(linkpath);
-  if (attr_string)
-    SH_FREE(attr_string);
-
-  return;
-}
-
-static void sh_dbIO_data_write_int (file_type * buf, char * fileHash, 
-				    const char * outpath, int truncate)
-{
-  sh_filestore_t p;
-  char *  fullpath = NULL;
-  char *  linkpath = NULL;
-  char *  attr_string = NULL;
-
-  SL_ENTER(_("sh_dbIO_data_write_int"));
-
-  do_writeout_checks(outpath);
-
-  if (sh.flag.update == S_FALSE)
-    {
-      if (pushdata_stdout == S_FALSE && pushdata_fd == -1)
-	{
-	  if (truncate == S_TRUE)
-	    pushdata_fd = open_writeout_data_truncate(outpath);
-	  else
-	    {
-	      pushdata_fd = open_writeout_data(outpath);
-	      /* Seek to eof */
-	      seek_writeout_data(pushdata_fd, outpath);
-	    }
-	}
-    }
-  else /* update == TRUE */
-    {
-      if (pushdata_isfirst == 1)
-	{
-	  TPT((0, FIL__, __LINE__, _("msg=<Update.>\n")));
-	  pushdata_fd = open_writeout_data(outpath);
-	  /* Seek to sof and truncate */
-	  seek_writeout_data_old(pushdata_fd, outpath);
- 	}
-    }
-
-  /* unconditionally initialize the structure */
-  memset(&p, 0, sizeof(sh_filestore_t));
-
-  if (buf != NULL) 
-    {
-      fullpath = prep_path(buf->fullpath, S_TRUE);
-    }
-
-  /* NOTE: TXT entries are c_mode[0] != 'l' and do not get decoded 
-   */
-  if (buf != NULL /* && buf->c_mode[0] == 'l' */ && buf->link_path != NULL) 
-    {  
-      if (buf->c_mode[0] == 'l')
-	linkpath = prep_path(buf->link_path, S_TRUE);
-      else
-	linkpath = prep_path(buf->link_path, S_FALSE);
-    }
-
-  if (buf != NULL && buf->attr_string != NULL) 
-    {
-      attr_string = prep_attr(buf->attr_string);
-    }
-
-  if (buf != NULL) 
-    {
-      prep_struct(&p, buf, fileHash);
-      if (attr_string)
-	p.mark |= REC_FLAGS_ATTR;
-      swap_data(&p);
-    }
-
-  /* write the start marker 
-   */
-  if (pushdata_isfirst == 1) 
-    {
-      if (sh.flag.update == S_FALSE)
-	write_start_marker(pushdata_fd);
-      pushdata_isfirst = 0;
-    }
-
-  if (buf && fullpath)
-    {
-      write_record(pushdata_fd, &p, fullpath, linkpath, attr_string);
-    }
-
-  if ((sh.flag.update != S_TRUE) && (pushdata_stdout == S_FALSE))
-    {
-      if (sh.flag.checkSum != SH_CHECK_INIT || (buf == NULL && fileHash == NULL))
-	{
-	  if (SL_ISERROR(sl_close (pushdata_fd))) 
-	    {
-	      char * tmp = sh_util_safe_name(outpath);
-	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGPATH,
-			      _("Failed to close baseline database"),
-			      _("sh_dbIO_data_write_int"),
-			      tmp);
-	      SH_FREE(tmp);
-	    }
-	  else {
-	    if (sh.flag.checkSum == SH_CHECK_INIT)
-	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_DCLOSE);
-	  }
-	  pushdata_fd = -1;
-	}
-    }
-
-  SL_RET0(_("sh_dbIO_data_write_int"));
-}
-
-SH_MUTEX_STATIC(mutex_writeout,PTHREAD_MUTEX_INITIALIZER);
-
-void sh_dbIO_data_write (file_type * buf, char * fileHash)
-{
-  SH_MUTEX_LOCK(mutex_writeout); 
-  sh_dbIO_data_write_int (buf, fileHash, file_path('D', 'W'), S_FALSE);
-  SH_MUTEX_UNLOCK(mutex_writeout); 
-  return;
-}
-
-
-static int dbIO_writeout(sh_file_t * mtab[TABSIZE], const char * outpath, int truncate)
-{
-  sh_file_t * p;
-  int         i;
-  file_type * f;
-  char   fileHash[KEY_LEN + 1];
-
-  SL_ENTER(_("dbIO_writeout"));
-
-  SH_MUTEX_LOCK(mutex_writeout); 
-  if (!SL_ISERROR(pushdata_fd))
-    {
-      sl_close(pushdata_fd);
-      pushdata_fd = -1;
-    }
-  pushdata_isfirst =  1;
-
-
-  SH_MUTEX_LOCK(mutex_hash);
-  for (i = 0; i < TABSIZE; ++i)
-    {
-      for (p = mtab[i]; p; p = p->next)
-	{
-	  f = sh_hash_create_ft (p, fileHash);
-	  sh_dbIO_data_write_int (f, fileHash, outpath, (i == 0) ? truncate : S_FALSE);
-	  if (f->attr_string) SH_FREE(f->attr_string);
-	  if (f->link_path)   SH_FREE(f->link_path);
-	  SH_FREE(f);
-	}
-    }
-  SH_MUTEX_UNLOCK(mutex_hash);
-
-  if (!SL_ISERROR(pushdata_fd))
-    {
-      sl_close(pushdata_fd);
-      pushdata_fd = -1;
-    }
-  pushdata_isfirst =  1;
-  SH_MUTEX_UNLOCK(mutex_writeout); 
-
-  SL_RETURN (0, _("dbIO_writeout"));
-}
-
-int sh_dbIO_writeout_update()
-{
-  sh_file_t ** mtab = get_default_data_table();
-
-  if (S_TRUE == file_is_remote())
-    {
-      sh_error_handle((-1), FIL__, __LINE__, S_FALSE, MSG_E_SUBGEN, 
-		      _("Baseline database is remote"), _("sh_dbIO_writeout"));
-      SL_RETURN (1, _("sh_dbIO_writeout_update"));
-    }
-
-  return dbIO_writeout(mtab, file_path('D', 'W'), S_FALSE);
-}
-
-int sh_dbIO_writeout_to_path(const char * path)
-{
-  sh_file_t ** mtab = get_default_data_table();
-  return dbIO_writeout(mtab, path, S_TRUE);
-}
-
-static void dbIO_write_record(sh_file_t * record, SL_TICKET fd)
-{
-  sh_filestore_t * p = &(record->theFile);
-  char * fullpath    = NULL;
-  char * linkpath    = NULL;
-  char * attr_string = NULL;
-
-  fullpath = prep_path(record->fullpath, S_TRUE);
-
-  /* NOTE: TXT entries are c_mode[0] != 'l' and do not get decoded 
-   */
-  if (record->linkpath != NULL && 0 != strcmp("-", record->linkpath)) 
-    {  
-      if (p->c_mode[0] == 'l')
-	linkpath = prep_path(record->linkpath, S_TRUE);
-      else
-	linkpath = prep_path(record->linkpath, S_FALSE);
-    }
-
-  if (record->attr_string != NULL) 
-    attr_string = prep_attr(record->attr_string);
-
-  prep_encode(p);
-  swap_data(p);
-
-  write_record(fd, p, fullpath, linkpath, attr_string);
-  return;
-}
-
-static void dbIO_write_entry(sh_file_t * p)
-{
-  static int is_first = 1;
-
-  if (is_first)
-    {
-      pushdata_isfirst =  1;
-      if (!sh.outpath || sh.outpath[0] == '\0') 
-	pushdata_stdout  =  S_TRUE;
-      else
-	pushdata_fd = open_writeout_data_truncate(sh.outpath);
-      write_start_marker(pushdata_fd);
-      pushdata_isfirst = 0;
-      is_first = 0;
-    }
-
-  dbIO_write_record(p, pushdata_fd);
-
-}
-
-
-/******************************************************************
- *
- * Listing the database.
- *
- ******************************************************************/ 
-
-static int ListBinary = S_FALSE;
-static char * ListFilter = NULL;
-
-int sh_dbIO_list_binary (const char * c)
-{
-  (void) c;
-  ListBinary = S_TRUE;
-  return 0;
-}
-int sh_dbIO_list_filter (const char * c)
-{
-  ListFilter = sh_util_strdup(c);
-  return 0;
-}
-
-#include "zAVLTree.h"
-
-static zAVLTree * filter_list = NULL;
-extern char * rtrim (char * str);
-
-#include <ctype.h>
-static void read_filter()
-{
-  int    i, n = 0;
-  size_t len;
-  char * key;
-  char * str;
-  char * line = SH_ALLOC(SH_MAXBUF);
-  FILE * fd   = fopen(ListFilter, "r");
-  
-  if (!fd)
-    {
-      perror(_("read_filter: fopen:"));
-      _exit(EXIT_FAILURE);
-    }
-  do {
-    i = sh_dbIO_getline (fd, line, SH_MAXBUF);
-    str = rtrim(line);
-    while (isspace((int)*str)) ++str;
-
-    key = sh_files_parse_input(str, &len);
-
-    if (key && *key == '/')
-      {
-	zAVL_string_set(&filter_list, key);
-	++n;
-      }
-  } while (i >= 0);
-
-  fclose(fd);
-  SH_FREE(line);
-
-  if (n == 0)
-    {
-      fprintf(stderr, _("read_filter: empty file <%s>\n"), ListFilter);
-      _exit (EXIT_FAILURE);
-    }
-  return;
-}
-
-static int check_filter(char * path)
-{
-  if (NULL == zAVL_string_get(filter_list, path))
-    return S_FALSE;
-  return S_TRUE;
-}
-
-int sh_dbIO_list_db (const char * db_file)
-{
-  sh_file_t * p;
-  SL_TICKET fd;
-  char * line;
-  int  errflag = 0;
-  int  flag = 0;
-  char * ListFile = get_list_file();
-
-  if (!db_file)
-    {
-      fputs(_("ERROR: no database file given\n"), stderr);
-      _exit(EXIT_FAILURE);
-      return -1; 
-    }
-  if (sl_is_suid())
-    {
-      fputs(_("ERROR: insufficient privilege\n"), stderr);
-      _exit (EXIT_FAILURE);
-      return -1; /* for Mac OSX compiler */
-    }
-  if (0 == strcmp(db_file, _("default")))
-    db_file = file_path('D', 'W');
-  if (!db_file)
-    {
-      fputs(_("ERROR: no filename\n"), stderr);
-      _exit(EXIT_FAILURE);
-      return -1; 
-    }
-
-  if (ListFilter) 
-    read_filter();
-
-  line = SH_ALLOC(MAX_PATH_STORE+2);
-
-  if ( SL_ISERROR(fd = sl_open_read(FIL__, __LINE__, db_file, SL_YESPRIV))) 
-    {
-      fprintf(stderr, _("ERROR: can't open %s for read (errnum = %ld)\n"), 
-	      db_file, fd);
-      _exit(EXIT_FAILURE);
-      return -1; 
-    }
-
-  /* fast forward to start of data
-   */
-  if (0 != sh_dbIO_setdataent(fd, line, MAX_PATH_STORE+1, db_file))
-    {
-      fprintf(stderr, _("ERROR: can't find start marker in %s\n"), 
-	      db_file);
-      _exit(EXIT_FAILURE);
-      return -1; 
-    }
-
-  while (1) 
-    {
-      p = sh_dbIO_getdataent (line, MAX_PATH_STORE+1, db_file, &errflag);
-      if ((p != NULL) && (p->fullpath[0] == '/'))
-	{
-	  if (!ListFile)
-	    {
-	      flag = 1;
-	      if (ListFilter && S_FALSE == check_filter(p->fullpath))
-		continue;
-	      if (ListBinary)
-		dbIO_write_entry (p);
-	      else
-		sh_hash_list_db_entry (p); 
-	    }
-	  else
-	    {
-	      if (0 != sl_strcmp(ListFile, p->fullpath))
-		{
-		  continue;
-		}
-	      flag = 1;
-	      if ('l' != p->theFile.c_mode[0])
-		{
-		  if (sh_hash_printcontent(p->linkpath) < 0)
-		    {
-		      fputs(_("Error listing file content\n"), stderr);
-		      _exit(EXIT_FAILURE);
-		      return -1;
-		    }
-		}
-	      else
-		{
-		  fputs(_("File is a link\n"), stderr);
-		  _exit(EXIT_FAILURE);
-		  return -1;
-		}
-	      break;
-	    }
-	}
-      else if (p == NULL)
-	{
-	  break;
-	}
-    }
-
-  if (line != NULL)
-    SH_FREE(line);
-  sl_close (fd);
-
-  fflush(NULL);
-
-  if (flag == 0)
-    {
-      fputs(_("File not found.\n"), stderr);
-      _exit(EXIT_FAILURE);
-    }
-  else if (errflag < 0)
-    {
-      fputs(_("Error while reading file.\n"), stderr);
-      _exit(EXIT_FAILURE);
-    }
-      
-  _exit(EXIT_SUCCESS);
-  return 0; 
-}
-
-/* if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE) */
-#endif
Index: trunk/src/sh_entropy.c
===================================================================
--- trunk/src/sh_entropy.c	(revision 591)
+++ trunk/src/sh_entropy.c	(revision 1)
@@ -30,8 +30,15 @@
 #endif
 
+#if TIME_WITH_SYS_TIME
+#include <sys/time.h>
+#include <time.h>
+#else
 #if HAVE_SYS_TIME_H
 #include <sys/time.h>
-#endif
+#else
 #include <time.h>
+#endif
+#endif
+
 
 #include <stdlib.h>
@@ -110,5 +117,5 @@
 int sh_entropy(int getbytes, char * nbuf)
 {
-    int fd = -1;
+    static int fd = -1;
     int n;
     byte buffer[256+2];
@@ -131,5 +138,5 @@
     if( do_restart ) {
         if( fd != -1 ) {
-            sl_close_fd(FIL__, __LINE__,  fd );
+            close( fd );
             fd = -1;
         }
@@ -140,9 +147,8 @@
         struct sockaddr_un addr;
         int addr_len;
-	int retval;
-
-#ifdef EGD_SOCKET_NAME
+
+      #ifdef EGD_SOCKET_NAME
         bname = EGD_SOCKET_NAME;
-#endif
+      #endif
         if ( !bname || !*bname )
             bname = _("=entropy");
@@ -164,5 +170,5 @@
         memset( &addr, 0, sizeof(addr) );
         addr.sun_family = AF_UNIX;
-        sl_strlcpy( addr.sun_path, name, sizeof(addr.sun_path) );
+        strcpy( addr.sun_path, name );              /* known to fit  */
         addr_len = offsetof( struct sockaddr_un, sun_path )
                    + strlen( addr.sun_path );
@@ -178,8 +184,5 @@
 	    SL_RETURN( -1, _("sh_entropy") );
 	  }
-	do {
-	  retval = connect(fd, (struct sockaddr *) &sinr, sizeof(sinr));
-	} while (retval < 0 && (errno == EINTR || errno == EINPROGRESS));
-        if( retval == -1 )
+        if( connect( fd, (struct sockaddr*)&addr, addr_len) == -1 )
 	  {
 	    myerror = errno;
@@ -188,5 +191,4 @@
 			     _("sh_entropy") ); 
 	    SH_FREE(name);
-	    sl_close_fd(FIL__, __LINE__, fd);
 	    SL_RETURN( -1, _("sh_entropy") );
 	  }
@@ -204,6 +206,5 @@
 	    sh_error_handle ((-1), FIL__, __LINE__, myerror, MSG_E_SUBGEN,
 			     _("cannot write to EGD"),
-			     _("sh_entropy") );
-	    sl_close_fd(FIL__, __LINE__, fd);
+			     _("sh_entropy") ); 
 	    SL_RETURN( -1, _("sh_entropy") );
 	  }
@@ -246,6 +247,5 @@
 	    sh_error_handle ((-1), FIL__, __LINE__, myerror, MSG_E_SUBGEN,
 			     _("cannot write to EGD"),
-			     _("sh_entropy") );
-	    sl_close_fd(FIL__, __LINE__, fd);
+			     _("sh_entropy") ); 
 	    SL_RETURN( -1, _("sh_entropy") );
 	  }
@@ -267,5 +267,5 @@
     }
     memset(buffer, 0, sizeof(buffer) );
-    sl_close_fd(FIL__, __LINE__, fd);
+
     SL_RETURN( 0, _("sh_entropy") ); /* success */
 }
@@ -276,11 +276,33 @@
 #if defined (HAVE_URANDOM)
 
-#include "sh_pthread.h"
-
-int read_mbytes(int timeout_val, const char * path, char * nbuf, int nbytes)
+#include <setjmp.h>
+
+static jmp_buf          entropy_timeout;
+
+static void sh_entropy_alarmhandle (int mysignal)
 {
-  int m_count;
+  (void) mysignal; /* avoid compiler warning */
+  longjmp (entropy_timeout, 1);
+}
+
+
+int read_mbytes(int timeout_val, char * path, char * nbuf, int nbytes)
+{
+  int count, m_count;
   int fd2;
 
+  struct  sigaction  new_act;
+  sigset_t           unblock;
+
+  struct sigaction      old_act;
+  volatile unsigned int old_alarm = 0;
+
+  new_act.sa_handler = sh_entropy_alarmhandle;
+  sigemptyset( &new_act.sa_mask );       /* set an empty mask       */
+  new_act.sa_flags = 0;                  /* init sa_flags           */
+
+  sigemptyset(&unblock);
+  sigaddset  (&unblock, SIGALRM);
+
   SL_ENTER(_("read_mbytes"));
 
@@ -288,12 +310,59 @@
     {
       /* Test whether file is a character device, and is 
-       * readable.
-       */
-      if (0 == sh_unix_device_readable(fd2)) 
+       * not world writeable.
+       */
+      if (0 == sh_unix_file_exists(fd2)) 
 	{
-	  m_count = sl_read_timeout_fd(fd2, nbuf, nbytes, 
-				       timeout_val, S_FALSE);
-	  if (m_count < 0)
-	    m_count = 0;
+
+	  /* alarm was triggered
+	   */
+	  if (setjmp(entropy_timeout) != 0)
+	    {
+	      alarm(0);
+	      sigaction (SIGALRM, &old_act, NULL);
+	      alarm(old_alarm);
+	      sigprocmask(SIG_UNBLOCK, &unblock, NULL);
+	      TPT((0,FIL__,__LINE__, _("msg=<read_mbytes: timeout>\n"))); 
+	      close (fd2);
+	      SL_RETURN(0, _("read_mbytes"));
+	    }
+
+	  /* timeout after 30 seconds
+	   */
+	  old_alarm = alarm(0);
+	  sigaction (SIGALRM, &new_act, &old_act);
+	  alarm(timeout_val);
+
+	  m_count = 0;
+
+	  while (m_count < nbytes) 
+	    {
+	      errno = 0; /* paranoia */
+	      count = read (fd2, &nbuf[m_count], nbytes-m_count);
+
+	      switch (count)
+		{
+		case -1:
+#ifdef EWOULDBLOCK
+                  if (errno == EINTR || errno == EAGAIN ||
+                      errno == EWOULDBLOCK)
+#else
+                  if (errno == EINTR || errno == EAGAIN)
+#endif
+                    continue;
+
+                  /* if errno == -1 && no continue: fallthrough to this */
+                case 0:
+                  break;
+                default:
+                  m_count += count;
+                }
+	    }
+	  close (fd2);
+
+	  alarm(0);
+	  sigaction (SIGALRM, &old_act, NULL);
+	  alarm(old_alarm);
+	  sigprocmask(SIG_UNBLOCK, &unblock, NULL);
 	}
       else
@@ -302,6 +371,4 @@
   else
     m_count = 0;
-
-  sl_close_fd(FIL__, __LINE__, fd2);
 
   TPT((0, FIL__, __LINE__, _("msg=<read_mbytes: OK>\n"))); 
@@ -317,5 +384,4 @@
   int    i, m_count = 0;
   char * keybuf;
-  UINT32 kbuf[KEY_BYT/sizeof(UINT32)];
   char   addbuf[2 * KEY_BYT];
 
@@ -327,10 +393,10 @@
     nbytes = KEY_BYT;
 
-  memset(nbuf, 0, nbytes);
+  memset(nbuf, '\0', nbytes);
 
 #ifdef NAME_OF_DEV_URANDOM
-  m_count = read_mbytes ( 10, NAME_OF_DEV_URANDOM, nbuf, nbytes);
+  m_count = read_mbytes (30, NAME_OF_DEV_RANDOM, nbuf, nbytes);
 #else
-  m_count = read_mbytes (300, NAME_OF_DEV_RANDOM,  nbuf, nbytes);
+  m_count = read_mbytes (300, NAME_OF_DEV_RANDOM, nbuf, nbytes);
 #endif
 
@@ -339,5 +405,5 @@
 #ifdef NAME_OF_DEV_URANDOM
       sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__, EIO, MSG_NODEV, 
-		       (long) sh.real.uid, NAME_OF_DEV_URANDOM);
+		       (long) sh.real.uid, NAME_OF_DEV_RANDOM);
 #else
       sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_NODEV, 
@@ -346,11 +412,11 @@
     }
 
-#ifdef NAME_OF_DEV_RANDOM
+#ifdef NAME_OF_DEV_URANDOM
   if (m_count < nbytes)
     {
-      i = read_mbytes(300, NAME_OF_DEV_RANDOM, &nbuf[m_count], nbytes-m_count);
+      i = read_mbytes(30, NAME_OF_DEV_URANDOM, &nbuf[m_count], nbytes-m_count);
       if (i == 0)
 	sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_NODEV, 
-			 (long) sh.real.uid, NAME_OF_DEV_RANDOM);
+			 (long) sh.real.uid, NAME_OF_DEV_URANDOM);
       else
 	m_count += i;
@@ -363,5 +429,5 @@
       /* -- Add previous entropy into the new pool. --
        */
-      memset(addbuf, 0, sizeof(addbuf));
+      memset(addbuf, '\0', sizeof(addbuf));
       for (i = 0; i < m_count; ++i)
 	addbuf[i]         = nbuf[i];
@@ -369,11 +435,9 @@
 	addbuf[i+KEY_BYT] = skey->poolv[i];
       keybuf = (char *) sh_tiger_hash_uint32 (addbuf, 
-					      TIGER_DATA, 2 * KEY_BYT,
-					      kbuf, KEY_BYT/sizeof(UINT32));
-      memset(addbuf, 0, sizeof(addbuf));
+					      TIGER_DATA, 2 * KEY_BYT);
+      memset(addbuf, '\0', sizeof(addbuf));
       
       /* -- Give out nbytes bytes from the new pool. --
        */
-      SH_MUTEX_LOCK_UNSAFE(mutex_skey);
       for (i = 0; i < KEY_BYT; ++i)
 	{
@@ -382,7 +446,5 @@
 	    nbuf[i] = keybuf[i];
 	}
-      SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
-      memset (keybuf, 0, KEY_BYT);
-      memset (kbuf,   0, sizeof(kbuf));
+      memset (keybuf, '\0', KEY_BYT);
       
       SL_RETURN(0, _("sh_entropy"));
@@ -409,13 +471,11 @@
 #endif
 #ifndef FD_ZERO
-#define FD_ZERO(p)      memset((char *)(p), 0, sizeof(*(p)))
+#define FD_ZERO(p)      memset((char *)(p), '\0', sizeof(*(p)))
 #endif
 
 #include "sh_static.h"
-#include "sh_pthread.h"
 
 static
 char   * com_path[] = {
-  N_("/usr/bin/xpg4/"),
   N_("/usr/ucb/"),
   N_("/bin/"),
@@ -424,5 +484,4 @@
   N_("/usr/sbin/"),
   N_("/usr/local/bin/"),
-  N_("/opt/local/bin/"),
   NULL
 };
@@ -439,5 +498,5 @@
 
 static
-sourcetable_t source_template[] = {
+sourcetable_t source[] = {
   { N_("w"),
     N_("w"),
@@ -508,31 +567,12 @@
   int pipedes[2];
   FILE *outf = NULL;
+  struct passwd * tempres;
   char * arg[4];
   char * envp[2];
-  size_t len;
-  char   arg0[80];
-  char   arg1[80];
-
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-  struct passwd    pwd;
-  char           * buffer;
-  struct passwd *  tempres;
-#else
-  struct passwd * tempres;
-#endif
 
   SL_ENTER(_("sh_popen"));
 
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-  buffer = SH_ALLOC(SH_PWBUF_SIZE);
-  sh_getpwnam_r(DEFAULT_IDENT, &pwd, buffer, SH_PWBUF_SIZE, &tempres);
-#else
-  tempres = sh_getpwnam(DEFAULT_IDENT);
-#endif
-
-  strncpy (arg0, _("/bin/sh"), sizeof(arg0));
-  arg[0] = arg0;
-  strncpy (arg1, _("-c"), sizeof(arg1));
-  arg[1] = arg1;
+  arg[0] = _("/bin/sh");
+  arg[1] = _("-c");
   arg[2] = command;
   arg[3] = NULL;
@@ -540,8 +580,7 @@
   if (sh.timezone != NULL)
     {
-      len = sl_strlen(sh.timezone) + 4;
-      envp[0] = calloc(1, len);     /* free() ok     */
+      envp[0] = malloc (sl_strlen(sh.timezone) + 4);     /* free() ok     */
       if (envp[0] != NULL)
-	sl_snprintf (envp[0], len, "TZ=%s", sh.timezone);
+	sprintf (envp[0], "TZ=%s", sh.timezone);         /* known to fit  */
       else
 	envp[0] = NULL;
@@ -561,6 +600,4 @@
   }
   
-  fflush (NULL);
-
   source->pid = aud_fork(FIL__, __LINE__);
   
@@ -568,6 +605,6 @@
    */
   if (source->pid == (pid_t) - 1) {
-    sl_close_fd(FIL__, __LINE__, pipedes[0]);
-    sl_close_fd(FIL__, __LINE__, pipedes[1]);
+    close(pipedes[0]);
+    close(pipedes[1]);
     if (envp[0] != NULL) free(envp[0]);
     SL_RETURN(NULL, _("sh_popen"));
@@ -576,23 +613,19 @@
   if (source->pid == (pid_t) 0) 
     {
-      int val_return;
 
       /* child - make read side of the pipe stdout 
        */
-      do {
-	val_return = dup2 (pipedes[STDOUT_FILENO], STDOUT_FILENO);
-      } while (val_return < 0 && errno == EINTR);
-
-      if (val_return < 0)
-	_exit(EXIT_FAILURE);
+      if (retry_aud_dup2(FIL__, __LINE__, 
+			 pipedes[STDOUT_FILENO], STDOUT_FILENO) < 0)
+	aud__exit(FIL__, __LINE__, EXIT_FAILURE);
       
       /* close the pipe descriptors 
        */
-      sl_close_fd   (FIL__, __LINE__, pipedes[STDIN_FILENO]);
-      sl_close_fd   (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
+      close   (pipedes[STDIN_FILENO]);
+      close   (pipedes[STDOUT_FILENO]);
 
       /* don't leak file descriptors
        */
-      sh_unix_closeall (3, -1, S_TRUE); /* in child process */
+      sh_unix_closeall (3, -1); /* in child process */
 
       /* zero priv info
@@ -603,73 +636,57 @@
        */
       i = 0; 
-      if (0 == geteuid()) 
-	{
-  
-	  if (NULL != tempres) {
-	    i = setgid(tempres->pw_gid);
-
-	    /*** locks up in dnmalloc ***/
-	    /*
-	     * if (i == 0)
-	     *   i = sh_unix_initgroups(DEFAULT_IDENT ,tempres->pw_gid);
-	     */
-
-	    if (i == 0) 
-	      i = setuid(tempres->pw_uid);
-	    /* make sure we cannot get root again
-	     */
-	    if ((tempres->pw_uid != 0) && 
-		(setuid(0) >= 0))
-	      i = -1;
-	  } else {
+      if (0 == geteuid()) {  
+	tempres = sh_getpwnam(DEFAULT_IDENT);
+	if (NULL != tempres) {
+	  i = aud_setgid(FIL__, __LINE__, tempres->pw_gid); 
+	  if (i == 0)
+	    i = sh_unix_initgroups(DEFAULT_IDENT ,tempres->pw_gid);
+	  if (i == 0) 
+	    i = aud_setuid(FIL__, __LINE__, tempres->pw_uid);
+	  /* make sure we cannot get root again
+	   */
+	  if ((tempres->pw_uid != 0) && (aud_setuid(FIL__, __LINE__, 0) >= 0))
 	    i = -1;
-	  }
+	} else {
+	  i = -1;
 	}
+      }
       
       /* some problem ...
        */
       if (i == -1) {
-	_exit(EXIT_FAILURE);
+	aud__exit(FIL__, __LINE__, EXIT_FAILURE);
       }
       
-      /* cppcheck-suppress leakNoVarFunctionCall */
-      if (NULL != freopen (_("/dev/null"), "r+", stderr))
-	{
+      freopen (_("/dev/null"), "r+", stderr);
       
-	  /* exec the program */
-	  do {
-	    val_return = execve (_("/bin/sh"), arg, envp);
-	  } while (val_return < 0 && errno == EINTR);
-	}
-
+      /* exec the program */
+      retry_aud_execve (FIL__, __LINE__, _("/bin/sh"), arg, envp);
+      
       /* failed 
        */
-      _exit(EXIT_FAILURE);
-    }
-
-  /* parent
-   */
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-  SH_FREE(buffer);
-#endif
-
-  if (envp[0] != NULL) 
-    free(envp[0]);
-  
-  sl_close_fd (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
-  retry_fcntl (FIL__, __LINE__, pipedes[STDIN_FILENO], F_SETFD, FD_CLOEXEC);
-  
-  outf = fdopen (pipedes[STDIN_FILENO], "r");
-  
-  if (outf == NULL) 
-    {
-      aud_kill (FIL__, __LINE__, source->pid, SIGKILL);
-      sl_close_fd (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
-      waitpid (source->pid, NULL, 0);
-      source->pid = 0;
-      SL_RETURN(NULL, _("sh_popen"));
-    }
-  
-  SL_RETURN(outf, _("sh_popen"));
+      aud__exit(FIL__, __LINE__, EXIT_FAILURE);
+    }
+
+    /* parent
+     */
+    if (envp[0] != NULL) 
+      free(envp[0]);
+
+    close (pipedes[STDOUT_FILENO]);
+    retry_fcntl (FIL__, __LINE__, pipedes[STDIN_FILENO], F_SETFD, FD_CLOEXEC);
+
+    outf = fdopen (pipedes[STDIN_FILENO], "r");
+
+    if (outf == NULL) 
+      {
+        aud_kill (FIL__, __LINE__, source->pid, SIGKILL);
+	close (pipedes[STDOUT_FILENO]);
+        waitpid (source->pid, NULL, 0);
+        source->pid = 0;
+	SL_RETURN(NULL, _("sh_popen"));
+      }
+
+    SL_RETURN(outf, _("sh_popen"));
 }
 
@@ -678,42 +695,15 @@
 {
     int status = 0;
-    int retval;
-    char msg[128];
-    char errbuf[SH_ERRBUF_SIZE];
 
     SL_ENTER(_("sh_pclose"));
 
-    retval = sl_fclose(FIL__, __LINE__, source->pipe);
-    if (retval)
+    status = fclose(source->pipe);
+    if (status)
       {
-	sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, retval, 
-			 MSG_E_SUBGEN,
-			 sh_error_message(retval, errbuf, sizeof(errbuf)),
-                         _("sh_pclose"));
 	SL_RETURN((-1), _("sh_pclose"));
       }
 
-    retval = waitpid(source->pid, &status, 0);
-    if (retval != source->pid)
-      {
-	sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, retval, 
-			 MSG_E_SUBGEN,
-			 sh_error_message(retval, errbuf, sizeof(errbuf)),
-                         _("sh_pclose"));
-
-	status = -1;
-      }
-#if !defined(USE_UNO)
-    else if (WIFSIGNALED(status))
-      {
-	sl_snprintf(msg, sizeof(msg), _("Subprocess terminated by signal %d"),
-		    WTERMSIG(status));
-	sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, retval, 
-			 MSG_E_SUBGEN,
-			 msg,
-                         _("sh_pclose"));
-	status = -1;
-      }
-#endif
+    if (waitpid(source->pid, NULL, 0) != source->pid)
+      status = -1;
 
     source->pipe = NULL;
@@ -738,5 +728,4 @@
 
   char * keybuf;
-  UINT32 kbuf[KEY_BYT/sizeof(UINT32)];
   char   addbuf[2 * KEY_BYT];
 
@@ -746,8 +735,5 @@
   int    maxFD = 0;
   int    imax, selcount;
-  char errbuf[SH_ERRBUF_SIZE];
-
-  sourcetable_t  *source = NULL;
-  
+
   SL_ENTER(_("sh_entropy"));
 
@@ -760,5 +746,4 @@
   /* --- If there is entropy in the pool, return it. ---
    */
-  SH_MUTEX_LOCK_UNSAFE(mutex_skey);
   if (skey->poolc >= nbytes)
     {
@@ -769,8 +754,6 @@
 	  --skey->poolc;
 	}
-      SH_MUTEX_UNLOCK_UNSAFE(mutex_skey); /* alternative path */
       SL_RETURN(0, _("sh_entropy"));
     }
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
 
 
@@ -783,17 +766,9 @@
     {
       sh_error_handle((-1), FIL__, __LINE__, caperr, MSG_E_SUBGEN,
-		      sh_error_message (caperr, errbuf, sizeof(errbuf)), 
+		      sh_error_message (caperr), 
 		      _("sl_get_cap_sub"));
     }
 
-  while (source_template[i].command != NULL) {
-    ++i;
-  }
-  source = SH_ALLOC(i * sizeof(sourcetable_t));
-  for (j = 0; j < i;++j)
-    memcpy(&source[j], &source_template[j], sizeof(sourcetable_t));
-  i = 0;
-
-  while (source_template[i].command != NULL) {
+  while (source[i].command != NULL) {
 
     j = 0;
@@ -803,5 +778,4 @@
 	sl_strlcat(combuf, _(source[i].command), 80);
 
-	/* flawfinder: ignore */
 	if ( access (combuf, X_OK) == 0) 
 	  {
@@ -925,47 +899,20 @@
     }
   buffer[bufcount] = '\0';
-  
-  SH_FREE(source);
 
   if (0 != (caperr = sl_drop_cap_sub()))
     {
       sh_error_handle((-1), FIL__, __LINE__, caperr, MSG_E_SUBGEN,
-		      sh_error_message (caperr, errbuf, sizeof(errbuf)), 
+		      sh_error_message (caperr), 
 		      _("sl_drop_cap_sub"));
     }
 
-#ifdef HAVE_GETTIMEOFDAY
-  {
-    unsigned short tseed[3];
-
-    gettimeofday(&tv, 0);
-    tseed[0] = tv.tv_sec & 0xFFFF;
-    tseed[1] = tv.tv_usec & 0xFFFF;
-    tseed[2] = (tv.tv_sec ^ tv.tv_usec) >> 16;
-    keybuf = (char *) sh_tiger_hash_uint32 ((char *) tseed, 
-					    TIGER_DATA, sizeof(tseed),
-					    kbuf, KEY_BYT/sizeof(UINT32));
-    memset(addbuf, 0, sizeof(addbuf));
-    for (i = 0; i < KEY_BYT; ++i)
-      {
-	addbuf[i]         = keybuf[i];
-	addbuf[i+KEY_BYT] = skey->poolv[i];
-      }
-    SH_MUTEX_LOCK_UNSAFE(mutex_skey);
-    for (i = 0; i < KEY_BYT; ++i)
-      skey->poolv[i] ^= keybuf[i];
-    SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
-  }
-#endif
-
   if (bufcount > 0) 
     {
       keybuf = (char *) sh_tiger_hash_uint32 (buffer, 
-					      TIGER_DATA, sl_strlen(buffer),
-					      kbuf, KEY_BYT/sizeof(UINT32));
+					      TIGER_DATA, sl_strlen(buffer));
 
       /* add previous entropy into the new pool
        */
-      memset(addbuf, 0, sizeof(addbuf));
+      memset(addbuf, '\0', sizeof(addbuf));
       for (i = 0; i < KEY_BYT; ++i)
 	{
@@ -974,17 +921,14 @@
 	}
       keybuf = (char *) sh_tiger_hash_uint32 (addbuf, 
-					      TIGER_DATA, sizeof(addbuf),
-					      kbuf, KEY_BYT/sizeof(UINT32));
-      memset(addbuf, 0, sizeof(addbuf));
+					      TIGER_DATA, sizeof(addbuf));
+      memset(addbuf, '\0', sizeof(addbuf));
       
       /* store in system pool
        */
-      SH_MUTEX_LOCK_UNSAFE(mutex_skey);
       for (i = 0; i < KEY_BYT; ++i)
 	skey->poolv[i] = keybuf[i];
       skey->poolc = KEY_BYT;
-      SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
-      memset (buffer, 0, BUF_ENT+2);
-      memset (keybuf, 0, KEY_BYT);
+      memset (buffer, '\0', BUF_ENT+2);
+      memset (keybuf, '\0', KEY_BYT);
       SH_FREE(buffer);
     } 
@@ -997,5 +941,4 @@
   /* give out nbytes Bytes from the entropy pool
    */
-  SH_MUTEX_LOCK_UNSAFE(mutex_skey);
   for (i = 0; i < nbytes; ++i)
     {
@@ -1003,5 +946,4 @@
       --skey->poolc;
     }
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
 
   SL_RETURN(0, _("sh_entropy"));
@@ -1011,38 +953,8 @@
 #endif
 
-#ifdef SH_CUTEST
-#include "CuTest.h"
-
-void Test_entropy (CuTest *tc)
-{
-  char                 bufx[9 * sizeof(UINT32) + 1];
-  char                 bufy[9 * sizeof(UINT32) + 1];
-  int                  status;
-  int                  count;
-
-  for (count = 0; count < 20; ++count)
-    {
-      memset(skey->poolv, 0, KEY_BYT);
-      skey->poolc = 0;
-
-      status = sh_entropy (24, bufx);
-      CuAssertTrue(tc, 0 == status);
-      
-      memset(skey->poolv, 0, KEY_BYT);
-      skey->poolc = 0;
-
-      status = sh_entropy (24, bufy);
-      CuAssertTrue(tc, 0 == status);
-      
-      CuAssertTrue(tc, 0 != memcmp(bufx, bufy, 24));
-    }
-}
-#endif
-
-
-
-
-
-
-
-
+
+
+
+
+
+
Index: trunk/src/sh_err_console.c
===================================================================
--- trunk/src/sh_err_console.c	(revision 591)
+++ trunk/src/sh_err_console.c	(revision 1)
@@ -25,5 +25,4 @@
 #include "sh_error.h"
 #include "sh_utils.h"
-#include "sh_sem.h"
 
 #undef  FIL__
@@ -33,11 +32,9 @@
 #include <sys/types.h>
 #include <fcntl.h>
-#include <sys/stat.h>
 #include <unistd.h>
 #include <signal.h>
-#include <sys/socket.h>
-#include <sys/un.h>
 
 extern int  OnlyStderr;
+
  
 #if !defined(O_NONBLOCK)
@@ -56,8 +53,4 @@
 #include <sys/msg.h>
 
-#if !defined(EIDRM)
-#define EIDRM (EINVAL)
-#endif
-
 struct sh_msgbuf {
   long mtype;
@@ -73,22 +66,20 @@
 /* Open the SysV message queue, creating it when neccesary
  */
-static int open_queue(void)
+static int open_ipc(void)
 {
   key_t            key;
-#if defined(WITH_TPT) 
-  char errbuf[SH_ERRBUF_SIZE];
-#endif
-
-  SL_ENTER(_("open_queue"));
+  int              error;
+
+  SL_ENTER(_("open_ipc"));
 
   /* get key
    */
-  key = ftok (DEFAULT_DATAROOT, '#');
-
+  key = ftok ("/tmp", '#');
   if (key == (key_t) -1)
     {
+      error = errno;
       TPT(( 0, FIL__, __LINE__, _("msg=<ftok: %s> errno=<%d>\n"), 
-	    sh_error_message(errno, errbuf, sizeof(errbuf)), errno));
-      SL_RETURN(-1, _("open_queue"));
+	    sh_error_message(error), error));
+      SL_RETURN(-1, _("open_ipc"));
     }
 
@@ -99,25 +90,27 @@
   if (msgid < 0)
     {
+      error = errno;
       TPT(( 0, FIL__, __LINE__, _("msg=<msgget: %s> errno=<%d>\n"), 
-	    sh_error_message(errno, errbuf, sizeof(errbuf)), errno));
-      SL_RETURN(-1, _("open_queue"));
-    }
-
-  SL_RETURN(0, _("open_queue"));
-}
-
-/* Close the SysV message queue and/or semaphore
+	    sh_error_message(error), error));
+      SL_RETURN(-1, _("open_ipc"));
+    }
+
+  SL_RETURN(0, _("open_ipc"));
+}
+
+/* Close the SysV message queue
  */
 void close_ipc (void)
 {
+  SL_ENTER(_("close_ipc"));
+
   if (msgid != (-1))
     (void) msgctl (msgid, IPC_RMID, NULL);
-  sh_sem_close();
-  return;
+  SL_RET0(_("close_ipc"));
 }
 
 /* Enable the message queue
  */
-int enable_msgq(const char * foo)
+int enable_msgq(char * foo)
 {
   int i;
@@ -128,33 +121,14 @@
 }
 
+/* #define MY_MAX_MSG    254 */
 #define MY_MAX_MSG    1022
 
-static void remove_message()
-{
-  int rc;
-  struct {
-    long    mtype;       /* Message type. */
-    char    mtext[128];  /* Message text. */
-  } recv_msg;
-
-  recv_msg.mtype = 1;
-  do {
-    rc = msgrcv(msgid, &recv_msg, sizeof(recv_msg.mtext), 1, 
-		MSG_NOERROR|IPC_NOWAIT);
-  } while (rc < 0 && errno == EINTR);
-
-  memset(&recv_msg, 0, sizeof(recv_msg));
-  return;
-}
-
-static int push_message_queue (const char * msg)
+static int push_message_queue (char * msg)
 {
   struct sh_msgbuf*   recv_msg = NULL;
   int              rc       = -1;
   static int       status   = -1;
+  int              error;
   int              count    = 0;
-#if defined(WITH_TPT) 
-  char errbuf[SH_ERRBUF_SIZE];
-#endif
 
   SL_ENTER(_("push_message_queue"));
@@ -169,10 +143,10 @@
     {
       TPT(( 0, FIL__, __LINE__, _("msg=<msg_queue not open>\n"))); 
-      status = open_queue();
+      status = open_ipc();
     }
 
   if (status < 0)
     {
-      TPT(( 0, FIL__, __LINE__, _("msg=<open_queue() failed>\n"))); 
+      TPT(( 0, FIL__, __LINE__, _("msg=<open_ipc() failed>\n"))); 
       SL_RETURN(-1, _("push_message_queue"));
     }
@@ -181,6 +155,6 @@
    *   long mtype;
    *   char mtext[1];  <-- sizeof(mtext) will be  1+MY_MAX_MSG
-   * } */
-
+   * }
+   */
   recv_msg = (struct sh_msgbuf*) SH_ALLOC(sizeof(struct sh_msgbuf)+MY_MAX_MSG);
   recv_msg->mtype = 1;
@@ -193,37 +167,45 @@
   if (count > 1)
     {
-      memset(recv_msg, 0, MY_MAX_MSG+1);
       SH_FREE(recv_msg);
       SL_RETURN(-1, _("push_message_queue"));
     }
 
-  do { errno = 0;
+  /* send the message
+   */ 
+  do {
+    errno = 0;
     rc = msgsnd(msgid, recv_msg, strlen(recv_msg->mtext)+1, IPC_NOWAIT);
-
-    if (rc == -1 && errno == EAGAIN)
-      remove_message();
-  } while (rc < 0 && (errno == EINTR && errno == EAGAIN));
+  }
+  while (rc < 0 && errno == EINTR);
   
   if (rc == -1 && errno != EAGAIN) 
     {
-      /* EIDRM is not in OpenBSD */
-      if (errno == EINVAL || errno == EIDRM) {
-	TPT(( 0, FIL__, __LINE__, _("msg=<msg_queue not open>\n"))); 
-	status = open_queue();
-	if (status == 0) {
-	  ++count;
-	  goto send_it; }
-      } else {
-	TPT(( 0, FIL__, __LINE__, _("msg=<msgsnd: %s> errno=<%d>\n"), 
-	      sh_error_message(errno, errbuf, sizeof(errbuf)), errno));
-	memset(recv_msg, 0, MY_MAX_MSG+1);
-	SH_FREE(recv_msg);
-	SL_RETURN(-1, _("push_message_queue"));
-      }
-    }
-
-  memset(recv_msg, 0, MY_MAX_MSG+1);
+      /* EIDRM is not in OpenBSD
+       */
+      if (errno == EINVAL
+#if defined(EIDRM)
+	  || errno == EIDRM
+#endif
+	  )
+	{
+	  TPT(( 0, FIL__, __LINE__, _("msg=<msg_queue not open>\n"))); 
+	  status = open_ipc();
+	  if (status == 0)
+	    {
+	      ++count;
+	      goto send_it;
+	    }
+	}
+      else
+	{
+	  error = errno;
+	  TPT(( 0, FIL__, __LINE__, _("msg=<msgsnd: %s> errno=<%d>\n"), 
+		sh_error_message(error), error));
+	  SH_FREE(recv_msg);
+	  SL_RETURN(-1, _("push_message_queue"));
+	}
+    }
+
   SH_FREE(recv_msg);
-
   SL_RETURN(0, _("push_message_queue"));
 }
@@ -240,25 +222,11 @@
 #endif
 
-#else /* no message queue */
-
-void close_ipc() { sh_sem_close(); return; }
-
 #endif
 
 static int count_dev_console = 0;
 
-typedef enum { SH_LOG_UNIX, SH_LOG_OTHER, SH_LOG_INDEF } sh_log_devtype;
-
-static sh_log_devtype dt[2] = { SH_LOG_INDEF, SH_LOG_INDEF };
-static int            st[2] = { SOCK_DGRAM, SOCK_DGRAM };
-
 void reset_count_dev_console(void)
 {
   count_dev_console = 0;
-  dt[0] = SH_LOG_INDEF;
-  dt[1] = SH_LOG_INDEF;
-  st[0] = SOCK_DGRAM;
-  st[1] = SOCK_DGRAM;
-  
   return;
 }
@@ -266,5 +234,5 @@
 /* ---- Set the console device. ----
  */
-int sh_log_set_console (const char * address)
+int sh_log_set_console (char * address)
 {
   SL_ENTER(_("sh_log_set_console"));
@@ -284,7 +252,7 @@
 
 #if defined(WITH_TRACE) || defined(WITH_TPT)
-char *  sh_log_console_name (void)
-{
-  if (sh.srvcons.name[0] == '\0' ||
+char *  sh_log_console_name ()
+{
+  if (! sh.srvcons.name || sh.srvcons.name[0] == '\0' ||
       0 == strcmp(sh.srvcons.name, _("NULL")))
     return (_("/dev/console"));
@@ -297,92 +265,11 @@
 #endif
 
-static int  find_socktype(const char * name)
-{
-#ifdef SOCK_SEQPACKET
-    int socktypes[3] = { SOCK_DGRAM, SOCK_SEQPACKET, SOCK_STREAM };
-    int try = 3;
-#else
-    int socktypes[2] = { SOCK_DGRAM, SOCK_STREAM };
-    int try = 2;
-#endif
-    int i;
-    for (i = 0; i < try; ++i) {
-	struct sockaddr_un addr;
-	int fd;
-    
-	if ( (fd = socket(AF_UNIX, socktypes[i], 0)) == -1) {
-	    return -1;
-	}
-	memset(&addr, 0, sizeof(addr));
-	addr.sun_family = AF_UNIX;
-	sl_strlcpy(addr.sun_path, name, sizeof(addr.sun_path));
-	if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == 0) {
-	    close(fd);
-	    return socktypes[i];
-	}
-	close(fd);
-    }
-    return -1;
-}
-
-int sh_log_console_open (const char * name, int slot)
-{
-  int fd = -1;
-
-  if (dt[slot] == SH_LOG_INDEF)
-    {
-      struct stat sb;
-      if (retry_stat(FIL__, __LINE__, name, &sb) == 0)
-	{
-	  if ((sb.st_mode & S_IFMT) == S_IFSOCK)
-	    {
-	      dt[slot] = SH_LOG_UNIX;
-	      st[slot] = find_socktype(name);
-	      if (st[slot] == -1) {
-	        sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		   _("Could not determine socket type."),  
-		   name);
-	      }
-	    }
-	  else
-	    dt[slot] = SH_LOG_OTHER;
-	}
-    }
-      
-  if (dt[slot] == SH_LOG_OTHER) {
-    fd = open ( name, O_WRONLY|O_APPEND|O_NOCTTY|O_NONBLOCK);
-  }
-  else if (dt[slot] == SH_LOG_UNIX && st[slot] != -1) {
-    struct sockaddr_un addr;
-    
-    if ( (fd = socket(AF_UNIX, st[slot], 0)) == -1) {
-	  char ebuf[SH_ERRBUF_SIZE];
-	  int  errnum = errno;
-	  sh_error_handle ((-1), FIL__, __LINE__, errnum, MSG_E_SUBGEN,
-	    sh_error_message(errnum, ebuf, sizeof(ebuf)),  
-	    name);
-      return -1;
-    }
-    memset(&addr, 0, sizeof(addr));
-    addr.sun_family = AF_UNIX;
-    sl_strlcpy(addr.sun_path, name, sizeof(addr.sun_path));
-    if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
-	  char ebuf[SH_ERRBUF_SIZE];
-	  int  errnum = errno;
-	  sh_error_handle ((-1), FIL__, __LINE__, errnum, MSG_E_SUBGEN,
-	    sh_error_message(errnum, ebuf, sizeof(ebuf)),
-	    name);
-      return -1;
-    }
-  }
-  return fd;
-}
-
 /* ---- Print out a message. ----
  */
-int  sh_log_console (const /*@null@*/char *errmsg)
+int  sh_log_console (/*@null@*/char *errmsg)
 {
   static int service_failure[2] = { 0, 0};
   int    fd[2] = { -1, -1};
+  int    sflags;
   int    cc;
   size_t len;
@@ -391,6 +278,6 @@
   /* static int logkey_seen = 0; */
   int    error;
+  struct sigaction sa_new, sa_old;
   static int blockMe = 0;
-  int    val_return;
 
   SL_ENTER(_("sh_log_console"));
@@ -411,16 +298,21 @@
 #endif
 
+  sa_new.sa_handler = SIG_IGN;
+  (void) sigemptyset(&sa_new.sa_mask);
+  sa_new.sa_flags   = 0;
+
+  /* Ignore SIGPIPE in case the console is actually a pipe.
+   */
+  (void) retry_sigaction(FIL__, __LINE__, SIGPIPE, &sa_new, &sa_old);
+ 
   if (sh.flag.isdaemon == S_FALSE || OnlyStderr == S_TRUE)
     {
       len = strlen(errmsg);
-      do {
-	val_return = write(STDERR_FILENO, errmsg, len);
-      } while (val_return < 0 && errno == EINTR); 
-      do {
-	val_return = write(STDERR_FILENO, "\n", 1);
-      } while (val_return < 0 && errno == EINTR); 
+      (void) write(STDERR_FILENO, errmsg, len);
+      (void) write(STDERR_FILENO, "\n", 1);
       /* 
        * fprintf (stderr, "%s\n", errmsg); 
        */
+      (void) retry_sigaction(FIL__, __LINE__, SIGPIPE, &sa_old, NULL);
       blockMe = 0;
       SL_RETURN(0, _("sh_log_console"));
@@ -429,12 +321,28 @@
   /* --- daemon && initialized ---
    */
-  if ( OnlyStderr == S_FALSE ) 
-    {
-      fd[0] = sh_log_console_open ( sh.srvcons.name, 0);
-
-      if (sh.srvcons.alt[0] != '\0')
+  if ( (OnlyStderr == S_FALSE) ) 
+    {
+      fd[0] = open ( sh.srvcons.name, O_WRONLY|O_APPEND|O_NOCTTY|O_NONBLOCK);
+      if (fd[0] >= 0) {
+	sflags = (int) retry_fcntl(FIL__, __LINE__, fd[0], F_GETFL, 0);
+	if (sflags >= 0)
+	  {
+	    (void) retry_fcntl(FIL__, __LINE__, fd[0], 
+			       F_SETFL, sflags & ~O_NONBLOCK);
+	  }
+      }
+
+      if (sh.srvcons.alt != NULL && sh.srvcons.alt[0] != '\0')
 	{
-	  fd[1] = sh_log_console_open (sh.srvcons.alt, 1);
-	  ccMax = 2;
+	  fd[1] = open (sh.srvcons.alt, O_WRONLY|O_APPEND|O_NOCTTY|O_NONBLOCK);
+	  if (fd[1] >= 0) {
+	    sflags = (int) retry_fcntl(FIL__, __LINE__, fd[1], F_GETFL, 0);
+	    if (sflags >= 0)
+	      {
+		(void) retry_fcntl(FIL__, __LINE__, fd[1], 
+				   F_SETFL, sflags & ~O_NONBLOCK);
+	      }
+	    ccMax = 2;
+	  }
 	}
 
@@ -453,13 +361,7 @@
 	  if (fd[cc] >= 0)
 	    {
-	      do {
-		val_return = write(fd[cc], errmsg, strlen(errmsg));
-	      } while (val_return < 0 && errno == EINTR);
-	      if (dt[cc] != SH_LOG_UNIX || st[cc] == SOCK_STREAM) {
-	        do {
-	          val_return = write(fd[cc], "\r\n",              2);
-	        } while (val_return < 0 && errno == EINTR);
-	      }
-	      (void) sl_close_fd(FIL__, __LINE__, fd[cc]);
+	      (void) write(fd[cc], errmsg, strlen(errmsg));
+	      (void) write(fd[cc], "\r\n",              2);
+	      (void) close(fd[cc]);
 	      service_failure[cc] = 0;
 	    }
@@ -469,4 +371,5 @@
     retval = 0;
 
+  (void) retry_sigaction(FIL__, __LINE__, SIGPIPE, &sa_old, NULL);
   blockMe = 0;
   SL_RETURN(retval, _("sh_log_console"));
Index: trunk/src/sh_err_log.c
===================================================================
--- trunk/src/sh_err_log.c	(revision 591)
+++ trunk/src/sh_err_log.c	(revision 1)
@@ -24,5 +24,4 @@
 #include <sys/types.h>
 #include <unistd.h>
-#include <time.h>
 
 #include "samhain.h"
@@ -70,5 +69,5 @@
   /* open the file, then check it 
    */
-  if ( SL_ISERROR(fd = sl_open_read (FIL__, __LINE__, path, SL_NOPRIV)))
+  if ( SL_ISERROR(fd = sl_open_read (path, SL_NOPRIV)))
     {
       fprintf(stderr, _("Could not open file <%s>\n"), path);
@@ -117,5 +116,5 @@
 static int just_list = S_FALSE;
 
-int sh_error_logverify_mod (const char * s)
+int sh_error_logverify_mod (char * s)
 {
   just_list = S_TRUE;
@@ -126,5 +125,5 @@
 } 
 
-int sh_error_logverify (const char * s)
+int sh_error_logverify (char * s)
 {
   SL_TICKET fd;
@@ -146,5 +145,4 @@
   char c_cont;
   int  chk_mode = CHK_KEY;
-  char hashbuf[KEYBUF_SIZE];
 
   sh_error_logoff();
@@ -164,5 +162,5 @@
     }
 
-  if ( SL_ISERROR(fd = sl_open_read (FIL__, __LINE__, s, SL_NOPRIV)) )
+  if ( SL_ISERROR(fd = sl_open_read (s, SL_NOPRIV)) )
     {
       fprintf(stderr, 
@@ -185,6 +183,6 @@
 #endif
 
-  buf  = (char *) SH_ALLOC( 2*SH_MSG_BUF+1 );
-  bufc = (char *) SH_ALLOC( 2*SH_MSG_BUF+1 );
+  buf  = (char *) SH_ALLOC( 2*SH_BUFSIZE+1 );
+  bufc = (char *) SH_ALLOC( 2*SH_BUFSIZE+1 );
 
   while (1 == 1) 
@@ -192,5 +190,5 @@
       /* get the log message
        */
-      if (sh_unix_getline (fd, buf, (2*SH_MSG_BUF)) < 0) 
+      if (sh_unix_getline (fd, buf, (2*SH_BUFSIZE)) < 0) 
 	break;
 
@@ -223,5 +221,5 @@
 	  start = 1;
 	  do {
-	    if ( sh_unix_getline (fd, buf, (2*SH_MSG_BUF)) < 0)
+	    if ( sh_unix_getline (fd, buf, (2*SH_BUFSIZE)) < 0)
 	      break;
 	  } while (buf[0] == '\0' || buf[0] == '\n');
@@ -294,5 +292,5 @@
 	{
 	  do {
-	    if ( sh_unix_getline (fd, bufc, (2*SH_MSG_BUF)) < 0)
+	    if ( sh_unix_getline (fd, bufc, (2*SH_BUFSIZE)) < 0)
 	      break;
 	  } while (bufc[0] == '\0' || bufc[0] == '\n');
@@ -302,7 +300,7 @@
 	      /* A continuation line. Add the newline. 
 	       */
-	      (void) sl_strlcat(buf, "\n", 2*SH_MSG_BUF+1);
+	      (void) sl_strlcat(buf, "\n", 2*SH_BUFSIZE+1);
 	      ++len;
-	      (void) sl_strlcat(buf, bufc, 2*SH_MSG_BUF+1);
+	      (void) sl_strlcat(buf, bufc, 2*SH_BUFSIZE+1);
 	      len += (int) sl_strlen(bufc);
 	    }
@@ -378,13 +376,12 @@
 	      key[0] = '\0';
 	      
-	      while (strlen(key) < KEY_LEN ) 
+	      while (sl_strlen(key) < KEY_LEN ) 
 		{ 
 		  if (key[0] != '\n' && key[0] != '\0')
-		    fprintf(stdout, "%s",_("New audit trail, enter key: "));
+		    fprintf(stdout, _("New audit trail, enter key: "));
 		  else if (key[0] == '\n')
 		    {
 		      (void) sl_strlcpy(key, 
-					sh_tiger_hash(NULL, TIGER_DATA, 0, 
-						      hashbuf, sizeof(hashbuf)), 
+					sh_tiger_hash(NULL, TIGER_DATA, 0), 
 					KEY_LEN+1);
 		      chk_mode = CHK_NON;
@@ -393,17 +390,15 @@
 		  (void) fflush(stdout); 
 		  key[0] = '\0';
-		  if (NULL != fgets(key, sizeof(key), stdin))
+		  (void) fgets(key, KEY_LEN+2, stdin);
+		  if (key[0] != '\n') 
 		    {
-		      if (key[0] != '\n') 
-			{
-			  if (key[strlen(key) - 1] == '\n')
-			    key[strlen(key) - 1] = '\0';
-			}
-		      if (key[0] == '/')
-			{
-			  chk_mode = CHK_FIL;
-			  (void) sl_strlcpy(path, key, KEY_LEN+1); 
-			  break;
-			}
+		      if (key[strlen(key) - 1] == '\n')
+			key[strlen(key) - 1] = '\0';
+		    }
+		  if (key[0] == '/')
+		    {
+		      chk_mode = CHK_FIL;
+		      (void) sl_strlcpy(path, key, KEY_LEN+1); 
+		      break;
 		    }
 		}
@@ -419,5 +414,5 @@
 		{
 		  chk_mode = CHK_KEY;
-		  fprintf(stdout, "%s",_("Key not found in file\n"));
+		  fprintf(stdout, _("Key not found in file\n"));
 		  goto findKey;
 		}
@@ -433,10 +428,9 @@
 	   */
 	  (void) sl_strlcpy (key, 
-			     sh_tiger_hash (key, TIGER_DATA, KEY_LEN,
-					    hashbuf, sizeof(hashbuf)), 
+			     sh_tiger_hash (key, TIGER_DATA, KEY_LEN), 
 			     KEY_LEN+1);
 	}
       
-      (void) sl_strlcat ( buf, key, 2*SH_MSG_BUF + 1);
+      (void) sl_strlcat ( buf, key, 2*SH_BUFSIZE + 1);
       
 #ifdef SH_STEALTH
@@ -446,6 +440,5 @@
       status = sl_strncmp (signature, 
 			   sh_tiger_hash (buf, TIGER_DATA, 
-					  (unsigned long) sl_strlen(buf),
-					  hashbuf, sizeof(hashbuf)),
+					  (unsigned long) sl_strlen(buf)),
 			   KEY_LEN);
       
@@ -541,5 +534,5 @@
   if (status == 0)
     {
-      fd = sl_open_write (FIL__, __LINE__, logfile, SL_YESPRIV);
+      fd = sl_open_write (logfile, SL_YESPRIV);
       if (SL_ISERROR(fd))
         {
@@ -560,7 +553,5 @@
         {
 	  tmp  = sh_util_safe_name (logfile);
-	  len      = sl_strlen(tmp);
-	  if (sl_ok_adds (6, len))
-	    len += 6;
+	  len      = 6 + sl_strlen(tmp);
 	  lockfile = SH_ALLOC(len);
 	  (void) sl_strlcpy(lockfile,        tmp, len);
@@ -598,6 +589,5 @@
         (*service_failure) = 1;
       }
-      if (tmp)
-	SH_FREE(tmp);
+      SH_FREE(tmp);
       SL_RETURN(-1, _("sh_log_open"));
     }
@@ -614,5 +604,5 @@
   char            sigkey_old[KEY_LEN+1];
   char            sigkey_new[KEY_LEN+1];
-  char            crypto[KEY_LEN+1];
+  char            crypt[KEY_LEN+1]; 
   struct  lfstc * next;
 } open_logfile;
@@ -623,5 +613,5 @@
 
 #ifdef SH_WITH_SERVER
-int set_flag_sep_log (const char * str)
+int set_flag_sep_log (char * str)
 {
   return sh_util_flagval(str, &flag_sep_log);
@@ -644,5 +634,5 @@
 
   SL_TICKET            fd = -1;
-  size_t               status;
+  long int             status;
   struct _sh_log_buf   log_msg;
 
@@ -652,6 +642,5 @@
   char               * sigkey_new;
   char               * sigkey_old;
-  char               * crypto;
-  char                 hashbuf[KEYBUF_SIZE];
+  char               * crypt;
 
   SL_ENTER(_("sh_log_file"));
@@ -667,5 +656,5 @@
 	  /* don't write second EOF mark
 	   */
-	  if (current->log_start != S_TRUE && sh.flag.islocked == GOOD)
+	  if (current->log_start != S_TRUE)
 	    {
 	      /* Don't use inet_peer == NULL, userwise a lock file will
@@ -743,7 +732,7 @@
       current->service_failure = 0;
       current->log_start       = S_TRUE;
-      memset(current->sigkey_old, 0, KEY_LEN+1);
-      memset(current->sigkey_new, 0, KEY_LEN+1);
-      memset(current->crypto,     0, KEY_LEN+1);
+      memset(current->sigkey_old, (int)'\0', KEY_LEN+1);
+      memset(current->sigkey_new, (int)'\0', KEY_LEN+1);
+      memset(current->crypt,      (int)'\0', KEY_LEN+1);
       current->next            = logfile_list;
       logfile_list             = current;
@@ -760,20 +749,14 @@
    */
 
-  status      =  sl_strlen (errmsg);
-  if (!sl_ok_adds(status, (2*KEY_LEN)) || !sl_ok_adds((2*KEY_LEN + status),32))
-    {
-      sl_close(fd);
-      SL_RETURN ((-1), _("sh_log_file"));
-    }
-      
+  status      = (long) sl_strlen (errmsg);
   log_msg.msg = (char *) SH_ALLOC ((size_t) (2*KEY_LEN + status + 32)); 
 
 #if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
-  if (skey->mlock_failed == S_FALSE) 
-    {
-      if ( (-1) == sh_unix_mlock( FIL__, __LINE__, log_msg.msg, 
+  if (skey->mlock_failed == SL_FALSE) 
+    {
+      if ( (-1) == sh_unix_mlock( log_msg.msg, 
 				  (size_t)(2*KEY_LEN + status + 32) ) ) 
 	{
-	  skey->mlock_failed = S_TRUE;
+	  skey->mlock_failed = SL_TRUE;
 #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
 	  sh_error_handle ((-1), FIL__, __LINE__, EPERM, MSG_MLOCK); 
@@ -782,7 +765,7 @@
     }
 #else
-  if (skey->mlock_failed == S_FALSE) 
-    {
-      skey->mlock_failed = S_TRUE;
+  if (skey->mlock_failed == SL_FALSE) 
+    {
+      skey->mlock_failed = SL_TRUE;
 #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
       sh_error_handle ((-1), FIL__, __LINE__, EPERM, MSG_MLOCK);
@@ -858,5 +841,5 @@
       sigkey_old = current->sigkey_old;
       sigkey_new = current->sigkey_new;
-      crypto     = current->crypto;
+      crypt      = current->crypt;
     }
   else
@@ -864,5 +847,5 @@
       sigkey_old = skey->sigkey_old;
       sigkey_new = skey->sigkey_new;
-      crypto     = skey->crypt;      /* flawfinder: ignore */
+      crypt      = skey->crypt;
     }
 
@@ -881,20 +864,19 @@
        */
       (void) sl_strlcpy(sigkey_new, 
-			sh_tiger_hash (sigkey_old, TIGER_DATA, KEY_LEN,
-				       hashbuf, sizeof(hashbuf)), 
+			sh_tiger_hash (sigkey_old, TIGER_DATA, KEY_LEN), 
 			KEY_LEN+1);
 
       /* Copy it to 'crypt' for encryption.
        */
-      (void) sl_strlcpy(crypto, sigkey_new, KEY_LEN+1);
+      (void) sl_strlcpy(crypt, sigkey_new, KEY_LEN+1);
 
       /* Use message and compiled-in key to encrypt.
        */
       BREAKEXIT(sh_util_encode);
-      sh_util_encode(crypto, log_msg.msg, 0, 'B');
+      sh_util_encode(crypt, log_msg.msg, 0, 'B');
 
       /* Send out the key.
        */
-      (void) sh_unix_time(0, log_msg.timestamp, KEY_LEN+1); 
+      (void) sl_strlcpy(log_msg.timestamp, sh_unix_time(0), KEY_LEN+1); 
 
       store1               = errFlags.loglevel;
@@ -918,6 +900,6 @@
 
       sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_KEY_MAIL,
-		       sh.prg_name, crypto, 
-		       crypto, log_msg.timestamp);
+		       sh.prg_name, crypt, 
+		       crypt, log_msg.timestamp);
 
       /* send to other allowed channels
@@ -931,5 +913,5 @@
 
       sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_KEY,
-		       sh.prg_name, crypto);
+		       sh.prg_name, crypt);
 
       /* Cleanup.
@@ -944,5 +926,5 @@
 
 
-      memset (crypto, 0, KEY_LEN);
+      memset (crypt, (int) '\0', KEY_LEN);
       sh.flag.log_start    = S_FALSE;  
       current->log_start   = S_FALSE;
@@ -952,6 +934,5 @@
       log_msg.timestamp[0] = '\0';
       (void) sl_strlcpy (sigkey_new, 
-			 sh_tiger_hash (sigkey_old, TIGER_DATA, KEY_LEN, 
-					hashbuf, sizeof(hashbuf)),
+			 sh_tiger_hash (sigkey_old, TIGER_DATA, KEY_LEN),
 			 KEY_LEN+1);
     }
@@ -959,35 +940,32 @@
   /* --- Sign the message with the signature key. ---
    */
-  sh_tiger_hash (log_msg.msg, TIGER_DATA,
-		 (unsigned long)(status + KEY_LEN), 
-		 (char *) hashbuf, (size_t) sizeof(hashbuf));
 
   (void) sl_strlcat (log_msg.msg, sigkey_new, (size_t)(status + KEY_LEN + 2));
+  
   (void) sl_strlcpy (log_msg.signature,
-		     sh_tiger_hash (log_msg.msg, (TigerType) TIGER_DATA,
-				    (unsigned long)(status + KEY_LEN), 
-				    hashbuf, sizeof(hashbuf)),
+		     sh_tiger_hash (log_msg.msg, TIGER_DATA, 
+				    (unsigned long)(status + KEY_LEN)), 
 		     KEY_LEN+1);
   (void) sl_strlcpy (sigkey_old, sigkey_new, KEY_LEN+1); 
 
-  /*@-usedef@*/
+  /*@-bufferoverflowhigh -usedef@*/
 #ifdef SH_USE_XML
   if (log_msg.timestamp[0] != '\0')
-    sl_snprintf(log_msg.sig, sizeof(log_msg.sig),
+    sprintf(log_msg.sig,                            /* known to fit  */
 #ifdef FIX_XML
-		_("\n<sig>%s%s</sig></log>\n"),          /* <sig> FIX XML */
-#else
-		_("\nsig>%s%s</sig></log>\n"),          /* <sig> FIX XML */
-#endif
-		log_msg.signature, log_msg.timestamp);
+	    _("\n<sig>%s%s</sig></log>\n"),          /* <sig> FIX XML */
+#else
+	    _("\nsig>%s%s</sig></log>\n"),          /* <sig> FIX XML */
+#endif
+	    log_msg.signature, log_msg.timestamp);
   else
-    sl_snprintf(log_msg.sig, sizeof(log_msg.sig),
+    sprintf(log_msg.sig,                            /* known to fit  */
 #ifdef FIX_XML
-		_("\n<sig>%s</sig></log>\n"),            /* <sig> FIX XML */
-#else
-		_("\nsig>%s</sig></log>\n"),            /* <sig> FIX XML */
-#endif
-		log_msg.signature);
-  /*@+usedef@*/
+	    _("\n<sig>%s</sig></log>\n"),            /* <sig> FIX XML */
+#else
+	    _("\nsig>%s</sig></log>\n"),            /* <sig> FIX XML */
+#endif
+	    log_msg.signature);
+  /*@+bufferoverflowhigh +usedef@*/
 
 #ifdef SH_STEALTH
@@ -1046,6 +1024,6 @@
   /* --- Clean up and free record. ---
    */
-  memset (log_msg.msg,       0, (size_t)(status + 2*KEY_LEN + 32));
-  memset (log_msg.signature, 0, KEY_LEN);
+  memset (log_msg.msg,       (int)'\0', (size_t)(status + 2*KEY_LEN + 32));
+  memset (log_msg.signature, (int)'\0', KEY_LEN);
   (void) sh_unix_munlock (log_msg.msg,  
 			  (size_t)(status + 2*KEY_LEN + 32));
@@ -1055,278 +1033,2 @@
 }
 
-/* >>>>>>>>>>>>>>>>>>>>>>>>>>>> efile <<<<<<<<<<<<<<<<<< */
-
-static char * gEfile = NULL;
-static int    gFail  = 0;
-static long   gGid   = 0;
-
-int sh_efile_group(const char * str)
-{
-  int  fail;
-  long gid = sh_group_to_gid(str, &fail);
-
-  if (fail < 0)
-    {
-      return -1;
-    }
-  gGid = gid;
-  return 0;
-}
-
-
-int sh_efile_path(const char * str) 
-{
-  if (!str || !strcmp(str, _("none")))
-    {
-      if (gEfile)
-	SH_FREE(gEfile);
-      gEfile = NULL;
-    }
-  else if (str[0] != '/')
-    {
-      return -1;
-    }
-  else
-    {
-      if (gEfile)
-	SH_FREE(gEfile);
-      gEfile = sh_util_strdup(str);
-    }
-  gFail = 0;
-  return 0;
-}
-
-/* write lock for filename
- */
-static int sh_efile_lock (char * filename, int flag)
-{
-  extern int get_the_fd (SL_TICKET ticket);
-  size_t len;
-  int    res = -1;
-  char myPid[64];
-  SL_TICKET  fd;
-  char * lockfile;
-  int    status;
-
-  sprintf (myPid, "%ld\n", (long) sh.pid);             /* known to fit  */
-
-  if (filename == NULL)
-    return res;
-
-  len = sl_strlen(filename);
-  if (sl_ok_adds(len, 6))
-    len += 6;
-  lockfile = SH_ALLOC(len);
-  sl_strlcpy(lockfile, filename,   len);
-  sl_strlcat(lockfile, _(".lock"), len);
-
-  if (  0 !=  (status = tf_trust_check (lockfile, SL_YESPRIV))
-	&& gFail == 0)
-    {
-      char * tmp  = sh_util_safe_name (lockfile);
-      sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_TRUST,
-			   (long) sh.effective.uid, tmp);
-      ++gFail;
-      SH_FREE(tmp);
-    }
-
-  if (status == 0)
-    {
-      if (flag == 0)
-	{
-	  /* --- Delete the lock file. --- 
-	   */
-	  res = retry_aud_unlink (FIL__, __LINE__, lockfile);
-	}
-      else
-	{
-	  unsigned int count = 0;
-
-	  /* fails if file exists 
-	   */
-	  do {
-	    fd = sl_open_safe_rdwr (FIL__, __LINE__, 
-				    lockfile, SL_YESPRIV);
-	    if (SL_ISERROR(fd))
-	      {
-		retry_msleep(0, 100);
-		++count;
-	      }
-
-	  } while (SL_ISERROR(fd) && count < 3);
-      
-	  if (!SL_ISERROR(fd))
-	    {
-	      int filed;
-
-	      res = sl_write (fd, myPid, sl_strlen(myPid));
-	      filed = get_the_fd(fd);
-	      fchmod (filed, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
-	      sl_close (fd);
-	    }
-	  else
-	    {
-	      static int nFail = 0;
-
-	      if (nFail == 0)
-		{
-		  char errmsg[1024];
-		  char * tmp  = sh_util_safe_name (lockfile);
-		  
-		  sl_snprintf(errmsg, sizeof(errmsg), 
-			      _("Error creating lockfile %s"),
-			      tmp);
-		  
-		  sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, 
-				   0, MSG_E_SUBGEN,
-				   errmsg, _("sh_efile_lock"));
-		  ++nFail;
-		  SH_FREE(tmp);
-		}
-	    }
-	}
-    }
-
-  SH_FREE(lockfile);
-  return res;
-}
-
-static size_t gSave[6] = { 0 };
-
-static void sh_efile_clear()
-{
-  int i;
-
-  for (i = 0; i < 6; ++i)
-    gSave[i] = 0;
-  return;
-}
-
-static void sh_efile_load(size_t * tmp)
-{
-  int i;
-
-  if (S_TRUE == sl_ok_adds (gSave[0], sh.statistics.bytes_hashed))
-    gSave[0] += sh.statistics.bytes_hashed;
-  if (S_TRUE == sl_ok_adds (gSave[1], sh.statistics.dirs_checked))
-    gSave[1] += sh.statistics.dirs_checked;
-  if (S_TRUE == sl_ok_adds (gSave[2], sh.statistics.files_checked))
-    gSave[2] += sh.statistics.files_checked;
-  if (S_TRUE == sl_ok_adds (gSave[3], sh.statistics.files_report))
-    gSave[3] += sh.statistics.files_report;
-  if (S_TRUE == sl_ok_adds (gSave[4], sh.statistics.files_error))
-    gSave[4] += sh.statistics.files_error;
-  if (S_TRUE == sl_ok_adds (gSave[5], sh.statistics.files_nodir))
-    gSave[5] += sh.statistics.files_nodir;
-
-  for (i = 0; i < 6; ++i)
-    tmp[i] = gSave[i];
-  return;
-}
-
-void sh_efile_report()
-{
-  extern int get_the_fd (SL_TICKET ticket);
-  SL_TICKET     fd;
-  char         *efile;
-  int           status = -1;
-
-  if (gEfile)
-    {
-      size_t tmp[6];
-
-      sh_efile_load(tmp);
-
-      efile = sh_util_strdup(gEfile);
-      
-      if (sh_efile_lock (efile, 1) < 0)
-	goto end;
-
-      if (  0 !=  (status = tf_trust_check (efile, SL_YESPRIV))
-	    && gFail == 0)
-	{
-	  char * tmp  = sh_util_safe_name (efile);
-	  sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_TRUST,
-			   (long) sh.effective.uid, tmp);
-	  ++gFail;
-	  SH_FREE(tmp);
-	}
-      
-      if (status == 0)
-	{
-	  fd = sl_open_write (FIL__, __LINE__, efile, SL_YESPRIV);
-
-	  if (!SL_ISERROR(fd))
-	    {
-	      char report[511];
-	      char tstamp[TIM_MAX];
-
-	      time_t now = time(NULL);
-	      int  filed = get_the_fd(fd);
-
-	      (void) sh_unix_time (now, tstamp, sizeof(tstamp));
-#ifdef HAVE_LONG_LONG
-	      sl_snprintf(report, sizeof(report), 
-			  _("%s %lld %ld %ld %ld %ld %ld %ld\n"),
-			  tstamp,
-			  (long long) now,
-			  (long) tmp[0], (long) tmp[1], (long) tmp[2], 
-			  (long) tmp[3], (long) tmp[4], (long) tmp[5]);
-#else
-	      sl_snprintf(report, sizeof(report), 
-			  _("%s %ld %ld %ld %ld %ld %ld %ld\n"),
-			  tstamp,
-			  (long) now,
-			  (long) tmp[0], (long) tmp[1], (long) tmp[2], 
-			  (long) tmp[3], (long) tmp[4], (long) tmp[5]);
-#endif
-			  
-	      status = sl_forward(fd);
-	      if (!SL_ISERROR(status))
-		sl_write (fd, report,  strlen(report));
-	      (void) sl_sync(fd);
-
-	      /* make group writeable, such that nagios can truncate */
-	      fchmod (filed, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
-	      status = fchown (filed, -1, gGid);
-	      if (status < 0)
-		{
-		  int  errnum = errno;
-		  static int nFail = 0;
-		  if (nFail == 0)
-		    {
-		      char errmsg[1024];
-		      char buf[256];
-		      char * tmp  = sh_util_safe_name (efile);
-
-		      sl_snprintf(errmsg, sizeof(errmsg), 
-				  _("Error changing group of %s to %ld: %s"),
-				  tmp, gGid, 
-				  sh_error_message (errnum, buf, sizeof(buf)));
-		      sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, 
-				       errnum, MSG_E_SUBGEN,
-				       errmsg, _("sh_efile_report"));
-		      ++nFail;
-		      SH_FREE(tmp);
-		    }
-		}
-
-	      (void) sl_close(fd);
-	    }
-	  else
-	    {
-	      status = -1;
-	    }
-	}
-  
-      (void) sh_efile_lock (efile, 0);
-    end:
-      SH_FREE(efile);
-
-      if (!SL_ISERROR(status))
-	{
-	  sh_efile_clear();
-	}
-    }
-  return;
-}
Index: trunk/src/sh_err_syslog.c
===================================================================
--- trunk/src/sh_err_syslog.c	(revision 591)
+++ trunk/src/sh_err_syslog.c	(revision 1)
@@ -31,5 +31,5 @@
 
 typedef struct log_fac_struct {
-  const char * name;
+  char * name;
   int    facility;
 } logfct;
@@ -110,5 +110,5 @@
 /* set syslog facility 
  */
-int  sh_log_set_facility (const char * c)
+int  sh_log_set_facility (char * c)
 {
   int loop = 0; 
@@ -131,26 +131,5 @@
 }
   
-static int sh_stamp_priority = LOG_ERR;
-
-/* set priority for heartbeat messages
- */
-int  sh_log_set_stamp_priority (const char * c)
-{
-  int retval = 0;
-
-  if      (0 == strcmp(c, _("LOG_DEBUG")))   { sh_stamp_priority = LOG_DEBUG; }
-  else if (0 == strcmp(c, _("LOG_INFO")))    { sh_stamp_priority = LOG_INFO;  }
-  else if (0 == strcmp(c, _("LOG_NOTICE")))  { sh_stamp_priority = LOG_NOTICE;}
-  else if (0 == strcmp(c, _("LOG_WARNING"))) { sh_stamp_priority = LOG_WARNING;}
-  else if (0 == strcmp(c, _("LOG_ERR")))     { sh_stamp_priority = LOG_ERR;   }
-  else if (0 == strcmp(c, _("LOG_CRIT")))    { sh_stamp_priority = LOG_CRIT;  }
-  else if (0 == strcmp(c, _("LOG_ALERT")))   { sh_stamp_priority = LOG_ALERT; }
-#ifdef LOG_EMERG
-  else if (0 == strcmp(c, _("LOG_EMERG")))   { sh_stamp_priority = LOG_EMERG; }
-#endif
-  else { retval = -1; }
-
-  return retval;
-}  
+  
 
 /* syslog error message
@@ -175,5 +154,5 @@
   else if (severity == SH_ERR_NOTICE) priority = LOG_NOTICE;
   else if (severity == SH_ERR_WARN)   priority = LOG_WARNING;
-  else if (severity == SH_ERR_STAMP)  priority = sh_stamp_priority;
+  else if (severity == SH_ERR_STAMP)  priority = LOG_ERR;
   else if (severity == SH_ERR_ERR)    priority = LOG_ERR;
   else if (severity == SH_ERR_SEVERE) priority = LOG_CRIT;
Index: trunk/src/sh_error.c
===================================================================
--- trunk/src/sh_error.c	(revision 591)
+++ trunk/src/sh_error.c	(revision 1)
@@ -20,17 +20,8 @@
 #include "config_xor.h"
 
-/* Required on Linux to get the correct strerror_r function. Also
- * for recursive mutexes (_XOPEN_SOURCE >= 500). Gives funny error
- * on Solaris 10/gcc ('c99' compiler required - huh? Isn't gcc 
- * good enough?).
- */
-#if !defined(__sun__) && !defined(__sun)
-#define _XOPEN_SOURCE 600
-#undef  _GNU_SOURCE
-#endif
-#include <string.h>
 #include <stdio.h>     
 #include <stdlib.h>     
 #include <stdarg.h>
+#include <string.h>
 #include <ctype.h>
 #include <limits.h>
@@ -59,8 +50,7 @@
 #include "sh_unix.h"
 #include "sh_tiger.h"
-#include "sh_nmail.h"
-#include "sh_xfer.h"
+#include "sh_mail.h"
+#include "sh_forward.h"
 #include "sh_prelude.h"
-#include "sh_pthread.h"
 
 #if defined(WITH_DATABASE)
@@ -77,6 +67,6 @@
 extern int clt_class;
 
-int flag_err_debug = S_FALSE;
-int flag_err_info  = S_FALSE;
+int flag_err_debug = SL_FALSE;
+int flag_err_info  = SL_FALSE;
 
 int  ShDFLevel[SH_ERR_T_END];
@@ -100,11 +90,11 @@
 static int  sh_error_init (void);
 
-inline
-static const char * get_format(unsigned long msg_id, int * priority, 
-			       unsigned int * class);
+/*@owned@*//*@null@*/inline
+static char * get_format(unsigned long msg_id, /*@out@*/int * priority, 
+			 /*@out@*/unsigned int * class);
 
 static int sh_error_string (struct _log_t * lmsg, va_list vl);
 
-extern int  sh_log_console (/*@null@*/const char *message);
+extern int  sh_log_console (/*@null@*/char *message);
 extern int  sh_log_syslog  (int  severity, /*@null@*/char *message);
 extern int  sh_log_file    (/*@null@*/char *message, 
@@ -112,5 +102,5 @@
 /* convert a string to a numeric priority
  */ 
-int sh_error_convert_level (const char * str_s);
+int sh_error_convert_level (char * str_s);
 
 static int  IsInitialized = BAD;
@@ -120,7 +110,4 @@
 int  OnlyStderr    = S_TRUE; 
 
-/* --- Enable facilities not safe for closeall(). --- 
- */
-int  enableUnsafe  = S_FALSE;
 
 /*********************************************
@@ -128,12 +115,11 @@
  *********************************************/
 
-int sh_error_verify (const char * s)
+int sh_error_verify (char * s)
 {
   char * foo;
-  char hashbuf[KEYBUF_SIZE];
 
   if (s[0] == '/')
     {
-      foo = sh_tiger_hash_gpg (s, TIGER_FILE, TIGER_NOLIM);
+      foo = sh_tiger_hash_gpg (s, TIGER_FILE, 0);
       fprintf (stdout, _("%s\n"),  foo);
       SH_FREE(foo);
@@ -143,7 +129,5 @@
       fprintf (stdout, _("string=<%s>, hash=<%s>\n"), 
 	       s, sh_tiger_hash (s, TIGER_DATA, 
-				 (unsigned long) sl_strlen(s), 
-				 hashbuf, sizeof(hashbuf))
-	       );
+				 (unsigned long) sl_strlen(s)));
     }
   (void) fflush(stdout);
@@ -164,62 +148,56 @@
 }
 
-void sh_error_enable_unsafe (int flag)
-{
-  enableUnsafe    = flag;
-  return;
-}
-
 static int dbg_store = 0;
 static int dbg_flag  = 0;
 
 static
-void compute_flag_err_debug(void)
+void compute_flag_err_debug()
 {
   if ((errFlags.loglevel & SH_ERR_ALL) != 0)
-    flag_err_debug = S_TRUE;
+    flag_err_debug = SL_TRUE;
   else if ((errFlags.printlevel & SH_ERR_ALL) != 0)
-    flag_err_debug = S_TRUE;
+    flag_err_debug = SL_TRUE;
   else if ((errFlags.maillevel & SH_ERR_ALL) != 0)
-    flag_err_debug = S_TRUE;
+    flag_err_debug = SL_TRUE;
   else if ((errFlags.exportlevel & SH_ERR_ALL) != 0)
-    flag_err_debug = S_TRUE;
+    flag_err_debug = SL_TRUE;
   else if ((errFlags.sysloglevel & SH_ERR_ALL) != 0)
-    flag_err_debug = S_TRUE;
+    flag_err_debug = SL_TRUE;
   else if ((errFlags.externallevel & SH_ERR_ALL) != 0)
-    flag_err_debug = S_TRUE;
+    flag_err_debug = SL_TRUE;
   else if ((errFlags.databaselevel & SH_ERR_ALL) != 0)
-    flag_err_debug = S_TRUE;
+    flag_err_debug = SL_TRUE;
   else if ((errFlags.preludelevel & SH_ERR_ALL) != 0)
-    flag_err_debug = S_TRUE;
+    flag_err_debug = SL_TRUE;
   else
-    flag_err_debug = S_FALSE;
+    flag_err_debug = SL_FALSE;
   return;
 }
 
 static
-void compute_flag_err_info(void)
+void compute_flag_err_info()
 {
   if ((errFlags.loglevel & SH_ERR_INFO) != 0)
-    flag_err_info = S_TRUE;
+    flag_err_info = SL_TRUE;
   else if ((errFlags.printlevel & SH_ERR_INFO) != 0)
-    flag_err_info = S_TRUE;
+    flag_err_info = SL_TRUE;
   else if ((errFlags.maillevel & SH_ERR_INFO) != 0)
-    flag_err_info = S_TRUE;
+    flag_err_info = SL_TRUE;
   else if ((errFlags.exportlevel & SH_ERR_INFO) != 0)
-    flag_err_info = S_TRUE;
+    flag_err_info = SL_TRUE;
   else if ((errFlags.sysloglevel & SH_ERR_INFO) != 0)
-    flag_err_info = S_TRUE;
+    flag_err_info = SL_TRUE;
   else if ((errFlags.externallevel & SH_ERR_INFO) != 0)
-    flag_err_info = S_TRUE;
+    flag_err_info = SL_TRUE;
   else if ((errFlags.databaselevel & SH_ERR_INFO) != 0)
-    flag_err_info = S_TRUE;
+    flag_err_info = SL_TRUE;
   else if ((errFlags.preludelevel & SH_ERR_INFO) != 0)
-    flag_err_info = S_TRUE;
+    flag_err_info = SL_TRUE;
   else
-    flag_err_info = S_FALSE;
+    flag_err_info = SL_FALSE;
   return;
 }
 
-void sh_error_dbg_switch(void)
+void sh_error_dbg_switch()
 {
   if (dbg_flag == 0)
@@ -230,5 +208,5 @@
 			     SH_ERR_SEVERE | SH_ERR_FATAL);
       dbg_flag  = 1;
-      flag_err_debug = S_TRUE;
+      flag_err_debug = SL_TRUE;
     }
   else {
@@ -241,16 +219,14 @@
 }
 
-static int sh_error_set_classmask (const char * str, int * facility_mask)
+static int sh_error_set_classmask (/*@notnull@*/char * c, int * facility_mask)
 {
   char * p;
-  char * q;
   int    num = 0;
   unsigned int    i;
   size_t len;
-  char * c;
 
   SL_ENTER(_("sh_error_set_classmask"));
   
-  if (str == NULL)
+  if (c == NULL)
     SL_RETURN( -1, _("sh_error_set_classmask"));
 
@@ -258,11 +234,7 @@
     (void) sh_error_init();
 
-  if (str[0] == (char) 34)
-    ++str;
-  len = strlen(str);
-
-  c = SH_ALLOC(len+1);
-  sl_strlcpy(c, str, len+1);
-
+  if (c[0] == (char) 34)
+    ++c;
+  len = strlen(c);
   if (c[len-1] == (char) 34)
     c[len-1] = '\0';
@@ -271,25 +243,14 @@
 
   do {
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
-    char * saveptr;
-    if (num == 0) {
-      p = strtok_r (c, " ,\t", &saveptr);
-      ++num;
-    } else {
-      p = strtok_r (NULL, " ,\t", &saveptr);
-    }
-#else
-    if (num == 0) {
-      p = strtok (c, " ,\t");
-      ++num;
-    } else {
+    if (num == 0)
+      {
+	p = strtok (c, " ,\t");
+	++num;
+      }
+    else
       p = strtok (NULL, " ,\t");
-    }
-#endif
 
     if (p == NULL)
       break;
-
-    q = p; while (*q != '\0') { *q = toupper( (int) *q); ++q; }
 
     for (i = 0; i < SH_CLA_MAX; ++i)
@@ -312,37 +273,36 @@
   } while (p);
 
-  SH_FREE(c);
   SL_RETURN( 0, _("sh_error_set_classmask"));
 }
 
-int sh_error_log_mask (const char * c)
+int sh_error_log_mask (char * c)
 {
   return (sh_error_set_classmask(c, &(errFlags.log_class)));
 }
-int sh_error_mail_mask (const char * c)
+int sh_error_mail_mask (char * c)
 {
   return (sh_error_set_classmask(c, &(errFlags.mail_class)));
 }
-int sh_error_print_mask (const char * c)
+int sh_error_print_mask (char * c)
 {
   return (sh_error_set_classmask(c, &(errFlags.print_class)));
 }
-int sh_error_export_mask (const char * c)
+int sh_error_export_mask (char * c)
 {
   return (sh_error_set_classmask(c, &(errFlags.export_class)));
 }
-int sh_error_syslog_mask (const char * c)
+int sh_error_syslog_mask (char * c)
 {
   return (sh_error_set_classmask(c, &(errFlags.syslog_class)));
 }
-int sh_error_external_mask (const char * c)
+int sh_error_external_mask (char * c)
 {
   return (sh_error_set_classmask(c, &(errFlags.external_class)));
 }
-int sh_error_database_mask (const char * c)
+int sh_error_database_mask (char * c)
 {
   return (sh_error_set_classmask(c, &(errFlags.database_class)));
 }
-int sh_error_prelude_mask (const char * c)
+int sh_error_prelude_mask (char * c)
 {
   return (sh_error_set_classmask(c, &(errFlags.prelude_class)));
@@ -351,100 +311,92 @@
 
 
-char * sh_error_message (int tellme, char * str, size_t len)
-{
-
-#if defined(HAVE_STRERROR_R)
-  if (len > 0) str[0] = '\0';
-  strerror_r(tellme, str, len);
-  return str;
-#elif defined(HAVE_STRERROR)
-  sl_strlcpy(str, strerror(tellme), len);
-  return str;
+/*@owned@*/char * sh_error_message (int tellme)
+{
+
+#if defined(HAVE_STRERROR)
+  /*@i@*/return strerror(tellme);
 #else
 
-  char *p = NULL;
 #ifdef EACCES
-    if (tellme == EACCES)  p = _("Permission denied.");
+    if (tellme == EACCES)  return _("Permission denied.");
 #endif
 #ifdef EAGAIN
-    if (tellme == EAGAIN)  p = _("Try again.");
+    if (tellme == EAGAIN)  return _("Try again.");
 #endif
 #ifdef EBADF
-    if (tellme == EBADF)   p = _("File descriptor in bad state.");
+    if (tellme == EBADF)   return _("File descriptor in bad state.");
 #endif
 #ifdef EEXIST
-    if (tellme == EEXIST)  p = _("File exists.");
+    if (tellme == EEXIST)  return _("File exists.");
 #endif
 #ifdef EFAULT
-    if (tellme == EFAULT)  p = _("Bad address.");
+    if (tellme == EFAULT)  return _("Bad address.");
 #endif
 #ifdef EINVAL
-    if (tellme == EINVAL)  p = _("Invalid argument.");
+    if (tellme == EINVAL)  return _("Invalid argument.");
 #endif
 #ifdef EISDIR
-    if (tellme == EISDIR)  p = _("Is a directory.");
+    if (tellme == EISDIR)  return _("Is a directory.");
 #endif
 #ifdef EINTR
-    if (tellme == EINTR)   p = _("System call was interrupted.");
+    if (tellme == EINTR)   return _("System call was interrupted.");
 #endif
 #ifdef EIO
-    if (tellme == EIO)     p = _("Low-level I/O error.");
+    if (tellme == EIO)     return _("Low-level I/O error.");
 #endif
 #ifdef ELOOP
-    if (tellme == ELOOP)   p = _("Too many symbolic links encountered.");
+    if (tellme == ELOOP)   return _("Too many symbolic links encountered.");
 #endif
 #ifdef EMFILE
-    if (tellme == EMFILE)  p = _("Too many open files.");
+    if (tellme == EMFILE)  return _("Too many open files.");
 #endif
 #ifdef EMLINK
-    if (tellme == EMLINK)  p = _("Too many links.");
+    if (tellme == EMLINK)  return _("Too many links.");
 #endif
 #ifdef ENAMETOOLONG
     if (tellme == ENAMETOOLONG) 
-                           p = _("File name too long."); 
+                           return _("File name too long."); 
 #endif
 #ifdef ENFILE
-    if (tellme == ENFILE)  p = _("File table overflow.");
+    if (tellme == ENFILE)  return _("File table overflow.");
 #endif
 #ifdef ENOENT
-    if (tellme == ENOENT)  p = _("File does not exist.");
+    if (tellme == ENOENT)  return _("File does not exist.");
 #endif
 #ifdef ENOMEM
-    if (tellme == ENOMEM)  p = _("Out of memory.");
+    if (tellme == ENOMEM)  return _("Out of memory.");
 #endif
 #ifdef ENOSPC
-    if (tellme == ENOSPC)  p = _("No space on device.");
+    if (tellme == ENOSPC)  return _("No space on device.");
 #endif
 #ifdef ENOTDIR
-    if (tellme == ENOTDIR) p = _("Not a directory.");
+    if (tellme == ENOTDIR) return _("Not a directory.");
 #endif
 #ifdef ENOTSOCK
-    if (tellme == ENOTSOCK) p = _("Not a socket.");
+    if (tellme == ENOTSOCK) return _("Not a socket.");
 #endif
 #ifdef EOPNOTSUPP
-    if (tellme == EOPNOTSUPP) p = _("Socket is not of type SOCK_STREAM.");
+    if (tellme == EOPNOTSUPP) return _("Socket is not of type SOCK_STREAM.");
 #endif
 #ifdef EPERM
-    if (tellme == EPERM)   p = _("Permission denied.");
+    if (tellme == EPERM)   return _("Permission denied.");
 #endif
 #ifdef EPIPE
-    if (tellme == EPIPE)   p = _("No read on pipe.");
+    if (tellme == EPIPE)   return _("No read on pipe.");
 #endif
 #ifdef EROFS
-    if (tellme == EROFS)    p = _("Read-only file system.");
+    if (tellme == EROFS)    return _("Read-only file system.");
 #endif
 #ifdef ETXTBSY
-    if (tellme == ETXTBSY) p = _("Text file busy.");
+    if (tellme == ETXTBSY) return _("Text file busy.");
 #endif
 #ifdef EWOULDBLOCK
     if (tellme == EWOULDBLOCK) 
-      p = _("No connections on non-blocking socket.");
+      return _("No connections on non-blocking socket.");
 #endif
 #ifdef EXDEV
-    if (tellme == EXDEV)    p = _("Not on same file system.");
-#endif
-    if (!p) p = _("Unknown error");
-    sl_strlcpy(str, p, len);
-    return str;
+    if (tellme == EXDEV)    return _("Not on same file system.");
+#endif
+    return _("Unknown error");
 #endif /* ifndef HAVE_STRERROR */
 }
@@ -471,5 +423,5 @@
 typedef struct eef 
 {
-  const char * str;
+  char * str;
   int    val;
 } eef_struc;
@@ -494,10 +446,8 @@
 };
 
-int sh_error_convert_level (const char * str_s)
+int sh_error_convert_level (char * str_s)
 {
   int i;
   int level = (-1);
-  char * tmp;
-  char * q;
   
   SL_ENTER(_("sh_error_convert_level"));
@@ -506,10 +456,7 @@
      SL_RETURN( -1, _("sh_error_convert_level"));
 
-  q = sh_util_strdup(str_s);
-  tmp = q; while (*tmp != '\0') { *tmp = tolower( (int) *tmp); ++tmp; }
-  
   for (i = 0; i < SH_EEF_MAX; ++i)
     {
-      if (0 == sl_strncmp(q, _(eef_tab[i].str), 
+      if (0 == sl_strncmp(str_s, _(eef_tab[i].str), 
                           sl_strlen(eef_tab[i].str))) 
 	{
@@ -519,5 +466,4 @@
     }
 
-  SH_FREE(q);
   SL_RETURN( level, _("sh_error_convert_level"));
 }
@@ -526,5 +472,5 @@
 /* --- Set severity levels. ---
  */
-int sh_error_set_iv (int iv, const char *  str_s)
+int sh_error_set_iv (int iv, char *  str_s)
 {
   int level = (-1);
@@ -558,20 +504,13 @@
 }
 
-int sh_error_set_level(const char * str_in, int * facility)
+int sh_error_set_level(char * str_in, int * facility)
 {
   register int  i, j, f = BAD;
 
   int  old_facility;
-  char * str_s;
-  char * str_orig;
-  char * tmp;
+  char * str_s = str_in;
 
   SL_ENTER(_("sh_error_set_level"));
 
-  str_s = sh_util_strdup(str_in);
-  str_orig = str_s;
-  
-  tmp = str_s; while (*tmp != '\0') { *tmp = tolower( (int) *tmp); ++tmp; }
-  
   if (IsInitialized == BAD) 
     (void) sh_error_init();
@@ -664,9 +603,4 @@
     }
 
-  if (!str_s)
-    {
-      if (str_orig) SH_FREE(str_orig);
-      SL_RETURN ((-1), _("sh_error_set_level"));
-    }
   /* skip to end of string
    */
@@ -692,17 +626,15 @@
       sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS, 
 		       _("priority"), str_in);
-      SH_FREE(str_orig);
       SL_RETURN (-1, _("sh_error_set_level"));
     }
   compute_flag_err_debug();
   compute_flag_err_info();
-  SH_FREE(str_orig);
   SL_RETURN (0, _("sh_error_set_level"));
 }
 
-#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
+#ifdef SH_WITH_CLIENT
 /* set severity for TCP export
  */
-int sh_error_setexport(const char *  str_s)
+int sh_error_setexport(char *  str_s)
 {
   static int reject = 0;
@@ -721,5 +653,5 @@
 extern void dlog_set_active(int flag);
 
-int sh_error_setprint(const char *  str_s)
+int sh_error_setprint(char *  str_s)
 {
   static int reject = 0;
@@ -744,5 +676,5 @@
 /* set level for error logging
  */
-int sh_error_setlog(const char * str_s)
+int sh_error_setlog(char * str_s)
 {
   static int reject = 0;
@@ -759,5 +691,5 @@
 /* set severity for syslog
  */
-int sh_error_set_syslog (const char * str_s)
+int sh_error_set_syslog (char * str_s)
 {
   static int reject = 0;
@@ -774,5 +706,5 @@
 /* set severity for external
  */
-int sh_error_set_external (const char * str_s)
+int sh_error_set_external (char * str_s)
 {
   static int reject = 0;
@@ -790,5 +722,5 @@
 /* set severity for database
  */
-int sh_error_set_database (const char * str_s)
+int sh_error_set_database (char * str_s)
 {
   static int reject = 0;
@@ -806,5 +738,5 @@
 /* set severity for prelude
  */
-int sh_error_set_prelude (const char * str_s)
+int sh_error_set_prelude (char * str_s)
 {
   static int reject = 0;
@@ -822,7 +754,7 @@
 /* init or re-init log facilities that need it
  */
-void sh_error_fixup(void)
-{
-#if defined(HAVE_LIBPRELUDE)
+void sh_error_fixup()
+{
+#if defined(HAVE_LIBPRELUDE_9)
   if ((errFlags.preludelevel & SH_ERR_NOT)   == 0)
     sh_prelude_init();
@@ -838,7 +770,7 @@
 /* to be called from sh_prelude_reset
  */
-void sh_error_init_prelude(void)
-{
-#if defined(HAVE_LIBPRELUDE)
+void sh_error_init_prelude()
+{
+#if defined(HAVE_LIBPRELUDE_9)
   if ((errFlags.preludelevel & SH_ERR_NOT)   == 0)
     sh_prelude_init();
@@ -852,5 +784,5 @@
 /* set severity for mailing
  */
-int sh_error_setseverity (const char * str_s)
+int sh_error_setseverity (char * str_s)
 {
   static int reject = 0;
@@ -866,70 +798,14 @@
 #ifdef SH_WITH_SERVER
 static char inet_peer[SH_MINIBUF] = { '\0' };
-#ifdef HAVE_LIBPRELUDE
-static char inet_peer_ip[SH_IP_BUF] = { '\0' };
-
-void sh_error_set_peer_ip(const char * str)
+
+void sh_error_set_peer(const char * str)
 {
   if (str == NULL)
-    inet_peer_ip[0] = '\0';
+    memset(inet_peer, '\0', SH_MINIBUF);
   else
-    sl_strlcpy(inet_peer_ip, str, sizeof(inet_peer_ip));
-}
-#endif
-
-void sh_error_set_peer(const char * str)
-{
-  if (str == NULL)
-    inet_peer[0] = '\0';
-  else
-    sl_strlcpy(inet_peer, str, sizeof(inet_peer));
-}
-#endif
-
-#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
-#include "sh_checksum.h"
-static char * sh_error_replace(const char * msg)
-{
-  char * ret   = NULL;
-
-  if (sh_tiger_get_hashtype () == SH_SHA256)
-    {
-      char * store = NULL;
-
-#ifdef SH_USE_XML
-      char c_end  = '"';
-      char * str  = _("chksum_old=\"");
-      char * str2 = _("chksum_new=\"");
-#else
-      char c_end  = '>';
-      char * str  = _("chksum_old=<");
-      char * str2 = _("chksum_new=<");
-#endif
-
-      ret = SHA256_ReplaceBaseByHex(msg, str, c_end);
-
-      if (ret) {
-	store = ret;
-	ret   = SHA256_ReplaceBaseByHex(ret, str2, c_end);
-	if (ret)
-	  SH_FREE(store);
-	else
-	  ret = store;
-      } else {
-	ret   = SHA256_ReplaceBaseByHex(msg, str2, c_end);
-      }
-    }
-  return ret;
-}
-static void sh_replace_free(char * msg)
-{
-  if (msg)
-    SH_FREE(msg);
-  return;
-}
-#else
-static char * sh_error_replace(const char * msg) { (void) msg; return NULL; }
-static void sh_replace_free(char * msg) { (void) msg; return; }
-#endif
+    sl_strlcpy(inet_peer, str, SH_MINIBUF);
+}
+#endif
+  
 
 /**********************************************************
@@ -944,7 +820,5 @@
  **********************************************************/
 
-SH_MUTEX_RECURSIVE(mutex_err_handle);
-
-void sh_error_handle (int sev1, const char * file, long line, 
+void sh_error_handle (int sev, char * file, long line, 
 		      long status, unsigned long msg_id, ...)
 {
@@ -954,18 +828,14 @@
   int    severity;
   unsigned int class;
-  const char * fmt;
-  volatile int sev = sev1;            /* Avoids the 'clobbered by longjmp' warning. */
-
-  int    flag_inet;
+  char * fmt;
+
+  int    flag_inet  = S_FALSE;
+  int    class_inet = clt_class;      /* initialize from global */
 
 #ifdef SH_WITH_SERVER
-  int    class_inet = clt_class;      /* initialize from global */
   char   local_inet_peer[SH_MINIBUF];
-#ifdef HAVE_LIBPRELUDE
-  char   local_inet_peer_ip[SH_IP_BUF];
-#endif    
-#endif
-
-#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
+#endif
+
+#ifdef SH_WITH_CLIENT
   char   * ex_msg;
 #endif
@@ -973,6 +843,4 @@
   char   * escape_msg;
 #endif
-
-  char   * hexmsg = NULL;
 
   static int    own_block = 0;
@@ -988,5 +856,5 @@
   static int syslog_block = 0;
   static int log_block    = 0;
-#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
+#if defined(SH_WITH_CLIENT)
   static int export_block = 0;
 #endif
@@ -1003,7 +871,4 @@
   SL_ENTER(_("sh_error_handle"));
 
-  SH_MUTEX_RECURSIVE_INIT(mutex_err_handle);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_err_handle);
-
 #ifdef SH_WITH_SERVER
   /* copy the global string into a local array
@@ -1011,45 +876,34 @@
   if ((msg_id == MSG_TCP_MSG) && (inet_peer[0] != '\0'))
     {
-      sl_strlcpy(local_inet_peer, inet_peer, sizeof(local_inet_peer));
+      sl_strlcpy(local_inet_peer, inet_peer, SH_MINIBUF);
       sh_error_set_peer(NULL);
     }
   else
     local_inet_peer[0] = '\0';
-
-#ifdef HAVE_LIBPRELUDE
-  if ((msg_id == MSG_TCP_MSG) && (inet_peer_ip[0] != '\0'))
-    {
-      sl_strlcpy(local_inet_peer_ip, inet_peer_ip, sizeof(local_inet_peer_ip));
-      sh_error_set_peer_ip(NULL);
-    }
-  else
-    local_inet_peer_ip[0] = '\0';
 #endif
 
   clt_class = (-1);      /* reset global */
-#endif
-
 
   if (own_block == 1)
     {
-      goto exit_here;
+      SL_RET0(_("sh_error_handle"));
     }
 
   /* --- Initialize to default values. ---
    */
+  own_block = 1;
   if (IsInitialized == BAD) 
     (void) sh_error_init();
-
-  /* Returns pointer to (constant|thread-specific) static memory
-   */
+  own_block = 0;
+
+  /* --- Consistency checks. ---
+   */
+  own_block = 1;
   fmt = /*@i@*/get_format (msg_id, &severity, &class);
-
-#ifdef SH_WITH_SERVER
+  own_block = 0;
+
   if (class_inet != (-1))
     class = (unsigned int) class_inet;
-#endif
-
-  /* --- Consistency check. ---
-   */
+
   ASSERT((fmt != NULL), _("fmt != NULL"))
   if (fmt == NULL)
@@ -1058,5 +912,5 @@
 	      _("ERROR: msg=<NULL format>, file=<%s>, line=<%ld>\n"), 
 	      file, line);
-      goto exit_here;
+      SL_RET0(_("sh_error_handle"));
     }
 
@@ -1066,23 +920,7 @@
     severity = sev;
 
-  /* --- Some statistics. ---
-   */
-#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
-  if ( ((1 << class) & ERROR_CLA) && 
-       (severity & (SH_ERR_ERR|SH_ERR_SEVERE|SH_ERR_FATAL)))
-    {
-      ++sh.statistics.files_error;
-    }
-#endif
-
-  /* these are messages from remote sources
-   */
   if ((severity  & SH_ERR_INET) != 0)
     {
       flag_inet = S_TRUE;
-    }
-  else
-    {
-      flag_inet  = S_FALSE;
     }
 
@@ -1095,5 +933,5 @@
        ( (errFlags.sysloglevel  & severity    ) == 0 || 
 	 (errFlags.syslog_class & (1 << class)) == 0 )     &&
-#if defined(SH_WITH_CLIENT) || defined(SH_WITH_CLIENT)
+#ifdef SH_WITH_CLIENT
        ( (errFlags.exportlevel  & severity    ) == 0 ||
 	 (errFlags.export_class & (1 << class)) == 0 )     &&
@@ -1114,23 +952,16 @@
 	 (errFlags.mail_class    & (1 << class)) == 0 )
 #ifdef SH_WITH_SERVER
-       && (flag_inet == S_FALSE) /* still log messages from remote sources */
+      && (flag_inet == S_FALSE)
 #endif
        )
     {
-      goto exit_here;
+      SL_RET0(_("sh_error_handle"));
     }
 
   if ((severity & SH_ERR_NOT) != 0)
     {
-      goto exit_here;
-    }
-  
-#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
-  if ((sh_global_check_silent > SH_SILENT_STD) &&
-      (((1 << FIL) & (1 << class)) != 0))
-    {
-      goto exit_here;
-    }
-#endif
+      SL_RET0(_("sh_error_handle"));
+    }
+
 
   /* Allocate space for the message.
@@ -1147,8 +978,11 @@
   lmsg->line     = line;
   lmsg->status   = status;
+  own_block = 0;
+
 
   /* Format the log message with timestamp etc.
    * Allocate lmsg->msg
    */
+  own_block = 1;
   va_start (vl, msg_id);
   (void) sh_error_string (lmsg, vl);
@@ -1156,5 +990,4 @@
   own_block = 0;
 
-  hexmsg = sh_error_replace(lmsg->msg);
 
   /* Log to stderr.
@@ -1177,5 +1010,5 @@
 	   *  Reports first error after failure. Always tries.
 	   */
-	  (void) sh_log_console (hexmsg ? hexmsg : lmsg->msg);
+	  (void) sh_log_console (lmsg->msg);
 	  print_block = 0;
 	}
@@ -1185,5 +1018,5 @@
   /* Full logging enabled.
    */
-  if (OnlyStderr == S_FALSE)  /* full error logging enabled */
+  if (OnlyStderr == BAD)  /* full error logging enabled */
     {
 
@@ -1205,5 +1038,5 @@
 	       * Ignores errors. Always tries.
 	       */
-	      (void) sh_log_syslog (lmsg->severity, hexmsg ? hexmsg : lmsg->msg);
+	      (void) sh_log_syslog (lmsg->severity, lmsg->msg);
 	      syslog_block = 0;
 	    }
@@ -1227,5 +1060,5 @@
 	       *  Reports first error after failure. Always tries.
 	       */
-	      (void) sh_ext_execute ('l', 'o', 'g', hexmsg ? hexmsg : lmsg->msg, 0);
+	      (void) sh_ext_execute ('l', 'o', 'g', lmsg->msg, 0);
 	      external_block = 0;
 	    }
@@ -1242,5 +1075,5 @@
 	  class != AUD)
 	{
-	  if (database_block == 0 && enableUnsafe == S_TRUE)
+	  if (database_block == 0)
 	    {
 	      /* truncates; query_max is 16k
@@ -1280,37 +1113,27 @@
        * to log server
        ****************************************************/
-#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
+#ifdef SH_WITH_CLIENT
       /* Export by TCP.
        */
-
-      if ( ((errFlags.exportlevel  & severity  )   != 0 &&
-	    (errFlags.export_class & (1 << class)) != 0 &&
-	    (errFlags.exportlevel  & SH_ERR_NOT)   == 0 &&
-	    class != AUD                               )
-#ifdef SH_WITH_SERVER
-	   /* always log inet to export */
-	   || (flag_inet == S_TRUE && sh.srvexport.name[0] != '\0') 
-#endif
-          /* sh.flag.isserver != GOOD                    && */
-          /* (flag_inet == S_FALSE) */ /* don't log inet to export */
-	   )
+      if ((errFlags.exportlevel  & severity  )   != 0 &&
+          (errFlags.export_class & (1 << class)) != 0 &&
+          (errFlags.exportlevel  & SH_ERR_NOT)   == 0 &&
+	  class != AUD                                &&
+          sh.flag.isserver != GOOD                    &&
+          (flag_inet == S_FALSE) ) /* don't log inet to export */
         {
           if (export_block == 0)
             {
 	      int retval;
-	      size_t ex_len;
 
 	      /* will truncate to 65280 bytes 
 	       */
               export_block = 1;
-	      /* ex_len = 64 + sl_strlen(lmsg->msg) + 1; */
-	      ex_len = sl_strlen(lmsg->msg);
-	      if (sl_ok_adds(ex_len, 65))
-		ex_len = 64 + ex_len + 1;
-	      ex_msg = SH_ALLOC (ex_len);
-
-	      sl_snprintf(ex_msg, ex_len, _("%d?%u?%s"),
-		      severity, class, lmsg->msg);
-              retval = sh_xfer_report (ex_msg);
+	      ex_msg = SH_ALLOC (64 + sl_strlen(lmsg->msg) + 1);
+	      /*@-bufferoverflowhigh@*/
+	      sprintf(ex_msg, _("%d?%u?%s"),             /* known to fit  */
+			   severity, class, lmsg->msg);
+	      /*@-bufferoverflowhigh@*/
+              retval = sh_forward (ex_msg);
 	      SH_FREE(ex_msg);
               export_block = 0;
@@ -1342,9 +1165,9 @@
 	      mail_block = 1;
 
-	      BREAKEXIT(sh_nmail_msg);
+	      BREAKEXIT(sh_mail_msg);
 	      if ( (severity & SH_ERR_FATAL) == 0) 
-		retval = sh_nmail_pushstack (severity, hexmsg ? hexmsg : lmsg->msg, NULL);
+		retval = sh_mail_pushstack (lmsg->msg);
 	      else 
-		retval = sh_nmail_msg (severity, hexmsg ? hexmsg : lmsg->msg, NULL);
+		retval = sh_mail_msg (lmsg->msg);
 
 	      mail_block = 0;
@@ -1368,5 +1191,5 @@
 	  )
 	{
-	  if (prelude_block == 0 && enableUnsafe == S_TRUE)
+	  if (prelude_block == 0)
 	    {
 	      /* will truncate to 998 bytes 
@@ -1378,13 +1201,7 @@
 	       *  Reports first error after failure. Always tries.
 	       */
-#if defined(HAVE_LIBPRELUDE) && defined(SH_WITH_SERVER) 
-	      (void) sh_prelude_alert (severity, (int) class, 
-				       hexmsg ? hexmsg : lmsg->msg, lmsg->status, msg_id, 
-				       local_inet_peer_ip);
-#else
-	      (void) sh_prelude_alert (severity, (int) class, 
-				       hexmsg ? hexmsg : lmsg->msg, lmsg->status, msg_id, 
-				       NULL);
-#endif
+	      (void) sh_prelude_alert (severity, (int) class, lmsg->msg,
+				       lmsg->status, msg_id);
+
 	      prelude_block = 0;
 	    }
@@ -1422,8 +1239,8 @@
 		    (void) sh_log_file (lmsg->msg, NULL);
 		  else
-                    (void) sh_log_file (lmsg->msg, local_inet_peer);
+		    (void) sh_log_file (lmsg->msg, local_inet_peer);
 		}
 #else
-              (void) sh_log_file (hexmsg ? hexmsg : lmsg->msg, NULL);
+              (void) sh_log_file (lmsg->msg, NULL);
 #endif
 	      /* sh_log_file (lmsg->msg); */
@@ -1440,95 +1257,13 @@
   if (lmsg->msg)
     SH_FREE( lmsg->msg );
-  sh_replace_free(hexmsg);
-
-  memset ( lmsg, 0, sizeof(struct _log_t) );
+
+  memset ( lmsg, (int) '\0', sizeof(struct _log_t) );
   MUNLOCK( (char *) lmsg,       sizeof(struct _log_t) );
   SH_FREE( lmsg );
   own_block = 0;
 
- exit_here:
-  ; /* label at end of compound statement */
-  SH_MUTEX_RECURSIVE_UNLOCK(mutex_err_handle);
-
   /*@i@*/SL_RET0(_("sh_error_handle"));
 /*@i@*/}
 
-#if defined(SH_WITH_MAIL)
-void sh_error_mail (const char * alias, int sev, 
-		    const char * file, long line, 
-		    long status, unsigned long msg_id, ...)
-{
-  va_list         vl;                 /* argument list          */
-  struct _log_t * lmsg;
-
-  int    severity;
-  unsigned int class;
-  const char * fmt;
-  int retval; 
-
-  SL_ENTER(_("sh_error_mail"));
-
-  /* Returns pointer to (constant|thread-specific) static memory
-   */
-  fmt = /*@i@*/get_format (msg_id, &severity, &class);
-
-  if (!fmt)
-    {
-      SL_RET0(_("sh_error_mail"));
-    }
-
-  /* --- Override the catalogue severity. ---
-   */
-  if (sev != (-1))
-    severity = sev;
-
-  /* --- Build the message. ---
-   */
-  lmsg = (struct _log_t *) SH_ALLOC(sizeof(struct _log_t));
-  MLOCK( (char *) lmsg, sizeof(struct _log_t));
-  /*@i@*/lmsg->msg = NULL;
-
-  /*@i@*/(void) sl_strlcpy(lmsg->format, fmt, SH_PATHBUF);
-  (void) sl_strlcpy(lmsg->file, file, SH_PATHBUF);
-  lmsg->severity = severity;
-  lmsg->class    = (int) class;
-  lmsg->line     = line;
-  lmsg->status   = status;
-
-  /* Format the log message with timestamp etc.
-   * Allocate lmsg->msg
-   */
-  va_start (vl, msg_id);
-  (void) sh_error_string (lmsg, vl);
-  va_end (vl);
-
-  if ( (severity & SH_ERR_FATAL) == 0) 
-    retval = sh_nmail_pushstack (severity, lmsg->msg, alias);
-  else 
-    retval = sh_nmail_msg (severity, lmsg->msg, alias);
-  
-  if (retval == -2)
-    {
-      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_QUEUE_FULL,
-		       _("email"));
-    }
-  SL_RET0(_("sh_error_mail"));
-}
-#else
-void sh_error_mail (const char * alias, int sev, 
-		    const char * file, long line, 
-		    long status, unsigned long msg_id, ...)
-{
-  (void) alias;
-  (void) sev;
-  (void) file;
-  (void) line;
-  (void) status;
-  (void) msg_id;
-
-  return;
-}
-/* defined(SH_WITH_MAIL) */
-#endif 
 
 /* -------------------------  
@@ -1543,5 +1278,5 @@
  */
 /*@owned@*/ /*@null@*/inline
-static const char * get_format(unsigned long msg_id, /*@out@*/ int * priority, 
+static char * get_format(unsigned long msg_id, /*@out@*/ int * priority, 
 			 /*@out@*/unsigned int * class)
 {
@@ -1558,5 +1293,5 @@
 	  *priority = (int) msg_cat[i].priority;
 	  *class    = (unsigned int) msg_cat[i].class;
-	  SL_RETURN (((const char *) _(msg_cat[i].format)), _("get_format"));
+	  SL_RETURN (_(msg_cat[i].format), _("get_format"));
 	}
       ++i;
@@ -1571,8 +1306,8 @@
 /* allocate space for user-defined message header
  */
-int sh_error_ehead (/*@null@*/const char * str_s)
+int sh_error_ehead (/*@null@*/char * str_s)
 {
   size_t size;
-  const char * s;
+  char * s;
 
   SL_ENTER(_("sh_error_ehead"));
@@ -1589,5 +1324,5 @@
   
   size = /*@i@*/strlen(s);
-  if (/*@i@*/s[size-1] == (char) 34) --size; /* truncate */
+  if (/*@i@*/s[size-1] == (char) 34) --size;
 
   if (ehead_format != NULL)
@@ -1706,5 +1441,5 @@
   }
 
-  (void) sh_unix_time (0, tst, 64);
+  (void) sl_strlcpy (tst, sh_unix_time (0), 64);
   line = (unsigned long) lmsg->line;
   (void) sl_strlcpy (cla, _(class_cat[lmsg->class]), 11);
@@ -1747,10 +1482,6 @@
       len      = sl_strlen(lmsg->msg);
       /*@i@*/required = sl_vsnprintf(&(lmsg->msg[len]), 
-				     (lmsg->msg_len - len), lmsg->format, vl);
-
-      if ((required >= 0) && 
-	  sl_ok_adds(required, len) &&
-	  sl_ok_adds((required+len), 4) &&
-	  ((required + len) > (lmsg->msg_len - 4)) )
+			      (lmsg->msg_len - len), lmsg->format, vl);
+      if ( (required + len) > (lmsg->msg_len - 4) )
 	{
 	  /*@i@*/p = SH_ALLOC(required + len + 4);
@@ -1760,5 +1491,5 @@
 	  lmsg->msg_len = required + len + 4;
 	  (void) sl_vsnprintf(&(lmsg->msg[len]), 
-			      (required + 1), lmsg->format, vl2);
+			      (required + len + 1), lmsg->format, vl2);
 	}
       va_end(vl2);
@@ -1805,13 +1536,7 @@
 			     SH_ERR_STAMP | SH_ERR_ERR    | SH_ERR_SEVERE |
 			     SH_ERR_FATAL);
-  flag_err_info           = S_TRUE;
-#endif
-
-#if defined(SH_WITH_SERVER)
-  errFlags.exportlevel    = SH_ERR_NOT;
-#else
+#endif
   errFlags.exportlevel    = (SH_ERR_STAMP | SH_ERR_ERR    | SH_ERR_SEVERE |
 			     SH_ERR_FATAL);
-#endif
 
   errFlags.log_class      = 0xFFFF;
Index: trunk/src/sh_extern.c
===================================================================
--- trunk/src/sh_extern.c	(revision 591)
+++ trunk/src/sh_extern.c	(revision 1)
@@ -40,5 +40,5 @@
 static FILE * pdbgc = NULL;
 #define PDBG_OPEN    if (pdbg == NULL) pdbg = fopen(PDGBFILE"main",  "a")  
-#define PDBG_CLOSE   sl_fclose (FIL__, __LINE__, pdbg); pdbg = NULL
+#define PDBG_CLOSE   fclose (pdbg); pdbg = NULL
 #define PDBG(arg)    fprintf(pdbg,  "PDBG: step %d\n", arg); fflush(pdbg)
 #define PDBG_D(arg)  fprintf(pdbg,  "PDBG: %d\n", arg); fflush(pdbg)
@@ -46,5 +46,5 @@
 
 #define PDBGC_OPEN   if (pdbgc == NULL) pdbgc = fopen(PDGBFILE"child", "a")  
-#define PDBGC_CLOSE  sl_fclose (FIL__, __LINE__, pdbgc); pdbgc = NULL
+#define PDBGC_CLOSE  fclose (pdbgc); pdbgc = NULL
 #define PDBGC(arg)   fprintf(pdbgc, "PDBGC: step %d\n", arg); fflush(pdbgc)
 #define PDBGC_D(arg) fprintf(pdbgc, "PDBGC: %d\n", arg); fflush(pdbgc)
@@ -74,8 +74,15 @@
 #include <sys/wait.h>
 
+#if TIME_WITH_SYS_TIME
+#include <sys/time.h>
+#include <time.h>
+#else
 #if HAVE_SYS_TIME_H
 #include <sys/time.h>
+#else
+#include <time.h>
 #endif
-#include <time.h>
+#endif
+
 
 #include "samhain.h"
@@ -85,5 +92,4 @@
 #include "sh_extern.h"
 #include "sh_calls.h"
-#include "sh_filter.h"
 #define SH_NEED_PWD_GRP 1
 #include "sh_static.h"
@@ -109,8 +115,7 @@
   FILE * outf = NULL;
   char * envp[1];
-  char * argp[2];
+  char * argp[1];
 
   char * errfile;
-  char errbuf[SH_ERRBUF_SIZE];
 
   static int some_error = 0;
@@ -121,29 +126,22 @@
   int         pfd = -1;
 #endif
-
+  
   SL_ENTER(_("sh_ext_popen"));
 
   /* Linux, HP-UX and FreeBSD will happily accept envp = argp = NULL
-   * (newer Linuxes (gcc 4.4.4) warn on argp == NULL, but accept it,
-   * as reported by T. Luettgert)
    * Solaris (and probably some other Unices) 
    *         needs a valid *envp[] with envp[0] = NULL;
    *         and similarly for argp
-   * OpenBSD finally needs non-null argp[0] ...
    */
-  argp[0] = task->command;
-  argp[1] = NULL;
   envp[0] = NULL;
+  argp[0] = NULL;
 
   /* 
    * --  check whether path is trustworthy
    */
-  status = sl_trustfile(task->command, NULL, NULL);
-#if 0
   if ((uid_t) -1 != task->trusted_users[0])
     {
       status = sl_trustfile(task->command, task->trusted_users, NULL);
     }
-#endif
 
   PDBG_OPEN;
@@ -180,13 +178,10 @@
    * --  check whether the checksum is correct; with linux emulate fdexec
    */
-#if ( !defined(__linux__) || ( defined(__linux__) && defined(HAVE_PTHREAD)) ) && !defined(SL_DEBUG)
+#if !defined(__linux__) && !defined(SL_DEBUG)
   if (task->checksum[0]  != '\0')
     {
-      char hashbuf[KEYBUF_SIZE];
       PDBG_S("checksum test");
       if (0 != sl_strcmp(task->checksum, 
-			 sh_tiger_hash (task->command, TIGER_FILE, TIGER_NOLIM,
-					hashbuf, sizeof(hashbuf))
-			 )
+			 sh_tiger_hash (task->command, TIGER_FILE, 0))
 	  )
 	{
@@ -216,15 +211,13 @@
       errnum = errno;
       sh_error_handle((-1), FIL__, __LINE__, errnum, MSG_E_SUBGEN, 
-		      sh_error_message(errnum, errbuf, sizeof(errbuf)), _("pipe"));
+		      sh_error_message(errnum), _("pipe"));
       SL_RETURN ((-1), _("sh_ext_popen"));
     }
 
   PDBG(3);
-
+  
   /* 
-   * -- Flush streams and fork 
+   * -- Fork 
    */
-  fflush (NULL);
-
   task->pid = aud_fork(FIL__, __LINE__);
 
@@ -233,10 +226,10 @@
       PDBG_S("fork() failure");
       /*@-usedef@*/
-      (void) sl_close_fd(FIL__, __LINE__, pipedes[0]);
-      (void) sl_close_fd(FIL__, __LINE__, pipedes[1]);
+      (void) close(pipedes[0]);
+      (void) close(pipedes[1]);
       /*@+usedef@*/
       errnum = errno;
       sh_error_handle((-1), FIL__, __LINE__, errnum, MSG_E_SUBGEN, 
-		      sh_error_message(errnum, errbuf, sizeof(errbuf)), _("fork"));
+		      sh_error_message(errnum), _("fork"));
       SL_RETURN ((-1), _("sh_ext_popen"));
     }
@@ -251,9 +244,9 @@
       if (S_TRUE == task->fork_twice)
 	{
-	  task->pid = fork();
+	  task->pid = aud_fork(FIL__, __LINE__);
 
 	  if (task->pid == (pid_t) - 1) 
 	    {
-	      _exit (EXIT_FAILURE);
+	      aud__exit (FIL__, __LINE__, EXIT_FAILURE);
 	    }
 	}
@@ -261,6 +254,4 @@
       if (task->pid == (pid_t) 0)
 	{
-	  int val_return;
-
 	  PDBGC_OPEN;
 	  PDBGC(1);
@@ -271,19 +262,13 @@
 	  if (task->rw == 'w')
 	    {
-	      do {
-		val_return = dup2 (pipedes[STDIN_FILENO], STDIN_FILENO);
-	      } while (val_return < 0 && errno == EINTR);
-
-	      if (val_return < 0)
-		_exit(EXIT_FAILURE);
+	      if (retry_aud_dup2(FIL__, __LINE__, 
+				 pipedes[STDIN_FILENO], STDIN_FILENO) < 0)
+		aud__exit(FIL__, __LINE__,EXIT_FAILURE);
 	    }
 	  else
 	    {
-	      do {
-		val_return = dup2 (pipedes[STDOUT_FILENO], STDOUT_FILENO);
-	      } while (val_return < 0 && errno == EINTR);
-
-	      if (val_return < 0)
-		_exit(EXIT_FAILURE);
+	      if (retry_aud_dup2(FIL__, __LINE__,
+				 pipedes[STDOUT_FILENO], STDOUT_FILENO) < 0)
+		aud__exit(FIL__, __LINE__,EXIT_FAILURE);
 	    }
 	  PDBGC(2);
@@ -292,11 +277,11 @@
 	  /* close the pipe descriptors 
 	   */
-	  (void) sl_close_fd   (FIL__, __LINE__, pipedes[STDIN_FILENO]);
-	  (void) sl_close_fd   (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
+	  (void) close   (pipedes[STDIN_FILENO]);
+	  (void) close   (pipedes[STDOUT_FILENO]);
 	  
 	  /* don't leak file descriptors
 	   */
 #if !defined(PDGBFILE)
-	  sh_unix_closeall (3, task->com_fd, S_TRUE); /* in child process */
+	  sh_unix_closeall (3, task->com_fd); /* in child process */
 #endif
 
@@ -311,13 +296,10 @@
 	      memset(skey, 0, sizeof(sh_key_t));
 
-	      if (setgid((gid_t) task->run_user_gid) != 0)
-		_exit(EXIT_FAILURE);
-	      if (setuid((uid_t) task->run_user_uid) != 0)
-		_exit(EXIT_FAILURE);
-
+	      (void) aud_setgid(FIL__, __LINE__,(gid_t) task->run_user_gid);
+	      (void) aud_setuid(FIL__, __LINE__,(uid_t) task->run_user_uid);
 	      /* make sure we cannot get root again
 	       */
-	      if (setuid(0) >= 0)
-		_exit(EXIT_FAILURE);
+	      if (aud_setuid(FIL__, __LINE__,0) >= 0)
+		aud__exit(FIL__, __LINE__,EXIT_FAILURE);
 	    }
 	  
@@ -338,8 +320,6 @@
 	    {
 	      PDBGC_S("r");
-	      do {
-		val_return = dup2 (STDOUT_FILENO, STDERR_FILENO);
-	      } while (val_return < 0 && errno == EINTR);
-
+	      (void) retry_aud_dup2 (FIL__, __LINE__, 
+				     STDOUT_FILENO, STDERR_FILENO);
 	      (void) fcntl  (STDIN_FILENO, F_SETFD, FD_CLOEXEC);
 	      /*
@@ -355,101 +335,45 @@
 	   * --  emulate an fdexec with checksum testing
 	   */
-
-#if !defined(HAVE_PTHREAD)
 	  if (task->checksum[0]  != '\0')
-#endif
 	    {
 	      PDBGC_S("fexecve");
 	      if (task->com_fd != (-1))
 		{
-		  do {
-		    val_return = dup (task->com_fd);
-		  } while (val_return < 0 && errno == EINTR);
-		  pfd = val_return;
+		  pfd = retry_aud_dup(FIL__, __LINE__, task->com_fd);
 		  if (pfd < 0)
 		    {
-		      PDBGC_S("fexecve: dup failed");
-		      _exit(EXIT_FAILURE);
+		      PDBGC_S("fexecve: dup2 failed");
+		      aud__exit(FIL__, __LINE__, EXIT_FAILURE);
 		    }
 		}
-#if !defined(HAVE_PTHREAD)
 	      else
 		{
-		  char hashbuf[KEYBUF_SIZE];
-
 		  fd = 
-		    sl_open_read(FIL__, __LINE__, task->command, 
+		    sl_open_read(task->command, 
 				 task->privileged==0 ? SL_NOPRIV : SL_YESPRIV);
-
+		  tiger_fd = fd;
 		  if (0 != sl_strcmp(task->checksum, 
 				     sh_tiger_hash (task->command, 
-						    fd, TIGER_NOLIM, hashbuf, sizeof(hashbuf))))
+						    TIGER_FD, 0)))
 		    {
 		      PDBGC_S("fexecve: checksum mismatch");
-		      sl_close(fd);
-		      _exit(EXIT_FAILURE);
+		      aud__exit(FIL__, __LINE__, EXIT_FAILURE);
 		    }
-
 		  pfd = get_the_fd(fd);
-
-		  do {
-		    val_return = dup (pfd);
-		  } while (val_return < 0 && errno == EINTR);
-		  pfd = val_return;
-
-		  sl_close(fd);
-		  fd = -1;
-
-		  if (pfd < 0)
-		    {
-		      PDBGC_S("fexecve: dup (2) failed");
-		      _exit(EXIT_FAILURE);
-		    }
 		}
-#endif
               
 	      PDBGC(5);
-	      /* Cannot use sprintf because of deadlock in malloc/free */
-	      {
-		static char digit[] = "0123456789";
-		char str0[128];
-		char str1[128];
-		int ival = pfd;
-		unsigned int n = 0;
-		unsigned int m = 0;
-
-		if (ival < 0) ival = -ival;
-		do {
-		  str0[n] = digit[ival % 10];
-		  ++n;
-		  ival /= 10;
-		} while (ival);
-
-		if (pfd < 0)
-		  {
-		    str0[n] = '-';
-		    ++n;
-		  }
-		str0[n] = '\0';
-		str1[n] = '\0';
-		while (n > 0)
-		  {
-		    str1[m] = str0[n-1];
-		    ++m; --n;
-		  }
-		sl_strlcpy(pname, _("/proc/self/fd/"), sizeof(pname));
-		sl_strlcat(pname, str1, sizeof(pname));
-	      }
-              if (access(pname, R_OK|X_OK) == 0) /* flawfinder: ignore */
+	      sprintf(pname, _("/proc/self/fd/%d"),      /* known to fit  */
+			   pfd);
+              if (access(pname, R_OK|X_OK) == 0) 
 		{
 		  PDBGC(6);
 		  PDBGC_CLOSE;
 		  fcntl  (pfd, F_SETFD, FD_CLOEXEC);
-		  do {
-		    val_return = execve (pname, 
-					 (task->argc == 0) ? argp : task->argv, 
-					 (task->envc == 0) ? NULL : task->envv
-					 );
-		  } while (val_return < 0 && errno == EINTR);
+		  retry_aud_execve (FIL__, __LINE__, 
+				    pname, 
+				    (task->argc == 0) ? NULL : task->argv, 
+				    (task->envc == 0) ? NULL : task->envv
+				    );
 		  
 		  errnum = errno;
@@ -461,5 +385,5 @@
 		  /* failed 
 		   */
-		  _exit((errnum == 0) ? (EXIT_SUCCESS) : (EXIT_FAILURE));
+		  aud__exit(FIL__, __LINE__, EXIT_FAILURE);
               }
 	      PDBGC_S("fexecve: not working");
@@ -470,5 +394,5 @@
 		sl_close(fd);
 	      else if (pfd != -1)
-		sl_close_fd(FIL__, __LINE__, pfd);
+		close(fd);
 	    }
 #endif
@@ -478,14 +402,13 @@
 	   * --  execute path if executable
 	   */
-	  if (0 == access(task->command, R_OK|X_OK)) /* flawfinder: ignore */
+	  if (0 == access(task->command, R_OK|X_OK))
 	    {
 	      PDBGC(5);
 	      PDBGC_CLOSE;
-	      do {
-		val_return = execve (task->command, 
-				     (task->argc == 0) ? argp : task->argv, 
-				     (task->envc == 0) ? envp : task->envv
-				     );
-	      } while (val_return < 0 && errno == EINTR);
+	      (void) retry_aud_execve (FIL__, __LINE__, 
+				       task->command, 
+				       (task->argc == 0) ? argp : task->argv, 
+				       (task->envc == 0) ? envp : task->envv
+				       );
 	    }
 	  errnum = errno;
@@ -497,5 +420,5 @@
 	  /* failed 
 	   */
-	  _exit((errnum == 0) ? (EXIT_SUCCESS) : (EXIT_FAILURE));
+	  aud__exit(FIL__, __LINE__, EXIT_FAILURE);
 	}
       /* 
@@ -504,5 +427,5 @@
       if (S_TRUE == task->fork_twice)
 	{
-	  _exit (0);
+	  aud__exit (FIL__, __LINE__, 0);
 	}
     }
@@ -524,5 +447,5 @@
     {
       PDBG_S("is w");
-      (void) sl_close_fd (FIL__, __LINE__, pipedes[STDIN_FILENO]);
+      (void) close (pipedes[STDIN_FILENO]);
       (void) retry_fcntl (FIL__, __LINE__, pipedes[STDOUT_FILENO], 
 			  F_SETFD, FD_CLOEXEC);
@@ -532,5 +455,5 @@
     {
       PDBG_S("is r");
-      (void) sl_close_fd (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
+      (void) close (pipedes[STDOUT_FILENO]);
       (void) retry_fcntl (FIL__, __LINE__, pipedes[STDIN_FILENO], 
 			  F_SETFD, FD_CLOEXEC);
@@ -554,6 +477,6 @@
 
       (void) aud_kill (FIL__, __LINE__, task->pid, SIGKILL);
-      (void) sl_close_fd (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
-      (void) sl_close_fd (FIL__, __LINE__, pipedes[STDIN_FILENO]);
+      (void) close (pipedes[STDOUT_FILENO]);
+      (void) close (pipedes[STDIN_FILENO]);
       (void) waitpid (task->pid, NULL, 0);
       task->pid = 0;
@@ -569,5 +492,5 @@
   PDBG_D(task->pipeFD);
 
-  task->pipeTI = sl_make_ticket(FIL__, __LINE__, task->pipeFD, _("pipe"), outf);
+  task->pipeTI = sl_make_ticket(task->pipeFD, _("pipe"));
 
   flags = (int) retry_fcntl (FIL__, __LINE__, task->pipeFD, F_GETFL, 0);
@@ -595,15 +518,10 @@
   char  infomsg[256];
 
-#ifdef WCONTINUED
-      int wflags = WNOHANG|WUNTRACED|WCONTINUED;
-#else
-      int wflags = WNOHANG|WUNTRACED;
-#endif
-
   SL_ENTER(_("sh_ext_pclose"));
 
-  PDBG_OPEN;
+  PDBGC_OPEN;
   PDBG_S(" -> pclose");
   (void) fflush(task->pipe);
+  (void) fclose(task->pipe);
   if (!SL_ISERROR(task->pipeTI))
     (void) sl_close(task->pipeTI);
@@ -618,43 +536,39 @@
 
     nochmal:
-      retval = waitpid(task->pid, &(task->exit_status), wflags);
+      retval = waitpid(task->pid, &(task->exit_status), WNOHANG|WUNTRACED);
       /*@-bufferoverflowhigh@*/
       if (task->pid == retval)
 	{
-#ifndef USE_UNO
 	  if (WIFEXITED(task->exit_status) != 0)
 	    {
 	      task->exit_status = WEXITSTATUS(task->exit_status);
-	      if ((flag_err_debug == S_TRUE) || (task->exit_status != 0))
-		sl_snprintf(infomsg, sizeof(infomsg),
-			    _("Subprocess exited normally with status %d"),
-			    task->exit_status);
+	      if ((flag_err_debug == SL_TRUE) || (task->exit_status != 0))
+		sprintf(infomsg,                         /* known to fit  */
+			_("Subprocess exited normally with status %d"),
+			task->exit_status);
 	    }
 	  else if (WIFSIGNALED(task->exit_status) != 0)
 	    {
-	      sl_snprintf(infomsg, sizeof(infomsg),
-			  _("Subprocess terminated by signal %d"),
-			  WTERMSIG(task->exit_status));
+	      sprintf(infomsg,                           /* known to fit  */
+		      _("Subprocess terminated by signal %d"),
+		      WTERMSIG(task->exit_status));
 	      task->exit_status = EXIT_FAILURE;
 	    }
 	  else if (WIFSTOPPED(task->exit_status) != 0)
 	    {
-	      sl_snprintf(infomsg, sizeof(infomsg),
-			  _("Subprocess stopped by signal %d, killing"),
-			  WSTOPSIG(task->exit_status));
+	      sprintf(infomsg,                           /* known to fit  */
+		      _("Subprocess stopped by signal %d, killing"),
+		      WSTOPSIG(task->exit_status));
 	      task->exit_status = EXIT_FAILURE;
 	      (void) aud_kill (FIL__, __LINE__, task->pid, 9);
 	      (void) retry_msleep (0, 30);
-	      (void) waitpid (task->pid, NULL, wflags);
+	      (void) waitpid (task->pid, NULL, WNOHANG|WUNTRACED);
 	    }
 	  else
 	    {
-	      sl_snprintf(infomsg, sizeof(infomsg),
-			  _("Subprocess exit status unknown"));
+	      sprintf(infomsg,                           /* known to fit  */
+		      _("Subprocess exit status unknown"));
 	      task->exit_status = EXIT_FAILURE;
 	    }
-#else
-	  task->exit_status = EXIT_FAILURE;
-#endif 
 	}
       else if (0 == retval)
@@ -667,6 +581,6 @@
 	    }
 	  (void) aud_kill (FIL__, __LINE__, task->pid, 9);
-	  sl_snprintf(infomsg, sizeof(infomsg),
-		      _("Subprocess not yet exited, killing"));
+	  sprintf(infomsg,                               /* known to fit  */
+		  _("Subprocess not yet exited, killing"));
 	  task->exit_status = EXIT_FAILURE;
 	  (void) waitpid (task->pid, NULL, 0);
@@ -674,11 +588,11 @@
       else
 	{
-	  sl_snprintf(infomsg, sizeof(infomsg),
-		      _("Waitpid returned error %d\n"), errno);
+	  sprintf(infomsg,                               /* known to fit  */
+		  _("Waitpid returned error %d\n"), errno);
 	  task->exit_status = EXIT_FAILURE;
 	}
       /*@+bufferoverflowhigh@*/
       status = task->exit_status;
-      if (flag_err_debug == S_TRUE)
+      if (flag_err_debug == SL_TRUE)
 	{
 	  sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, task->exit_status, 
@@ -720,7 +634,5 @@
       tas->argv[i]          = NULL;
       tas->envv[i]          = NULL;
-#if 0
       tas->trusted_users[i] = (uid_t) -1;
-#endif
     }
 
@@ -734,5 +646,5 @@
 
 
-int sh_ext_tas_add_envv(sh_tas_t * tas, const char * key, const char * val)
+int sh_ext_tas_add_envv(sh_tas_t * tas, char * key, char * val)
 {
   size_t sk = 0, sv = 0;
@@ -751,8 +663,4 @@
     sv = strlen(val) + 1;
 
-  if (!sl_ok_adds(sk, sv))
-    {
-      SL_RETURN (-1, _("sh_ext_tas_add_envv"));
-    }
   si = tas->envc;
   tas->envv[si] = SH_ALLOC(sk + sv);
@@ -789,5 +697,5 @@
 }
 
-int sh_ext_tas_add_argv(sh_tas_t * tas, const char * val)
+int sh_ext_tas_add_argv(sh_tas_t * tas, char * val)
 {
   size_t sv = 0;
@@ -814,5 +722,5 @@
 }
 
-void sh_ext_tas_command(sh_tas_t * tas, const char * command)
+void sh_ext_tas_command(sh_tas_t * tas, char * command)
 {
   size_t len = sl_strlen(command);
@@ -843,175 +751,4 @@
 }
 
-static void task_init (sh_tas_t * task)
-{
-  sh_ext_tas_init(task);
-
-  (void) sh_ext_tas_add_envv (task, _("SHELL"), 
-			      _("/bin/sh")); 
-  (void) sh_ext_tas_add_envv (task, _("PATH"),  
-			      _("/sbin:/bin:/usr/sbin:/usr/bin:/usr/ucb")); 
-  (void) sh_ext_tas_add_envv (task, _("IFS"), " \n\t"); 
-
-  if (sh.timezone != NULL)
-    {
-      (void) sh_ext_tas_add_envv(task,  "TZ", sh.timezone);
-    }
-  return;
-}
-
-int sh_ext_popen_init (sh_tas_t * task, const char * command, char * argv0, ...)
-{
-  va_list vl;
-  int status;
-
-  task_init (task);
-  
-  if (!argv0)
-    {
-      sh_ext_tas_command(task,  _("/bin/sh"));
-
-      (void) sh_ext_tas_add_argv(task,  _("/bin/sh"));
-      (void) sh_ext_tas_add_argv(task,  _("-c"));
-      (void) sh_ext_tas_add_argv(task,  command);
-    }
-  else
-    {
-      char * s;
-
-      sh_ext_tas_command(task,  command);
-
-      (void) sh_ext_tas_add_argv(task, argv0);
-
-      va_start (vl, argv0);
-      s = va_arg (vl, char * );
-      while (s != NULL)
-	{
-	  (void) sh_ext_tas_add_argv(task, s);
-	  s = va_arg (vl, char * );
-	}
-      va_end (vl);
-
-    }
-  task->rw = 'r';
-  task->fork_twice = S_FALSE;
-
-  status = sh_ext_popen(task);
-
-  return status;
-}
-
-/* Execute a system command */
-
-int sh_ext_system (char * command, char * argv0, ...)
-{
-  sh_tas_t task;
-  int    status;
-  va_list vl;
-  char * s;
-
-  SL_ENTER(_("sh_ext_system"));
-
-  task_init (&task);
-  
-  sh_ext_tas_command(&task,  command);
-
-  (void) sh_ext_tas_add_argv(&task, argv0);
-  
-  va_start (vl, argv0);
-  s = va_arg (vl, char * );
-  while (s != NULL)
-    {
-      (void) sh_ext_tas_add_argv(&task, s);
-      s = va_arg (vl, char * );
-    }
-  va_end (vl);
-
-  task.rw = 'r';
-  task.fork_twice = S_FALSE;
-
-  status = sh_ext_popen(&task);
-
-  if (status != 0)
-    {
-      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_E_SUBGEN, 
-                      _("Could not execute command"), _("sh_ext_system"));
-      SL_RETURN ((-1), _("sh_ext_system"));
-    }
-
-  /* close pipe and return exit status
-   */
-  (void) sh_ext_pclose(&task);
-  sh_ext_tas_free (&task);
-  SL_RETURN ((status), _("sh_ext_system"));
-}
-
-/* Execute command, return first line of output
- * ifconfig | grep -1 lo | tail -n 1 | sed s/.*inet addr:\([0-9.]*\)\(.*\)/\1/
- */
-char * sh_ext_popen_str (const char * command)
-{
-  sh_tas_t task;
-  struct  sigaction  new_act;
-  struct  sigaction  old_act;
-  char * out = NULL;
-  int    status;
-
-  SL_ENTER(_("sh_ext_popen_str"));
-
-  status = sh_ext_popen_init (&task, command, NULL, NULL);
-
-  if (status != 0)
-    {
-      sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, status, MSG_E_SUBGEN, 
-                      _("Could not open pipe"), _("sh_ext_popen_str"));
-      SL_RETURN ((NULL), _("sh_ext_popen_str"));
-    }
-
-   /* ignore SIGPIPE (instead get EPIPE if connection is closed)
-    */
-   new_act.sa_handler = SIG_IGN;
-   new_act.sa_flags   = 0;
-   sigemptyset( &new_act.sa_mask );
-   
-   (void) retry_sigaction (FIL__, __LINE__, SIGPIPE, &new_act, &old_act);
-   
-  /* read from the open pipe
-   */
-  if (task.pipe != NULL)
-    {
-      int try = 1200; /* 1000 * 0.1 = 120 sec */
-      sh_string * s = sh_string_new(0);
-      do {
-	sh_string_read(s, task.pipe, 0);
-	if (sh_string_len(s) == 0)
-	  {
-	    /* cppcheck-suppress syntaxError */
-	    --try; retry_msleep(0, 100);
-	  }
-      } while (sh_string_len(s) == 0 && try != 0);
-
-      if (sh_string_len(s) == 0)
-	{
-	  sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, status, MSG_E_SUBGEN, 
-			  _("No output from command"), _("sh_ext_popen_str"));
-	}
-
-      out = sh_util_strdup(sh_string_str(s));
-      sh_string_destroy(&s);
-    }
-
-  /* restore old signal handler
-   */
-  (void) retry_sigaction (FIL__, __LINE__, SIGPIPE, &old_act, NULL);
-
-  /* close pipe and return exit status
-   */
-  (void) sh_ext_pclose(&task);
-  sh_ext_tas_free (&task);
-  SL_RETURN ((out), _("sh_ext_popen_str"));
-}
-
-
-
 
 /* ---------------  EXTERN STUFF ------------------- */
@@ -1023,6 +760,10 @@
   char     type[4];
 
-  sh_filter_type * filter;
-
+  int      for_c;
+  char   * for_v[32];
+  int      fand_c;
+  char   * fand_v[32];
+  int      fnot_c;
+  char   * fnot_v[32];
   time_t   deadtime;
   time_t   last_run;
@@ -1052,4 +793,5 @@
 sh_com_t * command_init(void)
 {
+  int         i;
   uid_t       ff_euid;
   sh_com_t  * ext_com = NULL;
@@ -1061,5 +803,5 @@
   if (!ext_com)
     {
-      SL_RETURN( NULL, _("command_init"));
+      SL_RETURN( NULL, ("command_init"));
     }
 
@@ -1067,19 +809,25 @@
 
   (void) sl_get_euid(&ff_euid);
-#if 0
   ext_com->tas.trusted_users[0] = (uid_t) 0;
   ext_com->tas.trusted_users[1] = (uid_t) (ff_euid);
-#endif
 
   /* ------------------------------------------------- */
 
   set3(ext_com->type, 'l', 'o', 'g');
-  ext_com->filter       = NULL;
+  ext_com->for_c        = 0;
+  ext_com->fand_c       = 0;
+  ext_com->fnot_c       = 0;
   ext_com->deadtime     = 0;
   ext_com->last_run     = 0;
 
+  for (i = 0; i < 32; ++i)
+    {
+      ext_com->for_v[i]         = NULL;
+      ext_com->fand_v[i]        = NULL;
+      ext_com->fnot_v[i]        = NULL;
+    }
   ext_com->next             = NULL;
 
-  SL_RETURN( ext_com, _("command_init"));
+  SL_RETURN( ext_com, ("command_init"));
 }
 
@@ -1094,8 +842,6 @@
 
 static
-int sh_ext_add_envv(const char * key, const char * val)
-{
-  int retval; 
-
+int sh_ext_add_envv(char * key, char * val)
+{
   SL_ENTER(_("sh_ext_add_envv"));
 
@@ -1107,10 +853,7 @@
     }
 
-  retval = sh_ext_tas_add_envv(&(ext_coms->tas), key, val);
-
-  if (retval >= 0) 
-    retval = 0;
-
-  SL_RETURN (retval, _("sh_ext_add_envv"));
+  (void) sh_ext_tas_add_envv(&(ext_coms->tas), key, val);
+
+  SL_RETURN (0, _("sh_ext_add_envv"));
 }
 
@@ -1118,5 +861,5 @@
 
 static 
-int sh_ext_init(const char * command)
+int sh_ext_init(char * command)
 {
   sh_com_t * retval;
@@ -1153,11 +896,7 @@
 
 static
-int sh_ext_uid (const char * user, /*@out@*/uid_t * uid, /*@out@*/gid_t * gid)
-{
-  struct passwd *  tempres;
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-  struct passwd    pwd;
-  char           * buffer = SH_ALLOC(SH_PWBUF_SIZE);
-#endif
+int sh_ext_uid (char * user, /*@out@*/uid_t * uid, /*@out@*/gid_t * gid)
+{
+  struct passwd * tempres;
 
   SL_ENTER(_("sh_ext_uid"));
@@ -1167,15 +906,7 @@
   if (user == NULL)
     {
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-      SH_FREE(buffer);
-#endif
       SL_RETURN (-1, _("sh_ext_uid"));
     }
-
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-  sh_getpwnam_r(user, &pwd, buffer, SH_PWBUF_SIZE, &tempres);
-#else
   tempres = sh_getpwnam(user);
-#endif
 
   if (NULL != tempres) 
@@ -1183,13 +914,7 @@
       *uid = tempres->pw_uid;  
       *gid = tempres->pw_gid;
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-      SH_FREE(buffer);
-#endif
       SL_RETURN (0, _("sh_ext_uid"));
     } 
 
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-  SH_FREE(buffer);
-#endif
   SL_RETURN (-1, _("sh_ext_uid"));
 }
@@ -1197,11 +922,9 @@
 
 static
-int sh_ext_add (const char * argstring, int * ntok, char * stok[])
+int sh_ext_add (char * argstring, int * ntok, char * stok[])
 {
   int    i = 0;
   size_t s;
   char * p;
-  char * new;
-  size_t len;
 
   SL_ENTER(_("sh_ext_add"));
@@ -1212,23 +935,10 @@
     }
 
-  len = strlen(argstring) + 1;
-  new = SH_ALLOC(len);
-  sl_strlcpy(new, argstring, len); 
-
   do
     {
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
-      char * saveptr;
       if (i == 0)
-	p = strtok_r (new, ", \t", &saveptr);
-      else
-	p = strtok_r (NULL, ", \t", &saveptr);
-#else
-      if (i == 0)
-	p = strtok (new, ", \t");
+	p = strtok (argstring, ", \t");
       else
 	p = strtok (NULL, ", \t");
-#endif
-
       if (p == NULL)
 	break;
@@ -1247,5 +957,4 @@
 
   *ntok = i;
-  SH_FREE(new);
 
   SL_RETURN (0, _("sh_ext_add"));
@@ -1262,5 +971,5 @@
  * -- start a new external command, and add it to the list
  */ 
-int sh_ext_setcommand(const char * cmd)
+int sh_ext_setcommand(char * cmd)
 {
   int i;
@@ -1280,4 +989,5 @@
 int sh_ext_cleanup(void)
 {
+  int i;
   sh_com_t * retval;
 
@@ -1291,6 +1001,10 @@
       sh_ext_tas_free (&(retval->tas));
 
-      if (retval->filter)
-	sh_filter_free (retval->filter);
+      for (i = 0; i < 32; ++i)
+	{
+	  if (NULL != retval->for_v[i])  SH_FREE(retval->for_v[i]);
+	  if (NULL != retval->fand_v[i]) SH_FREE(retval->fand_v[i]);
+	  if (NULL != retval->fnot_v[i]) SH_FREE(retval->fnot_v[i]);
+	}
 
       SH_FREE(retval);
@@ -1302,49 +1016,31 @@
 
 /*
- * -- explicitely close a command
- */
-int sh_ext_close_command (const char * str)
-{
-  (void) str;
+ * -- add keywords to the OR filter
+ */
+int sh_ext_add_or (char * str)
+{
   if (ext_coms == NULL || ext_failed == (-1))
     return (-1);
-  ext_failed = (-1);
-  return 0;
+  return (sh_ext_add (str, &(ext_coms->for_c), ext_coms->for_v));
 }
 
 /*
- * -- add keywords to the OR filter
- */
-int sh_ext_add_or (const char * str)
+ * -- add keywords to the AND filter
+ */
+int sh_ext_add_and (char * str)
 {
   if (ext_coms == NULL || ext_failed == (-1))
     return (-1);
-  if (ext_coms->filter == NULL)
-    ext_coms->filter = sh_filter_alloc();
-  return (sh_filter_add(str, ext_coms->filter, SH_FILT_OR));
+  return (sh_ext_add (str, &(ext_coms->fand_c), ext_coms->fand_v));
 }
 
 /*
- * -- add keywords to the AND filter
- */
-int sh_ext_add_and (const char * str)
+ * -- add keywords to the NOT filter
+ */
+int sh_ext_add_not (char * str)
 {
   if (ext_coms == NULL || ext_failed == (-1))
     return (-1);
-  if (ext_coms->filter == NULL)
-    ext_coms->filter = sh_filter_alloc();
-  return (sh_filter_add(str, ext_coms->filter, SH_FILT_AND));
-}
-
-/*
- * -- add keywords to the NOT filter
- */
-int sh_ext_add_not (const char * str)
-{
-  if (ext_coms == NULL || ext_failed == (-1))
-    return (-1);
-  if (ext_coms->filter == NULL)
-    ext_coms->filter = sh_filter_alloc();
-  return (sh_filter_add(str, ext_coms->filter, SH_FILT_NOT));
+  return (sh_ext_add (str, &(ext_coms->fnot_c), ext_coms->fnot_v));
 }
 
@@ -1352,5 +1048,5 @@
  * -- add keywords to the CL argument list
  */
-int sh_ext_add_argv (const char * str)
+int sh_ext_add_argv (char * str)
 {
   if (ext_coms == NULL || ext_failed == (-1))
@@ -1362,9 +1058,11 @@
  * -- add a path to the environment
  */
-int sh_ext_add_default (const char * dummy)
-{
-  char * p = NULL;
+int sh_ext_add_default (char * dummy)
+{
+  /* while this assignment looks ridiculous, it is here to avoid
+   * an 'unused parameter' warning
+   */
+  char * p = (dummy == NULL ? dummy : NULL);
   int    i;
-  char   dir[SH_PATHBUF];
 
   SL_ENTER(_("sh_ext_add_default"));
@@ -1374,11 +1072,9 @@
       SL_RETURN(0, _("sh_ext_add_default"));
     }
-  p = sh_unix_getUIDdir (SH_ERR_ERR, (uid_t) ext_coms->tas.run_user_uid, 
-			 dir, sizeof(dir));
+  p = sh_unix_getUIDdir (SH_ERR_ERR, (uid_t) ext_coms->tas.run_user_uid);
   if (p)
     (void) sh_ext_add_envv (_("HOME"), p);
   (void) sh_ext_add_envv (_("SHELL"), _("/bin/sh")); 
-  (void) sh_ext_add_envv (_("PATH"),  _("/sbin:/bin:/usr/sbin:/usr/bin")); 
-  (void) sh_ext_add_envv (_("IFS"), " \n\t"); 
+  (void) sh_ext_add_envv (_("PATH"),  _("/sbin:/usr/sbin:/bin:/usr/bin")); 
   i = (p == NULL ? (-1) :  0);
   SL_RETURN(i, _("sh_ext_add_default"));
@@ -1388,8 +1084,7 @@
  * -- add an environment variable
  */
-int sh_ext_add_environ (const char * str)
+int sh_ext_add_environ (char * str)
 {
   int i;
-
   SL_ENTER(_("sh_ext_add_environ"));
   i = sh_ext_add_envv (NULL, str);
@@ -1400,5 +1095,5 @@
  * -- set deadtime
  */
-int sh_ext_deadtime (const char * str)
+int sh_ext_deadtime (char * str)
 {
   long    deadtime = 0;
@@ -1409,14 +1104,14 @@
   if (ext_coms == NULL || ext_failed == (-1) || str == NULL)
     {
-      SL_RETURN (-1, _("sh_ext_deadtime"));
+      SL_RETURN (-1, ("sh_ext_deadtime"));
     }
   deadtime = strtol(str, &tail, 10);
   if (tail == str || deadtime < 0 || deadtime == LONG_MAX)
     {
-      SL_RETURN (-1, _("sh_ext_deadtime"));
+      SL_RETURN (-1, ("sh_ext_deadtime"));
     }
   
   ext_coms->deadtime = (time_t) deadtime;  
-  SL_RETURN (0, _("sh_ext_deadtime"));  
+  SL_RETURN (0, ("sh_ext_deadtime"));  
 }
 
@@ -1424,5 +1119,5 @@
  * -- define type
  */
-int sh_ext_type (const char * str)
+int sh_ext_type (char * str)
 {
   SL_ENTER(_("sh_ext_type"));
@@ -1459,5 +1154,5 @@
  * -- define checksum
  */
-int sh_ext_checksum (const char * str)
+int sh_ext_checksum (char * str)
 {
   SL_ENTER(_("sh_ext_checksum"));
@@ -1480,5 +1175,5 @@
  * -- choose privileges
  */
-int sh_ext_priv (const char * c)
+int sh_ext_priv (char * c)
 {
 
@@ -1507,19 +1202,56 @@
 static int sh_ext_filter (char * message, sh_com_t * task)
 {
+  int i;
+  int j = 0;
   time_t now_time;
 
   SL_ENTER(_("sh_ext_filter"));
 
-  if (task->filter)
-    {
-      if (0 != sh_filter_filter (message, task->filter))
+  /* Presence of any of these keywords prevents execution.
+   */
+  if (task->fnot_c > 0)
+    {
+      for (i = 0; i < task->fnot_c; ++i)
+	{
+	  if (NULL != sl_strstr(message, task->fnot_v[i]))
+	    {
+	      SL_RETURN ((-1), _("sh_ext_filter"));
+	    }
+	}
+    }
+
+  /* Presence of all of these keywords is required for execution.
+   */
+  if (task->fand_c > 0)
+    {
+      j = 0;
+
+      for (i = 0; i < task->fand_c; ++i)
+	if (NULL != sl_strstr(message, task->fand_v[i]))
+	  ++j;
+
+      if (j != task->fand_c)
 	{
 	  SL_RETURN ((-1), _("sh_ext_filter"));
 	}
-    }
-
-  /* Filter passed, check deadtime */
-
-  if (task->deadtime != (time_t) 0)
+
+    }
+
+  /* Presence of at least one of these keywords is required for execution.
+   */
+  if (task->for_c > 0)
+    {
+      for (i = 0; i < task->for_c; ++i)
+	{
+	  if (NULL != sl_strstr(message, task->for_v[i]))
+	    {
+	      goto checkdeadtime;
+	    }
+	}
+      SL_RETURN ((-1), _("sh_ext_filter"));
+    }
+
+ checkdeadtime:
+  if (task->deadtime != (time_t) 0)   /* deadtime */
     {
       now_time = time (NULL);
@@ -1554,5 +1286,4 @@
   int        status = 0;
   char     * tmp;
-  char errbuf[SH_ERRBUF_SIZE];
 
   static  int some_error = 0;
@@ -1583,5 +1314,4 @@
   while (listval != NULL)
     {
-      PDBG_OPEN;
       PDBG(-2);
       if (t1 == listval->type[0] &&
@@ -1595,5 +1325,5 @@
 	    {
 	      sh_error_handle((-1), FIL__, __LINE__, caperr, MSG_E_SUBGEN,
-			      sh_error_message (caperr, errbuf, sizeof(errbuf)), 
+			      sh_error_message (caperr), 
 			      _("sl_get_cap_sub"));
 	    }
@@ -1674,5 +1404,5 @@
 	    {
 	      sh_error_handle((-1), FIL__, __LINE__, caperr, MSG_E_SUBGEN,
-			      sh_error_message (caperr, errbuf, sizeof(errbuf)), 
+			      sh_error_message (caperr), 
 			      _("sl_drop_cap_sub"));
 	    }
Index: trunk/src/sh_fInotify.c
===================================================================
--- trunk/src/sh_fInotify.c	(revision 591)
+++ 	(revision )
@@ -1,725 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 2011       Rainer Wichmann                                */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-/***************************************************************************
- *
- * This file provides a module for samhain to use inotify for file checking.
- *
- */
-
-#include "config_xor.h"
-
-#if (defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)) 
-
-#include "samhain.h"
-#include "sh_utils.h"
-#include "sh_modules.h"
-#include "sh_pthread.h"
-#include "sh_inotify.h"
-#include "sh_unix.h"
-#include "sh_hash.h"
-#include "sh_dbIO.h"
-#include "sh_files.h"
-#include "sh_ignore.h"
-
-#define FIL__  _("sh_fInotify.c")
-
-sh_watches sh_file_watches = SH_INOTIFY_INITIALIZER;
-
-#if defined(HAVE_SYS_INOTIFY_H) 
-
-static sh_watches sh_file_missing = SH_INOTIFY_INITIALIZER;
-
-#include <sys/inotify.h>
-
-/* --- Configuration ------- */
-
-static int ShfInotifyActive = S_FALSE;
-static int ShfInotifyClose  = S_FALSE; /* for reconf, mark instance for closing */
-
-static unsigned long ShfInotifyWatches = 0;
-
-static int sh_fInotify_active(const char *s) 
-{
-  int value;
-    
-  SL_ENTER(_("sh_fInotify_active"));
-  value = sh_util_flagval(s, &ShfInotifyActive);
-  if (value == 0 && ShfInotifyActive != S_FALSE)
-    {
-      sh.flag.inotify |= SH_INOTIFY_USE;
-      sh.flag.inotify |= SH_INOTIFY_DOSCAN;
-      sh.flag.inotify |= SH_INOTIFY_NEEDINIT;
-    }
-  if (value == 0 && ShfInotifyActive == S_FALSE)
-    {
-      sh.flag.inotify = 0;
-    }
-  SL_RETURN((value), _("sh_fInotify_active"));
-}
-
-static int sh_fInotify_watches(const char *s) 
-{
-  int retval = -1;
-  char * foo;
-  unsigned long value;
-    
-  SL_ENTER(_("sh_fInotify_watches"));
-
-  value = strtoul(s, &foo, 0);
-  if (*foo == '\0')
-    {
-      ShfInotifyWatches = (value > 2147483647) ? 2147483647 /* MAX_INT_32 */: value;
-      retval = 0;
-    }
-  SL_RETURN((retval), _("sh_fInotify_watches"));
-}
-  
-  
-sh_rconf sh_fInotify_table[] = {
-    {
-        N_("inotifyactive"),
-        sh_fInotify_active,
-    },
-    {
-        N_("inotifywatches"),
-        sh_fInotify_watches,
-    },
-    {
-        NULL,
-        NULL
-    }
-};
-
-/* --- End Configuration --- */
-
-static int sh_fInotify_init_internal(void);
-static int sh_fInotify_process(struct inotify_event * event);
-static int sh_fInotify_report(struct inotify_event * event, char * filename,
-			      int class, unsigned long check_flags, int ftype, int rdepth);
-
-int sh_fInotify_init(struct mod_type * arg)
-{
-#ifndef HAVE_PTHREAD
-  (void) arg;
-  return SH_MOD_FAILED;
-#else
-
-  if (ShfInotifyActive == S_FALSE)
-    return SH_MOD_FAILED;
-
-  if (sh.flag.checkSum == SH_CHECK_INIT)
-    return SH_MOD_FAILED;
-
-  if (arg != NULL && arg->initval < 0 &&
-      (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
-    {
-      /* Init from main thread */
-      SH_INOTIFY_IFUSED( sh.flag.inotify |= SH_INOTIFY_DOSCAN;   );
-      SH_INOTIFY_IFUSED( sh.flag.inotify |= SH_INOTIFY_NEEDINIT; );
-
-      if (0 == sh_pthread_create(sh_threaded_module_run, (void *)arg))
-	{
-	  return SH_MOD_THREAD;
-	}
-      else
-	{
-	  sh.flag.inotify = 0;
-	  return SH_MOD_FAILED;
-	}
-    }
-  else if (arg != NULL && arg->initval < 0 &&
-      (sh.flag.isdaemon != S_TRUE && sh.flag.loop != S_TRUE))
-    {
-      sh.flag.inotify = 0;
-      return SH_MOD_FAILED;
-    }
-  else if (arg != NULL && arg->initval == SH_MOD_THREAD &&
-	   (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
-    {
-      /* Reconfigure from main thread */
-
-      SH_INOTIFY_IFUSED( sh.flag.inotify |= SH_INOTIFY_DOSCAN;   );
-      SH_INOTIFY_IFUSED( sh.flag.inotify |= SH_INOTIFY_NEEDINIT; );
-      return SH_MOD_THREAD;
-    }
-
-  /* Within thread, init */
-  return sh_fInotify_init_internal();
-#endif
-}
-
-int sh_fInotify_run()
-{
-  ssize_t len = -1;
-  char *  buffer;
-  static int count  = 0;
-  static int count2 = 0;
-
-  if (ShfInotifyActive == S_FALSE)
-    {
-      return SH_MOD_FAILED;
-    }
-
-  if (ShfInotifyClose == S_TRUE)
-    {
-      ShfInotifyClose = S_FALSE;
-      sh_inotify_close();
-    }
-
-  if ( (sh.flag.inotify & SH_INOTIFY_DOSCAN) ||
-       (sh.flag.inotify & SH_INOTIFY_NEEDINIT))
-    {
-      if (0 != sh_fInotify_init_internal())
-	{
-	  return SH_MOD_FAILED;
-	}
-    }
-
-  buffer = SH_ALLOC(16384);
-
-  /* Blocking read from inotify file descriptor.
-   */
-  len = sh_inotify_read_timeout (buffer, 16384, 1);
-  
-  if (len > 0)
-    {
-      struct inotify_event *event;
-      long i = 0;
-
-      while (i < len) 
-	{
-	  event = (struct inotify_event *) &(buffer[i]);
-	  
-	  sh_fInotify_process(event);
-	  
-	  i += sizeof (struct inotify_event) + event->len;
-	}
-
-      if ( (sh.flag.inotify & SH_INOTIFY_DOSCAN) ||
-	   (sh.flag.inotify & SH_INOTIFY_NEEDINIT))
-	{
-	  if (0 != sh_fInotify_init_internal())
-	    {
-	      SH_FREE(buffer);
-	      return SH_MOD_FAILED;
-	    }
-	}
-     }
-
-  /* Re-scan 'dormant' list of sh_file_missing. 
-   */ 
-  sh_inotify_recheck_watches (&sh_file_watches, &sh_file_missing);
-
-  ++count; 
-  ++count2;
-
-  if (count >= 10)
-    {
-      count = 0; /* Re-expand glob patterns to discover added files. */
-      SH_INOTIFY_IFUSED( sh.flag.inotify |= SH_INOTIFY_INSCAN; );
-      sh_files_check_globFilePatterns();
-      SH_INOTIFY_IFUSED( sh.flag.inotify &= ~SH_INOTIFY_INSCAN;  );
-      SH_INOTIFY_IFUSED( sh.flag.inotify |= SH_INOTIFY_NEEDINIT; );
-    }
-
-  if (count2 >= 300)
-    {
-      count2 = 0; /* Update baseline database. */
-      if (sh.flag.checkSum == SH_CHECK_CHECK && sh.flag.update == S_TRUE)
-	sh_dbIO_writeout_update ();
-    }
-
-  SH_FREE(buffer);
-  return 0;
-}
-
-/* We block in the read() call on the inotify descriptor,
- * so we always run.
- */
-int sh_fInotify_timer(time_t tcurrent)
-{
-  (void) tcurrent;
-  return 1;
-}
-
-int sh_fInotify_cleanup()
-{
-  sh_inotify_purge_dormant(&sh_file_watches);
-  sh_inotify_remove(&sh_file_watches);
-  sh_inotify_init(&sh_file_watches);
-  return 0;
-}
-
-/* This runs in the main thread, so it won't close
- * the (threadspecific) inotify instance.
- */
-int sh_fInotify_reconf()
-{
-  sh.flag.inotify   = 0;
-
-  ShfInotifyWatches = 0;
-  ShfInotifyActive  = 0;
-
-  ShfInotifyClose   = S_TRUE;
-
-  return sh_fInotify_cleanup();
-}
-
-#define PROC_WATCHES_MAX _("/proc/sys/fs/inotify/max_user_watches")
-
-static void sh_fInotify_set_nwatches()
-{
-  static int fails = 0;
-
-  if (ShfInotifyWatches == 0 || fails == 1)
-    return;
-
-  if (0 == access(PROC_WATCHES_MAX, R_OK|W_OK)) /* flawfinder: ignore */
-    {
-      FILE * fd;
-
-      if (NULL != (fd = fopen(PROC_WATCHES_MAX, "r+")))
-	{
-	  char   str[128];
-	  char * ret;
-	  char * ptr;
-	  unsigned long  wn;
-
-	  str[0] = '\0';
-	  ret = fgets(str, 128, fd);
-	  if (ret && *str != '\0')
-	    {
-	      wn = strtoul(str, &ptr, 0);
-	      if (*ptr == '\0' || *ptr == '\n')
-		{
-		  if (wn < ShfInotifyWatches)
-		    {
-		      sl_snprintf(str, sizeof(str), "%lu\n", ShfInotifyWatches);
-		      (void) fseek(fd, 0L, SEEK_SET);
-		      fputs(str, fd);
-		    }
-		  sl_fclose(FIL__, __LINE__, fd);
-		  return;
-		}
-	    }
-	  sl_fclose(FIL__, __LINE__, fd);
-	}
-    }
-  SH_MUTEX_LOCK(mutex_thread_nolog);
-  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-		  _("Cannot set max_user_watches"), 
-		  _("sh_fInotify_set_nwatches"));
-  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-  fails = 1;
-  return;
-}
-
-/* The watch fd is thread specific. To have it in the fInotify thread,
- * the main thread writes a list of files/dirs to watch, and here we
- * now pop files from the list to add watches for them.
- */
-static int sh_fInotify_init_internal()
-{
-  char * filename;
-  int    class;
-  int    type;
-  int    rdepth;
-  unsigned long check_flags;
-  int    retval;
-  int    errnum;
-
-  if (ShfInotifyActive == S_FALSE)
-    return SH_MOD_FAILED;
-
-  /* Wait until file scan is finished.
-   */
-  while((sh.flag.inotify & SH_INOTIFY_DOSCAN) != 0)
-    {
-      retry_msleep(1,0);
-
-      if (ShfInotifyActive == S_FALSE)
-	return SH_MOD_FAILED;
-    }
-
-  sh_fInotify_set_nwatches();
-
-  while (NULL != (filename = sh_inotify_pop_dormant(&sh_file_watches, 
-						    &class, &check_flags, 
-						    &type, &rdepth)))
-    {
-      retval = sh_inotify_add_watch(filename, &sh_file_watches, &errnum,
-				    class, check_flags, type, rdepth);
-
-      if (retval < 0)
-	{
-	  char errbuf[SH_ERRBUF_SIZE];
-
-	  sh_error_message(errnum, errbuf, sizeof(errbuf));
-
-	  if ((errnum == ENOENT) || (errnum == EEXIST))
-	    {
-	      /* (1) Did it exist at init ? 
-	       */
-	      if (sh_hash_have_it (filename) >= 0)
-		{
-		  /* (2) Do we want to report on it ?
-		   */
-		  if (S_FALSE == sh_ignore_chk_del(filename))
-		    {
-		      char * epath = sh_util_safe_name (filename);
-
-		      SH_MUTEX_LOCK(mutex_thread_nolog);
-		      sh_error_handle( SH_ERR_ALL /* debug */,
-				       FIL__, __LINE__, errnum, MSG_E_SUBGPATH, 
-				       errbuf, _("sh_fInotify_init_internal"), epath);
-		      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-		      SH_FREE(epath);
-		    }
-		}
-	    }
-	  else
-	    {
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle((-1), FIL__, __LINE__, errnum, MSG_E_SUBGEN, 
-			       errbuf, _("sh_fInotify_init_internal"));
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	    }
-	}
-      SH_FREE(filename);
-    }
-
-  /* Need this because mod_check() may run after
-   * DOSCAN is finished, hence wouldn't call init().
-   */
-  SH_INOTIFY_IFUSED( sh.flag.inotify &= ~SH_INOTIFY_NEEDINIT; );
-
-  return 0;
-}
-
-static void sh_fInotify_logmask(struct inotify_event * event)
-{
-  char dbgbuf[256];
-  
-  sl_strlcpy (dbgbuf, "inotify mask: ", sizeof(dbgbuf));
-  
-  if (event->mask & IN_ACCESS) sl_strlcat(dbgbuf, "IN_ACCESS ", sizeof(dbgbuf));
-  if (event->mask & IN_ATTRIB) sl_strlcat(dbgbuf, "IN_ATTRIB ", sizeof(dbgbuf));
-  if (event->mask & IN_CLOSE_WRITE) sl_strlcat(dbgbuf, "IN_CLOSE_WRITE ", sizeof(dbgbuf));
-  if (event->mask & IN_CLOSE_NOWRITE) sl_strlcat(dbgbuf, "IN_CLOSE_NOWRITE ", sizeof(dbgbuf));
-  if (event->mask & IN_CREATE) sl_strlcat(dbgbuf, "IN_CREATE ", sizeof(dbgbuf));
-  if (event->mask & IN_DELETE) sl_strlcat(dbgbuf, "IN_DELETE ", sizeof(dbgbuf));
-  if (event->mask & IN_DELETE_SELF) sl_strlcat(dbgbuf, "IN_DELETE_SELF ", sizeof(dbgbuf));
-  if (event->mask & IN_MODIFY) sl_strlcat(dbgbuf, "IN_MODIFY ", sizeof(dbgbuf));
-  if (event->mask & IN_MOVE_SELF) sl_strlcat(dbgbuf, "IN_MOVE_SELF ", sizeof(dbgbuf));
-  if (event->mask & IN_MOVED_FROM) sl_strlcat(dbgbuf, "IN_MOVED_FROM ", sizeof(dbgbuf));
-  if (event->mask & IN_MOVED_TO) sl_strlcat(dbgbuf, "IN_MOVED_TO ", sizeof(dbgbuf));
-  if (event->mask & IN_OPEN) sl_strlcat(dbgbuf, "IN_OPEN ", sizeof(dbgbuf));
-  if (event->mask & IN_IGNORED) sl_strlcat(dbgbuf, "IN_IGNORED ", sizeof(dbgbuf));
-  if (event->mask & IN_ISDIR) sl_strlcat(dbgbuf, "IN_ISDIR ", sizeof(dbgbuf));
-  if (event->mask & IN_Q_OVERFLOW) sl_strlcat(dbgbuf, "IN_Q_OVERFLOW ", sizeof(dbgbuf));
-  if (event->mask & IN_UNMOUNT) sl_strlcat(dbgbuf, "IN_UNMOUNT ", sizeof(dbgbuf));
-  
-  SH_MUTEX_LOCK(mutex_thread_nolog);
-  sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-		  dbgbuf, _("sh_fInotify_process"));
-  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-}
-
-static int sh_fInotify_process(struct inotify_event * event)
-{
-  int class;
-  int ftype;
-  int rdepth;
-  unsigned long check_flags;
-  char * filename;
-  extern int flag_err_debug;
-
-  if (flag_err_debug == S_TRUE)
-    {
-      sh_fInotify_logmask(event);
-    }
-
-  if (event->wd >= 0)
-    {
-      filename = sh_inotify_search_item(&sh_file_watches, event->wd, 
-					&class, &check_flags, &ftype, &rdepth);
-
-      if (filename)
-	{
-	  sh_fInotify_report(event, filename, class, check_flags, ftype, rdepth);
-	  SH_FREE(filename);
-	}
-      else if (sh.flag.inotify & SH_INOTIFY_NEEDINIT)
-	{
-	  return 1;
-	}
-      else if ((event->mask & IN_UNMOUNT) == 0 && (event->mask & IN_IGNORED) == 0)
-	{
-	  /* Remove watch ? Seems reasonable. */
-	  sh_inotify_rm_watch(NULL, NULL, event->wd);
-
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, event->wd, MSG_E_SUBGEN, 
-			  _("Watch removed: file path unknown"), 
-			  _("sh_fInotify_process"));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	}
-    }
-  else if ((event->mask & IN_Q_OVERFLOW) != 0)
-    {
-      SH_INOTIFY_IFUSED( sh.flag.inotify |= SH_INOTIFY_DOSCAN;   );
-      SH_INOTIFY_IFUSED( sh.flag.inotify |= SH_INOTIFY_NEEDINIT; );
-
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, event->wd, MSG_E_SUBGEN, 
-		      _("Inotify queue overflow"), 
-		      _("sh_fInotify_process"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      return 1;
-    }
-
-  return 0;
-}
-
-void sh_fInotify_report_add(char * path, int class, unsigned long check_flags)
-{
-  if (S_FALSE == sh_ignore_chk_new(path))
-    {
-      int reported = 0;
-
-      sh_files_clear_file_reported(path);
-      
-      sh_files_search_file(path, &class, &check_flags, &reported);
-      
-      sh_files_filecheck (class, check_flags, path, NULL,
-			  &reported, 0);
-      if (SH_FFLAG_REPORTED_SET(reported))
-	sh_files_set_file_reported(path);
-    }
-  return;
-}
-
-
-static void sh_fInotify_report_miss(char * name, int level)
-{
-  char * tmp = sh_util_safe_name (name);
-
-  SH_MUTEX_LOCK(mutex_thread_nolog);
-  if (!sh_global_check_silent)
-    sh_error_handle (level, FIL__, __LINE__, 0, MSG_FI_MISS, tmp);
-  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-  ++sh.statistics.files_report;
-  SH_FREE(tmp);
-  return;
-}
-
-static int sh_fInotify_report_change (struct inotify_event * event, 
-				      char * path, char * filename,
-				      int class, unsigned long check_flags, int ftype)
-{
-  int    reported;
-  int ret;
-
-
-  if (S_FALSE == sh_ignore_chk_mod(path))
-    {
-      ret  = sh_files_search_file(path, &class, &check_flags, &reported);
-
-      if ((ret == 0) && (event->len > 0) && (ftype == SH_INOTIFY_FILE))
-	{
-	  ; /* do nothing, watch was for directory monitored as file only */
-	}
-      else
-	{
-	  sh_files_filecheck (class, check_flags, filename,
-			      (event->len > 0) ? event->name : NULL,
-			      &reported, 0);
-	}
-    }
-  return 0;
-}
-
-
-static int sh_fInotify_report_missing (struct inotify_event * event, 
-				       char * path,
-				       int class, unsigned long check_flags, int ftype)
-{
-  int    reported;
-  int isdir = (event->mask & IN_ISDIR);
-  int level = (class == SH_LEVEL_ALLIGNORE) ? 
-    ShDFLevel[class] : 
-    ShDFLevel[(isdir == 0) ? SH_ERR_T_FILE : SH_ERR_T_DIR];
-
-  if (S_FALSE == sh_ignore_chk_del(path))
-    {
-      if (0 != hashreport_missing(path, level))
-	{
-	  int ret = sh_files_search_file(path, &class, &check_flags, &reported);
-	  
-	  if ((ret == 0) && (event->len > 0) && (ftype == SH_INOTIFY_FILE))
-	    {
-	      ; /* do nothing, watch was for directory monitored as file only */
-	    }
-	  else
-	    {
-	      /* Removal of a directory triggers:
-	       * (1) IN_DELETE IN_ISDIR
-	       * (2) IN_DELETE_SELF
-	       */
-	      if ((event->mask & IN_DELETE_SELF) == 0)
-		sh_fInotify_report_miss(path, level);
-	    }
-	}
-    }
-
-  sh_hash_set_missing(path);
-
-  if (sh.flag.reportonce == S_TRUE)
-    sh_files_set_file_reported(path);
-
-  /* Move to 'dormant' list, if not file within directory. 
-   */
-  if (event->len == 0)
-    sh_inotify_rm_watch(&sh_file_watches, &sh_file_missing, event->wd);
-
-  return 0;
-}
-
-static int sh_fInotify_report_added (struct inotify_event * event, 
-				     char * path, char * filename,
-				     int class, unsigned long check_flags, 
-				     int ftype, int rdepth)
-{
-  if (S_FALSE == sh_ignore_chk_new(path))
-    {
-      int reported;
-      int ret;
-      int retD = 0;
-      int rdepthD = rdepth;
-      
-      sh_files_clear_file_reported(path);
-	  
-      ret = sh_files_search_file(path, &class, &check_flags, &reported);
-      
-      if ((ret == 0) && (event->len > 0) && (ftype == SH_INOTIFY_FILE))
-	{
-	  ; /* do nothing, watch was for directory monitored as file only */
-	}
-      else
-	{
-	  int classD = class;
-	  int reportedD = reported; 
-	  unsigned long check_flagsD = check_flags;
-	  
-	  if (event->mask & IN_ISDIR)
-	    {
-	      retD = sh_files_search_dir(path, &classD, &check_flagsD, 
-					 &reportedD, &rdepthD);
-
-	      if (retD != 0)
-		{
-		  if (ret == 0) /* because checked as file below */
-		    {
-		      class      = classD;
-		      check_flags = check_flagsD;
-		    }
-		}
-	      rdepthD -= 1; if (rdepthD < -1) rdepthD = -1;
-	    }
-	  
-	  sh_files_filecheck (class, check_flags, filename,
-			      (event->len > 0) ? event->name : NULL,
-			      &reported, 0);
-	  
-	  if (event->mask & IN_ISDIR)
-	    {
-	      SH_INOTIFY_IFUSED( sh.flag.inotify |= SH_INOTIFY_INSCAN;   );
-	      /* Here we use classD because we check as directory now */
-	      sh_files_checkdir (classD, check_flagsD, rdepthD, 
-				 path, (event->len > 0) ? event->name : NULL);
-	      SH_INOTIFY_IFUSED( sh.flag.inotify &= ~SH_INOTIFY_INSCAN;  );
-	      SH_INOTIFY_IFUSED( sh.flag.inotify |= SH_INOTIFY_NEEDINIT; );
-	      sh_dirs_reset  ();
-	      sh_files_reset ();
-	    }
-	}
-      
-      if (SH_FFLAG_REPORTED_SET(reported))
-	sh_files_set_file_reported(path);
-      
-      if ((ret != 0) || (event->mask & IN_ISDIR))
-	{
-	  int inotify_type = SH_INOTIFY_FILE;
-	  
-	  if ((event->mask & IN_ISDIR) && (rdepthD >= 0))
-	    inotify_type = SH_INOTIFY_DIR;
-
-	  sh_inotify_add_watch(path, &sh_file_watches, &ret,
-			       class, check_flags, 
-			       inotify_type, 
-			       rdepthD);
-	}
-    }
-  return 0;
-}
-
-static int sh_fInotify_report(struct inotify_event * event, char * filename,
-			      int class, unsigned long check_flags, int ftype, int rdepth)
-{
-  char * fullpath = NULL;
-  char * path;
-
-  if (event->len > 0)
-    {
-      fullpath = sh_util_strconcat(filename, "/", event->name, NULL);
-      path = fullpath;
-    }
-  else
-    {
-      path = filename;
-    }
-
-  if ( (event->mask & (IN_ATTRIB|IN_MODIFY)) != 0)
-    {
-      sh_fInotify_report_change (event, path, filename,
-				 class, check_flags, ftype);
-    }
-  else if ((event->mask & (IN_DELETE|IN_DELETE_SELF|IN_MOVE_SELF|IN_MOVED_FROM)) != 0)
-    {
-      sh_fInotify_report_missing (event, path,
-				  class, check_flags, ftype);
-   }
-  else if((event->mask & (IN_CREATE|IN_MOVED_TO)) != 0)
-    {
-      sh_fInotify_report_added (event, path, filename,
-				class, check_flags, 
-				ftype, rdepth);
-    }
-
-  if (fullpath)
-    SH_FREE(fullpath);
-
-  return 0;
-}
-
-
-#endif
-
-#endif
Index: trunk/src/sh_fifo.c
===================================================================
--- trunk/src/sh_fifo.c	(revision 591)
+++ trunk/src/sh_fifo.c	(revision 1)
@@ -32,7 +32,4 @@
 #include "sh_mem.h"
 #include "sh_unix.h"
-#include "sh_utils.h"
-#include "sh_string.h"
-#include "sh_fifo.h"
 
 #if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
@@ -40,159 +37,23 @@
 #endif
 
-#define SH_FIFO_TAGGED 1
-#define SH_FIFO_M_FAIL 2
-#define SH_FIFO_MARKED 4
-
-/* Prepare an email message and return it. Iterate over list on stack and
- * check for each if it is valid for recipient 'tag'. If yes, add to the
- * returned string.
- * okNull == False means that item->s_xtra must be defined
- */
-sh_string * tag_list (SH_FIFO * fifo, char * tag,
-		      int(*valid)(int, const char*, const char*, const void*),
-		      const void * info, int okNull)
-{
-  struct dlist * item;
-  sh_string * result = NULL;
-
-  if (fifo && fifo->fifo_cts > 0)
-    {
-      item = fifo->head_ptr;
-
-      while (item)
-	{
-	  /* Same recipient, or no recipient ( := all )
-	   */
-	  if ( (tag && item->s_xtra && 0 == strcmp(item->s_xtra, tag)) ||
-	       ((okNull == S_TRUE) && !(item->s_xtra)) )
-	    {
-	      if (valid == NULL)
-		{
-		  item->transact |= SH_FIFO_TAGGED;
-		}
-	      else
-		{
-		  /* level, message, recipient, list */
-		  if (!valid(item->i_xtra, item->data, tag, info))
-		    goto skipped;
-		  item->transact |= SH_FIFO_TAGGED;
-		}
-	      if (!result)
-		{
-		  result = sh_string_new_from_lchar(item->data, strlen(item->data));
-		}
-	      else
-		{
-		  result = sh_string_cat_lchar(result, "\r\n", 2);
-		  result = sh_string_add_from_char(result, item->data);
-		}
-	    }
-	skipped:
-	  item = item->next;
-	}
-    }
-  return result;
-}
-
-void rollback_list (SH_FIFO * fifo)
-{
-  struct dlist * item;
-
-  if (fifo && fifo->fifo_cts > 0)
-    {
-      item = fifo->head_ptr;
-
-      while (item && 0 != (item->transact & SH_FIFO_TAGGED))
-	{
-	  item->transact |= SH_FIFO_M_FAIL;
-	  item = item->next;
-	}
-    }
-}
-
-void mark_list (SH_FIFO * fifo)
-{
-  struct dlist * item;
-
-  if (fifo && fifo->fifo_cts > 0)
-    {
-      item = fifo->head_ptr;
-
-      while (item && 0 != (item->transact & SH_FIFO_TAGGED))
-	{
-	  item->transact |= SH_FIFO_MARKED;
-	  item = item->next;
-	}
-    }
-}
-
-void reset_list (SH_FIFO * fifo)
-{
-  struct dlist * item;
-
-  if (fifo && fifo->fifo_cts > 0)
-    {
-      item = fifo->head_ptr;
-
-      while (item)
-	{
-	  item->transact = 0;
-	  item = item->next;
-	}
-    }
-}
-
-int commit_list (SH_FIFO * fifo)
-{
-  struct dlist * item;
-  struct dlist * getit;
-  int    retval = 0;
-
-  if (fifo && fifo->fifo_cts > 0)
-    {
-      item = fifo->head_ptr;
-
-      while (item)
-	{
-	  getit = NULL;
-
-	  if ( 0 != (item->transact & SH_FIFO_MARKED) && /* sent              */
-	       0 == (item->transact & SH_FIFO_M_FAIL) )  /* no recipient fail */
-	    {
-	      if (item == fifo->head_ptr)
-		fifo->head_ptr   = item->next;
-	      if (item == fifo->tail_ptr)
-		fifo->tail_ptr   = item->prev;
-	      if (item->prev)
-		item->prev->next = item->next;
-	      if (item->next)
-		item->next->prev = item->prev;
-	      --(fifo->fifo_cts);
-	      getit = item;
-	    }
-	  item  = item->next;
-
-	  /* Delete it
-	   */
-	  if (getit)
-	    {
-	      size_t len = sl_strlen(getit->data);
-	      memset(getit->data, 0, len);
-	      if (NULL != sl_strstr (getit->data, _("LOGKEY")))
-		MUNLOCK(getit->data, (len+1));
-	      if (getit->s_xtra)
-		SH_FREE(getit->s_xtra);
-	      SH_FREE(getit->data);
-	      SH_FREE(getit);
-	      ++retval;
-	    }
-	}
-    }
-  return retval;
-}
+#define SH_FIFO_MAX 128
+
+struct dlist {
+  /*@null@*//*@dependent@*/ struct dlist * next;
+  char         * data;
+  /*@null@*//*@dependent@*/ struct dlist * prev;
+};
+
+typedef struct fifo_str {
+  /*@null@*//*@dependent@*/ struct dlist * head_ptr;
+  /*@null@*//*@dependent@*/ struct dlist * tail_ptr;
+  int            fifo_cts;
+} SH_FIFO;
+
+
 
 /* push an item on the head of the list
  */
-int push_list (SH_FIFO * fifo, const char * indat, int in_i, const char * in_str)
+int push_list (SH_FIFO * fifo, char * indat)
 {
   struct dlist * item;
@@ -218,18 +79,14 @@
     }
   item            = SH_ALLOC(sizeof(struct dlist));
-  item->data      = SH_ALLOC(len+1);
+  /*@i@*/ item->data      = SH_ALLOC(len+1);
   
   if (NULL != sl_strstr (indat, _("LOGKEY")))
-    MLOCK(item->data, (len+1));
-
-  sl_strlcpy (item->data, indat, len+1);
+    {
+      MLOCK(item->data, (len+1));
+      ;
+    }
+
+  strcpy (item->data, indat);                   /* known to fit  */
   item->data[len] = '\0';
-
-  item->i_xtra = in_i;
-  if (in_str)
-    item->s_xtra = sh_util_strdup(in_str);
-  else
-    item->s_xtra = NULL;
-  item->transact = 0;
 
   if (fifo->tail_ptr == NULL)
@@ -240,5 +97,5 @@
   else
     {
-      fifo->head_ptr->prev = item;
+      /*@i@*/ fifo->head_ptr->prev = item;
       item->prev           = NULL;
     }
@@ -254,5 +111,5 @@
 /* push an item on the tail of the list
  */
-int push_tail_list (SH_FIFO * fifo, const char * indat, int in_i, const char * in_str)
+int push_tail_list (SH_FIFO * fifo, char * indat)
 {
   struct dlist * item;
@@ -278,18 +135,14 @@
 
   item            = SH_ALLOC(sizeof(struct dlist));
-  item->data      = SH_ALLOC(len+1);
+  /*@i@*/item->data      = SH_ALLOC(len+1);
 
   if (NULL != sl_strstr (indat, _("LOGKEY")))
-    MLOCK(item->data, (len+1));
-
-  sl_strlcpy (item->data, indat, len+1);
+    {
+      MLOCK(item->data, (len+1));
+      ;
+    }
+
+  strcpy (item->data, indat);                          /* known to fit  */
   item->data[len] = '\0';
-
-  item->i_xtra = in_i;
-  if (in_str)
-    item->s_xtra = sh_util_strdup(in_str);
-  else
-    item->s_xtra = NULL;
-  item->transact = 0;
 
   if (fifo->head_ptr == NULL)
@@ -309,5 +162,5 @@
   ++(fifo->fifo_cts);
 
-  SL_RETURN((fifo->fifo_cts), _("push_tail_list"));
+  SL_RETURN((0), _("push_tail_list"));
 }
 
@@ -342,13 +195,15 @@
   len         = sl_strlen(getit->data);
   retval      = SH_ALLOC(len+1);
-  sl_strlcpy (retval, getit->data, len+1);
+  strcpy (retval, getit->data);                        /* known to fit  */
+  retval[len] = '\0';
  
   memset(getit->data, 0, len);
 
   if (NULL != sl_strstr (retval, _("LOGKEY")))
-    MUNLOCK(getit->data, (len+1));
-
-  if (getit->s_xtra)
-    SH_FREE(getit->s_xtra);
+    {
+      MUNLOCK(getit->data, (len+1));
+      ;
+    }
+
   SH_FREE(getit->data);
   SH_FREE(getit);
@@ -360,82 +215,4 @@
 
 
-#ifdef SH_CUTEST
-#include "CuTest.h"
-
-void Test_fifo (CuTest *tc) {
-
-  SH_FIFO ff;
-  int ret;
-  char * p;
-
-  fifo_init(&ff);
-
-  p = sh_fifo_pop(&ff);
-  CuAssertPtrEquals(tc, NULL, p);
-
-  /* first sequence */
-  ret = sh_fifo_push(&ff, "one");
-  CuAssertIntEquals(tc,1,ret);
-  ret = sh_fifo_push(&ff, "two");
-  CuAssertIntEquals(tc,2,ret);
-  ret = sh_fifo_push(&ff, "three");
-  CuAssertIntEquals(tc,3,ret);
-
-  p = sh_fifo_pop(&ff);
-  CuAssertPtrNotNull(tc, p);
-  CuAssertStrEquals(tc,"one", p);
-  p = sh_fifo_pop(&ff);
-  CuAssertPtrNotNull(tc, p);
-  CuAssertStrEquals(tc,"two", p);
-  p = sh_fifo_pop(&ff);
-  CuAssertPtrNotNull(tc, p);
-  CuAssertStrEquals(tc,"three", p);
-  p = sh_fifo_pop(&ff);
-  CuAssertPtrEquals(tc, NULL, p);
-
-  /* second sequence */
-  ret = sh_fifo_push(&ff, "one");
-  CuAssertIntEquals(tc,1,ret);
-  p = sh_fifo_pop(&ff);
-  CuAssertPtrNotNull(tc, p);
-  CuAssertStrEquals(tc,"one", p);
-  ret = sh_fifo_push_tail(&ff, "one");
-  CuAssertIntEquals(tc,1,ret);
-  p = sh_fifo_pop(&ff);
-  CuAssertPtrNotNull(tc, p);
-  CuAssertStrEquals(tc,"one", p);
-  p = sh_fifo_pop(&ff);
-  CuAssertPtrEquals(tc, NULL, p);
-
-  /* third sequence */
-  ret = sh_fifo_push(&ff, "one");
-  CuAssertIntEquals(tc,1,ret);
-  ret = sh_fifo_push(&ff, "two");
-  CuAssertIntEquals(tc,2,ret);
-  ret = sh_fifo_push(&ff, "three");
-  CuAssertIntEquals(tc,3,ret);
-
-  p = sh_fifo_pop(&ff);
-  CuAssertPtrNotNull(tc, p);
-  CuAssertStrEquals(tc,"one", p);
-  ret = sh_fifo_push_tail(&ff, p);
-  CuAssertIntEquals(tc,3,ret);
-
-  p = sh_fifo_pop(&ff);
-  CuAssertPtrNotNull(tc, p);
-  CuAssertStrEquals(tc,"one", p);
-  p = sh_fifo_pop(&ff);
-  CuAssertPtrNotNull(tc, p);
-  CuAssertStrEquals(tc,"two", p);
-  p = sh_fifo_pop(&ff);
-  CuAssertPtrNotNull(tc, p);
-  CuAssertStrEquals(tc,"three", p);
-  p = sh_fifo_pop(&ff);
-  CuAssertPtrEquals(tc, NULL, p);
-}
-
-#endif
-
-
-
-
+
+
Index: trunk/src/sh_files.c
===================================================================
--- trunk/src/sh_files.c	(revision 591)
+++ trunk/src/sh_files.c	(revision 1)
@@ -20,42 +20,7 @@
 #include "config_xor.h"
 
-#if defined(HAVE_PTHREAD_MUTEX_RECURSIVE) && defined(HAVE_DIRENT_H) && defined(HAVE_SCANDIR)
-
-/* Linux */
-#if defined(__linux__)
-#define _XOPEN_SOURCE 700
-#define SH_USE_SCANDIR 1
-
-/* FreeBSD */
-#elif defined(__FreeBSD__)
-
-#if __FreeBSD__ >= 8
-#define __XSI_VISIBLE 700
-#define SH_USE_SCANDIR 1
-#endif
-
-/* OpenBSD */
-#elif defined(__OpenBSD__)
-#define __POSIX_VISIBLE 200809
-#define SH_USE_SCANDIR 1
-
-#elif defined(__NetBSD__)
-#define _NETBSD_SOURCE
-#define SH_USE_SCANDIR 1
-
-/* other os */
-#else
-#define _XOPEN_SOURCE 500
-
-#endif
-
-#elif defined(HAVE_PTHREAD_MUTEX_RECURSIVE)
-#define _XOPEN_SOURCE 500
-#endif
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <ctype.h>
 #include <limits.h>
 
@@ -66,14 +31,7 @@
 #include <sys/types.h>
 #include <unistd.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-
-#if !defined(O_NOATIME)
-#if defined(__linux__) && (defined(__i386__) || defined(__x86_64__) || defined(__PPC__))
-#define O_NOATIME 01000000
-#endif
-#endif
 
 #include <utime.h>
+
 
 #ifdef HAVE_DIRENT_H
@@ -93,13 +51,8 @@
 #endif
 #endif
-#define NEED_ADD_DIRENT
 
 #ifdef HAVE_GLOB_H
 #include <glob.h>
 #endif
-#ifdef HAVE_FNMATCH_H
-#include <fnmatch.h>
-#endif
-
 
 #include "samhain.h"
@@ -107,5 +60,4 @@
 #if (defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)) 
 
-#include "sh_pthread.h"
 #include "sh_error.h"
 #include "sh_utils.h"
@@ -115,170 +67,15 @@
 #include "sh_hash.h"
 #include "sh_ignore.h"
-#include "sh_inotify.h"
 #include "zAVLTree.h"
-#include "sh_dbIO.h"
 
 #undef  FIL__
 #define FIL__  _("sh_files.c")
 
-extern sh_watches sh_file_watches;
-
-static char * sh_files_C_dequote (char * s, size_t * length)
-{
-  size_t i, len = *length;
-  int    flag = 0;
-  char  *p, *q, *po, *pend;
-  
-  /* search for backslash
-   */
-  for (i = 0; i < len; ++i)
-    {
-      if (s[i] == '\\')
-	{
-	  flag = 1;
-	  break;
-	}
-    }
-
-  if (flag == 0 || *s == '\0')
-    return s;
-
-  po = SH_ALLOC(len+1); *po = '\0'; p = po; pend = &po[len];
-
-  q = s;
-
-  do
-    {
-      if (*q == '\\')
-	{
-	  ++q;
-
-	  if (*q == '\0')
-	    { *p = *q; flag = 0; break; }
-	  else if (*q == 'a')
-	    { *p = '\a'; ++p; ++q; }
-	  else if (*q == 'b')
-	    { *p = '\b'; ++p; ++q; }
-	  else if (*q == 'f')
-	    { *p = '\f'; ++p; ++q; }
-	  else if (*q == 'n')
-	    { *p = '\n'; ++p; ++q; }
-	  else if (*q == 'r')
-	    { *p = '\r'; ++p; ++q; }
-	  else if (*q == 't')
-	    { *p = '\t'; ++p; ++q; }
-	  else if (*q == 'v')
-	    { *p = '\v'; ++p; ++q; }
-	  else if (*q == '\\')
-	    { *p = '\\'; ++p; ++q; }
-	  else if (*q == '\'')
-	    { *p = '\''; ++p; ++q; }
-	  else if (*q == '"')
-	    { *p = '"';  ++p; ++q; }
-	  else if (*q == 'x')
-	    {
-	      if (isxdigit((int) q[1]) && isxdigit((int) q[2]))
-		{
-		  /* hexadecimal value following */
-		  unsigned char cc = (16 * sh_util_hexchar(q[1])) 
-		    + sh_util_hexchar(q[2]);
-		  *p = (char) cc;
-		  ++p; q += 3;
-		}
-	      else
-		{
-		  *p = '\0'; flag = 0; break;
-		}
-	    }
-	  else if (isdigit((int)*q))
-	    {
-	      if (isdigit((int) q[1]) && q[1] < '8' && 
-		  isdigit((int) q[2]) && q[2] < '8')
-		{
-		  /* octal value following */
-		  char tmp[4];  unsigned char cc;
-		  tmp[0] = *q; ++q; tmp[1] = *q; ++q; tmp[2] = *q; ++q; 
-		  tmp[3] = '\0';
-		  cc = strtol(tmp, NULL, 8);
-		  *p = (char) cc; ++p;
-		}
-	      else
-		{
-		  *p = '\0'; flag = 0; break;
-		}
-	    }
-	  else
-	    {
-	      /* invalid escape sequence */
-	      *p = '\0'; flag = 0; break;
-	    }
-	}
-      else
-	{
-	  *p = *q; 
-	  ++p; ++q;
-	}
-    } while (*q && p <= pend);
-
-  SL_REQUIRE (p <= pend, _("p <= pend"));
-
-  if (flag)
-    {
-      *p = '\0';
-      *length = strlen(po);
-    }
-  else
-    {
-      SH_FREE(po);
-      po = NULL;
-      *length = 0;
-    }
-
-  SL_REQUIRE (*length <= len, _("*length <= len"));
-
-  SH_FREE(s);
-  return po;
-}
-
-char * sh_files_parse_input(const char * str_s, size_t * len)
-{
-  char  * p;
-  
-  if (!str_s || *str_s == '\0')
-    return NULL;
-
-  *len = sl_strlen(str_s);
-  if (sh.flag.checkSum == SH_CHECK_INIT)
-    {
-      size_t addspace = sh_dbIO_get_rootfs_len();
-      if (addspace != 0 && S_TRUE == sl_ok_adds (*len, addspace))
-	*len += addspace;
-    }
-  
-  if ( (str_s[0] == '"'  && str_s[*len-1] == '"' ) ||
-       (str_s[0] == '\'' && str_s[*len-1] == '\'') )
-    {
-      if (*len < 3)
-	return NULL;
-      --(*len);
-      p = sh_util_strdup_l(&str_s[1], *len);
-      p[*len-1] = '\0';
-      --(*len);
-    }
-  else
-    {
-      p = sh_util_strdup_l(str_s, *len);
-    }
-
-  p = sh_files_C_dequote(p, len);
-
-  return p;
-}
-
+extern int safe_logger (int signal, int method, pid_t thepid);
 
 extern int flag_err_debug;
 extern int flag_err_info;
 
-int sh_files_reportonce(const char * c)
+int sh_files_reportonce(char * c)
 {
   int i;
@@ -289,5 +86,5 @@
 }
     
-int sh_files_fulldetail(const char * c)
+int sh_files_fulldetail(char * c)
 {
   int i;
@@ -307,6 +104,4 @@
   long    NumCDev;
   long    NumBDev;
-  long    NumDoor;
-  long    NumPort;
   long    NumAll;
   long    TotalBytes;
@@ -317,9 +112,9 @@
   char                  * name;
   int                     class;
-  unsigned long           check_flags;
+  unsigned long           check_mask;
   int                     rdepth;
   short                   checked;
   short                   childs_checked;
-  short                   is_reported;
+  short                   reported;
   /* struct dirstack_entry * next; */
 } dirstack_t;
@@ -338,5 +133,4 @@
     here = (dirstack_t *) inptr;
 
-  /* cppcheck-suppress uninitStructMember */
   if (here->name != NULL)
     SH_FREE(here->name);
@@ -354,10 +148,4 @@
 }
 
-#define SH_LIST_FILE 0
-#define SH_LIST_DIR1 1
-#define SH_LIST_DIR2 2
-
-
-static int which_dirList = SH_LIST_DIR1;
 
 static zAVLTree * zdirListOne   = NULL;
@@ -365,13 +153,14 @@
 static zAVLTree * zfileList     = NULL;
 
-SH_MUTEX_STATIC(mutex_zfiles,      PTHREAD_MUTEX_INITIALIZER);
-SH_MUTEX_STATIC(mutex_zglob,       PTHREAD_MUTEX_INITIALIZER);
-SH_MUTEX_RECURSIVE(mutex_zdirs);
-
-static int        sh_files_fullpath  (const char * testdir, 
-				      const char * d_name, 
+
+static int        sh_files_fullpath  (char * testdir, char * d_name, 
 				      char * statpath);
-static int        sh_files_pushdir   (int class, const char * str_s);
-static int        sh_files_pushfile  (int class, const char * str_s);
+static int        sh_files_pushdir   (int class, char * str_s);
+static int        sh_files_pushfile  (int class, char * str_s);
+static int        sh_files_checkdir  (int class, int rdepth, char * dirName,
+				      char * relativeName);
+static ShFileType sh_files_filecheck (int class, char * dirName, 
+				      char * fileName, int * reported,
+				      int rsrcflag);
 
 static long MaxRecursionLevel = 0;
@@ -379,5 +168,5 @@
 /* set default recursion level
  */
-int sh_files_setrecursion (const char * flag_s)
+int sh_files_setrecursion (char * flag_s)
 {
   long flag = 0;
@@ -389,5 +178,5 @@
     SL_RETURN((-1), _("sh_files_setrecursion"));
 
-  if (sh.flag.opts == S_TRUE)  
+  if (sh.flag.opts == 1)  
     reject = 1;
 
@@ -403,120 +192,4 @@
 }
 
-static int handle_filecheck_ret(dirstack_t * ptr, char * tmp_in, int status)
-{
-  int fcount = 0;
-  char * tmp;
-
-  if (!tmp_in)
-    tmp = sh_util_safe_name (ptr->name);
-  else
-    tmp = tmp_in;
-
-  if (status == SH_FILE_UNKNOWN && (!SH_FFLAG_REPORTED_SET(ptr->is_reported)))
-    {
-      TPT(( 0, FIL__, __LINE__, _("msg=<file: %s> status=<%d>\n"), 
-	    tmp, status));
-      
-      if ( sh.flag.checkSum == SH_CHECK_INIT  || 
-	   sh_hash_have_it (ptr->name) >= 0)
-	{
-	  if (S_FALSE == sh_ignore_chk_del(ptr->name))
-	    {
-	      if (0 != hashreport_missing(ptr->name, 
-					  (ptr->class == SH_LEVEL_ALLIGNORE) ? 
-					  ShDFLevel[ptr->class] : 
-					  ShDFLevel[SH_ERR_T_FILE])) {
-		if (tmp == NULL) 
-		  tmp = sh_util_safe_name (ptr->name);
-		if (!sh_global_check_silent)
-		  sh_error_handle ((ptr->class == SH_LEVEL_ALLIGNORE) ? 
-				   ShDFLevel[ptr->class] : 
-				   ShDFLevel[SH_ERR_T_FILE],
-				   FIL__, __LINE__, 0, MSG_FI_MISS,
-				   tmp);
-		++sh.statistics.files_report;
-	      }
-	    }
-	}
-      else /* not there at init, and still missing */
-	{
-	  if (tmp == NULL) 
-	    tmp = sh_util_safe_name (ptr->name);
-	  sh_error_handle (SH_ERR_NOTICE,
-			   FIL__, __LINE__, 0,
-			   MSG_FI_FAIL,
-			   tmp);
-	}
-
-      if (sh.flag.checkSum != SH_CHECK_INIT) 
-	sh_hash_set_missing(ptr->name);
-
-      if (sh.flag.reportonce == S_TRUE)
-	SET_SH_FFLAG_REPORTED(ptr->is_reported);
-    }
-  else 
-    {
-      /* exists (status >= 0), but was missing (reported == TRUE)
-       */
-      if (status != SH_FILE_UNKNOWN && SH_FFLAG_REPORTED_SET(ptr->is_reported))
-	{
-	  CLEAR_SH_FFLAG_REPORTED(ptr->is_reported);
-	  sh_hash_clear_flag(ptr->name, SH_FFLAG_ENOENT);
-	}
-      
-      /* Catchall
-       */
-      else if (status == SH_FILE_UNKNOWN)
-	{
-	  /* Thu Mar  7 15:09:40 CET 2002 Make sure missing file
-	   * is reported if ptr->reported == S_TRUE because the
-	   * file has been added.
-	   */
-	  if (sh_hash_have_it (ptr->name) >= 0 && 
-	      !SH_FFLAG_REPORTED_SET(ptr->is_reported))
-	    {
-	      if (S_FALSE == sh_ignore_chk_del(ptr->name))
-		{
-		  if (0 != hashreport_missing(ptr->name, 
-					      (ptr->class == SH_LEVEL_ALLIGNORE) ? 
-					      ShDFLevel[ptr->class] : 
-					      ShDFLevel[SH_ERR_T_FILE])) {
-		    if (tmp == NULL) 
-		      tmp = sh_util_safe_name (ptr->name);
-		    if (!sh_global_check_silent)
-		      sh_error_handle ((ptr->class == SH_LEVEL_ALLIGNORE)? 
-				       ShDFLevel[ptr->class] : 
-				       ShDFLevel[SH_ERR_T_FILE],
-				       FIL__, __LINE__, 0, MSG_FI_MISS,
-				       tmp);
-		    ++sh.statistics.files_report;
-		  }
-		}
-
-	      /* delete from database
-	       */
-	      if (sh.flag.checkSum != SH_CHECK_INIT) 
-		sh_hash_set_missing(ptr->name);
-	    }
-	  else
-	    {
-	      if (tmp == NULL) 
-		tmp = sh_util_safe_name (ptr->name);
-	      sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, 0,
-			       MSG_FI_FAIL,
-			       tmp);
-	      if (sh.flag.checkSum != SH_CHECK_INIT)
-		sh_hash_set_visited_true(ptr->name);
-	    }
-	}
-      
-      ++fcount;
-    }
-  if (!tmp_in)
-    SH_FREE(tmp);
-
-  return fcount;
-}
-
 
 unsigned long sh_files_chk ()
@@ -529,7 +202,6 @@
 
   dirstack_t * ptr;
-  char       * dir;
+  char       * base;
   char       * file;
-  int          tmp_reported;
   
   SL_ENTER(_("sh_files_chk"));
@@ -545,6 +217,6 @@
       if (ptr->checked == S_FALSE)
 	{
-	  dir  = sh_util_dirname (ptr->name);
-	  file = sh_util_basename (ptr->name);
+	  base = sh_util_basename (ptr->name);
+	  file = sh_util_filename (ptr->name);
 #if defined(WITH_TPT)
 	  tmp = sh_util_safe_name (ptr->name);
@@ -552,33 +224,124 @@
 
 	  
-	  if (flag_err_info == S_TRUE)
+	  if (flag_err_info == SL_TRUE)
 	    {
-	      char pstr[32];
 #if !defined(WITH_TPT)
 	      tmp = sh_util_safe_name (ptr->name);
 #endif
-	      sl_strlcpy(pstr, sh_hash_getpolicy(ptr->class), sizeof(pstr));
-	      sh_error_handle ((-1),  FIL__, __LINE__, 0, 
-			       MSG_FI_CHK, pstr, tmp);
+	      sh_error_handle ((-1),  FIL__, __LINE__, 0, MSG_FI_CHK,
+			       tmp);
 	    }
 
-	  if ((sh.flag.inotify & SH_INOTIFY_INSCAN) != 0)
-	    {
-	      sh_inotify_add_watch_later(ptr->name, &sh_file_watches, NULL,
-					 ptr->class, ptr->check_flags, 
-					 SH_INOTIFY_FILE, 0);
-	    }
-
 	  BREAKEXIT(sh_files_filecheck);
-	  tmp_reported = ptr->is_reported; /* fix aliasing warning */ 
-	  status = sh_files_filecheck (ptr->class, ptr->check_flags, dir, file, 
-				       &tmp_reported, 0);
-	  ptr->is_reported = tmp_reported;
+	  status = sh_files_filecheck (ptr->class, base, file, 
+				       (int *) &(ptr->reported), 0);
 	  
 	  TPT(( 0, FIL__, __LINE__, 
 		_("msg=<filecheck complete: %s> status=<%d> reported=<%d>\n"), 
-		tmp, status, ptr->is_reported));
-
-	  fcount += handle_filecheck_ret(ptr, tmp, status);
+		tmp, status, ptr->reported));
+
+	  if (status == SH_FILE_UNKNOWN && ptr->reported == S_FALSE) 
+	    {
+	      TPT(( 0, FIL__, __LINE__, _("msg=<file: %s> status=<%d>\n"), 
+		    tmp, status));
+
+	      if ( sh.flag.checkSum == SH_CHECK_INIT  || 
+		  sh_hash_have_it (ptr->name) >= 0)
+		{
+		  if (S_FALSE == sh_ignore_chk_del(ptr->name))
+		    {
+		      if (0 != hashreport_missing(ptr->name, 
+						  (ptr->class == SH_LEVEL_ALLIGNORE) ? 
+						  ShDFLevel[ptr->class] : 
+						  ShDFLevel[SH_ERR_T_FILE])) {
+			if (tmp == NULL) 
+			  tmp = sh_util_safe_name (ptr->name);
+			sh_error_handle ((ptr->class == SH_LEVEL_ALLIGNORE) ? 
+					 ShDFLevel[ptr->class] : 
+					 ShDFLevel[SH_ERR_T_FILE],
+					 FIL__, __LINE__, 0, MSG_FI_MISS,
+					 tmp);
+		      }
+		    }
+		}
+	      else /* not there at init, and still missing */
+		{
+		  if (tmp == NULL) 
+		    tmp = sh_util_safe_name (ptr->name);
+		  sh_error_handle (SH_ERR_NOTICE,
+				   FIL__, __LINE__, 0,
+				   MSG_FI_FAIL,
+				   tmp);
+		}
+#ifndef REPLACE_OLD
+	      /* this will tell that we have seen the file, and thus prevent
+	       * deletion from the database, resulting in an incomplete
+	       * message when the file reappears
+	       */
+	      if (sh.flag.checkSum != SH_CHECK_INIT) 
+		sh_hash_set_visited_true(ptr->name);
+#else
+	      if (sh.flag.checkSum != SH_CHECK_INIT) 
+		sh_hash_set_missing(ptr->name);
+#endif
+	      if (sh.flag.reportonce == S_TRUE)
+		ptr->reported = S_TRUE;
+	    }
+	  else 
+	    {
+	      /* exists (status >= 0), but was missing (reported == TRUE)
+	       */
+	      if (status != SH_FILE_UNKNOWN && ptr->reported == S_TRUE)
+		{
+		  ptr->reported = S_FALSE;
+		}
+	      /* Catchall
+	       */
+	      else if (status == SH_FILE_UNKNOWN)
+		{
+		  /* Thu Mar  7 15:09:40 CET 2002 Make sure missing file
+		   * is reported if ptr->reported == S_TRUE because the
+		   * file has been added.
+		   */
+		  if (sh_hash_have_it (ptr->name) >= 0)
+		    {
+		      if (S_FALSE == sh_ignore_chk_del(ptr->name))
+			{
+			  if (0 != hashreport_missing(ptr->name, 
+						      (ptr->class == SH_LEVEL_ALLIGNORE) ? 
+						      ShDFLevel[ptr->class] : 
+						      ShDFLevel[SH_ERR_T_FILE])) {
+			    if (tmp == NULL) 
+			      tmp = sh_util_safe_name (ptr->name);
+			    sh_error_handle ((ptr->class == SH_LEVEL_ALLIGNORE)? 
+					     ShDFLevel[ptr->class] : 
+					     ShDFLevel[SH_ERR_T_FILE],
+					     FIL__, __LINE__, 0, MSG_FI_MISS,
+					     tmp);
+			  }
+			}
+#ifndef REPLACE_OLD
+		      if (sh.flag.checkSum != SH_CHECK_INIT) 
+			sh_hash_set_visited_true(ptr->name);
+#else
+		      /* delete from database
+		       */
+		      if (sh.flag.checkSum != SH_CHECK_INIT) 
+			sh_hash_set_missing(ptr->name);
+#endif
+		    }
+		  else
+		    {
+		      if (tmp == NULL) 
+			tmp = sh_util_safe_name (ptr->name);
+		      sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, 0,
+				       MSG_FI_FAIL,
+				       tmp);
+		      if (sh.flag.checkSum != SH_CHECK_INIT)
+			sh_hash_set_visited_true(ptr->name);
+		    }
+		}
+	      ++fcount;
+	    }
 	  
 	  if (tmp != NULL)
@@ -587,8 +350,6 @@
 	      tmp = NULL;
 	    }
-	  if (file)
-	    SH_FREE(file);
-	  if (dir)
-	    SH_FREE(dir);
+	  SH_FREE(file);
+	  SH_FREE(base);
 
 	  ptr->checked = S_TRUE;
@@ -599,116 +360,10 @@
 }
 
-static zAVLTree * fileTree = NULL;
-static zAVLTree * dirTree  = NULL;
-
-static void clear_lists()
-{
-  if (fileTree) {
-    zAVL_string_reset(fileTree);
-    fileTree  = NULL;
-  }
-  if (dirTree) {
-    zAVL_string_reset(dirTree);
-    dirTree  = NULL;
-  }
-  return;
-}
-
-static void add_to_filelist(zAVLTree * tree)
-{
-  dirstack_t * ptr;
-  zAVLCursor   avlcursor;
-
-  SL_ENTER(_("add_to_filelist"));
-
-  SH_MUTEX_LOCK(mutex_zfiles);
-  for (ptr = (dirstack_t *) zAVLFirst(&avlcursor, tree); ptr;
-       ptr = (dirstack_t *) zAVLNext(&avlcursor))
-    zAVL_string_set (&fileTree, ptr->name);
-  SH_MUTEX_UNLOCK(mutex_zfiles);
-  SL_RET0(_("add_to_filelist"));
-}
-static void add_to_dirlist(zAVLTree * tree)
-{
-  dirstack_t * ptr;
-  zAVLCursor   avlcursor;
-
-  SL_ENTER(_("add_to_dirlist"));
-
-  SH_MUTEX_LOCK(mutex_zfiles);
-  for (ptr = (dirstack_t *) zAVLFirst(&avlcursor, tree); ptr;
-       ptr = (dirstack_t *) zAVLNext(&avlcursor))
-    zAVL_string_set (&dirTree, ptr->name);
-  SH_MUTEX_UNLOCK(mutex_zfiles);
-  SL_RET0(_("add_to_dirlist"));
-}
-char * sh_files_findfile(const char * path)
-{
-  return zAVL_string_get (fileTree, path);
-}
-
-void * sh_dummy_621_candidate;
-
-static char * intern_find_morespecific_dir(zAVLTree * tree, 
-					   const char * path, size_t * len)
-{
-  dirstack_t * ptr;
-  zAVLCursor   avlcursor;
-  size_t       l_path = strlen(path);
-  size_t       l_name;
-  char *       candidate = NULL;
-  volatile size_t       l_candidate = 0;
-  
-  if (NULL == tree)
-    return NULL;
-
-  sh_dummy_621_candidate = (void *) &candidate;
-
-  SH_MUTEX_LOCK(mutex_zfiles);
-  for (ptr = (dirstack_t *) zAVLFirst(&avlcursor, tree); ptr;
-       ptr = (dirstack_t *) zAVLNext(&avlcursor))
-    {
-      l_name = strlen(ptr->name);
-      if (l_name <= l_path)
-	{
-	  if (0 == strncmp(ptr->name, path, l_name))
-	    {
-	      if ((l_name == l_path) || (path[l_name] == '/'))
-		{
-		  if (!candidate || (l_candidate < l_name))
-		    {
-		      candidate = ptr->name;
-		      l_candidate = l_name;
-		      *len = l_candidate;
-		    }
-		}
-	    }
-	}
-    }
-  SH_MUTEX_UNLOCK(mutex_zfiles);
-  return candidate;
-}
-char * sh_files_find_mostspecific_dir(const char * path)
-{
-  size_t l_one = 0;
-  size_t l_two = 0;
-  char * one;
-  char * two;
-
-  one = intern_find_morespecific_dir(zdirListOne, path, &l_one);
-  two = intern_find_morespecific_dir(zdirListTwo, path, &l_two);
-
-  if      (l_one > l_two) return one;
-  else                    return two;
-}
-
 int sh_files_delfilestack ()
 {
   SL_ENTER(_("sh_files_delfilestack"));
 
-  SH_MUTEX_LOCK(mutex_zfiles);
   zAVLFreeTree (zfileList, free_dirstack);
   zfileList = NULL;
-  SH_MUTEX_UNLOCK(mutex_zfiles);
 
   SL_RETURN(0, _("sh_files_delfilestack"));
@@ -729,8 +384,5 @@
 	    ptr->rdepth = MaxRecursionLevel;
 	  }
-
-	if ( (ptr->rdepth      == (-1)) && 
-	     (ptr->class       == SH_LEVEL_ALLIGNORE) && 
-	     (sh.flag.checkSum != SH_CHECK_INIT))
+	if (ptr->rdepth == (-1) && sh.flag.checkSum != SH_CHECK_INIT)
 	  hash_remove_tree (ptr->name);
       }
@@ -741,16 +393,6 @@
 int sh_files_setrec ()
 {
-  volatile int ret;
-  SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
-  clear_lists();
-  add_to_dirlist(zdirListOne);
-  add_to_dirlist(zdirListTwo);
-  add_to_filelist(zfileList);
   sh_files_setrec_int(zdirListOne);
-  ret = sh_files_setrec_int(zdirListTwo);
-  SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
-
-  return ret;
+  return sh_files_setrec_int(zdirListTwo);
 }
 
@@ -766,9 +408,6 @@
 int sh_files_deldirstack ()
 {
-  SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
   zdirListOne = sh_files_deldirstack_int(zdirListOne);
   zdirListTwo = sh_files_deldirstack_int(zdirListTwo);
-  SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
   return 0;
 }
@@ -781,9 +420,8 @@
   SL_ENTER(_("sh_files_reset"));
 
-  SH_MUTEX_LOCK(mutex_zfiles);
   for (ptr = (dirstack_t *) zAVLFirst(&avlcursor, zfileList); ptr;
        ptr = (dirstack_t *) zAVLNext(&avlcursor))
     ptr->checked = 0;
-  SH_MUTEX_UNLOCK(mutex_zfiles);
+
   SL_RET0(_("sh_files_reset"));
 }
@@ -797,6 +435,4 @@
   SL_ENTER(_("sh_dirs_reset"));
 
-  SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
   for (ptr = (dirstack_t *) zAVLFirst(&avlcursor1, zdirListOne); ptr;
        ptr = (dirstack_t *) zAVLNext(&avlcursor1))
@@ -806,5 +442,4 @@
        ptr = (dirstack_t *) zAVLNext(&avlcursor2))
     ptr->checked = 0;
-  SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
 
   SL_RET0(_("sh_dirs_reset"));
@@ -812,61 +447,47 @@
 
 
-int sh_files_pushfile_prelink (const char * str_s)
+int sh_files_pushfile_prelink (char * str_s)
 {
   return (sh_files_pushfile (SH_LEVEL_PRELINK, str_s));
 }
 
-int sh_files_pushfile_user0 (const char * str_s)
+int sh_files_pushfile_user0 (char * str_s)
 {
   return (sh_files_pushfile (SH_LEVEL_USER0, str_s));
 }
 
-int sh_files_pushfile_user1 (const char * str_s)
+
+int sh_files_pushfile_user1 (char * str_s)
 {
   return (sh_files_pushfile (SH_LEVEL_USER1, str_s));
 }
 
-int sh_files_pushfile_user2 (const char * str_s)
-{
-  return (sh_files_pushfile (SH_LEVEL_USER2, str_s));
-}
-
-int sh_files_pushfile_user3 (const char * str_s)
-{
-  return (sh_files_pushfile (SH_LEVEL_USER3, str_s));
-}
-
-int sh_files_pushfile_user4 (const char * str_s)
-{
-  return (sh_files_pushfile (SH_LEVEL_USER4, str_s));
-}
-
-
-int sh_files_pushfile_ro (const char * str_s)
+
+int sh_files_pushfile_ro (char * str_s)
 {
   return (sh_files_pushfile (SH_LEVEL_READONLY, str_s));
 }
 
-int sh_files_pushfile_attr (const char * str_s)
+int sh_files_pushfile_attr (char * str_s)
 {
   return (sh_files_pushfile (SH_LEVEL_ATTRIBUTES, str_s));
 }
 
-int sh_files_pushfile_log (const char * str_s)
+int sh_files_pushfile_log (char * str_s)
 {
   return (sh_files_pushfile (SH_LEVEL_LOGFILES, str_s));
 }
 
-int sh_files_pushfile_glog (const char * str_s)
+int sh_files_pushfile_glog (char * str_s)
 {
   return (sh_files_pushfile (SH_LEVEL_LOGGROW, str_s));
 }
 
-int sh_files_pushfile_noig (const char * str_s)
+int sh_files_pushfile_noig (char * str_s)
 {
   return (sh_files_pushfile (SH_LEVEL_NOIGNORE, str_s));
 }
 
-int sh_files_pushfile_allig (const char * str_s)
+int sh_files_pushfile_allig (char * str_s)
 {
   return (sh_files_pushfile (SH_LEVEL_ALLIGNORE, str_s));
@@ -891,13 +512,11 @@
 /* set mask(class)
  */
-static int sh_files_parse_mask (unsigned long * mask, const char * str)
-{
-  int l = 0, i = 0, act = 0, k = 0;
+static int sh_files_parse_mask (unsigned long * mask, char * str)
+{
+  int l, i = 0, act = 0, k = 0;
   char  myword[64];
   
   SL_ENTER(_("sh_files_parse_mask"));
 
-  myword[0] = '\0';
-
   if (str == NULL)
     {
@@ -908,8 +527,6 @@
 
   while (i < l) {
-
     if (str[i] == '\0')
       break;
-
     if (str[i] == ' ' || str[i] == '\t' || str[i] == ',')
       {
@@ -921,16 +538,13 @@
       {
 	act = +1; ++i;
-	myword[0] = '\0';
-	goto getword;
+	continue;
       }
     else if (str[i] == '-')
       {
 	act = -1; ++i;
-	myword[0] = '\0';
-	goto getword;
+	continue;
       }
     else /* a word */
       {
-      getword:
 	k = 0;
 	while (k < 63 && str[i] != ' ' && str[i] != '\t' && str[i] != ','
@@ -941,63 +555,41 @@
 	myword[k] = '\0';
 
-	if (sl_strlen(myword) == 0)
-	  {
-	    SL_RETURN ( (-1), _("sh_files_parse_mask"));
-	  }
-
 /* checksum     */
-	if      (0 == strcmp(myword, _("CHK")))
+	if (0 == strncmp(myword, _("CHK"), 3))
 	  sh_files_set_mask (mask, MODI_CHK, act);
 /* link         */
-	else if (0 == strcmp(myword, _("LNK")))
+	if (0 == strncmp(myword, _("LNK"), 3))
 	  sh_files_set_mask (mask, MODI_LNK, act);
 /* inode        */
-	else if (0 == strcmp(myword, _("RDEV")))
+	if (0 == strncmp(myword, _("RDEV"), 3))
 	  sh_files_set_mask (mask, MODI_RDEV, act);
 /* inode        */
-	else if (0 == strcmp(myword, _("INO")))
+	if (0 == strncmp(myword, _("INO"), 3))
 	  sh_files_set_mask (mask, MODI_INO, act);
 /* user         */
-	else if (0 == strcmp(myword, _("USR")))
+	if (0 == strncmp(myword, _("USR"), 3))
 	  sh_files_set_mask (mask, MODI_USR, act);
 /* group        */
-	else if (0 == strcmp(myword, _("GRP")))
+	if (0 == strncmp(myword, _("GRP"), 3))
 	  sh_files_set_mask (mask, MODI_GRP, act);
 /* mtime        */
-	else if (0 == strcmp(myword, _("MTM")))
+	if (0 == strncmp(myword, _("MTM"), 3))
 	  sh_files_set_mask (mask, MODI_MTM, act);
 /* ctime        */
-	else if (0 == strcmp(myword, _("CTM")))
+	if (0 == strncmp(myword, _("CTM"), 3))
 	  sh_files_set_mask (mask, MODI_CTM, act);
 /* atime        */
-	else if (0 == strcmp(myword, _("ATM")))
+	if (0 == strncmp(myword, _("ATM"), 3))
 	  sh_files_set_mask (mask, MODI_ATM, act);
 /* size         */
-	else if (0 == strcmp(myword, _("SIZ")))
+	if (0 == strncmp(myword, _("SIZ"), 3))
 	  sh_files_set_mask (mask, MODI_SIZ, act);
 /* file mode    */
-	else if (0 == strcmp(myword, _("MOD")))
+	if (0 == strncmp(myword, _("MOD"), 3))
 	  sh_files_set_mask (mask, MODI_MOD, act);
 /* hardlinks    */
-	else if (0 == strcmp(myword, _("HLN")))
+	if (0 == strncmp(myword, _("HLN"), 3))
 	  sh_files_set_mask (mask, MODI_HLN, act);
-/* size may grow */
-	else if (0 == strcmp(myword, _("SGROW")))
-	  sh_files_set_mask (mask, MODI_SGROW, act);
-/* use prelink */
-	else if (0 == strcmp(myword, _("PRE")))
-	  sh_files_set_mask (mask, MODI_PREL, act);
-/* get content */
-	else if (0 == strcmp(myword, _("TXT")))
-	  sh_files_set_mask (mask, MODI_TXT, act);
-/* get audit report */
-	else if (0 == strcmp(myword, _("AUDIT")))
-	  sh_files_set_mask (mask, MODI_AUDIT, act);
-	else
-	  {
-	    SL_RETURN ( (-1), _("sh_files_parse_mask"));
-	  }
-	act       = 0;
-	myword[0] = '\0';
+	
       }
   }
@@ -1005,49 +597,37 @@
 }
 
-int sh_files_redef_prelink(const char * str)
+int sh_files_redef_prelink(char * str)
 {
   return (sh_files_parse_mask(&mask_PRELINK, str));
 } 
-int sh_files_redef_user0(const char * str)
+int sh_files_redef_user0(char * str)
 {
   return (sh_files_parse_mask(&mask_USER0, str));
 } 
-int sh_files_redef_user1(const char * str)
+int sh_files_redef_user1(char * str)
 {
   return (sh_files_parse_mask(&mask_USER1, str));
 } 
-int sh_files_redef_user2(const char * str)
-{
-  return (sh_files_parse_mask(&mask_USER2, str));
-} 
-int sh_files_redef_user3(const char * str)
-{
-  return (sh_files_parse_mask(&mask_USER3, str));
-} 
-int sh_files_redef_user4(const char * str)
-{
-  return (sh_files_parse_mask(&mask_USER4, str));
-} 
-int sh_files_redef_readonly(const char * str)
+int sh_files_redef_readonly(char * str)
 {
   return (sh_files_parse_mask(&mask_READONLY, str));
 } 
-int sh_files_redef_loggrow(const char * str)
+int sh_files_redef_loggrow(char * str)
 {
   return (sh_files_parse_mask(&mask_LOGGROW, str));
 } 
-int sh_files_redef_logfiles(const char * str)
+int sh_files_redef_logfiles(char * str)
 {
   return (sh_files_parse_mask(&mask_LOGFILES, str));
 } 
-int sh_files_redef_attributes(const char * str)
+int sh_files_redef_attributes(char * str)
 {
   return (sh_files_parse_mask(&mask_ATTRIBUTES, str));
 } 
-int sh_files_redef_noignore(const char * str)
+int sh_files_redef_noignore(char * str)
 {
   return (sh_files_parse_mask(&mask_NOIGNORE, str));
 } 
-int sh_files_redef_allignore(const char * str)
+int sh_files_redef_allignore(char * str)
 {
   return (sh_files_parse_mask(&mask_ALLIGNORE, str));
@@ -1059,27 +639,21 @@
     {
     case SH_LEVEL_READONLY:
-      return (unsigned long) (mask_READONLY | MODI_INIT);
+      return (unsigned long) mask_READONLY;
     case SH_LEVEL_ATTRIBUTES:
-      return (unsigned long) (mask_ATTRIBUTES | MODI_INIT);
+      return (unsigned long) mask_ATTRIBUTES;
     case SH_LEVEL_LOGFILES:
-      return (unsigned long) (mask_LOGFILES | MODI_INIT);
+      return (unsigned long) mask_LOGFILES;
     case SH_LEVEL_LOGGROW:
-      return (unsigned long) (mask_LOGGROW | MODI_INIT);
+      return (unsigned long) mask_LOGGROW;
     case SH_LEVEL_ALLIGNORE:
-      return (unsigned long) (mask_ALLIGNORE | MODI_INIT);
+      return (unsigned long) mask_ALLIGNORE;
     case SH_LEVEL_NOIGNORE:
-      return (unsigned long) (mask_NOIGNORE | MODI_INIT);
+      return (unsigned long) mask_NOIGNORE;
     case SH_LEVEL_USER0:
-      return (unsigned long) (mask_USER0 | MODI_INIT);
+      return (unsigned long) mask_USER0;
     case SH_LEVEL_USER1:
-      return (unsigned long) (mask_USER1 | MODI_INIT);
-    case SH_LEVEL_USER2:
-      return (unsigned long) (mask_USER2 | MODI_INIT);
-    case SH_LEVEL_USER3:
-      return (unsigned long) (mask_USER3 | MODI_INIT);
-    case SH_LEVEL_USER4:
-      return (unsigned long) (mask_USER4 | MODI_INIT);
+      return (unsigned long) mask_USER1;
     case SH_LEVEL_PRELINK:
-      return (unsigned long) (mask_PRELINK | MODI_INIT);
+      return (unsigned long) mask_PRELINK;
     default:
       return (unsigned long) 0;
@@ -1105,16 +679,10 @@
 {
   char * p;
-  char errbuf[SH_ERRBUF_SIZE];
 
   SL_ENTER(_("sh_files_globerr"));
-
-  if (errnum == ENOTDIR || errnum == ENOENT)
-    {
-      SL_RETURN(0, _("sh_files_globerr"));
-    }
 
   p = sh_util_safe_name (epath);
   sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, errnum, MSG_FI_GLOB,
-		   sh_error_message (errnum, errbuf, sizeof(errbuf)), p);
+		   sh_error_message (errnum), p);
   SH_FREE(p);
 
@@ -1126,11 +694,9 @@
 #endif
 
-int sh_files_push_file_int (int class, const char * str_s, size_t len, 
-			    unsigned long check_flags)
+int sh_files_push_file_int (int class, const char * str_s, int len)
 {
   dirstack_t * new_item_ptr;
   char  * fileName;
   int     ret;
-  volatile int     count = 0;
 
   SL_ENTER(_("sh_files_push_file_int"));
@@ -1143,17 +709,16 @@
   new_item_ptr->name           = fileName;
   new_item_ptr->class          = class;
-  new_item_ptr->check_flags     = check_flags;
+  new_item_ptr->check_mask     = sh_files_maskof(class);
   new_item_ptr->rdepth         = 0;
   new_item_ptr->checked        = S_FALSE;
-  new_item_ptr->is_reported    = 0;
+  new_item_ptr->reported       = S_FALSE;
   new_item_ptr->childs_checked = S_FALSE;
 
-  SH_MUTEX_LOCK(mutex_zfiles);
   if (zfileList == NULL)
     {
-      zfileList = zAVLAllocTree (zdirstack_key, zAVL_KEY_STRING);
+      zfileList = zAVLAllocTree (zdirstack_key);
       if (zfileList == NULL) 
 	{
-	  (void) safe_logger (0, 0, NULL);
+	  (void) safe_logger (0, 0, getpid());
 	  aud__exit(FIL__, __LINE__, EXIT_FAILURE);
 	}
@@ -1161,278 +726,27 @@
 
   ret = zAVLInsert (zfileList, new_item_ptr);
-  SH_MUTEX_UNLOCK(mutex_zfiles);
 
   if (-1 == ret)
     {
-      (void) safe_logger (0, 0, NULL);
+      (void) safe_logger (0, 0, getpid());
       aud__exit(FIL__, __LINE__, EXIT_FAILURE);
     }
-  else if (3 == ret)
-    { 
-      if (sh.flag.started != S_TRUE)
-	sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DOUBLE,
-			 fileName);
-      SH_FREE(fileName);
-      SH_FREE(new_item_ptr);
-      new_item_ptr = NULL;
-    }
-  else
-    {
-      int           reported;
-      unsigned long check_flags = sh_files_maskof(class);
-
-      if ((sh.flag.inotify & SH_INOTIFY_INSCAN) != 0)
-	{
-	  sh_files_filecheck (class, check_flags, str_s, NULL,
-			      &reported, 0);
-	  if (SH_FFLAG_REPORTED_SET(reported))
-	    sh_files_set_file_reported(str_s);
-	  sh_inotify_add_watch_later(str_s, &sh_file_watches, NULL,
-				     class, check_flags, 
-				     SH_INOTIFY_FILE, 0);
-	}
-
-      if (MODI_AUDIT_ENABLED(check_flags))
-	{
-	  sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGPATH,
-			  _("Setting audit watch"),
-			  _("sh_files_push_file_int"), str_s);
-	  sh_audit_mark(str_s);
-	}
-      ++count;
-    }
-  SL_RETURN(count, _("sh_files_push_file_int"));
-}
-
-int sh_files_push_dir_int (int class, char * tail, size_t len, int rdepth, unsigned long check_flags);
-
+  if (3 == ret)
+    sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DOUBLE,
+		     fileName);
+
+  SL_RETURN(0, _("sh_files_push_file_int"));
+}
+
+
+static int sh_files_pushfile (int class, char * str_s)
+{
+  char  * tmp;
+  int     len;
 #ifdef HAVE_GLOB_H
-
-typedef struct globstack_entry {
-  char                  * name;
-  char                  * type_name;
-  int                     class;
-  unsigned long           check_flags;
-  int                     rdepth;
-  short                   type;
-  /* struct dirstack_entry * next; */
-} sh_globstack_t;
-
-static zAVLTree * zglobList   = NULL;
-
-zAVLKey zglobstack_key (void const * arg)
-{
-  const sh_globstack_t * sa = (const sh_globstack_t *) arg;
-  return (zAVLKey) sa->type_name;
-}
-
-
-static int sh_files_pushglob (int class, int type, const char * p, int rdepth,
-			       unsigned long check_flags_in, int flag)
-{
+  glob_t  pglob;
   int     globstatus = -1;
   unsigned int     gloop;
-  glob_t  pglob;
-
-  volatile int     count = 0;
-  volatile unsigned long check_flags = (flag == 0) ? sh_files_maskof(class) : check_flags_in;
-  
-  SL_ENTER(_("sh_files_pushglob"));
-
-  pglob.gl_offs = 0;
-  globstatus    = glob (p, 0, sh_files_globerr, &pglob);
-  
-  if (sh.flag.checkSum != SH_CHECK_INIT)
-    {
-      sh_globstack_t * new_item_ptr;
-      char  * fileName;
-      char  * typeName;
-      int     ret;
-      
-      SH_MUTEX_TRYLOCK(mutex_zfiles);
-      fileName = sh_util_strdup (p);
-      typeName = sh_util_strconcat ((type == SH_LIST_FILE) ? "F" : "D", p, NULL);
-      
-      new_item_ptr = (sh_globstack_t *) SH_ALLOC (sizeof(sh_globstack_t));
-      
-      new_item_ptr->name           = fileName;
-      new_item_ptr->type_name      = typeName;
-      new_item_ptr->class          = class;
-      new_item_ptr->check_flags    = check_flags;
-      new_item_ptr->rdepth         = rdepth;
-      new_item_ptr->type           = type;
-      
-      if (zglobList == NULL)
-	{
-	  zglobList = zAVLAllocTree (zglobstack_key, zAVL_KEY_STRING);
-	  if (zglobList == NULL) 
-	    {
-	      (void) safe_logger (0, 0, NULL);
-	      aud__exit(FIL__, __LINE__, EXIT_FAILURE);
-	    }
-	}
-      
-      ret = zAVLInsert (zglobList, new_item_ptr);
-      
-      if (ret != 0) /* already in list */
-	{
-	  SH_FREE(fileName);
-	  SH_FREE(typeName);
-	  SH_FREE(new_item_ptr);
-	}
-      SH_MUTEX_TRYLOCK_UNLOCK(mutex_zfiles);
-    }
-
-
-  if (globstatus == 0 && pglob.gl_pathc > 0)
-    {
-      for (gloop = 0; gloop < (unsigned int) pglob.gl_pathc; ++gloop)
-	{
-	  if (type == SH_LIST_FILE)
-	    {
-	      count += sh_files_push_file_int (class, pglob.gl_pathv[gloop], 
-					       sl_strlen(pglob.gl_pathv[gloop]), check_flags);
-	    }
-	  else
-	    {
-	      which_dirList = type;
-
-	      count += sh_files_push_dir_int  (class, pglob.gl_pathv[gloop], 
-					       sl_strlen(pglob.gl_pathv[gloop]), rdepth, check_flags);
-	    }
-	}
-    }
-  else
-    {
-      char * tmp = sh_util_safe_name (p);
-      
-      if (pglob.gl_pathc == 0
-#ifdef GLOB_NOMATCH
-	  || globstatus == GLOB_NOMATCH
-#endif
-	  )
-	sh_error_handle ((sh.flag.started != S_TRUE) ? SH_ERR_ERR : SH_ERR_NOTICE, 
-			 FIL__, __LINE__, 
-			 globstatus, MSG_FI_GLOB,
-			 _("No matches found"), tmp);
-#ifdef GLOB_NOSPACE
-      else if (globstatus == GLOB_NOSPACE)
-	sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
-			 globstatus, MSG_FI_GLOB,
-			 _("Out of memory"), tmp);
-#endif
-#ifdef GLOB_ABORTED
-      else if (globstatus == GLOB_ABORTED)
-	sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
-			 globstatus, MSG_FI_GLOB,
-			 _("Read error"), tmp);
-#endif
-      else 
-	sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
-			 globstatus, MSG_FI_GLOB,
-			 _("Unknown error"), tmp);
-      
-      SH_FREE(tmp);
-      
-    }
-  
-  globfree(&pglob);
-  SL_RETURN(count, _("sh_files_pushglob"));
-  return count;
-}
-
-void sh_files_check_globFilePatterns()
-{
-  sh_globstack_t * testPattern;
-  zAVLCursor   cursor;
-
-  SL_ENTER(_("sh_files_check_globPatterns"));
-
-  SH_MUTEX_LOCK(mutex_zglob);
-  for (testPattern = (sh_globstack_t *) zAVLFirst (&cursor, zglobList); 
-       testPattern;
-       testPattern = (sh_globstack_t *) zAVLNext  (&cursor))
-    {
-      if (testPattern->type == SH_LIST_FILE)
-	{
-	  sh_files_pushglob(testPattern->class, testPattern->type, 
-			    testPattern->name, testPattern->rdepth,
-			    testPattern->check_flags, 1);
-	}
-    }
-  SH_MUTEX_UNLOCK(mutex_zglob);
-  SL_RET0(_("sh_files_check_globPatterns"));
-}
-
-void sh_files_check_globPatterns()
-{
-  sh_globstack_t * testPattern;
-  zAVLCursor   cursor;
-
-  SL_ENTER(_("sh_files_check_globPatterns"));
-
-  SH_MUTEX_LOCK(mutex_zglob);
-  for (testPattern = (sh_globstack_t *) zAVLFirst (&cursor, zglobList); 
-       testPattern;
-       testPattern = (sh_globstack_t *) zAVLNext  (&cursor))
-    {
-      sh_files_pushglob(testPattern->class, testPattern->type, 
-			testPattern->name, testPattern->rdepth,
-			testPattern->check_flags, 1);
-    }
-  SH_MUTEX_UNLOCK(mutex_zglob);
-  SL_RET0(_("sh_files_check_globPatterns"));
-}
-
-/* the destructor
- */
-void free_globstack (void * inptr)
-{
-  sh_globstack_t * here;
-
-  SL_ENTER(_("free_globstack"));
-  if (inptr == NULL)
-    SL_RET0(_("free_globstack"));
-  else
-    here = (sh_globstack_t *) inptr;
-
-  /* cppcheck-suppress uninitStructMember */
-  if (here->name != NULL)
-    SH_FREE(here->name);
-  if (here->type_name != NULL)
-    SH_FREE(here->type_name);
-  SH_FREE(here);
-  SL_RET0(_("free_globstack"));
-}
-
-int sh_files_delglobstack ()
-{
-  SL_ENTER(_("sh_files_delglobstack"));
-
-  SH_MUTEX_LOCK(mutex_zglob);
-  zAVLFreeTree (zglobList, free_globstack);
-  zglobList = NULL;
-  SH_MUTEX_UNLOCK(mutex_zglob);
-
-  SL_RETURN(0, _("sh_files_delglobstack"));
-}
-  
-
-#else
-void sh_files_check_globPatterns()
-{
-  return;
-}
-int sh_files_delglobstack ()
-{
-  return 0;
-}
-#endif
-
-static int sh_files_pushfile (int class, const char * str_s)
-{
-  size_t  len;
-  char  * tmp;
-  char  * p;
+#endif
 
   static int reject = 0;
@@ -1446,24 +760,21 @@
    * is the only one -- and will stay the only one
    */
-  if (sh.flag.opts == S_TRUE) 
+  if (sh.flag.opts == 1) 
     {
       sh_files_delfilestack ();
       sh_files_deldirstack ();
-      sh_files_delglobstack ();
       reject = 1;
     }
 
-  p = sh_files_parse_input(str_s, &len);
-  if (!p || len == 0)
-    SL_RETURN((-1), _("sh_files_pushfile"));
-
-  if (sh.flag.checkSum == SH_CHECK_INIT)
-    p = sh_dbIO_rootfs_prepend(p);
-      
+  if (str_s == NULL) 
+    SL_RETURN((-1),_("sh_files_pushfile"));
+
+  len = sl_strlen(str_s);
+
   if (len >= PATH_MAX) 
     {
       /* Name too long
        */
-      tmp = sh_util_safe_name (p);
+      tmp = sh_util_safe_name (str_s);
       sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_2LONG,
 		       tmp);
@@ -1471,9 +782,15 @@
       SL_RETURN((-1),_("sh_files_pushfile"));
     } 
-  else if (p[0] != '/') 
+  else if (len < 1) 
+    {
+      /* Should not happen (str_s == NULL caught further above)
+       */
+      SL_RETURN((-1),_("sh_files_pushfile"));
+    } 
+  else if (str_s[0] != '/') 
     {
       /* Not an absolute path
        */
-      tmp = sh_util_safe_name (p);
+      tmp = sh_util_safe_name (str_s);
       sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_NOPATH,
 		       tmp);
@@ -1486,26 +803,68 @@
        * special case of the root directory.
        */
-      if (p[len-1] == '/' && len > 1)
+      if (str_s[len-1] == '/' && len > 1)
 	{
-	  p[len-1] = '\0';
+	  str_s[len-1] = '\0';
 	  --len;
 	}
+      
     } 
 
 #ifdef HAVE_GLOB_H
-  if (0 == sh_files_has_metachar(p))
-    {
-      sh_files_push_file_int (class, p, len, sh_files_maskof(class));
+  if (0 == sh_files_has_metachar(str_s))
+    {
+      sh_files_push_file_int (class, str_s, len);
     }
   else
     {
-      sh_files_pushglob (class, SH_LIST_FILE, p, 0, 0, 0);
+      pglob.gl_offs = 0;
+      globstatus    = glob (str_s, 0, sh_files_globerr, &pglob);
+
+      if (globstatus == 0 && pglob.gl_pathc > 0)
+	{
+	  for (gloop = 0; gloop < (unsigned int) pglob.gl_pathc; ++gloop)
+	    sh_files_push_file_int (class, pglob.gl_pathv[gloop], 
+				    sl_strlen(pglob.gl_pathv[gloop]));
+	}
+      else
+	{
+	  tmp = sh_util_safe_name (str_s);
+
+	  if (pglob.gl_pathc == 0
+#ifdef GLOB_NOMATCH
+	      || globstatus == GLOB_NOMATCH
+#endif
+	      )
+	    sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, 
+			     globstatus, MSG_FI_GLOB,
+			     _("No matches found"), tmp);
+#ifdef GLOB_NOSPACE
+	  else if (globstatus == GLOB_NOSPACE)
+	    sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
+			     globstatus, MSG_FI_GLOB,
+			     _("Out of memory"), tmp);
+#endif
+#ifdef GLOB_ABORTED
+	  else if (globstatus == GLOB_ABORTED)
+	    sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
+			     globstatus, MSG_FI_GLOB,
+			     _("Read error"), tmp);
+#endif
+	  else 
+	    sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
+			     globstatus, MSG_FI_GLOB,
+			     _("Unknown error"), tmp);
+
+	  SH_FREE(tmp);
+
+	}
+
+      globfree(&pglob);
     }
 
 #else
-  sh_files_push_file_int (class, p, len, sh_files_maskof(class));
-#endif
-
-  SH_FREE(p);
+  sh_files_push_file_int (class, str_s, len);
+#endif
+
   SL_RETURN((0),_("sh_files_pushfile"));
 }
@@ -1536,19 +895,10 @@
 int sh_files_is_allignore (char * str)
 {
-  int retval = 0;
-
-  SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
-  retval = sh_files_is_allignore_int(str, zdirListOne);
-
-  if (NULL != zdirListTwo && retval == 0)
-    {
-      retval = sh_files_is_allignore_int(str, zdirListTwo);
-    }
-  SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
-  return retval;
-}
-
-void * sh_dummy_1493_ptr;
+  if (1 == sh_files_is_allignore_int(str, zdirListOne))
+    return 1;
+  if (NULL == zdirListTwo)
+    return 0;
+  return sh_files_is_allignore_int(str, zdirListTwo);
+}
 
 unsigned long sh_dirs_chk (int which)
@@ -1559,15 +909,9 @@
   dirstack_t * dst_ptr;
   int          status;
-  int          tmp_reported;
-  volatile int          filetype = SH_FILE_UNKNOWN;
-  volatile unsigned long dcount = 0;
+  unsigned long dcount = 0;
   char       * tmp;
   
   SL_ENTER(_("sh_dirs_chk"));
 
-  sh_dummy_1493_ptr = (void *) &ptr;
-  
-  SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
   if (which == 1)
     tree = zdirListOne;
@@ -1579,10 +923,9 @@
     {
       if (sig_urgent > 0) {
-	goto out;
+	SL_RETURN(dcount, _("sh_dirs_chk"));
       }
 
       if (ptr->checked == S_FALSE)
 	{
-	  SH_MUTEX_LOCK(mutex_zfiles);
 	  /* 28 Aug 2001 check the top level directory
 	   */
@@ -1594,36 +937,25 @@
 		{
 		  BREAKEXIT(sh_files_filecheck);
-		  tmp_reported = dst_ptr->is_reported;
-		  filetype = sh_files_filecheck (dst_ptr->class, dst_ptr->check_flags, 
-						 ptr->name,  
-						 NULL,  &tmp_reported, 0);
-		  dst_ptr->is_reported = tmp_reported;
-		  (void) handle_filecheck_ret(dst_ptr, NULL, filetype);
-
+		  sh_files_filecheck (dst_ptr->class,  ptr->name,  
+				      NULL,  &status, 0);
 		  dst_ptr->checked = S_TRUE;
 		  status           = S_TRUE;
+		  break;
 		}
 	      else
 		{
 		  status           = S_TRUE;
+		  break;
 		}
 	    }
-	  SH_MUTEX_UNLOCK(mutex_zfiles);
 
 	  if (status == S_FALSE)
-	    {
-	      tmp_reported = ptr->is_reported;
-	      filetype = sh_files_filecheck (ptr->class,  ptr->check_flags, 
-					     ptr->name,  NULL,  &tmp_reported, 0);
-	      ptr->is_reported = tmp_reported;
-	      (void) handle_filecheck_ret(ptr, NULL, filetype);
-	    }
+	    sh_files_filecheck (ptr->class,  ptr->name,  NULL,  &status, 0);
 
 	  BREAKEXIT(sh_files_checkdir);
-	  status = sh_files_checkdir (ptr->class, ptr->check_flags, 
-				      ptr->rdepth, ptr->name, 
+	  status = sh_files_checkdir (ptr->class, ptr->rdepth, ptr->name, 
 				      ptr->name);
 
-	  if (status < 0 && (!SH_FFLAG_REPORTED_SET(ptr->is_reported))) 
+	  if (status < 0 && ptr->reported == S_FALSE) 
 	    {
 	      /* directory is missing
@@ -1636,15 +968,13 @@
 					      ShDFLevel[SH_ERR_T_DIR])) {
 		    tmp = sh_util_safe_name (ptr->name);
-		    if (!sh_global_check_silent)
-		      sh_error_handle ((ptr->class == SH_LEVEL_ALLIGNORE) ? 
-				       ShDFLevel[ptr->class] : 
-				       ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__,
-				       0, MSG_FI_MISS, tmp);
-		    ++sh.statistics.files_report;
+		    sh_error_handle ((ptr->class == SH_LEVEL_ALLIGNORE) ? 
+				     ShDFLevel[ptr->class] : 
+				     ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__,
+				     0, MSG_FI_MISS, tmp);
 		    SH_FREE(tmp);
 		  }
 		}
 	      if (sh.flag.reportonce == S_TRUE)
-		SET_SH_FFLAG_REPORTED(ptr->is_reported);
+		ptr->reported = S_TRUE;
 	    } 
 	  else 
@@ -1652,8 +982,7 @@
 	      /* exists (status >= 0), but was missing (reported == TRUE)
 	       */
-	      if (status >= 0 && SH_FFLAG_REPORTED_SET(ptr->is_reported))
+	      if (status >= 0 && ptr->reported == S_TRUE)
 		{
-		  CLEAR_SH_FFLAG_REPORTED(ptr->is_reported);
-		  sh_hash_clear_flag(ptr->name, SH_FFLAG_ENOENT);
+		  ptr->reported = S_FALSE;
 #if 0
 		  /* obsoleted (really?) by the mandatory sh_files_filecheck()
@@ -1661,11 +990,9 @@
 		   */
 		  tmp = sh_util_safe_name (ptr->name);
-		  if (!sh_global_check_silent)
-		    sh_error_handle ((ptr->class == SH_LEVEL_ALLIGNORE) ? 
-				     ShDFLevel[ptr->class] : 
-				     ShDFLevel[SH_ERR_T_DIR],
-				     FIL__, __LINE__, 0, MSG_FI_ADD,
-				     tmp);
-		  ++sh.statistics.files_report;
+		  sh_error_handle ((ptr->class == SH_LEVEL_ALLIGNORE) ? 
+				   ShDFLevel[ptr->class] : 
+				   ShDFLevel[SH_ERR_T_DIR],
+				   FIL__, __LINE__, 0, MSG_FI_ADD,
+				   tmp);
 		  SH_FREE(tmp);
 #endif
@@ -1686,90 +1013,72 @@
 	      ++dcount;
 	    }
-	  ptr->checked        = S_TRUE;
-	  ptr->childs_checked = S_TRUE;
+	  ptr->checked = S_TRUE;
 	}
 
       if (sig_urgent > 0) {
-	goto out;
+	SL_RETURN(dcount, _("sh_dirs_chk"));
       }
 
     }
- out:
-  ; /* 'label at end of compound statement' */
-  SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
-
   SL_RETURN(dcount, _("sh_dirs_chk"));
 }
 
-int sh_files_pushdir_prelink (const char * str_s)
+int sh_files_pushdir_prelink (char * str_s)
 {
   return (sh_files_pushdir (SH_LEVEL_PRELINK, str_s));
 }
 
-int sh_files_pushdir_user0 (const char * str_s)
+int sh_files_pushdir_user0 (char * str_s)
 {
   return (sh_files_pushdir (SH_LEVEL_USER0, str_s));
 }
 
-int sh_files_pushdir_user1 (const char * str_s)
+int sh_files_pushdir_user1 (char * str_s)
 {
   return (sh_files_pushdir (SH_LEVEL_USER1, str_s));
 }
 
-int sh_files_pushdir_user2 (const char * str_s)
-{
-  return (sh_files_pushdir (SH_LEVEL_USER2, str_s));
-}
-
-int sh_files_pushdir_user3 (const char * str_s)
-{
-  return (sh_files_pushdir (SH_LEVEL_USER3, str_s));
-}
-
-int sh_files_pushdir_user4 (const char * str_s)
-{
-  return (sh_files_pushdir (SH_LEVEL_USER4, str_s));
-}
-
-int sh_files_pushdir_attr (const char * str_s)
+int sh_files_pushdir_attr (char * str_s)
 {
   return (sh_files_pushdir (SH_LEVEL_ATTRIBUTES, str_s));
 }
 
-int sh_files_pushdir_ro (const char * str_s)
+int sh_files_pushdir_ro (char * str_s)
 {
   return (sh_files_pushdir (SH_LEVEL_READONLY, str_s));
 }
 
-int sh_files_pushdir_log (const char * str_s)
+int sh_files_pushdir_log (char * str_s)
 {
   return (sh_files_pushdir (SH_LEVEL_LOGFILES, str_s));
 }
 
-int sh_files_pushdir_glog (const char * str_s)
+int sh_files_pushdir_glog (char * str_s)
 {
   return (sh_files_pushdir (SH_LEVEL_LOGGROW, str_s));
 }
 
-int sh_files_pushdir_noig (const char * str_s)
+int sh_files_pushdir_noig (char * str_s)
 {
   return (sh_files_pushdir (SH_LEVEL_NOIGNORE, str_s));
 }
 
-int sh_files_pushdir_allig (const char * str_s)
+int sh_files_pushdir_allig (char * str_s)
 {
   return (sh_files_pushdir (SH_LEVEL_ALLIGNORE, str_s));
 }
 
+static int which_dirList = 1;
+
 int set_dirList (int which)
 {
   if (which == 2)
-    which_dirList = SH_LIST_DIR2;
+    which_dirList = 2;
   else
-    which_dirList = SH_LIST_DIR1;
+    which_dirList = 1;
   return 0;
 }
 
-int sh_files_push_dir_int (int class, char * tail, size_t len, int rdepth, unsigned long check_flags)
+int sh_files_push_dir_int (int class, char * tail, int len, int rdepth)
 {
   zAVLTree   * tree;
@@ -1787,13 +1096,11 @@
   new_item_ptr->name           = dirName;
   new_item_ptr->class          = class;
-  new_item_ptr->check_flags     = check_flags;
+  new_item_ptr->check_mask     = sh_files_maskof(class);
   new_item_ptr->rdepth         = rdepth;
   new_item_ptr->checked        = S_FALSE;
-  new_item_ptr->is_reported    = 0;
+  new_item_ptr->reported       = S_FALSE;
   new_item_ptr->childs_checked = S_FALSE;
 
-  SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
-  if (which_dirList == SH_LIST_DIR1)
+  if (which_dirList == 1)
     {
       tree = zdirListOne;
@@ -1806,11 +1113,11 @@
   if (tree == NULL)
     {
-      tree = zAVLAllocTree (zdirstack_key, zAVL_KEY_STRING);
+      tree = zAVLAllocTree (zdirstack_key);
       if (tree == NULL) 
 	{
-	  (void) safe_logger (0, 0, NULL);
+	  (void) safe_logger (0, 0, getpid());
 	  aud__exit(FIL__, __LINE__, EXIT_FAILURE);
 	}
-      if (which_dirList == SH_LIST_DIR1)
+      if (which_dirList == 1)
 	zdirListOne = tree;
       else
@@ -1819,82 +1126,53 @@
 
   ret = zAVLInsert (tree, new_item_ptr);
-  SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
 
   if (-1 == ret)
     {
-      (void) safe_logger (0, 0, NULL);
+      (void) safe_logger (0, 0, getpid());
       aud__exit(FIL__, __LINE__, EXIT_FAILURE);
     }
   if (3 == ret)
-    { 
-      if (sh.flag.started != S_TRUE)
-	sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DOUBLE,
-			 dirName);
-      SH_FREE(dirName);
-      SH_FREE(new_item_ptr);
-      new_item_ptr = NULL;
-    }
-  else
-    {
-      if (MODI_AUDIT_ENABLED(check_flags))
-	{
-	  sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGPATH,
-			  _("Setting audit watch"),
-			  _("sh_files_push_file_int"), tail);
-	  sh_audit_mark(tail);
-	}
-    }
+    sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DOUBLE,
+		     dirName);
+
   SL_RETURN(0, _("sh_files_push_dir_int"));
 }
 
-static int sh_files_pushdir (int class, const char * str_s)
+static int sh_files_pushdir (int class, char * str_s)
 {
   char  * tmp;
-  size_t  len;
+  int     len;
   int     rdepth = 0;
   char  * tail = NULL;
-  char  * p;
+
+#ifdef HAVE_GLOB_H
+  glob_t  pglob;
+  int     globstatus = -1;
+  unsigned int     gloop;
+#endif
 
   SL_ENTER(_("sh_files_pushdir"));
 
-  if (sh.flag.opts == S_TRUE) {
+  if (sh.flag.opts == 1) {
     sh_files_delfilestack ();
     sh_files_deldirstack ();
-    sh_files_delglobstack ();
   }
 
-  p = sh_files_parse_input(str_s, &len);
-  if (!p || len == 0)
-    SL_RETURN((-1),_("sh_files_pushdir"));
-
-  if (p[0] != '/')
-    {
-      rdepth = strtol(p, &tail, 10);
-      if (tail == p)
-	{
-	  SH_FREE(p);
-	  SL_RETURN((-1), _("sh_files_pushdir"));
-	}
+  if (str_s == NULL)
+    SL_RETURN((-1), _("sh_files_pushdir"));
+  
+
+  if (str_s[0] != '/')
+    {
+      rdepth = strtol(str_s, &tail, 10);
+      if (tail == str_s)
+	SL_RETURN((-1), _("sh_files_pushdir"));
     }
   else
-    tail   = p;
+    tail   = str_s;
   
 
-  if (tail == p)
-    {
-      /* Setting to an invalid number will force MaxRecursionLevel,
-       * see sh_files_setrec_int()
-       */
-      rdepth = (-2);
-    }
-  else if ( (rdepth < (-1) || rdepth > 99) || 
-	    ((rdepth == (-1)) && (class != SH_LEVEL_ALLIGNORE)) )
-    {
-      SH_FREE(p);
-      SL_RETURN((-1), _("sh_files_pushdir"));
-    }
-
-  if (sh.flag.checkSum == SH_CHECK_INIT)
-    tail = sh_dbIO_rootfs_prepend(tail);
+  if (rdepth < (-1) || tail == str_s || rdepth > 99)
+    rdepth = (-2);
 
   len = sl_strlen(tail);
@@ -1906,10 +1184,8 @@
 		       tmp);
       SH_FREE(tmp);
-      SH_FREE(p);
       SL_RETURN((-1), _("sh_files_pushdir"));
     } 
   else if (len < 1) 
     {
-      SH_FREE(p);
       SL_RETURN((-1), _("sh_files_pushdir"));
     } 
@@ -1920,9 +1196,9 @@
 		       tmp);
       SH_FREE(tmp);
-      SH_FREE(p);
       SL_RETURN((-1), _("sh_files_pushdir"));
     } 
   else 
     {
+      
       if (tail[len-1] == '/' && len > 1)
 	{
@@ -1930,4 +1206,5 @@
 	  --len;
 	}
+      
     } 
 
@@ -1935,26 +1212,66 @@
   if (0 == sh_files_has_metachar(tail))
     {
-      sh_files_push_dir_int (class, tail, len, rdepth, sh_files_maskof(class));
+      sh_files_push_dir_int (class, tail, len, rdepth);
     }
   else
     {
-      sh_files_pushglob (class, which_dirList, tail, rdepth, 0, 0);
+      pglob.gl_offs = 0;
+      globstatus    = glob (tail, 0, sh_files_globerr, &pglob);
+
+      if (globstatus == 0 && pglob.gl_pathc > 0)
+	{
+	  for (gloop = 0; gloop < (unsigned int) pglob.gl_pathc; ++gloop)
+	    sh_files_push_dir_int (class, 
+				   pglob.gl_pathv[gloop], 
+				   sl_strlen(pglob.gl_pathv[gloop]), 
+				   rdepth);
+	}
+      else
+	{
+	  tmp = sh_util_safe_name (tail);
+
+	  if (pglob.gl_pathc == 0
+#ifdef GLOB_NOMATCH
+	      || globstatus == GLOB_NOMATCH
+#endif
+	      )
+	    sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, 
+			     globstatus, MSG_FI_GLOB,
+			     _("No matches found"), tmp);
+#ifdef GLOB_NOSPACE
+	  else if (globstatus == GLOB_NOSPACE)
+	    sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
+			     globstatus, MSG_FI_GLOB,
+			     _("Out of memory"), tmp);
+#endif
+#ifdef GLOB_ABORTED
+	  else if (globstatus == GLOB_ABORTED)
+	    sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
+			     globstatus, MSG_FI_GLOB,
+			     _("Read error"), tmp);
+#endif
+	  else 
+	    sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
+			     globstatus, MSG_FI_GLOB,
+			     _("Unknown error"), tmp);
+	  SH_FREE(tmp);
+	}
+
+      globfree(&pglob);
     }
 #else  
-  sh_files_push_dir_int (class, tail, len, rdepth, sh_files_maskof(class));
-#endif
-
-  SH_FREE(p);
+  sh_files_push_dir_int (class, tail, len, rdepth);
+#endif
+
   SL_RETURN((0), _("sh_files_pushdir"));
-}
-
-/**
+}  
+
 struct sh_dirent {
+  /* char               sh_d_name[NAME_MAX + 2]; */
   char             * sh_d_name;
   struct sh_dirent * next;
 };
-**/
-
-void kill_sh_dirlist (struct sh_dirent * dirlist)
+
+static void kill_sh_dirlist (struct sh_dirent * dirlist)
 {
   struct sh_dirent * this;
@@ -1972,17 +1289,17 @@
 /* -- add an entry to a directory listing
  */
-struct sh_dirent * addto_sh_dirlist (struct dirent * thisEntry, 
-				     struct sh_dirent * dirlist)
+static struct sh_dirent * addto_sh_dirlist (struct dirent * thisEntry, 
+					    struct sh_dirent * dirlist)
 {
   struct sh_dirent * this;
-  size_t len;
+  int i;
 
   if (thisEntry == NULL)
     return dirlist;
   
-  len = sl_strlen(thisEntry->d_name);
-  if (len == 0)
+  i = sl_strlen(thisEntry->d_name);
+  if (i == 0)
     return dirlist;
-  ++len;
+  ++i;
   
   this = SH_ALLOC(sizeof(struct sh_dirent));
@@ -1990,6 +1307,6 @@
     return dirlist;
 
-  this->sh_d_name = SH_ALLOC(len);
-  sl_strlcpy(this->sh_d_name, thisEntry->d_name, len);
+  this->sh_d_name = SH_ALLOC(i);
+  sl_strlcpy(this->sh_d_name, thisEntry->d_name, i);
 
   this->next = dirlist;
@@ -2001,5 +1318,5 @@
 /* Simply sets our boolean as to whether this check is active 
  */
-int sh_files_check_hardlinks (const char * opt)
+int sh_files_check_hardlinks (char * opt)
 {
   int i;
@@ -2017,5 +1334,5 @@
 static struct sh_hle_struct * sh_hl_exc = NULL;
 
-int sh_files_hle_reg (const char * str)
+int sh_files_hle_reg (char * str)
 {
   long   offset;
@@ -2077,72 +1394,41 @@
       tmp = tmp->next;
     }
-#ifdef HAVE_FNMATCH_H
-  if ( (offset == 1) && (0 == fnmatch(_("/run/user/*"), path, FNM_PATHNAME)) )
-    {
-      /* gvfs directory in /run/user/username/ */
-      SL_RETURN(0, _("sh_files_hle_test"));
-    }
-#endif
-
   SL_RETURN(-1, _("sh_files_hle_test"));
 }
 #endif
 
-/* This is the LCG from Numerical Recipies. Deterministic. 
+/* -- check a single directory and its content
  */
-static unsigned int simple_rand(unsigned int * state)
-{
-  *state = 1664525 * (*state) + 1013904223;
-  return *state;
-}
-
-void * sh_dummy_dirlist;
-void * sh_dummy_tmpcat;
-
-/* -- Check a single directory and its content. Does not
- *    check the directory inode itself.
- */
-int sh_files_checkdir (int iclass, unsigned long check_flags, 
-		       int idepth, char * iname, 
-		       char * relativeName)
-{
-  struct sh_dirent * dirlist;
-  struct sh_dirent * dirlist_orig;
+static int sh_files_checkdir (int iclass, int idepth, char * iname, 
+			      char * relativeName)
+{
+  struct sh_dirent * dirlist = NULL;
+  struct sh_dirent * dirlist_orig = NULL;
 
   DIR *           thisDir = NULL;
   struct dirent * thisEntry;
-#if defined(SH_USE_SCANDIR)
-  struct dirent **entryList;
-  int             entry;
-#endif
   int             status;
   int             dummy = S_FALSE;
-  dir_type      * theDir;
+  dir_type        theDir;
   ShFileType      checkit;
-  static unsigned int state = 1;
-
-  file_type     * theFile;
+
+
+  file_type       theFile;
   char          * tmpname;
   char          * tmpcat;
-  char errbuf[SH_ERRBUF_SIZE];
 
   int             rdepth = 0;
   int             class  = 0;
-  volatile int    rdepth_next;
-  volatile int    class_next;
-  volatile int    file_class_next;
-  volatile unsigned long   check_flags_next;
-  volatile unsigned long   file_check_flags_next;
-
-  volatile int    checked_flag  = S_FALSE;
-  volatile int    cchecked_flag = S_FALSE;
+  int             rdepth_next;
+  int             class_next;
+  int             file_class_next;
+
+  int             checked_flag  = S_FALSE;
+  int             cchecked_flag = S_FALSE;
 
   dirstack_t *    dst_ptr;
-  dirstack_t *    tmp_ptr;
 
   int             hardlink_num = 0;
-#if !defined(HOST_IS_DARWIN)
-  size_t          len;
-#endif
+
 
   SL_ENTER(_("sh_files_checkdir"));
@@ -2173,12 +1459,7 @@
     }
 
-  if (flag_err_info == S_TRUE)
-    {
-      char pstr[32];
-
-      sl_strlcpy(pstr, sh_hash_getpolicy(iclass), sizeof(pstr));
-      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_CHK, pstr, tmpname);
-    }
-
+  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_CHK,
+		   tmpname);
+  
   /* ---- check input ----
    */
@@ -2201,24 +1482,17 @@
       SL_RETURN((-1), _("sh_files_checkdir"));
     }
-
+    
+    
   /* ---- stat the directory ----
    */
-  theFile = SH_ALLOC(sizeof(file_type));
-  sl_strlcpy (theFile->fullpath, iname, PATH_MAX);
-  theFile->attr_string = NULL;
-  theFile->link_path   = NULL;
-  theFile->check_flags  = check_flags;
+  sl_strlcpy (theFile.fullpath, iname, PATH_MAX);
 
   (void) relativeName;
   status = sh_unix_getinfo (ShDFLevel[SH_ERR_T_DIR], 
 			    iname,
-			    theFile, NULL, iclass);
+			    &theFile, NULL, iclass);
 
   if ((sig_termfast == 1) || (sig_terminate == 1)) 
     {
-      if (theFile->attr_string) SH_FREE(theFile->attr_string);
-      if (theFile->link_path)   SH_FREE(theFile->link_path);
-      SH_FREE(theFile);
-      SH_FREE(tmpname);
       SL_RETURN((0), _("sh_files_checkdir"));
     }
@@ -2226,36 +1500,19 @@
   if (status == -1)
     {
-      if (theFile->attr_string) SH_FREE(theFile->attr_string);
-      if (theFile->link_path)   SH_FREE(theFile->link_path);
-      SH_FREE(theFile);
-      SH_FREE(tmpname);
+      SH_FREE(tmpname); 
       SL_RETURN((-1), _("sh_files_checkdir"));
     }
 
-  if (theFile->c_mode[0] != 'd') 
+  if (theFile.c_mode[0] != 'd') 
     { 
-      if (!sh_global_check_silent)
-	sh_error_handle (ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__, 0,
-			 MSG_FI_NODIR,
-			 tmpname);
-      ++sh.statistics.files_nodir;
-      if (theFile->attr_string) SH_FREE(theFile->attr_string);
-      if (theFile->link_path)   SH_FREE(theFile->link_path);
-      SH_FREE(theFile);
-      SH_FREE(tmpname);
+      sh_error_handle (ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__, 0,
+		       MSG_FI_NODIR,
+		       tmpname);
+      SH_FREE(tmpname); 
       SL_RETURN((-1), _("sh_files_checkdir"));
     }
 
-  if ((sh.flag.inotify & SH_INOTIFY_INSCAN) != 0)
-    {
-      sh_inotify_add_watch_later(iname, &sh_file_watches, &status,
-				 iclass, check_flags, SH_INOTIFY_DIR, idepth);
-    }
-   
-  hardlink_num = theFile->hardlinks;
-
-  if (theFile->attr_string) SH_FREE(theFile->attr_string);
-  if (theFile->link_path)   SH_FREE(theFile->link_path);
-  SH_FREE(theFile);
+  hardlink_num = theFile.hardlinks;
+
 
   /* ---- open directory for reading ---- 
@@ -2271,76 +1528,42 @@
       sh_error_handle (ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__, 0, 
 		       MSG_E_OPENDIR,
-		       sh_error_message (status, errbuf, sizeof(errbuf)), tmpname);
+		       sh_error_message (status), tmpname);
       SH_FREE(tmpname); 
+
       SL_RETURN((-1), _("sh_files_checkdir"));
     }
 
-  theDir = SH_ALLOC(sizeof(dir_type));
-
-  theDir->NumRegular  = 0;
-  theDir->NumDirs     = 0;
-  theDir->NumSymlinks = 0;
-  theDir->NumFifos    = 0;
-  theDir->NumSockets  = 0;
-  theDir->NumCDev     = 0;
-  theDir->NumBDev     = 0;
-  theDir->NumDoor     = 0;
-  theDir->NumPort     = 0;
-  theDir->NumAll      = 0;
-  theDir->TotalBytes  = 0;
-  sl_strlcpy (theDir->DirPath, iname, PATH_MAX); 
-
-
-  sh_dummy_dirlist = (void *) &dirlist;
-  sh_dummy_tmpcat  = (void *) &tmpcat;
+  theDir.NumRegular  = 0;
+  theDir.NumDirs     = 0;
+  theDir.NumSymlinks = 0;
+  theDir.NumFifos    = 0;
+  theDir.NumSockets  = 0;
+  theDir.NumCDev     = 0;
+  theDir.NumBDev     = 0;
+  theDir.NumAll      = 0;
+  theDir.TotalBytes  = 0;
+  sl_strlcpy (theDir.DirPath, iname, PATH_MAX); 
+
 
   /* ---- read ----
    */
-  SH_MUTEX_LOCK(mutex_readdir);
-
-  dirlist = NULL;
-  dirlist_orig = NULL;
-
-#if defined(SH_USE_SCANDIR)
-  entry = scandir(iname, &entryList, 0, alphasort);
-  while(entry > 0) { /* scandir() may return -1 on error! */
-      entry--;
-      thisEntry = entryList[entry];
-#else
   do {
       thisEntry = readdir (thisDir);
-#endif
       if (thisEntry != NULL) 
 	{
-	  ++theDir->NumAll;
+	  ++theDir.NumAll;
 	  if (sl_strcmp (thisEntry->d_name, ".") == 0)
 	    { 
-	      ++theDir->NumDirs;
-#if defined(SH_USE_SCANDIR)
-	      free(entryList[entry]); /* scandir() mallocs entries */
-#endif
+	      ++theDir.NumDirs;
 	      continue;
 	    }
 	  if (sl_strcmp (thisEntry->d_name, "..") == 0)
 	    {
-	      ++theDir->NumDirs;
-#if defined(SH_USE_SCANDIR)
-	      free(entryList[entry]); /* scandir() mallocs entries */
-#endif
+	      ++theDir.NumDirs;
 	      continue;
 	    }
 	  dirlist = addto_sh_dirlist (thisEntry, dirlist);
-#if defined(SH_USE_SCANDIR)
-	  free(entryList[entry]); /* scandir() mallocs entries */
-#endif
 	}
-  }
-#if defined(SH_USE_SCANDIR)
-  free(entryList);
-#else
-  while (thisEntry != NULL);
-#endif
-  
-  SH_MUTEX_UNLOCK(mutex_readdir);
+  } while (thisEntry != NULL);
 
   closedir (thisDir);
@@ -2359,12 +1582,10 @@
     if (sig_termfast == 1) 
       {
-	SH_FREE(theDir);
-	SH_FREE(tmpname);
 	SL_RETURN((0), _("sh_files_checkdir"));
       }
 
     BREAKEXIT(sh_derr);
-
-    if (0 == (simple_rand(&state) % 5)) (void) sh_derr();
+    if (0 == (rand() % 5))
+      (void) sh_derr();
     
     /* ---- Check the file. ---- 
@@ -2378,7 +1599,5 @@
     rdepth_next     = rdepth - 1;
     class_next      = class;
-    check_flags_next = check_flags;
     file_class_next = class;
-    file_check_flags_next = check_flags;
     checked_flag    = -1;
     cchecked_flag   = -1;
@@ -2388,6 +1607,4 @@
      * is checked with the policy of the parent directory
      */
-    SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
-    SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
     dst_ptr         = (dirstack_t *) zAVLSearch(zdirListOne, tmpcat);
 
@@ -2399,7 +1616,7 @@
 	 */
 	file_class_next    = dst_ptr->class;
-	file_check_flags_next = dst_ptr->check_flags;
 	checked_flag       = dst_ptr->checked;
 	cchecked_flag      = dst_ptr->childs_checked;
+	break;
       }
 
@@ -2415,12 +1632,10 @@
 	     */
 	    file_class_next    = dst_ptr->class;
-	    file_check_flags_next = dst_ptr->check_flags;
 	    checked_flag       = dst_ptr->checked;
 	    cchecked_flag      = dst_ptr->childs_checked;
+	    break;
 	  }
       }
-    SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
-
-    SH_MUTEX_LOCK_UNSAFE(mutex_zfiles);
+
     dst_ptr         = (dirstack_t *) zAVLSearch(zfileList, tmpcat);
 
@@ -2432,18 +1647,8 @@
 	 */
 	file_class_next    = dst_ptr->class;
-	file_check_flags_next = dst_ptr->check_flags;
 	checked_flag       = dst_ptr->checked;
-	/* not set, hence always FALSE                   */
-	/* cchecked_flag      = dst_ptr->childs_checked; */
-
-	if (checked_flag != S_TRUE)
-	  {
-	    /* -- need to check the file itself --
-	     */
-	    if (sh.flag.reportonce == S_TRUE)
-	      dummy = dst_ptr->is_reported;
-	  }
+	cchecked_flag      = dst_ptr->childs_checked;
+	break;
       }
-    SH_MUTEX_UNLOCK_UNSAFE(mutex_zfiles);
     
     /* ---- Has been checked already. ----
@@ -2456,5 +1661,5 @@
 	if (checkit == SH_FILE_DIRECTORY) 
 	  {
-	    ++theDir->NumDirs;
+	    ++theDir.NumDirs;
 	  }
 	SH_FREE(tmpcat);
@@ -2475,38 +1680,26 @@
 	/* -- need to check the file itself --
 	 */
-	/* -- moved up -- 
-	 * if (dst_ptr && sh.flag.reportonce == S_TRUE)
-	 *   dummy = dst_ptr->is_reported;
-	 */
-
-	checkit = sh_files_filecheck (file_class_next, file_check_flags_next, 
+	if (dst_ptr && sh.flag.reportonce == S_TRUE)
+	  dummy = dst_ptr->reported;
+
+	checkit = sh_files_filecheck (file_class_next, 
 				      iname, 
 				      dirlist->sh_d_name,
 				      &dummy, 0);
 
-	
-	SH_MUTEX_LOCK_UNSAFE(mutex_zfiles);
-	dst_ptr         = (dirstack_t *) zAVLSearch(zfileList, tmpcat);
-
 	if (dst_ptr && checked_flag == S_FALSE)
 	  dst_ptr->checked = S_TRUE;
-
 	/* Thu Mar  7 15:09:40 CET 2002 Propagate the 'reported' flag
 	 */
 	if (dst_ptr && sh.flag.reportonce == S_TRUE)
-	  dst_ptr->is_reported = dummy;
-
-	if (dst_ptr)
-	  dst_ptr->childs_checked = S_TRUE;
-	SH_MUTEX_UNLOCK_UNSAFE(mutex_zfiles);
+	  dst_ptr->reported = dummy;
       }
     
     if      (checkit == SH_FILE_REGULAR)   
-      ++theDir->NumRegular;
+      ++theDir.NumRegular;
     
     else if (checkit == SH_FILE_DIRECTORY) 
       {
-	++theDir->NumDirs;
-
+	++theDir.NumDirs;
 	if (rdepth_next >= 0 && cchecked_flag != S_TRUE) 
 	  {
@@ -2519,74 +1712,57 @@
 	    cchecked_flag = -1;
 	    
-	    SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
-	    SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
-	    tmp_ptr     = (dirstack_t *) zAVLSearch(zdirListOne, tmpcat);
-
-	    if (tmp_ptr) 
+	    dst_ptr     = (dirstack_t *) zAVLSearch(zdirListOne, tmpcat);
+
+	    if (dst_ptr) 
 	      {
 		TPT((0, FIL__, __LINE__, 
 		     _("msg=<%s -> recursion depth %d\n>"),
-		     tmp_ptr->name, tmp_ptr->rdepth));
-		rdepth_next   = tmp_ptr->rdepth;
-		class_next    = tmp_ptr->class;
-		check_flags_next = tmp_ptr->check_flags;
+		     dst_ptr->name, dst_ptr->rdepth));
+		rdepth_next   = dst_ptr->rdepth;
+		class_next    = dst_ptr->class;
 		/* 28. Aug 2001 reversed
 		 */
-		cchecked_flag = tmp_ptr->childs_checked;
-		checked_flag  = tmp_ptr->checked;
+		cchecked_flag = dst_ptr->childs_checked;
+		checked_flag  = dst_ptr->checked;
+		break;
 	      }
 	    
 	    if (checked_flag == -1)
 	      {
-		tmp_ptr     = (dirstack_t *) zAVLSearch(zdirListTwo, tmpcat);
-
-		if (tmp_ptr) 
+		dst_ptr     = (dirstack_t *) zAVLSearch(zdirListTwo, tmpcat);
+
+		if (dst_ptr) 
 		  {
 		    TPT((0, FIL__, __LINE__, 
 			 _("msg=<%s -> recursion depth %d\n>"),
-			 tmp_ptr->name, tmp_ptr->rdepth));
-		    rdepth_next   = tmp_ptr->rdepth;
-		    class_next    = tmp_ptr->class;
-		    check_flags_next = tmp_ptr->check_flags;
+			 dst_ptr->name, dst_ptr->rdepth));
+		    rdepth_next   = dst_ptr->rdepth;
+		    class_next    = dst_ptr->class;
 		    /* 28. Aug 2001 reversed
 		     */
-		    cchecked_flag = tmp_ptr->childs_checked;
-		    checked_flag  = tmp_ptr->checked;
+		    cchecked_flag = dst_ptr->childs_checked;
+		    checked_flag  = dst_ptr->checked;
+		    break;
 		  }
 	      }
-
-	    if (tmp_ptr && cchecked_flag == S_FALSE)
-	      {
-		tmp_ptr->childs_checked = S_TRUE;
-		/*
-		 * 04. Feb 2006 avoid double checking
-		 */
-		tmp_ptr->checked        = S_TRUE;
-	      }
-	    SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
-
+	    
 	    if (cchecked_flag == S_FALSE)
 	      {
-		sh_files_checkdir (class_next, check_flags_next, rdepth_next, 
-				   tmpcat, dirlist->sh_d_name);
-		/*
-		  tmp_ptr->childs_checked = S_TRUE;
-		  tmp_ptr->checked        = S_TRUE;
-		*/
+		sh_files_checkdir (class_next, rdepth_next, tmpcat, 
+				   dirlist->sh_d_name);
+		dst_ptr->childs_checked = S_TRUE;
 	      }
 	    else if (checked_flag == -1)
-	      sh_files_checkdir (class_next, check_flags_next, rdepth_next, 
-				 tmpcat, dirlist->sh_d_name);
+	      sh_files_checkdir (class_next, rdepth_next, tmpcat, 
+				 dirlist->sh_d_name);
 	    
 	  }
       }
     
-    else if (checkit == SH_FILE_SYMLINK)   ++theDir->NumSymlinks;
-    else if (checkit == SH_FILE_FIFO)      ++theDir->NumFifos;
-    else if (checkit == SH_FILE_SOCKET)    ++theDir->NumSockets;
-    else if (checkit == SH_FILE_CDEV)      ++theDir->NumCDev;
-    else if (checkit == SH_FILE_BDEV)      ++theDir->NumBDev;
-    else if (checkit == SH_FILE_DOOR)      ++theDir->NumDoor;
-    else if (checkit == SH_FILE_PORT)      ++theDir->NumPort;
+    else if (checkit == SH_FILE_SYMLINK)   ++theDir.NumSymlinks;
+    else if (checkit == SH_FILE_FIFO)      ++theDir.NumFifos;
+    else if (checkit == SH_FILE_SOCKET)    ++theDir.NumSockets;
+    else if (checkit == SH_FILE_CDEV)      ++theDir.NumCDev;
+    else if (checkit == SH_FILE_BDEV)      ++theDir.NumBDev;
     
     SH_FREE(tmpcat);
@@ -2594,29 +1770,21 @@
     if ((sig_termfast == 1) || (sig_terminate == 1)) 
       {
-	SH_FREE(theDir);
-	sh_dummy_dirlist = NULL;
-	SH_FREE(tmpname);
 	SL_RETURN((0), _("sh_files_checkdir"));
       }
     
     dirlist = dirlist->next;
-
-    /* -- moved up, only affects zfileList anyway
-     * if (dst_ptr)
-     *   dst_ptr->childs_checked = S_TRUE;
-     */
-
+    
   } while (dirlist != NULL);
 
-  if (flag_err_info == S_TRUE)
+  if (flag_err_info == SL_TRUE)
     {
       sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DSUM,
-		       theDir->NumDirs,
-		       theDir->NumRegular,
-		       theDir->NumSymlinks,
-		       theDir->NumFifos,
-		       theDir->NumSockets,
-		       theDir->NumCDev,
-		       theDir->NumBDev);
+		       theDir.NumDirs,
+		       theDir.NumRegular,
+		       theDir.NumSymlinks,
+		       theDir.NumFifos,
+		       theDir.NumSockets,
+		       theDir.NumCDev,
+		       theDir.NumBDev);
     }
 
@@ -2627,15 +1795,12 @@
    * Hardlink check; not done on MacOS X because of resource forks
    */
-  if ((sh_check_hardlinks == S_TRUE) && (hardlink_num != theDir->NumDirs)) 
-    {
-      if (0 != sh_files_hle_test(hardlink_num-theDir->NumDirs, iname))
+  if ((sh_check_hardlinks == S_TRUE) && (hardlink_num != theDir.NumDirs)) 
+    {
+      if (0 != sh_files_hle_test(hardlink_num-theDir.NumDirs, iname))
 	{
-	  len = strlen(tmpname);
-	  if (sl_ok_adds(len, 256)) 
-	    len += 256;
-	  tmpcat = SH_ALLOC(len);
-	  sl_snprintf(tmpcat, len, 
+	  tmpcat = SH_ALLOC(strlen(tmpname) + 256);
+	  sl_snprintf(tmpcat, strlen(tmpname) + 256, 
 		      _("%s: subdirectory count (%d) != hardlinks (%d)"),
-		      tmpname, theDir->NumDirs, hardlink_num);
+		      tmpname, theDir.NumDirs, hardlink_num);
 	  sh_error_handle (ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__, 0, 
 			   MSG_E_SUBGEN, tmpcat, _("sh_files_checkdir"));
@@ -2646,78 +1811,31 @@
 
   SH_FREE(tmpname);
-  SH_FREE(theDir);
-
-  sh_dummy_dirlist = NULL;
 
   SL_RETURN((0), _("sh_files_checkdir"));
 }
 
-void sh_files_fixup_mask (int class, unsigned long * check_flags)
-{
-  if (class == SH_LEVEL_ALLIGNORE)
-    MODI_SET((*check_flags), MODI_ALLIGNORE);
-  sh_tiger_get_mask_hashtype(check_flags);
-  return;
-}
-
 int get_the_fd (SL_TICKET ticket);
 
-static int sh_use_rsrc = S_FALSE;
-
-int sh_files_use_rsrc(const char * str)
-{
-  return sh_util_flagval(str, &sh_use_rsrc);
-}
-
-static void * sh_dummy_fileName;
-static void * sh_dummy_tmpname;
-static void * sh_dummy_tmpdir;
-
-ShFileType sh_files_filecheck (int class, unsigned long check_flags,
-			       const char * dirName, 
-			       const char * infileName,
-			       int * reported, 
-			       int rsrcflag)
+
+static ShFileType sh_files_filecheck (int class, char * dirName, 
+				      char * fileName,
+				      int * reported, 
+				      int rsrcflag)
 {
   /* 28 Aug 2001 allow NULL fileName
    */
-  char          * fullpath;
-  char            fileHash[2*(KEY_LEN + 1)];
+  char            fullpath[PATH_MAX];
+  char            fileHash[KEY_LEN + 1];
   int             status;
-  file_type     * theFile;
+  file_type       theFile;
   char          * tmpdir;
   char          * tmpname;
-  const char    * fileName;
-#if !defined(O_NOATIME)
   struct utimbuf  utime_buf;
-#endif
-  static unsigned int state = 1;
-  char            sc;
 
   SL_ENTER(_("sh_files_filecheck"));
 
-  fullpath = SH_ALLOC(PATH_MAX);
-  theFile  = SH_ALLOC(sizeof(file_type));
-
-  /* Take the address to keep gcc from putting it into a register. 
-   * Avoids the 'clobbered by longjmp' warning. 
-   */
-  sh_dummy_fileName = (void *) &fileName;
-  sh_dummy_tmpname  = (void *) &tmpname;
-  sh_dummy_tmpdir   = (void *) &tmpdir;
-
   BREAKEXIT(sh_derr);
-
-  if (0 == (simple_rand(&state) % 2)) (void) sh_derr();
-
-  if (dirName && infileName && (dirName[0] == '/') && (dirName[1] == '\0')
-      && (infileName[0] == '/') && (infileName[1] == '\0'))
-    {
-      fileName = NULL;
-    }
-  else
-    {
-      fileName = infileName;
-    }
+  if (0 == (rand() % 2))
+    (void) sh_derr();
 
   /* fileName may be NULL if this is a directory
@@ -2725,9 +1843,5 @@
   if (dirName == NULL /* || fileName == NULL */)
     {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
       sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_NULL);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      SH_FREE(fullpath);
-      SH_FREE(theFile);
       SL_RETURN(SH_FILE_UNKNOWN, _("sh_files_filecheck"));
     }
@@ -2740,9 +1854,7 @@
 	{
 	  tmpname = sh_util_safe_name (fileName);
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
 	  sh_error_handle (ShDFLevel[SH_ERR_T_NAME], FIL__, __LINE__, 0,
 			   MSG_FI_OBSC2,
 			   "", tmpname);
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
 	  SH_FREE(tmpname);
 	}
@@ -2751,9 +1863,7 @@
 	  tmpdir  = sh_util_safe_name (dirName);
 	  tmpname = sh_util_safe_name (fileName);
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
 	  sh_error_handle (ShDFLevel[SH_ERR_T_NAME], FIL__, __LINE__, 0,
 			   MSG_FI_OBSC2,
 			   tmpdir, tmpname);
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
 	  SH_FREE(tmpname);
 	  SH_FREE(tmpdir);
@@ -2767,23 +1877,18 @@
       tmpdir  = sh_util_safe_name (dirName);
       tmpname = sh_util_safe_name (fileName);
-      SH_MUTEX_LOCK(mutex_thread_nolog);
       sh_error_handle (ShDFLevel[SH_ERR_T_FILE],  FIL__, __LINE__, 0,
 		       MSG_FI_2LONG2,
 		       tmpdir, tmpname);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
       SH_FREE(tmpname);
       SH_FREE(tmpdir);
-      SH_FREE(fullpath);
-      SH_FREE(theFile);
       SL_RETURN(SH_FILE_UNKNOWN, _("sh_files_filecheck"));
     } 
 
+
   /* stat the file and determine checksum (if a regular file)
    */
-  sl_strlcpy (theFile->fullpath, fullpath, PATH_MAX);
-  theFile->check_flags    = check_flags /* sh_files_maskof(class) */;
-  theFile->file_reported = (*reported);
-  theFile->attr_string   = NULL;
-  theFile->link_path     = NULL;
+  sl_strlcpy (theFile.fullpath, fullpath, PATH_MAX);
+  theFile.check_mask = sh_files_maskof(class);
+  theFile.reported   = (*reported);
 
   TPT(( 0, FIL__, __LINE__, _("msg=<checking file: %s>\n"),  fullpath));
@@ -2792,6 +1897,6 @@
 			     ShDFLevel[class] : ShDFLevel[SH_ERR_T_FILE], 
 			     fileName,
-			     theFile, fileHash, class);
-
+			     &theFile, fileHash, class);
+  
   if (status != 0)
     {
@@ -2799,9 +1904,5 @@
 	    fullpath, status));
       if (class == SH_LEVEL_ALLIGNORE && sh.flag.checkSum != SH_CHECK_INIT)
-	sh_hash_set_visited_true (fullpath);
-      if (theFile->attr_string) SH_FREE(theFile->attr_string);
-      if (theFile->link_path)   SH_FREE(theFile->link_path);
-      SH_FREE(fullpath);
-      SH_FREE(theFile);
+	  sh_hash_set_visited_true (fullpath);
       SL_RETURN(SH_FILE_UNKNOWN, _("sh_files_filecheck"));
     }
@@ -2813,27 +1914,23 @@
   /* report
    */
-  if ((flag_err_debug == S_TRUE) && (theFile->c_mode[0] == '-'))
+  if ((flag_err_debug == SL_TRUE) && (theFile.c_mode[0] == '-'))
     {
       tmpname = sh_util_safe_name (fullpath); /* fixed in 1.5.4 */
-      SH_MUTEX_LOCK(mutex_thread_nolog);
       sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_CSUM,
 		       fileHash, tmpname);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
       SH_FREE(tmpname);
     } 
   ++sh.statistics.files_checked;
       
-  if ( sh.flag.checkSum == SH_CHECK_INIT) 
-    {
-      if (class == SH_LEVEL_ALLIGNORE)
-	MODI_SET(theFile->check_flags, MODI_ALLIGNORE);
-      if (S_TRUE == sh_ignore_chk_mod(theFile->fullpath))
-	MODI_SET(theFile->check_flags, MODI_NOCHECK);
-      sh_tiger_get_mask_hashtype(&(theFile->check_flags));
-
-      sh_dbIO_rootfs_strip(theFile->fullpath);
-      if (theFile->link_path)
-	sh_dbIO_rootfs_strip_link(theFile->link_path);
-      sh_dbIO_data_write (theFile, fileHash);
+  if      ( sh.flag.checkSum == SH_CHECK_INIT && sh.flag.update == S_FALSE ) 
+    {
+      sh_hash_pushdata (&theFile, fileHash);
+    }
+  else if (sh.flag.checkSum == SH_CHECK_INIT && sh.flag.update == S_TRUE )
+    {
+      if (0 == sh_hash_compdata (class, &theFile, fileHash, NULL, -1))
+	{
+	  sh_hash_pushdata (&theFile, fileHash);
+	}
     }
   else if (sh.flag.checkSum == SH_CHECK_CHECK 
@@ -2842,39 +1939,30 @@
 	   ) 
     {
-      if (sh.flag.update == S_TRUE)
-	{
-	  if (class == SH_LEVEL_ALLIGNORE)
-	    MODI_SET(theFile->check_flags, MODI_ALLIGNORE);
-	  if (S_TRUE == sh_ignore_chk_mod(theFile->fullpath))
-	    MODI_SET(theFile->check_flags, MODI_NOCHECK);
-	  sh_tiger_get_mask_hashtype(&(theFile->check_flags));
-	}
-      sh_hash_compdata (class, theFile, fileHash, NULL, -1);
+      sh_hash_compdata (class, &theFile, fileHash, NULL, -1);
     }
   
-  (*reported) = theFile->file_reported;
+  (*reported) = theFile.reported;
 
   /* reset the access time 
    */
+  if (class == SH_LEVEL_NOIGNORE && (theFile.check_mask & MODI_ATM) != 0)
+    {
+      utime_buf.actime   = (time_t) theFile.atime;
+      utime_buf.modtime  = (time_t) theFile.mtime;
 #if !defined(O_NOATIME)
-  if (class == SH_LEVEL_NOIGNORE && (theFile->check_flags & MODI_ATM) != 0)
-    {
-      utime_buf.actime   = (time_t) theFile->atime;
-      utime_buf.modtime  = (time_t) theFile->mtime;
-
       retry_aud_utime (FIL__, __LINE__, fullpath, &utime_buf);
-    }
-#endif
+#endif
+    }
   
-#if defined(HOST_IS_DARWIN)
+#ifdef HOST_IS_DARWIN
   /*
    * Check for resource fork
    */
-  if ( (sh_use_rsrc == S_TRUE) && (theFile->c_mode[0] != 'd') && (rsrcflag == 0) )
+  if ( (theFile.c_mode[0] != 'd') && (rsrcflag == 0) )
     {
       int  dummy;
       static int rsrc_init = 0;
       static char rsrc[17];
-      char * testpath = SH_ALLOC(PATH_MAX);
+      char testpath[PATH_MAX];
 
       if (rsrc_init == 0) {
@@ -2888,25 +1976,17 @@
       if (sl_strlen(testpath) == (17 + sl_strlen(fullpath)))
 	{
-	  if (S_TRUE == sh_unix_file_exists (testpath))
+	  if (0 == sh_unix_file_stat (testpath))
 	    {
-	      sh_files_filecheck (class, check_flags, fullpath, rsrc, &dummy, 1);
+	      sh_files_filecheck (class, fullpath, rsrc, &dummy, 1);
 	    }
 	}
-      SH_FREE(testpath);
     }
 #else
-  (void) rsrcflag; /* avoid compiler warning */
+      (void) rsrcflag; /* avoid compiler warning */
 #endif
 
  ret_point:
 
-  sc = theFile->c_mode[0];
-
-  if (theFile->attr_string) SH_FREE(theFile->attr_string);
-  if (theFile->link_path)   SH_FREE(theFile->link_path);
-  SH_FREE(fullpath);
-  SH_FREE(theFile);
-
-  switch (sc) 
+  switch (theFile.c_mode[0]) 
     {
     case '-': SL_RETURN(SH_FILE_REGULAR, _("sh_files_filecheck"));   
@@ -2916,6 +1996,4 @@
     case 'b': SL_RETURN(SH_FILE_BDEV, _("sh_files_filecheck"));      
     case '|': SL_RETURN(SH_FILE_FIFO, _("sh_files_filecheck"));      
-    case 'D': SL_RETURN(SH_FILE_DOOR, _("sh_files_filecheck"));    
-    case 'P': SL_RETURN(SH_FILE_PORT, _("sh_files_filecheck"));    
     case 's': SL_RETURN(SH_FILE_SOCKET, _("sh_files_filecheck"));    
     default:  SL_RETURN(SH_FILE_UNKNOWN, _("sh_files_filecheck"));   
@@ -2927,6 +2005,5 @@
 /* concatenate statpath = testdir"/"d_name
  */
-static int sh_files_fullpath (const char * testdir, const char * d_name, 
-			      char * statpath)
+static int sh_files_fullpath (char * testdir, char * d_name, char * statpath)
 {
   int llen = 0;
@@ -2953,160 +2030,4 @@
 }
 
-/* -----------------------------------
- * Routines required for inotify 
- * -----------------------------------
- */
-int sh_files_search_dir(char * name, int * class, 
-			unsigned long *check_flags, int *reported,
-			int * rdepth)
-{
-  volatile int retval = 0;
-#if defined(HAVE_GLOB_H) && defined(HAVE_FNMATCH_H)
-  sh_globstack_t * testPattern;
-  zAVLCursor   cursor;
-#endif
-  dirstack_t * item;
-
-  SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
-
-  item = zAVLSearch(zdirListOne, name);
-
-  if (item)
-    {
-      *check_flags = item->check_flags;
-      *class      = item->class;
-      *reported   = item->is_reported;
-      *rdepth     = item->rdepth;
-      item->checked        = S_FALSE;
-      item->childs_checked = S_FALSE;
-      item->is_reported    = S_FALSE;
-      retval = 1;
-      goto out;
-    }
-
-  item = zAVLSearch(zdirListTwo, name);
-
-  if (item)
-    {
-      *check_flags = item->check_flags;
-      *class      = item->class;
-      *reported   = item->is_reported;
-      *rdepth     = item->rdepth;
-      item->checked        = S_FALSE;
-      item->childs_checked = S_FALSE;
-      item->is_reported    = S_FALSE;
-      retval = 1;
-      goto out;
-    }
-
-#if defined(HAVE_GLOB_H) && defined(HAVE_FNMATCH_H)
-  SH_MUTEX_LOCK(mutex_zglob);
-  for (testPattern = (sh_globstack_t *) zAVLFirst (&cursor, zglobList); 
-       testPattern;
-       testPattern = (sh_globstack_t *) zAVLNext  (&cursor))
-    {
-      if (testPattern->type == SH_LIST_DIR1 || 
-	  testPattern->type == SH_LIST_DIR2)
-	{
-	  if (0 == fnmatch(testPattern->name, name, FNM_PATHNAME|FNM_PERIOD))
-	    {
-	      *check_flags = testPattern->check_flags;
-	      *class      = testPattern->class;
-	      *rdepth     = testPattern->rdepth;
-	      retval = 1;
-	      break;
-	    }
-	
-	}
-    }
-  SH_MUTEX_UNLOCK(mutex_zglob);
-#endif
- out:
-  ; /* 'label at end of compound statement' */
-  SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
-  return retval;
-}
-
-int sh_files_search_file(char * name, int * class, 
-			 unsigned long *check_flags, int *reported)
-{
-  volatile int retval = 0;
-#if defined(HAVE_GLOB_H) && defined(HAVE_FNMATCH_H)
-  sh_globstack_t * testPattern;
-  zAVLCursor   cursor;
-#endif
-  dirstack_t * item;
-
-  SH_MUTEX_LOCK(mutex_zfiles);
-  item = zAVLSearch(zfileList, name);
-
-  if (item)
-    {
-      *check_flags = item->check_flags;
-      *class      = item->class;
-      *reported   = item->is_reported;
-      retval = 1;
-    }
-  SH_MUTEX_UNLOCK(mutex_zfiles);
-
-#if defined(HAVE_GLOB_H) && defined(HAVE_FNMATCH_H)
-  if (retval == 0)
-    {
-      SH_MUTEX_LOCK(mutex_zglob);
-      for (testPattern = (sh_globstack_t *) zAVLFirst (&cursor, zglobList); 
-	   testPattern;
-	   testPattern = (sh_globstack_t *) zAVLNext  (&cursor))
-	{
-	  if (testPattern->type == SH_LIST_FILE)
-	    {
-	      if (0 == fnmatch(testPattern->name, name, 
-			       FNM_PATHNAME|FNM_PERIOD))
-		{
-		  *check_flags = testPattern->check_flags;
-		  *class      = testPattern->class;
-		  retval = 1;
-		  break;
-		}
-	      
-	    }
-	}
-      SH_MUTEX_UNLOCK(mutex_zglob);
-    }
-#endif
-
-  return retval;
-}
-
-void sh_files_set_file_reported(const char * name)
-{
-  dirstack_t * item;
-
-  SH_MUTEX_LOCK_UNSAFE(mutex_zfiles);
-  item = zAVLSearch(zfileList, name);
-
-  if (item)
-    {
-      if (sh.flag.reportonce == S_TRUE)
-	SET_SH_FFLAG_REPORTED(item->is_reported);
-    }
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_zfiles);
-  return;
-}
-
-void sh_files_clear_file_reported(const char * name)
-{
-  dirstack_t * item;
-
-  SH_MUTEX_LOCK_UNSAFE(mutex_zfiles);
-  item = zAVLSearch(zfileList, name);
-
-  if (item)
-    {
-      CLEAR_SH_FFLAG_REPORTED(item->is_reported);
-    }
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_zfiles);
-  return;
-}
 
 /* -----------------------------------
@@ -3122,5 +2043,4 @@
   dirstack_t * pfilL;
   zAVLCursor   cursor;
-  volatile int retval = -1;
 
   SL_ENTER(_("check_file"));
@@ -3133,18 +2053,12 @@
     {
       if (0 == strcmp(name, pfilL->name) &&
-	  (pfilL->check_flags & MODI_ATM) == 0 &&
-	  (pfilL->check_flags & MODI_CTM) == 0 &&
-	  (pfilL->check_flags & MODI_MTM) == 0)
-	{
-	  retval = 0;
-	  break;
-	}
-    }
-
-  SL_RETURN(retval, _("check_file"));
-}
-
-static void * sh_dummy_pdirL;
-
+	  (pfilL->check_mask & MODI_ATM) == 0 &&
+	  (pfilL->check_mask & MODI_CTM) == 0 &&
+	  (pfilL->check_mask & MODI_MTM) == 0)
+	SL_RETURN(0, _("check_file"));
+    }
+  SL_RETURN((-1), _("check_file"));
+}
+  
 int sh_files_test_setup_int (zAVLTree * tree)
 {
@@ -3158,6 +2072,4 @@
   SL_ENTER(_("sh_files_test_setup"));
 
-  sh_dummy_pdirL = (void *) &pdirL;
-
   for (pdirL = (dirstack_t *) zAVLFirst (&cursor1, tree); pdirL;
        pdirL = (dirstack_t *) zAVLNext  (&cursor1))
@@ -3165,5 +2077,4 @@
       dlen = strlen(pdirL->name);
 
-      SH_MUTEX_LOCK(mutex_zfiles);
       for (pfilL = (dirstack_t *) zAVLFirst (&cursor2, zfileList); pfilL;
 	   pfilL = (dirstack_t *) zAVLNext  (&cursor2))
@@ -3185,7 +2096,7 @@
 		  (0 == strncmp(pfilL->name, pdirL->name, dlen)))
 		{
-		  if ((pdirL->check_flags & MODI_ATM) != 0  ||
-		      (pdirL->check_flags & MODI_MTM) != 0  ||
-		      (pdirL->check_flags & MODI_CTM) != 0)
+		  if ((pdirL->check_mask & MODI_ATM) != 0  ||
+		      (pdirL->check_mask & MODI_MTM) != 0  ||
+		      (pdirL->check_mask & MODI_CTM) != 0)
 		    {
 		      if (check_file (pdirL->name) != 0)
@@ -3196,5 +2107,4 @@
 	    }
 	}
-      SH_MUTEX_UNLOCK(mutex_zfiles);
     }
 
@@ -3204,6 +2114,9 @@
 int sh_files_test_double (zAVLTree * firstList, zAVLTree * secondList)
 {
+  int          count;
   int          retval = 0;
+
   zAVLCursor   cursor;
+
   dirstack_t * first;
 
@@ -3214,4 +2127,5 @@
       if (NULL != zAVLSearch(secondList, first->name))
 	{
+	  ++count;
 	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DOUBLE,
 			   first->name);
@@ -3222,12 +2136,10 @@
 }
 
-extern void     aud_exit   (const char * file, int line, int fd);
+extern void     aud_exit   (char * file, int line, int fd);
       
 int sh_files_test_setup ()
 {
-  int retval;
-
-  SH_MUTEX_RECURSIVE_INIT(mutex_zdirs);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_zdirs);
+  int retval = 0;
+
   /* Test for modifications allowed in ReadOnly directory
    */  
@@ -3244,188 +2156,12 @@
   if (retval != 0)
     aud_exit(FIL__, __LINE__, EXIT_FAILURE);
-  SH_MUTEX_RECURSIVE_UNLOCK(mutex_zdirs);
-
+
+  /*
+  retval = sh_files_test_double (zfileList,   NULL);
+  if (retval != 0)
+    aud_exit(FIL__, __LINE__, EXIT_FAILURE);
+  */
   return 0;
 }
 
 #endif
-
-#ifdef SH_CUTEST
-#include "CuTest.h"
-
-void Test_file_lists (CuTest *tc)
-{
-#if (defined (SH_WITH_CLIENT) || defined (SH_STANDALONE))
-
-  extern int hash_remove_tree_test(char * s, char * fullpath, size_t len_s);
-
-  char * test;
-  int ret;
-
-  sh_files_pushfile_ro("/usr/test");
-  sh_files_pushfile_ro("/usr/bin/test");
-  sh_files_pushfile_ro("/usr/bin/foo/test");
-
-  sh_files_pushdir_ro("/usr");
-  sh_files_pushdir_attr("/usr/bin");
-  sh_files_pushdir_ro("/usr/bin/foo");
-
-  add_to_dirlist(zdirListOne);
-  add_to_dirlist(zdirListTwo);
-  add_to_filelist(zfileList);
-
-  test = sh_files_findfile("/usr/tes");
-  CuAssertTrue(tc, test == NULL);
-  test = sh_files_findfile("/usr/test");
-  CuAssertPtrNotNull(tc, test);
-  test = sh_files_findfile("/usr/testi");
-  CuAssertTrue(tc, test == NULL);
-  test = sh_files_findfile("/test");
-  CuAssertTrue(tc, test == NULL);
-
-  test = sh_files_find_mostspecific_dir("/usr/bin/foo/test");
-  CuAssertStrEquals(tc, "/usr/bin/foo", test);
-  test = sh_files_find_mostspecific_dir("/usr/bin/test");
-  CuAssertStrEquals(tc, "/usr/bin", test);
-  test = sh_files_find_mostspecific_dir("/usr/test");
-  CuAssertStrEquals(tc, "/usr", test);
-  test = sh_files_find_mostspecific_dir("/test");
-  CuAssertTrue(tc, test == NULL);
-  test = sh_files_find_mostspecific_dir("/usr/foo/test");
-  CuAssertStrEquals(tc, "/usr", test);
-
-  test = sh_files_find_mostspecific_dir("/usr/bin");
-  CuAssertStrEquals(tc, "/usr/bin", test);
-
-  ret = hash_remove_tree_test("/usr", "/usr/test", strlen("/usr"));
-  CuAssertIntEquals(tc, S_FALSE, ret);
-  ret = hash_remove_tree_test("/usr", "/usr/testi", strlen("/usr"));
-  CuAssertIntEquals(tc, S_TRUE, ret);
-  ret = hash_remove_tree_test("/usr", "/usr/tes", strlen("/usr"));
-  CuAssertIntEquals(tc, S_TRUE, ret);
-
-  ret = hash_remove_tree_test("/usr/bin", "/usr/test", strlen("/usr/bin"));
-  CuAssertIntEquals(tc, S_FALSE, ret);
-  ret = hash_remove_tree_test("/usr/bin", "/usr/testi", strlen("/usr/bin"));
-  CuAssertIntEquals(tc, S_FALSE, ret);
-  ret = hash_remove_tree_test("/usr/bin", "/usr/tes", strlen("/usr/bin"));
-  CuAssertIntEquals(tc, S_FALSE, ret);
-
-  ret = hash_remove_tree_test("/usr/bin", "/usr/bin/test", strlen("/usr/bin"));
-  CuAssertIntEquals(tc, S_FALSE, ret);
-  ret = hash_remove_tree_test("/usr/bin", "/usr/bin/testi", strlen("/usr/bin"));
-  CuAssertIntEquals(tc, S_TRUE, ret);
-  ret = hash_remove_tree_test("/usr/bin", "/usr/bin/tes", strlen("/usr/bin"));
-  CuAssertIntEquals(tc, S_TRUE, ret);
-
-  ret = hash_remove_tree_test("/usr/bin", "/usr/bin", strlen("/usr/bin"));
-  CuAssertIntEquals(tc, S_TRUE, ret);
-  ret = hash_remove_tree_test("/usr", "/usr", strlen("/usr"));
-  CuAssertIntEquals(tc, S_TRUE, ret);
-  ret = hash_remove_tree_test("/usr", "/usrbin", strlen("/usr"));
-  CuAssertIntEquals(tc, S_FALSE, ret);
-  ret = hash_remove_tree_test("/", "/usrbin", strlen("/"));
-  CuAssertIntEquals(tc, S_TRUE, ret);
-  ret = hash_remove_tree_test("/", "/usr", strlen("/"));
-  CuAssertIntEquals(tc, S_FALSE, ret);
-
-#else
-  (void) tc; /* fix compiler warning */
-  return;
-#endif
-}
-
-void Test_file_dequote (CuTest *tc)
-{
-#if (defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)) 
-
-  char str1[]  = "1234567890";
-  char str1a[] = "123456\\\"789\\r";
-  char str1b[] = "12345678\\r9";
-  char str1c[] = "12345678\\x0a_9";
-  char str1d[] = "12345678\\007_9";
-  char str1e[] = "123456789\\\\";
-
-  char str2[] = "1234567890\\xw";
-  char str3[] = "1234567890\\xw99";
-  char str4[] = "1234567890\\0ww";
-  char str5[] = "12345\\g67890";
-  char str6[] = "1234567890\\009a";
-
-  char *s, *p, *q;
-  size_t lo, lr;
-
-  s = SH_ALLOC(64); sl_strlcpy(s, str1, 64); p = s; lo = strlen(s); lr = lo;
-  q = sh_files_C_dequote(s, &lr);
-  CuAssertPtrNotNull(tc, q);
-  CuAssertTrue(tc, p  == q);
-  CuAssertTrue(tc, lr == lo);
-
-  s = SH_ALLOC(64); sl_strlcpy(s, str1a, 64); p = s; lo = strlen(s); lr = lo;
-  q = sh_files_C_dequote(s, &lr);
-  CuAssertPtrNotNull(tc, q);
-  CuAssertTrue(tc, p  != q);
-  CuAssertTrue(tc, 0 == strcmp(q, "123456\"789\r"));
-  CuAssertTrue(tc, lr == (lo-2));
-
-  s = SH_ALLOC(64); sl_strlcpy(s, str1b, 64); p = s; lo = strlen(s); lr = lo;
-  q = sh_files_C_dequote(s, &lr);
-  CuAssertPtrNotNull(tc, q);
-  CuAssertTrue(tc, p  != q);
-  CuAssertTrue(tc, 0 == strcmp(q, "12345678\r9"));
-  CuAssertTrue(tc, lr == (lo-1));
-
-  s = SH_ALLOC(64); sl_strlcpy(s, str1c, 64); p = s; lo = strlen(s); lr = lo;
-  q = sh_files_C_dequote(s, &lr);
-  CuAssertPtrNotNull(tc, q);
-  CuAssertTrue(tc, p  != q);
-  CuAssertTrue(tc, 0 == strcmp(q, "12345678\x0a_9"));
-  CuAssertTrue(tc, lr == (lo-3));
-
-  s = SH_ALLOC(64); sl_strlcpy(s, str1d, 64); p = s; lo = strlen(s); lr = lo;
-  q = sh_files_C_dequote(s, &lr);
-  CuAssertPtrNotNull(tc, q);
-  CuAssertTrue(tc, p  != q);
-  CuAssertTrue(tc, 0 == strcmp(q, "12345678\007_9"));
-  CuAssertTrue(tc, lr == (lo-3));
-
-  s = SH_ALLOC(64); sl_strlcpy(s, str1e, 64); p = s; lo = strlen(s); lr = lo;
-  q = sh_files_C_dequote(s, &lr);
-  CuAssertPtrNotNull(tc, q);
-  CuAssertTrue(tc, p  != q);
-  CuAssertTrue(tc, 0 == strcmp(q, "123456789\\"));
-  CuAssertTrue(tc, lr == (lo-1));
-
-  s = SH_ALLOC(64); sl_strlcpy(s, str2, 64); p = s; lo = strlen(s); lr = lo;
-  q = sh_files_C_dequote(s, &lr);
-  CuAssertTrue(tc, q == NULL);
-  CuAssertTrue(tc, lr == 0);
-
-  s = SH_ALLOC(64); sl_strlcpy(s, str3, 64); p = s; lo = strlen(s); lr = lo;
-  q = sh_files_C_dequote(s, &lr);
-  CuAssertTrue(tc, q == NULL);
-  CuAssertTrue(tc, lr == 0);
-
-  s = SH_ALLOC(64); sl_strlcpy(s, str4, 64); p = s; lo = strlen(s); lr = lo;
-  q = sh_files_C_dequote(s, &lr);
-  CuAssertTrue(tc, q == NULL);
-  CuAssertTrue(tc, lr == 0);
-
-  s = SH_ALLOC(64); sl_strlcpy(s, str5, 64); p = s; lo = strlen(s); lr = lo;
-  q = sh_files_C_dequote(s, &lr);
-  CuAssertTrue(tc, q == NULL);
-  CuAssertTrue(tc, lr == 0);
-
-  s = SH_ALLOC(64); sl_strlcpy(s, str6, 64); p = s; lo = strlen(s); lr = lo;
-  q = sh_files_C_dequote(s, &lr);
-  CuAssertTrue(tc, q == NULL);
-  CuAssertTrue(tc, lr == 0);
-
-  return;
-#else
-  (void) tc; /* fix compiler warning */
-  return;
-#endif
-}
-#endif
-
Index: trunk/src/sh_filetype.c
===================================================================
--- trunk/src/sh_filetype.c	(revision 591)
+++ 	(revision )
@@ -1,608 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 2011 Rainer Wichmann                                      */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-#include "config_xor.h"
-
-#ifndef NULL
-#if !defined(__cplusplus)
-#define NULL ((void*)0)
-#else
-#define NULL (0)
-#endif
-#endif
-
-#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
-
-#include "samhain.h"
-#include "sh_mem.h"
-#include "sh_error_min.h"
-#include "sh_utils.h"
-
-#define FIL__ _("sh_filetype.c")
-
-#include <string.h>
-#include <stdlib.h>
-#include <ctype.h>
-
-/* #define SH_FILE_MAIN 1 */
-#ifdef  SH_FILE_MAIN
-#include <stdio.h>
-#define _(a) a
-#define N_(a) a
-#define sl_strlcpy strncpy
-#endif
-
-#define SH_FTYPE_MAX 32
-
-/* List of filetype description, in the format: 
- * offset : type(0=text, 1=binary) : length(if binary) : G1 : G2 : G3 : Name : Teststring
- *
- * This list is mostly taken from the 'filetype' library by Paul L Daniels.
- *
- * Copyright (c) 2003, PLD
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or 
- * without modification, are permitted provided that the 
- * following conditions are met:
- * 
- *  * Redistributions of source code must retain the above 
- *  copyright notice, this list of conditions and the following 
- *  disclaimer.
- *  
- *  * Redistributions in binary form must reproduce the above 
- *  copyright notice, this list of conditions and the following 
- *  disclaimer in the documentation and/or other materials provided 
- *  with the distribution.
- *  
- *  * Neither the name of the PLD nor the names of its contributors 
- *  may be used to endorse or promote products derived from this software 
- *  without specific prior written permission.
- *  
- *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
- *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
- *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
- *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
- *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
- *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
- *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
- *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
- *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
- *  POSSIBILITY OF SUCH DAMAGE.
- */
-
-
-char * sh_ftype_list[] = {
-
-  N_("6:0:0:IMAGE:COMPRESSED:JPG:JFIF Jpeg:JFIF"),
-  N_("0:0:0:IMAGE:COMPRESSED:PNG:PNG:=89PNG=0d=0a=1a=0a"),
-  N_("0:0:0:IMAGE:COMPRESSED:JPG:JFIF Jpeg:=FF=D8=FF"),
-  N_("0:0:0:IMAGE:COMPRESSED:GIF:GIF:GIF97a"),
-  N_("0:0:0:IMAGE:COMPRESSED:GIF:GIF:GIF89a"),
-  N_("0:0:0:IMAGE:COMPRESSED:GIF:GIF:GIF87a"),
-  N_("0:1:4:IMAGE:COMPRESSED:TIFF:TIFF-LE:II=2A=00"),
-  N_("0:1:4:IMAGE:COMPRESSED:TIFF:TIFF-BE:MM=00=2A"),
-  N_("0:1:2:IMAGE:COMPRESSED:PCX:PCX25:=0A=00"),
-  N_("0:1:2:IMAGE:COMPRESSED:PCX:PCX28WP:=0A=02"),
-  N_("0:1:2:IMAGE:COMPRESSED:PCX:PCX28NP:=0A=03"),
-  N_("0:1:2:IMAGE:COMPRESSED:PCX:PCX30:=0A=05"),
-  N_("0:0:0:IMAGE:RAW:BMP:Bitmap:BM"),
-  N_("0:0:0:IMAGE:RAW:XPM:XPM:/* XPM */"),
-  N_("0:0:0:IMAGE:SPECIAL:AUTOCAD:DWT:AC=31=30=31"),
-  N_("0:0:0:IMAGE:SPECIAL:AUTOCAD:DWF:(DWF V"),
-  N_("0:0:0:IMAGE:SPECIAL:AUTOCAD:WMF:=D7=CD=C6=9A"),
-  N_("0:0:0:IMAGE:SPECIAL:AUTOCAD:DWG:AC10"),
-  N_("8:0:0:IMAGE:SPECIAL:COREL:CorelDraw:CDR"),
-  N_("0:0:0:IMAGE:SPECIAL:FITS:Fits file:SIMPLE=20=20="),
-  N_("1536:0:0:IMAGE:SPECIAL:VISIO:VisioDraw:Visio"),
-  N_("128:0:0:IMAGE:SPECIAL:DICM:DICOM medical:DICM"),
-  N_("0:0:0:IMAGE:SPECIAL:PHS:Photoshop:8BPS"),
-  N_("0:0:0:IMAGE:SPECIAL:XCF:Gimp XCF:gimp xcf"),
-  N_("0:0:0:MOVIE:COMPRESSED:RIFF:RIFF/AVI Movie:RIFF"),
-  N_("0:0:0:MOVIE:RAW:MOV:SGI Movie:MOVI:.mov SGI Movie"),
-  N_("0:1:4:MOVIE:COMPRESSED:MPG:Mpeg 2:=00=00=01=BA"),
-  N_("0:1:4:MOVIE:COMPRESSED:MPG:Mpeg 2:=00=00=01=B3"),
-  N_("4:0:0:MOVIE:COMPRESSED:QT:QuickTime:moov"),
-  N_("4:0:0:MOVIE:COMPRESSED:QT:QuickTime:mdat"),
-  N_("36:0:0:MOVIE:COMPRESSED:QT:QuickTime:moov"),
-  N_("36:0:0:MOVIE:COMPRESSED:QT:QuickTime:mdat"),
-  N_("68:0:0:MOVIE:COMPRESSED:QT:QuickTime:moov"),
-  N_("68:0:0:MOVIE:COMPRESSED:QT:QuickTime:mdat"),
-  N_("0:1:3:MOVIE:COMPRESSED:FLI:FLIC animation:=00=11=AF"),
-  N_("0:0:0:MOVIE:COMPRESSED:FLASH:Flash data:FWS"),
-  N_("0:0:0:MOVIE:COMPRESSED:FLASH:Flash data:CWS"),
-  N_("0:0:0:MOVIE:COMPRESSED:FLASH:Flash video:FLV"),
-  N_("0:0:0:MOVIE:COMPRESSED:WMV:WMV:=30=26=B2=75=8E=66=CF"),
-  N_("0:0:0:AUDIO:RAW:SND:Sun Audio:.snd"),
-  N_("0:0:0:AUDIO:RAW:EMOD:EMOD:Mod"),
-  N_("1080:0:0:AUDIO:RAW:MOD:SoundTracker (.M.K):.M.K"),
-  N_("1080:0:0:AUDIO:RAW:MOD:SoundTracker (M.K.):M.K."),
-  N_("1080:0:0:AUDIO:RAW:MOD:NoiseTracker:N.T."),
-  N_("1080:0:0:AUDIO:RAW:MOD:SoundTracker (M!K!):M!K!"),
-  N_("1080:0:0:AUDIO:RAW:MOD:SoundTracker (M&K!):M&K!"),
-  N_("8:0:0:AUDIO:RAW:WAVE:Wave:WAVE"),
-  N_("0:1:4:AUDIO:RAW:DEC:DEC-Audio:=00=64=73=2E"),
-  N_("0:0:0:AUDIO:STANDARD:MIDI:Midi:MThd"),
-  N_("0:0:0:AUDIO:COMPRESSED:REAL:RealMedia:.RMF"),
-  N_("0:0:0:AUDIO:COMPRESSED:OGG:Ogg Vorbis:OggS"),
-  N_("0:0:0:AUDIO:COMPRESSED:FLAC:Flac:fLaC"),
-  N_("0:1:5:AUDIO:COMPRESSED:MP3:MP3 Audio:=49=44=33=02=00"),
-  N_("0:1:5:AUDIO:COMPRESSED:MP3:MP3 Audio:=49=44=33=03=00"),
-  N_("0:1:5:AUDIO:COMPRESSED:MP3:MP3 Audio:=49=44=33=04=00"),
-  N_("0:1:2:AUDIO:COMPRESSED:MP3:MP3 Audio:=ff=fb"),
-  N_("0:1:2:AUDIO:COMPRESSED:MP3:MP3 Audio:=ff=fa"),
-  N_("2:0:0:ARCHIVE:COMPRESSED:LHA:Lha 0:-lh0-"),
-  N_("2:0:0:ARCHIVE:COMPRESSED:LHA:Lha 1:-lh1-"),
-  N_("2:0:0:ARCHIVE:COMPRESSED:LHA:Lha 4:-lz4-"),
-  N_("2:0:0:ARCHIVE:COMPRESSED:LHA:Lha z5:-lz5-"),
-  N_("2:0:0:ARCHIVE:COMPRESSED:LHA:Lha 5:-lh5-"),
-  N_("0:0:0:ARCHIVE:COMPRESSED:RAR:RarArchive:Rar!"),
-  N_("0:0:0:ARCHIVE:COMPRESSED:ZIP:PkZip:PK=03=04"),
-  N_("0:0:0:ARCHIVE:COMPRESSED:7Z:7-Zip:=37=7A=BC=AF=27=1C"),
-  N_("0:0:0:ARCHIVE:COMPRESSED:COMPRESS:Compress:=1F=89"),
-  N_("0:0:0:ARCHIVE:COMPRESSED:GZIP:Gzip:=1F=8B"),
-  N_("0:0:0:ARCHIVE:COMPRESSED:BZIP2:Bzip2:BZh"),
-  N_("0:0:0:ARCHIVE:COMPRESSED:ARJ:ARJ:=60=ea"),
-  N_("0:0:0:ARCHIVE:COMPRESSED:ARJ:ARJ:=ea=60"),
-  N_("0:0:0:ARCHIVE:COMPRESSED:HPAK:HPack:HPAK"),
-  N_("0:0:0:ARCHIVE:COMPRESSED:JAM:Jam:=E9,=01JAM"),
-  N_("0:0:0:ARCHIVE:COMPRESSED:SQUISH:Squish:SQSH"),
-  N_("0:1:8:ARCHIVE:COMPRESSED:CAB:MS Cabinet:MSCF=00=00=00=00"),
-  N_("20:0:0:ARCHIVE:COMPRESSED:ZOO:Zoo:=FD=C4=A7=DC"),
-  N_("0:0:0:ARCHIVE:COMPRESSED:XPK:Amiga XPK Archive:XPKF"),
-  N_("0:0:0:ARCHIVE:PACKAGE:RPM:RPM:=ED=AB=EE=DB"),
-  N_("0:0:0:ARCHIVE:PACKAGE:DEB:DEB:!<arch>=0A""debian"),
-  N_("0:0:0:ARCHIVE:UNIX:AR:AR:!<arch>"),
-  N_("0:0:0:ARCHIVE:UNIX:AR:AR:<ar>"),
-  N_("257:1:8:ARCHIVE:UNIX:TAR:TAR:ustar=20=20=00"),
-  N_("257:1:6:ARCHIVE:UNIX:TAR:TAR:ustar=00"),
-  N_("0:0:0:LIBRARY:JAVA:CLASS:Java:=CA=FE=BA=BE"),
-  N_("2108:0:0:DOCUMENT:OFFICE:WORD:Word v5:MSWordDoc"),
-  N_("2112:0:0:DOCUMENT:OFFICE:WORD:Word v5:MSWordDoc"),
-  N_("2080:0:0:DOCUMENT:OFFICE:EXCEL:Excel v4:Microsoft Excel"),
-  N_("2080:0:0:DOCUMENT:OFFICE:WORD:MS Word:Microsoft Word"),
-  N_("0:0:0:DOCUMENT:OFFICE:WORD:Word:=94=A6=2E"),
-  N_("512:1:19:DOCUMENT:OFFICE:WORD:Word:R=00o=00o=00t=00 =00""E=00n=00t=00r=00y"),
-  N_("0:1:9:DOCUMENT:OFFICE:ALL:MSOffice:=D0=CF=11=E0=A1=B1=1A=E1=00"),
-  N_("0:0:0:DOCUMENT:ADOBE:PDF:PortableDocument:%PDF-"),
-  N_("0:0:0:DOCUMENT:ADOBE:EPS:EncapsulatedPS:%!PS-ADOBE EPS"),
-  N_("0:0:0:DOCUMENT:STANDARD:RTF:RichText:{\\rtf"),
-  N_("6:1:4:DOCUMENT:STANDARD:RTF:RichText Compressed:=00=00LZ"),
-  N_("6:0:0:DOCUMENT:ID:VCARD:VCARD:vcard"),
-  N_("0:0:0:EXECUTABLE:DOS:EXE:DosExe:MZ"),
-  N_("0:0:0:EXECUTABLE:DOS:EXE:DosExe:LZ"),
-  N_("0:0:0:EXECUTABLE:DOS:COM:DosCom 1:=E9"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Bourne:#!/bin/sh"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Bourne:#! /bin/sh"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Bourne:#!/bin/bash"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Bourne:#! /bin/bash"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Bourne:#!/usr/bin/bash"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Bourne:#! /usr/bin/bash"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Csh:#!/usr/bin/csh"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Csh:#! /usr/bin/csh"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Csh:#!/bin/csh"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Csh:#! /bin/csh"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Korn:#! /usr/bin/ksh"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Korn:#!/usr/bin/ksh"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Korn:#! /bin/ksh"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Korn:#!/bin/ksh"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Tenex:#!/usr/bin/tcsh"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Tenex:#! /usr/bin/tcsh"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Tenex:#!/bin/tcsh"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Tenex:#! /bin/tcsh"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Zsh:#!/usr/bin/zsh"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Zsh:#! /usr/bin/zsh"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Zsh:#!/bin/zsh"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Zsh:#! /bin/zsh"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:ash:#!/usr/bin/ash"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:ash:#! /usr/bin/ash"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:ash:#!/bin/ash"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:ash:#! /bin/ash"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:awk:#!/usr/bin/nawk"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:awk:#! /usr/bin/nawk"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:awk:#!/bin/nawk"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:awk:#! /bin/nawk"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:awk:#!/bin/gawk"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:awk:#! /bin/gawk"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:awk:#!/bin/awk"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:awk:#! /bin/awk"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:perl:#!/usr/bin/perl"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:perl:#! /usr/bin/perl"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:perl:#!/bin/perl"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:perl:#! /bin/perl"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Shell script:#!/"),
-  N_("0:0:0:EXECUTABLE:UNIX:SHELL:Shell script:#! /"),
-  N_("0:0:0:EXECUTABLE:UNIX:ELF:Linux ELF32:=7f""ELF=01"),
-  N_("0:0:0:EXECUTABLE:UNIX:ELF:Linux ELF:=7f""ELF=02"),
-  N_("0:0:0:EXECUTABLE:DOS:COM:DosCom 2:=8c"),
-  N_("0:0:0:EXECUTABLE:DOS:COM:DosCom 3:=eb"),
-  N_("0:0:0:EXECUTABLE:DOS:COM:DosCom 4:=b8"),
-  N_("0:1:4:EXECUTABLE:AMIGAOS:EXECUTABLE:AmigaOS Executable:=00=00=03=F3"),
-  N_("0:1:20:DATABASE:ANY:ACCESS:MSAccess:=00=01=00=00Standard=20Jet=20""DB=00"),
-  N_("0:1:2:DATABASE:ANY:MYSQL:MySQL database:=fe=01"),
-  N_("0:1:4:DATABASE:ANY:MYSQL:MySQL database:=fe=fe=03=00"),
-  N_("0:1:4:DATABASE:ANY:MYSQL:MySQL database:=fe=fe=07=00"),
-  N_("0:1:4:DATABASE:ANY:MYSQL:MySQL database:=fe=fe=05=00"),
-  N_("0:1:4:DATABASE:ANY:MYSQL:MySQL database:=fe=fe=06=00"),
-  
-  NULL, 
-  NULL, 
-  NULL, 
-  NULL, 
-
-  NULL, 
-  NULL, 
-  NULL, 
-  NULL, 
-
-  NULL, 
-  NULL, 
-  NULL, 
-  NULL, 
-
-  NULL, 
-  NULL, 
-  NULL, 
-  NULL, 
-
-  NULL,
-};
-
-static unsigned int    sh_ftype_def = 0;
-
-#define SH_FTYPE_ADD  16
-
-struct sh_ftype_rec {
-  size_t offset;
-  size_t length;
-  char   pattern[SH_FTYPE_MAX];
-
-  char   type[SH_FTYPE_MAX];
-};
-
-
-struct sh_ftype_rec ** sh_ftype_arr = NULL;
-static unsigned int    sh_ftype_nn  = 0;
-
-#if !defined(SH_FILE_MAIN)
-
-static unsigned int    sh_ftype_usr = 0;
-
-extern char * unquote_string (const char * str, size_t len);
-
-int sh_restrict_add_ftype(const char * str)
-{
-  size_t len;
-  char * cond;
-
-  if (sh_ftype_def == 0)
-    {
-      while(sh_ftype_list[sh_ftype_def] != NULL) ++sh_ftype_def;
-    }
-
-  if (!str) 
-    {
-      if (sh_ftype_usr > 0)
-	{
-	  unsigned int i, j = sh_ftype_def;
-	  
-	  for (i = 0; i < sh_ftype_usr; ++i)
-	    {
-	      SH_FREE(sh_ftype_list[j+i]);
-	      sh_ftype_list[j+i] = NULL;
-	    }   
-	  sh_ftype_usr = 0;
-	}
-
-      if (sh_ftype_arr)
-	{
-	  unsigned int i = 0;
-	  
-	  while(sh_ftype_arr[i] != NULL)
-	    {
-	      SH_FREE(sh_ftype_arr[i]);
-	      ++i;
-	    }
-	  SH_FREE(sh_ftype_arr);
-	  sh_ftype_arr = NULL;
-	}
-    }
-  else if (sh_ftype_usr < SH_FTYPE_ADD)
-    {
-      len = strlen(str);
-      cond = unquote_string(str, len);
-      sh_ftype_list[sh_ftype_def+sh_ftype_usr] = cond;
-      ++sh_ftype_usr;
-    }
-  else
-    {
-      return -1;
-    }
-  return 0;
-}
-
-
-#endif
-
-
-static int init_record(unsigned int n, char * define,
-		       struct sh_ftype_rec * record)
-{
-  unsigned int offset, dtype, length, i = 0, xn = 0;
-  char type[SH_FTYPE_MAX];
-  char pattern[SH_FTYPE_MAX];
-
-  char * end;
-  char * start;
-  
-  offset = strtoul(define, &end, 0);
-  if (*end != ':')
-    return -1;
-
-  start = end; ++start;
-  dtype  = strtoul(start,  &end, 0);
-  if (*end != ':')
-    return -1;
-
-  start = end; ++start;
-  length = strtoul(start,  &end, 0);
-  if (*end != ':')
-    return -1;
-  
-  start = end; ++start;
-
-  while (*start && (i < sizeof(type)))
-    {
-      type[i] = *start; ++start;
-      if (type[i] == ':') 
-	++xn;
-      if (xn == 3)
-	{
-	  type[i] = '\0';
-	  break;
-	}
-      ++i;
-    }
-  if (xn != 3)
-    return -1;
-
-  start = strchr(start, ':');
-
-  if (!start)
-    return -1;
-
-  ++start;
-
-  if (dtype == 0)
-    {
-      sl_strlcpy(pattern, start, sizeof(pattern));
-      length = strlen(pattern);
-    }
-  else if (length <= sizeof(pattern))
-    {
-      memcpy(pattern, start, length);
-    }
-  else
-    {
-      return -1;
-    }
-
-  for (i = 0; i < n; ++i)
-    {
-      if (sh_ftype_arr[i]->length <= length &&
-	  sh_ftype_arr[i]->offset == offset)
-	{
-	  if (0 == memcmp(sh_ftype_arr[i]->pattern, pattern, 
-			  sh_ftype_arr[i]->length))
-	    {
-#ifdef  SH_FILE_MAIN
-	      fprintf(stderr, 
-		      "Pattern %d (%s / %s) override by earlier pattern %d (%s / %s)\n",
-		      n, type, pattern,
-		      i, sh_ftype_arr[i]->type, sh_ftype_arr[i]->pattern);
-#else
-	      char errbuf[256];
-	      
-	      sl_snprintf(errbuf, sizeof(errbuf),
-			  _("Pattern %d (%s) override by earlier pattern %d (%s)"),
-			  n, type,
-			  i, sh_ftype_arr[i]->type);
-	      sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
-			      errbuf,
-			      _("init_record"));
-#endif
-	    }
-	}
-    }
-
-
-  record->offset = offset;
-  record->length = length;
-  memcpy(record->pattern, pattern, length);
-  sl_strlcpy(record->type, type, SH_FTYPE_MAX);
-
-  return 0;
-}
-
-static void file_arr_init()
-{
-  unsigned int i, nn = 0;
-
-  if (sh_ftype_def == 0)
-    {
-      while(sh_ftype_list[sh_ftype_def] != NULL) ++sh_ftype_def;
-    }
-
-  while (sh_ftype_list[nn] != NULL) ++nn;
-
-#ifdef  SH_FILE_MAIN
-  printf("%d definitions found, defined = %d\n", nn, sh_ftype_def);
-#endif
-
-#ifdef  SH_FILE_MAIN
-  sh_ftype_arr = calloc((nn+1), sizeof(struct sh_ftype_rec *));
-#else
-  sh_ftype_arr = SH_ALLOC((nn+1) * sizeof(struct sh_ftype_rec *));
-#endif
-
-  for(i = 0; i < nn; i++)
-    {
-#ifdef  SH_FILE_MAIN
-      sh_ftype_arr[i] = calloc(1, sizeof(struct sh_ftype_rec));
-#else
-      sh_ftype_arr[i] = SH_ALLOC(sizeof(struct sh_ftype_rec));
-#endif
-
-      memset(sh_ftype_arr[i], 0, sizeof(struct sh_ftype_rec));
-      if (i < sh_ftype_def) 
-	{
-	  char   * p  = _(sh_ftype_list[i]);
-	  size_t len  = strlen(p);
-	  char * cond = unquote_string(p, len); 
-	  
-	  init_record(i,             cond, sh_ftype_arr[i]);
-	}
-      else 
-	{
-	  init_record(i, sh_ftype_list[i], sh_ftype_arr[i]);
-	}
-    }
-  sh_ftype_arr[nn] = NULL;
-  sh_ftype_nn      = nn;
-
-  return;
-}
-
-static char * check_filetype(char * filetype, 
-			     const char * buffer, size_t buflen)
-{
-  unsigned int i;
-  const char * p;
-
-  if (!sh_ftype_arr)
-    {
-      file_arr_init();
-    }
-  
-  for (i = 0; i < sh_ftype_nn; ++i)
-    {
-      if (sh_ftype_arr[i]->length > 0 && 
-	  (sh_ftype_arr[i]->length + sh_ftype_arr[i]->offset) < buflen)
-	{
-	  p = &buffer[sh_ftype_arr[i]->offset];
-
-	  if (0 == memcmp(p, sh_ftype_arr[i]->pattern, sh_ftype_arr[i]->length))
-	    {
-	      sl_strlcpy(filetype, sh_ftype_arr[i]->type, SH_FTYPE_MAX);
-	      return (filetype);
-	    }
-	}
-    }
-
-  if (buflen > 0) {
-
-    int flag = 0;
-
-    p = buffer;
-    for (i = 0; i < buflen; ++i) {
-      if (*p == '\0')
-	{
-	  sl_strlcpy(filetype, _("FILE:BINARY:UNKNOWN"), SH_FTYPE_MAX);
-	  goto out;
-	}
-      else if (!isgraph((int)*p) && !isspace((int)*p))
-	{
-	  flag = 1;
-	}
-      ++p;
-    }
-    if (flag == 0)
-      {
-	sl_strlcpy(filetype, _("FILE:TEXT:ASCII"), SH_FTYPE_MAX);
-	goto out;
-      }
-  }
-  sl_strlcpy(filetype, _("FILE:UNKNOWN:UNKNOWN"), SH_FTYPE_MAX);
- out:
-  return filetype;
-}
-
-#if !defined(SH_FILE_MAIN)
-
-int matches_filetype(SL_TICKET ft, char * test_type)
-{
-  char buffer[3072];
-  char filetype[SH_FTYPE_MAX];
-  long len;
-
-  len = sl_read_timeout (ft, buffer, sizeof(buffer), 12, S_TRUE);
-
-  sl_rewind(ft);
-
-  if (len > 0)
-    {
-      check_filetype(filetype, buffer, len);
-    }
-  else
-    {
-      sl_strlcpy(filetype, _("FILE:UNKNOWN:UNKNOWN"), SH_FTYPE_MAX);
-    }
-
-  if (0 == strcmp(filetype, test_type))
-    {
-      return 1;
-    }
-
-  return 0;
-}
-
-#else
-/* SH_FILE_MAIN */
-#include <unistd.h>
-
-int main (int argc, char * argv[])
-{
-  char buffer[3072];
-  char filetype[SH_FTYPE_MAX];
-  size_t len;
-
-  FILE * fh = fopen(argv[1], "r");
-
-  if (fh)
-    {
-      int fd = fileno(fh);
-
-      len = read(fd, buffer, 3072);
-
-      check_filetype(filetype, buffer, len);
-
-      fprintf(stdout, "%s: %s\n", argv[1], filetype);
-
-      fclose(fh);
-      
-      return 0;
-    }
-  return 1;
-}
-#endif
-
-#endif
-/* #if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE) */
Index: trunk/src/sh_filter.c
===================================================================
--- trunk/src/sh_filter.c	(revision 591)
+++ 	(revision )
@@ -1,301 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 2009 Rainer Wichmann                                      */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-
-#include "config_xor.h"
-
-#include <string.h>
-#ifdef HAVE_REGEX_H
-#include <regex.h>
-#endif
-
-#include "samhain.h"
-#include "sh_utils.h"
-#include "sh_mem.h"
-#include "sh_filter.h"
-
-#undef  FIL__
-#define FIL__  _("sh_filter.c")
-
-
-void sh_filter_free (sh_filter_type * filter)
-{
-  int i;
-
-  if (filter)
-    {
-      for (i = 0; i < filter->for_c; ++i) {
-#ifdef HAVE_REGEX_H
-	if (filter->for_v[i])
-	  regfree(filter->for_v[i]);
-#else
-	if (filter->for_v[i])
-	  SH_FREE(filter->for_v[i]);
-#endif
-	filter->for_v[i] = NULL; 
-      }
-      filter->for_c = 0;
-
-      for (i = 0; i < filter->fand_c; ++i) {
-#ifdef HAVE_REGEX_H
-	if (filter->fand_v[i])
-	  regfree(filter->fand_v[i]);
-#else
-	if (filter->fand_v[i])
-	  SH_FREE(filter->fand_v[i]);
-#endif
-	filter->fand_v[i] = NULL; 
-      }
-      filter->fand_c = 0;
-
-      for (i = 0; i < filter->fnot_c; ++i) {
-#ifdef HAVE_REGEX_H
-	if (filter->fnot_v[i])
-	  regfree(filter->fnot_v[i]);
-#else
-	if (filter->fnot_v[i])
-	  SH_FREE(filter->fnot_v[i]);
-#endif
-	filter->fnot_v[i] = NULL; 
-      }
-      filter->fnot_c = 0;
-    }
-}
-
-
-int sh_filter_add (const char * str, sh_filter_type * filter, int type)
-{
-  int     i = 0;
-  int     flag = 0;
-  size_t  s;
-
-  char  * dupp;
-  char  * p;
-  char  * end;
-  int   * ntok;
-  void ** stok;
-
-  SL_ENTER(_("sh_filter_filteradd"));
-
-  if (NULL == str || NULL == filter)
-    {
-      SL_RETURN((-1), _("sh_filter_filteradd")); 
-    }
-
-  if (type == SH_FILT_OR) {
-    ntok = &(filter->for_c);
-    stok = filter->for_v;
-  }
-  else if (type == SH_FILT_AND) {
-    ntok = &(filter->fand_c);
-    stok = filter->fand_v;
-  }
-  else if (type == SH_FILT_NOT) {
-    ntok = &(filter->fnot_c);
-    stok = filter->fnot_v;
-  }
-  else {
-    SL_RETURN((-1), _("sh_filter_filteradd")); 
-  }
-
-  /* cppcheck-suppress uninitvar */
-  i = *ntok;
-  if (i == SH_FILT_NUM) {
-    SL_RETURN((-1), _("sh_filter_filteradd")); 
-  }
-
-  dupp = sh_util_strdup(str);
-  p   = dupp;
-
-  do
-    {
-      while (*p == ',' || *p == ' ' || *p == '\t')
-	++p;
-      if (*p == '\0')
-	break;
-
-      end = p; ++end;
-      if (*end == '\0')
-	break;
-
-      if (*p == '\'')
-	{
-	  ++p; end = p; if (*end != '\'') ++end;
-	  if (*p == '\0' || *end == '\0')
-	    break;
-	  while (*end != '\0' && *end != '\'')
-	    ++end;
-	}
-      else if (*p == '"')
-	{
-	  ++p; end = p; if (*end != '"') ++end;
-	  if (*p == '\0' || *end == '\0')
-	    break;
-	  while (*end != '\0' && *end != '"')
-	    ++end;
-	}
-      else
-	{
-	  while (*end != '\0' && *end != ',' && *end != ' ' && *end != '\t')
-	    ++end;
-	}
-      if (*end == '\0')
-	flag = 1;
-      else
-	*end = '\0';
-
-      s = strlen(p);
-      if (s > 0) 
-	{
-	  ++s;
-#ifdef HAVE_REGEX_H
-	  if (stok[i] != NULL)
-	    regfree((regex_t *) stok[i]);
-	  {
-	    int status;
-
-	    stok[i] = SH_ALLOC(sizeof(regex_t));
-
-	    status = regcomp((regex_t *) stok[i], p, 
-				 REG_NOSUB|REG_EXTENDED);
-	    if (status != 0) 
-	      {
-		char * errbuf = SH_ALLOC(BUFSIZ);
-		(void) regerror(status, (regex_t *) stok[i], 
-				errbuf, BUFSIZ); 
-		errbuf[BUFSIZ-1] = '\0';
-		sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_REGEX,
-				 errbuf, p);
-		SH_FREE(errbuf);
-	      }
-	  }
-#else
-	  if (stok[i] != NULL)
-	    SH_FREE(stok[i]);
-
-	  stok[i] = SH_ALLOC(s);
-	  (void) sl_strlcpy((char *) stok[i], p, s);
-#endif
-	  ++i;
-	}
-
-      p = end; ++p;
-
-      if (i == SH_FILT_NUM)
-	break;
-    }
-  while (p != NULL && *p != '\0' && flag == 0);
-
-  *ntok = i;
-  SH_FREE(dupp);
-
-  SL_RETURN (0, _("sh_filter_filteradd"));
-}
-
-#ifdef HAVE_REGEX_H
-static int sh_filter_cmp(const char * message, void * pattern)
-{
-  int result;
-
-  result = regexec((regex_t *)pattern, message, 0, NULL, 0);
-
-  if (result != 0)
-    return -1;
-
-  /* Successful match. */
-  return 0;
-}
-#else
-static int sh_filter_cmp(const char * message, void * pattern)
-{
-  if (NULL == sl_strstr(message, (char *)pattern))
-    return -1;
-
-  /* Successful match. */
-  return 0;
-}
-#endif
-
-/*
- * -- Check filters. Returns 0 if message passes.
- */ 
-int sh_filter_filter (const char * message, sh_filter_type * filter)
-{
-  int i;
-
-  SL_ENTER(_("sh_filter_filter"));
-
-  if (filter)
-    {
-
-      /* Presence of any of these keywords prevents execution.
-       */
-      if (filter->fnot_c > 0)
-	{
-	  for (i = 0; i < filter->fnot_c; ++i)
-	    {
-	      if (0 == sh_filter_cmp(message, filter->fnot_v[i]))
-		{
-		  SL_RETURN ((-1), _("sh_filter_filter"));
-		}
-	    }
-	}
-      
-      /* Presence of all of these keywords is required for execution.
-       */
-      if (filter->fand_c > 0)
-	{
-	  for (i = 0; i < filter->fand_c; ++i)
-	    {
-	      if (0 != sh_filter_cmp(message, filter->fand_v[i]))
-		{
-		  SL_RETURN ((-1), _("sh_filter_filter"));
-		}
-	    }
-	}
-      
-      /* Presence of at least one of these keywords is required for execution.
-       */
-      if (filter->for_c > 0)
-	{
-	  for (i = 0; i < filter->for_c; ++i)
-	    {
-	      if (0 == sh_filter_cmp(message, filter->for_v[i]))
-		{
-		  goto isok;
-		}
-	    }
-	  SL_RETURN ((-1), _("sh_filter_filter"));
-	}
-    }
-
- isok:
-  SL_RETURN ((0), _("sh_filter_filter"));
-}
-
-sh_filter_type * sh_filter_alloc(void)
-{
-  sh_filter_type * filter = SH_ALLOC(sizeof(sh_filter_type));
-
-  memset(filter, 0, sizeof(sh_filter_type));
-  filter->for_c  = 0; 
-  filter->fand_c = 0; 
-  filter->fnot_c = 0;
-  return filter;
-}
Index: trunk/src/sh_forward.c
===================================================================
--- trunk/src/sh_forward.c	(revision 1)
+++ trunk/src/sh_forward.c	(revision 1)
@@ -0,0 +1,5652 @@
+/* SAMHAIN file system integrity testing                                   */
+/* Copyright (C) 1999, 2000 Rainer Wichmann                                */
+/*                                                                         */
+/*  This program is free software; you can redistribute it                 */
+/*  and/or modify                                                          */
+/*  it under the terms of the GNU General Public License as                */
+/*  published by                                                           */
+/*  the Free Software Foundation; either version 2 of the License, or      */
+/*  (at your option) any later version.                                    */
+/*                                                                         */
+/*  This program is distributed in the hope that it will be useful,        */
+/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
+/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
+/*  GNU General Public License for more details.                           */
+/*                                                                         */
+/*  You should have received a copy of the GNU General Public License      */
+/*  along with this program; if not, write to the Free Software            */
+/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
+
+#include "config_xor.h"
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+
+/* Must be early on FreeBSD
+ */
+#include <sys/types.h>
+
+/* must be .le. than (1020 * 64)
+ * (see sh_tools.c -- put_header)
+ *
+ * also: must be  (N * 16), otherwise
+ * binary files cannot be transferred encrypted
+ *
+ * 65280 = (1020*64)
+ * #define TRANS_BYTES 8000  V0.8
+ */
+#ifdef  SH_ENCRYPT_2
+#define TRANS_BYTES 65120
+#else
+#define TRANS_BYTES 65280
+#endif
+
+/* timeout for session key
+ */
+#define TIMEOUT_KEY 7200
+
+/* max time between connection attempts
+ */
+#define TIMEOUT_CON 2048 
+
+/* #undef  SRP_DEBUG */
+/* #define SRP_DEBUG */
+
+#ifdef HAVE_MEMORY_H
+#include <memory.h>
+#endif
+
+#if TIME_WITH_SYS_TIME
+#include <sys/time.h>
+#include <time.h>
+#else
+#if HAVE_SYS_TIME_H
+#include <sys/time.h>
+#else
+#include <time.h>
+#endif
+#endif
+
+/*
+#ifdef TM_IN_SYS_TIME
+#include <sys/time.h>
+#else
+#include <time.h>
+#endif
+*/
+
+#ifdef HAVE_SYS_SELECT_H
+#include <sys/select.h>
+#endif
+
+#ifdef  HAVE_UNISTD_H
+#include <errno.h>
+#include <signal.h>
+#include <setjmp.h>
+#include <pwd.h>
+#include <grp.h>
+#include <sys/stat.h>
+#include <sys/resource.h>
+#include <fcntl.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#endif
+
+#ifndef FD_SET
+#define NFDBITS         32
+#define FD_SET(n, p)    ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
+#define FD_CLR(n, p)    ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
+#define FD_ISSET(n, p)  ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
+#endif /* !FD_SET */
+#ifndef FD_SETSIZE
+#define FD_SETSIZE      32
+#endif
+#ifndef FD_ZERO
+#define FD_ZERO(p)      memset((char *)(p), '\0', sizeof(*(p)))
+#endif
+
+#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
+#include <sys/mman.h>
+#endif
+
+
+#include <netdb.h> 
+#include <sys/types.h> 
+#include <netinet/in.h> 
+#include <sys/socket.h> 
+#ifndef S_SPLINT_S
+#include <arpa/inet.h>
+#endif
+
+#include "samhain.h"
+#include "sh_tiger.h"
+#include "sh_utils.h"
+#include "sh_unix.h"
+#include "sh_forward.h"
+#include "sh_srp.h"
+#include "sh_fifo.h"
+#include "sh_tools.h"
+#include "sh_entropy.h"
+#include "sh_html.h"
+#include "sh_mail.h"
+#include "sh_socket.h"
+#define SH_NEED_GETHOSTBYXXX
+#include "sh_static.h"
+
+#ifdef SH_ENCRYPT
+#include "rijndael-api-fst.h"
+char * sh_tools_makePack (unsigned char * header, 
+			  char * payload, int payload_size,
+			  keyInstance * keyInstE);
+char * sh_tools_revertPack (unsigned char * header, char * message,
+			    keyInstance * keyInstE, 
+			    unsigned long message_size);
+#endif
+
+/* define this if you want to debug the client/server communication */
+/* #define SH_DBG_PROT 1 */
+
+#ifdef  SH_DBG_PROT
+#define SH_SHOWPROT(c,d) sh_tools_show_header((c), (d))
+#else
+#define SH_SHOWPROT(c,d) 
+#endif
+
+/* the port client will be connecting to 
+ */
+#ifndef SH_DEFAULT_PORT
+#define SH_DEFAULT_PORT 49777    
+#endif
+
+#ifndef SH_SELECT_REPEAT
+#define SH_SELECT_REPEAT 60
+#endif
+
+#ifndef SH_HEADER_SIZE
+#define SH_HEADER_SIZE 7
+#endif
+
+#ifndef SH_CHALLENGE_SIZE
+#define SH_CHALLENGE_SIZE 9
+#endif
+
+#undef  FIL__
+#define FIL__  _("sh_forward.c")
+
+int     clt_class = (-1);
+
+#ifndef SH_STANDALONE
+
+#if defined(WITH_TRACE) || defined(WITH_TPT) 
+char * hu_trans(const char * ihu)
+{
+  static char ohu[17];
+  sprintf(ohu, _("%c%03o"), '\\',                   /* known to fit  */
+	  (unsigned char) ihu[0]);
+  sprintf(&(ohu[4]), _("%c%03o"), '\\',             /* known to fit  */
+	  (unsigned char) ihu[1]);
+  sprintf(&(ohu[8]), _("%c%03o"), '\\',             /* known to fit  */
+	  (unsigned char) ihu[2]);
+  sprintf(&(ohu[12]), _("%c%03o"), '\\',            /* known to fit  */
+	  (unsigned char) ihu[3]);
+  ohu[16] = '\0';
+  return ohu;
+}
+#endif
+
+static int StripDomain = S_TRUE;
+
+int sh_forward_set_strip (char * str)
+{
+  static int fromcl = 0;
+  char dummy[2] = "F";
+
+  if (fromcl == 1)
+    return 0;
+
+  if (str == NULL)
+    {
+      fromcl = 1;
+      return (sh_util_flagval(dummy, &StripDomain));
+    }
+  else
+    return (sh_util_flagval(str, &StripDomain));
+}
+
+#include <ctype.h>
+
+const char * sh_strip_domain (char *name)
+{
+  char *          first;
+  static char     name_2[SH_MINIBUF+1];
+  register int    i = 0;
+
+  SL_ENTER(_("sh_strip_domain"));
+
+  if (StripDomain == S_FALSE || (first  = strchr(name, '.')) == NULL) 
+    {
+      SL_RETURN( name, _("sh_strip_domain"));
+    }
+  else
+    {
+
+      /* check whether it is in dotted number format
+       * --> last part must be kept
+       */
+      if (0 != is_numeric(name))
+	{
+	  SL_RETURN( name, _("sh_strip_domain"));
+	  /*
+	  i = sl_strlen(name) - 1;
+	  while (name[i] != '.' && i >= 0)
+	    --i;
+	  if (name[i] == '.') ++i;
+	  sl_strlcpy( name_2, &name[i], SH_MINIBUF +1 );
+	  */
+	}
+      else
+	{
+	  memset (name_2, 0, SH_MINIBUF+1);
+	  first = name;
+	  while (i < SH_MINIBUF && *first != '.' && *first != '\0')
+	    {
+	      name_2[i] = *first;
+	      ++first; ++i;
+	    }
+	  name_2[SH_MINIBUF] = '\0';
+	}
+    }
+
+  SL_RETURN( name_2, _("sh_strip_domain"));
+}
+
+/* #ifndef SH_STANDALONE */
+#endif
+
+#ifndef USE_SRP_PROTOCOL
+static
+void sh_passwd (char * salt, char * password, char * nounce, char *hash)
+{
+
+  char           *combi;
+  size_t          len;
+  register int i;
+  unsigned char * dez = NULL;
+
+  if (password == NULL)
+    dez = (unsigned char *) &(skey->pw[0]);
+  else if (sl_strlen(password) < PW_LEN)
+    {
+      fprintf(stderr, _("Password has less than %d chars !\n"),
+		   PW_LEN);
+      _exit(EXIT_FAILURE);
+    }
+
+  if (password == NULL)
+    {
+      /* --- copy password ---
+       */
+      for (i = 0; i < PW_LEN; ++i)
+	{ 
+	  skey->vernam[i] = (char)(*dez); 
+	  ++dez; 
+	}
+      (void) sl_strlcpy (skey->vernam,
+			 sh_tiger_hash(skey->vernam, TIGER_DATA, PW_LEN), 
+			 KEY_LEN+1);
+    }
+  else
+    {
+      (void) sl_strlcpy (skey->vernam, password, KEY_LEN+1);
+    }
+
+  len = sl_strlen(salt) + sl_strlen(skey->vernam) + 1;
+  if (nounce != NULL) 
+    len += sl_strlen(nounce);
+  
+  /* H(s,P)
+   */
+  combi = SH_ALLOC(len);
+  (void) sl_strlcpy (combi, salt, len);
+  (void) sl_strlcat (combi, skey->vernam, len);
+  if (nounce != NULL)
+    (void) sl_strlcat (combi, nounce, len);
+  (void) sl_strlcpy (hash, 
+		     sh_tiger_hash(combi, TIGER_DATA, 
+				   (unsigned long) sl_strlen(combi)),
+		     KEY_LEN+1);
+
+  /*
+    fprintf(stderr, "DD: A: <%s>\n", salt);
+    fprintf(stderr, "DD: P: <%s>\n", skey->pw); 
+    fprintf(stderr, "DD: V: <%s>\n", skey->vernam); 
+    fprintf(stderr, "DD: C: <%s>\n", combi);
+    fprintf(stderr, "DD: H: <%s>\n", hash); 
+  */
+
+  SH_FREE (combi);
+  hash[KEY_LEN] = '\0';
+  return;
+}
+#endif
+
+#ifdef SH_WITH_CLIENT
+
+static int count_dev_server = 0;
+
+void reset_count_dev_server(void)
+{
+  count_dev_server = 0;
+  return;
+}
+
+int sh_forward_setlogserver (char * address)
+{
+  SL_ENTER(_("sh_forward_setlogserver"));
+
+  if (address != NULL && count_dev_server < 2 
+      && sl_strlen(address) < SH_PATHBUF && sl_strlen(address) > 0) 
+    {
+      if (count_dev_server == 0)
+	(void) sl_strlcpy (sh.srvexport.name, address, SH_PATHBUF);
+      else
+	(void) sl_strlcpy (sh.srvexport.alt,  address, SH_PATHBUF);
+
+      ++count_dev_server;
+      SL_RETURN (0, _("sh_forward_setlogserver"));
+    }
+  SL_RETURN (-1, _("sh_forward_setlogserver"));
+}
+
+static
+int sh_forward_send_intern (int mysocket, char protocol, char * micro, 
+			    char * msgbuf, unsigned long length, int docrypt)
+{
+  unsigned long           numbytes, countbytes;
+  int                     flag_err = 0;
+  unsigned char           head[SH_HEADER_SIZE];
+  char                  * outbuf;
+
+#ifdef SH_ENCRYPT
+
+  unsigned long           blkfac;
+  int                     rem;
+  unsigned long           length2;
+  char                  * msg2buf = NULL;
+  char                  * p, * q;
+  RIJ_BYTE                    inBlock[B_SIZ]; 
+  RIJ_BYTE                    outBlock[B_SIZ];
+  unsigned long           j;
+  cipherInstance          cipherInst;
+  int                     err_num;
+#else
+  docrypt = SL_FALSE; /* dummy to fix compiler warning */
+#endif
+
+  SL_ENTER(_("sh_forward_send_intern"));
+
+#ifdef SH_ENCRYPT
+  if      ((S_TRUE == docrypt) && ((protocol & SH_PROTO_EN2) != (char)0))
+    {
+      put_header (head, (int)protocol, &length, micro);
+      msg2buf  = sh_tools_makePack (head, msgbuf, (int) length, 
+				    &(skey->keyInstE));
+      /*@-usedef@*/
+      length   = (unsigned long) (256 * (unsigned int)head[1] + 
+				  (unsigned int)head[2]);
+      /*@+usedef@*/
+      outbuf   = msg2buf;
+    }
+  else if ((S_TRUE == docrypt) && ((protocol & SH_PROTO_ENC) != (char)0))
+    {
+      blkfac  = length/B_SIZ;
+      rem     = (int) (length - (B_SIZ * blkfac));
+      length2 = (B_SIZ * blkfac) + ((rem == 0) ? 0 : B_SIZ);
+
+      msg2buf = SH_ALLOC((size_t)length2);
+      p       = msgbuf;
+      q       = msg2buf;
+      
+      err_num = cipherInit (&cipherInst, (RIJ_BYTE)MODE_CBC, NULL);
+
+      if (err_num < 0)
+	sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
+			errorExplain(err_num), 
+			_("sh_forward_send_intern: cipherInit"));
+
+
+      for (j = 0; j < blkfac; ++j)
+	{
+	  memcpy(inBlock, p, B_SIZ);
+	  err_num = blockEncrypt(&cipherInst, &(skey->keyInstE), 
+				 inBlock, 128 * BNUM, outBlock);
+	  if (err_num < 0)
+	    sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
+			    errorExplain(err_num), 
+			    _("sh_forward_send_intern: blockEncrypt"));
+	  memcpy(q, outBlock, B_SIZ);
+	  p += B_SIZ;
+	  q += B_SIZ;
+	}
+      if (rem > 0)
+	{
+	  memset(inBlock, 0, B_SIZ);
+	  memcpy(inBlock, p, (size_t)rem);
+	  err_num = blockEncrypt(&cipherInst, &(skey->keyInstE), 
+				 inBlock, 128 * BNUM, outBlock);
+	  if (err_num < 0)
+	    sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
+			    errorExplain(err_num), 
+			    _("sh_forward_send_intern: blockEncrypt"));
+	  memcpy(q, outBlock, B_SIZ);
+	  q += B_SIZ;
+	}
+
+      outbuf = msg2buf;
+      length = length2;
+      put_header (head, (int)protocol, &length, micro);
+    }
+  else
+    {
+      outbuf = msgbuf;
+      put_header (head, (int)protocol, &length, micro);
+    }
+#else
+  outbuf = msgbuf;
+  put_header (head, (int)protocol, &length, micro);
+#endif
+
+  SH_SHOWPROT(head,'>');
+  
+  numbytes     = SH_HEADER_SIZE;
+  countbytes   = write_port (mysocket, 
+			     (char *) head, numbytes,
+			     &flag_err, 300);
+
+  if (countbytes == numbytes && outbuf != NULL)
+    {
+      numbytes     = (length);
+      countbytes   = write_port (mysocket, 
+				 outbuf, numbytes,
+				 &flag_err, 300);
+    }
+
+#ifdef SH_ENCRYPT
+  /*@-usedef@*/
+  if (msg2buf != NULL)
+    SH_FREE(msg2buf);
+  /*@+usedef@*/
+#endif
+
+  if (countbytes == numbytes)
+    {
+      SL_RETURN( 0, _("sh_forward_send_intern"));
+    }
+  else
+    {
+      SL_RETURN( flag_err, _("sh_forward_send_intern"));
+    }
+}
+static
+int sh_forward_send (int mysocket, char protocol, char * micro, 
+		     char * msgbuf, unsigned long length)
+{
+  int i;
+  SL_ENTER(_("sh_forward_send"));
+  TPT(( 0, FIL__, __LINE__, _("msg=<Send.>\n")));
+  i =  sh_forward_send_intern (mysocket, protocol, micro, 
+			       msgbuf, length, S_FALSE);
+  SL_RETURN(i, _("sh_forward_send"));
+}
+static
+int sh_forward_send_crypt (int mysocket, char protocol, char * micro, 
+			   char * msgbuf, unsigned long length)
+{
+  int i;
+  SL_ENTER(_("sh_forward_send_crypt"));
+#ifdef SH_ENCRYPT
+  TPT(( 0, FIL__, __LINE__, _("msg=<Send encrypted.>\n")));
+#else
+  TPT(( 0, FIL__, __LINE__, _("msg=<Send.>\n")));
+#endif
+  i = sh_forward_send_intern (mysocket, protocol, micro, 
+			      msgbuf, length, S_TRUE);
+  SL_RETURN(i, _("sh_forward_send_crypt"));
+}
+
+
+/* receive answer, add a trailing NULL to terminate string
+ * rev 0.8
+ */
+static
+long sh_forward_receive_intern (int mysocket, char protocol, char * micro,     
+				char *  msgbuf, unsigned long length, 
+				int docrypt)
+{
+  unsigned long numbytes, countbytes;
+  int           flag_err = -1;
+  unsigned char head[SH_HEADER_SIZE];
+
+#ifdef SH_ENCRYPT
+
+  unsigned long           head_length;
+  unsigned long           blkfac;
+  /* unsigned long           length2; */
+  char                  * p, * q, * tmp;
+  RIJ_BYTE                    inBlock[B_SIZ]; 
+  RIJ_BYTE                    outBlock[B_SIZ];
+  unsigned long           j;
+  cipherInstance          cipherInst;
+  int                     err_num;
+#else
+  docrypt = SL_FALSE; /* dummy to fix compiler warning */
+#endif
+
+  SL_ENTER(_("sh_forward_receive_intern"));
+
+  if (micro != NULL)
+    micro[4]     = '\0';
+
+  if (msgbuf != NULL)
+    msgbuf[0]     = '\0';
+
+  numbytes     = SH_HEADER_SIZE;
+  countbytes   = read_port (mysocket, 
+			    (char *) head, numbytes,
+			    &flag_err, 300);
+
+  if (countbytes != numbytes)
+    {
+      TPT(( 0, FIL__, __LINE__, _("msg=<countbytes != numbytes>\n")));
+      SL_RETURN(flag_err, _("sh_forward_receive_intern"));
+    }
+  /*@-usedef +ignoresigns@*/
+  else if (head[0] != protocol &&  
+	   (head[0] & SH_PROTO_SRP) == (char)0 /* not set */)
+    {
+      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_MISMATCH);
+      SL_RETURN((-1), _("sh_forward_receive_intern"));
+    }
+  /*@+usedef -ignoresigns@*/
+  else
+    {
+      get_header (head, &numbytes, micro);
+      SH_SHOWPROT(head, '<');
+
+      if (numbytes > 0)
+	{
+	  numbytes = (numbytes > length ? length : numbytes);
+
+	  countbytes   = read_port (mysocket, 
+				    msgbuf, numbytes,
+				    &flag_err, 300);
+
+	  if (countbytes < length)
+	    msgbuf[countbytes] = '\0';
+	  else
+	    msgbuf[length-1] = '\0';
+
+	  if (flag_err != 0)
+	    {
+	      TPT(( 0, FIL__, __LINE__, _("msg=<read error>\n")));
+	      SL_RETURN((-1), _("sh_forward_receive_intern"));
+	    }
+	}
+    }
+
+#ifdef SH_ENCRYPT
+  if      ((S_TRUE == docrypt) && ((protocol & SH_PROTO_EN2) != (char)0))
+    {
+      tmp = SH_ALLOC((size_t)length);
+      memcpy(tmp, msgbuf, (size_t)length);
+      tmp = sh_tools_revertPack (head, tmp, &(skey->keyInstD), countbytes);
+
+      head_length = (unsigned long) (256 * (unsigned int)head[1] + 
+				     (unsigned int)head[2]);
+      head_length = (head_length > length ? length : head_length);
+      length      = head_length;
+
+      memcpy(msgbuf, tmp, (size_t)length);
+      msgbuf[length] = '\0';
+      SH_FREE(tmp);
+      if (countbytes == numbytes) 
+	{
+	  countbytes = length; /* to avoid error on return, see below */
+	}
+      numbytes = length;
+    }
+  else if ((S_TRUE == docrypt) && ((protocol & SH_PROTO_ENC) != (char)0))
+    {
+      /* Decrypt only complete blocks. If there is an incomplete block,
+       * something is wrong anyway.
+       * Decrypt in place.
+       */
+      blkfac  = countbytes/B_SIZ;
+      /* length2 = (B_SIZ * blkfac); */
+      p       = msgbuf;
+      q       = msgbuf;
+      
+      err_num = cipherInit (&cipherInst, (RIJ_BYTE)MODE_CBC, NULL);
+
+      if (err_num < 0)
+	sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
+			errorExplain(err_num), 
+			_("sh_forward_receive_intern: cipherInit"));
+
+      for (j = 0; j < blkfac; ++j)
+	{
+	  memcpy(inBlock, p, B_SIZ);
+	  err_num = blockDecrypt(&cipherInst, &(skey->keyInstD), 
+				 inBlock, 128 * BNUM, outBlock);
+	  if (err_num < 0)
+	    sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
+			    errorExplain(err_num), 
+			    _("sh_forward_receive_intern: blockDecrypt"));
+	  memcpy(q, outBlock, B_SIZ);
+	  p += 16;
+	  q += 16;
+	}
+    }
+#endif
+
+  if (countbytes == numbytes)
+    {
+      SL_RETURN(((long)numbytes), _("sh_forward_receive_intern"));
+    }
+  else
+    {
+      TPT(( 0, FIL__, __LINE__, _("msg=<short read>\n")));
+      SL_RETURN(flag_err, _("sh_forward_receive_intern"));
+    }
+}
+
+static
+long sh_forward_receive (int mysocket, char protocol, char * micro,     
+			 char * msgbuf, unsigned long length)
+{
+  long i;
+  SL_ENTER(_("sh_forward_receive"));
+  TPT(( 0, FIL__, __LINE__, _("msg=<Receive.>\n")));
+  i = sh_forward_receive_intern (mysocket, protocol, micro, 
+				 msgbuf, length, S_FALSE);
+  SL_RETURN(i, _("sh_forward_receive"));
+}
+
+static
+long sh_forward_receive_crypt (int mysocket, char protocol, char * micro,     
+			       char * msgbuf, unsigned long length)
+{
+  long i;
+  SL_ENTER(_("sh_forward_receive_crypt"));
+#ifdef SH_ENCRYPT
+  TPT(( 0, FIL__, __LINE__, _("msg=<Receive encrypted.>\n")));
+#else
+  TPT(( 0, FIL__, __LINE__, _("msg=<Receive.>\n")));
+#endif
+  i = sh_forward_receive_intern (mysocket, protocol, micro, 
+				 msgbuf, length, S_TRUE);
+  SL_RETURN(i, _("sh_forward_receive"));
+}
+
+/**************************************************
+ *
+ *
+ *  C L I E N T  
+ *
+ *
+ ***************************************************/
+
+
+#include <time.h>
+
+static SH_FIFO * fifo = NULL;
+
+static long sh_forward_try (char * errmsg);
+
+
+long sh_forward (char * errmsg)
+{
+  static int           have_server = GOOD;
+  long   status;
+  char * popmsg;
+  static int failed = GOOD;
+
+  SL_ENTER(_("sh_forward"));
+
+  /* --- No log server available. ---
+   */
+  if (have_server == GOOD && sh.srvexport.name[0] == '\0')
+    {
+      have_server = BAD;
+      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NONAME);
+      SL_RETURN (-1, _("sh_forward"));
+    }
+  else if (have_server == BAD)
+    {
+      SL_RETURN (-1, _("sh_forward"));
+    }
+
+  /* --- Allocate fifo. ---
+   */
+  if (fifo == NULL)
+    {
+      fifo = SH_ALLOC(sizeof(SH_FIFO));
+      fifo_init(fifo);
+    }
+
+  /* --- Check for messages on the queue, and send them first. ---
+   */
+  while (NULL != (popmsg = pop_list(fifo)) )
+    {
+      status = sh_forward_try (popmsg);
+      if (status != 0)
+	{
+	  (void) push_tail_list (fifo, popmsg); 
+	  SH_FREE(popmsg);
+	  if (SH_FIFO_MAX == push_list (fifo, errmsg))
+	    {
+	      SL_RETURN (-2, _("sh_forward"));
+	    }
+	  SL_RETURN (-1, _("sh_forward"));
+	}
+      SH_FREE(popmsg);
+    }
+
+  /* --- Now send the error message. ---
+   */ 
+  status = sh_forward_try (errmsg);
+  if (status != 0)
+    {
+      if (failed == GOOD)
+	sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_SRV_FAIL,
+			 _("log server"), 
+			 sh.srvexport.name);
+      failed = BAD;
+      if (SH_FIFO_MAX == push_list (fifo, errmsg))    /* push message on stack */
+	{
+	  SL_RETURN (-2, _("sh_forward"));
+	}
+      SL_RETURN (-1, _("sh_forward"));
+    }
+
+  failed = GOOD;
+  SL_RETURN (0, _("sh_forward"));  
+}
+
+static long sh_forward_try_impl (char * errmsg, char what);
+
+static long sh_forward_try (char * errmsg)
+{
+  long i;
+  SL_ENTER(_("sh_forward_try"));
+  i = sh_forward_try_impl (errmsg, (char)SH_PROTO_MSG);
+  SL_RETURN(i, _("sh_forward_try")); 
+}
+
+long sh_forward_req_file (char * file)
+{
+  long i;
+  char tmp_file[8];
+  SL_ENTER(_("sh_forward_req_file"));
+  (void) sl_strlcpy(tmp_file, file, 8);
+  i = sh_forward_try_impl (tmp_file, (char)SH_PROTO_BIG);
+  SL_RETURN(i, _("sh_forward_req_file")); 
+}
+
+static  long sh_forward_try_impl (char * errmsg, char what)
+{
+  static int           initialized = BAD;
+  static int           conn_state  = GOOD;
+  int                  sockfd;
+  int                  flag_err;
+  char               * answer;
+
+  unsigned char        theProto;
+
+  char                 hash[KEY_LEN+1];
+  size_t               len;
+  char               * buffer;
+#ifdef SH_ENCRYPT_2
+  size_t               pos;  /* for the server command */
+#endif
+  char   head_u[5];
+
+  char   nsrv[KEY_LEN+1];
+  char   nclt[KEY_LEN+1];
+  SL_TICKET sfd = -1;
+  int    transfercount;
+
+  char   foo_M1[KEY_LEN+1];
+  UINT32 ticks;
+
+  char         error_msg[256];
+  char         error_call[SH_MINIBUF];
+  int          error_num = 0;
+
+#ifdef USE_SRP_PROTOCOL
+  char   u_real[SH_CHALLENGE_SIZE];
+  char * foo_A;
+  char * foo_Sc;
+  char * M;
+#else
+  char   nounce[KEY_LEN+1];
+  char   temp[2*KEY_LEN+1];
+  char   nonce_u[KEY_LEN+1];
+#endif
+  
+#ifdef SH_ENCRYPT
+  int err_num;
+#endif
+
+  static time_t time_now  = 1200;
+  static time_t time_last =    0;
+
+  static time_t timeout_val =  1;
+
+  SL_ENTER(_("sh_forward_try_impl"));
+
+  /* --- No message to transmit. ---
+   */
+  if (errmsg == NULL && initialized == GOOD)
+    SL_RETURN( 0, _("sh_forward_try_impl"));
+  
+  /* --- Connection in bad state. ---
+   */
+  if (initialized == BAD || conn_state == BAD)
+    {
+      timeout_val = 
+	(time_t)((timeout_val > TIMEOUT_CON) ? TIMEOUT_CON : timeout_val);
+
+      /* --- Retry bad attempt only after some time. ---
+       */
+      time_now  = time (NULL);
+      if ((time_now - time_last) < timeout_val) 
+	{
+	  TPT(( 0, FIL__, __LINE__, _("msg=<Within deadtime, no retry.>\n")));
+	  SL_RETURN( (-1), _("sh_forward_try_impl"));
+	}
+      TPT(( 0, FIL__, __LINE__, _("msg=<Retry.>\n")));
+    }
+  time_last  = time (NULL);
+
+
+  /* --- Try to connect to log server. ---
+   */
+  error_call[0] = '\0';
+
+  sockfd = connect_port_2 (sh.srvexport.name, sh.srvexport.alt, 
+			   SH_DEFAULT_PORT, 
+			   error_call, &error_num, error_msg, 256);
+
+  if (sockfd < 0)
+    {
+      conn_state = BAD;
+      timeout_val *= 2;
+      sh_error_handle ((-1), FIL__, __LINE__, error_num, 
+		       MSG_E_NET, error_msg, error_call,
+		       _("export"), sh.srvexport.name);
+      SL_RETURN( (-1), _("sh_forward_try_impl"));
+    }
+
+  conn_state = GOOD;
+
+  /*************************
+   *
+   *  initialization
+   * 
+   */
+
+  flag_err = 0;
+  answer   = SH_ALLOC(512);
+  MLOCK(answer, 512);
+
+
+#ifndef USE_SRP_PROTOCOL
+
+  /**************************************************
+   *
+   * --- challenge/response authentication ---
+   *
+   **************************************************/
+
+  if (initialized == BAD)
+    {
+      theProto = (unsigned char) SH_PROTO_SRP;
+
+      TPT(( 0, FIL__, __LINE__, _("msg=<c/r: entry>\n")));
+
+      (void) sl_strlcpy (answer, sh.host.name, 512);
+
+      flag_err = sh_forward_send (sockfd, (char) theProto, _("SALT"), 
+				  answer,  (unsigned long)sl_strlen(answer));
+
+      TPT(( 0, FIL__, __LINE__, _("msg=<c/r: sent SALT, flag_err = %d>\n"), 
+	    flag_err));
+
+      /* get nonce from server
+       */
+      if (flag_err == 0)
+	{
+	  flag_err = (int) sh_forward_receive (sockfd, (char)theProto, head_u, 
+					       answer,  511);
+	  flag_err = (flag_err < 0) ? flag_err : 0;
+	  TPT(( 0, FIL__, __LINE__, 
+		_("msg=<c/r: rcvt nonce, flag_err = %d>\n"), 
+		flag_err));
+	}
+
+      /* entry point for jump from message forward if session key must
+       * be re-initialized
+       */	 
+ initBlock:
+
+      if (0 == check_request (head_u, _("INIT")) && 
+	  flag_err == 0                         &&
+	  sl_strlen(answer) >  KEY_LEN )
+	(void) sl_strlcpy(nounce, &answer[KEY_LEN], KEY_LEN+1);
+      else
+	flag_err = (-1);
+
+      TPT(( 0, FIL__, __LINE__, _("msg=<c/r: rcvt INIT, flag_err = %d>\n"), 
+	    flag_err));
+
+      /* verify random nonce v from server H(v, P)v
+       */
+      sh_passwd (nounce, NULL, NULL, temp);
+      if ( 0 != sl_strncmp(temp, answer, KEY_LEN))
+	flag_err = (-1);
+
+      TPT(( 0, FIL__, __LINE__, _("msg=<c/r: vrfy nonce, flag_err = %d>\n"), 
+	    flag_err));
+
+
+      /* --- Create own nonce. ---
+       */
+      ticks = (UINT32) taus_get (&(skey->rng0[0]), 
+				 &(skey->rng1[0]),
+				 &(skey->rng2[0]));
+      (void) sl_strlcpy(nonce_u, 
+			sh_tiger_hash((char *) &ticks, 
+				      TIGER_DATA, 
+				      (unsigned long)sizeof(UINT32)),
+			KEY_LEN+1);
+
+      /* --- Form the message H(H(u,v),P)u ---
+       */
+      (void) sl_strlcpy(temp, nonce_u, 2*KEY_LEN+1); 
+      (void) sl_strlcat(temp,  nounce, 2*KEY_LEN+1); 
+      (void) sl_strlcpy(temp, 
+			sh_tiger_hash(temp, 
+				      TIGER_DATA, 
+				      (unsigned long)sl_strlen(temp)),
+			KEY_LEN+1);
+      sh_passwd (temp, NULL, NULL, foo_M1);
+      (void) sl_strlcpy(temp, foo_M1, 2*KEY_LEN+1);
+      (void) sl_strlcat(temp, nonce_u, 2*KEY_LEN+1);
+
+      /* --- Send it to server. ---
+       */
+      if (flag_err == 0)
+	{
+	  flag_err = (int) sh_forward_send (sockfd, 
+					    (char)(theProto|SH_PROTO_SRP), 
+					    _("PASS"), temp, 
+					    (unsigned long)sl_strlen(temp));
+	  TPT(( 0, FIL__, __LINE__, _("msg=<c/r: sent PASS, flag_err = %d>\n"),
+		flag_err));
+	}
+
+      if (flag_err == 0)
+	{
+	  flag_err = (int)sh_forward_receive (sockfd,
+					      (char)(theProto|SH_PROTO_SRP), 
+					      head_u, answer,  511);  
+	  sh_passwd (nounce, NULL, nonce_u, foo_M1);
+	  (void) sl_strlcpy (skey->session, foo_M1, KEY_LEN+1);
+#ifdef SH_ENCRYPT
+	  err_num = makeKey(&(skey->keyInstE), 
+			    (RIJ_BYTE)DIR_ENCRYPT, 192, skey->session);
+	  if (err_num < 0)
+	    sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
+			    errorExplain(err_num), 
+			    _("sh_forward_try_impl: makeKey"));
+
+	  err_num = makeKey(&(skey->keyInstD), 
+			    (RIJ_BYTE)DIR_DECRYPT, 192, skey->session);
+	  if (err_num < 0)
+	    sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
+			    errorExplain(err_num), 
+			    _("sh_forward_try_impl: make_key"));
+#endif
+	  initialized = GOOD;
+	}
+
+      if (initialized == BAD)
+	{
+	  timeout_val *= 2;
+	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NOAUTH);
+	  memset(answer, 0, sizeof(answer));
+	  MUNLOCK(answer, 512);
+	  SH_FREE(answer);
+	  SL_RETURN( (-1), _("sh_forward_try_impl"));
+	}
+      else
+	{
+	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_AUTH);
+	}
+    }    
+
+#else
+
+
+  /* This is the SRP authenticated key exchange protocol.
+   * Produces a session key skey->session.
+   */
+  if (initialized == BAD)
+    {
+      TPT(( 0, FIL__, __LINE__, _("msg=<srp: entry>\n")));
+
+      theProto = SH_PROTO_SRP;
+
+      sl_strlcpy (answer, sh.host.name, 512);
+      flag_err = sh_forward_send    (sockfd, theProto, _("SALT "), 
+				     answer,  sl_strlen(answer)); 
+
+      TPT(( 0, FIL__, __LINE__, _("msg=<srp: sent SALT, flag_err = %d>\n"), 
+	    flag_err));
+
+      if (flag_err == 0)
+	{
+	  flag_err = sh_forward_receive (sockfd, theProto, head_u, 
+					 answer,  511);
+	  flag_err = (flag_err < 0) ? flag_err : 0;
+	  TPT(( 0, FIL__, __LINE__, 
+		_("msg=<srp: rcvt nonce, flag_err = %d>\n"), 
+		flag_err));
+	}
+
+      /* Entry point for jump from message forward if session key must
+       * be re-initialized.
+       */	 
+    initBlock:
+      TPT(( 0, FIL__, __LINE__, _("msg=<srp: INIT>\n")));
+
+      if (flag_err == 0 &&
+	  (0 == check_request (head_u, _("INIT"))))
+	{
+	  if (0 != sh_srp_init())
+	    sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_EBGN);
+	  else /* if (0 == sh_srp_init()) */
+	    {
+	      TPT(( 0, FIL__, __LINE__, _("msg=<srp: bignum initialized>\n")));
+
+	      sh_srp_x (answer, NULL);  /* x        password      */
+	      sh_srp_make_a ();         /* a        random number */
+	      foo_A = sh_srp_A();       /* g^a                    */
+
+	      TPT(( 0, FIL__, __LINE__, _("msg=<srp: A = %s>\n"), foo_A));
+
+	      if (foo_A == NULL)
+		flag_err = (-1);
+
+	      if (flag_err == 0)
+		flag_err = sh_forward_send    (sockfd, 
+					       (theProto|SH_PROTO_SRP), 
+					       _("PC01"),
+					       foo_A, sl_strlen(foo_A)+1); 
+	      if (flag_err == 0)
+		{
+		  flag_err = sh_forward_receive (sockfd, 
+						 (theProto|SH_PROTO_SRP),
+						 head_u,
+						 answer, 511);
+		  flag_err = (flag_err < 0) ? flag_err : 0;
+		  TPT(( 0, FIL__, __LINE__, _("msg=<srp: B = %s>\n"), answer));
+		  TPT(( 0, FIL__, __LINE__, _("msg=<srp: u = %03o-%03o-%03o-%03o>\n"), head_u[0], head_u[1], head_u[2], head_u[3]));
+		}
+
+	      /*                     u        nounce        */
+	      /*                     B        answer        */
+	      /*                     S = (B-g^x)^(a+ux)     */
+	      
+	      if (flag_err == 0)
+		{ 
+		  if (0 != sh_srp_check_zero (answer))
+		    sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_EZERO);
+		  else /* if (0 != sh_srp_check_zero (answer)) */
+		    {
+		      sl_strlcpy(u_real, sh_tiger_hash(head_u, TIGER_DATA, 4), 
+				 SH_CHALLENGE_SIZE);
+		      foo_Sc = sh_srp_S_c (u_real, answer);
+
+		      TPT(( 0, FIL__, __LINE__, _("msg=<srp: U = %s>\n"), 
+			    u_real));
+		      TPT(( 0, FIL__, __LINE__, _("msg=<srp:Sc = %s>\n"), 
+			    foo_Sc));
+
+		      /* --- Now send H(A,B,H(Sc)) and check. --- 
+		       */
+		      if (foo_Sc != NULL)
+			{
+			  sl_strlcpy(foo_M1, 
+				     sh_srp_M(foo_A, 
+					      answer, 
+					      sh_tiger_hash(foo_Sc, 
+							    TIGER_DATA, 
+							    sl_strlen(foo_Sc))),
+				     KEY_LEN+1);
+
+			  TPT(( 0, FIL__, __LINE__, _("msg=<srp:M1 = %s>\n"), 
+				foo_M1));
+
+			  flag_err = sh_forward_send(sockfd, 
+						     (theProto|SH_PROTO_SRP), 
+						     _("PC02"),
+						     foo_M1, KEY_LEN+1);
+			}
+		      else
+			{
+			  flag_err = (-1);
+			}
+
+		      if (flag_err == 0)
+			{
+			  flag_err =sh_forward_receive(sockfd, 
+						       (theProto|SH_PROTO_SRP),
+						       head_u, 
+						       answer, 511);
+			  flag_err = (flag_err < 0) ? flag_err : 0;
+			  TPT(( 0, FIL__, __LINE__, _("msg=<srp: M = %s>\n"), 
+				answer));
+			}
+
+		      if (flag_err == 0   &&
+			  (0 == check_request (head_u, _("PARP"))))
+			{
+			  /* ------  verify M2 = H(A, M1, K) --------
+			   */
+			  M = sh_srp_M (foo_A, foo_M1,
+					sh_tiger_hash(foo_Sc,
+						      TIGER_DATA,
+						      sl_strlen(foo_Sc)));
+			  if (M != NULL && 
+			      0 == sl_strncmp (answer, M, KEY_LEN+1))
+			    {
+			      sl_strlcpy (skey->session, 
+					  sh_tiger_hash(foo_Sc, 
+							TIGER_DATA,
+							sl_strlen(foo_Sc)),
+					  KEY_LEN+1);
+			      TPT(( 0, FIL__, __LINE__, 
+				    _("msg=<srp: Key = %s>\n"), 
+				    skey->session));
+
+#ifdef SH_ENCRYPT
+			      err_num = makeKey(&(skey->keyInstE), 
+						DIR_ENCRYPT, 
+						192, skey->session);
+			      if (err_num < 0)
+				sh_error_handle((-1), FIL__, __LINE__, -1, 
+						MSG_E_SUBGEN,
+						errorExplain(err_num), 
+						_("sh_forward_try_impl: makeKey"));
+			      err_num = makeKey(&(skey->keyInstD), 
+						DIR_DECRYPT, 
+						192, skey->session);
+			      if (err_num < 0)
+				sh_error_handle((-1), FIL__, __LINE__, -1, 
+						MSG_E_SUBGEN,
+						errorExplain(err_num), 
+						_("sh_forward_try_impl: makeKey"));
+#endif
+			      initialized = GOOD;
+			    }
+			}
+		      if (foo_Sc != NULL)
+			SH_FREE(foo_Sc);
+		    }
+		}
+	      if (foo_A != NULL)
+		SH_FREE(foo_A);
+	      sh_srp_exit();
+	    }
+	}
+
+      if (initialized == BAD)
+	{
+	  timeout_val *= 2;
+	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NOAUTH);
+	  memset(answer, '\0', sizeof(answer));
+	  MUNLOCK(answer, 512);
+	  SH_FREE(answer);
+	  SL_RETURN( (-1), _("sh_forward_try_impl"));
+	}
+      else
+	{
+	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_AUTH);
+	}
+    }
+
+#endif
+
+  /* no message, just session key negotiated
+   */
+  if (errmsg == NULL)
+    {
+      timeout_val = 1;
+      memset(answer, 0, sizeof(answer));
+      MUNLOCK(answer, 512);
+      SH_FREE(answer);
+      TPT(( 0, FIL__, __LINE__, _("msg=<No message.>\n")));
+      SL_RETURN( (0), _("sh_forward_try_impl"));
+    }
+  else if (what == (char)SH_PROTO_BIG)
+    {
+      MUNLOCK(answer, 512);
+      SH_FREE (answer);
+      answer   = SH_ALLOC(TRANS_BYTES + 256);
+      MLOCK(answer, TRANS_BYTES + 256);
+      TPT(( 0, FIL__, __LINE__, _("msg=<File transfer.>\n")));
+    }
+
+
+  (void) sl_strlcpy (answer, sh_util_siggen(skey->session,
+					    sh.host.name,
+					    sl_strlen(sh.host.name)), 
+	      KEY_LEN+1);
+  TPT((0, FIL__, __LINE__, _("msg=<host %s>\n"), sh.host.name));
+  TPT((0, FIL__, __LINE__, _("msg=<ckey %s>\n"), skey->session));
+  TPT((0, FIL__, __LINE__, _("msg=<sign %s>\n"), answer));
+
+    
+  (void) sl_strlcat (answer, sh.host.name, 512);
+
+  TPT((0, FIL__, __LINE__, _("msg=<mesg %s>\n"), answer));
+
+  /***********************************************
+   *
+   * send the message
+   *
+   */
+
+  if (what == (char) SH_PROTO_MSG)
+    {
+      theProto = (unsigned char)SH_PROTO_MSG;
+
+      /* say HELO       
+       */
+
+      flag_err = sh_forward_send    (sockfd, 
+				     (char)theProto, _("HELO"),
+				     answer, 
+				     (unsigned long)sl_strlen(answer)); 
+      TPT(( 0, FIL__, __LINE__, _("msg=<Sent %s, status %d.>\n"), 
+	    answer, flag_err));
+      if (flag_err == 0)
+	{ 
+	  /* --- Get challenge. ---  
+	   */
+	  flag_err = (int) sh_forward_receive (sockfd, 
+					       (char)SH_PROTO_MSG, head_u, 
+					       answer, 255);   
+	  TPT(( 0, FIL__, __LINE__, _("msg=<Rcvt %s, u %s, status %d.>\n"), 
+	    answer, hu_trans(head_u), flag_err));
+	  flag_err = (flag_err < 0) ? flag_err : 0;
+	  
+	  if (flag_err == 0)
+	    {
+	      
+	      /* --- Re-negotiate key. ---
+	       */
+	      if (0 == check_request_nerr(head_u, _("INIT")))
+		{
+		  flag_err    = 0;
+		  initialized = BAD;
+		  goto initBlock;
+		}
+	      
+	      else if (0 == check_request(head_u, _("TALK")))
+		{
+
+		  /* --- Save the challenge. ---  
+		   */
+		  (void) sl_strlcpy(nsrv, answer, KEY_LEN + 1);
+		  
+		  /* --- Hash(msg,challenge,sessionkey). ---  
+		   */
+		  len    = sl_strlen(errmsg) + sl_strlen(answer) 
+		    + KEY_LEN + 1;
+		  len = (size_t)((len < 256) ? 256 : len);
+		  buffer = SH_ALLOC(len);
+		  MLOCK(buffer, len);
+		  (void) sl_strlcpy(buffer, errmsg, len);
+		  (void) sl_strlcat(buffer, answer, len);
+		  (void) sl_strlcpy(hash, 
+				    sh_util_siggen (skey->session, 
+						    buffer, 
+						    sl_strlen(buffer)), 
+				    KEY_LEN+1);
+		  TPT((0, FIL__, __LINE__, _("msg=<sign %s.>\n"),
+		       sh_util_siggen(skey->session, buffer, 
+				      sl_strlen(buffer))));  		      
+
+		  (void) sl_strlcpy(buffer, errmsg, len);
+		  (void) sl_strlcat(buffer, hash,   len);
+
+		  flag_err = 
+		    sh_forward_send_crypt (sockfd, 
+#ifdef SH_ENCRYPT
+#ifdef SH_ENCRYPT_2
+					   (char)(SH_PROTO_MSG|SH_PROTO_ENC|SH_PROTO_EN2),
+#else
+					   (char)(SH_PROTO_MSG|SH_PROTO_ENC),
+#endif
+#else
+					   (char)(SH_PROTO_MSG),
+#endif
+					   _("MESG"),
+					   buffer, 
+					   (unsigned long)(sl_strlen(buffer)+1));
+		  TPT(( 0, FIL__, __LINE__, 
+			_("msg=<Sent %s, status %d.>\n"), 
+			answer, flag_err));
+
+		  /* --- Get confirmation. ---
+		   */
+		  if (flag_err == 0)
+		    {
+		      flag_err = (int)
+			sh_forward_receive_crypt (sockfd, 
+#ifdef SH_ENCRYPT
+#ifdef SH_ENCRYPT_2
+						  (char)(SH_PROTO_MSG|SH_PROTO_ENC|SH_PROTO_EN2|SH_PROTO_END),
+#else
+						  (char)(SH_PROTO_MSG|SH_PROTO_ENC|SH_PROTO_END),
+#endif
+#else
+						  (char)(SH_PROTO_MSG|SH_PROTO_END),
+#endif
+						  head_u, 
+						  answer, 255);   
+		      TPT(( 0, FIL__, __LINE__, 
+			    _("msg=<Rcvt %s, u %s, status %d.>\n"), 
+			    answer, hu_trans(head_u), flag_err));
+		      flag_err = (flag_err < 0) ? flag_err : 0;
+		    }
+
+
+		  /* --- Check confirmation. ---
+		   */
+		  if (flag_err == 0)
+		    {
+		      /*   CLIENT CONF RECV
+		       * 
+		       *   first KEY_LEN bytes must be
+		       *   sig(skey->session (errmsg nsrv))
+		       *
+		       */
+		      (void) sl_strlcpy(buffer, errmsg, len);
+		      (void) sl_strlcat(buffer, nsrv,   len);
+		      flag_err = sl_strncmp(answer,
+					    sh_util_siggen(skey->session, 
+							   buffer,
+							   sl_strlen(buffer)),
+					    KEY_LEN);
+		      TPT((0, FIL__, __LINE__, _("msg=<sign %s.>\n"),
+			   sh_util_siggen(skey->session, buffer, 
+					  sl_strlen(buffer))));
+
+		      if (flag_err != 0)
+			{
+#ifdef ENOMSG
+			  flag_err = ENOMSG;
+#else
+			  flag_err = EIO;
+#endif
+			  sh_error_handle((-1), FIL__, __LINE__, flag_err,
+					  MSG_TCP_NOCONF);
+			}
+		      else
+			{
+#ifdef SH_ENCRYPT_2
+			  /* --- SERVER CMD --- */
+			  if (answer[KEY_LEN] != '\0' && 
+			      sl_strlen(answer) > (2*KEY_LEN))
+			    {
+			      pos = sl_strlen(answer) - (2*KEY_LEN);
+			      /*
+			       * buffer is  >= 256
+			       * answer has <= 255 bytes
+			       */
+			      (void) sl_strlcpy(buffer, &answer[KEY_LEN], 
+						pos+1);
+			      flag_err = 
+				sl_strncmp(&answer[KEY_LEN+pos],
+					   sh_util_siggen(skey->session, 
+							  buffer,
+							  pos),
+					   KEY_LEN);
+			      
+			      TPT((0, FIL__, __LINE__, 
+				   _("CONF RECV <%d> <%s>\n"),
+				   flag_err, &answer[KEY_LEN]));
+			      
+			      if (flag_err != 0) {
+				sh_error_handle((-1), FIL__, __LINE__, 
+						flag_err,
+						MSG_TCP_NOCONF);
+			      } else {
+				sh_socket_server_cmd(buffer);
+			      }
+			      flag_err = 0;
+
+			    } else {
+			      
+			      TPT((0, FIL__, __LINE__, 
+				   _("CONF RECV <0> <[null]>\n")));
+			      
+			    }
+			  /* --- SERVER CMD END --- */
+#endif
+			  sh_error_handle((-1), FIL__, __LINE__, 0,
+					  MSG_TCP_CONF);
+			}
+		    }
+
+		  memset(buffer, 0, len);
+		  MUNLOCK(buffer, len);
+		  SH_FREE(buffer);
+		}
+	      else
+		{
+		  /* --- Unexpected reply from server. ---
+		   */
+		  sh_error_handle((-1), FIL__, __LINE__, 0,
+				  MSG_TCP_UNEXP);
+		  flag_err = (-1);
+		}
+	    }
+	}
+    }
+
+
+  else if (what == (char)SH_PROTO_BIG)
+    {
+      theProto = (unsigned char) SH_PROTO_BIG;
+
+      /* --- Say HELO  ---       
+       */
+      flag_err = sh_forward_send    (sockfd, (char) theProto, _("HELO"),
+				     answer, (unsigned long)sl_strlen(answer));
+      TPT(( 0, FIL__, __LINE__, _("msg=<Sent %s, status %d.>\n"), 
+	    answer, flag_err));
+
+      if (flag_err == 0)
+	{ 
+	  /* --- Get NSRV. ---  
+	   */
+	  flag_err = (int) sh_forward_receive (sockfd, 
+					       (char)SH_PROTO_BIG, head_u, 
+					       answer, 255);
+	  TPT(( 0, FIL__, __LINE__, _("msg=<Rcvt %s, u %s, status %d.>\n"), 
+	    answer, hu_trans(head_u), flag_err));
+	  flag_err = (flag_err < 0) ? flag_err : 0;
+	}   
+
+      if (flag_err == 0)
+	{
+	  
+	  /* --- Re-negotiate key. ---
+	   */
+	  if (0 == check_request_nerr(head_u, _("INIT")))
+	    {
+	      flag_err    = 0;
+	      initialized = BAD;
+	      goto initBlock;
+	    }
+	  
+ 
+	  else if (0 == check_request(head_u, _("NSRV")))
+	    {
+#ifdef SH_ENCRYPT
+	      /* --- Set encryption flag. ---
+	       */
+#ifdef SH_ENCRYPT_2
+	      theProto = 
+		(unsigned char)(SH_PROTO_BIG | SH_PROTO_ENC | SH_PROTO_EN2);
+#else
+	      theProto = (unsigned char)(SH_PROTO_BIG | SH_PROTO_ENC);
+#endif
+#endif
+
+	      (void) sl_strlcpy(nsrv, answer, KEY_LEN+1);
+	      
+	      /* --- Generate a nonce. ---
+	       */
+	      ticks = (UINT32) taus_get (&(skey->rng0[0]), 
+					 &(skey->rng1[0]),
+					 &(skey->rng2[0]));
+              
+	      (void) sl_strlcpy(nclt, 
+				sh_tiger_hash((char *) &ticks, 
+					      TIGER_DATA, 
+					      (unsigned long)sizeof(UINT32)),
+				KEY_LEN+1);
+
+	      /* --- Compute H(nsrv, nclt, skey). ---
+	       */
+	      buffer = sh_util_strconcat (nsrv, nclt, 
+					  skey->session, NULL);
+	      (void)sl_strlcpy(foo_M1, 
+			       sh_tiger_hash(buffer, TIGER_DATA,
+					     (unsigned long)sl_strlen(buffer)),
+			       KEY_LEN+1);
+	      memset (buffer, 0, sl_strlen(buffer));
+
+	      /* --- Send (nclt, msg) ---
+	       */
+	      (void) sl_strlcpy(buffer, nclt, KEY_LEN+1);
+	      (void) sl_strlcat(buffer, errmsg, KEY_LEN+5);
+
+#ifndef SH_ENCRYPT
+	      buffer[KEY_LEN+4] = theProto;
+	      buffer[KEY_LEN+5] = '\0';
+	      sh_tools_hash_add(foo_M1, buffer, KEY_LEN+5);
+#endif
+
+	      flag_err = 
+		sh_forward_send_crypt (sockfd, (char) theProto, _("NCLT"),
+				       buffer, 
+				       (unsigned long) sl_strlen(buffer));
+ 
+	      TPT(( 0, FIL__, __LINE__, _("msg=<Sent %s, status %d.>\n"), 
+		    buffer, flag_err));
+	      SH_FREE (buffer);
+	    }   
+	}
+
+      if (flag_err == 0)
+	{
+	  /* --- Receive the file. ---
+	   */
+
+          /* --- Open a temporary file. ---
+           */
+	  
+          if ( (sfd = open_tmp ()) < 0)
+	    {
+	      flag_err = (-1);
+	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_EFIL);
+	    }
+	  else
+	    {
+	      /* --- Read from socket into tmp file. ---
+	       */
+	      transfercount = 0;
+	      flag_err      = 0;
+
+	      do {
+		flag_err = (int)
+		  sh_forward_receive_crypt (sockfd, 
+#ifdef SH_ENCRYPT
+#ifdef SH_ENCRYPT_2
+					    (char)(SH_PROTO_BIG|SH_PROTO_EN2|SH_PROTO_ENC),
+#else
+					    (char)(SH_PROTO_BIG|SH_PROTO_ENC),
+#endif
+#else
+					    (char)(SH_PROTO_BIG),
+#endif
+					    head_u, 
+					    answer, 
+					    TRANS_BYTES + 256);
+
+		TPT(( 0, FIL__, __LINE__, 
+		      _("msg=<Received: %d bytes, marked %s.>\n"),
+		      flag_err, hu_trans(head_u)));
+
+		if (flag_err > 0 && 0 == check_request_nerr(head_u, _("FILE")))
+		  {
+		    if (0 == hash_check (foo_M1, answer, flag_err))
+		      {
+			(void) sl_write(sfd, &answer[KEY_LEN], 
+					flag_err-KEY_LEN);
+			++transfercount;
+			flag_err = 
+			  sh_forward_send_crypt (sockfd, (char) theProto, 
+						 _("RECV"),
+						 nclt, 
+						 (unsigned long)sl_strlen(nclt));
+
+		      }
+		    else
+		      {
+			TPT(( 0, FIL__, __LINE__, 
+			      _("msg=<File transfer: Hash check failed.>\n")));
+			break;
+		      }
+		  }
+		else
+		  {
+		    TPT(( 0, FIL__, __LINE__, 
+			  _("msg=<File transfer: No more data.>\n")));
+		    break;
+		  }
+	      } while (transfercount < 32000); /* 64 Mbyte */
+		    
+	      if (0 == check_request_nerr(head_u, _("EEOT")) &&
+		  0 <  flag_err                             &&
+		  0 == hash_check (foo_M1, answer, (int)strlen(answer)))
+		{
+		  flag_err = 
+		    sh_forward_send_crypt (sockfd, (char) theProto, 
+					   _("EOTE"),
+					   nclt, 
+					   (unsigned int) sl_strlen(nclt));
+
+		  (void) rewind_tmp (sfd);
+		  (void) sl_sync(sfd);
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FOK);
+		}
+	      else
+		{
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FBAD);
+		  (void) sl_close (sfd);
+		  sfd = (-1);
+		}
+
+	      (void) close (sockfd);
+	      memset(answer, 0, TRANS_BYTES + 256);
+	      MUNLOCK(answer, TRANS_BYTES + 256);
+	      SH_FREE(answer);
+	      timeout_val = 1;
+
+	      SL_RETURN( (sfd), _("sh_forward_try_impl"));
+	    }
+	}
+
+      (void) close (sockfd);
+      memset(answer, 0, TRANS_BYTES + 256);
+      MUNLOCK(answer, TRANS_BYTES + 256);
+      SH_FREE(answer);
+      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FBAD);
+      timeout_val *= 2;
+
+      SL_RETURN( (-1), _("sh_forward_try_impl"));
+    }
+		  
+      
+ 
+  (void) close (sockfd);
+  memset(answer, 0, 512);
+  MUNLOCK(answer, 512);
+  SH_FREE(answer);
+
+#ifndef EIO
+#define EIO 5
+#endif
+  
+
+#ifdef SH_ERROR_H  
+  if (flag_err != 0)
+    {
+      conn_state = BAD;
+      timeout_val *= 2;
+      if (flag_err < 0 || NULL == sh_error_message(flag_err))
+	flag_err = EIO;
+      sh_error_handle((-1), FIL__, __LINE__, flag_err, MSG_TCP_ECONN,
+		      sh_error_message(flag_err));
+      SL_RETURN( (-1), _("sh_forward_try_impl"));
+    }
+#endif
+  timeout_val = 1;
+
+  SL_RETURN( (0), _("sh_forward_try_impl"));
+}
+
+/* #ifdef SH_WITH_CLIENT */
+#endif
+
+
+#if defined (SH_WITH_SERVER)
+
+#ifndef USE_SRP_PROTOCOL
+
+int sh_forward_make_client (char * str)
+{
+  /* char *          safer; */
+  char            key[KEY_LEN+1];
+  unsigned char   in[PW_LEN+1];
+  int    i = 0, j, k, l = 0;
+  
+  if (sl_strlen(str) != (PW_LEN * 2)) 
+    {
+      fprintf(stderr, 
+	      _("Input must be a %d digit hexadecimal number"\
+		" (only 0-9, a-f, A-F allowed in input)\n"),
+	      (PW_LEN * 2));
+      _exit(EXIT_FAILURE);
+    }
+  
+  while (i < (PW_LEN * 2))
+    {
+      k = sh_util_hexchar(str[i]); j = sh_util_hexchar(str[i+1]); 
+      if (k != -1 && j != -1) 
+        {
+          in[l] = (k * 16 + j);
+          ++l; i+= 2;
+        }
+      else
+        {
+          fprintf(stderr, _("Invalid char %c\n"), str[i]);
+          _exit(EXIT_FAILURE);
+        }
+    }
+  in[PW_LEN] = '\0';
+
+  sl_strlcpy ((char *)key, 
+	      sh_tiger_hash ((char*)in, TIGER_DATA, PW_LEN), 
+	      KEY_LEN+1);
+  key[KEY_LEN] = '\0';
+  
+  fprintf(stdout, _("Client entry: Client=HOSTNAME@00000000@%s\n"), 
+	  key);
+  fflush(stdout);
+
+  _exit(EXIT_SUCCESS);
+  return 0;
+}
+
+#else
+
+int sh_forward_make_client (char * str)
+{
+  char * foo_v;
+
+  char   salt[17];
+  char   key[KEY_LEN+1];
+  char   in[PW_LEN];
+  int    i = 0, j, k, l = 0;
+  
+  if (sl_strlen(str) != (PW_LEN*2)) 
+    {
+      fprintf(stderr, 
+	      _("Input must be a %d digit hexadecimal number"\
+		" (only 0-9, a-f, A-F allowed in input)\n"),
+	      (PW_LEN*2));
+      _exit(EXIT_FAILURE);
+    }
+
+    while (i < (PW_LEN*2))
+      {
+        k = sh_util_hexchar(str[i]); j = sh_util_hexchar(str[i+1]); 
+        if (k != -1 && j != -1) 
+          {
+            in[l] = (k * 16 + j);
+            ++l; i+= 2;
+          }
+        else
+          {
+            fprintf(stderr, _("Invalid char %c\n"), str[i]);
+            _exit(EXIT_FAILURE);
+          }
+      }
+    
+  
+    if (0 == sh_srp_init())
+      {
+	sh_util_keyinit(key, KEY_LEN);
+	sl_strlcpy(salt, sh_tiger_hash(key, TIGER_DATA, KEY_LEN), 17); 
+	sh_srp_x (salt, in);
+	foo_v  = sh_srp_verifier ();
+	fprintf(stdout, _("Client=HOSTNAME@%s@%s\n"), 
+		salt, foo_v);
+	fflush(stdout);
+	SH_FREE(foo_v);
+	sh_srp_exit();
+	_exit(EXIT_SUCCESS);
+      }
+    fprintf(stdout, _("ERROR initializing BigNum library.\n"));
+    fflush (stdout);
+    _exit(EXIT_FAILURE);
+    return -1;
+}
+#endif
+
+
+int sh_forward_create_password (char * dummy)
+{
+  UINT32   val[2]; 
+  char     output[KEY_LEN+1];
+
+  val[0] = taus_get (&(skey->rng0[0]), &(skey->rng0[1]), &(skey->rng0[2]));
+  val[1] = taus_get (&(skey->rng0[0]), &(skey->rng0[1]), &(skey->rng0[2]));
+
+  sl_strlcpy (output, 
+	      sh_tiger_hash((char *)(&val[0]), TIGER_DATA, 2*sizeof(UINT32)),
+	      KEY_LEN);
+
+  output[16] = '\0';
+
+  fprintf(stdout, _("%s\n"), output);
+  fflush (stdout);
+
+  if (dummy)
+    _exit(EXIT_SUCCESS);
+  else
+    _exit(EXIT_SUCCESS);  
+  return (0);  /* avoid compiler warning */
+}
+
+/* #if defined (SH_WITH_SERVER) */
+#endif
+
+/**************************************************
+ *
+ *
+ *  S E R V E R   
+ *
+ *
+ ***************************************************/
+
+#ifdef SH_WITH_SERVER
+
+#include "sh_readconf.h"
+
+
+#define CONN_FREE    0
+#define CONN_READING 1
+#define CONN_SENDING 2
+#define CONN_PAUSE   3
+#define CONN_BUSY    4
+
+char * clt_stat[] = {
+  N_("Inactive"),
+  N_("Started"),
+  N_("ILLEGAL"),
+  N_("FAILED"),
+  N_("Exited"),
+  N_("PANIC"),
+  N_("POLICY"),
+  N_("File_transfer"),
+  N_("Message"),
+  N_("TIMEOUT_EXCEEDED"),
+  N_("Suspended"),
+  N_("Filecheck"),
+};
+
+#include <time.h>
+
+/* in sh_html.h:
+ *  typedef struct client_entry {
+ *  } client_t;
+ */
+
+#include "zAVLTree.h"
+
+/* Function to return the key for indexing
+ * the argument 
+ */
+zAVLKey sh_avl_key (void const * arg)
+{
+  const client_t * sa = (const client_t *) arg;
+  return (zAVLKey) sa->hostname;
+}
+
+zAVLTree * all_clients = NULL;
+
+void sh_forward_html_write()
+{
+  SL_ENTER(_("sh_forward_html_write"));
+  sh_html_write(all_clients);
+  SL_RET0(_("sh_forward_html_write"));
+}
+
+
+int sh_forward_use_clt_class (char * c)
+{
+  int i;
+  SL_ENTER(_("sh_forward_use_clt_class"));
+  i = sh_util_flagval(c, &(sh.flag.client_class));
+  SL_RETURN(i, _("sh_forward_use_clt_class"));
+}
+
+int sh_forward_use_clt_sev (char * c)
+{
+  int i;
+  SL_ENTER(_("sh_forward_use_clt_sev"));
+  i = sh_util_flagval(c, &(sh.flag.client_severity));
+  SL_RETURN(i, _("sh_forward_use_clt_sev"));  
+}
+
+
+/* the destructor
+ */
+void free_client(void * inptr)
+{
+  client_t * here;
+
+  SL_ENTER(_("free_client"));
+  if (inptr == NULL)
+    SL_RET0(_("free_client"));
+  else
+    here = (client_t *) inptr;
+
+  if (here->hostname != NULL)
+    SH_FREE(here->hostname);
+  if (here->salt != NULL)
+    SH_FREE(here->salt);
+  if (here->verifier != NULL)
+    SH_FREE(here->verifier);
+  SH_FREE(here);
+  SL_RET0(_("free_client"));
+}
+
+int sh_forward_register_client (char * str)
+{
+  client_t   * newclt;
+  client_t   * testclt;
+
+  char       * ptr;
+  int          sepnum = 0;
+  int          sep[2];
+  register int i = 0;
+  int          siz_str = 0;
+
+  SL_ENTER(_("sh_forward_register_client"));
+
+  ptr = str; 
+  while (*ptr) {
+    if (*ptr == '@' && sepnum < 2 ) 
+      { 
+	sep[sepnum] = i;
+	++sepnum;
+      } 
+    ++ptr; ++i; 
+  }
+
+  if (all_clients == NULL)
+    {
+      all_clients = zAVLAllocTree (sh_avl_key);
+      if (all_clients == NULL) 
+	{
+	  (void) safe_logger (0, 0, getpid());
+	  aud__exit(FIL__, __LINE__, EXIT_FAILURE);
+	}
+    }
+  
+  if (sepnum == 2 && sep[0] > 0)
+    {
+      newclt = SH_ALLOC (sizeof(client_t));
+      newclt->hostname = SH_ALLOC (sep[0]+1);
+      newclt->salt     = SH_ALLOC (sep[1]-sep[0]);
+      newclt->verifier = SH_ALLOC (sl_strlen(str)-sep[1]+1);
+      newclt->exit_flag         = 0;
+      newclt->dead_flag         = 0;
+#ifdef SH_ENCRYPT
+#ifdef SH_ENCRYPT_2
+      newclt->encf_flag         = SH_PROTO_ENC|SH_PROTO_EN2;
+      newclt->ency_flag         = SH_PROTO_ENC|SH_PROTO_EN2;
+#else
+      newclt->encf_flag         = SH_PROTO_ENC;
+      newclt->ency_flag         = SH_PROTO_ENC;
+#endif
+#else
+      newclt->encf_flag         = 0;
+      newclt->ency_flag         = 0;
+#endif
+      newclt->session_key[0]    = '\0';
+      newclt->last_connect      = (time_t) 0;
+      newclt->session_key_timer = (time_t) 0;
+      newclt->status_now        = CLT_INACTIVE;
+      for (i = 0; i < CLT_MAX; ++i) 
+	newclt->status_arr[i] = CLT_INACTIVE;
+      sl_strlcpy(newclt->timestamp[CLT_INACTIVE],   sh_unix_time(0), TIM_MAX);
+      sl_strlcpy(newclt->hostname,  &str[0],        sep[0]+1);
+      sl_strlcpy(newclt->salt,      &str[sep[0]+1], sep[1]-sep[0]);
+      sl_strlcpy(newclt->verifier,  &str[sep[1]+1], sl_strlen(str)-sep[1]+1);
+
+      testclt = (client_t *) zAVLSearch (all_clients, newclt->hostname);
+
+      if (testclt != NULL)
+	{
+	  SH_FREE(testclt->verifier);
+	  siz_str = strlen (newclt->verifier) + 1;
+	  testclt->verifier = SH_ALLOC (siz_str);
+	  sl_strlcpy(testclt->verifier, newclt->verifier, siz_str);
+
+	  SH_FREE(testclt->salt);
+	  siz_str = strlen (newclt->salt) + 1;
+	  testclt->salt = SH_ALLOC (siz_str);
+	  sl_strlcpy(testclt->salt, newclt->salt, siz_str);
+
+	  testclt->dead_flag = 0;
+	      
+	  free_client(newclt);
+	  SL_RETURN( 0, _("sh_forward_register_client"));
+	}
+      else
+	{
+	  if (0 == zAVLInsert (all_clients, newclt))
+	    {
+	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_CREG,
+			      newclt->hostname, 
+			      newclt->salt, newclt->verifier);
+	      SL_RETURN( 0, _("sh_forward_register_client"));
+	    }
+	}
+    }
+  SL_RETURN (-1, _("sh_forward_register_client"));
+}
+
+typedef struct {
+  int             state;
+  int             fd;
+  char          * buf;
+  unsigned char   head[SH_HEADER_SIZE];
+  char            challenge[SH_CHALLENGE_SIZE];
+  char            peer[SH_MINIBUF+1];
+  client_t      * client_entry;
+  char          * K;
+  char          * M1;
+  char          * A;
+  int             headcount;
+  unsigned long   bytecount;
+  unsigned long   bytes_to_send;
+  unsigned long   bytes_to_get;
+  int             pass;
+  unsigned long   timer;
+
+  char          * FileName;
+  unsigned long   FileLength;
+  unsigned long   FileSent;
+  char            FileType[5];
+
+  struct sockaddr_in addr_peer;
+} sh_conn_t;
+
+
+
+void sh_forward_do_free (sh_conn_t * conn)
+{
+  SL_ENTER(_("sh_forward_do_free"));
+
+  if (conn->K != NULL) 
+    {
+      SH_FREE(conn->K);
+      conn->K           = NULL;
+    }
+  if (conn->A != NULL) 
+    {
+      SH_FREE(conn->A);
+      conn->A           = NULL;
+    }
+  if (conn->M1 != NULL) 
+    {
+      SH_FREE(conn->M1);
+      conn->M1           = NULL;
+    }
+  if (conn->buf != NULL) 
+    {
+      SH_FREE(conn->buf);
+      conn->buf          = NULL;
+    }
+  if (conn->fd != (-1))
+    {
+      close (conn->fd);
+      conn->fd            = -1;
+    }
+  memset(conn->challenge, '\0', SH_CHALLENGE_SIZE);
+  conn->state         = CONN_FREE;
+  conn->headcount     = 0;
+  conn->bytecount     = 0;
+  conn->bytes_to_send = 0;
+  conn->bytes_to_get  = 0;
+  conn->pass          = 0;
+  conn->timer         = 0;
+  conn->client_entry  = NULL;
+
+  if (conn->FileName != NULL) 
+    {
+      SH_FREE(conn->FileName);
+      conn->FileName     = NULL;
+    }
+  conn->FileLength     = 0;
+  conn->FileSent       = 0;
+  memset(conn->FileType, '\0', 5);
+
+  --server_status.conn_open;
+  
+  SL_RET0(_("sh_forward_do_free"));
+}
+
+/****************************************
+ *
+ *   -- Reconfiguration. --
+ *
+ *   (1) Mark all clients as 'dead'.
+ *   (2) Reload configuration - clients
+ *       in config are non-dead now.
+ *   (3) Remove all clients still
+ *       marked as 'dead'.
+ */
+
+/* -- Mark all clients as dead.
+ */
+void sh_forward_mark_dead ()
+{
+  zAVLCursor avlcursor;
+  client_t * item;
+
+  SL_ENTER(_("sh_forward_mark_dead"));
+
+  for (item = (client_t *) zAVLFirst(&avlcursor, all_clients); item;
+       item = (client_t *) zAVLNext(&avlcursor))
+    {
+      item->dead_flag = 1;
+    }
+  SL_RET0(_("sh_forward_mark_dead"));
+}
+
+
+/* -- Clean tree from dead clients.
+ */
+void sh_forward_clean_tree ()
+{
+  zAVLCursor avlcursor;
+  client_t * item;
+
+  SL_ENTER(_("sh_forward_clean_tree"));
+
+ repeat_search:
+
+  for (item = (client_t *) zAVLFirst(&avlcursor, all_clients); item;
+       item = (client_t *) zAVLNext(&avlcursor))
+    {
+      if (item->dead_flag == 1)
+	{
+	  zAVLDelete (all_clients, item->hostname);
+	  free_client (item);
+	  goto repeat_search;
+	}
+    }
+  SL_RET0(_("sh_forward_clean_tree"));
+}
+
+/*
+ *
+ **********************************************/
+
+
+
+/* -- SERVER SEND FUNKTION. --
+ */
+void sh_forward_prep_send_int (sh_conn_t * conn, 
+			       char * msg, unsigned long length,
+			       char * u, char protocol,
+			       int docrypt)
+{
+  register unsigned long i;
+  unsigned long           length2;
+
+#ifdef SH_ENCRYPT
+  unsigned long           blkfac = 0;
+  int                     rem = 0;
+  char                  * p, * q;
+  RIJ_BYTE                    inBlock[B_SIZ]; 
+  RIJ_BYTE                    outBlock[B_SIZ];
+  unsigned int            j;
+  cipherInstance          cipherInst;
+  int                     err_num;
+#else
+  (void) docrypt;
+#endif
+
+  SL_ENTER(_("sh_forward_prep_send_int"));
+
+  TPT((0, FIL__, __LINE__, _("msg=<%s>, docrypt=<%d>\n"), msg, docrypt ));
+
+#ifdef SH_ENCRYPT
+  if      ((S_TRUE == docrypt) && ((protocol & SH_PROTO_EN2) != 0) )
+    {
+      length2 = length;
+    }
+  else if ((S_TRUE == docrypt) && ((protocol & SH_PROTO_ENC) != 0) )
+    {
+      blkfac  = length/B_SIZ;
+      rem     = length - (B_SIZ * blkfac);
+      length2 = (B_SIZ * blkfac) + ((rem == 0) ? 0 : B_SIZ);
+    }
+  else
+    {
+      length2 = length;
+    }
+#else
+  length2 = length;
+#endif
+
+  conn->headcount     = 0;
+  conn->bytecount     = 0;
+  conn->bytes_to_send = 0;
+  conn->bytes_to_get  = 0;
+
+  if (conn->buf != NULL) 
+    {
+      SH_FREE(conn->buf);
+      conn->buf           = NULL;
+    }
+
+
+  put_header (conn->head, protocol, &length2, u);
+  SH_SHOWPROT(conn->head,'>');
+
+  TPT((0, FIL__, __LINE__, _("msg=<put_header done>\n") ));
+
+  if (msg == NULL) 
+    length2 = 0;
+  
+#ifdef SH_ENCRYPT
+  if      ((S_TRUE == docrypt) && ((protocol & SH_PROTO_EN2) != 0))
+    {
+      TPT((0, FIL__, __LINE__, _("encrypting (version 2)\n")));
+      
+      conn->buf = sh_tools_makePack (conn->head, msg, length2,
+				     &(conn->client_entry->keyInstE));
+    }
+  else if ((S_TRUE == docrypt) && ((protocol & SH_PROTO_ENC) != 0))
+    {
+      conn->buf       = SH_ALLOC(length2 + 1);
+
+      p       = msg;
+      q       = conn->buf;
+
+      TPT((0, FIL__, __LINE__, _("encrypting (version 1)\n")));
+
+      err_num = cipherInit (&cipherInst, MODE_CBC, NULL);
+      if (err_num < 0)
+	sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
+			errorExplain(err_num), 
+			_("sh_forward_prep_send_int: cipherInit"));
+
+      for (j = 0; j < blkfac; ++j)
+	{
+	  memcpy(inBlock, p, B_SIZ);
+	  err_num = blockEncrypt(&cipherInst, &(conn->client_entry->keyInstE), 
+		       inBlock, 128 * BNUM, outBlock);
+	  if (err_num < 0)
+	    sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
+			    errorExplain(err_num), 
+			    _("sh_forward_prep_send_int: blockEncrypt"));
+	  memcpy(q, outBlock, B_SIZ);
+	  p += B_SIZ;
+	  q += B_SIZ;
+	}
+      if (rem > 0)
+	{
+	  /* incomplete block at end
+	   */
+	  memset(inBlock, '\0', B_SIZ);
+	  memcpy(inBlock, p, rem);
+	  err_num = blockEncrypt(&cipherInst, &(conn->client_entry->keyInstE), 
+		       inBlock, 128 * BNUM, outBlock);
+	  if (err_num < 0)
+	    sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
+			    errorExplain(err_num), 
+			    _("sh_forward_prep_send_int: blockEncrypt"));
+	  memcpy(q, outBlock, B_SIZ);
+	  q += B_SIZ;
+	}
+
+      TPT((0, FIL__, __LINE__, _("msg=<encryption done>\n") ));
+    }
+  else
+    {
+      conn->buf       = SH_ALLOC(length2 + 1);
+
+      for (i = 0; i < length2; ++i) 
+	conn->buf[i] = msg[i];
+      conn->buf[length2] = '\0';
+      TPT((0, FIL__, __LINE__, _("msg=<no encryption done>\n") ));
+    }
+#else
+  conn->buf       = SH_ALLOC(length2 + 1);
+
+  for (i = 0; i < length; ++i) 
+    conn->buf[i] = msg[i];
+  conn->buf[length2] = '\0';
+  TPT((0, FIL__, __LINE__, _("msg=<no encryption done>\n") ));
+#endif
+
+  conn->state     = CONN_SENDING;
+  SL_RET0(_("sh_forward_prep_send_int"));
+}
+
+/* -- Send/Receive. --
+ */
+void sh_forward_prep_send (sh_conn_t * conn, 
+			   char * msg, unsigned long length,
+			   char * u, char protocol)
+{
+  SL_ENTER(_("sh_forward_prep_send"));
+  sh_forward_prep_send_int (conn,  msg, length, u, protocol, S_FALSE);
+  SL_RET0(_("sh_forward_prep_send"));
+}  
+
+void sh_forward_prep_send_crypt (sh_conn_t * conn, 
+				 char * msg, unsigned long length,
+				 char * u, char protocol)
+{
+  SL_ENTER(_("sh_forward_prep_send_crypt"));
+  sh_forward_prep_send_int (conn,  msg, length, u, protocol, S_TRUE);
+  SL_RET0(_("sh_forward_prep_send_crypt"));
+}  
+
+/* #include <sys/times.h> */
+
+#if defined(WITH_EXTERNAL)
+#include "sh_extern.h"
+#endif
+
+/* -- Update the client status. --
+ *
+ * Update the status array for the client,
+ * and eventually call external program.
+ */
+static void status_update (client_t * conn, int status)
+{ 
+#if defined(WITH_EXTERNAL)
+  char msg[2 * SH_MINIBUF + TIM_MAX + 3];
+#endif
+
+  SL_ENTER(_("status_update"));
+
+  if (conn == NULL || 
+      status < 0   || status >= CLT_MAX)
+    SL_RET0(_("status_update"));
+
+  conn->status_now = status;
+  conn->status_arr[status] = status;
+  sl_strlcpy(conn->timestamp[status],
+	     sh_unix_time(0), 
+	     TIM_MAX);
+
+#if defined(WITH_EXTERNAL)
+  sprintf(msg, _("%s %s %s"),                       /* known to fit  */
+	  conn->hostname, 
+	  conn->timestamp[status],
+	  _(clt_stat[status]));
+  sh_ext_execute('s', 'r', 'v', msg, 0);
+#endif
+
+  SL_RET0(_("status_update"));
+}
+
+static time_t time_client_limit = 86400;
+
+int sh_forward_set_time_limit (char * c)
+{
+  long val;
+
+  SL_ENTER(_("sh_forward_set_time_limit"));
+
+  val = strtol (c, (char **)NULL, 10);
+  if (val <= 0)
+    SL_RETURN( (-1), _("sh_forward_set_time_limit"));
+
+  val = (val < 0 ? 0 : val);
+
+  time_client_limit = (time_t) val;
+  SL_RETURN( (0), _("sh_forward_set_time_limit"));
+}
+
+
+/* -- Check for time limit exceeded. --
+ */
+static int client_time_check()
+{
+  zAVLCursor avlcursor;
+  client_t * item;
+
+  SL_ENTER(_("client_time_check"));
+
+  if (time_client_limit == (time_t) 0)
+    SL_RETURN( 0, _("client_time_check"));
+
+  for (item = (client_t *) zAVLFirst(&avlcursor, all_clients); item;
+       item = (client_t *) zAVLNext(&avlcursor))
+    {
+      if (item->exit_flag == 0 && item->last_connect != (time_t) 0)
+	{
+	  if ( (time(NULL) - item->last_connect) > time_client_limit)
+	    {
+	      if (item->status_now != CLT_TOOLONG)
+		{
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_TIMEXC,
+				  item->hostname);
+		  status_update (item, CLT_TOOLONG);
+		}
+	    }
+	}
+    }
+  SL_RETURN( 0, _("client_time_check"));
+}
+
+static int lookup_err = SH_ERR_SEVERE;
+
+int sh_forward_lookup_level (char * c)
+{
+  int ci =  sh_error_convert_level (c);
+
+  SL_ENTER(_("sh_forward_lookup_level"));
+
+  if (ci >= 0)
+    {
+      lookup_err = ci;
+      SL_RETURN( 0, _("sh_forward_lookup_level"));
+    }
+  else
+    SL_RETURN( (-1), _("sh_forward_lookup_level"));
+}
+
+#ifndef MAXHOSTNAMELEN
+#define MAXHOSTNAMELEN  127
+#endif
+
+int check_addr (const char * claim, struct sockaddr_in addr_peer)
+{
+  char               h_name[MAXHOSTNAMELEN + 1];
+  char               h_peer[MAXHOSTNAMELEN + 1];
+  char               h_peer_IP[16];
+  char               tmp_peer_IP[16];
+  struct hostent   * he;
+  char            ** p = NULL;
+  int                i;
+  int                flag = 0;
+
+  SL_ENTER(_("check_addr"));
+
+  if (claim == NULL)
+    {
+      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
+		      _("NULL input"), _("check_addr"));
+      SL_RETURN ((-1), _("check_addr"));
+    }
+
+  /* Make sure we have the canonical name for the client
+   */
+  he = sh_gethostbyname (claim);
+
+  if (he != NULL && he->h_name != NULL)
+    {
+      if (NULL == strchr(he->h_name, '.') && he->h_addr_list != NULL)
+	{
+	  he = sh_gethostbyaddr(he->h_addr_list[0], 
+				he->h_length, 
+				he->h_addrtype);
+	}
+    }
+
+  /* copy canonical name into h_name
+   */
+  if (he != NULL && he->h_name != NULL)
+    sl_strlcpy(h_name, he->h_name, MAXHOSTNAMELEN + 1);
+  else
+    {
+      sh_error_handle(lookup_err, FIL__, __LINE__, 0, MSG_TCP_RESCLT,
+		      claim);
+      SL_RETURN ((0), _("check_addr"));
+    }
+
+
+  /* get canonical name of socket peer
+   */
+  he = sh_gethostbyaddr ((char *) &(addr_peer.sin_addr), 
+			 sizeof(addr_peer.sin_addr),
+			 AF_INET);
+
+  if (he != NULL && he->h_name != NULL)
+    {
+      if (0 == sl_strcmp(he->h_name, _("localhost")))
+	sl_strlcpy(h_peer, sh.host.name, MAXHOSTNAMELEN + 1);
+      else
+	sl_strlcpy(h_peer, he->h_name, MAXHOSTNAMELEN + 1);
+    }
+  else
+    {
+      sl_strlcpy(tmp_peer_IP,
+		 inet_ntoa (*(struct in_addr *) &(addr_peer.sin_addr)),
+		 16);
+      sh_error_handle(lookup_err, FIL__, __LINE__, 0, MSG_TCP_RESPEER,
+		      claim, tmp_peer_IP);
+      SL_RETURN ((0), _("check_addr"));
+    }
+
+  sl_strlcpy(h_peer_IP, 
+	     inet_ntoa (*(struct in_addr *) he->h_addr),
+	     16);
+
+#if 0
+  if (S_FALSE == DoReverseLookup)
+    {
+      SL_RETURN ((0), _("check_addr"));
+    }
+#endif
+
+  /* reverse lookup
+   */
+  if (0 != sl_strncmp(_("127."), 
+		      inet_ntoa (*(struct in_addr *) &(addr_peer.sin_addr)),
+		      4))
+    {
+      he = sh_gethostbyname(h_peer);
+      
+      if (he != NULL)
+	{
+	  for (p = he->h_addr_list; *p; ++p)
+	    {
+	      if (0 == memcmp (*p, &(addr_peer.sin_addr), 
+			       sizeof(addr_peer.sin_addr))) 
+		break;
+	      ++i;
+	    }
+	}
+      if (he == NULL || *p == NULL) 
+	{
+	  sl_strlcpy(tmp_peer_IP,
+		     inet_ntoa (*(struct in_addr *) &(addr_peer.sin_addr)),
+		     16);
+	  sh_error_handle(lookup_err, FIL__, __LINE__, 0, MSG_TCP_LOOKERS,
+			  claim, h_peer, tmp_peer_IP);
+	  SL_RETURN ((0), _("check_addr"));
+	}
+    }
+
+
+  if ((0 == sl_strcmp(h_peer, h_name)) || (0 == sl_strcmp(h_peer_IP, h_name)))
+    {
+      SL_RETURN ((0), _("check_addr"));
+    }
+  else
+    {
+      i = 0;
+      while (he->h_aliases[i] != NULL)
+	{
+	  if (0 == sl_strcmp(he->h_aliases[i], h_name))
+	    {
+	      flag = 1;
+	      break;
+	    }
+	  ++i;
+	}
+      if (flag == 0) 
+	sh_error_handle(lookup_err, FIL__, __LINE__, 0, MSG_TCP_LOOKUP,
+			claim, h_peer);
+    }
+
+  SL_RETURN ((0), _("check_addr"));
+}
+
+static int UseSocketPeer = S_FALSE;
+
+int set_socket_peer (char * c)
+{
+  return sh_util_flagval(c, &UseSocketPeer);
+}
+
+
+/* -- Search register. --
+ */
+client_t * search_register(sh_conn_t * conn, int pos)
+{
+  client_t * this_client;
+  char       peer_ip[16];
+  char       peer_name[MAXHOSTNAMELEN+1];
+  char     * search_string;
+  struct sockaddr_in peer_addr;
+  struct hostent   * he;
+  char    ** p = NULL;
+
+  SL_ENTER(_("search_register"));
+
+  if (UseSocketPeer == S_TRUE)
+    {
+      peer_addr = conn->addr_peer;
+      sl_strlcpy(peer_ip, 
+		 inet_ntoa (*(struct in_addr *) &(peer_addr.sin_addr)), 16);
+
+      /* get canonical name of socket peer
+       */
+      he = sh_gethostbyaddr ((char *) &(peer_addr.sin_addr), 
+			     sizeof(peer_addr.sin_addr),
+			     AF_INET);
+
+      if (he != NULL && he->h_name != NULL)
+	{
+	  if (0 == sl_strcmp(he->h_name, _("localhost")))
+	    sl_strlcpy(peer_name, sh.host.name, MAXHOSTNAMELEN + 1);
+	  else
+	    sl_strlcpy(peer_name, he->h_name, MAXHOSTNAMELEN + 1);
+
+	  /* Reverse lookup 
+	   */
+	  if (0 != sl_strncmp(peer_ip, _("127."), 4))
+	    {
+	      he = sh_gethostbyname(peer_name);
+	      
+	      if (he != NULL)
+	      {
+		for (p = he->h_addr_list; *p; ++p)
+		  {
+		    if (0 == memcmp (*p, &(peer_addr.sin_addr), 
+				     sizeof(peer_addr.sin_addr))) 
+		      break;
+		  }
+	      }
+	      if (he == NULL || *p == NULL) 
+	      {
+		/*
+		sh_error_handle(lookup_err, FIL__, __LINE__, 0, 
+				MSG_TCP_LOOKERS,
+				conn->buf[pos], peer_name, peer_ip);
+		*/
+		sl_strlcpy(peer_name, peer_ip, MAXHOSTNAMELEN + 1);
+	      }
+	    }
+	}
+      else
+	{
+	  sl_strlcpy(peer_name, peer_ip, MAXHOSTNAMELEN + 1);
+	}
+      search_string = peer_name;
+    }
+  else
+    {
+      search_string = &(conn->buf[pos]);
+
+      if (0 != check_addr (search_string, conn->addr_peer))
+	{
+	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
+			  _("Reverse lookup failed"), search_string);
+	  sh_forward_do_free (conn);
+	  SL_RETURN( NULL, _("search_register"));
+	} 
+    }
+
+  /* ----  search the register  -----
+   */
+  this_client = zAVLSearch(all_clients, search_string);
+
+  if (this_client == NULL)
+    {
+      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
+		      _("Not in client list"), search_string);
+      sh_forward_do_free (conn);
+      SL_RETURN( NULL, _("search_register"));
+    } 
+  if (this_client->exit_flag == 1)
+    {
+      TPT((0, FIL__, __LINE__, _("msg=<this_client->exit_flag == 1>\n")));
+      this_client->session_key_timer = (time_t) 0;
+      this_client->session_key[0]    = '\0';
+      this_client->exit_flag         = 0;    
+    }
+  TPT((0, FIL__, __LINE__, _("msg=<search_register: client %s>\n"), 
+       this_client->hostname));
+  TPT((0, FIL__, __LINE__, _("msg=<search_register: key %s>\n"), 
+       this_client->session_key));
+  SL_RETURN( this_client, _("search_register"));
+}  
+
+
+/************************************************************************
+ *
+ * Here we check the message received, and decide on the answer to send
+ * (if any). The connection is in CONN_PAUSED state, thus we must:
+ * (i)   define the proper reaction
+ * (ii)  reset to CONN_READING or CONN_WRITING or CONN_FREE
+ * (iii) eventually reset the connection entry
+ *
+ *************************************************************************/
+static
+void check_protocol(sh_conn_t * conn, int state)
+{
+  client_t * this_client; 
+
+  char     * cmd;
+
+  char       hash[SH_MAXMSGLEN + KEY_LEN + KEY_LEN + 1];
+  char     * buffer;
+  long       len;
+
+  int        clt_sev;
+  char     * ptok;
+
+  UINT32     ticks;
+  int        i;
+  char     * test;
+  char       u[5] = "OOOO";
+
+  SL_TICKET  sfd = -1;
+  char     * read_buf = 0;
+  char     * send_buf;
+  int        bytes;
+
+#ifdef SH_ENCRYPT
+  int        blkfac;
+  int        rem;
+  int        send_bytes;
+  int        err_num;
+#endif
+
+
+#ifdef USE_SRP_PROTOCOL
+  char     * foo_B;
+  char     * foo_Ss;
+#endif
+
+  SL_ENTER(_("check_protocol"));
+
+  /* seed / re-seed the PRNG if required
+   */
+  (void) taus_seed();
+
+
+  /* protocols: 
+   * -- (iii)    file transfer
+   * -- (ii)     authenticated message transfer
+   * -- (i)      SRP key exchange
+   */
+
+  /* --------- FILE TRANSFER  -----------
+   */
+  if ( (conn->head[0] & SH_PROTO_SRP) == 0  &&
+       (conn->head[0] & SH_PROTO_BIG) != 0  /* is set */ )
+    {
+
+      if (state == SH_DO_READ)        /* finished reading */
+	{
+	  TPT(( 0, FIL__, __LINE__, _("msg=<File transfer - entry.>\n")));
+
+	  /* -- Client requests challenge. --
+	   */
+	  if (0 == check_request_nerr ((char *) &(conn->head[3]), _("HELO")))
+	    {
+
+	      TPT(( 0, FIL__, __LINE__, 
+		    _("msg=<File transfer - HELO (1).>\n")));
+
+	      if (conn->buf == NULL || sl_strlen(conn->buf) <= KEY_LEN)
+		{
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NOCLT);
+		  sh_forward_do_free (conn);
+		  SL_RET0(_("check_protocol"));
+		} 
+
+	      /* ----  search the register  -----
+	       */
+	      
+	      this_client = search_register (conn, KEY_LEN);
+	      if (this_client == NULL)
+		SL_RET0(_("check_protocol"));
+
+	      /* ---- force authentication -----
+	       */
+
+	      if (this_client->session_key[0] == '\0' ||
+		  (time(NULL) - this_client->session_key_timer) 
+		  > (time_t) TIMEOUT_KEY )
+		{
+		  /* fake an auth request and jump there
+		   */
+		  conn->head[0]  = (conn->head[0] | SH_PROTO_SRP);
+		  conn->head[3]  = 'S';
+		  conn->head[4]  = 'A';
+		  conn->head[5]  = 'L';
+		  conn->head[6]  = 'T';
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FAUTH,
+				  &(conn->buf[KEY_LEN]));
+		  strcpy(conn->buf,                      /* known to fit  */
+			 &(conn->buf[KEY_LEN])); 
+		  this_client->session_key[0]    = '\0';
+		  this_client->session_key_timer = (time_t) 1;
+		  goto servInit;
+		}
+
+	      /* --- check whether hostname is properly signed ---
+	       */ 
+	      if (conn->K != NULL) 
+		{
+		  SH_FREE(conn->K);
+		  conn->K = NULL;
+		}
+	      i = sl_strlen(&(conn->buf[KEY_LEN])) + KEY_LEN + 1;
+	      conn->K = SH_ALLOC(i);
+
+	      sl_strlcpy (conn->K, 
+			  sh_util_siggen(this_client->session_key,
+					 &(conn->buf[KEY_LEN]),
+					 sl_strlen(&(conn->buf[KEY_LEN]))),
+			  KEY_LEN+1);
+	      TPT((0, FIL__, __LINE__, _("msg=<host %s>\n"),
+		   &(conn->buf[KEY_LEN])));
+	      TPT((0, FIL__, __LINE__, _("msg=<ckey %s>\n"),
+		   this_client->session_key));
+	      TPT((0, FIL__, __LINE__, _("msg=<sign %s.>\n"),
+		   sh_util_siggen(this_client->session_key, 
+				  &(conn->buf[KEY_LEN]), 
+				  sl_strlen(&(conn->buf[KEY_LEN])))));
+
+	      if (0 != sl_strncmp(conn->K, conn->buf, KEY_LEN))
+		{
+		  TPT((0, FIL__, __LINE__, _("msg=<clt %s>\n"), conn->buf));
+		  TPT((0, FIL__, __LINE__, _("msg=<srv %s>\n"), conn->K));
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
+				  _("Signature mismatch"), 
+				  &(conn->buf[KEY_LEN]));
+
+		  this_client->session_key_timer =
+		    time(NULL) - (2*TIMEOUT_KEY);
+
+		  sh_forward_do_free (conn);
+		  SL_RET0(_("check_protocol"));
+		}
+	      SH_FREE(conn->K); 
+	      conn->K = NULL;
+
+	      /* --- create and send a nonce ---
+	       */
+	      
+	      conn->client_entry = this_client;
+	      sl_strlcpy (conn->peer, &(conn->buf[KEY_LEN]), SH_MINIBUF+1);
+
+	      ticks = (UINT32) taus_get (&(skey->rng0[0]), 
+					 &(skey->rng1[0]),
+					 &(skey->rng2[0]));
+	      if (conn->K != NULL) 
+		{
+		  SH_FREE(conn->K);
+		  conn->K = NULL;
+		}
+	      conn->K = SH_ALLOC(KEY_LEN+1);
+	      sl_strlcpy (conn->K, 
+			  sh_tiger_hash ((char *) &ticks, 
+					 TIGER_DATA, sizeof(UINT32)), 
+			  KEY_LEN+1);
+
+	      TPT((0, FIL__, __LINE__, _("msg=<send nonce>\n")));
+	      sh_forward_prep_send (conn, conn->K, KEY_LEN+1, _("NSRV"), 
+				    SH_PROTO_BIG);
+	    }
+
+	  /* --- Client has send a message. Check state and message. ---
+	   */
+	  else if (0 == check_request_nerr((char *)&(conn->head[3]), _("NCLT")) &&
+		   conn->client_entry != NULL                           &&
+		   sl_strlen(conn->buf) > KEY_LEN                       &&
+		   conn->K != NULL)
+	    {
+
+	      TPT(( 0, FIL__, __LINE__, 
+		    _("msg=<File transfer - NCLT (3).>\n")));
+
+	      /* --- get client nonce and compute hash ---
+	       */
+	      if (conn->A != NULL)
+		{
+		  SH_FREE(conn->A);
+		  conn->A = NULL;
+		}
+	      conn->A = SH_ALLOC(3*KEY_LEN+1);
+	      sl_strlcpy (conn->A, conn->K, KEY_LEN+1); 
+	      sl_strlcat(conn->A, conn->buf, /* ignore remainder */
+			 2*KEY_LEN+1);
+	      sl_strlcat(conn->A, conn->client_entry->session_key, 
+			 3*KEY_LEN+1);
+	      sl_strlcpy (conn->K, sh_tiger_hash(conn->A,TIGER_DATA,3*KEY_LEN),
+			  KEY_LEN+1);
+	      SH_FREE(conn->A); 
+	      conn->A = NULL;
+
+
+#ifdef SH_ENCRYPT
+	      if ((conn->client_entry->encf_flag != 0) &&
+		  ((conn->head[0] & SH_PROTO_ENC) == 0)) 
+		{
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_MISENC,
+				  _("file download"),
+#ifdef SH_ENCRYPT_2
+				  _("version2"),
+#else
+				  _("version1"),
+#endif
+				  _("none"));
+		  if (sl_strlen(conn->buf) > (KEY_LEN + 5)) {
+		    if (sh_tools_hash_vfy(conn->K, conn->buf, KEY_LEN+5)) {
+		      if (conn->buf[KEY_LEN+4] == conn->head[0]) {
+			/* conn->client_entry->encf_flag = 0 */ ; /* FIXME */
+		      }
+		    }
+		  }
+		}
+	      else if ((conn->client_entry->encf_flag != 0) &&
+		       ((conn->head[0] & SH_MASK_ENC) != 
+			conn->client_entry->encf_flag))
+		{
+		  sh_error_handle(SH_ERR_NOTICE, FIL__, __LINE__, 0, 
+				  MSG_TCP_MISENC,
+				  _("file download"),
+#ifdef SH_ENCRYPT_2
+				  _("version2"),
+#else
+				  _("version1"),
+#endif
+				  ((conn->head[0] & SH_PROTO_EN2) == SH_PROTO_EN2) ? _("version2") : _("version1")
+				  );
+		  conn->client_entry->encf_flag = 
+		    (conn->head[0] & SH_MASK_ENC);
+		}
+#else
+	      if ((conn->head[0] & SH_PROTO_ENC) != 0) 
+		{
+		  sh_error_handle((-1), FIL__, __LINE__, 0, 
+				  MSG_TCP_MISENC,
+				  _("file download"),
+				  _("none"), 
+				  ((conn->head[0] & SH_PROTO_EN2) == SH_PROTO_EN2) ? _("version2") : _("version1"));
+		}
+#endif
+
+
+	      /* ---- K = H(NSRV, NCLT, session_key) -------
+	       */ 
+
+	      if (conn->FileName != NULL)
+		{
+		  SH_FREE(conn->FileName);
+		  conn->FileName = NULL;
+		}
+
+	      if (0 == sl_strncmp (_("CONF"), &(conn->buf[KEY_LEN]), 4))
+		{
+		  strcpy(conn->FileType, _("CONF"));     /* known to fit  */
+		  conn->FileName = get_client_conf_file(conn->peer, 
+							&(conn->FileLength));
+		  conn->FileSent = 0;
+		}
+	      else  if (0 == sl_strncmp (_("DATA"), &(conn->buf[KEY_LEN]), 4))
+		{
+		  strcpy(conn->FileType, _("DATA"));     /* known to fit  */
+		  conn->FileName = get_client_data_file(conn->peer, 
+							&(conn->FileLength));
+		  conn->FileSent = 0;
+		}
+	      else
+		{
+		  ptok = sh_util_safe_name(&(conn->buf[KEY_LEN]));
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FFILE,
+				  conn->peer, 
+				  ptok);
+		  SH_FREE(ptok);
+		  status_update (conn->client_entry, CLT_FAILED);
+		  sh_forward_do_free (conn);
+		}
+
+	      bytes = -1;
+
+	      if (conn != NULL && conn->FileName != NULL)
+		{
+		  sfd = sl_open_read(conn->FileName, SL_YESPRIV);
+		  if (!SL_ISERROR(sfd))
+		    {
+		      read_buf = SH_ALLOC(TRANS_BYTES);
+		      bytes = sl_read (sfd, read_buf, TRANS_BYTES);
+		      sl_close(sfd);
+		    }
+
+		  else
+		    {
+		      sh_error_handle((-1), FIL__, __LINE__, sfd, 
+				      MSG_E_ACCESS,
+				      (long) geteuid(),
+				      conn->FileName);
+		    }
+		  if (bytes >= 0)
+		    {
+#ifdef SH_ENCRYPT
+		      /* need to send N * B_SIZ bytes
+		       */ 
+		      blkfac = bytes / B_SIZ;
+		      rem    = bytes - (blkfac * B_SIZ);
+		      if (rem != 0)
+			{
+			  memset(&read_buf[bytes], '\n', (B_SIZ-rem));
+			  ++blkfac;
+			  send_bytes = blkfac * B_SIZ;
+			}
+		      else
+			send_bytes = bytes;
+
+		      send_buf = hash_me(conn->K, read_buf, 
+					 send_bytes);
+
+		      sh_forward_prep_send_crypt (conn, send_buf, 
+						  send_bytes+KEY_LEN, 
+						  _("FILE"),  
+						  SH_PROTO_BIG|conn->client_entry->encf_flag);
+#else
+		      send_buf = hash_me(conn->K, read_buf, bytes);
+		      sh_forward_prep_send_crypt (conn, send_buf, 
+						  bytes+KEY_LEN, 
+						  _("FILE"),  SH_PROTO_BIG);
+#endif
+		      conn->FileSent += bytes;
+		      if (send_buf != NULL) 
+			{
+			  SH_FREE(send_buf);
+			}
+		      SH_FREE(read_buf);
+		    }
+		}
+
+	      if (conn == NULL    || conn->FileName == NULL || 
+		  SL_ISERROR(sfd) || bytes < 0)
+		{
+		  sh_error_handle((-1), FIL__, __LINE__, sfd, MSG_TCP_NFILE,
+				  conn->peer, 
+				  (conn->FileName == NULL) ? 
+				  _("(NULL)") : conn->FileName);
+		  status_update (conn->client_entry, CLT_FAILED);
+		  sh_forward_do_free (conn);
+		}
+
+	    }
+	  
+	 else if (0 == check_request_nerr((char *)&(conn->head[3]), 
+					  _("RECV"))                    &&
+		   conn->client_entry != NULL                           &&
+		   conn->K != NULL                                      &&
+		   conn->FileName != NULL)
+	    {
+
+	      TPT(( 0, FIL__, __LINE__, 
+		    _("msg=<File transfer - RCVT (5+).>\n")));
+
+	      if (conn->FileSent == conn->FileLength)
+		{
+		  send_buf = hash_me(conn->K, conn->peer, 
+				     sl_strlen(conn->peer));
+#ifdef SH_ENCRYPT
+		  sh_forward_prep_send_crypt (conn, send_buf, 
+					      sl_strlen(conn->peer)+KEY_LEN, 
+					      _("EEOT"),  
+					      SH_PROTO_BIG|conn->client_entry->encf_flag);
+#else
+		  sh_forward_prep_send_crypt (conn, send_buf, 
+					      sl_strlen(conn->peer)+KEY_LEN, 
+					      _("EEOT"),  
+					      SH_PROTO_BIG);
+#endif
+		  SH_FREE(send_buf);
+		}
+	      else
+		{
+		  bytes = -1;
+		  sfd = sl_open_read(conn->FileName, SL_YESPRIV);
+		  if (!SL_ISERROR(sfd))
+		    {
+		      read_buf = SH_ALLOC(TRANS_BYTES);
+		      sl_seek (sfd, (off_t) conn->FileSent);
+		      bytes = sl_read (sfd, read_buf, TRANS_BYTES);
+		      sl_close(sfd);
+		    }
+		  else
+		    {
+		      sh_error_handle((-1), FIL__, __LINE__, sfd, 
+				      MSG_E_ACCESS,
+				      (long) geteuid(),
+				      conn->FileName);
+		    }
+		  if (bytes >= 0)
+		    {
+#ifdef SH_ENCRYPT
+		      /* need to send N * B_SIZ bytes
+		       */ 
+		      blkfac = bytes / B_SIZ;
+		      rem    = bytes - (blkfac * B_SIZ);
+		      if (rem != 0)
+			{
+			  memset(&read_buf[bytes], '\n', (B_SIZ-rem));
+			  ++blkfac;
+			  send_bytes = blkfac * B_SIZ;
+			}
+		      else
+			send_bytes = bytes;
+
+		      send_buf = hash_me(conn->K, read_buf, 
+					 send_bytes);
+
+		      sh_forward_prep_send_crypt (conn, send_buf, 
+						  send_bytes+KEY_LEN, 
+						  _("FILE"),  
+						  SH_PROTO_BIG|conn->client_entry->encf_flag);
+#else
+
+		      send_buf = hash_me(conn->K, read_buf, bytes);
+		      sh_forward_prep_send_crypt (conn, send_buf, 
+						  bytes+KEY_LEN, 
+						  _("FILE"),  
+						  SH_PROTO_BIG);
+#endif
+
+		      conn->FileSent += bytes;
+		      SH_FREE(send_buf);
+		      SH_FREE(read_buf);
+		    }
+		  else
+		    {
+		      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NFILE,
+				      conn->peer,
+				      (conn->FileName == NULL) ? 
+				      _("(NULL)") : conn->FileName);
+		      status_update (conn->client_entry, CLT_FAILED);
+		      sh_forward_do_free (conn);
+		    }
+		}
+	    }
+
+
+	  else if (0 == check_request_nerr((char *)&(conn->head[3]), 
+					   _("EOTE")) &&
+		   conn->client_entry != NULL)
+	    {
+
+	      TPT(( 0, FIL__, __LINE__, 
+		    _("msg=<File transfer - EOTE (7).>\n")));
+
+	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_OKFILE,
+			      conn->peer);
+
+	      if ((conn->client_entry->status_now != CLT_SUSPEND) &&
+		  (conn->client_entry->status_now != CLT_TOOLONG))
+		{ status_update (conn->client_entry, CLT_FILE); }
+	      else
+		{ conn->client_entry->session_key[0]    = '\0'; }
+	      conn->client_entry->last_connect = time (NULL);
+	      sh_forward_do_free (conn);
+	    }
+	    
+
+	  /* client does something unexpected
+	   */
+	  else  /* ---- ??? ----- */
+	    {
+	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FINV,
+			      1, conn->pass, conn->peer,  
+			      '\\', conn->head[3], '\\',conn->head[4],
+			      '\\', conn->head[5], '\\',conn->head[6]);
+	      status_update (conn->client_entry, CLT_FAILED);
+	      sh_forward_do_free (conn);
+	    }
+	}
+
+      else if (state == SH_DO_WRITE)  /* finished writing */
+	{
+	  TPT(( 0, FIL__, __LINE__, _("msg=<File transfer - (wait).>\n")));
+
+	  /* challenge is sent, now wait for message from client
+	   */
+	  conn->headcount     = 0;
+	  conn->bytecount     = 0;
+	  conn->bytes_to_send = 0;
+	  conn->bytes_to_get  = 0;
+	  if (conn->buf != NULL) 
+	    {
+	      SH_FREE(conn->buf);
+	      conn->buf           = NULL;
+	    }
+	  conn->state     = CONN_READING;
+	}
+      SL_RET0(_("check_protocol"));
+    }
+
+  /* ---------  message exchange  -----------
+   */
+  if ((conn->head[0] & SH_PROTO_SRP) == 0  && 
+      (conn->head[0] & SH_PROTO_MSG) != 0  /* is set */ )
+    {
+
+      if (state == SH_DO_READ)        /* finished reading */
+	{
+
+	  TPT(( 0, FIL__, __LINE__, _("msg=<Message transfer - entry.>\n")));
+
+	  /* client requests challenge
+	   */
+	 if (0 == check_request_nerr ((char *)&(conn->head[3]), _("HELO")))
+	    {
+
+	      TPT(( 0, FIL__, __LINE__, 
+		    _("msg=<Message transfer - HELO (1).>\n")));
+
+	      if (conn->buf == NULL || sl_strlen(conn->buf) <= KEY_LEN )
+		{
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NOCLT);
+		  sh_forward_do_free (conn);
+		  SL_RET0(_("check_protocol"));
+		} 
+
+	      TPT(( 0, FIL__, __LINE__, _("msg=<Rcvt %s.>\n"), conn->buf));
+
+	      /* ----  search the register  -----
+	       */
+	      this_client = search_register (conn, KEY_LEN);
+	      if (NULL == this_client)
+		SL_RET0(_("check_protocol"));
+
+	      /* ---- force authentication -----
+	       */
+	      if ( (this_client->session_key[0] == '\0') || 
+		   ((time(NULL)-this_client->session_key_timer) 
+		    > (time_t) TIMEOUT_KEY)
+		   )
+		{
+
+		  /* fake an auth request and jump there
+		   */
+		  conn->head[0]  = (conn->head[0] | SH_PROTO_SRP);
+		  conn->head[3]  = 'S';
+		  conn->head[4]  = 'A';
+		  conn->head[5]  = 'L';
+		  conn->head[6]  = 'T';
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FAUTH,
+				  &(conn->buf[KEY_LEN]));
+		  strcpy(conn->buf,                      /* known to fit  */
+			 &(conn->buf[KEY_LEN]));
+		  this_client->session_key[0]    = '\0';
+		  this_client->session_key_timer = (time_t) 1;
+
+		  goto servInit;
+		}
+	      
+	      /* check whether hostname is properly signed
+	       */ 
+	      if (conn->K != NULL)
+		{ 
+		  SH_FREE(conn->K);
+		  conn->K = NULL;
+		}
+	      i = sl_strlen(&(conn->buf[KEY_LEN])) + KEY_LEN + 1;
+	      conn->K = SH_ALLOC(i);
+
+	      sl_strlcpy (conn->K, 
+			  sh_util_siggen(this_client->session_key,
+					 &(conn->buf[KEY_LEN]),
+					 sl_strlen(&(conn->buf[KEY_LEN]))),
+			  KEY_LEN+1);
+	      TPT((0, FIL__, __LINE__, _("msg=<host %s>\n"), 
+		   &(conn->buf[KEY_LEN])));
+	      TPT((0, FIL__, __LINE__, _("msg=<ckey %s>\n"), 
+		   this_client->session_key));
+	      TPT((0, FIL__, __LINE__, _("msg=<sign %s>\n"), conn->K));
+
+	      if (0 != sl_strncmp(conn->K, conn->buf, KEY_LEN))
+		{
+		  TPT(( 0, FIL__, __LINE__, _("msg=<Rcvt %s>\n"), conn->buf));
+		  TPT(( 0, FIL__, __LINE__, _("msg=<Want %s>\n"), conn->K));
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
+				  _("Signature mismatch"), 
+				  &(conn->buf[KEY_LEN]));
+
+		  this_client->session_key_timer = 
+		    time(NULL) - (2*TIMEOUT_KEY);
+
+		  sh_forward_do_free (conn);
+		  SL_RET0(_("check_protocol"));
+		}
+	      SH_FREE(conn->K); 
+	      conn->K = NULL;
+
+	      /* -- create a nonce and send it --
+	       */
+
+	      conn->client_entry = this_client;
+	      sl_strlcpy (conn->peer, &(conn->buf[KEY_LEN]), SH_MINIBUF+1);
+
+	      ticks = (UINT32) taus_get (&(skey->rng0[0]), 
+					 &(skey->rng1[0]),
+					 &(skey->rng2[0]));
+	      test = (char *) &ticks;
+	      sh_util_cpylong (conn->challenge, test, 4);
+	      conn->challenge[4] = '\0';
+	      for (i = 0; i < 4; ++i)
+		if (conn->challenge[i] == '\0')
+		  conn->challenge[i] = 0x01;
+
+	      sh_forward_prep_send (conn, conn->challenge, 5, _("TALK"), 
+				    SH_PROTO_MSG);
+	      TPT(( 0, FIL__, __LINE__, _("msg=<Sent %s.>\n"), 
+		    hu_trans(conn->challenge)));
+	    }
+
+	  /* Client has send a message. Check whether we are in proper
+	   * state, and verify message.
+	   */
+	  else if (0 == 
+		   check_request_nerr((char *)&(conn->head[3]), _("MESG")) &&
+		   conn->client_entry != NULL                           &&
+		   conn->client_entry->session_key[0] != '\0'           &&
+		   (len = sl_strlen(conn->buf) - KEY_LEN) > 0           &&
+		   sl_strlen(conn->challenge) == 4)
+	    {
+	      TPT(( 0, FIL__, __LINE__, 
+		    _("msg=<Message transfer - MESG (3).>\n")));
+
+#ifdef SH_ENCRYPT
+	      if (conn->client_entry->encf_flag == 0) {
+		conn->client_entry->ency_flag = 0;
+	      }
+	      if ((conn->client_entry->ency_flag != 0) && 
+		  ((conn->head[0] & SH_PROTO_ENC) == 0)) 
+		{
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_MISENC,
+				  _("message transfer"), 
+#ifdef SH_ENCRYPT_2
+				  _("version2"),
+#else
+				  _("version1"),
+#endif 
+				  _("none"));
+		  /* conn->client_entry->ency_flag = 0; */
+		}
+	      else if ((conn->client_entry->ency_flag != 0) &&
+		       ((conn->head[0] & SH_MASK_ENC) != 
+			conn->client_entry->ency_flag))
+		{
+		  sh_error_handle(SH_ERR_NOTICE, FIL__, __LINE__, 0, 
+				  MSG_TCP_MISENC,
+				  _("message transfer"), 
+#ifdef SH_ENCRYPT_2
+				  _("version2"),
+#else
+				  _("version1"),
+#endif 
+				  ((conn->head[0] & SH_PROTO_EN2) == SH_PROTO_EN2) ? _("version2") : _("version1"));
+		  conn->client_entry->ency_flag = 
+		    (conn->head[0] & SH_MASK_ENC); 
+		}
+#else
+	      if ((conn->head[0] & SH_PROTO_ENC) != 0) 
+		{
+		  sh_error_handle((-1), FIL__, __LINE__, 0, 
+				  MSG_TCP_MISENC,
+				  _("message transfer"), 
+				  _("none"), 
+				  ((conn->head[0] & SH_PROTO_EN2) == SH_PROTO_EN2) ? _("version2") : _("version1"));
+		}
+#endif
+
+	      TPT(( 0, FIL__, __LINE__, _("msg=<Rcvt %s.>\n"), conn->buf));
+	      /* get hash from message end, truncate message
+	       */
+	      sl_strlcpy(hash, &(conn->buf[len]), KEY_LEN+1);
+	      conn->buf[len] = '\0';
+	      
+	      /* verify hash
+	       */
+	      buffer = sh_util_strconcat(conn->buf, conn->challenge, NULL);
+	      i =  sl_strncmp(hash, 
+			      sh_util_siggen(conn->client_entry->session_key,
+					     buffer,
+					     sl_strlen(buffer)),
+			      KEY_LEN);
+	      TPT((0, FIL__, __LINE__, _("msg=<sign %s.>\n"),
+		   sh_util_siggen(conn->client_entry->session_key,
+				  buffer,
+				  sl_strlen(buffer))));
+
+
+	      if (0 != i)
+		{
+		  TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
+		  status_update (conn->client_entry, CLT_FAILED);
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
+				  _("Msg signature mismatch"), conn->peer);
+		  conn->client_entry->session_key_timer = 
+		    time(NULL) - (2*TIMEOUT_KEY);
+		  sh_forward_do_free (conn);
+		  SL_RET0(_("check_protocol"));
+		}
+	      else
+		{
+		  conn->client_entry->last_connect = time (NULL);
+
+		  if (NULL != sl_strstr(conn->buf,      _("EXIT")))
+		    {
+		      TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
+		      conn->client_entry->exit_flag = 1;
+		      status_update (conn->client_entry, CLT_EXITED);
+		    }
+		  else if (NULL != sl_strstr(conn->buf, _("PANIC")))
+		    {
+		      TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
+		      status_update (conn->client_entry, CLT_PANIC);
+		    }
+		  else if (NULL != sl_strstr(conn->buf, _("SUSPEND")))
+		    {
+		      TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
+		      status_update (conn->client_entry, CLT_SUSPEND);
+		    }
+		  else if (NULL != sl_strstr(conn->buf, _("POLICY")))
+		    {
+		      TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
+		      status_update (conn->client_entry, CLT_POLICY);
+		    }
+		  else if (NULL != sl_strstr(conn->buf, 
+					     _("File check completed")))
+		    {
+		      TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
+		      status_update (conn->client_entry, CLT_CHECK);
+		    }
+		  else if (NULL != sl_strstr(conn->buf, _("START")))
+		    {
+		      TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
+		      sh_socket_add2reload (conn->client_entry->hostname);
+		      if (conn->client_entry->status_now == CLT_SUSPEND) {
+			status_update (conn->client_entry, CLT_ILLEGAL);
+			sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_ILL,
+					conn->peer);
+		      }
+		      else
+			status_update (conn->client_entry, CLT_STARTED);
+		    }
+		  else
+		    {
+		      TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
+		      if (0 != sl_strcmp(conn->buf, 
+					 _("Runtime configuration reloaded")))
+			{
+			  sh_socket_add2reload (conn->client_entry->hostname);
+			}
+		      status_update (conn->client_entry, CLT_MSG);
+		    }
+
+		  TPT((0, FIL__, __LINE__, _("msg=<status updated>\n")));
+		  clt_sev   = atoi(conn->buf);
+		  clt_class = (-1);
+		  ptok    = strchr(conn->buf, '?');
+		  if (ptok != NULL)
+		    {
+		      ++ptok;
+		      if (ptok != NULL && sh.flag.client_class == S_TRUE) 
+			clt_class = atoi(ptok);  /* is a global */
+		      ptok = strchr(ptok, '?');
+		      if (ptok != NULL) 
+			++ptok;
+		    }
+		  if (sh.flag.client_severity == S_FALSE)
+		    clt_sev = (-1);
+
+		  /* here we expect an xml formatted message, thus we don't
+		     escape xml special chars (flag == 0) */
+		  ptok = 
+		    sh_tools_safe_name ((ptok!=NULL) ? ptok : conn->buf, 0);
+
+		  /* push client name to error routine
+                   */
+                  sh_error_set_peer(sh_strip_domain (conn->peer));
+		  sh_error_handle(clt_sev, FIL__, __LINE__, 0, MSG_TCP_MSG,
+				  sh_strip_domain (conn->peer), 
+				  ptok);
+                  sh_error_set_peer(NULL);
+
+		  TPT((0, FIL__, __LINE__, _("msg=<%s>\n"), ptok));
+		  SH_FREE(ptok);
+		  clt_class = (-1);
+		}
+	      memset(buffer, '\0', sl_strlen(buffer));
+	      SH_FREE(buffer);
+
+	      /* SERVER CONF SEND
+	       */
+	      buffer = sh_util_strconcat(conn->buf,
+					 conn->challenge,
+					 NULL);
+	      sl_strlcpy(hash, 
+			 sh_util_siggen ( conn->client_entry->session_key,
+					  buffer,
+					  sl_strlen(buffer)),
+			 KEY_LEN+1);
+	      
+	      /* --- SERVER CMD --- */
+	      cmd = sh_socket_check (conn->peer);
+
+	      if (cmd != NULL)
+		{
+		  /* max cmd size is SH_MAXMSGLEN bytes
+		   */
+		  sl_strlcpy(&hash[KEY_LEN], cmd, SH_MAXMSGLEN);
+		  sl_strlcat(&hash[KEY_LEN],
+			     sh_util_siggen ( conn->client_entry->session_key,
+					      &hash[KEY_LEN],
+					      sl_strlen(&hash[KEY_LEN])),
+			     SH_MAXMSGLEN+KEY_LEN+1);
+		  
+		  TPT((0, FIL__, __LINE__, _("CONF SEND <0> <%s>\n"), 
+			  &hash[KEY_LEN]));
+		  
+		} else {
+		  
+		  TPT((0, FIL__, __LINE__, _("CONF SEND <0> <[NULL]>\n")));
+		  
+		}
+	      /* --- SERVER CMD END --- */
+
+	      TPT((0, FIL__, __LINE__, _("msg=<sign %s.>\n"),
+		   sh_util_siggen(conn->client_entry->session_key,
+				  buffer,
+				  sl_strlen(buffer))));
+	  
+#ifdef SH_ENCRYPT
+	      sh_forward_prep_send_crypt (conn, hash, 
+					  sl_strlen(hash) /* KEY_LEN */, 
+					  _("CONF"), 
+					  SH_PROTO_MSG|SH_PROTO_END|conn->client_entry->ency_flag);
+#else
+	      sh_forward_prep_send_crypt (conn, hash, 
+					  sl_strlen(hash) /* KEY_LEN */, 
+					  _("CONF"), 
+					  SH_PROTO_MSG|SH_PROTO_END);
+#endif
+
+	      memset(buffer, '\0', sl_strlen(buffer));
+	      SH_FREE(buffer);
+	      
+	      /* sh_forward_do_free (conn); */
+	    }
+	  
+	  /* client does something unexpected
+	   */
+	  else  /* ---- ??? ----- */
+	    {
+	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FINV,
+			      2, conn->pass, conn->peer,  
+			      '\\', conn->head[3], '\\',conn->head[4],
+			      '\\', conn->head[5], '\\',conn->head[6]);
+	      status_update (conn->client_entry, CLT_FAILED);
+	      conn->client_entry->session_key_timer = 
+		time(NULL) - (2*TIMEOUT_KEY);
+	      sh_forward_do_free (conn);
+	    }
+	}
+      else if (state == SH_DO_WRITE)  /* finished writing */
+	{
+	  if (0 != (conn->head[0] & SH_PROTO_END))
+	    {
+	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_OKMSG,
+			      sh_strip_domain (conn->peer));
+	      sh_forward_do_free (conn);
+	      SL_RET0(_("check_protocol"));
+	    }
+
+	  TPT(( 0, FIL__, __LINE__, _("msg=<Msg transfer - (wait).>\n")));
+
+	  /* challenge is sent, now wait for message from client
+	   */
+	  conn->headcount     = 0;
+	  conn->bytecount     = 0;
+	  conn->bytes_to_send = 0;
+	  conn->bytes_to_get  = 0;
+	  if (conn->buf != NULL) 
+	    {
+	      SH_FREE(conn->buf);
+	      conn->buf           = NULL;
+	    }
+	  conn->state     = CONN_READING;
+	}
+      TPT((0, FIL__, __LINE__, _("msg=<return>\n") ));
+      SL_RET0(_("check_protocol"));
+    }
+
+  /* ---------  authentication  -----------
+   */
+
+  /* entry point for jump from message forward if session key must
+   * be re-initialized
+   */	 
+ servInit:
+
+  if ( (conn->head[0] & SH_PROTO_SRP) != 0   /* is set */ )
+    {
+
+#ifndef USE_SRP_PROTOCOL
+
+      if (state == SH_DO_READ)        /* finished reading */
+	{
+	  TPT((0, FIL__, __LINE__, _("msg=<Authentication - entry.>\n")));
+
+	  /* first pass -- client request salt  
+	   */
+	  if (conn->pass    == 1) 
+	    {
+
+	      TPT((0, FIL__, __LINE__, 
+		   _("msg=<Authentication - SALT (1).>\n")));
+
+	      if (conn->buf == NULL || sl_strlen(conn->buf) == 0)
+		{
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NOCLT);
+		  sh_forward_do_free (conn);
+		  SL_RET0(_("check_protocol"));
+		} 
+		  
+
+	      /* search the register
+	       */
+
+	      this_client = search_register (conn, 0);
+	      if (NULL == this_client)
+		SL_RET0(_("check_protocol"));
+
+
+	      conn->client_entry = this_client;
+	      sl_strlcpy (conn->peer, conn->buf, SH_MINIBUF+1);
+
+	      if (0 != check_request_s((char *)&(conn->head[3]), 
+				       _("SALT"),conn->peer))
+		{
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
+				  _("No salt requested"), conn->peer);
+		  status_update (conn->client_entry, CLT_FAILED);
+		  conn->client_entry->session_key_timer = 
+		    time(NULL) - (2*TIMEOUT_KEY);
+		  sh_forward_do_free (conn);
+		  SL_RET0(_("check_protocol"));
+		} 
+
+	       
+	      /* -- create server nounce v --
+	       */
+	      ticks = (UINT32) taus_get (&(skey->rng0[0]), 
+					 &(skey->rng1[0]),
+					 &(skey->rng2[0]));
+	      
+	      if (conn->A != NULL)
+		{
+		  SH_FREE(conn->A);
+		  conn->A = NULL;
+		}
+	      conn->A = SH_ALLOC(KEY_LEN+1);
+
+	      sl_strlcpy(conn->A, 
+			 sh_tiger_hash((char *) &ticks, 
+				       TIGER_DATA, sizeof(UINT32)),
+			 KEY_LEN+1);
+	      u[0] = 'I'; u[1] = 'N'; u[2] = 'I'; u[3] = 'T'; u[4] = '\0';
+
+	      if (conn->M1 != NULL)
+		{
+		  SH_FREE(conn->M1);
+		  conn->M1 = NULL;
+		}
+	      conn->M1 = SH_ALLOC(2*KEY_LEN+1);
+
+	      /* compute hash key H(v(server), P)v(server)
+	       */
+	      sh_passwd (conn->A, conn->client_entry->verifier, 
+			 NULL, conn->M1);
+
+	      sl_strlcat(conn->M1, conn->A, 2*KEY_LEN+1);
+
+
+	      /* --- send H(v(server), P)v(server) ----
+	       */
+      	      sh_forward_prep_send (conn, 
+				    conn->M1, 
+				    sl_strlen(conn->M1), 
+				    u, 
+				    (conn->head[0]|SH_PROTO_SRP));
+
+	      SH_FREE(conn->M1); 
+	      conn->M1 = NULL;
+	    }
+
+	  /* client -- third pass
+	   * Message is H(H(u,v),P)u
+	   *
+	   * A := v, verifier := H(password), 
+	   */
+	  else if (conn->pass    == 3                   && 
+		   conn->client_entry != NULL)
+	    {
+
+	      TPT((0, FIL__, __LINE__, 
+		   _("msg=<Authentication - PASS (3).>\n")));
+				  
+	      if (0 != check_request_s((char *) &(conn->head[3]), _("PASS"), 
+				       conn->peer)                    ||
+		  sl_strlen(conn->buf) <= KEY_LEN                        ||
+		  conn->A == NULL)
+		{
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
+				  _("Invalid client request"), conn->peer);
+		  status_update (conn->client_entry, CLT_FAILED);
+		  conn->client_entry->session_key_timer = 
+		    time(NULL) - (2*TIMEOUT_KEY);
+		  sh_forward_do_free (conn);
+		  SL_RET0(_("check_protocol"));
+		} 
+
+	      /* store random nonce u from client
+	       */
+	      if (conn->K != NULL)
+		{
+		  SH_FREE(conn->K);
+		  conn->K = NULL;
+		}
+	      conn->K = SH_ALLOC(KEY_LEN+1);
+	      sl_strlcpy(conn->K, &(conn->buf[KEY_LEN]), KEY_LEN+1);
+
+	      /* verify random nonce u from client
+	       */
+	      if (conn->M1 != NULL)
+		{
+		  SH_FREE(conn->M1);
+		  conn->M1 = NULL;
+		}
+	      conn->M1 = sh_util_strconcat(conn->K, conn->A, NULL);
+
+	      TPT((0, FIL__, __LINE__, _("msg=<c/r: K = %s>\n"), conn->K));
+	      TPT((0, FIL__, __LINE__, _("msg=<c/r: A = %s>\n"), conn->A));
+	      TPT((0, FIL__, __LINE__, _("msg=<c/r: M = %s>\n"), conn->M1));
+
+	      sl_strlcpy(hash, sh_tiger_hash (conn->M1, 
+					      TIGER_DATA, 
+					      sl_strlen(conn->M1)), KEY_LEN+1); 
+	      sh_passwd (hash, conn->client_entry->verifier, NULL, conn->M1);
+
+	      TPT((0, FIL__, __LINE__, _("msg=<c/r: H = %s>\n"), hash));
+	      TPT((0, FIL__, __LINE__, _("msg=<c/r: P = %s>\n"), conn->M1));
+
+	      if ( 0 != sl_strncmp(conn->M1, conn->buf, KEY_LEN))
+		{
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
+				  _("Session key mismatch"), conn->peer);
+		  status_update (conn->client_entry, CLT_FAILED);
+		  conn->client_entry->session_key_timer = 
+		    time(NULL) - (2*TIMEOUT_KEY);
+		  sh_forward_do_free (conn);
+		  SL_RET0(_("check_protocol"));
+		} 
+		  
+
+	      /* ---- compute hash key H(v, P, u) ----
+	       */
+	      
+	      sh_passwd (conn->A, conn->client_entry->verifier, conn->K,
+			 conn->M1);
+
+	      sl_strlcpy(conn->client_entry->session_key, 
+			 conn->M1, KEY_LEN+1);
+	      TPT((0, FIL__, __LINE__, _("msg=<c/r: Key = %s>\n"), 
+		   conn->client_entry->session_key));
+
+#ifdef SH_ENCRYPT
+	       err_num = makeKey(&(conn->client_entry->keyInstE), 
+				 DIR_ENCRYPT, 192, 
+				 conn->client_entry->session_key);
+	       if (err_num < 0)
+		 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
+				 errorExplain(err_num), 
+				 _("check_protocol: makeKey"));
+	       err_num = makeKey(&(conn->client_entry->keyInstD), 
+				 DIR_DECRYPT, 192, 
+				 conn->client_entry->session_key);
+	       if (err_num < 0)
+		 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
+				 errorExplain(err_num), 
+				 _("check_protocol: makeKey"));
+#endif
+
+	      if (conn->K  != NULL) SH_FREE (conn->K);
+	      conn->K  = NULL;
+	      if (conn->A  != NULL) SH_FREE (conn->A);
+	      conn->A  = NULL;
+	      if (conn->M1 != NULL) SH_FREE (conn->M1);
+	      conn->M1 = NULL;
+
+	      /* if (conn->client_entry->status_now == CLT_STARTED */
+	      if (((conn->client_entry->status_now != CLT_INACTIVE) &&
+		   (conn->client_entry->status_now != CLT_EXITED)   &&
+		   (conn->client_entry->status_now != CLT_SUSPEND))
+		  && conn->client_entry->session_key_timer > (time_t) 1)
+		{
+		  status_update (conn->client_entry, CLT_ILLEGAL);
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_ILL,
+				  conn->peer);
+		}
+	      else if (conn->client_entry->session_key_timer == (time_t) 0)
+		{
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NEW,
+				  conn->peer);
+		  if (conn->client_entry->status_now != CLT_SUSPEND)
+		    status_update (conn->client_entry, CLT_STARTED);
+		}
+
+	      conn->client_entry->session_key_timer = time (NULL);
+	      conn->client_entry->last_connect = time (NULL);
+
+	      /* put in read state
+	       */
+	      sh_forward_prep_send (conn, 
+				    _("AUTH"),
+				    5, 
+				    _("AUTH"), 
+				    (conn->head[0]|SH_PROTO_SRP));
+
+	    }
+	  else
+	    {
+	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FINV,
+			      3, conn->pass, conn->peer, 
+			      '\\', conn->head[3], '\\', conn->head[4],
+			      '\\', conn->head[5], '\\', conn->head[6]);
+	      sh_forward_do_free (conn);
+	    }
+	}
+
+#else
+      /* use SRP                    */
+
+
+      if (state == SH_DO_READ)        /* finished reading */
+	{
+
+	  TPT((0, FIL__, __LINE__, _("msg=<Authentication - entry.>\n")));
+
+	  /* first pass -- client request salt  
+	   */
+	  if (conn->pass    == 1)
+	    {
+	      TPT((0, FIL__, __LINE__, 
+		   _("msg=<Authentication - SALT (1).>\n")));
+
+	      if (conn->buf == NULL)
+		{
+		  sh_error_handle( (-1), FIL__, __LINE__, 0, MSG_TCP_NOCLT);
+		  sh_forward_do_free (conn);
+		  SL_RET0(_("check_protocol"));
+		} 
+
+	      /* search the register
+	       */
+	      this_client = search_register(conn, 0);
+	      if (NULL == this_client)
+		SL_RET0(_("check_protocol"));
+
+	      conn->client_entry = this_client;
+	      sl_strlcpy (conn->peer, conn->buf, SH_MINIBUF+1);
+
+	      if (0 != check_request_s((char *)&(conn->head[3]), _("SALT"),
+				       conn->peer))
+		{
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
+				  _("No salt requested"), conn->peer);
+		  status_update (conn->client_entry, CLT_FAILED);
+		  conn->client_entry->session_key_timer = 
+		    time(NULL) - (2*TIMEOUT_KEY);
+		  sh_forward_do_free (conn);
+		  SL_RET0(_("check_protocol"));
+		} 
+		  
+
+	      u[0] = 'I'; u[1] = 'N'; u[2] = 'I'; u[3] = 'T'; u[4] = '\0';
+	      
+	      sh_forward_prep_send (conn, 
+				    conn->client_entry->salt, 
+				    sl_strlen(conn->client_entry->salt), 
+				    u, 
+				    (conn->head[0]|SH_PROTO_SRP));
+	    }
+
+	  /* client has sent A -- third pass
+	   */
+	  else if (conn->pass == 3                    && 
+		   conn->client_entry != NULL)
+	    {
+
+	      TPT((0, FIL__, __LINE__, 
+		   _("msg=<Authentication - PC01 (3).>\n")));
+
+	      if (0 != check_request_s((char *)&(conn->head[3]),_("PC01"),conn->peer)||
+		  conn->buf == NULL
+		  )
+		{
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
+				  _("Invalid client request"), conn->peer);
+		  status_update (conn->client_entry, CLT_FAILED);
+		  conn->client_entry->session_key_timer = 
+		    time(NULL) - (2*TIMEOUT_KEY);
+		  sh_forward_do_free (conn);
+		  SL_RET0(_("check_protocol"));
+		} 
+
+	      if (0 != sh_srp_init())
+		{
+		  status_update (conn->client_entry, CLT_FAILED);
+		  sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0, 
+				  MSG_TCP_EBGN);
+		  sh_forward_do_free (conn);
+		  SL_RET0(_("check_protocol"));
+		}
+		  
+
+	      /* check A, only send B if correct 
+	       */
+	      if ( sl_strlen(conn->buf) < SH_BUFSIZE && 
+		   0 == sh_srp_check_zero (conn->buf) )
+		{
+		  len = sl_strlen(conn->buf)+1;
+
+		  if (conn->A != NULL)
+		    {
+		      SH_FREE(conn->A);
+		      conn->A = NULL;
+		    }
+		  conn->A = SH_ALLOC(len);
+		  sl_strlcpy (conn->A, conn->buf, len);
+		  
+		  /* 
+		   * compute B 
+		   */
+		  if (0 != sh_srp_make_a ())     /* b        random number */
+		    {
+		      status_update (conn->client_entry, CLT_FAILED);
+		      
+		      sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0, 
+				      MSG_TCP_EBGN);
+		      sh_srp_exit();
+		      sh_forward_do_free (conn);
+		      SL_RET0(_("check_protocol"));
+		    }
+             
+		  foo_B = sh_srp_B               /* B = v + g^b            */
+		    (conn->client_entry->verifier);
+
+		  if (foo_B == NULL)
+		    {
+		      status_update (conn->client_entry, CLT_FAILED);
+		      
+		      sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0, 
+				      MSG_TCP_EBGN);
+		      sh_srp_exit();
+		      sh_forward_do_free (conn);
+		      SL_RET0(_("check_protocol"));
+		    }
+
+		  TPT((0, FIL__, __LINE__, _("msg=<srp: A = %s>\n"), conn->A));
+		  TPT((0, FIL__, __LINE__, _("msg=<srp: B = %s>\n"), foo_B));
+
+		  /* 
+		   * create nonce u 
+		   */
+		  ticks = (UINT32) taus_get (&(skey->rng0[0]), 
+					     &(skey->rng1[0]),
+					     &(skey->rng2[0])); 
+		  test = (char *) &ticks;
+		  sh_util_cpylong (u, test, 4);  /* u        nounce        */
+		  u[4] = '\0';
+		  sl_strlcpy(conn->challenge, 
+			     sh_tiger_hash(u, TIGER_DATA, 4),
+			     SH_CHALLENGE_SIZE);
+
+		  TPT((0, FIL__, __LINE__, _("msg=<srp: u = %03o-%03o-%03o-%03o>\n"), u[0], u[1], u[2], u[3]));
+		  TPT((0, FIL__, __LINE__, _("msg=<srp: U = %s>\n"), 
+		       conn->challenge));
+
+		  /* 
+		   * compute the session key K and M1 = Hash(A,B,K)
+		   */
+		  foo_Ss = sh_srp_S_s (conn->challenge, 
+				       conn->A, 
+				       conn->client_entry->verifier);
+		  if (foo_Ss == NULL)
+		    {
+		      status_update (conn->client_entry, CLT_FAILED);
+		      
+		      sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0, 
+				      MSG_TCP_EBGN);
+		      sh_srp_exit();
+		      sh_forward_do_free (conn);
+		      SL_RET0(_("check_protocol"));
+		    }
+
+		  if (conn->K != NULL)
+		    {
+		      SH_FREE(conn->K);
+		      conn->K = NULL;
+		    }
+		  conn->K = SH_ALLOC(KEY_LEN+1);
+		  sl_strlcpy(conn->K, 
+			     sh_tiger_hash(foo_Ss, TIGER_DATA, 
+					   sl_strlen(foo_Ss)),
+			     KEY_LEN+1);
+
+		  if (conn->M1 != NULL)
+		    {
+		      SH_FREE(conn->M1);
+		      conn->M1 = NULL;
+		    }
+		  conn->M1 = SH_ALLOC(KEY_LEN+1);
+		  sl_strlcpy(conn->M1, 
+			  sh_srp_M (conn->A, foo_B, conn->K),
+			  KEY_LEN+1);
+
+		  TPT((0, FIL__, __LINE__, _("msg=<srp:Ss = %s>\n"), foo_Ss));
+		  TPT((0, FIL__, __LINE__, _("msg=<srp: K = %s>\n"), conn->K));
+		  TPT((0, FIL__, __LINE__, _("msg=<srp:M1 = %s>\n"),conn->M1));
+
+		  /*
+		   * send B
+		   */
+		  sh_forward_prep_send (conn, 
+					foo_B,
+					sl_strlen(foo_B)+1,
+					u,
+					(conn->head[0]|SH_PROTO_SRP));
+		  if (foo_Ss != NULL)
+		    {
+		      SH_FREE(foo_Ss);
+		      foo_Ss = NULL;
+		    }
+		  if (foo_B  != NULL)
+		    {
+		      SH_FREE(foo_B);
+		      foo_B = NULL;
+		    }
+		}
+	      else
+		{
+		  status_update (conn->client_entry, CLT_FAILED);
+
+		  sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0, 
+				  MSG_TCP_EZERO);
+		  sh_forward_do_free (conn);
+		}
+
+	      sh_srp_exit();
+	    }
+		  
+	  /* client has sent M1 -- fifth pass
+	   */
+	  else if (conn->pass    == 5           && 
+		   conn->client_entry != NULL) 
+	    {
+	      TPT((0, FIL__, __LINE__, 
+		   _("msg=<Authentication - PC02 (5).>\n")));
+
+	      /* check that the state is valid
+	       */
+	      if (0 != check_request_s((char *)&(conn->head[3]), _("PC02"),
+				       conn->peer)                   ||
+		  conn->A == NULL || conn->K == NULL || conn->M1 == NULL)
+		{
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
+				  _("Invalid client request"), conn->peer);
+		  status_update (conn->client_entry, CLT_FAILED);
+		  conn->client_entry->session_key_timer = 
+		    time(NULL) - (2*TIMEOUT_KEY);
+		  sh_forward_do_free (conn);
+		  SL_RET0(_("check_protocol"));
+		} 
+
+	      /* ------ verify M1 = H(A,  B, K) -------
+	       * -----    send M2 = H(A, M1, K) -------
+	       */
+	      if (conn->buf != NULL && 
+		  sl_strncmp(conn->buf, conn->M1, KEY_LEN) == 0)
+		{
+		  /*
+		   * send M2
+		   */
+		  sh_forward_prep_send (conn, 
+					sh_srp_M (conn->A, conn->M1, conn->K),
+					KEY_LEN+1,
+					_("PARP"),
+					(conn->head[0]|SH_PROTO_SRP));
+
+		  if (conn->A  != NULL) SH_FREE(conn->A);  conn->A  = NULL;
+		  if (conn->M1 != NULL) SH_FREE(conn->M1); conn->M1 = NULL;
+		  sl_strlcpy(conn->client_entry->session_key, 
+			     conn->K, KEY_LEN+1);
+		  TPT((0, FIL__, __LINE__, _("msg=<key %s>\n"), 
+		       conn->client_entry->session_key));
+
+#ifdef SH_ENCRYPT
+		  err_num = makeKey(&(conn->client_entry->keyInstE), 
+				    DIR_ENCRYPT, 192, 
+				    conn->client_entry->session_key);
+		  if (err_num < 0)
+		    sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
+				    errorExplain(err_num), 
+				    _("sh_forward_prep_send_int: makeKey"));
+		  err_num = makeKey(&(conn->client_entry->keyInstD), 
+				    DIR_DECRYPT, 192, 
+				    conn->client_entry->session_key);
+		  if (err_num < 0)
+		    sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
+				    errorExplain(err_num), 
+				    _("sh_forward_prep_send_int: makeKey"));
+#endif
+
+		  if (conn->K  != NULL) SH_FREE(conn->K);  conn->K  = NULL;
+
+		  conn->client_entry->last_connect = time (NULL);
+		  
+		  if (((conn->client_entry->status_now != CLT_INACTIVE) &&
+		       (conn->client_entry->status_now != CLT_EXITED)   &&
+		       (conn->client_entry->status_now != CLT_SUSPEND))
+		      && conn->client_entry->session_key_timer > (time_t) 1)
+		    {
+		      status_update (conn->client_entry, CLT_ILLEGAL);
+
+		      sh_error_handle((-1), FIL__, __LINE__, 0, 
+				      MSG_TCP_ILL,
+				      conn->peer);
+		    }
+		  else if (conn->client_entry->session_key_timer == (time_t) 0)
+		    {
+		      sh_error_handle((-1), FIL__, __LINE__, 0, 
+				      MSG_TCP_NEW,
+				      conn->peer);
+		      if (conn->client_entry->status_now != CLT_SUSPEND)
+			status_update (conn->client_entry, CLT_STARTED);
+		    }
+		  conn->client_entry->session_key_timer = time (NULL);
+
+		}
+	      else
+		{
+		  status_update (conn->client_entry, CLT_FAILED);
+		  conn->client_entry->session_key_timer = 
+		    time(NULL) - (2*TIMEOUT_KEY);
+
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
+				  _("Session key mismatch"), conn->peer);
+		  sh_forward_do_free (conn);
+		} 
+	    }
+
+	  else
+	    {
+	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FINV,
+			      4, conn->pass, conn->peer, 
+			      '\\', conn->head[3], '\\', conn->head[4],
+			      '\\', conn->head[5], '\\', conn->head[6]);
+	      sh_forward_do_free (conn);
+	    }
+	}
+
+#endif
+
+      else if (state == SH_DO_WRITE)  /* finished writing */
+	{
+	  TPT((0, FIL__, __LINE__, _("msg=<Authentication -- (wait).>\n")));
+
+	  conn->headcount     = 0;
+	  conn->bytecount     = 0;
+	  conn->bytes_to_send = 0;
+	  conn->bytes_to_get  = 0;
+	  if (conn->buf != NULL) 
+	    {
+	      SH_FREE(conn->buf);
+	      conn->buf           = NULL;
+	    }
+	  conn->state     = CONN_READING;
+	}
+    }
+  SL_RET0(_("check_protocol"));
+}
+
+
+/***********************************************************
+ *
+ *    SERVER RECEIVE FUNCTION
+ *
+ ***********************************************************
+ */
+int sh_forward_do_read (sh_conn_t * conn)
+{
+  unsigned long   byteread;     /* bytes read         */
+
+#ifdef SH_ENCRYPT
+
+  unsigned long           blkfac = 0;
+  /* unsigned long           length2; */
+  char                  * p = NULL, * q = NULL;
+  RIJ_BYTE                    inBlock[B_SIZ]; 
+  RIJ_BYTE                    outBlock[B_SIZ];
+  unsigned int            j;
+  cipherInstance          cipherInst;
+  int                     err_num;
+#endif
+
+  SL_ENTER(_("sh_forward_do_read"));
+
+  if (conn->state == CONN_SENDING)
+    {
+      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_SYNC,
+		      conn->peer);
+      SL_RETURN( (-1), _("sh_forward_do_read"));
+    }
+
+  if (conn->headcount < SH_HEADER_SIZE) 
+    {
+      conn->bytes_to_get = SH_HEADER_SIZE - conn->headcount;
+      byteread           = read (conn->fd, &(conn->head[conn->headcount]),
+				    conn->bytes_to_get);
+      if (byteread > 0 || errno == EINTR) 
+	{
+	  if (byteread > 0) 
+	    conn->headcount += byteread;
+	  if (conn->headcount == SH_HEADER_SIZE)
+	    {
+		conn->bytes_to_get = 
+		  (256 * (unsigned int)conn->head[1] + 
+		   (unsigned int)conn->head[2]);
+		SH_SHOWPROT(conn->head, '<');
+		conn->bytecount = 0;
+	    }
+	}
+      else
+	{
+	  goto conn_reset;
+	}
+      SL_RETURN( (0), _("sh_forward_do_read"));
+    }
+
+
+  /* limit message size
+   */
+  /*
+  conn->bytes_to_get = 
+    (conn->bytes_to_get > (16*SH_BUFSIZE - 1)) ? 
+    (16*SH_BUFSIZE - 1) : conn->bytes_to_get;
+  */
+  conn->bytes_to_get = (conn->bytes_to_get > TRANS_BYTES) ? 
+    TRANS_BYTES : conn->bytes_to_get;
+
+  if (conn->headcount == SH_HEADER_SIZE && conn->bytes_to_get > 0)
+    {
+      if ((conn->bytecount > 0) && (conn->bytes_to_get > conn->bytecount))
+	{
+	  /* do nothing */;
+	}
+      else
+	{
+	  if (conn->buf != NULL)
+	    SH_FREE (conn->buf);
+	  conn->buf       = SH_ALLOC (conn->bytes_to_get + 1);
+	  conn->bytecount = 0;
+	}
+
+      byteread           = read (conn->fd, &(conn->buf[conn->bytecount]),
+				 conn->bytes_to_get - conn->bytecount);
+      if (byteread > 0 || errno == EINTR) 
+	{
+	  if (byteread > 0) 
+	    conn->bytecount    += byteread;
+	  if (conn->bytecount == conn->bytes_to_get) 
+	    {
+	      ++conn->pass;
+	      /* always terminate with NULL - we might use sl_strcmp()
+	       */
+	      conn->buf[conn->bytecount] = '\0';
+	      conn->state                = CONN_PAUSE;
+
+#ifdef SH_ENCRYPT
+	      if      ((conn->head[0] & SH_PROTO_EN2) != 0) /* if encrypted */
+		{
+		  conn->buf = 
+		    sh_tools_revertPack (conn->head, conn->buf,
+					 &(conn->client_entry->keyInstD),
+					 conn->bytecount);
+		}
+	      else if ((conn->head[0] & SH_PROTO_ENC) != 0) /* if encrypted */
+		{
+		  /* Decrypt only complete blocks. 
+		   * If there is an incomplete block,
+		   * something is wrong anyway.
+		   * Decrypt in place.
+		   */
+		  blkfac  = conn->bytecount / B_SIZ;
+		  /* length2 = (B_SIZ * blkfac); */
+		  p       = conn->buf;
+		  q       = conn->buf;
+      
+		  err_num = cipherInit (&cipherInst, MODE_CBC, NULL);
+		  if (err_num < 0)
+		    sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
+				    errorExplain(err_num), 
+				    _("sh_forward_do_read: cipherInit"));
+
+		  for (j = 0; j < blkfac; ++j)
+		    {
+		      memcpy(inBlock, p, B_SIZ);
+		      err_num = blockDecrypt(&cipherInst, 
+					     &(conn->client_entry->keyInstD), 
+					     inBlock, 128 * BNUM, outBlock);
+		      if (err_num < 0)
+			sh_error_handle((-1), FIL__, __LINE__, -1, 
+					MSG_E_SUBGEN,
+					errorExplain(err_num), 
+					_("sh_forward_do_read: blockDecrypt"));
+		      memcpy(q, outBlock, B_SIZ);
+		      p += 16;
+		      q += 16;
+		    }
+		}
+#endif
+
+	      /* ------  HERE CALL check_protocol(conn) -------  */
+	      check_protocol(conn, SH_DO_READ);
+	    }
+	}
+      else
+	{
+	  goto conn_reset;
+	}
+    }
+
+  else if (conn->headcount == SH_HEADER_SIZE && conn->bytes_to_get == 0)
+    {
+      if (conn->buf != NULL)
+	SH_FREE (conn->buf);
+      conn->buf       = NULL;
+      conn->bytecount = 0;
+      ++conn->pass;
+      conn->state     = CONN_PAUSE;
+      /* fprintf(stderr, "\n**** FIXME null read ****\n\n"); */
+      /* ------  HERE CALL check_protocol(conn) -------  */
+      check_protocol(conn, SH_DO_READ);
+    }
+      
+  SL_RETURN( (0), _("sh_forward_do_read"));
+
+ conn_reset:
+  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_RESET,
+		  conn->peer);
+  sh_forward_do_free ( conn );
+  SL_RETURN( (-1), _("sh_forward_do_read"));
+}
+
+#if !defined(O_NONBLOCK)
+#if defined(O_NDELAY)
+#define O_NONBLOCK  O_NDELAY
+#else
+#define O_NONBLOCK  0
+#endif
+#endif
+
+/* send to the client
+ */
+int sh_forward_do_write (sh_conn_t * conn)
+{
+  int    flags;
+  long   arg = 0;
+  long   bytesent;     /* bytes read         */
+
+  SL_ENTER(_("sh_forward_do_write"));
+
+  /* ---- consistency check ------
+   */
+  if (conn->state == CONN_READING)
+    {
+      sh_error_handle( (-1), FIL__, __LINE__, 0, MSG_TCP_SYNC,
+		      conn->peer);
+      SL_RETURN( (-1), _("sh_forward_do_write"));
+    }
+      
+  
+  flags = retry_fcntl (FIL__, __LINE__, conn->fd, F_GETFL, arg);
+  retry_fcntl (FIL__, __LINE__, conn->fd, F_SETFL,  flags|O_NONBLOCK);
+
+  /* ---- send the header ------
+   */
+  if (conn->headcount < SH_HEADER_SIZE) 
+    {
+      conn->bytes_to_send = SH_HEADER_SIZE - conn->headcount;
+      bytesent            = write (conn->fd, 
+				   &(conn->head[conn->headcount]), 
+				   conn->bytes_to_send);
+      if (bytesent >= 0 || errno == EINTR || errno == EAGAIN) 
+	{
+	  if (bytesent > 0) 
+	    conn->headcount += bytesent;
+	  if (conn->headcount == SH_HEADER_SIZE) 
+	    {
+	      conn->bytes_to_send = 
+		(256 * (int)conn->head[1] + (int)conn->head[2]);
+	    }
+	}
+      else 
+	{
+	  goto conn_reset_w;
+	}
+      if (conn->fd >= 0)
+	retry_fcntl (FIL__, __LINE__, conn->fd, F_SETFL,  flags);
+      SL_RETURN( (0), _("sh_forward_do_write"));
+    }
+
+
+  /* ---- send the body ------
+   */
+
+  if (conn->headcount == SH_HEADER_SIZE && conn->bytes_to_send > 0 &&
+      conn->buf != NULL)
+    {
+      bytesent           = write (conn->fd, &(conn->buf[conn->bytecount]), 
+				     conn->bytes_to_send - conn->bytecount);
+      if (bytesent >= 0 || errno == EINTR || errno == EAGAIN) 
+	{
+	  if (bytesent > 0) 
+	    conn->bytecount    += bytesent;
+	  if (conn->bytecount == conn->bytes_to_send) 
+	    {
+	      ++conn->pass;
+	      conn->state         = CONN_PAUSE;
+	      /* ------  HERE CALL check_protocol(conn) -------  */
+	      check_protocol(conn, SH_DO_WRITE);
+	    }
+	}
+      else
+	{
+	  goto conn_reset_w;
+	}
+    }
+      
+  else if (conn->headcount == SH_HEADER_SIZE && conn->bytes_to_send == 0)
+    {
+      ++conn->pass;
+      conn->state     = CONN_PAUSE;
+      /* fprintf(stderr, "\n**** FIXME null write ****\n\n"); */
+      /* ------  HERE CALL check_protocol(conn) -------  */
+      check_protocol(conn, SH_DO_WRITE);
+    }
+
+  if (conn->fd >= 0)
+    retry_fcntl (FIL__, __LINE__, conn->fd, F_SETFL,  flags);
+  SL_RETURN( (0), _("sh_forward_do_write"));
+
+ conn_reset_w:
+  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_RESET,
+		  conn->peer);
+  sh_forward_do_free ( conn );
+  SL_RETURN( (-1), _("sh_forward_do_write"));
+}
+
+/* accept a connection from a client
+ */ 
+#include <syslog.h>
+#ifdef SH_USE_LIBWRAP
+#include <tcpd.h>
+
+#ifndef ALLOW_SEVERITY 
+#define ALLOW_SEVERITY LOG_INFO
+#define DENY_SEVERITY  LOG_WARNING
+#endif
+
+int allow_severity;
+int deny_severity;
+#endif
+
+int sh_forward_accept (int sock, sh_conn_t * newconn)
+{
+  int                errflag;
+  int                rc;
+  struct sockaddr_in addr;
+#ifdef SH_USE_LIBWRAP
+  struct request_info request;
+  char                errbuf[128];
+  char                daemon[128];
+#endif
+  
+  /* handle AIX (size_t addrlen) in wrapper
+   */
+  int                addrlen = sizeof(addr);
+
+  SL_ENTER(_("sh_forward_accept"));
+
+  rc = retry_accept(FIL__, __LINE__, sock, 
+		    (struct sockaddr *) &addr, &addrlen);
+
+  if (rc >= 0)
+    {
+
+      if (addrlen == 0)
+	{
+	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
+			  _("Connecting entity unknown"), _("accept"));
+	  newconn->fd    = -1;
+	  newconn->state = CONN_FREE;
+	  close(rc);
+	  SL_RETURN( (-1), _("sh_forward_accept"));
+	}
+
+#ifdef SH_USE_LIBWRAP
+      sl_strlcpy(daemon, SH_INSTALL_NAME, 128);
+      request_init(&request, RQ_DAEMON, daemon, RQ_FILE, rc, 0);
+      fromhost(&request);
+      if (!hosts_access(&request)) 
+	{
+	  sl_strlcpy(errbuf, _("Refused connection from "), 128);
+	  sl_strlcat(errbuf,   eval_client(&request), 128);
+
+	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
+			  errbuf, _("libwrap"));
+	  newconn->fd    = -1;
+	  newconn->state = CONN_FREE;
+	  close(rc);
+	  SL_RETURN( (-1), _("sh_forward_accept"));
+	}
+#endif
+
+      memcpy (&(newconn->addr_peer), &addr, sizeof(struct sockaddr_in));
+
+      /* prepare for usage of connection
+       */
+      (void) retry_fcntl( FIL__, __LINE__, rc, F_SETFD, 1 );
+      newconn->fd           = rc;
+      newconn->state        = CONN_READING;
+      newconn->timer        = (unsigned long) time (NULL);
+
+      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_CNEW, newconn->fd);
+
+      SL_RETURN( (0), _("sh_forward_accept"));
+    }
+  else
+    {
+      errflag = errno;
+      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
+		      sh_error_message(errflag), _("accept"));
+      newconn->fd    = -1;
+      newconn->state = CONN_FREE;
+      SL_RETURN( (-1), _("sh_forward_accept"));
+    }
+}
+
+extern char sh_sig_msg[64];  /* defined in sh_unix.c */
+
+/* ------------  port and interface -------
+ */
+static unsigned int server_port = SH_DEFAULT_PORT;
+
+int sh_forward_set_port (char * str)
+{
+  int retval = 0;
+  unsigned long   i;
+  char * endptr = str;
+  
+  SL_ENTER(_("sh_forward_set_port"));
+  i = strtoul (str, &endptr, 0);
+  if (endptr == str) {
+    retval = -1;
+  } else if (i > 65535) {
+    retval = -1;
+  } else {
+    server_port = i;
+  }
+  SL_RETURN( (retval), _("sh_forward_set_port"));
+}
+
+static struct in_addr server_interface;
+static int            use_server_interface = 0;
+
+int sh_forward_set_interface (char * str)
+{
+  if (0 == strcmp(str, _("INADDR_ANY")))
+    {
+      use_server_interface = 0;
+      return 0;
+    }
+  if (0 == /*@-unrecog@*/inet_aton(str, &server_interface)/*@+unrecog@*/) 
+    {
+      use_server_interface = 0;
+      return -1;
+    }
+  use_server_interface = 1;
+  return 0;
+}
+
+/* ------------  print error --------------
+ */
+struct sock_err_st {
+  char msg[128];
+  int  errnum;
+  int  port;
+  int  line;
+  int  euid;
+};
+
+static struct sock_err_st sock_err[2];
+
+void sh_forward_printerr(char * str, int errnum, unsigned int port, int line)
+{
+  int slot = 0;
+
+  if (port != server_port)
+    slot = 1;
+  if (str == NULL)
+    sock_err[slot].msg[0] = '\0';
+  else
+    sl_strlcpy(sock_err[slot].msg, str, 128);
+  sock_err[slot].errnum = errnum;
+  sock_err[slot].port   = port;
+  sock_err[slot].line   = line;
+  sock_err[slot].euid   = (int) geteuid();
+}
+
+int sh_forward_printerr_final(int slot)
+{
+  SL_ENTER(_("sh_forward_printerr_final"));
+  if (sock_err[slot].msg[0] != '\0')
+    {
+      dlog(1, FIL__, __LINE__, 
+	   _("Could not set up the listening socket for the server because of the\nfollowing error: %s\nPossible reasons include:\n - insufficient privilege for UID %d, or\n - the port %d is already used by another program.\n"),
+	   sh_error_message(sock_err[slot].errnum), sock_err[slot].euid, 
+	   sock_err[slot].port);
+      sh_error_handle((-1), FIL__, sock_err[slot].line, 
+		      sock_err[slot].errnum, MSG_EXIT_ABORTS,
+		      sh_error_message(sock_err[slot].errnum),
+		      sh.prg_name,
+		      sock_err[slot].msg);
+      SL_RETURN((-1), _("sh_forward_printerr_final"));
+    }
+  SL_RETURN(0, _("sh_forward_printerr_final"));
+}
+
+static   sh_conn_t        * conns = NULL;
+#define  TIME_OUT_DEF 300
+static   int  maxconn = 0;  /* maximum number of simultaneous connections */
+
+
+#ifdef INET_SYSLOG
+#define INET_SUSPEND_TIME 180		/* equal to 3 minutes */
+#define SH_MINSOCK 3
+int create_syslog_socket (int flag);
+static int recv_syslog_socket   (int fd);
+static int syslog_sock = -1;
+#else
+#define SH_MINSOCK 2
+#endif
+
+extern int pf_unix_fd;
+
+/* the tcp socket, and the function to establish it
+ */
+static int sh_tcp_sock = -1;
+
+int sh_create_tcp_socket ()
+{
+  struct sockaddr_in addr;
+  int addrlen      = sizeof(addr);
+
+  int sock   = -1;
+  int errnum = 0;
+  int flag   = 1; /* non-zero to enable an option */
+
+  SL_ENTER(_("sh_create_tcp_socket"));
+
+  sh_forward_printerr (NULL, 0, server_port, __LINE__);
+
+  /* create the socket, bind() it and listen()
+   */
+  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
+    {
+      errnum = errno; 
+      sh_forward_printerr (_("socket"), errnum, server_port, __LINE__);
+      SL_RETURN((-1), _("sl_create_tcp_socket"));
+    }
+  (void) retry_fcntl( FIL__, __LINE__, sock, F_SETFD, 1 );
+  
+  if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
+		  (void *) &flag, sizeof(flag)) < 0 )
+    {
+      errnum = errno;
+      sh_forward_printerr (_("setsockopt"), errnum, server_port, __LINE__);
+      SL_RETURN((-1), _("sl_create_tcp_socket"));
+    }
+  
+  addr.sin_family      = AF_INET;
+  addr.sin_port        = htons(server_port);
+  if (use_server_interface == 0)
+    addr.sin_addr.s_addr = INADDR_ANY;
+  else
+    memcpy(&addr.sin_addr, &server_interface, sizeof(struct in_addr));
+  
+  if ( bind(sock, (struct sockaddr *) &addr, addrlen) < 0) 
+    {
+      errnum = errno;
+      sh_forward_printerr (_("bind"), errnum, server_port, __LINE__);
+      SL_RETURN((-1), _("sl_create_tcp_socket"));
+    }
+  
+  if ( retry_fcntl( FIL__, __LINE__, sock, F_SETFL, O_NONBLOCK ) < 0 )
+    {
+      errnum = errno;
+      sh_forward_printerr (_("fcntl"), errnum, server_port, __LINE__);
+      SL_RETURN((-1), _("sl_create_tcp_socket"));
+    }
+  
+  if ( listen(sock, 5) < 0)
+    {
+      errnum = errno;
+      sh_forward_printerr (_("listen"), errnum, server_port, __LINE__);
+      SL_RETURN((-1), _("sl_create_tcp_socket"));
+    }
+
+  sh_tcp_sock = sock;
+
+  SL_RETURN((sock), _("sl_create_tcp_socket"));
+}
+
+/*****************************************
+ *
+ * This is the server main loop.
+ *
+ * The server is set up for listening, and
+ * and starts a select() loop.
+ *
+ *****************************************/
+
+void sh_receive()
+{
+#ifdef SH_USE_XML
+  extern int  sh_log_file    (char * message, char * inet_peer);
+#endif
+
+  int                sock = -1;
+  sh_conn_t        * cx;
+  fd_set             readset;
+  fd_set             writeset;
+  struct timeval     tv;
+  int                num_sel;
+  int                errnum;
+  int                nowconn;
+  int                status;
+  int                high_fd;
+  register int       i;
+  long               dummy = 0;
+  unsigned long      time_now;
+  unsigned long      time_last = 0;
+  unsigned long      time_out = TIME_OUT_DEF;  
+  
+  time_t told;
+  time_t tcurrent;
+
+  unsigned long tchkold;
+
+  struct  sigaction  new_act;
+  struct  sigaction  old_act;
+  
+  SL_ENTER(_("sh_receive"));
+
+  /* ignore SIGPIPE (instead get EPIPE if connection is closed)
+   *      --- we have called sh_unix_init() already ---
+   */
+  new_act.sa_handler = SIG_IGN;
+  retry_sigaction (FIL__, __LINE__, SIGPIPE, &new_act, &old_act);
+
+  if ( sh_forward_printerr_final(0) < 0)
+    {
+      aud_exit(FIL__, __LINE__, EXIT_FAILURE);
+    }
+  sock = sh_tcp_sock;
+
+  /* ****************************************************************
+   *
+   * This is a non-forking server. We use select() on the listen()
+   * socket to watch for new connections. For new connections, accept()
+   * will return a new socket that is put in the read/write filesets.
+   * Data about active connections are kept in the 'conns' table. 
+   *
+   ******************************************************************/
+  
+  /* The table to hold info on sockets.
+   * We reserve 6 file descriptors for misc. use.
+   * The POSIX lower limit on open files seems to be eight. 
+   */
+  maxconn = get_open_max() - 6;
+  maxconn = (((int)FD_SETSIZE) < maxconn) ? FD_SETSIZE : maxconn;
+
+  conns   = SH_ALLOC (sizeof(sh_conn_t) * maxconn);
+
+  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_START_SRV,
+		  (maxconn-1), sock);
+
+  /* timer
+   */
+  tcurrent                   = (unsigned long) time (NULL);
+  told                       = tcurrent;
+
+  tchkold                    = tcurrent;
+  
+  for (i = SH_MINSOCK; i < maxconn; ++i)
+    {
+      conns[i].buf         = NULL;
+      conns[i].K           = NULL;
+      conns[i].A           = NULL;
+      conns[i].M1          = NULL;
+      conns[i].FileName    = NULL;
+      conns[i].fd          = -1;
+      sh_forward_do_free ( &conns[i]);
+    }
+  
+  /* status init
+   */
+  server_status.conn_open  = 0;
+  server_status.conn_total = 0;
+  server_status.conn_max   = maxconn-1;
+  server_status.start      = time (NULL);
+  server_status.last       = (time_t) 0;
+
+  nowconn    = 1;
+  tv.tv_sec  = 5;
+  tv.tv_usec = 0;
+  
+  /* conns[0] is the listen() socket. Always in read mode.
+   */
+  conns[0].fd    = sock;
+  conns[0].state = CONN_READING;
+  high_fd = sock;
+  
+  conns[1].fd    = pf_unix_fd;
+  conns[1].state = CONN_READING;
+  high_fd = (pf_unix_fd > high_fd) ? pf_unix_fd : high_fd;
+  
+#ifdef INET_SYSLOG
+  conns[2].fd = -1;
+  if ( sh_forward_printerr_final(1) < 0)
+    {
+      SH_FREE(conns);
+      conns = NULL;
+      aud_exit(FIL__, __LINE__, EXIT_FAILURE);
+    }
+  sock = syslog_sock;
+
+  if (sock >= 0)
+    {
+      conns[2].fd    = sock;
+      conns[2].state = CONN_READING;
+      high_fd = (high_fd > conns[2].fd) ? high_fd : conns[2].fd;
+    }
+#endif
+  
+  sh_html_write(all_clients);
+  
+  /* This is the select() loop.
+   */
+  while (1 == 1)
+    {
+
+    if (sig_raised > 0)
+      {
+	TPT((0, FIL__, __LINE__, _("msg=<Process a signal.>\n")))
+
+	if (sig_termfast == 1)  /* SIGTERM */
+	  {
+	    TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
+	    strncpy (sh_sig_msg, _("SIGTERM"), 20);
+	    --sig_raised; --sig_urgent;
+	    aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
+	  }
+	  
+	if (sig_config_read_again == 1)
+	  {
+	    TPT((0, FIL__, __LINE__, _("msg=<Re-read configuration.>\n")));
+	    sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_RECONF);
+
+
+	    /* -- Delete the name server cache. --
+	     */
+
+	    delete_cache();
+#if defined(WITH_EXTERNAL)
+	    /* -- Delete list of external tasks. --
+	     */
+	    (void) sh_ext_cleanup();
+#endif
+	    /* - mark all clients dead
+	     * - read configuration file
+	     * - remove clients still dead
+	     */
+	    sh_forward_mark_dead ();
+
+#if defined(SH_WITH_MAIL)
+	    reset_count_dev_mail();
+#endif
+	    reset_count_dev_console();
+	    reset_count_dev_time();
+	    sl_trust_purge_user();
+
+	    (void) sh_readconf_read ();
+	    for (i = SH_MINSOCK; i < maxconn; ++i)
+	      if (conns[i].state != CONN_FREE   && 
+		  conns[i].client_entry != NULL &&
+		  conns[i].client_entry->dead_flag == 1)
+		sh_forward_do_free ( &conns[i]);
+	    sh_forward_clean_tree ();
+
+	    sig_config_read_again = 0;
+	    --sig_raised;
+	  }
+
+	if (sig_fresh_trail == 1) /* SIGIOT */
+	  {
+	    /* Logfile access 
+	     */
+#ifdef SH_USE_XML
+	    sh_log_file (NULL, NULL);
+#endif
+	    TPT((0, FIL__, __LINE__, _("msg=<Logfile stop/restart.>\n")));
+	    sh_error_only_stderr (S_TRUE);
+	    sh_unix_rm_lock_file(sh.srvlog.name);
+	    retry_msleep(3, 0);
+	    sh.flag.log_start = S_TRUE;
+	    sh_error_only_stderr (S_FALSE);
+	    sig_fresh_trail       = 0;
+	    --sig_raised;
+	  }
+	
+	  
+	if (sig_terminate == 1 && nowconn < 2)  /* SIGQUIT */
+	  {
+	    TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
+	    strncpy (sh_sig_msg, _("SIGQUIT"), 20);
+	    --sig_raised; --sig_urgent;
+	    aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
+	  }
+	
+	  
+	if (sig_debug_switch == 1)  /* SIGUSR1 */
+	  {
+	    TPT((0, FIL__, __LINE__, _("msg=<Debug switch.>\n")));
+	    sh_error_dbg_switch();
+	    sig_debug_switch = 0;
+	    --sig_raised;
+	  }
+	
+	if (sig_suspend_switch == 1)  /* SIGUSR2 */
+	  {
+	    TPT((0, FIL__, __LINE__, _("msg=<Suspend switch.>\n")));
+	    if (sh_global_suspend_flag == 1) {
+	      sh_global_suspend_flag = 0;
+	    } else {
+	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_SUSPEND, 
+			      sh.prg_name);
+	      sh_global_suspend_flag = 1;
+	    }
+	    sig_suspend_switch = 0;
+	    --sig_raised; --sig_urgent;
+	  }
+
+	sig_raised = (sig_raised < 0) ? 0 : sig_raised;
+	sig_urgent = (sig_urgent < 0) ? 0 : sig_urgent;
+	TPT((0, FIL__, __LINE__, _("msg=<End signal processing.>\n")));
+      }
+      
+      if (sh_global_suspend_flag == 1)
+	{
+	  (void) retry_msleep (1, 0);
+	  continue;
+	}
+
+      /* Recompute the descriptor set. select() modifies it,
+       * thus we update it using the info from the connection table.
+       * Also recompute the number of open connections.
+       */
+      FD_ZERO( &readset );
+      FD_ZERO( &writeset );
+      FD_SET(conns[0].fd, &readset );
+      high_fd   = conns[0].fd;
+
+      if (conns[1].fd > -1)
+	{
+	  FD_SET(conns[1].fd, &readset );
+	  high_fd   = (high_fd > conns[1].fd) ? high_fd : conns[1].fd;
+	}
+
+#ifdef INET_SYSLOG
+      if (conns[2].fd > -1)
+	{
+	  FD_SET(conns[2].fd, &readset );
+	  high_fd   = (high_fd > conns[2].fd) ? high_fd : conns[2].fd;
+	}
+#endif
+
+      time_now  = (unsigned long) time (NULL);
+      nowconn   = 1;
+      
+      for (i = SH_MINSOCK; i < maxconn; ++i)
+	{
+	  /* eliminate timed out connections
+	   */
+	  if (conns[i].state != CONN_FREE) 
+	    {
+	      if (time_now-conns[i].timer > time_out)
+		{
+		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_TIMOUT,
+				  conns[i].peer);
+		  sh_forward_do_free ( &conns[i]);
+		}
+	      else
+		++nowconn;
+	    }
+	  
+	  
+	  if       (conns[i].state   == CONN_READING)
+	    { 
+	      FD_SET(conns[i].fd, &readset);
+	      high_fd = (high_fd < conns[i].fd ? conns[i].fd : high_fd);
+	    }
+	  else if  (conns[i].state   == CONN_SENDING)
+	    {
+	      FD_SET(conns[i].fd, &writeset);
+	      high_fd = (high_fd < conns[i].fd ? conns[i].fd : high_fd);
+	    }
+	}
+
+      /* -- Exponentially reduce timeout limit if more than 1/2 full. --
+       */
+      if (nowconn > (maxconn/2))
+	time_out = ( (time_out/2) > 1) ? (time_out/2) : 1;
+      else
+	time_out = TIME_OUT_DEF;
+      
+      
+      
+      /* -- Do the select(). --
+       */
+      num_sel = select(high_fd+1, &readset, &writeset, NULL, &tv);
+      errnum  = errno;
+      
+      /* reset timeout - modified by select() on some systems
+       */
+      tv.tv_sec  = 5;
+      tv.tv_usec = 0;
+      
+
+      if ( (time_now - time_last) > 2L)
+	{
+	  time_last = time_now;
+	  if (sh_html_write(all_clients) < 0)
+	    sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_HTML);
+	}
+      
+      
+      /* Error handling.
+       */
+      if ( num_sel < 0 )        /* some error             */
+	{
+	  if (sig_raised == 1)
+	    {
+	      sig_raised = 2;
+	      continue;
+	    }
+
+	  if ( errnum == EINTR)
+	    continue;	  /* try again              */
+
+	  if ( errnum == EBADF)
+	    {
+	      /* seek and destroy the bad fd
+	       */
+	      for (i = SH_MINSOCK; i < high_fd; ++i)
+		{
+		  if ((conns[i].state == CONN_READING) ||
+		      (conns[i].state == CONN_SENDING))
+		    {
+		      if (-1 == retry_fcntl(FIL__, __LINE__, 
+					    conns[i].fd, F_GETFL, dummy))
+			sh_forward_do_free ( &conns[i]);
+		    }
+		}
+	      continue;
+	    }
+
+	  sh_error_handle((-1), FIL__, __LINE__, errnum, MSG_EXIT_ABORTS,
+			  sh_error_message(errnum), 
+			  sh.prg_name,
+			  _("select"));
+	  aud_exit(FIL__, __LINE__,  EXIT_FAILURE );
+	}
+      
+
+      /* log the timestamp
+       */
+      if ((tcurrent - told) > sh.looptime )
+	{
+	  told = tcurrent;
+#ifdef MEM_DEBUG
+	  sh_mem_check();
+	  sh_unix_count_mlock();
+#else
+	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_STAMP);
+#endif
+	}
+
+#if defined(SH_WITH_MAIL)
+      /* 
+       * flush the mail queue
+       */
+      if (tcurrent - sh.mailTime.alarm_last > sh.mailTime.alarm_interval) 
+	{
+	  TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
+	  (void) sh_mail_msg (NULL);
+	  sh.mailTime.alarm_last = tcurrent;
+	}
+#endif
+#ifdef MEM_DEBUG
+      sh_mem_dump();
+#endif
+
+      tcurrent = (unsigned long) time (NULL);
+
+      /* select() timeout handling.
+       */
+      if ( num_sel == 0 )       /* timeout - no connection */ 
+	{
+	  if (sh_html_write(all_clients) < 0)
+	    sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_HTML);
+	  continue;
+	}
+
+      /* New connection.
+       */
+      if ( FD_ISSET(conns[0].fd , &readset )) /* a new connection   */
+	{
+	  --num_sel;
+	  status = 0;
+	  if (nowconn < maxconn && sig_terminate == 0 && sig_termfast == 0)
+	    {
+	      i = SH_MINSOCK;
+	      while (i < maxconn)
+		{
+		  if (conns[i].state == CONN_FREE)
+		    {
+		      status = sh_forward_accept (conns[0].fd, &conns[i]);
+		      if (status == 0)
+			{
+			  high_fd = 
+			    (high_fd > conns[i].fd ? high_fd : conns[i].fd);
+			  ++server_status.conn_open;
+			  ++server_status.conn_total;
+			  server_status.last = time (NULL);
+			}
+		      break;
+		    }
+		  ++i;
+		}
+	    }
+	  if (status == 0) 
+	    continue;
+	}
+      
+      /* check for commands on the socket
+       */
+      if (conns[1].fd > (-1) && FD_ISSET(conns[1].fd , &readset ))
+	{
+	  sh_socket_poll();
+	}
+
+#ifdef INET_SYSLOG
+      if (conns[2].fd > (-1) && FD_ISSET(conns[2].fd , &readset ))
+	{
+	  recv_syslog_socket (conns[2].fd);
+	}
+#endif
+
+      /* Check for pending read/write on the rest of the sockets.
+       */
+      for ( i = SH_MINSOCK; num_sel > 0 && i < maxconn; ++i )
+	{
+	  if (sig_termfast == 1)
+	    break;
+
+	  cx = &conns[i];
+	  if ( cx->state == CONN_READING &&
+	       FD_ISSET( cx->fd, &readset ) )
+	    {
+	      --num_sel;
+	      sh_forward_do_read ( cx );
+	    }
+	  else if ( cx->state == CONN_SENDING &&
+		    FD_ISSET( cx->fd, &writeset ) )
+	    {
+	      --num_sel;
+	      sh_forward_do_write ( cx );
+	    }
+	}
+
+      /* check for time limit exceeded
+       */
+      if ((tcurrent - tchkold) > (unsigned int) 3 )
+	{
+	  tchkold = tcurrent;
+	  client_time_check(/* all_clients */);
+	}
+      tcurrent = tcurrent;
+      
+      /* seed / re-seed the PRNG if required
+       */
+      (void) taus_seed();
+      
+    }
+  /* notreached */
+}
+
+void  free_client_tree ()
+{
+  SL_ENTER(_("free_client_tree"));
+  zAVLFreeTree (all_clients, free_client);
+  SL_RET0(_("free_client_tree"));
+}
+
+void sh_forward_free_all ()
+{
+  register int i;
+  
+  SL_ENTER(_("sh_forward_free_all"));
+
+  if (conns != NULL)
+    for (i = SH_MINSOCK; i < maxconn; ++i)
+      {
+	sh_forward_do_free ( &conns[i]);
+      }
+
+
+  free_client_tree ();
+
+  if (conns != NULL)
+    SH_FREE (conns);
+
+  SL_RET0(_("sh_forward_free_all"));
+}
+
+#ifdef INET_SYSLOG
+
+#ifdef HAVE_INET_ATON
+static char * my_inet_ntoa(struct in_addr in)
+{
+  return inet_ntoa(in);
+}
+#else
+static char * my_inet_ntoa(struct in_addr in)
+{
+  unsigned char a, b, c, d;
+  static char   foo[16];
+  char          bar[4];
+  memcpy (bar, &(in.s_addr), 4); /* memory alignment (?) */
+  memcpy (&a, &bar[0], 1);
+  memcpy (&b, &bar[1], 1);
+  memcpy (&c, &bar[2], 1);
+  memcpy (&d, &bar[3], 1);
+  sprintf(foo, "%d.%d.%d.%d",                       /* known to fit  */
+	  (int) a, (int) b, (int) c, (int) d);
+  return foo;
+}
+#endif
+
+
+/* Unlike Linux / FreeBSD, most systems don't define the stuff below
+ * in syslog.h
+ */
+
+#ifndef LOG_FAC
+#define LOG_FAC(p)      (((p) & LOG_FACMASK) >> 3)
+#endif
+
+#ifndef LOG_PRI
+#define LOG_PRI(p)      ((p) & LOG_PRIMASK)
+#endif
+
+typedef struct sh_code {
+        char    *c_name;
+        int     c_val;
+} SH_CODE;
+
+SH_CODE sh_facilitynames[] =
+{
+#ifdef LOG_AUTH
+  { N_("auth"), LOG_AUTH },
+#endif
+#ifdef LOG_AUTHPRIV 
+  { N_("authpriv"), LOG_AUTHPRIV },
+#endif
+#ifdef LOG_CRON
+  { N_("cron"), LOG_CRON },
+#endif
+#ifdef LOG_DAEMON
+  { N_("daemon"), LOG_DAEMON },
+#endif
+#ifdef LOG_FTP
+  { N_("ftp"), LOG_FTP },
+#endif
+#ifdef LOG_KERN
+  { N_("kern"), LOG_KERN },
+#endif
+#ifdef LOG_LPR
+  { N_("lpr"), LOG_LPR },
+#endif
+#ifdef LOG_MAIL
+  { N_("mail"), LOG_MAIL },
+#endif
+#ifdef INTERNAL_MARK
+  { N_("mark"), INTERNAL_MARK },          /* INTERNAL */
+#endif
+#ifdef LOG_NEWS
+  { N_("news"), LOG_NEWS },
+#endif
+#ifdef LOG_AUTH
+  { N_("security"), LOG_AUTH },           /* DEPRECATED */
+#endif
+#ifdef LOG_SYSLOG
+  { N_("syslog"), LOG_SYSLOG },
+#endif
+#ifdef LOG_USER
+  { N_("user"), LOG_USER },
+#endif
+#ifdef LOG_UUCP
+  { N_("uucp"), LOG_UUCP },
+#endif
+#ifdef LOG_LOCAL0
+  { N_("local0"), LOG_LOCAL0 },
+#endif
+#ifdef LOG_LOCAL1
+  { N_("local1"), LOG_LOCAL1 },
+#endif
+#ifdef LOG_LOCAL2 
+  { N_("local2"), LOG_LOCAL2 },
+#endif
+#ifdef LOG_LOCAL3
+  { N_("local3"), LOG_LOCAL3 },
+#endif
+#ifdef LOG_LOCAL4
+  { N_("local4"), LOG_LOCAL4 },
+#endif
+#ifdef LOG_LOCAL5
+  { N_("local5"), LOG_LOCAL5 },
+#endif
+#ifdef LOG_LOCAL6
+  { N_("local6"), LOG_LOCAL6 },
+#endif
+#ifdef LOG_LOCAL7
+  { N_("local7"), LOG_LOCAL7 },
+#endif
+  { NULL, -1 }
+};
+ 
+
+SH_CODE sh_prioritynames[] =
+{  
+#ifdef LOG_ALERT
+  { N_("alert"), LOG_ALERT },
+#endif
+#ifdef LOG_CRIT
+  { N_("crit"), LOG_CRIT },
+#endif
+#ifdef LOG_DEBUG
+  { N_("debug"), LOG_DEBUG },
+#endif
+#ifdef LOG_EMERG
+  { N_("emerg"), LOG_EMERG },
+#endif
+#ifdef LOG_ERR
+  { N_("err"), LOG_ERR },
+#endif
+#ifdef LOG_ERR
+  { N_("error"), LOG_ERR },               /* DEPRECATED */
+#endif
+#ifdef LOG_INFO
+  { N_("info"), LOG_INFO },
+#endif
+#ifdef INTERNAL_NOPRI
+  { N_("none"), INTERNAL_NOPRI },         /* INTERNAL */
+#endif
+#ifdef LOG_NOTICE
+  { N_("notice"), LOG_NOTICE },
+#endif
+#ifdef LOG_EMERG
+  { N_("panic"), LOG_EMERG },             /* DEPRECATED */
+#endif
+#ifdef LOG_WARNING
+  { N_("warn"), LOG_WARNING },            /* DEPRECATED */
+#endif
+#ifdef LOG_WARNING
+  { N_("warning"), LOG_WARNING },
+#endif
+  { NULL, -1 }
+};
+
+static int enable_syslog_socket = S_FALSE;
+
+static int recv_syslog_socket (int fd)
+{
+  static time_t      return_next = 0;
+  int                priority = 0;
+  int                fac, pri;
+  int                i;
+  char             * cfac = NULL;
+  char             * cpri = NULL;
+  int                res;
+  char             * tmp;
+  char             * bptr;
+  char             * ptr = NULL;
+  char               buf[1048];
+  struct sockaddr_in from;
+
+  /* The 6th argument in recvfrom is *socklen_t in Linux and *BSD, 
+   * but *int everywhere else. Because socklen_t is unsigned int, there
+   * should be no problem as long as  sizeof(struct sockaddr_in) < INT_MAX ...
+   */
+  int                fromlen = sizeof(from);
+
+  if (enable_syslog_socket == S_FALSE)
+    return 0;
+
+  SL_ENTER(_("recv_syslog_socket"));
+
+  if (return_next > 0)
+    {
+      if ( (time(NULL) - return_next) < 2)
+	SL_RETURN( 0, _("recv_syslog_socket"));
+      else
+	return_next = 0;
+    }
+
+  res = recvfrom(fd,  buf,  1047, 0, (struct sockaddr *) &from, &fromlen);
+
+  if (res > 0)
+    {
+      res = (res < 1047) ? res : 1047; 
+      buf[res] = '\0';
+      if (res > 1 && buf[res-1] == '\n')
+	buf[res-1] = '\0';
+
+      /* here we expect an xml formatted message, thus we don't
+	 escape xml special chars (flag == 0) */
+      /* commented out to not escape twice    */
+      /* bptr = sh_tools_safe_name(buf, 0);   */
+      bptr = buf;
+
+      if (!bptr || !(*bptr))
+	{
+	  res = errno;
+	  TPT(( 0, FIL__, __LINE__, _("msg=<UDP error: %d>\n"), res));
+	  sh_error_handle((-1), FIL__, __LINE__, res, MSG_ERR_SYSLOG,
+			  sh_error_message(res), my_inet_ntoa(from.sin_addr));
+	  SL_RETURN( (-1), _("recv_syslog_socket"));
+	}      
+
+      TPT(( 0, FIL__, __LINE__, _("msg=<UDP message from %s>\n"),
+	    my_inet_ntoa(from.sin_addr)));
+      ptr = bptr;
+      i = 0;
+      if (*ptr == '<') 
+	{
+	  ++ptr; ++i;
+	  while (i < res &&
+		 (unsigned char) *ptr > 47 && (unsigned char) *ptr < 58)
+	    {
+	      priority = 10 * priority + (*ptr - '0');
+	      ++ptr;
+	      ++i;
+	    }
+	  if (*ptr == '>')
+	    ++ptr;
+	}
+      fac = LOG_FAC(priority);
+      i = 0; 
+      while (sh_facilitynames[i].c_name != NULL)
+	{
+	  if (sh_facilitynames[i].c_val == (fac<<3))
+	    { cfac = sh_util_strdup(_(sh_facilitynames[i].c_name)); break; }
+	  ++i;
+	}
+      pri = LOG_PRI(priority);
+      i = 0; 
+      while (sh_prioritynames[i].c_name != NULL)
+	{
+	  if (sh_prioritynames[i].c_val == pri)
+	    { cpri = sh_util_strdup(_(sh_prioritynames[i].c_name)); break; }
+	  ++i;
+	}
+
+      /* here we do not expect an xml formatted message, thus we escape
+	 xml special chars (flag == 1) */
+      tmp = sh_tools_safe_name (ptr, 1);
+      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_INET_SYSLOG,
+		      my_inet_ntoa(from.sin_addr), 
+		      (cfac == NULL) ? _("none") : cfac, 
+		      (cpri == NULL) ? _("none") : cpri, 
+		      (ptr  == NULL) ? _("none") : ptr);
+      if (cfac != NULL)
+	SH_FREE(cfac);
+      if (cpri != NULL)
+	SH_FREE(cpri);
+      SH_FREE(tmp);
+      /* SH_FREE(bptr); */
+    }
+
+  else if (res < 0 && errno != EINTR)
+    {
+      res = errno;
+      TPT(( 0, FIL__, __LINE__, _("msg=<UDP error: %d>\n"), res));
+      sh_error_handle((-1), FIL__, __LINE__, res, MSG_ERR_SYSLOG,
+		      sh_error_message(res), my_inet_ntoa(from.sin_addr));
+
+      /* don't accept anything the next 2 seconds
+       */
+      return_next = time(NULL);
+      SL_RETURN( (-1), _("recv_syslog_socket"));
+    }      
+  SL_RETURN( (0), _("recv_syslog_socket"));
+}
+
+int set_syslog_active(char * c)
+{
+  return sh_util_flagval(c, &enable_syslog_socket);
+}
+
+/* callerFlag == S_TRUE means override the enable_syslog_socket flag
+ */
+int create_syslog_socket (int callerFlag)
+{
+  int                flag = 1;  /* non-zero to enable an option */
+  int sock;
+  int errnum;
+  int res;
+  struct sockaddr_in addr;
+  int addrlen      = sizeof(addr);
+
+  SL_ENTER(_("create_syslog_socket"));
+
+  if (callerFlag == S_FALSE)
+    {
+      if (enable_syslog_socket == S_FALSE && syslog_sock >= 0)
+	{
+	  /* user does not wish to use this facility
+	   */
+	  TPT(( 0, FIL__, __LINE__, _("msg=<close syslog socket>\n")));
+	  close(syslog_sock);
+	  syslog_sock = -1;
+	}
+      SL_RETURN((-1), _("create_syslog_socket"));
+    }
+
+  sh_forward_printerr (NULL, 0, 514, __LINE__);
+
+  /* create the socket, bind() it and listen()
+   */
+  sock = socket(AF_INET, SOCK_DGRAM, 0);
+
+  if (sock < 0)
+    {
+      errnum = errno; 
+      sh_forward_printerr (_("syslog socket"), errnum, 514, __LINE__);
+      SL_RETURN((-1), _("create_syslog_socket"));
+    }
+  (void) retry_fcntl( FIL__, __LINE__, sock, F_SETFD, 1 );
+  
+  if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
+		  (void *) &flag, sizeof(flag)) < 0 )
+    {
+      errnum = errno;
+      sh_forward_printerr (_("syslog setsockopt SO_REUSEADDR"), 
+			   errnum, 514, __LINE__);
+      SL_RETURN((-1), _("create_syslog_socket"));
+    }
+
+#if defined(SO_BSDCOMPAT)
+  if ( setsockopt(sock, SOL_SOCKET, SO_BSDCOMPAT,
+		  (void *) &flag, sizeof(flag)) < 0 )
+    {
+      errnum = errno;
+      sh_forward_printerr (_("syslog setsockopt SO_BSDCOMPAT"), 
+			   errnum, 514, __LINE__);
+      SL_RETURN((-1), _("create_syslog_socket"));
+    }
+#endif
+  
+  memset(&addr, 0, sizeof(addr));
+  addr.sin_family      = AF_INET;
+  addr.sin_port        = htons(514);
+  
+  res = bind(sock, (struct sockaddr *) &addr, addrlen);
+
+  if ( res < 0) 
+    {
+      errnum = errno;
+      sh_forward_printerr (_("syslog bind"), errnum, 514, __LINE__);
+      close(sock);
+      SL_RETURN((-1), _("create_syslog_socket"));
+    }
+
+  syslog_sock = sock;
+
+  SL_RETURN((sock), _("create_syslog_socket"));
+}
+/* #ifdef INET_SYSLOG */
+#endif
+
+
+
+/* #ifdef SH_WITH_SERVER */
+#endif
+
+
+  
+
+
+
Index: trunk/src/sh_getopt.c
===================================================================
--- trunk/src/sh_getopt.c	(revision 591)
+++ trunk/src/sh_getopt.c	(revision 1)
@@ -30,14 +30,9 @@
 #include "sh_error.h"
 #include "sh_getopt.h"
-#include "sh_unix.h"
 #include "sh_files.h"
 #include "sh_utils.h"
 #include "sh_mail.h"
-#include "sh_xfer.h"
+#include "sh_forward.h"
 #include "sh_hash.h"
-#include "sh_dbIO.h"
-#include "sh_dbCheck.h"
-#include "sh_dbCreate.h"
-#include "sh_sem.h"
 
 #if defined(WITH_EXTERNAL)
@@ -45,5 +40,5 @@
 #endif
 
-extern int      sh_calls_set_bind_addr (const char *);
+extern int      sh_calls_set_bind_addr (char *);
 
 #undef  FIL__
@@ -57,19 +52,17 @@
 
 typedef struct options {
-  const char * longopt;
+  char * longopt;
   const char   shortopt;
-  const char * usage;
+  char * usage;
   int          hasArg;
-  int (*func)(const char * opt);
+  int (*func)(char * opt);
 } opttable_t;
 
 /*@noreturn@*/
-static int sh_getopt_usage (const char * dummy);
+static int sh_getopt_usage (char * dummy);
 #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) 
-static int sh_getopt_forever (const char * dummy);
-static int sh_getopt_outpath (const char * dummy);
-#endif
-static int sh_getopt_copyright (const char * dummy);
-static int sh_getopt_version (const char * dummy);
+static int sh_getopt_forever (char * dummy);
+#endif
+static int sh_getopt_copyright (char * dummy);
 
 static opttable_t op_table[] = {
@@ -86,21 +79,4 @@
     HAS_ARG_NO, 
     sh_util_set_interactive },
-  { N_("listfile"),  
-    '-', 
-    N_("Run update with listfile"),  
-    HAS_ARG_YES, 
-    sh_util_update_file },
-#endif
-#if defined(SH_WITH_SERVER) || defined(SH_WITH_CLIENT)
-  { N_("server-port"),  
-    '-', 
-    N_("Set the server port to connect to"),  
-    HAS_ARG_YES, 
-    sh_xfer_server_port },
-  { N_("server-host"),  
-    '-', 
-    N_("Set the server host to connect to"),  
-    HAS_ARG_YES, 
-    sh_xfer_set_logserver },
 #endif
 #ifdef SH_WITH_SERVER
@@ -114,5 +90,5 @@
     N_("Log fully qualified name of client host"),  
     HAS_ARG_NO, 
-    sh_xfer_set_strip },
+    sh_forward_set_strip },
   { N_("chroot"),  
     '-', 
@@ -136,5 +112,5 @@
     HAS_ARG_YES, 
     sh_calls_set_bind_addr },
-#if defined(SH_WITH_SERVER) || defined(SH_WITH_CLIENT)
+#ifdef SH_WITH_CLIENT
   { N_("set-export-severity"),  
     'e', 
@@ -227,10 +203,10 @@
     N_("Compute a client registry entry for password"),  
     HAS_ARG_YES, 
-    sh_xfer_make_client },
+    sh_forward_make_client },
   { N_("gen-password"),  
     'G', 
     N_("Generate a random password"),  
     HAS_ARG_NO, 
-    sh_xfer_create_password },
+    sh_forward_create_password },
 #endif
 
@@ -241,64 +217,24 @@
     HAS_ARG_NO, 
     sh_getopt_forever},
-  { N_("outfile"),  
-    'o', 
-    N_("Pathname for output file"),  
-    HAS_ARG_YES, 
-    sh_getopt_outpath},
-  { N_("list-file"),  
-    '-', 
-    N_("Modify -d to show content of a single file"),  
-    HAS_ARG_YES, 
-    set_list_file},
-  { N_("list-filter"),  
-    '-', 
-    N_("Modify -d to filter by file list given in text file"),  
-    HAS_ARG_YES, 
-    sh_dbIO_list_filter},
   { N_("full-detail"),  
     'a', 
-    N_("Modify -d to show full details"),  
+    N_("Modify -d to list full details"),  
     HAS_ARG_NO, 
     set_full_detail},
   { N_("delimited"),  
     '-', 
-    N_("Modify -d to show full details, comma delimited"),  
+    N_("Modify -d to list full details, comma delimited"),  
     HAS_ARG_NO, 
     set_list_delimited},
-  { N_("binary"),  
-    '-', 
-    N_("Modify -d to output in binary database format"),  
-    HAS_ARG_NO, 
-    sh_dbIO_list_binary},
   { N_("list-database"),  
     'd', 
     N_("List database content (like ls -l)"),  
     HAS_ARG_YES, 
-    sh_dbIO_list_db},
+    sh_hash_list_db},
   { N_("init2stdout"),  
     '-', 
     N_("Write database to stdout on init"),  
     HAS_ARG_NO, 
-    sh_dbIO_writeout_stdout},
-  { N_("verify-database"),  
-    '-', 
-    N_("Verify the given database"),  
-    HAS_ARG_YES, 
-    sh_dbCheck_verify},
-  { N_("create-database"),  
-    '-', 
-    N_("Create database from file list"),  
-    HAS_ARG_YES, 
-    sh_dbCreate},
-  { N_("init-rootfs"),  
-    '-', 
-    N_("Build database based on another rootfs"),  
-    HAS_ARG_YES, 
-    sh_dbIO_init_rootfs},
-  { N_("wait-on-check"),  
-    'w', 
-    N_("Timed wait for end of filecheck (0 for no timeout)"),  
-    HAS_ARG_YES, 
-    sh_sem_wait},
+    sh_hash_pushdata_stdout},
 #endif
   { N_("trace-logfile"),  
@@ -322,37 +258,4 @@
     HAS_ARG_NO, 
     sh_getopt_usage },
-  { N_("version"),  
-    'v', 
-    N_("Show version and compiled-in options"),  
-    HAS_ARG_NO, 
-    sh_getopt_version },
-#if defined(HAVE_LIBPRELUDE)
-  /* need to skip over these */
-  { N_("prelude"),  
-    '-', 
-    N_("Prelude generic options"),  
-    HAS_ARG_NO, 
-    NULL },
-  { N_("profile"),  
-    '-', 
-    N_("Profile to use for this analyzer"),  
-    HAS_ARG_YES, 
-    NULL },
-  { N_("heartbeat-interval"),  
-    '-', 
-    N_("Number of seconds between two heartbeats"),  
-    HAS_ARG_YES, 
-    NULL },
-  { N_("server-addr"),  
-    '-', 
-    N_("Address where this sensor should report to"),  
-    HAS_ARG_YES, 
-    NULL },
-  { N_("analyzer-name"),  
-    '-', 
-    N_("Name for this analyzer"),  
-    HAS_ARG_YES, 
-    NULL },
-#endif
   /* last entry -- required !! -- */
   { NULL, 
@@ -363,332 +266,58 @@
 };
 
-
-static void sh_getopt_print_log_facilities (void)
+static int sh_getopt_copyright (char * dummy)
 {
-  int num = 0;
-
-  fputs (_("Compiled-in log facilities:\n"), stdout);
-
-#ifndef DEFAULT_CONSOLE
-  if (num > 0) fputc ('\n', stdout);
-  printf ("%s", _(" console (/dev/console)")); ++num;
-#else
-  if (num > 0) fputc ('\n', stdout);
-  if (0 == strcmp (DEFAULT_CONSOLE, _("NULL")))
-    { printf ("%s", _("console (/dev/console)"));  ++num; }
-  else
-    { printf (_("console (%s)"), DEFAULT_CONSOLE);  ++num; }
-#endif
-  if (num > 0) fputc ('\n', stdout);
-  fputs  (_(" syslog"), stdout); ++num;
-  if (num > 0) fputc ('\n', stdout);
-  printf (_(" logfile (%s)"), DEFAULT_ERRFILE); ++num;
-
-#if defined(WITH_EXTERNAL)
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" external program"), stdout); ++num;
-#endif
-
-#if defined(WITH_MESSAGE_QUEUE)
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" message queue"), stdout); ++num;
-#endif
- 
-#if defined(WITH_DATABASE)
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" database"), stdout); ++num;
-#ifdef WITH_ODBC
-  fputs (_(" (odbc)"), stdout);
-#endif
-#ifdef WITH_ORACLE
-  fputs (_(" (Oracle)"), stdout);
-#endif
-#ifdef WITH_POSTGRES
-  fputs (_(" (PostgreSQL)"), stdout);
-#endif
-#ifdef WITH_MYSQL 
-  fputs (_(" (MySQL)"), stdout);
-#endif
-#endif
-
-#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" server"), stdout); ++num;
-#endif
-
-#if defined(SH_WITH_MAIL)
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" email"), stdout); ++num;
-#endif
-
-#ifdef HAVE_LIBPRELUDE
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" prelude (0.9.6+)"), stdout); ++num;
-#endif
-
-  if (num == 0)
-    fputs (_(" none"), stdout);
-  fputc ('\n', stdout);
-  return;
-}
-
-static void sh_getopt_print_options (void)
-{
-  int num = 0;
-
-
-#if defined(SH_STANDALONE)
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_("Standalone executable"), stdout); ++num;
-#endif
-#if defined(SH_WITH_CLIENT)
-  if (num > 0) fputc ('\n', stdout);
-  printf (_("Client executable (port %d)"), SH_DEFAULT_PORT); ++num;
-#endif
-#if defined(SH_WITH_SERVER)
-  if (num > 0) fputc ('\n', stdout);
-  printf (_("Server executable (port %d, user %s)"), 
-	  SH_DEFAULT_PORT, DEFAULT_IDENT); 
-  ++num;
-#endif
-#if defined(USE_IPVX)
-  fputs (_(", IPv6 supported"), stdout);
-#endif
-
-  fputs (_(", compiled-in options:"), stdout);
-
-#if defined(USE_SYSTEM_MALLOC)
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" using system malloc"), stdout); ++num;
-#else
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" using dnmalloc"), stdout); ++num;
-#endif
-
-#if defined(HAVE_EGD_RANDOM)
-  if (num > 0) fputc ('\n', stdout);
-  printf (_(" using entropy gathering daemon (%s)"), EGD_SOCKET_NAME); ++num;
-#endif
-#if defined(HAVE_UNIX_RANDOM)
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" using unix entropy gatherer"), stdout); ++num;
-#endif
-#if defined(HAVE_URANDOM)
-  if (num > 0) fputc ('\n', stdout);
-  printf (_(" using entropy device (%s)"), NAME_OF_DEV_RANDOM); ++num;
-#endif
-
-#ifdef WITH_GPG
-  if (num > 0) fputc ('\n', stdout);
-  printf (_(" GnuPG signatures (%s)"), DEFAULT_SIG_PATH); ++num;
-#ifdef HAVE_SIG_CHECKSUM
-  if (num > 0) fputc ('\n', stdout);
-  printf (_("   -- GnuPG checksum:  %s"), SIG_HASH); ++num;
-#endif
-#ifdef USE_FINGERPRINT
-  if (num > 0) fputc ('\n', stdout);
-  printf (_("   -- Key fingerprint: %s"), SH_GPG_FP); ++num;
-#endif
-#endif
-
-#if defined(SH_EVAL_SHELL)
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" shell expansion in configuration file supported"), stdout); ++num;
-#endif
-
-#if defined(SL_DEBUG)
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" debug build (do not use for production)"), stdout); ++num;
-#endif
-#if defined(SCREW_IT_UP)
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" anti-debugger"), stdout); ++num;
-#endif
-#if defined(SH_USE_XML)
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" xml log format"), stdout); ++num;
-#endif
-#if defined(HAVE_NTIME)
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" using time server"), stdout); ++num;
-#endif
-#if defined(HAVE_REGEX_H)
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" posix regex support"), stdout); ++num;
-#endif
-
-
-#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
-#if defined(HAVE_LIBZ)
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" optionally store full text for files"), stdout); ++num;
-#endif
-#if !defined(SH_COMPILE_STATIC) && defined(__linux__) && defined(HAVE_AUPARSE_H) && defined(HAVE_AUPARSE_LIB)
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" optionally report auditd record of changed file"), stdout); ++num;
-#endif
-#if defined(USE_XATTR)
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" check SELinux attributes"), stdout); ++num;
-#endif
-#if defined(USE_ACL)
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" check Posix ACLs"), stdout); ++num;
-#endif
-#if defined(RELOAD_DATABASE)
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" fetch database on reload"), stdout); ++num;
-#endif
-#endif
-
-#if defined(SH_WITH_SERVER)
-
-#if !defined(HAVE_GETPEEREID) && !defined(SO_PEERCRED) && !defined(HAVE_STRUCT_CMSGCRED) && !defined(HAVE_STRUCT_FCRED) && !(defined(HAVE_STRUCT_SOCKCRED) && defined(LOCAL_CREDS))
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" command socket authentication: use SetSocketPassword"), stdout); 
-  ++num;
-#else
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" command socket authentication: use SetSocketAllowUID"), stdout); 
-  ++num;
-#endif
-
-#if defined(SH_USE_LIBWRAP)
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" support tcp wrapper"), stdout); ++num;
-#endif
-#if defined(INET_SYSLOG)
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" support listening on 514/udp (syslog)"), stdout); ++num;
-#endif
-#endif
-
-  if (num == 0)
-    fputs (_(" none"), stdout);
-  fputc ('\n', stdout);
-  return;
-}
-
-static void sh_getopt_print_modules (void)
-{
-#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) 
-  int num = 0;
-  
-  fputs (_("Compiled-in modules:\n"), stdout);
-#ifdef SH_USE_UTMP
-  if (num > 0) fputc (',', stdout);
-  fputs (_(" login/logout"), stdout); ++num;
-#endif
-#ifdef SH_USE_MOUNTS
-  if (num > 0) fputc (',', stdout);
-  fputs (_(" mount options"), stdout); ++num;
-#endif
-#ifdef SH_USE_USERFILES
-  if (num > 0) fputc (',', stdout);
-  fputs (_(" userfiles"), stdout); ++num;
-#endif
-#ifdef SH_USE_KERN
-  if (num > 0) fputc (',', stdout);
-  fputs (_(" kernel"), stdout); ++num;
-#endif
-#ifdef SH_USE_SUIDCHK
-  if (num > 0) fputc (',', stdout);
-  fputs (_(" suid"), stdout); ++num;
-#endif
-#ifdef SH_USE_PROCESSCHECK
-  if (num > 0) fputc (',', stdout);
-  fputs (_(" processes"), stdout); ++num;
-#endif
-#ifdef SH_USE_PORTCHECK
-  if (num > 0) fputc (',', stdout);
-  fputs (_(" ports"), stdout); ++num;
-#endif
-#ifdef USE_LOGFILE_MONITOR
-  if (num > 0) fputc (',', stdout);
-  fputs (_(" logfile monitor"), stdout); ++num;
-#endif
-#if defined(USE_REGISTRY_CHECK)
-  if (num > 0) fputc ('\n', stdout);
-  fputs (_(" Windows registry"), stdout); ++num;
-#endif
-  if (num == 0)
-    fputs (_(" none"), stdout);
-  fputc ('\n', stdout);
-#endif
-  return;
-}
-
-static int sh_getopt_version (const char * dummy)
-{
-  (void) dummy;
-  fprintf (stdout,
-	   _("This is samhain (%s), "\
-	     "(c) 1999-2024 Rainer Wichmann (http://la-samhna.de).\n"),
-	   VERSION);
-  fprintf (stdout, "%s",_("This software comes with ABSOLUTELY NO WARRANTY. "));
-  fprintf (stdout, "%s",_("Use at own risk.\n\n"));
-
-  sh_getopt_print_log_facilities ();
-  sh_getopt_print_modules ();
-  sh_getopt_print_options ();
-
-  _exit (EXIT_SUCCESS);
-  /*@notreached@*/
-  return 0; /* make compilers happy */
-}
-static int sh_getopt_copyright (const char * dummy)
-{
-  fprintf (stdout, "%s",
-	   _("Copyright (C) 1999-2019 Rainer Wichmann"\
+  fprintf (stdout, 
+	   _("Copyright (C) 1999-2005 Rainer Wichmann"\
 	     " (http://la-samhna.de).\n\n"));
 
-  fprintf (stdout, "%s",
+  fprintf (stdout, 
 	   _("This program is free software; "\
 	     "you can redistribute it and/or modify\n"));
-  fprintf (stdout, "%s",_("it under the terms of the GNU General "\
+  fprintf (stdout, _("it under the terms of the GNU General "\
 		     "Public License as published by\n"));
-  fprintf (stdout, "%s",_("the Free Software Foundation; either version 2 "\
+  fprintf (stdout, _("the Free Software Foundation; either version 2 "\
 		     "of the License, or\n"));
-  fprintf (stdout, "%s",_("(at your option) any later version.\n\n"));
-
-  fprintf (stdout, "%s",_("This program is distributed in the hope "\
+  fprintf (stdout, _("(at your option) any later version.\n\n"));
+
+  fprintf (stdout, _("This program is distributed in the hope "\
 		     "that it will be useful,\n"));
-  fprintf (stdout, "%s",_("but WITHOUT ANY WARRANTY; "\
+  fprintf (stdout, _("but WITHOUT ANY WARRANTY; "\
 		     "without even the implied warranty of\n"));
-  fprintf (stdout, "%s",_("MERCHANTABILITY or FITNESS FOR A PARTICULAR "\
-		     "PURPOSE. See the\n"));
-  fprintf (stdout, "%s",_("GNU General Public License for more details.\n\n"));
-
-  fprintf (stdout, "%s",_("You should have received a copy of the "\
+  fprintf (stdout, _("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."\
+		     " See the\n"));
+  fprintf (stdout, _("GNU General Public License for more details.\n\n"));
+
+  fprintf (stdout, _("You should have received a copy of the "\
 		     "GNU General Public License\n"));
-  fprintf (stdout, "%s",_("along with this program; "\
+  fprintf (stdout, _("along with this program; "\
 		     "if not, write to the Free Software\n"));
-  fprintf (stdout, "%s",_("Foundation, Inc., 59 Temple Place - Suite 330, "\
+  fprintf (stdout, _("Foundation, Inc., 59 Temple Place - Suite 330, "\
 		     "Boston, MA  02111-1307, USA.\n\n"));
 
-  fprintf (stdout, "%s",_("This product makes use of the reference "\
-		     "implementation of the TIGER message\n"));
-  fprintf (stdout, "%s",_("digest algorithm. This code is copyright Eli Biham "\
+  fprintf (stdout, _("This product makes use of the reference implementation "\
+		     "of the TIGER message\n"));
+  fprintf (stdout, _("digest algorithm. This code is copyright Eli Biham "\
 		     "(biham@cs.technion.ac.il)\n"));
-  fprintf (stdout, "%s",_("and Ross Anderson (rja14@cl.cam.ac.uk). It can be used "\
+  fprintf (stdout, _("and Ross Anderson (rja14@cl.cam.ac.uk). It can be used "\
 		     "freely without any\n"));
-  fprintf (stdout, "%s",_("restrictions.\n"));
+  fprintf (stdout, _("restrictions.\n"));
 #if defined(USE_SRP_PROTOCOL) && !defined(SH_STANDALONE)
 #if (!defined(HAVE_LIBGMP) || !defined(HAVE_GMP_H))
-  fprintf (stdout, "%s",_("This product makes use of the 'bignum' library by "\
+  fprintf (stdout, _("This product makes use of the 'bignum' library by "\
 		     "Henrik Johansson\n"));
-  fprintf (stdout, "%s",_("(Henrik.Johansson@Nexus.Comm.SE). If you are "\
-		     "including this library in a\n"));
-  fprintf (stdout, "%s",_("commercial product, be sure to distribute ALL of"\
+  fprintf (stdout, _("(Henrik.Johansson@Nexus.Comm.SE). If you are including "\
+		     "this library in a\n"));
+  fprintf (stdout, _("commercial product, be sure to distribute ALL of"\
 		     " it with the product.\n"));
 #endif
-  fprintf (stdout, "%s",_("This product uses the 'Secure Remote Password' "\
+  fprintf (stdout, _("This product uses the 'Secure Remote Password' "\
 		     "cryptographic\n"));
-  fprintf (stdout, "%s",_("authentication system developed by Tom Wu "\
+  fprintf (stdout, _("authentication system developed by Tom Wu "\
 		     "(tjw@CS.Stanford.EDU).\n"));
 #endif
-  fprintf (stdout, "%s",_("\nPlease refer to the file COPYING in the source "\
+  fprintf (stdout, _("\nPlease refer to the file COPYING in the source "\
 		     "distribution for a"));
-  fprintf (stdout, "%s",_("\nfull list of incorporated code and associated "\
+  fprintf (stdout, _("\nfull list of incorporated code and associated "\
 		     "licenses.\n"));
 
@@ -702,5 +331,5 @@
 
 /*@noreturn@*/
-static int sh_getopt_usage (const char * dummy)
+static int sh_getopt_usage (char * dummy)
 {
   int  i;
@@ -714,10 +343,10 @@
   fprintf (stdout,
 	   _("This is samhain (%s), "\
-	     "(c) 1999-2024 Rainer Wichmann (http://la-samhna.de).\n"),
+	     "(c) 1999-2005 Rainer Wichmann (http://la-samhna.de).\n"),
 	   VERSION);
-  fprintf (stdout, "%s",_("This software comes with ABSOLUTELY NO WARRANTY. "));
-  fprintf (stdout, "%s",_("Use at own risk.\n"));
-
-  fprintf (stdout, "%s",_("Usage:\n\n"));
+  fprintf (stdout, _("This software comes with ABSOLUTELY NO WARRANTY. "));
+  fprintf (stdout, _("Use at own risk.\n"));
+
+  fprintf (stdout, _("Usage:\n\n"));
 
   for (i = 0; op_table[i].longopt != NULL; ++i) {
@@ -728,5 +357,5 @@
     if (op_table[i].shortopt != '-' && 
 	strchr(opts, op_table[i].shortopt) != NULL)
-      fprintf (stdout, "%s",_("Short option char collision !\n"));
+      fprintf (stdout, _("Short option char collision !\n"));
     opts[i] = op_table[i].shortopt;
 
@@ -734,11 +363,12 @@
     if (op_table[i].hasArg == HAS_ARG_NO) {
       if (sl_strlen(op_table[i].longopt) < 10) 
-	sl_strlcpy(fmt,_("%c%c%c        --%-s,\t\t\t %s\n"), sizeof(fmt));
+	strcpy(fmt,_("%c%c%c        --%-s,\t\t\t %s\n"));/* known to fit  */
       else if (sl_strlen(op_table[i].longopt) < 17)
-	sl_strlcpy(fmt, _("%c%c%c        --%-s,\t\t %s\n"), sizeof(fmt));
+	strcpy(fmt, _("%c%c%c        --%-s,\t\t %s\n")); /* known to fit  */
       else 
-	sl_strlcpy(fmt, _("%c%c%c        --%-s,\t %s\n"), sizeof(fmt));
-      /* flawfinder: ignore */
-      fprintf (stdout, fmt,
+	strcpy(fmt, _("%c%c%c        --%-s,\t %s\n"));   /* known to fit  */
+      /*@-formatconst@*/
+      fprintf (stdout,
+	       fmt,
 	       (op_table[i].shortopt == '-') ? ' ' : '-',
 	       (op_table[i].shortopt == '-') ? ' ' : op_table[i].shortopt,
@@ -746,11 +376,15 @@
 	       _(op_table[i].longopt),
 	       _(op_table[i].usage));
+      /*@+formatconst@*/
     } else {
       if (sl_strlen(op_table[i].longopt) < 12) 
-	sl_strlcpy(fmt, _("%c%c %s  --%-s=<arg>,\t\t %s\n"), sizeof(fmt));  
+	strcpy(fmt,                                      /* known to fit  */
+	       _("%c%c %s  --%-s=<arg>,\t\t %s\n"));  
       else 
-	sl_strlcpy(fmt, _("%c%c %s  --%-s=<arg>,\t %s\n"), sizeof(fmt));   
-      /* flawfinder: ignore */
-      fprintf (stdout, fmt,
+	strcpy(fmt,                                      /* known to fit  */ 
+	       _("%c%c %s  --%-s=<arg>,\t %s\n"));   
+      /*@-formatconst@*/
+      fprintf (stdout,
+	       fmt,
 	       (op_table[i].shortopt == '-') ? ' ' : '-',
 	       (op_table[i].shortopt == '-') ? ' ' : op_table[i].shortopt,
@@ -758,8 +392,9 @@
 	       _(op_table[i].longopt),
 	       _(op_table[i].usage));
+      /*@+formatconst@*/
     }
   }
 
-  fprintf (stdout, "%s",
+  fprintf (stdout, 
 	   _("\nPlease report bugs to support@la-samhna.de.\n"));
 
@@ -778,22 +413,10 @@
 
 #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) 
-static int sh_getopt_forever (const char * dummy)
+static int sh_getopt_forever (char * dummy)
 {
-  (void) dummy;
+  dummy = (void *) dummy;
   SL_ENTER(_("sh_getopt_forever"));
   sh.flag.loop = S_TRUE;
   SL_RETURN(0, _("sh_getopt_forever"));
-}
-
-static int sh_getopt_outpath (const char * str)
-{
-  if (str)
-    {
-      if (sh.outpath)
-	SH_FREE(sh.outpath);
-      sh.outpath = sh_util_strdup(str);
-      return 0;
-    }
-  return -1;
 }
 #endif  
@@ -851,5 +474,5 @@
 			      /* not last option
 			       */
-			      fprintf (stderr, "%s",
+			      fprintf (stderr, 
 				       _("Error: short option with argument is not last in option string\n"));
 			      (void) sh_getopt_usage(_("fail"));
@@ -859,6 +482,5 @@
 			      /* argument required, but no avail 
 			       */
-			      fprintf (stderr, "%s",
-				       _("Error: missing argument\n"));
+			      fprintf (stderr, _("Error: missing argument\n"));
 			      (void) sh_getopt_usage(_("fail"));
 			    } 
@@ -867,8 +489,7 @@
 			      /* call function with argument */
 			      --argc; ++argv;
-			      if (NULL != op_table[i].func &&
-				  0 != (* op_table[i].func )(argv[1]))
+			      if (0 != (* op_table[i].func )(argv[1]))
 				fprintf (stderr, 
-					 _("Error processing option -%c\n"),
+					 _("Error processing option -%c"),
 					 op_table[i].shortopt);
 			      break;
@@ -877,8 +498,7 @@
 		      else 
 			{
-			  if (NULL != op_table[i].func &&
-			      0 != (* op_table[i].func )(NULL))
+			  if (0 != (* op_table[i].func )(NULL))
 			    fprintf (stderr, 
-				     _("Error processing option -%c\n"),
+				     _("Error processing option -%c"),
 				     op_table[i].shortopt);
 			  break;
@@ -898,5 +518,5 @@
 	    {
 	      /* unrecognized short option */
-	      fprintf (stderr, "%s",_("Error: unrecognized short option\n"));
+	      fprintf (stderr, _("Error: unrecognized short option\n"));
 	      (void) sh_getopt_usage(_("fail"));
 	    }
@@ -910,5 +530,5 @@
 	  for (i = 0; op_table[i].longopt != NULL; ++i) 
 	    {
-
+      
 	      if (sl_strncmp(_(op_table[i].longopt), 
 			     &argv[1][2], 
@@ -918,26 +538,9 @@
 		  if ( op_table[i].hasArg == HAS_ARG_YES ) 
 		    {
-		      theequal = strchr(argv[1], '=');
-		      if (theequal == NULL) 
+		      if ( (theequal = strchr(argv[1], '=')) == NULL) 
 			{ 
-			  if (argc < 3) 
-			    { 
-			      /* argument required, but no avail 
-			       */
-			      fprintf (stderr, "%s",
-				       _("Error: missing argument\n"));
-			      (void) sh_getopt_usage(_("fail"));
-			    } 
-			  else 
-			    {
-			      /* call function with argument */
-			      --argc; ++argv;
-			      if (NULL != op_table[i].func &&
-				  0 != (* op_table[i].func )(argv[1]))
-				fprintf (stderr, 
-					 _("Error processing option -%s\n"),
-					 op_table[i].longopt);
-			      break;
-			    }
+			  fprintf (stderr, _("Error: missing argument\n"));
+			  /* argument required, but no avail */
+			  (void) sh_getopt_usage(_("fail"));
 			} 
 		      else 
@@ -947,8 +550,7 @@
 			      ++theequal;
 			      /* call function with argument */
-			      if (NULL != op_table[i].func &&
-				  0 != (* op_table[i].func )(theequal))
+			      if (0 != (* op_table[i].func )(theequal))
 				fprintf (stderr, 
-					 _("Error processing option -%s\n"),
+					 _("Error processing option -%s"),
 					 op_table[i].longopt);
 			      break;
@@ -956,6 +558,5 @@
 			  else 
 			    {
-			      fprintf (stderr, "%s",
-				       _("Error: invalid argument\n"));
+			      fprintf (stderr, _("Error: invalid argument\n"));
 			      /* argument required, but no avail */
 			      (void) sh_getopt_usage(_("fail"));
@@ -965,8 +566,7 @@
 		  else 
 		    {
-		      if (NULL != op_table[i].func && 
-			  0 != (* op_table[i].func )(NULL))
+		      if (0 != (* op_table[i].func )(NULL))
 			fprintf (stderr, 
-				 _("Error processing option -%s\n"),
+				 _("Error processing option -%s"),
 				 op_table[i].longopt);
 		      break;
@@ -986,5 +586,5 @@
 	    {
 	      /* unrecognized long option */
-	      fprintf (stderr, "%s",_("Error: unrecognized long option\n"));
+	      fprintf (stderr, _("Error: unrecognized long option\n"));
 	      (void) sh_getopt_usage(_("fail"));
 	    }
Index: trunk/src/sh_gpg.c
===================================================================
--- trunk/src/sh_gpg.c	(revision 1)
+++ trunk/src/sh_gpg.c	(revision 1)
@@ -0,0 +1,1078 @@
+/* SAMHAIN file system integrity testing                                   */
+/* Copyright (C) 1999, 2000 Rainer Wichmann                                */
+/*                                                                         */
+/*  This program is free software; you can redistribute it                 */
+/*  and/or modify                                                          */
+/*  it under the terms of the GNU General Public License as                */
+/*  published by                                                           */
+/*  the Free Software Foundation; either version 2 of the License, or      */
+/*  (at your option) any later version.                                    */
+/*                                                                         */
+/*  This program is distributed in the hope that it will be useful,        */
+/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
+/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
+/*  GNU General Public License for more details.                           */
+/*                                                                         */
+/*  You should have received a copy of the GNU General Public License      */
+/*  along with this program; if not, write to the Free Software            */
+/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
+
+#include "config_xor.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+
+#if defined(WITH_GPG) || defined(WITH_PGP)
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <signal.h>
+#if defined(SH_WITH_SERVER)
+#include <pwd.h>
+#endif
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <errno.h>
+#include <sys/wait.h>
+
+#include <string.h>
+#ifdef HAVE_MEMORY_H
+#include <memory.h>
+#endif
+
+
+#if !defined(O_NONBLOCK)
+#if defined(O_NDELAY)
+#define O_NONBLOCK  O_NDELAY
+#else
+#define O_NONBLOCK  0
+#endif
+#endif
+
+
+#include "samhain.h"
+#include "sh_utils.h"
+#include "sh_error.h"
+#include "sh_tiger.h"
+#if defined(SH_WITH_SERVER)
+#define SH_NEED_PWD_GRP 1
+#include "sh_static.h"
+#endif
+
+static struct {
+  char     conf_id[SH_MINIBUF+1];
+  char     conf_fp[SH_MINIBUF+1];
+  char     data_id[SH_MINIBUF+1];
+  char     data_fp[SH_MINIBUF+1];
+} gp;
+
+typedef struct {
+  pid_t    pid;
+  FILE   * pipe;
+} sh_gpg_popen_t;
+
+#define SH_GPG_OK      0
+#define SH_GPG_BAD     1
+#define SH_GPG_BADSIGN 2
+
+/* replace #if 0 by #if 1 and set an appropriate path in front of '/pdbg.'
+ * for debugging
+ */
+#if 0
+#define PDGBFILE "/pdbg."
+#endif
+
+#if defined(PDGBFILE)
+FILE * pdbg;
+FILE * pdbgc;
+#define PDBG_OPEN    pdbg = fopen(PDGBFILE"main",  "a")  
+#define PDBG_CLOSE   fclose (pdbg)
+#define PDBG(arg)    fprintf(pdbg,  "PDBG: step %d\n", arg); fflush(pdbg)
+#define PDBG_D(arg)  fprintf(pdbg,  "PDBG: %d\n", arg); fflush(pdbg)
+#define PDBG_S(arg)  fprintf(pdbg,  "PDBG: %s\n", arg); fflush(pdbg)
+
+#define PDBGC_OPEN   pdbgc = fopen(PDGBFILE"child", "a")  
+#define PDBGC_CLOSE  fclose (pdbgc)
+#define PDBGC(arg)   fprintf(pdbgc, "PDBG: step %d\n", arg); fflush(pdbgc)
+#define PDBGC_D(arg) fprintf(pdbgc, "PDBG: %d\n", arg); fflush(pdbgc)
+#define PDBGC_S(arg) fprintf(pdbgc, "PDBG: %s\n", arg); fflush(pdbgc)
+#else
+#define PDBG_OPEN    
+#define PDBG_CLOSE   
+#define PDBG(arg)    
+#define PDBG_D(arg)  
+#define PDBG_S(arg)  
+#define PDBGC_OPEN    
+#define PDBGC_CLOSE   
+#define PDBGC(arg)    
+#define PDBGC_D(arg)  
+#define PDBGC_S(arg)  
+#endif
+
+#undef  FIL__
+#define FIL__  _("sh_gpg.c")
+
+#ifdef GPG_HASH
+static int sh_gpg_checksum (SL_TICKET checkfd, int flag)
+{
+  char * test_gpg;
+  char * test_ptr1 = NULL;
+  char * test_ptr2 = NULL;
+  char   wstrip1[128];
+  char   wstrip2[128];
+  int    i, k;
+#include "sh_gpg_chksum.h"
+
+  SL_ENTER(_("sh_gpg_checksum"));
+
+  tiger_fd = checkfd;
+#if defined(WITH_PGP)
+  test_gpg = sh_tiger_hash_gpg (DEFAULT_PGP_PATH, TIGER_FD, 0);
+#else
+  test_gpg = sh_tiger_hash_gpg (DEFAULT_GPG_PATH, TIGER_FD, 0);
+#endif
+  
+  test_ptr1 = strchr(GPG_HASH, ':');
+  if (test_gpg != NULL)
+    test_ptr2 = strchr(test_gpg, ':');
+  
+  if (test_ptr2 != NULL)
+    test_ptr2 += 2;
+  else
+    test_ptr2 = test_gpg;
+  if (test_ptr1 != NULL)
+    test_ptr1 += 2;
+  else
+    test_ptr1 = GPG_HASH;
+
+  /* Tue Jun 24 23:11:54 CEST 2003 (1.7.9) -- strip whitespace
+   */
+  k = 0;
+  for (i = 0; i < 127; ++i)
+    {
+      if (test_ptr1[i] == '\0')
+	break;
+      if (test_ptr1[i] != ' ')
+	{
+	  wstrip1[k] = test_ptr1[i];
+	  ++k;
+	}
+    }
+  wstrip1[k] = '\0';
+
+  for(i = 0; i < KEY_LEN; ++i)
+    {
+      if (gpgchk[i] != wstrip1[i]) 
+	{
+	  sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0, MSG_E_GPG_CHK, 
+			  gpgchk, wstrip1);
+	  break;
+	}
+    }
+
+  k = 0;
+  for (i = 0; i < 127; ++i)
+    {
+      if (test_ptr2[i] == '\0')
+	break;
+      if (test_ptr2[i] != ' ')
+	{
+	  wstrip2[k] = test_ptr2[i];
+	  ++k;
+	}
+    }
+  wstrip2[k] = '\0';
+
+  if (0 != sl_strncmp(wstrip1, wstrip2, 127))
+    {
+      TPT(((0), FIL__, __LINE__, _("msg=<pgp checksum: %s>\n"), test_gpg));
+      TPT(((0), FIL__, __LINE__, _("msg=<Compiled-in : %s>\n"), GPG_HASH));
+      TPT(((0), FIL__, __LINE__, _("msg=<wstrip1     : %s>\n"), wstrip1));
+      TPT(((0), FIL__, __LINE__, _("msg=<wstrip2     : %s>\n"), wstrip2));
+      if (flag == 1)
+	sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_GPG, 
+			GPG_HASH, test_gpg);
+      dlog(1, FIL__, __LINE__, _("The compiled-in checksum of the gpg binary\n(%s)\ndoes not match the actual checksum\n(%s).\nYou need to recompile with the correct checksum."), wstrip1, wstrip2);
+      SH_FREE(test_gpg);
+      SL_RETURN((-1), _("sh_gpg_checksum"));
+    }
+  SH_FREE(test_gpg);
+  SL_RETURN( (0), _("sh_gpg_checksum"));
+}
+#endif
+
+static FILE * sh_gpg_popen (sh_gpg_popen_t  *source, int fd, 
+			    int mode, char * id, char * homedir)
+{
+  int pipedes[2];
+  FILE * outf = NULL;
+  char * envp[2];
+  char   path[256];
+  char   cc1[32];
+  char   cc2[32];
+#if defined(WITH_PGP)
+  char   cc3[32];
+  char   cc0[3] = "-f";
+#endif
+#if defined(WITH_GPG)
+  char   cc0[2] = "-";
+  char   cc3[32];
+  char   cc4[SH_PATHBUF+32];
+  char   cc5[32] = "--no-tty";
+#endif
+
+  char * arg[9];
+
+#if defined(HAVE_GPG_CHECKSUM)
+  SL_TICKET   checkfd;
+  int         myrand;
+  int         i;
+#if defined(__linux__)
+  int         get_the_fd(SL_TICKET);
+  char        pname[128];
+  int         pfd;
+#endif
+#endif
+
+  SL_ENTER(_("sh_gpg_popen"));
+
+#if defined(WITH_GPG)
+  /* -- GnuPG -- */
+  sl_strlcpy (path,  DEFAULT_GPG_PATH,  256);
+  sl_strlcpy (cc1,   _("--status-fd"),  32);
+  sl_strlcpy (cc2,   _("--verify"),     32);
+  sl_strlcpy (cc3,   _("--homedir"),    32);
+  /* sl_strlcpy (cc4,   sh.effective.home, SH_PATHBUF+32); */
+  sl_strlcpy (cc4,   homedir,           SH_PATHBUF+32);
+  sl_strlcat (cc4,   _("/.gnupg"),      SH_PATHBUF+32); 
+
+  /* fprintf(stderr, "YULE: homedir=%s\n", homedir); */
+
+#if defined(SH_WITH_SERVER)
+  if (0 == sl_ret_euid())   /* privileges not dropped yet */
+    {
+      struct stat lbuf;
+      int         status_stat = 0;
+      struct passwd * tempres = getpwnam(DEFAULT_IDENT);
+      if (!tempres)
+	tempres = sh_getpwnam(DEFAULT_IDENT);
+      if (!tempres)
+	{
+	  dlog(1, FIL__, __LINE__, 
+	       _("User %s does not exist. Please add the user to your system.\n"), 
+	       DEFAULT_IDENT);
+	  status_stat = -1;
+	}
+      if (!tempres->pw_dir || tempres->pw_dir[0] == '\0')
+	{
+	  dlog(1, FIL__, __LINE__, 
+	       _("User %s does not have a home directory.\nPlease add the home directory for this user to your system.\n"), 
+	       DEFAULT_IDENT);
+	  status_stat = -2;
+	}
+      if (status_stat == 0)
+	{
+	  sl_strlcpy (cc4, tempres->pw_dir, SH_PATHBUF+32); 
+	  sl_strlcat (cc4,   _("/.gnupg"),      SH_PATHBUF+32); 
+	  status_stat =  retry_lstat(FIL__, __LINE__, cc4, &lbuf);
+	  if (status_stat == -1)
+	    {
+	      dlog(1, FIL__, __LINE__, 
+		   _("Gnupg directory %s for user %s\ndoes not exist or is not accessible.\nPlease add the directory and put the keyring (pubring.gpg) there\nto verify the configuration file.\n"),
+		   cc4, DEFAULT_IDENT);
+	      status_stat = -3;
+	    }
+	}
+      if (status_stat == 0 && lbuf.st_uid != tempres->pw_uid)
+	{
+	  dlog(1, FIL__, __LINE__, 
+	       _("Gnupg directory %s\nis not owned by user %s.\n"), 
+	       cc4, DEFAULT_IDENT);
+	  status_stat = -4;
+	}
+      if (status_stat == 0)
+	{
+	  sl_strlcat (cc4,   _("/pubring.gpg"),      SH_PATHBUF+32); 
+	  status_stat =  retry_lstat(FIL__, __LINE__, cc4, &lbuf);
+	  if (status_stat == -1)
+	    {
+	      dlog(1, FIL__, __LINE__, 
+		   _("Gnupg public keyring %s for user %s\ndoes not exist or is not accessible.\nPlease add the directory and put the keyring (pubring.gpg) there\nto verify the configuration file.\n"),
+		   cc4, DEFAULT_IDENT);
+	      status_stat = -5;
+	    }
+	}
+      if (status_stat == 0 && lbuf.st_uid != tempres->pw_uid)
+	{
+	  dlog(1, FIL__, __LINE__, 
+	       _("Gnupg public keyring %s\nis not owned by user %s.\n"), 
+	       cc4, DEFAULT_IDENT);
+	  status_stat = -6;
+	}
+      if (status_stat != 0)
+	{
+	  sh_error_handle((-1), FIL__, __LINE__, status_stat, MSG_EXIT_ABORT1, 
+			  sh.prg_name);
+	  aud_exit (FIL__, __LINE__, EXIT_FAILURE);
+	}
+      sl_strlcpy (cc4, tempres->pw_dir, SH_PATHBUF+32); 
+      sl_strlcat (cc4,   _("/.gnupg"),      SH_PATHBUF+32); 
+    }
+#endif
+
+  arg[0] = path; 
+  arg[1] = cc1;
+  arg[2] = "1";
+  arg[3] = cc2;
+  arg[4] = cc3;
+  arg[5] = cc4;
+  arg[6] = cc5;
+  arg[7] = cc0;
+  arg[8] = NULL;
+
+  /* catch 'unused parameter' compiler warning
+   */
+  (void) mode;
+  (void) id;
+#elif defined(WITH_PGP)
+  /* -- PGP -- */
+  sl_strlcpy (path,  DEFAULT_PGP_PATH, 256);
+  if (mode == 0)
+    {
+      sl_strlcpy (cc1,   _("+language=en"),  32);
+      sl_strlcpy (cc2,   _("-o"),     32);
+      sl_strlcpy (cc3,   _("/dev/null"),     32);
+      
+      arg[0] = path; 
+      arg[1] = cc1;
+      arg[2] = cc2; 
+      arg[3] = cc3; 
+      arg[4] = cc0;
+      arg[5] = NULL;
+    }
+  else
+    {
+      sl_strlcpy (cc1,   _("+language=en"),  32);
+      sl_strlcpy (cc2,   _("-kvc"),     32);       
+      
+      arg[0] = path; 
+      arg[1] = cc1;
+      arg[2] = cc2;
+      arg[3] = id;
+      arg[4] = NULL;
+      arg[5] = NULL;
+    }
+#endif
+
+  /* use homedir of effective user
+   */
+  if (sh.effective.home != NULL)
+    {
+      envp[0] = malloc (sl_strlen(sh.effective.home) + 6); /* free() ok   */
+      if (envp[0] != NULL)
+	sprintf (envp[0], "HOME=%s",                     /* known to fit  */
+		 sh.effective.home); 
+      envp[1] = NULL;
+    }
+  else
+    {
+      envp[0] = NULL;
+    }
+
+  /* Create the pipe 
+   */
+  if (aud_pipe(FIL__, __LINE__, pipedes) < 0) 
+    {
+      if (envp[0] != NULL) 
+	free(envp[0]);
+      SL_RETURN( (NULL), _("sh_gpg_popen"));
+    }
+  
+  source->pid = aud_fork(FIL__, __LINE__);
+  
+  /* Failure
+   */
+  if (source->pid == (pid_t) - 1) 
+    {
+      close(pipedes[0]);
+      close(pipedes[1]);
+      if (envp[0] != NULL) 
+	free(envp[0]);
+      SL_RETURN( (NULL), _("sh_gpg_popen"));
+    }
+
+  if (source->pid == (pid_t) 0) 
+    {
+
+      /* child - make read side of the pipe stdout 
+       */
+      if (retry_aud_dup2(FIL__, __LINE__,
+			pipedes[STDOUT_FILENO], STDOUT_FILENO) < 0)
+	{
+	  TPT(((0), FIL__, __LINE__, _("msg=<dup2 on pipe failed>\n")));
+	  dlog(1, FIL__, __LINE__, _("Internal error: dup2 failed\n"));
+	  aud__exit(FIL__, __LINE__, EXIT_FAILURE);
+	}
+      
+      /* close the pipe descriptors 
+       */
+      close (pipedes[STDIN_FILENO]);
+      close (pipedes[STDOUT_FILENO]);
+      
+
+#if defined(WITH_PGP)
+      if (mode == 0) 
+	{
+	  if (retry_aud_dup2(FIL__, __LINE__, fd, STDIN_FILENO) < 0)
+	    {
+	      TPT(((0), FIL__, __LINE__, _("msg=<dup2 on fd failed>\n")));
+	      dlog(1, FIL__, __LINE__, _("Internal error: dup2 failed\n"));
+	      aud__exit(FIL__, __LINE__, EXIT_FAILURE);
+	    }
+	}
+#else
+      if (retry_aud_dup2(FIL__, __LINE__, fd, STDIN_FILENO) < 0)
+	{
+	  TPT(((0), FIL__, __LINE__, _("msg=<dup2 on fd failed>\n")));
+	  dlog(1, FIL__, __LINE__, _("Internal error: dup2 failed\n"));
+	  aud__exit(FIL__, __LINE__, EXIT_FAILURE);
+	}
+#endif
+ 
+      /* don't leak file descriptors
+       */
+      sh_unix_closeall (3, -1); /* in child process */
+
+      freopen(_("/dev/null"), "r+", stderr); 
+
+      /* We should become privileged if SUID,
+       * to be able to read the keyring.
+       * We have checked that gpg is OK,
+       * AND that only a trusted user could overwrite
+       * gpg.
+       */
+      memset (skey, '\0', sizeof(sh_key_t));
+      aud_setuid(FIL__, __LINE__, geteuid());
+      
+      PDBGC_OPEN;
+      PDBGC_D((int)getuid());
+      PDBGC_D((int)geteuid());
+
+      {
+	int i = 0;
+	while (arg[i] != NULL)
+	  {
+	    PDBGC_S(arg[i]);
+	    ++i;
+	  }
+      }
+      PDBGC_CLOSE;
+
+      /* exec the program */
+
+#if defined(__linux__) && defined(HAVE_GPG_CHECKSUM)
+      /* 
+       * --  emulate an fexecve with checksum testing
+       */
+#if defined(WITH_PGP)
+      checkfd = sl_open_read(DEFAULT_PGP_PATH, SL_NOPRIV);
+#else
+      checkfd = sl_open_read(DEFAULT_GPG_PATH, SL_NOPRIV);
+#endif
+
+      if (0 != sh_gpg_checksum(checkfd, 0))
+	aud__exit(FIL__, __LINE__, EXIT_FAILURE);
+
+      pfd = get_the_fd(checkfd);
+      sprintf(pname, _("/proc/self/fd/%d"),             /* known to fit  */
+		   pfd);
+      if (0 == access(pname, R_OK|X_OK))
+	{
+	  fcntl  (pfd, F_SETFD, FD_CLOEXEC);
+	  retry_aud_execve (FIL__, __LINE__,  pname, arg, envp);
+	      
+	  dlog(1, FIL__, __LINE__, _("Unexpected error: execve %s failed\n"),
+	       pname);
+	  /* failed 
+	   */
+	  aud__exit(FIL__, __LINE__, EXIT_FAILURE);
+	}
+	  
+      /* procfs not working, go ahead 
+       */
+      sl_close(checkfd);
+#endif
+
+#if defined(HAVE_GPG_CHECKSUM)
+      /* This is an incredibly ugly kludge to prevent an attacker
+       * from knowing when it is safe to slip in a fake executable
+       * between the integrity check and the execve
+       */
+      myrand = (int) taus_get (&(skey->rng0[0]), &(skey->rng1[0]), 
+			       &(skey->rng2[0]));
+      myrand = (myrand < 0) ? (-myrand) : myrand;
+      myrand = (myrand % 32) + 2;
+
+      for (i = 0; i < myrand; ++i)
+	{
+#if defined(WITH_PGP)
+	  checkfd = sl_open_fastread(DEFAULT_PGP_PATH, SL_NOPRIV);
+#else
+	  checkfd = sl_open_fastread(DEFAULT_GPG_PATH, SL_NOPRIV);
+#endif
+	  if (0 != sh_gpg_checksum(checkfd, 0)) {
+	    aud__exit(FIL__, __LINE__, EXIT_FAILURE);
+	  }
+	  sl_close(checkfd);
+	}
+#endif
+			       
+
+#if defined(WITH_GPG)
+      retry_aud_execve (FIL__, __LINE__, DEFAULT_GPG_PATH, arg, envp);
+      dlog(1, FIL__, __LINE__, _("Unexpected error: execve %s failed\n"),
+	   DEFAULT_GPG_PATH);
+#elif defined(WITH_PGP)
+      retry_aud_execve (FIL__, __LINE__, DEFAULT_PGP_PATH, arg, envp);
+#endif
+      
+      /* failed 
+       */
+      TPT(((0), FIL__, __LINE__, _("msg=<execve failed>\n")));
+      dlog(1, FIL__, __LINE__, _("Unexpected error: execve failed\n"));
+      aud__exit(FIL__, __LINE__, EXIT_FAILURE);
+    }
+
+  /* parent
+   */
+
+  if (envp[0] != NULL) 
+    free(envp[0]);
+
+  close (pipedes[STDOUT_FILENO]);
+  retry_fcntl (FIL__, __LINE__, pipedes[STDIN_FILENO], F_SETFD, FD_CLOEXEC);
+  retry_fcntl (FIL__, __LINE__, pipedes[STDIN_FILENO], F_SETFL,  O_NONBLOCK);
+
+  outf = fdopen (pipedes[STDIN_FILENO], "r");
+  
+  if (outf == NULL) 
+    {
+      aud_kill (FIL__, __LINE__, source->pid, SIGKILL);
+      close (pipedes[STDOUT_FILENO]);
+      waitpid (source->pid, NULL, 0);
+      source->pid = 0;
+      SL_RETURN( (NULL), _("sh_gpg_popen"));
+    }
+  
+  SL_RETURN( (outf), _("sh_gpg_popen"));
+}
+
+
+static int sh_gpg_pclose (sh_gpg_popen_t *source)
+{
+  int status = 0;
+  
+  SL_ENTER(_("sh_gpg_pclose"));
+
+  status = fclose(source->pipe);
+  if (status)
+    SL_RETURN( (-1), _("sh_gpg_pclose"));
+  
+  if (waitpid(source->pid, NULL, 0) != source->pid)
+    status = -1;
+  
+  source->pipe = NULL;
+  source->pid = 0;
+  SL_RETURN( (status), _("sh_gpg_pclose"));
+}
+ 
+static
+int sh_gpg_check_file_sign(int fd, char * sign_id, char * sign_fp, 
+			   char * homedir, int whichfile)
+{
+  struct stat buf;
+  char line[256];
+  sh_gpg_popen_t  source;
+  int have_id = BAD, have_fp = BAD, status = 0;
+#ifdef WITH_PGP
+  char *ptr;
+#endif
+
+#ifdef HAVE_GPG_CHECKSUM
+  SL_TICKET checkfd;
+#endif
+
+  SL_ENTER(_("sh_gpg_check_file_sign"));
+
+  /* check whether GnuPG exists and has the correct checksum
+   */
+#if defined(WITH_GPG)
+
+  TPT(((0), FIL__, __LINE__, _("msg=<Check signature>\n")));
+  TPT(((0), FIL__, __LINE__, _("msg=<gpg is %s>\n"), DEFAULT_GPG_PATH));
+
+  if (0 != retry_lstat(FIL__, __LINE__, DEFAULT_GPG_PATH, &buf))
+    {
+      status = errno;
+      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_ERR_LSTAT,
+		      sh_error_message(status), DEFAULT_GPG_PATH);
+      SL_RETURN( SH_GPG_BAD, _("sh_gpg_check_file_sign"));
+    }
+
+  if (0 != tf_trust_check (DEFAULT_GPG_PATH, SL_YESPRIV))
+    SL_RETURN( SH_GPG_BAD, _("sh_gpg_check_file_sign"));
+
+#ifdef HAVE_GPG_CHECKSUM
+  checkfd = sl_open_read(DEFAULT_GPG_PATH, SL_YESPRIV);
+
+  if (0 != sh_gpg_checksum(checkfd, 1))
+    {
+      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
+		      _("Checksum mismatch"), 
+		      _("gpg_check_file_sign"));
+      sl_close(checkfd);
+      SL_RETURN( SH_GPG_BAD, _("sh_gpg_check_file_sign"));
+    }
+  sl_close(checkfd);
+#endif
+
+#elif defined(WITH_PGP)
+
+  TPT(((0), FIL__, __LINE__, _("msg=<Check signature>\n")));
+  TPT(((0), FIL__, __LINE__, _("msg=<pgp is %s>\n"), DEFAULT_PGP_PATH));
+
+  if (0 != retry_lstat(FIL__, __LINE__, DEFAULT_PGP_PATH, &buf))
+    {
+      status = errno;
+      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_ERR_LSTAT,
+		      sh_error_message(status), DEFAULT_PGP_PATH);
+      SL_RETURN( SH_GPG_BAD, _("sh_gpg_check_file_sign"));
+    }
+  if (0 != tf_trust_check (DEFAULT_PGP_PATH, SL_YESPRIV))
+    SL_RETURN( SH_GPG_BAD, _("sh_gpg_check_file_sign"));
+
+#ifdef HAVE_GPG_CHECKSUM
+  checkfd = sl_open_read(DEFAULT_PGP_PATH, SL_YESPRIV);
+
+  if (0 != sh_gpg_checksum(checkfd, 1))
+    {
+      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
+		      _("Checksum mismatch"), 
+		      _("gpg_check_file_sign"));
+      sl_close(checkfd);
+      SL_RETURN( SH_GPG_BAD, _("sh_gpg_check_file_sign"));
+    }
+  sl_close(checkfd);
+#endif
+
+#endif
+
+  TPT(((0), FIL__, __LINE__, _("msg=<Open pipe to check signature>\n")));
+
+  fflush(NULL);
+ 
+  source.pipe   = sh_gpg_popen  ( &source, fd, 0, NULL, homedir );
+
+  if (NULL == source.pipe)
+    {
+      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
+		      _("Could not open pipe"), 
+		      _("gpg_check_file_sign"));
+      SL_RETURN( SH_GPG_BAD, _("sh_gpg_check_file_sign"));
+    }
+
+  TPT(((0), FIL__, __LINE__, _("msg=<Open pipe success>\n")));
+
+ xagain:
+
+  errno = 0;
+
+  while (NULL != fgets(line, 255, source.pipe))
+    {
+
+      TPT(((0), FIL__, __LINE__, _("msg=<gpg out: %s>\n"), line));
+      if (line[strlen(line)-1] == '\n')
+	line[strlen(line)-1] = ' ';
+      sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN, 
+		      line, 
+		      _("gpg_check_file_sign"));
+
+      if (sl_strlen(line) < 18) 
+	continue;
+#if defined(WITH_GPG)
+      /* Sun May 27 18:40:05 CEST 2001
+       */
+      if (0 == sl_strncmp(_("BADSIG"), &line[9], 6) ||
+	  0 == sl_strncmp(_("ERRSIG"), &line[9], 6) ||
+	  0 == sl_strncmp(_("NO_PUBKEY"), &line[9], 6) ||
+	  0 == sl_strncmp(_("NODATA"), &line[9], 6) ||
+	  0 == sl_strncmp(_("SIGEXPIRED"), &line[9], 6))
+	{
+	  if      (0 == sl_strncmp(_("BADSIG"), &line[9], 6)) {
+	    dlog(1, FIL__, __LINE__, 
+		 _("%s file is signed, but the signature is invalid."),
+		 ((whichfile == 1) ? _("Configuration") : _("Database")));
+	  } 
+	  else if (0 == sl_strncmp(_("NO_PUBKEY"), &line[9], 6)) {
+	    dlog(1, FIL__, __LINE__, 
+		 _("%s file is signed, but the public key to verify the signature is not in my keyring %s/.gnupg/pubring.asc."), 
+		 ((whichfile == 1) ? _("Configuration") : _("Database")),
+		 homedir);
+	  }
+	  else if (0 == sl_strncmp(_("ERRSIG"), &line[9], 6)) {
+	    dlog(1, FIL__, __LINE__, 
+		 _("%s file is signed, but the public key to verify the signature is not in my keyring %s/.gnupg/pubring.asc."), 
+		 ((whichfile == 1) ? _("Configuration") : _("Database")),
+		 homedir);
+	  }
+	  else if (0 == sl_strncmp(_("SIGEXPIRED"), &line[9], 6)) {
+	    dlog(1, FIL__, __LINE__, 
+		 _("%s file is signed, but the public key to verify the signature has expired."), 
+		 ((whichfile == 1) ? _("Configuration") : _("Database")));
+	  }
+	  else if (0 == sl_strncmp(_("NODATA"), &line[9], 6)) {
+	    dlog(1, FIL__, __LINE__, 
+		 _("%s file is not signed."), 
+		 ((whichfile == 1) ? _("Configuration") : _("Database")));
+	  }
+
+	  have_fp = BAD; have_id = BAD;
+	  break;
+	}
+      if (0 == sl_strncmp(_("GOODSIG"), &line[9], 7))
+	{
+	  sl_strlcpy (sign_id, &line[25], SH_MINIBUF+1);
+	  sign_id[sl_strlen(sign_id)-1] = '\0';  /* remove trailing '"' */
+	  have_id = GOOD;
+	} 
+      if (0 == sl_strncmp(_("VALIDSIG"), &line[9], 8))
+	{
+	  strncpy (sign_fp, &line[18], 40);
+	  sign_fp[40] = '\0';
+	  have_fp = GOOD;
+	}
+#elif defined(WITH_PGP)
+      if (0 == sl_strncmp(_("Bad signature"), line, 13) ||
+	  0 == sl_strncmp(_("Error"), line, 5) ||
+	  0 == sl_strncmp(_("Malformed"), line, 9) ||
+	  0 == sl_strncmp(_("WARNING"), line, 7) ||
+	  0 == sl_strncmp(_("ERROR"), line, 5) 
+	  )
+	{
+	  have_fp = BAD; have_id = BAD;
+	  break;
+	}
+      if (0 == sl_strncmp(_("Good signature"), line, 14))
+	{
+	  ptr = strchr ( line, '"');
+	  ++ptr;
+	  sl_strlcpy (sign_id, ptr, SH_MINIBUF+1);
+	  sign_id[sl_strlen(sign_id)-1] = '\0'; /* remove trailing dot */
+	  sign_id[sl_strlen(sign_id)-2] = '\0'; /* remove trailing '"' */
+	  have_id = GOOD;
+	}
+#endif
+    }
+
+  if (ferror(source.pipe) && errno == EAGAIN) 
+    {
+      clearerr(source.pipe);
+      goto xagain;
+    }
+ 
+  sh_gpg_pclose (&source);
+
+  TPT(((0), FIL__, __LINE__, _("msg=<Close pipe>\n")));
+
+#ifdef WITH_PGP
+  /* get the fingerprint */
+
+  source.pipe   = sh_gpg_popen  ( &source, fd, 1,  sign_id, homedir);
+  if (NULL == source.pipe)
+    {
+      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
+		      _("Could not open pipe for fp"), 
+		      _("gpg_check_file_sign"));
+      SL_RETURN( SH_GPG_BAD, _("sh_gpg_check_file_sign"));
+    }
+
+  TPT(((0), FIL__, __LINE__, _("msg=<Open pipe success>\n")));
+
+ yagain:
+
+  errno = 0;
+
+  while (NULL != fgets(line, 255, source.pipe))
+    {
+      if (line[strlen(line)-1] == '\n')
+	line[strlen(line)-1] = ' ';
+      sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN, 
+		      line, 
+		      _("gpg_check_file_sign"));
+
+      if (sl_strlen(line) < 18) 
+	continue;
+      ptr = strtok (line, " ");
+      while (ptr)
+	{
+	  ptr = strtok (NULL, " ");
+	  if (ptr && 0 == sl_strncmp (ptr, _("fingerprint"), 11))
+	    {
+	      ptr = strtok (NULL, " "); /* to '=' */
+	      sign_fp[0] = '\0';
+	      while (ptr)
+		{
+		  ptr = strtok (NULL, " "); /* part of fingerprint */
+		  sl_strlcat (sign_fp, ptr, SH_MINIBUF+1);
+		}
+	      /* sign_fp[sl_strlen(sign_fp)-1] = '\0'; remove trailing '\n' */
+	      if (sl_strlen(sign_fp) > 0) 
+		have_fp = GOOD;
+	      break;
+	    } 
+	} 
+    }
+
+  if (ferror(source.pipe) && errno == EAGAIN) 
+    {
+      clearerr(source.pipe);
+      goto yagain;
+    }
+ 
+  sh_gpg_pclose (&source);
+#endif
+
+  if (have_id == GOOD)
+    {
+      TPT(((0), FIL__, __LINE__, _("msg=<Got signator ID>\n")));
+      ;
+    }
+  if (have_fp == GOOD)
+    {
+      TPT(((0), FIL__, __LINE__, _("msg=<Got fingerprint>\n")));
+      ;
+    }
+
+  if (have_id == GOOD && have_fp == GOOD)
+    SL_RETURN( SH_GPG_OK, _("sh_gpg_check_file_sign"));
+  else
+    {
+      if (have_id == BAD)
+	sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
+			_("No good signature"), 
+			_("gpg_check_file_sign"));
+      else
+	sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
+			_("No fingerprint for key"), 
+			_("gpg_check_file_sign"));
+      SL_RETURN( SH_GPG_BADSIGN, _("sh_gpg_check_file_sign"));
+    }
+}
+
+int get_the_fd(SL_TICKET file_1);
+
+int sh_gpg_check_sign (long file_1, long file_2, int what)
+{
+  int status = SH_GPG_BAD;
+  int fd1 = 0;
+  int fd2 = 0;
+  static int smsg = S_FALSE;
+  char  * tmp;
+  char  * tmp2;
+
+  char  * homedir = sh.effective.home;
+#if defined(SH_WITH_SERVER)
+  struct passwd * tempres;
+#endif
+#ifdef USE_FINGERPRINT
+#include "sh_gpg_fp.h"
+#endif
+
+  SL_ENTER(_("sh_gpg_check_sign"));
+
+
+  if (what == 0 || what == 1)
+    fd1 = get_the_fd(file_1);
+  if (what == 0 || what == 2)
+    fd2 = get_the_fd(file_2);
+
+
+  if (fd1 < 0 || fd2 < 0)
+    {
+      TPT(((0), FIL__, __LINE__, _("msg=<GPG_CHECK: FD1 = %d>\n"), fd1));
+      TPT(((0), FIL__, __LINE__, _("msg=<GPG_CHECK: FD2 = %d>\n"), fd2));
+      dlog(1, FIL__, __LINE__, 
+	   _("This looks like an unexpected internal error.\n"));
+      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1, sh.prg_name);
+      aud_exit (FIL__, __LINE__, EXIT_FAILURE);
+      SL_RETURN( (-1), _("sh_gpg_check_sign"));
+    }
+  
+  if (what == 0 || what == 1)
+    {
+      TPT(((0), FIL__, __LINE__, _("msg=<GPG_CHECK: FD1 = %d>\n"), fd1));
+#if defined(SH_WITH_SERVER)
+      tempres = getpwnam(DEFAULT_IDENT);
+      if (!tempres)
+	tempres = sh_getpwnam(DEFAULT_IDENT);
+      if ((tempres != NULL) && (0 == sl_ret_euid()))
+	{
+	  /* privileges not dropped yet*/
+	  homedir = tempres->pw_dir;
+	}
+#endif
+      status = sh_gpg_check_file_sign(fd1, gp.conf_id, gp.conf_fp, homedir, 1);
+      TPT(((0), FIL__, __LINE__, _("msg=<CONF SIGUSR: |%s|>\n"), gp.conf_id));
+      TPT(((0), FIL__, __LINE__, _("msg=<CONF SIGFP:  |%s|>\n"), gp.conf_fp));
+    }
+
+  if ((what == 0 && SH_GPG_OK == status) || what == 2)
+    {
+      TPT(((0), FIL__, __LINE__, _("msg=<GPG_CHECK: FD2 = %d>\n"), fd2));
+#if defined(SH_WITH_SERVER)
+      tempres = getpwnam(DEFAULT_IDENT);
+      if (!tempres)
+	tempres = sh_getpwnam(DEFAULT_IDENT);
+      if ((tempres != NULL) && (0 == sl_ret_euid()))
+	{
+	  /* privileges not dropped yet*/
+	  homedir = tempres->pw_dir;
+	}
+#endif
+      status = sh_gpg_check_file_sign(fd2, gp.data_id, gp.data_fp, homedir, 2);
+      TPT(((0), FIL__, __LINE__, _("msg=<DATA SIGUSR: |%s|>\n"), gp.data_id));
+      TPT(((0), FIL__, __LINE__, _("msg=<DATA SIGFP:  |%s|>\n"), gp.data_fp));
+    }
+  
+  if (SH_GPG_OK == status && what == 1)
+    {
+#ifdef USE_FINGERPRINT
+      if ((sl_strcmp(SH_GPG_FP, gp.conf_fp) == 0))
+	{
+	  int i;
+
+	  for(i = 0; i < (int) sl_strlen(gp.conf_fp); ++i)
+	    {
+	      if (gpgfp[i] != gp.conf_fp[i]) 
+		{
+		  sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0, 
+				  MSG_E_GPG_FP, 
+				  gpgfp, gp.conf_fp);
+		  break;
+		}
+	    }
+
+	  if (smsg == S_FALSE)
+	    {
+	      tmp  = sh_util_safe_name(gp.conf_id);
+	      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_GH,
+			       sh.prg_name, sh.real.uid,
+			       (sh.flag.hidefile == S_TRUE) ? 
+			       _("(hidden)") : file_path('C', 'R'), 
+			       tmp, 
+			       gp.conf_fp);
+	      SH_FREE(tmp);
+	    }
+	  smsg = S_TRUE;
+	  SL_RETURN(0, _("sh_gpg_check_sign"));
+	}
+      else
+	{
+	  /* fp mismatch
+	   */
+	  dlog(1, FIL__, __LINE__, 
+	       _("The fingerprint of the signing key: %s\ndoes not match the compiled-in fingerprint: %s.\nTherefore the signature could not be verified.\n"), 
+	       gp.conf_fp, SH_GPG_FP);
+	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
+		      _("Fingerprint mismatch"), 
+		      _("gpg_check_sign"));
+	  status = SH_GPG_BADSIGN;
+	}
+#else
+      if (smsg == S_FALSE)
+	{
+	  tmp = sh_util_safe_name(gp.conf_id);
+	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_GH,
+			   sh.prg_name, sh.real.uid,
+			   (sh.flag.hidefile == S_TRUE) ? 
+			   _("(hidden)") : file_path('C', 'R'), 
+			   tmp, 
+			   gp.conf_fp);
+	  SH_FREE(tmp);
+	}
+      smsg = S_TRUE;
+      SL_RETURN(0, _("sh_gpg_check_sign"));
+#endif
+    }
+  
+  else if (SH_GPG_OK == status && (what == 2 || what == 0))
+    {
+      if ((sl_strcmp(gp.data_id, gp.conf_id) == 0) &&
+	  (sl_strcmp(gp.data_fp, gp.conf_fp) == 0))
+	{
+	  SL_RETURN(0, _("sh_gpg_check_sign"));
+	}
+      else
+	{
+	  /* ID or fp not equal 
+	   */
+	  dlog(1, FIL__, __LINE__, 
+	       _("The fingerprint or ID of the signing key is not the same for the\nconfiguration file and the file signature database.\nTherefore the signature could not be verified.\n"));
+	  tmp  = sh_util_safe_name (gp.conf_id);
+	  tmp2 = sh_util_safe_name (gp.data_id);
+	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_GH2,
+			   sh.prg_name, sh.real.uid,
+			   (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('C', 'R'),
+			   tmp,  gp.conf_fp,
+			   (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('D', 'R'),
+			   tmp2, gp.data_fp);
+	  SH_FREE(tmp);
+	  SH_FREE(tmp2);
+	}
+    }
+
+  if (status != SH_GPG_OK) 
+    {
+      uid_t   e_uid  = sl_ret_euid();
+      char  * e_home = sh.effective.home;
+
+#if defined(SH_WITH_SERVER)
+      struct passwd * e_tempres = getpwnam(DEFAULT_IDENT);
+      if (!e_tempres)
+	e_tempres = sh_getpwnam(DEFAULT_IDENT);
+      if ((e_tempres != NULL) && (0 == sl_ret_euid()))   
+	{
+	  /* privileges not dropped yet */
+	  e_uid  = e_tempres->pw_uid;
+	  e_home = e_tempres->pw_dir;
+	}
+#endif
+      dlog(1, FIL__, __LINE__, 
+	   _("The signature of the configuration file or the file signature database\ncould not be verified. Possible reasons are:\n - gpg binary (%s) not found\n - invalid signature\n - the signature key is not in the private keyring of UID %d,\n - there is no keyring in %s/.gnupg, or\n - the file is not signed - did you move /filename.asc to /filename ?\nTo create a signed file, use (remove old signatures before):\n   gpg -a --clearsign --not-dash-escaped FILE\n   mv FILE.asc FILE\n"),
+#if defined(WITH_GPG) 
+	   DEFAULT_GPG_PATH,
+#else
+	   DEFAULT_PGP_PATH,
+#endif
+	   (int) e_uid, e_home);
+    }
+
+  TPT(((0), FIL__, __LINE__, _("msg=<Status = %d>\n"), status));
+
+  sh_error_handle((-1), FIL__, __LINE__, status, MSG_EXIT_ABORT1, sh.prg_name);
+  aud_exit (FIL__, __LINE__, EXIT_FAILURE);
+
+  return (-1); /* make compiler happy */
+}  
+
+/* #ifdef WITH_GPG */
+#endif
+
+
+
+
+
+
+
+
Index: trunk/src/sh_guid.c
===================================================================
--- trunk/src/sh_guid.c	(revision 591)
+++ 	(revision )
@@ -1,376 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 2015 Rainer Wichmann                                      */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-#include "config_xor.h"
-#include "samhain.h"
-#include "sh_utils.h"
-
-#include <stdio.h>
-#include <string.h>
-
-/*
- * gen_uuid.c --- generate a DCE-compatible uuid
- *
- * Copyright (C) 1996, 1997, 1998, 1999 Theodore Ts'o.
- *
- * %Begin-Header%
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, and the entire permission notice in its entirety,
- *    including the disclaimer of warranties.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. The name of the author may not be used to endorse or promote
- *    products derived from this software without specific prior
- *    written permission.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
- * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
- * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
- * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
- * DAMAGE.
- * %End-Header%
- */
-
-#define UUID_SIZE 36
-
-struct uuid {
-        UINT32        time_low;
-        UINT16        time_mid;
-        UINT16        time_hi_and_version;
-        UINT16        clock_seq;
-        UBYTE node[6];
-};
-
-typedef unsigned char uuid_t[16];
-
-
-static void uuid_pack(const struct uuid *uu, uuid_t ptr)
-{
-  UINT32          tmp;
-  unsigned char   *out = ptr;
-  
-  tmp = uu->time_low;
-  out[3] = (unsigned char) tmp;
-  tmp >>= 8;
-  out[2] = (unsigned char) tmp;
-  tmp >>= 8;
-  out[1] = (unsigned char) tmp;
-  tmp >>= 8;
-  out[0] = (unsigned char) tmp;
-  
-  tmp = uu->time_mid;
-  out[5] = (unsigned char) tmp;
-  tmp >>= 8;
-  out[4] = (unsigned char) tmp;
-  
-  tmp = uu->time_hi_and_version;
-  out[7] = (unsigned char) tmp;
-  tmp >>= 8;
-  out[6] = (unsigned char) tmp;
-  
-  tmp = uu->clock_seq;
-  out[9] = (unsigned char) tmp;
-  tmp >>= 8;
-  out[8] = (unsigned char) tmp;
-  
-  memcpy(out+10, uu->node, 6);
-  return;
-}
-
-static void uuid_unpack(const uuid_t in, struct uuid *uu)
-{
-  const uint8_t	*ptr = in;
-  uint32_t		tmp;
-  
-  tmp = *ptr++;
-  tmp = (tmp << 8) | *ptr++;
-  tmp = (tmp << 8) | *ptr++;
-  tmp = (tmp << 8) | *ptr++;
-  uu->time_low = tmp;
-  
-  tmp = *ptr++;
-  tmp = (tmp << 8) | *ptr++;
-  uu->time_mid = tmp;
-  
-  tmp = *ptr++;
-  tmp = (tmp << 8) | *ptr++;
-  uu->time_hi_and_version = tmp;
-  
-  tmp = *ptr++;
-  tmp = (tmp << 8) | *ptr++;
-  uu->clock_seq = tmp;
-  
-  memcpy(uu->node, ptr, 6);
-  return;
-}
-
-static void get_random_bytes(unsigned char * buf, size_t len)
-{
-  unsigned int j;
-
-  union {
-    UINT32 i;
-    char c[sizeof(UINT32)];
-  } u;
-
-  do {
-    u.i = taus_get();
-
-    for (j= 0; j < sizeof(UINT32); j++)
-      {
-	if (len) {
-	  --len;
-	  *buf = u.c[j];
-	  ++buf;
-	}
-      }
-  } while (len);
-
-  return;
-}
-
-static void uuid_generate_random(uuid_t out)
-{
-  uuid_t  buf;
-  struct uuid uu;
-
-  get_random_bytes(buf, sizeof(buf));
-  uuid_unpack(buf, &uu);
-
-  /* Version and variant 
-   */
-  uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000;
-  uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF)
-    | 0x4000;
-  uuid_pack(&uu, out);
-  return;
-}
-
-static const char *fmt_lower =	N_("%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x");
-
-static void uuid_unparse(const uuid_t uu, char *out, size_t len)
-{
-  struct uuid uuid;
-  char fmt[80];
-  
-
-  sl_strlcpy(fmt, _(fmt_lower), sizeof(fmt));
-
-  uuid_unpack(uu, &uuid);
-
-  sl_snprintf (out, len, fmt,
-	       uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
-	       uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
-	       uuid.node[0], uuid.node[1], uuid.node[2],
-	       uuid.node[3], uuid.node[4], uuid.node[5]);
-
-  return;
-}
-
-#if defined(__linux__)
-static char * uuid_generate_random_linux(char * out, size_t len)
-{
-  FILE * fd = fopen(_("/proc/sys/kernel/random/uuid"), "r");
-
-  if (fd)
-    {
-      if (NULL != fgets(out, len, fd))
-	{
-	  size_t ll = strlen(out);
-	  if (ll > 0 && out[ll-1] == '\n') {
-	    --ll;
-	    out[ll] = '\0';
-	  }
-	}
-      fclose(fd);
-    }
-  return out;
-}
-#endif
-
-static char * uuid_generate_random_gen(char * out, size_t len)
-{
-  uuid_t u;
-
-  uuid_generate_random(u);
-  uuid_unparse(u, out, len);
-
-  return out;
-}
-
-char * sh_uuid_generate_random(char * out, size_t len)
-{
-  *out = '\0';
-
-#if defined(__linux__)
-  uuid_generate_random_linux(out, len);
-  if (UUID_SIZE == strlen(out))
-    return out;
-#endif
-
-  uuid_generate_random_gen(out, len);
-  return out;
-}
-
-#include <ctype.h>
-int sh_uuid_check(const char * in)
-{
-  int 		i;
-  const char	*cp;
-
-  if (strlen(in) != UUID_SIZE)
-    return -1;
-  for (i=0, cp = in; i <= UUID_SIZE; i++,cp++) {
-    if ((i == 8) || (i == 13) || (i == 18) ||
-	(i == 23)) {
-      if (*cp == '-')
-	continue;
-      else
-	return -1;
-    }
-    if (i== UUID_SIZE)
-      if (*cp == 0)
-	continue;
-    if (!isxdigit(*cp))
-      return -1;
-  }
-  return 0;
-}
-
-
-#ifdef SH_CUTEST
-#include "CuTest.h"
-
-#include <stdlib.h>
-
-static int uuid_type(const uuid_t uu)
-{
-  struct uuid		uuid;
-  
-  uuid_unpack(uu, &uuid);
-  return ((uuid.time_hi_and_version >> 12) & 0xF);
-}
-
-#define UUID_VARIANT_NCS	0
-#define UUID_VARIANT_DCE	1
-#define UUID_VARIANT_MICROSOFT	2
-#define UUID_VARIANT_OTHER	3
-
-#define UUID_TYPE_DCE_TIME   1
-#define UUID_TYPE_DCE_RANDOM 4
-
-static int uuid_variant(const uuid_t uu)
-{
-  struct uuid		uuid;
-  int			var;
-  
-  uuid_unpack(uu, &uuid);
-  var = uuid.clock_seq;
-  
-  if ((var & 0x8000) == 0)
-    return UUID_VARIANT_NCS;
-  if ((var & 0x4000) == 0)
-    return UUID_VARIANT_DCE;
-  if ((var & 0x2000) == 0)
-    return UUID_VARIANT_MICROSOFT;
-  return UUID_VARIANT_OTHER;
-}
-
-static int uuid_parse(const char *in, uuid_t uu)
-{
-  struct uuid	uuid;
-  int 		i;
-  const char	*cp;
-  char		buf[3];
-  
-  if (sh_uuid_check(in) < 0)
-    return -1;
-    
-  uuid.time_low = strtoul(in, NULL, 16);
-  uuid.time_mid = strtoul(in+9, NULL, 16);
-  uuid.time_hi_and_version = strtoul(in+14, NULL, 16);
-  uuid.clock_seq = strtoul(in+19, NULL, 16);
-  cp = in+24;
-  buf[2] = 0;
-  for (i=0; i < 6; i++) {
-    buf[0] = *cp++;
-    buf[1] = *cp++;
-    uuid.node[i] = strtoul(buf, NULL, 16);
-  }
-  
-  uuid_pack(&uuid, uu);
-  return 0;
-}
-
-void Test_uuid (CuTest *tc) {
-
-  char * p; int res;
-  char out[80];
-  size_t len = sizeof(out);
-  uuid_t uu;
-  int type, variant;
-
-  p = uuid_generate_random_gen(out, len);
-  CuAssertPtrNotNull(tc, p);
-  res = strlen(p);
-  CuAssertIntEquals(tc,UUID_SIZE,res);
-  res = uuid_parse(p, uu);
-  CuAssertIntEquals(tc,0,res);
-  type = uuid_type(uu); 
-  CuAssertIntEquals(tc,UUID_TYPE_DCE_RANDOM,type);
-  variant = uuid_variant(uu);
-  CuAssertIntEquals(tc,UUID_VARIANT_DCE,variant);
-
-#if defined(__linux__)
-  p = uuid_generate_random_linux(out, len);
-  CuAssertPtrNotNull(tc, p);
-  res = strlen(p);
-  CuAssertIntEquals(tc,UUID_SIZE,res);
-  res = uuid_parse(p, uu);
-  CuAssertIntEquals(tc,0,res);
-  type = uuid_type(uu); 
-  CuAssertIntEquals(tc,UUID_TYPE_DCE_RANDOM,type);
-  variant = uuid_variant(uu);
-  CuAssertIntEquals(tc,UUID_VARIANT_DCE,variant);
-#endif
-
-  p = sh_uuid_generate_random(out, len);
-  CuAssertPtrNotNull(tc, p);
-  res = strlen(p);
-  CuAssertIntEquals(tc,UUID_SIZE,res);
-  res = uuid_parse(p, uu);
-  CuAssertIntEquals(tc,0,res);
-  type = uuid_type(uu); 
-  CuAssertIntEquals(tc,UUID_TYPE_DCE_RANDOM,type);
-  variant = uuid_variant(uu);
-  CuAssertIntEquals(tc,UUID_VARIANT_DCE,variant);
-
-}
-#endif
Index: trunk/src/sh_hash.c
===================================================================
--- trunk/src/sh_hash.c	(revision 591)
+++ trunk/src/sh_hash.c	(revision 1)
@@ -20,14 +20,18 @@
 #include "config_xor.h"
 
+/* define this if you want version 1.3 style database file */
+/* #define OLD_BUG */
+
+/* make sure state changes of a file are always reported, even
+ *  with reportonlyonce=true
+ */
+/* #define REPLACE_OLD *//* moved to samhain.h */
+
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <sys/types.h>
-#ifdef HAVE_SYS_SYSMACROS_H
-#include <sys/sysmacros.h>
-#endif
 #include <sys/stat.h>
 #include <unistd.h>
-#include <ctype.h>
 
 #ifdef MAJOR_IN_MKDEV
@@ -43,71 +47,252 @@
 #endif
 
-
 #if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
 
-#include "samhain.h"
+#include "sh_hash.h"
 #include "sh_utils.h"
-#include "sh_unix.h"
-#include "sh_dbIO_int.h"
-#include "sh_dbIO.h"
-#include "sh_hash.h"
 #include "sh_error.h"
 #include "sh_tiger.h"
-#include "sh_sig.h"
+#include "sh_gpg.h"
 #include "sh_unix.h"
 #include "sh_files.h"
 #include "sh_ignore.h"
-#include "sh_pthread.h"
 
 #if defined(SH_WITH_CLIENT)
-#include "sh_xfer.h"
-#endif
-
-
-#define SH_KEY_NULL _("000000000000000000000000000000000000000000000000")
-
+#include "sh_forward.h"
+#endif
 
 #undef  FIL__
 #define FIL__  _("sh_hash.c")
 
-SH_MUTEX_INIT(mutex_hash,PTHREAD_MUTEX_INITIALIZER);
-
 static char * all_items (file_type * theFile, char * fileHash, int is_new);
 
-static const char  *policy[] = {
-  N_("[]"),
-  N_("[ReadOnly]"),
-  N_("[LogFiles]"),
-  N_("[GrowingLogs]"),
-  N_("[IgnoreNone]"),
-  N_("[IgnoreAll]"),
-  N_("[Attributes]"),
-  N_("[User0]"),
-  N_("[User1]"),
-  N_("[User2]"),
-  N_("[User3]"),
-  N_("[User4]"),
-  N_("[Prelink]"),
-  NULL
-};
-
-static int report_checkflags = S_FALSE;
-int set_report_checkflags(const char * c)
-{
-  return sh_util_flagval(c, &report_checkflags);
-}
-int get_report_checkflags()
-{
-  return report_checkflags;
-}
-
-
-
-const char * sh_hash_getpolicy(int class)
-{
-  if (class > 0 && class < SH_ERR_T_DIR)
-    return _(policy[class]);
-  return _("[indef]");
-}
+#define QUOTE_CHAR '='
+
+static char * unquote_string (char * str)
+{
+  int    i = 0, j, k = 0, len;
+  char * tmp;
+
+  SL_ENTER(_("unquote_string"));
+
+  if (str == NULL)
+    {
+      SL_RETURN(NULL, _("unquote_string"));
+    }
+
+  len = sl_strlen(str);
+
+  tmp = SH_ALLOC(len + 1);
+  for (j = 0; j <= len; ++j)
+    {
+      if (str[j] == QUOTE_CHAR && j < (len-2))
+	{
+	  if (sh_util_hexchar(str[j+1]) >= 0 && sh_util_hexchar(str[j+2]) >= 0)
+	    {
+	      i = 16 * sh_util_hexchar(str[j+1]) + sh_util_hexchar(str[j+2]);
+	      tmp[k] = i; j += 2;
+	    }
+	  else
+	    {
+	      tmp[k] = str[j];
+	    }
+	}
+      else
+	tmp[k] = str[j];
+      ++k;
+    }
+  SL_RETURN(tmp, _("unquote_string"));
+}
+
+static char i2h[2];
+
+char * int2hex (unsigned char i)
+{
+  int j, k;
+
+  j = i / 16;
+  k = i - (j*16);
+  if (j < 10) 
+    i2h[0] = '0'+j;
+  else
+    i2h[0] = 'A'+(j-10);
+  
+  if (k < 10) 
+    i2h[1] = '0'+k;
+  else
+    i2h[1] = 'A'+(k-10);
+
+  return i2h;
+}
+
+static char * quote_string (char * str)
+{
+  int    i = 0, j, k = 0, len;
+  char * tmp;
+  char * tmp2;
+
+  SL_ENTER(_("quote_string"));
+
+  if (str == NULL)
+    {
+      SL_RETURN(NULL, _("quote_string"));
+    }
+
+  len = sl_strlen(str);
+
+  for (j = 0; j < len; ++j)
+    if (str[j] == '\n' || str[j] == QUOTE_CHAR) ++i;
+
+  tmp = SH_ALLOC(len + 1 + 3*i);
+  for (j = 0; j <= len; ++j)
+    {
+      if (str[j] == '\n')
+	{
+	  tmp2 = int2hex((unsigned char) '\n'); /* was 'n', fixed in 1.5.4 */
+	  tmp[k] = QUOTE_CHAR; ++k;
+	  tmp[k] = tmp2[0];    ++k;
+	  tmp[k] = tmp2[1];
+	}
+      else if (str[j] == QUOTE_CHAR)
+	{
+	  tmp2 = int2hex((unsigned char) QUOTE_CHAR);
+	  tmp[k] = QUOTE_CHAR; ++k;
+	  tmp[k] = tmp2[0];    ++k;
+	  tmp[k] = tmp2[1];
+	}
+      else
+	{
+	  tmp[k] = str[j];
+	}
+      ++k;
+    }
+  SL_RETURN(tmp, _("quote_string"));
+}
+
+static UINT32 * swap_32 (UINT32 * iptr)
+{
+#ifdef WORDS_BIGENDIAN
+  unsigned char swap;
+  unsigned char * ii = (unsigned char *) iptr;
+  swap = ii[0]; ii[0] = ii[3]; ii[3] = swap;
+  swap = ii[1]; ii[1] = ii[2]; ii[2] = swap;
+  return iptr;
+#else
+  return iptr;
+#endif
+}
+
+static UINT64 *  swap_64 (UINT64 * iptr)
+{
+#ifdef WORDS_BIGENDIAN
+#ifdef UINT64_IS_32
+  swap_32 ((UINT32*) iptr);
+#else
+  unsigned char swap;
+  unsigned char * ii = (unsigned char *) iptr;
+  swap = ii[0]; ii[0] = ii[7]; ii[7] = swap;
+  swap = ii[1]; ii[1] = ii[6]; ii[6] = swap;
+  swap = ii[2]; ii[2] = ii[5]; ii[5] = swap;
+  swap = ii[3]; ii[3] = ii[4]; ii[4] = swap;
+#endif
+  return iptr;
+#else
+  return iptr;
+#endif
+}
+
+static unsigned short *  swap_short (unsigned short * iptr)
+{
+#ifdef WORDS_BIGENDIAN
+  if (sizeof(short) == 4)
+    swap_32 ((UINT32*) iptr);
+  else
+    {
+      /* alignment problem */
+      unsigned char swap;
+      static unsigned short ooop;
+      unsigned char * ii;
+      ooop = *iptr;
+      ii = (unsigned char *) &ooop;
+      /* printf("SWAP0: %hd  %d\n", *iptr, sizeof(unsigned short)); */
+      swap = ii[0]; ii[0] = ii[1]; ii[1] = swap;
+      /* printf("SWAP1: %hd\n", (unsigned short) ooop); */
+#ifndef OLD_BUG
+      return &ooop;
+#endif
+    }
+  return iptr;
+#else
+  return iptr;
+#endif
+}
+
+
+/* MAX_PATH_STORE must be >= KEY_LEN
+ */
+#define MAX_PATH_STORE 12287
+
+typedef struct store_info {
+  /*
+  char             fullpath[MAX_PATH_STORE+2];
+  char             linkpath[MAX_PATH_STORE+2];
+  */
+  UINT32           mode;
+  UINT32           linkmode;
+
+  UINT64           dev;
+  UINT64           rdev;
+  UINT32           hardlinks;
+  UINT32           ino;
+  UINT64           size;
+  UINT64           atime;
+  UINT64           mtime;
+  UINT64           ctime;
+  UINT32           owner;
+  UINT32           group;
+
+#ifdef OLD_BUG
+#if defined(__linux__)
+  UINT32           attributes;
+  char             c_attributes[16];
+#endif
+#else
+  /* #if defined(__linux__) */
+  UINT32           attributes;
+  char             c_attributes[16];
+  /* endif                  */
+#endif
+  unsigned short   mark;
+  char             c_owner[USER_MAX+2];
+  char             c_group[GROUP_MAX+2];
+  char             c_mode[11];
+  char             checksum[KEY_LEN+1];
+} sh_filestore_t;
+  
+typedef struct file_info {
+  sh_filestore_t   theFile;
+  char           * fullpath;
+  char           * linkpath;
+  int              visited;
+  int              reported;
+  int              allignore;
+  unsigned long    modi_mask;
+  struct           file_info * next;
+} sh_file_t;
+
+  static char  *policy[] = {
+    N_("[]"),
+    N_("[ReadOnly]"),
+    N_("[LogFiles]"),
+    N_("[GrowingLogs]"),
+    N_("[IgnoreNone]"),
+    N_("[IgnoreAll]"),
+    N_("[Attributes]"),
+    N_("[User0]"),
+    N_("[User1]"),
+    N_("[Prelink]"),
+    NULL
+  };
+
 
 /**********************************
@@ -120,4 +305,21 @@
 #include "sh_hash.h"
 
+/* must fit an int              */
+#define TABSIZE 2048  
+
+/* must fit an unsigned short   */
+/* changed for V0.8, as the     */
+/* database format has changed  */
+
+/* changed again for V0.9       */
+/* #define REC_MAGIC 19         */
+/* changed again for V1.3       */
+#ifdef OLD_BUG
+#define REC_MAGIC 20
+#else
+/* changed again for V1.4       */
+#define REC_MAGIC 21
+#endif
+
 
 /**************************************************************
@@ -126,7 +328,10 @@
  *
  **************************************************************/
-file_type * sh_hash_create_ft (const sh_file_t * p, char * fileHash)
+static file_type * sh_hash_create_ft (const sh_file_t * p, char * fileHash)
 {
   file_type * theFile;
+#if defined(__linux__)
+  int i = 16;
+#endif
 
   SL_ENTER(_("sh_hash_create_ft"));
@@ -136,17 +341,18 @@
   sl_strlcpy(theFile->c_mode, p->theFile.c_mode, 11);
   theFile->mode  =  p->theFile.mode;
-#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
-  sl_strlcpy(theFile->c_attributes, p->theFile.c_attributes, ATTRBUF_SIZE);
+#if defined(__linux__)
+  sl_strlcpy(theFile->c_attributes, p->theFile.c_attributes, i /* 16 */);
   theFile->attributes =  p->theFile.attributes;
 #endif
 
   sl_strlcpy(theFile->fullpath, p->fullpath, PATH_MAX);
-  if (p->linkpath != NULL /* && theFile->c_mode[0] == 'l' */)
-    {
-      theFile->link_path = sh_util_strdup(p->linkpath);
-    }
-  else
-    {
-      theFile->link_path = NULL;
+  if (p->linkpath != NULL && theFile->c_mode[0] == 'l')
+    {
+      sl_strlcpy(theFile->linkpath, p->linkpath, PATH_MAX);
+    }
+  else
+    {
+      theFile->linkpath[0] = '-';
+      theFile->linkpath[1] = '\0';
     }
   sl_strlcpy(fileHash, p->theFile.checksum, KEY_LEN+1);
@@ -167,52 +373,25 @@
   theFile->dev   =  p->theFile.dev;
   theFile->hardlinks = p->theFile.hardlinks;
-  theFile->check_flags = p->theFile.checkflags;
-
-  if (p->attr_string)
-    theFile->attr_string = sh_util_strdup(p->attr_string);
-  else
-    theFile->attr_string = NULL;
 
   SL_RETURN((theFile), _("sh_hash_create_ft"));
 }
 
-struct two_sh_file_t {
-  sh_file_t * prev;
-  sh_file_t * this;
-};
-
-static sh_file_t * hashsearch (const char * s);
-static int hashsearch_prev (const char * s, struct two_sh_file_t * a, int * index); 
-
+static sh_file_t * hashsearch (char * s);
+
+static sh_file_t * tab[TABSIZE];
 
 /**************************************************************
  *
- * >>>> The internal database <<<
+ * compute hash function
  *
  **************************************************************/
-
-static sh_file_t * tab[TABSIZE];
-
-sh_file_t ** get_default_data_table()
-{
-  return tab;
-}
-
-/**************************************************************
- *
- * compute hash function
- *
- **************************************************************/
-
-static int hashfunc(const char *s) 
-{
-  unsigned int n = 0; 
+static int hashfunc(char *s) 
+{
+  unsigned n = 0; 
 
   for ( ; *s; s++) 
     n = 31 * n + *s; 
-
-  return n & (TABSIZE - 1); /* % TABSIZE */; 
+  return n % TABSIZE; 
 } 
-
 
 int hashreport_missing( char *fullpath, int level)
@@ -223,12 +402,6 @@
   file_type * theFile;
   char * str;
-  char hashbuf[KEYBUF_SIZE];
-  volatile int  retval;
 
   /* --------  find the entry for the file ----------------       */
-
-  SH_MUTEX_LOCK(mutex_hash);
-
-  retval = 0;
 
   if (sl_strlen(fullpath) <= MAX_PATH_STORE) 
@@ -237,39 +410,18 @@
     p = hashsearch( sh_tiger_hash(fullpath, 
 				  TIGER_DATA, 
-				  sl_strlen(fullpath),
-				  hashbuf, sizeof(hashbuf))
+				  sl_strlen(fullpath))
 		    );
   if (p == NULL)
-    {
-      retval = -1;
-      goto unlock_and_return;
-    }
+    return -1;
 
   theFile = sh_hash_create_ft (p, fileHash);
   str = all_items(theFile, fileHash, 0);
   tmp = sh_util_safe_name(fullpath);
-
-  SH_MUTEX_LOCK(mutex_thread_nolog);
-  if (!sh_global_check_silent)
-    sh_error_handle (level, FIL__, __LINE__, 0, 
-		     MSG_FI_MISS2, tmp, str);
-  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-  ++sh.statistics.files_report;
-
+  sh_error_handle (level, FIL__, __LINE__, 0, 
+		   MSG_FI_MISS2, tmp, str);
   SH_FREE(tmp);
   SH_FREE(str);
-  if (theFile->attr_string) SH_FREE(theFile->attr_string);
-  if (theFile->link_path)   SH_FREE(theFile->link_path);
   SH_FREE(theFile);
-
- unlock_and_return:
-  ; /* 'label at end of compound statement */
-  SH_MUTEX_UNLOCK(mutex_hash);
-
-  /* remove here to avoid second message from hash_unvisited */
-  if (retval == 0)
-    sh_hash_remove (fullpath);
-
-  return retval;
+  return 0;
 }
 
@@ -280,25 +432,4 @@
  *
  **************************************************************/
-static sh_file_t * delete_db_entry(sh_file_t *p)
-{
-  if (p->fullpath)
-    {
-      SH_FREE(p->fullpath);
-      p->fullpath = NULL;
-    }
-  if (p->linkpath)
-    {
-      SH_FREE(p->linkpath);
-      p->linkpath = NULL;
-    }
-  if (p->attr_string)
-    {
-      SH_FREE(p->attr_string);
-      p->attr_string = NULL;
-    }
-  SH_FREE(p);
-  return NULL;
-}
-
 static void hash_unvisited (int j, 
 			    sh_file_t *prev, sh_file_t *p, ShErrLevel level)
@@ -310,5 +441,7 @@
   char   fileHash[KEY_LEN + 1];
   file_type * theFile;
+
   char * str;
+
 
   SL_ENTER(_("hash_unvisited"));
@@ -322,42 +455,38 @@
     }
 
-  /* Not a fully qualified path, i.e. some info stored by some module
+  /* Kernel info
    */
-  if (p->fullpath[0] != '/')
+  if (p->fullpath[0] == 'K')
     {
       SL_RET0(_("hash_unvisited"));
     }
 
-  /* visited   flag not set: not seen; 
-   * checked   flag     set: not seen (i.e. missing), and already checked 
-   * reported  flag not set: not reported yet
-   * allignore flag not set: not under IgnoreAll
+  /* visited   = FALSE: not seen; 
+   * visited   = 99:    not seen, and already checked 
+   * reported  = FALSE: not reported yet
+   * allignore = FALSE: not under IgnoreAll
    *
    * Files/directories under IgnoreAll are noticed as missing already
    * during the file check.
    */
-  if (((!SH_FFLAG_VISITED_SET(p->fflags)) || SH_FFLAG_CHECKED_SET(p->fflags)) 
-      && (!SH_FFLAG_REPORTED_SET(p->fflags))
-      /* && (!SH_FFLAG_ALLIGNORE_SET(p->fflags)) */)
+  if ((p->visited == S_FALSE || p->visited == 99) && p->reported == S_FALSE 
+      && p->allignore == S_FALSE)
     {
       i = retry_lstat(FIL__, __LINE__, p->fullpath, &buf);
 
-     /* if file does not exist
+      /* if file does not exist
        */
       if (0 != i)
 	{
-	  ptr = sh_util_dirname (p->fullpath);
+	  ptr = sh_util_basename (p->fullpath);
 	  if (ptr)
 	    {
 	      /* If any of the parent directories is under IgnoreAll
 	       */
-	      if ((0 != sh_files_is_allignore(ptr)) || SH_FFLAG_ALLIGNORE_SET(p->fflags))
+	      if (0 != sh_files_is_allignore(ptr))
 		level = ShDFLevel[SH_LEVEL_ALLIGNORE];
 	      SH_FREE(ptr);
 	    }
-
-	  /* Only report if !SH_FFLAG_CHECKED_SET
-	   */
-	  if (!SH_FFLAG_CHECKED_SET(p->fflags))
+	  if (p->visited != 99)
 	    {
 	      if (S_FALSE == sh_ignore_chk_del(p->fullpath))
@@ -367,11 +496,7 @@
 		  theFile = sh_hash_create_ft (p, fileHash);
 		  str = all_items(theFile, fileHash, 0);
-		  if (!sh_global_check_silent)
-		    sh_error_handle (level, FIL__, __LINE__, 0, 
-				     MSG_FI_MISS2, tmp, str);
-		  ++sh.statistics.files_report;
+		  sh_error_handle (level, FIL__, __LINE__, 0, 
+				   MSG_FI_MISS2, tmp, str);
 		  SH_FREE(str);
-		  if (theFile->attr_string) SH_FREE(theFile->attr_string);
-		  if (theFile->link_path)   SH_FREE(theFile->link_path);
 		  SH_FREE(theFile);
 
@@ -380,11 +505,19 @@
 	    }
 
-	  /* We rewrite the db on update, thus we need to keep this
+	  /* We rewrite the db on update, thus we need to push this out
 	   * if the user does not want to purge it from the db.
 	   */
-
-	  if ((sh.flag.reportonce == S_TRUE && sh.flag.update == S_FALSE) || 
-	      (S_TRUE == sh.flag.update && S_TRUE == sh_util_ask_update(p->fullpath)))
+	  if (sh.flag.checkSum == SH_CHECK_INIT && 
+	      sh.flag.update   == S_TRUE &&
+	      S_FALSE          == sh_util_ask_update(p->fullpath))
 	    {
+	      theFile = sh_hash_create_ft (p, fileHash);
+	      sh_hash_pushdata (theFile, fileHash);
+	      SH_FREE(theFile);
+	    }
+
+	  if (sh.flag.reportonce == S_TRUE)
+	    {
+#ifdef REPLACE_OLD
 	      /* Remove the old entry
 	       */
@@ -393,14 +526,26 @@
 	      else
 		prev->next = p->next;
-
-	      delete_db_entry(p);
-
+	      if (p->fullpath)
+		{
+		  SH_FREE(p->fullpath);
+		  p->fullpath = NULL;
+		}
+	      if (p->linkpath)
+		{
+		  SH_FREE(p->linkpath);
+		  p->linkpath = NULL;
+		}
+	      SH_FREE(p);
+	      p = NULL;
 	      SL_RET0(_("hash_unvisited"));
+#else
+	      p->reported = S_TRUE; 
+#endif
 	    }
 	}
     }
 
-  else if (SH_FFLAG_VISITED_SET(p->fflags) && SH_FFLAG_REPORTED_SET(p->fflags) 
-	   && (!SH_FFLAG_ALLIGNORE_SET(p->fflags)))
+  else if (p->visited == S_TRUE && p->reported == S_TRUE 
+	   && p->allignore == S_FALSE)
     {
       if (S_FALSE == sh_ignore_chk_new(p->fullpath))
@@ -410,11 +555,7 @@
 	  theFile = sh_hash_create_ft (p, fileHash);
 	  str = all_items(theFile, fileHash, 0);
-	  if (!sh_global_check_silent)
-	    sh_error_handle (level, FIL__, __LINE__, 0, 
-			     MSG_FI_MISS2, tmp, str);
-	  ++sh.statistics.files_report;
+	  sh_error_handle (level, FIL__, __LINE__, 0, 
+			   MSG_FI_MISS2, tmp, str);
 	  SH_FREE(str);
-	  if (theFile->attr_string)
-	    SH_FREE(theFile->attr_string);
 	  SH_FREE(theFile);
 
@@ -422,17 +563,13 @@
 	}
 
-      CLEAR_SH_FFLAG_REPORTED(p->fflags);
+      p->reported = S_FALSE;
     }
 
   if (sh.flag.reportonce == S_FALSE)
-    CLEAR_SH_FFLAG_REPORTED(p->fflags);
-
-  CLEAR_SH_FFLAG_VISITED(p->fflags);
-  CLEAR_SH_FFLAG_CHECKED(p->fflags);
-  SET_SH_FFLAG_ENOENT(p->fflags);
-
+    p->reported = S_FALSE;
+
+  p->visited = S_FALSE;
   SL_RET0(_("hash_unvisited"));
 }
-
 
 
@@ -447,6 +584,4 @@
 
   SL_ENTER(_("sh_hash_unvisited"));
-
-  SH_MUTEX_LOCK(mutex_hash);
   for (i = 0; i < TABSIZE; ++i)
     {
@@ -454,120 +589,5 @@
 	hash_unvisited (i, tab[i], tab[i], level);
     }
-  SH_MUTEX_UNLOCK(mutex_hash);
-
   SL_RET0(_("hash_unvisited"));
-}
-
-/*********************************************************************
- *
- * Remove a single file from the database.
- *
- *********************************************************************/
-void sh_hash_remove_unconditional (const char * path)
-{
-  struct two_sh_file_t entries;
-  int index;
-
-  SL_ENTER(_("sh_hash_remove_unconditional"));
-
-  SH_MUTEX_LOCK(mutex_hash);
-  if (0 == hashsearch_prev (path, &entries, &index))
-    {
-      sh_file_t * p = entries.this;
-      
-      /* Remove the old entry
-       */
-      if (entries.prev == p)
-	tab[index] = p->next;
-      else
-	entries.prev->next = p->next;
-      
-      delete_db_entry(p);
-    }
-  SH_MUTEX_UNLOCK(mutex_hash);
-
-  SL_RET0(_("sh_hash_remove_unconditional"));
-}
-
-void sh_hash_remove (const char * path)
-{
-  SL_ENTER(_("sh_hash_remove"));
-
-  if ((sh.flag.reportonce == S_TRUE && sh.flag.update == S_FALSE) || 
-      (S_TRUE == sh.flag.update && S_TRUE == sh_util_ask_update(path)))
-    {
-      sh_hash_remove_unconditional (path);
-    }
-  SL_RET0(_("sh_hash_remove"));
-}
-
-
-/*********************************************************************
- *
- * Search for unvisited entries in the database, custom error handler.
- *
- *********************************************************************/
-void sh_hash_unvisited_custom (char prefix, void(*handler)(const char * key))
-{
-  int i;
-  sh_file_t *p    = NULL;
-  sh_file_t *prev = NULL;
-  sh_file_t *next = NULL;
-
-  SL_ENTER(_("sh_hash_unvisited_custom"));
-
-  SH_MUTEX_LOCK(mutex_hash);
-  for (i = 0; i < TABSIZE; ++i)
-    {
-      if (tab[i] != NULL)
-	{
-	  p = tab[i]; prev = p;
-
-	  do 
-	    {
-	      next = p->next;
-
-	      if (p->fullpath && 
-		  prefix == p->fullpath[0])
-		{
-		  if ((!SH_FFLAG_VISITED_SET(p->fflags)) 
-		      && (!SH_FFLAG_REPORTED_SET(p->fflags)))
-		    {
-		      handler(p->fullpath);
-
-		      if (!SH_FFLAG_CHECKED_SET(p->fflags))
-			{
-			  /* delete */
-			  if (tab[i] == p)
-			    {
-			      tab[i] = p->next;
-			      prev   = tab[i];
-			      next   = prev;
-			    }
-			  else
-			    {
-			      prev->next = p->next;
-			      next       = prev->next;
-			    }
-
-			  p = delete_db_entry(p);
-			}
-		    }
-		  if (p)
-		    {
-		      CLEAR_SH_FFLAG_VISITED(p->fflags);
-		      CLEAR_SH_FFLAG_CHECKED(p->fflags);
-		    }
-		}
-	      if (p)
-		prev = p;
-	      p    = next;
-	    } 
-	  while (p);
-	}
-    }
-  SH_MUTEX_UNLOCK(mutex_hash);
-
-  SL_RET0(_("hash_unvisited_custom"));
 }
 
@@ -597,9 +617,4 @@
       SH_FREE(p->linkpath);
       p->linkpath = NULL;
-    }
-  if (p->attr_string)
-    {
-      SH_FREE(p->attr_string);
-      p->attr_string = NULL;
     }
   SH_FREE(p);
@@ -614,5 +629,5 @@
  *
  ***********************************************************************/
-static sh_file_t * hashsearch (const char * s) 
+static sh_file_t * hashsearch (char * s) 
 {
   sh_file_t * p;
@@ -620,43 +635,13 @@
   SL_ENTER(_("hashsearch"));
 
-  if (s)
-    {
-      for (p = tab[hashfunc(s)]; p; p = p->next)
-	if ((p->fullpath != NULL) && (0 == strcmp(s, p->fullpath))) 
-	  SL_RETURN( p, _("hashsearch"));
-    } 
+  if (!s)
+    SL_RETURN( NULL, _("hashsearch"));
+
+  for (p = tab[hashfunc(s)]; p; p = p->next) 
+    if ((p->fullpath != NULL) && (0 == strcmp(s, p->fullpath))) 
+      SL_RETURN( p, _("hashsearch")); 
   SL_RETURN( NULL, _("hashsearch"));
 } 
 
-static int hashsearch_prev (const char * s, struct two_sh_file_t * a, int * index) 
-{
-  sh_file_t * this;
-  sh_file_t * prev = NULL;
-
-  SL_ENTER(_("hashsearch_prev"));
-
-  if (s)
-    {
-      *index = hashfunc(s);
-      this   = tab[*index];
-      prev   = this;
-
-      if (this)
-	{
-	  do {
-	    if ((this->fullpath != NULL) && (0 == strcmp(s, this->fullpath)))
-	      {
-		a->prev = prev;
-		a->this = this;	
-		SL_RETURN( 0, _("hashsearch_prev"));
-	      }
-	    prev = this;
-	    this = this->next;
-	  } while(this);
-	} 
-    }
-  SL_RETURN( -1, _("hashsearch"));
-} 
-
 
 /***********************************************************************
@@ -665,8 +650,7 @@
  *
  ***********************************************************************/
-void hashinsert (sh_file_t * mtab[TABSIZE], sh_file_t * s) 
+static void hashinsert (sh_file_t * s) 
 {
   sh_file_t * p;
-  sh_file_t * q;
   int key;
 
@@ -675,36 +659,34 @@
   key = hashfunc(s->fullpath);
 
-  if (mtab[key] == NULL) 
-    {
-      mtab[key] = s;
-      mtab[key]->next = NULL;
+  if (tab[key] == NULL) 
+    {
+      tab[key] = s;
+      tab[key]->next = NULL;
       SL_RET0(_("hashinsert"));
     } 
   else 
     {
-      p = mtab[key];
+      p = tab[key];
       while (1) 
 	{
-	  if (p && p->fullpath && 0 == strcmp(s->fullpath, p->fullpath))
+	  if (p && p->fullpath && 
+	      0 == strcmp(s->fullpath, p->fullpath) &&
+	      strlen(s->fullpath) == strlen(p->fullpath))
 	    {
-	      q = p->next;
-	      SH_FREE(p->fullpath);
-	      if(p->linkpath)    SH_FREE(p->linkpath);
-	      if(p->attr_string) SH_FREE(p->attr_string);
-	      memcpy(p, s, sizeof(sh_file_t));
-	      p->next = q;
-	      SH_FREE(s); s = NULL;
+	      SH_FREE(s->fullpath);
+	      if(s->linkpath)
+		SH_FREE(s->linkpath);
+	      SH_FREE(s);
+	      s = NULL;
 	      SL_RET0(_("hashinsert"));
 	    }
-	  else if (p && p->next == NULL) 
-	    {
-	      p->next = s;
-	      p->next->next = NULL;
-	      SL_RET0(_("hashinsert"));
-	    }
-	  if (p)
-	    p = p->next;
-	  else /* cannot really happen, but llvm/clang does not know */
-	    break;
+	  else 
+	    if (p->next == NULL) 
+	      {
+		p->next = s;
+		p->next->next = NULL;
+		SL_RET0(_("hashinsert"));
+	      }
+	  p = p->next;
 	}
     }
@@ -713,7 +695,43 @@
 
 
-
 /******************************************************************
  *
+ * Get a single line
+ *
+ ******************************************************************/
+static FILE * sh_fin_fd = NULL;
+
+static int sh_hash_getline (FILE * fd, char * line, int sizeofline)
+{
+  register int  n = 0;
+  char        * res;
+
+  if (sizeofline < 2) {
+    line[0] = '\0';
+    return 0;
+  }
+  res = fgets(line, sizeofline, fd);
+  if (res == NULL)
+    {
+      line[0] = '\0';
+      return -1;
+    }
+  n = strlen(line);
+  if (n > 1) {
+    --n;
+    line[n] = '\0';
+  }
+  return n;
+}
+
+static void sh_hash_getline_end ()
+{
+  fclose (sh_fin_fd);
+  sh_fin_fd = NULL;
+  return;
+}
+
+/******************************************************************
+ *
  * ------- Check functions -------
  *
@@ -722,15 +740,244 @@
 static int IsInit = 0;
 
-void sh_hash_set_initialized()
-{
-  IsInit = 1;
-  return;
-}
-
-int sh_hash_get_initialized()
-{
-  return IsInit;
-}
-
+
+/******************************************************************
+ *
+ * Fast forward to start of data
+ *
+ ******************************************************************/
+int sh_hash_setdataent (SL_TICKET fd, char * line, int size, char * file)
+{
+  long i;
+  extern int get_the_fd (SL_TICKET ticket);
+
+  SL_ENTER(_("sh_hash_setdataent"));
+
+  sl_rewind (fd);
+
+  if (sh_fin_fd != NULL)
+    {
+      fclose (sh_fin_fd);
+      sh_fin_fd = NULL;
+    }
+
+  sh_fin_fd = fdopen(get_the_fd(fd), "rb");
+  if (!sh_fin_fd)
+    {
+      dlog(1, FIL__, __LINE__, 
+	   _("The file signature database: %s is not readable.\n"),
+	   (NULL == file) ? _("(null)") : file);
+      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_P_NODATA,
+		       ( (NULL == file) ? _("(null)") : file)
+		       );
+      aud_exit (FIL__, __LINE__, EXIT_FAILURE);
+    }
+
+  while (1) 
+    {
+      i =  sh_hash_getline (sh_fin_fd, line, size-1);
+      if (i < 0 ) 
+	{
+	  SH_FREE(line);
+	  dlog(1, FIL__, __LINE__, 
+	       _("The file signature database: %s does not\ncontain any data, or the start-of-file marker is missing (unlikely,\nunless modified by hand).\n"),
+	       (NULL == file) ? _("(null)") : file);
+	       
+	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_P_NODATA,
+			   ( (NULL == file) ? _("(null)") : file)
+			   );
+	  aud_exit (FIL__, __LINE__, EXIT_FAILURE);
+	}
+
+#if defined(SH_STEALTH)
+      if (0 == sl_strncmp (line, N_("[SOF]"), 5)) 
+#else
+      if (0 == sl_strncmp (line, _("[SOF]"),  5)) 
+#endif
+	break;
+    }
+  SL_RETURN( 1, _("sh_hash_setdataent"));
+}
+
+static int sh_hash_setdataent_old (SL_TICKET fd, char * line, int size, 
+				   char * file)
+{
+  long i;
+
+  SL_ENTER(_("sh_hash_setdataent_old"));
+
+  sl_rewind (fd);
+
+  while (1) 
+    {
+      i =  sh_unix_getline (fd, line, size-1);
+      if (i < 0 ) 
+	{
+	  SH_FREE(line);
+	  dlog(1, FIL__, __LINE__, 
+	       _("The file signature database: %s does not\ncontain any data, or the start-of-file marker is missing (unlikely,\nunless modified by hand).\n"),
+	       (NULL == file) ? _("(null)") : file);
+	       
+	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_P_NODATA,
+			   ( (NULL == file) ? _("(null)") : file)
+			   );
+	  aud_exit (FIL__, __LINE__, EXIT_FAILURE);
+	}
+
+#if defined(SH_STEALTH)
+      if (0 == sl_strncmp (line, N_("[SOF]"), 5)) 
+#else
+      if (0 == sl_strncmp (line, _("[SOF]"),  5)) 
+#endif
+	break;
+    }
+  SL_RETURN( 1, _("sh_hash_setdataent_old"));
+}
+
+/******************************************************************
+ *
+ * Read next record
+ *
+ ******************************************************************/
+sh_file_t *  sh_hash_getdataent (SL_TICKET fd, char * line, int size)
+{
+  sh_file_t * p;
+  sh_filestore_t ft;
+  long i;
+  char * fullpath;
+  char * linkpath;
+  char * tmp;
+
+  SL_ENTER(_("sh_hash_getdataent"));
+
+  (void) fd;
+
+  /* Read next record -- Part One 
+   */
+  p = SH_ALLOC(sizeof(sh_file_t));
+
+  i = fread (&ft, sizeof(sh_filestore_t), 1, sh_fin_fd);
+  /* i = sl_read(fd, &ft, sizeof(sh_filestore_t)); */
+  /* if ( SL_ISERROR(i) || i == 0) */
+  if (i < 1)
+    {
+      SH_FREE(p);
+      SL_RETURN( NULL, _("sh_hash_getdataent"));
+    }
+
+  swap_32(&(ft.mode));
+  swap_32(&(ft.linkmode));
+  swap_64(&(ft.dev));
+  swap_64(&(ft.rdev));
+  swap_32(&(ft.hardlinks));
+  swap_32(&(ft.ino));
+  swap_64(&(ft.size));
+  swap_64(&(ft.atime));
+  swap_64(&(ft.mtime));
+  swap_64(&(ft.ctime));
+  swap_32(&(ft.owner));
+  swap_32(&(ft.group));
+#if defined(__linux__)
+  swap_32(&(ft.attributes));
+#endif
+#ifdef OLD_BUG
+  swap_short(&(ft.mark));
+#else
+  ft.mark = *(swap_short(&(ft.mark)));
+#endif
+
+  if (ft.mark != REC_MAGIC)
+    {
+      SH_FREE(p);
+      SL_RETURN( NULL, _("sh_hash_getdataent"));
+    }
+
+  /* Read next record -- Part Two -- Fullpath
+   */
+  i =  sh_hash_getline (sh_fin_fd, line, size-1);
+  if (i < 0 ) 
+    {
+      SH_FREE(line);
+      SH_FREE(p);
+      dlog(1, FIL__, __LINE__, 
+	   _("There is a corrupt record in the file signature database: %s\nThe file path is missing.\n"),
+	   (NULL == file_path('D', 'R'))? _("(null)"):file_path('D', 'R'));
+      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_P_NODATA,
+			   ( (NULL == file_path('D', 'R')) ? _("(null)") :
+			     file_path('D', 'R'))
+			    );
+      aud_exit (FIL__, __LINE__,EXIT_FAILURE);
+    }
+
+  tmp = unquote_string (line);
+  i = sl_strlen(tmp)+1;
+  fullpath = SH_ALLOC(i);
+  sl_strlcpy (fullpath, tmp, i);
+  if (tmp)
+    SH_FREE(tmp);
+  if (fullpath[i-2] == '\n')
+    fullpath[i-2] = '\0';
+
+  /* Read next record -- Part Three -- Linkpath
+   */
+  i =  sh_hash_getline (sh_fin_fd, line, size-1);
+  if (i < 0 ) 
+    {
+      SH_FREE(line);
+      SH_FREE(fullpath);
+      SH_FREE(p);
+      dlog(1, FIL__, __LINE__, 
+	   _("There is a corrupt record in the file signature database: %s\nThe link path (or its placeholder) is missing.\n"),
+	   (NULL == file_path('D', 'R'))? _("(null)"):file_path('D', 'R'));
+      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_P_NODATA,
+		       ( (NULL == file_path('D', 'R')) ? _("(null)") :
+			 file_path('D', 'R'))
+		       );
+      aud_exit (FIL__, __LINE__,EXIT_FAILURE);
+    }
+
+  tmp = unquote_string (line);
+  i = sl_strlen(tmp)+1;
+  linkpath = SH_ALLOC(i);
+  sl_strlcpy (linkpath, tmp, i);
+  if (tmp)
+    SH_FREE(tmp);
+  if (linkpath[i-2] == '\n')
+    linkpath[i-2] = '\0';
+
+  /* Read next record -- Part Four -- Decode
+   */
+#if defined(SH_STEALTH)
+  sh_do_decode(fullpath,    sl_strlen(fullpath));
+  
+#if defined(__linux__)
+  sh_do_decode(ft.c_attributes,   sl_strlen(ft.c_attributes));
+#endif
+  
+  sh_do_decode(ft.c_mode,   sl_strlen(ft.c_mode));
+  sh_do_decode(ft.c_owner,  sl_strlen(ft.c_owner));
+  sh_do_decode(ft.c_group,  sl_strlen(ft.c_group));
+  sh_do_decode(ft.checksum, sl_strlen(ft.checksum));
+  
+  
+  if (ft.c_mode[0] == 'l') 
+    {  
+      sh_do_decode(linkpath, sl_strlen(linkpath));
+    }
+#endif
+
+  memcpy( &(*p).theFile, &ft, sizeof(sh_filestore_t) );
+  p->visited   = S_FALSE;
+  p->reported  = S_FALSE;
+  p->allignore = S_FALSE;
+  p->modi_mask = 0L;
+  p->fullpath  = fullpath;
+  p->linkpath  = linkpath;
+
+  /* set to an invalid value 
+   */
+  ft.mark = (REC_MAGIC + 5);
+
+  SL_RETURN( p, _("sh_hash_getdataent"));
+}
 
 /******************************************************************
@@ -741,64 +988,227 @@
 void sh_hash_init ()
 {
-  volatile int  retval  = 0;
-  volatile int  exitval = EXIT_SUCCESS;
+  sh_file_t * p;
+  SL_TICKET fd    = (-1);
+  long i;
+  int count = 0;
+  char * line = NULL;
+
+#if defined(WITH_GPG) || defined(WITH_PGP)
+  extern int get_the_fd (SL_TICKET ticket);
+  FILE *   fin_cp;
+
+  char * buf  = NULL;
+  int    bufc;
+  int    flag_pgp = S_FALSE;
+  int    flag_nohead = S_FALSE;
+  SL_TICKET fdTmp = (-1);
+  SL_TICKET open_tmp (void);
+#endif
 
   SL_ENTER(_("sh_hash_init"));
 
-  if ( sh.flag.checkSum == SH_CHECK_INIT )
-    {
-      dlog(1, FIL__, __LINE__, 
-	   _("Attempt to load the baseline database during initialisation. This is an internal error, please report it to the developer.\n"));
-      SH_ABORT;
+  if (IsInit == 1) 
+    SL_RET0(_("sh_hash_init"));
+
+  for (i = 0; i < TABSIZE; ++i) 
+    tab[i] = NULL;
+
+#if defined(SH_WITH_CLIENT)
+
+  /* Data file from Server
+   */
+
+  if (fd == (-1) && 0 == sl_strcmp(file_path('D', 'R'), _("REQ_FROM_SERVER")))
+    {
+      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_D_DSTART);
+      fd = sh_forward_req_file(_("DATA"));
+      if (SL_ISERROR(fd))
+	{
+	  dlog(1, FIL__, __LINE__, 
+	       _("Could not retrieve the file signature database from the server(errnum = %ld).\nPossible reasons include:\n - the server is not running,\n - session key negotiation failed (see the manual for proper setup), or\n - the server cannot access the file.\n"), fd); 
+	  sh_error_handle ((-1), FIL__, __LINE__, fd, MSG_EXIT_ABORT1, 
+			   sh.prg_name);
+	  aud_exit (FIL__, __LINE__, EXIT_FAILURE);
+	}
+      sl_rewind (fd);
+
+      tiger_fd = fd;
+      sl_strlcpy (sh.data.hash, 
+		  sh_tiger_hash (file_path('C', 'R'), /*irrelevant, TIGER_FD*/ 
+				 TIGER_FD, 0),
+		  KEY_LEN+1);
+      sl_rewind (fd);
+    }
+  else 
+#endif
+    /* Local data file
+     */
+
+    if (fd == (-1))
+      {
+	if ( SL_ISERROR(fd = sl_open_read(file_path('D', 'R'), SL_YESPRIV))) 
+	  {
+	    TPT(( 0, FIL__, __LINE__, _("msg=<Error opening: %s>\n"), 
+		  file_path('D', 'R')));
+	    dlog(1, FIL__, __LINE__, 
+		 _("Could not open the local file signature database for reading because\nof the following error: %s (errnum = %ld)\nIf this is a permission problem, you need to change file permissions\nto make the file readable for the effective UID: %d\n"), 
+		 sl_get_errmsg(), fd, (int) sl_ret_euid());
+	    sh_error_handle ((-1), FIL__, __LINE__, fd, MSG_EXIT_ABORT1, 
+			     sh.prg_name);
+	    aud_exit (FIL__, __LINE__, EXIT_FAILURE);
+	  }
+	
+	TPT(( 0, FIL__, __LINE__, _("msg=<Opened database: %s>\n"), 
+	      file_path('D', 'R')));
+
+	tiger_fd = fd;
+	if (0 != sl_strncmp(sh.data.hash, 
+			    sh_tiger_hash (file_path('D', 'R'), TIGER_FD, 0),
+			    KEY_LEN)
+	    && sh.flag.checkSum != SH_CHECK_INIT) 
+	  {
+	    dlog(1, FIL__, __LINE__, 
+		 _("The checksum of the file signature database has changed since startup.\n"));
+	    sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_AUTH,
+			     ( (NULL == file_path('D', 'R')) ? _("(null)") :
+			       file_path('D', 'R') )
+			     );
+	    aud_exit (FIL__, __LINE__, EXIT_FAILURE);
+	  }
+	sl_rewind (fd);
+
+      } /* new 1.4.8 */
+
+  if (sig_termfast == 1)  /* SIGTERM */
+    {
+      TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
+      --sig_raised; --sig_urgent;
+      aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
+    }
+
+#if defined(WITH_GPG) || defined(WITH_PGP)
+  /* new 1.4.8: also checked for server data */
+
+  /* extract the data and copy to temporary file
+   */
+  fdTmp = open_tmp();
+
+  fin_cp = fdopen(get_the_fd(fd), "rb");
+  buf = (char *) SH_ALLOC(8192);
+
+
+  /* while ( (bufc = sh_unix_getline (fd, buf, 8191)) > 0) */
+  while (NULL != fgets(buf, 8192, fin_cp))
+    {
+      bufc = 0; 
+      while (bufc < 8192) { 
+	if (buf[bufc] == '\n') { ++bufc; break; }
+	++bufc;
+      }
+
+      if (sig_termfast == 1)  /* SIGTERM */
+	{
+	  TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
+	  --sig_raised; --sig_urgent;
+	  aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
+	}
+
+      if (flag_pgp == S_FALSE &&
+	  (0 == sl_strcmp(buf, _("-----BEGIN PGP SIGNED MESSAGE-----\n"))||
+	   0 == sl_strcmp(buf, _("-----BEGIN PGP MESSAGE-----\n")))
+	  )
+	{
+	  flag_pgp = S_TRUE;
+	  sl_write(fdTmp, buf, bufc);
+	  continue;
+	}
+      
+      if (flag_pgp == S_TRUE && flag_nohead == S_FALSE)
+	{
+	  if (buf[0] == '\n')
+	    {
+	      flag_nohead = S_TRUE;
+	      sl_write(fdTmp, buf, 1);
+	      continue;
+	    }
+	  else if (0 == sl_strncmp(buf, _("Hash:"), 5) ||
+		   0 == sl_strncmp(buf, _("NotDashEscaped:"), 15))
+	    {
+	      sl_write(fdTmp, buf, bufc);
+	      continue;
+	    }
+	  else
+	    continue;
+	}
+    
+      if (flag_pgp == S_TRUE && buf[0] == '\n')
+	{
+	  sl_write(fdTmp, buf, 1);
+	}
+      else if (flag_pgp == S_TRUE)
+	{
+	  /* sl_write_line(fdTmp, buf, bufc); */
+	  sl_write(fdTmp, buf, bufc);
+	}
+      
+      if (flag_pgp == S_TRUE && 
+	  0 == sl_strcmp(buf, _("-----END PGP SIGNATURE-----\n")))
+	break;
+    }
+  SH_FREE(buf);
+  sl_close(fd);
+  fclose(fin_cp);
+
+  fd = fdTmp;
+  sl_rewind (fd);
+
+  /* Validate signature of open file.
+   */
+  if (0 != sh_gpg_check_sign (0, fd, 2))
+    {
       aud_exit (FIL__, __LINE__, EXIT_FAILURE);
     }
-
-  SH_MUTEX_LOCK(mutex_hash);
-
-  if (IsInit == 1)
-    { 
-      goto unlock_and_return;
-    }
-
-  /* Initialization completed.
+  sl_rewind (fd);
+#endif
+  /* } new 1.4.8 check sig also for files downloaded from server */
+
+  line = SH_ALLOC(MAX_PATH_STORE+1);
+
+  /* fast forward to start of data
    */
-  retval = sh_dbIO_load_db(tab);
-
-  if (0 == retval)
-    IsInit = 1;
-  else
-    exitval = EXIT_FAILURE;
-
- unlock_and_return:
-  ; /* 'label at end of compound statement */
-  SH_MUTEX_UNLOCK(mutex_hash);
-  if (retval == 0)
-    {
-      SL_RET0(_("sh_hash_init"));
-    }
-  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1, sh.prg_name);
-  aud_exit (FIL__, __LINE__, exitval);
-}
-
-void sh_hash_init_and_checksum()
-{
-  TPT((0, FIL__, __LINE__, _("msg=<Get checksum of the database.>\n")))
-  if (sh.flag.checkSum == SH_CHECK_CHECK) 
-    {
-      if (0 != sl_strcmp(file_path('D', 'R'), _("REQ_FROM_SERVER")))
-	{
-	  char hashbuf[KEYBUF_SIZE];
-	  (void) sl_strlcpy(sh.data.hash,
-			    sh_tiger_hash (file_path('D', 'R'), 
-					   TIGER_FILE, TIGER_NOLIM, 
-					   hashbuf, sizeof(hashbuf)), 
-			    KEY_LEN+1);
-	}
-
-      /* this eventually fetches the file from server to get checksum
-       */
-      sh_hash_init ();
-    }
-  return;
+  sh_hash_setdataent(fd, line, MAX_PATH_STORE, file_path('D', 'R'));
+
+  while (1) 
+    {
+      if (sig_termfast == 1)  /* SIGTERM */
+	{
+	  TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
+	  --sig_raised; --sig_urgent;
+	  aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
+	}
+
+      p = sh_hash_getdataent (fd, line, MAX_PATH_STORE);
+      if (p != NULL)
+	{
+	  hashinsert (p); 
+	  ++count;
+	}
+      else
+	break;
+    }
+
+  if (line != NULL)
+    SH_FREE(line);
+
+  /* Always keep db in memory, so we have no open file
+   */
+  sl_close (fd);
+  sh_hash_getline_end();
+  fd = -1;
+
+  /* --- Initialization done successfully. ---
+   */
+  IsInit = 1;
+  SL_RET0(_("sh_hash_init"));
 }
   
@@ -814,11 +1224,6 @@
   SL_ENTER(_("sh_hash_hashdelete"));
 
-  /* need deadlock detection here if called from exit handler 
-   */
-  SH_MUTEX_TRYLOCK(mutex_hash);
-
   if (IsInit == 0) 
-    goto unlock_and_exit;
-
+    SL_RET0(_("sh_hash_hashdelete"));
   for (i = 0; i < TABSIZE; ++i) 
     if (tab[i] != NULL)
@@ -828,20 +1233,459 @@
       }
   IsInit = 0;
-
- unlock_and_exit:
-  ; /* 'label at end of compound statement */
-  SH_MUTEX_TRYLOCK_UNLOCK(mutex_hash);
-
   SL_RET0(_("sh_hash_hashdelete"));
 }
 
-static int sh_loosedircheck = S_FALSE;
-
-int sh_hash_loosedircheck(const char * str)
-{
-  return sh_util_flagval(str, &sh_loosedircheck);
-}
-
-
+/******************************************************************
+ *
+ * Insert a file into the database.
+ *
+ ******************************************************************/ 
+static int       pushdata_isfirst =  1;
+static SL_TICKET pushdata_fd      = -1;
+
+static int       pushdata_stdout  =  S_FALSE;
+
+static char * sh_db_version_string = NULL;
+
+int sh_hash_pushdata_stdout (char * str)
+{
+  if (!str)
+    { pushdata_stdout  =  S_TRUE; return 0; }
+  return -1;
+}
+
+int sh_hash_version_string(char * str)
+{
+  int i;
+  if (str)
+    {
+      i = sl_strlen(str);
+      if (sh_db_version_string != NULL) {
+	SH_FREE(sh_db_version_string);
+      }
+      if (0 == sl_strncmp(str, _("NULL"), 4))
+	{
+	  sh_db_version_string = NULL;
+	  return 0;
+	}
+      sh_db_version_string = SH_ALLOC(i+1);
+      sl_strlcpy(sh_db_version_string, str, i+1);
+      return 0;
+    }
+  return -1;
+}
+
+#if 0
+void sh_hash_pushdata_reset ()
+{
+  if (!SL_ISERROR(pushdata_fd))
+    {
+      sl_close(pushdata_fd);
+      pushdata_fd = -1;
+    }
+  pushdata_isfirst =  1;
+}
+#endif
+
+void sh_hash_pushdata (file_type * buf, char * fileHash)
+{
+  static long p_count = 0;
+
+  int         status = 0;
+
+  char      * tmp;
+  size_t      tmp_len = 0;
+  size_t      old_len = 0;
+
+  sh_filestore_t p;
+
+  struct stat sbuf;
+
+  char *  fullpath = NULL;
+  char *  linkpath = NULL;
+
+  char * line = NULL;
+
+  char   timestring[81];
+  char * timep;
+
+#if !defined(__linux__)
+  int    i;
+#endif
+
+  SL_ENTER(_("sh_hash_pushdata"));
+
+  fullpath = SH_ALLOC(MAX_PATH_STORE+1);
+  linkpath = SH_ALLOC(MAX_PATH_STORE+1);
+
+  linkpath[0] =  '-'; 
+  linkpath[1] = '\0'; 
+  fullpath[0] =  '-'; 
+  fullpath[1] = '\0';
+
+  if (!buf) {
+    memset(&p, '\0', sizeof(sh_filestore_t));
+  }
+
+  if ((pushdata_stdout == S_TRUE) && (sh.flag.update == S_TRUE))
+    {
+      dlog(1, FIL__, __LINE__, 
+	   _("You cannot write the database to stdout when you use update rather than init.\n"));
+      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORTS,
+		      _("Writing database to stdout with update"), 
+		      sh.prg_name, 
+		      _("sh_hash_pushdata"));
+      aud_exit(FIL__, __LINE__, EXIT_FAILURE);
+    }
+
+  if ((pushdata_stdout == S_TRUE) && (sl_is_suid()))
+    {
+      dlog(1, FIL__, __LINE__, 
+	   _("You cannot write the database to stdout when running with suid privileges.\n"));
+      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORTS,
+		      _("Writing database to stdout when suid"), 
+		      sh.prg_name, 
+		      _("sh_hash_pushdata"));
+      aud_exit(FIL__, __LINE__, EXIT_FAILURE);
+    }
+
+
+  if ((pushdata_stdout == S_FALSE) && 
+      ( (NULL == file_path('D', 'W')) || 
+	(0 == sl_strcmp(file_path('D', 'W'), _("REQ_FROM_SERVER"))) ))
+    {
+      dlog(1, FIL__, __LINE__, 
+	   _("You need to configure a local path for initializing the database\nlike ./configure --with-data-file=REQ_FROM_SERVER/some/local/path\n"));
+      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORTS,
+		      _("No local path for database specified"), 
+		      sh.prg_name, 
+		      _("sh_hash_pushdata"));
+      aud_exit(FIL__, __LINE__, EXIT_FAILURE);
+    }
+
+
+  if ((pushdata_stdout == S_FALSE) && (pushdata_isfirst == 1))  
+    {
+      /* Warn that file already exists; file_path != NULL here because
+       * checked above
+       */
+      if (0 == retry_lstat(FIL__, __LINE__, file_path('D', 'W'), &sbuf))
+	{
+	  if (sh.flag.update == S_FALSE)
+	    {
+	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_FI_DBEX,
+			      file_path('D', 'W'));
+	    }
+	}
+    }
+
+
+  if (sh.flag.update == S_FALSE)
+    {
+      if (pushdata_stdout == S_FALSE)
+	{
+	  pushdata_fd = -1;
+	  if ( SL_ISERROR(pushdata_fd = sl_open_write(file_path('D', 'W'), SL_YESPRIV))) {
+	    SH_FREE(fullpath);
+	    SH_FREE(linkpath);
+	    sh_error_handle((-1), FIL__, __LINE__, pushdata_fd, MSG_E_ACCESS,
+			    geteuid(), file_path('D', 'W'));
+	    SL_RET0(_("sh_hash_pushdata"));
+	  }
+	  if ( SL_ISERROR(status = sl_forward(pushdata_fd))) {
+	    SH_FREE(fullpath);
+	    SH_FREE(linkpath);
+	    sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGPATH,
+			    _("Fast forward failed"), _("sh_hash_pushdata"),
+			    file_path('D', 'W'));
+	    SL_RET0(_("sh_hash_pushdata"));
+	  }
+	}
+    }
+  else /* update == TRUE */
+    {
+      if (pushdata_isfirst == 1)
+	{
+	  TPT((0, FIL__, __LINE__, _("msg=<Update.>\n")))
+	  if ( SL_ISERROR(pushdata_fd = sl_open_rdwr(file_path('D', 'W'), SL_YESPRIV))){
+	    SH_FREE(fullpath);
+	    SH_FREE(linkpath);
+	    sh_error_handle((-1), FIL__, __LINE__, pushdata_fd, MSG_E_ACCESS,
+			    geteuid(), file_path('D', 'W'));
+	    SL_RET0(_("sh_hash_pushdata"));
+	  }
+	  line = SH_ALLOC(MAX_PATH_STORE+1);
+	  if (SL_ISERROR(sh_hash_setdataent_old (pushdata_fd, line, 
+						 MAX_PATH_STORE, 
+						 file_path('D', 'W'))))
+	    {
+	      SH_FREE(fullpath);
+	      SH_FREE(linkpath);
+	      SH_FREE(line);
+	      SL_RET0(_("sh_hash_pushdata"));
+	    }
+	  SH_FREE(line);
+	}
+    }
+	 
+  if (buf != NULL && buf->fullpath != NULL) {
+
+    old_len = sl_strlen(buf->fullpath);
+#if defined(SH_STEALTH)
+    sh_do_encode(buf->fullpath, old_len);
+#endif
+    tmp = quote_string(buf->fullpath);
+    tmp_len = sl_strlen(tmp);
+#if defined(SH_STEALTH)
+    sh_do_decode(buf->fullpath, old_len);
+#endif
+
+    if (tmp_len <= MAX_PATH_STORE) 
+      {
+	sl_strlcpy(fullpath, buf->fullpath, MAX_PATH_STORE+1);
+	/*
+	if (sl_strlen(buf->fullpath) < (MAX_PATH_STORE-3))
+	  {
+	    fullpath[MAX_PATH_STORE-2] = '\0';
+	    fullpath[MAX_PATH_STORE-1] = '\n';
+	  }
+	*/
+      } 
+    else 
+      {
+	sl_strlcpy(fullpath, 
+		   sh_tiger_hash (buf->fullpath,
+				  TIGER_DATA, old_len), 
+		   KEY_LEN+1);
+      }
+    SH_FREE(tmp);
+  }
+
+#if defined(SH_STEALTH)
+  sh_do_encode(fullpath, sl_strlen(fullpath));
+#endif
+
+  tmp = quote_string(fullpath);
+  sl_strlcpy(fullpath, tmp, MAX_PATH_STORE+1);
+  SH_FREE(tmp);
+
+  if (buf != NULL && buf->c_mode[0] == 'l' && buf->linkpath != NULL) 
+    {  
+
+      old_len = sl_strlen(buf->linkpath);
+#if defined(SH_STEALTH)
+      sh_do_encode(buf->linkpath, old_len);
+#endif
+      tmp = quote_string(buf->linkpath);
+      tmp_len = sl_strlen(tmp);
+#if defined(SH_STEALTH)
+      sh_do_decode(buf->linkpath, old_len);
+#endif
+
+      if (tmp_len <= MAX_PATH_STORE) 
+	{
+	  sl_strlcpy(linkpath, buf->linkpath, MAX_PATH_STORE+1);  
+	} 
+      else 
+	{
+	  sl_strlcpy(linkpath, 
+		     sh_tiger_hash (buf->linkpath,
+				    TIGER_DATA, old_len),
+		     KEY_LEN+1);
+	}
+      SH_FREE(tmp);
+
+#if defined(SH_STEALTH)
+      sh_do_encode(linkpath, sl_strlen(linkpath));
+#endif
+      tmp = quote_string(linkpath);
+      sl_strlcpy(linkpath, tmp, MAX_PATH_STORE+1);
+      SH_FREE(tmp);
+    }
+
+  if (buf != NULL) {
+    p.mark = REC_MAGIC;
+    sl_strlcpy(p.c_mode,   buf->c_mode,   11);
+    sl_strlcpy(p.c_group,  buf->c_group,  GROUP_MAX+1);
+    sl_strlcpy(p.c_owner,  buf->c_owner,  USER_MAX+1);
+    if (fileHash) {
+      sl_strlcpy(p.checksum, fileHash,      KEY_LEN+1);
+    }
+#if defined(__linux__)
+    sl_strlcpy(p.c_attributes, buf->c_attributes, 13);
+#else
+    for (i = 0; i < 12; ++i) p.c_attributes[i] = '-';
+    p.c_attributes[12] = '\0';
+#endif
+    
+#if defined(SH_STEALTH)
+    sh_do_encode(p.c_mode,   sl_strlen(p.c_mode));
+    sh_do_encode(p.c_owner,  sl_strlen(p.c_owner));
+    sh_do_encode(p.c_group,  sl_strlen(p.c_group));
+    sh_do_encode(p.checksum, sl_strlen(p.checksum));
+
+    sh_do_encode(p.c_attributes,   sl_strlen(p.c_attributes));
+#endif
+    
+#if defined(__linux__)
+    p.attributes  = (UINT32) buf->attributes;
+#else
+    p.attributes  = 0;
+#endif
+    p.linkmode    = (UINT32) buf->linkmode;
+    p.hardlinks   = (UINT32) buf->hardlinks;
+    p.dev   = (UINT64) buf->dev;
+    p.rdev  = (UINT64) buf->rdev;
+    p.mode  = (UINT32) buf->mode;
+    p.ino   = (UINT32) buf->ino;
+    p.size  = (UINT64) buf->size;
+    p.mtime = (UINT64) buf->mtime;
+    p.atime = (UINT64) buf->atime;
+    p.ctime = (UINT64) buf->ctime;
+    p.owner = (UINT32) buf->owner;
+    p.group = (UINT32) buf->group;
+    
+    swap_32(&(p.mode));
+    swap_32(&(p.linkmode));
+    swap_64(&(p.dev));
+    swap_64(&(p.rdev));
+    swap_32(&(p.hardlinks));
+    swap_32(&(p.ino));
+    swap_64(&(p.size));
+    swap_64(&(p.atime));
+    swap_64(&(p.mtime));
+    swap_64(&(p.ctime));
+    swap_32(&(p.owner));
+    swap_32(&(p.group));
+    swap_32(&(p.attributes));
+
+#ifdef OLD_BUG
+    swap_short(&(p.mark));
+#else
+    p.mark = *(swap_short(&(p.mark)));
+#endif
+  }
+
+  /* write the start marker 
+   */
+  if (pushdata_isfirst == 1) 
+    {
+      if (sh.flag.update == S_FALSE)
+	{
+	  if (sh_db_version_string != NULL)
+	    {
+	      if (pushdata_stdout == S_FALSE)
+		{
+		  sl_write (pushdata_fd, _("\n#Host "), 7);
+		  sl_write (pushdata_fd, sh.host.name, 
+			    sl_strlen(sh.host.name));
+		  sl_write (pushdata_fd, _(" Version "), 9);
+		  sl_write (pushdata_fd, sh_db_version_string, 
+			    sl_strlen(sh_db_version_string));
+		  sl_write (pushdata_fd, _(" Date "), 6);
+		  timep = sh_unix_time(0);
+		  sl_strlcpy(timestring, timep, sizeof(timestring));
+		  sl_write (pushdata_fd, timestring, sl_strlen(timestring));
+		  sl_write (pushdata_fd,        "\n", 1);
+		} else {
+		  printf (_("\n#Host "));
+		  printf ("%s", sh.host.name);
+		  printf (_(" Version "));
+		  printf ("%s", sh_db_version_string);
+		  printf (_(" Date "));
+		  timep = sh_unix_time(0);
+		  sl_strlcpy(timestring, timep, sizeof(timestring));
+		  printf ("%s\n", timestring);
+		}
+	    }
+
+	  if (pushdata_stdout == S_FALSE)
+	    {
+#if defined(SH_STEALTH)
+	      sl_write      (pushdata_fd,        "\n", 1);
+	      sl_write_line (pushdata_fd, N_("[SOF]"), 5);
+#else
+	      sl_write_line (pushdata_fd, _("\n[SOF]"),  6);
+#endif
+	    }
+	  else 
+	    {
+#if defined(SH_STEALTH)
+	      printf ("\n%s\n", N_("[SOF]"));
+#else
+	      printf ("%s\n", _("\n[SOF]"));
+#endif
+	    }
+	}
+      pushdata_isfirst = 0;
+    }
+      
+  if (pushdata_stdout == S_FALSE)
+    {
+      sl_write      (pushdata_fd,       &p, sizeof(sh_filestore_t));
+      sl_write_line (pushdata_fd, fullpath,    sl_strlen(fullpath));
+      sl_write_line (pushdata_fd, linkpath,    sl_strlen(linkpath));
+    } else {
+      fwrite (&p, sizeof(sh_filestore_t), 1, stdout);
+      printf ("%s\n", fullpath);
+      printf ("%s\n", linkpath);
+    }
+
+  ++p_count;
+
+  if ((sh.flag.update != S_TRUE) && (pushdata_stdout == S_FALSE))
+    {
+      sl_close (pushdata_fd);
+      pushdata_fd = -1;
+    }
+
+  SH_FREE(fullpath);
+  SH_FREE(linkpath);
+
+  SL_RET0(_("sh_hash_pushdata"));
+}
+
+int sh_hash_writeout()
+{
+  sh_file_t * p;
+  int         i;
+  file_type * f;
+  char   fileHash[KEY_LEN + 1];
+
+  SL_ENTER(_("sh_hash_writeout"));
+
+  if (S_TRUE == file_is_remote())
+    {
+      sh_error_handle((-1), FIL__, __LINE__, S_FALSE, MSG_E_SUBGEN, 
+		      _("Baseline database is remote"), _("sh_hash_writeout"));
+      SL_RETURN (1, _("sh_hash_writeout"));
+    }
+
+  if (!SL_ISERROR(pushdata_fd))
+    {
+      sl_close(pushdata_fd);
+      pushdata_fd = -1;
+    }
+  pushdata_isfirst =  1;
+
+  for (i = 0; i < TABSIZE; ++i)
+    {
+      for (p = tab[i]; p; p = p->next)
+	{
+	  f = sh_hash_create_ft (p, fileHash);
+	  sh_hash_pushdata (f, fileHash);
+	  SH_FREE(f);
+	}
+    }
+
+  if (!SL_ISERROR(pushdata_fd))
+    {
+      sl_close(pushdata_fd);
+      pushdata_fd = -1;
+    }
+  pushdata_isfirst =  1;
+
+  SL_RETURN (0, _("sh_hash_writeout"));
+}
 
 
@@ -851,176 +1695,55 @@
  *
  *********************************************************************/
-static sh_file_t *  sh_hash_have_it_int (const char * newname)
+static sh_file_t *  sh_hash_have_it_int (char * newname)
 {
   sh_file_t * p;
-  char hashbuf[KEYBUF_SIZE];
-
-  SL_ENTER(_("sh_hash_have_it_int"));
+
+  SL_ENTER(_("sh_hash_have_it"));
 
   if (newname == NULL)
-    SL_RETURN( (NULL), _("sh_hash_have_it_int"));
-
+    SL_RETURN( (NULL), _("sh_hash_have_it"));
+
+  if (IsInit != 1) 
+    sh_hash_init();
   if (sl_strlen(newname) <= MAX_PATH_STORE) 
     p = hashsearch(newname);
   else 
-    p = hashsearch ( sh_tiger_hash(newname, TIGER_DATA, sl_strlen(newname),
-				   hashbuf, sizeof(hashbuf)) );
+    p = hashsearch ( sh_tiger_hash(newname, TIGER_DATA, sl_strlen(newname)) );
   if (p == NULL) 
-     SL_RETURN( (NULL), _("sh_hash_have_it_int"));
-
-  SL_RETURN( (p), _("sh_hash_have_it_int"));
-}
-
-int sh_hash_have_it (const char * newname)
-{
-  sh_file_t * p;
-  int retval;
-
-  if (IsInit != 1) 
-    sh_hash_init();
-
-  SH_MUTEX_LOCK(mutex_hash);
-
-  retval = 0;
-
-  p = sh_hash_have_it_int (newname);
-
-  if (!p) 
-    retval = (-1);
-  else if ((!SH_FFLAG_ALLIGNORE_SET(p->fflags)) && 
-	   (p->modi_mask & MODI_CHK) != 0 &&
-	   (p->modi_mask & MODI_MOD) != 0)
-    retval = 1;
-  SH_MUTEX_UNLOCK(mutex_hash);
-
-  return retval;
-}
-
-int sh_hash_get_it (const char * newname, file_type * tmpFile, char * fileHash)
-{
-  sh_file_t * p;
-  int retval;
-
-  if (IsInit != 1) 
-    sh_hash_init();
-
-  tmpFile->link_path   = NULL;
-  tmpFile->attr_string = NULL;
-
-  SH_MUTEX_LOCK(mutex_hash);
-
-  retval = (-1);
-
-  p = sh_hash_have_it_int (newname);
-  if (p)
-    {
-      sl_strlcpy(tmpFile->fullpath,  p->fullpath, PATH_MAX);
-      if (p->linkpath)
-	tmpFile->link_path = sh_util_strdup (p->linkpath);
-      tmpFile->size  = p->theFile.size;
-      tmpFile->mtime = p->theFile.mtime;
-      tmpFile->ctime = p->theFile.ctime;
-      tmpFile->atime = p->theFile.atime;
-
-      if (NULL != fileHash)
-	sl_strlcpy(fileHash, p->theFile.checksum, KEY_LEN+1);
-
-      tmpFile->attr_string = NULL;
-      retval = 0;
-    }
-  SH_MUTEX_UNLOCK(mutex_hash);
-
-  return retval;
-}
-
-int sh_hash_getflags (char * filename)
-{
-  sh_file_t * p;
-  int retval = 0;
-
-  if ( sh.flag.checkSum != SH_CHECK_INIT )
-    {
-      if (IsInit != 1) 
-	sh_hash_init();
-      
-      SH_MUTEX_LOCK(mutex_hash);
-      p = sh_hash_have_it_int (filename);
-      if (p)
-	retval = p->fflags;
-      else
-	retval = -1;
-      SH_MUTEX_UNLOCK(mutex_hash);
-    }
-  return retval;
-}
-
-int sh_hash_setflags (char * filename, int flags)
-{
-  sh_file_t * p;
-  int retval = 0;
-
-  if ( sh.flag.checkSum != SH_CHECK_INIT )
-    {
-      if (IsInit != 1) 
-	sh_hash_init();
-      
-      SH_MUTEX_LOCK(mutex_hash);
-      p = sh_hash_have_it_int (filename);
-      if (p)
-	{
-	  p->fflags = flags;
-	  retval = 0;
-	}
-      else
-	retval = -1;
-      SH_MUTEX_UNLOCK(mutex_hash);
-    }
-  return retval;
-}
-
-/* needs lock to be threadsafe
- */
-void sh_hash_set_flag (char * filename, int flag_to_set)
-{
-  sh_file_t * p;
-
-  if ( sh.flag.checkSum != SH_CHECK_INIT )
-    {
-      if (IsInit != 1) 
-	sh_hash_init();
-      
-      SH_MUTEX_LOCK(mutex_hash);
-      p = sh_hash_have_it_int (filename);
-      if (p)
-	{
-	  p->fflags |= flag_to_set;
-	}
-      SH_MUTEX_UNLOCK(mutex_hash);
-    }
-  return;
-}
-
-/* needs lock to be threadsafe
- */
-void sh_hash_clear_flag (char * filename, int flag_to_clear)
-{
-  sh_file_t * p;
-
-  if ( sh.flag.checkSum != SH_CHECK_INIT )
-    {
-      if (IsInit != 1) 
-	sh_hash_init();
-      
-      SH_MUTEX_LOCK(mutex_hash);
-      p = sh_hash_have_it_int (filename);
-      if (p)
-	{
-	  p->fflags &= ~flag_to_clear;
-	}
-      SH_MUTEX_UNLOCK(mutex_hash);
-    }
-  return;
-}
-
+     SL_RETURN( (NULL), _("sh_hash_have_it"));
+  /*
+  if (p->allignore == S_FALSE && 
+      (p->modi_mask & MODI_CHK) != 0 &&
+      (p->modi_mask & MODI_MOD) != 0)
+    SL_RETURN( (1), _("sh_hash_have_it"));
+  */
+  SL_RETURN( (p), _("sh_hash_have_it"));
+}
+
+int sh_hash_have_it (char * newname)
+{
+  sh_file_t * p = sh_hash_have_it_int (newname);
+
+  if (!p) return (-1);
+  if (p->allignore == S_FALSE && 
+      (p->modi_mask & MODI_CHK) != 0 &&
+      (p->modi_mask & MODI_MOD) != 0)
+    return 1;
+  return 0;
+}
+
+int sh_hash_get_it (char * newname, file_type * tmpFile)
+{
+  sh_file_t * p = sh_hash_have_it_int (newname);
+  if (!p)
+    return (-1);
+  sl_strlcpy(tmpFile->fullpath, p->fullpath, PATH_MAX);
+  sl_strlcpy(tmpFile->linkpath, p->linkpath, PATH_MAX);
+  tmpFile->size  = p->theFile.size;
+  tmpFile->mtime = p->theFile.mtime;
+  tmpFile->ctime = p->theFile.ctime;
+  return 0;
+}
+ 
 
 /*****************************************************************
@@ -1034,6 +1757,4 @@
 {
   sh_file_t * p;
-  char hashbuf[KEYBUF_SIZE];
-  int  retval;
 
   SL_ENTER(_("sh_hash_set_visited_int"));
@@ -1041,40 +1762,25 @@
   if (newname == NULL)
     SL_RETURN((-1), _("sh_hash_set_visited_int"));
-
   if (IsInit != 1) 
     sh_hash_init();
-
-  SH_MUTEX_LOCK(mutex_hash);
 
   if (sl_strlen(newname) <= MAX_PATH_STORE) 
     p = hashsearch(newname);
   else 
-    p = hashsearch (sh_tiger_hash(newname, TIGER_DATA, sl_strlen(newname),
-				  hashbuf, sizeof(hashbuf)));
+    p = hashsearch (sh_tiger_hash(newname, TIGER_DATA, sl_strlen(newname)));
   
-  if (p)
-    {
-      if (flag == SH_FFLAG_CHECKED)
-	{
-	  CLEAR_SH_FFLAG_REPORTED(p->fflags);
-	  CLEAR_SH_FFLAG_VISITED(p->fflags);
-	  SET_SH_FFLAG_CHECKED(p->fflags);
-	}
-      else
-	{
-	  SET_SH_FFLAG_VISITED(p->fflags);
-	  CLEAR_SH_FFLAG_CHECKED(p->fflags);
-	  if (flag == SH_FFLAG_REPORTED)
-	    SET_SH_FFLAG_REPORTED(p->fflags);
-	  else
-	    CLEAR_SH_FFLAG_REPORTED(p->fflags);
-	}
-      retval = 0;
-    }
-  else
-    retval = -1;
-
-  SH_MUTEX_UNLOCK(mutex_hash);
-  SL_RETURN((retval), _("sh_hash_set_visited_int"));
+  if (p == NULL) 
+    SL_RETURN((-1), _("sh_hash_set_visited_int"));
+  if (flag == 99)
+    {
+      p->visited  = 99;
+      p->reported = S_FALSE;
+    }
+  else
+    {
+      p->visited  = S_TRUE;
+      p->reported = flag;
+    }
+  SL_RETURN((0), _("sh_hash_set_visited_int"));
 }
 
@@ -1085,33 +1791,22 @@
 {
   int i;
-  SL_ENTER(_("sh_hash_set_missing"));
-
-  i = sh_hash_set_visited_int(newname, SH_FFLAG_CHECKED);
-
-  if (sh.flag.checkSum != SH_CHECK_INIT) {
-    sh_hash_remove(newname);
-  }
-
-  SL_RETURN(i, _("sh_hash_set_missing"));
-}
-
-/* mark the file as visited and reported
- */
+  SL_ENTER(_("sh_hash_set_visited"));
+  i = sh_hash_set_visited_int(newname, 99);
+  SL_RETURN(i, _("sh_hash_set_visited"));
+}
+
 int sh_hash_set_visited (char * newname)
 {
   int i;
   SL_ENTER(_("sh_hash_set_visited"));
-  i = sh_hash_set_visited_int(newname, SH_FFLAG_REPORTED);
+  i = sh_hash_set_visited_int(newname, S_TRUE);
   SL_RETURN(i, _("sh_hash_set_visited"));
 }
 
-/* mark the file as visited and NOT reported
- * used to avoid deletion of file from internal database
- */
 int sh_hash_set_visited_true (char * newname)
 {
   int i;
   SL_ENTER(_("sh_hash_set_visited_true"));
-  i = sh_hash_set_visited_int(newname, 0);
+  i = sh_hash_set_visited_int(newname, S_FALSE);
   SL_RETURN(i, _("sh_hash_set_visited_true"));
 }
@@ -1124,66 +1819,76 @@
  ******************************************************************/
 
-void sh_hash_push2db (const char * key, struct store2db * save)
-{
+static char * sh_hash_charhex( unsigned char i )
+{
+  static char i2h[2];
+  int j, k;
+
+  j = i / 16;
+  k = i - (j*16);
+
+  if (j < 10) i2h[0] = '0'+j;
+  else        i2h[0] = 'A'+(j-10);
+  
+  if (k < 10) i2h[1] = '0'+k;
+  else        i2h[1] = 'A'+(k-10);
+
+  return i2h;
+}
+
+void sh_hash_push2db (char * key, unsigned long val1, 
+		      unsigned long val2, unsigned long val3,
+		      unsigned char * str, int size)
+{
+  file_type   tmpFile;
   int         i = 0;
   char      * p;
-  char        i2h[2];
-  file_type * tmpFile = SH_ALLOC(sizeof(file_type));
-
-  int size            = save->size;
-  unsigned char * str = save->str;
-
-
-  tmpFile->attr_string = NULL;
-  tmpFile->link_path   = NULL;
-
-  sl_strlcpy(tmpFile->fullpath, key, PATH_MAX);
-  tmpFile->size  = save->val0;
-  tmpFile->mtime = save->val1;
-  tmpFile->ctime = save->val2;
-  tmpFile->atime = save->val3;
-
-  tmpFile->mode  = 0;
-  tmpFile->owner = 0;
-  tmpFile->group = 0;
-  sl_strlcpy(tmpFile->c_owner, _("root"), 5);
-  sl_strlcpy(tmpFile->c_group, _("root"), 5);
-
-  tmpFile->check_flags = 0;
+
+  sl_strlcpy(tmpFile.fullpath, key, PATH_MAX);
+  tmpFile.size  = val1;
+  tmpFile.mtime = val2;
+  tmpFile.ctime = val3;
+
+  tmpFile.atime = 0;
+  tmpFile.mode  = 0;
+  tmpFile.owner = 0;
+  tmpFile.group = 0;
+  sl_strlcpy(tmpFile.c_owner, _("root"), 5);
+  sl_strlcpy(tmpFile.c_group, _("root"), 5);
 
   if ((str != NULL) && (size < (PATH_MAX/2)-1))
     {
-      tmpFile->c_mode[0] = 'l';  
-      tmpFile->c_mode[1] = 'r'; tmpFile->c_mode[2]  = 'w';
-      tmpFile->c_mode[3] = 'x'; tmpFile->c_mode[4]  = 'r'; 
-      tmpFile->c_mode[5] = 'w'; tmpFile->c_mode[6]  = 'x'; 
-      tmpFile->c_mode[7] = 'r'; tmpFile->c_mode[8]  = 'w'; 
-      tmpFile->c_mode[9] = 'x'; tmpFile->c_mode[10] = '\0';
-      tmpFile->link_path = SH_ALLOC((size * 2) + 2);
+      tmpFile.c_mode[0] = 'l';  
+      tmpFile.c_mode[1] = 'r'; tmpFile.c_mode[2]  = 'w';
+      tmpFile.c_mode[3] = 'x'; tmpFile.c_mode[4]  = 'r'; 
+      tmpFile.c_mode[5] = 'w'; tmpFile.c_mode[6]  = 'x'; 
+      tmpFile.c_mode[7] = 'r'; tmpFile.c_mode[8]  = 'w'; 
+      tmpFile.c_mode[9] = 'x'; tmpFile.c_mode[10] = '\0'; 
       for (i = 0; i < size; ++i)
 	{
-	  p = sh_util_charhex (str[i],i2h);
-	  tmpFile->link_path[2*i]   = p[0];
-	  tmpFile->link_path[2*i+1] = p[1];
-	  tmpFile->link_path[2*i+2] = '\0';
-	}
-    }
-  else
-    {
-      for (i = 0; i < 10; ++i) 
-	tmpFile->c_mode[i] = '-';
-      tmpFile->c_mode[10] = '\0';
-      tmpFile->link_path = sh_util_strdup("-");
-    }
-
-  if (sh.flag.checkSum == SH_CHECK_INIT)
-    sh_dbIO_data_write (tmpFile, 
-			(save->checksum[0] == '\0') ? SH_KEY_NULL : save->checksum);
-  else
-    sh_hash_pushdata_memory (tmpFile, 
-			     (save->checksum[0] == '\0') ? SH_KEY_NULL : save->checksum);
-
-  if (tmpFile->link_path) SH_FREE(tmpFile->link_path);
-  SH_FREE(tmpFile);
+	  p = sh_hash_charhex (str[i]);
+	  tmpFile.linkpath[2*i]   = p[0];
+	  tmpFile.linkpath[2*i+1] = p[1];
+	  tmpFile.linkpath[2*i+2] = '\0';
+	}
+    }
+  else
+    {
+      tmpFile.c_mode[0] = '-';  
+      tmpFile.c_mode[1] = '-'; tmpFile.c_mode[2]  = '-';
+      tmpFile.c_mode[3] = '-'; tmpFile.c_mode[4]  = '-'; 
+      tmpFile.c_mode[5] = '-'; tmpFile.c_mode[6]  = '-'; 
+      tmpFile.c_mode[7] = '-'; tmpFile.c_mode[8]  = '-'; 
+      tmpFile.c_mode[9] = '-'; tmpFile.c_mode[10] = '\0'; 
+      tmpFile.linkpath[0] = '-'; tmpFile.linkpath[1] = '\0';
+    }
+
+  if (sh.flag.checkSum == SH_CHECK_CHECK && 
+      sh.flag.update == S_TRUE)
+    sh_hash_pushdata_memory (&tmpFile, 
+			     _("000000000000000000000000000000000000000000000000"));
+  else
+    sh_hash_pushdata (&tmpFile, 
+		      _("000000000000000000000000000000000000000000000000"));
+
   return;
 }
@@ -1191,59 +1896,55 @@
 extern int sh_util_hextobinary (char * binary, char * hex, int bytes);
 
-char * sh_hash_db2pop (const char * key, struct store2db * save)
-{
+char * sh_hash_db2pop (char * key, unsigned long * val1, 
+		       unsigned long * val2, unsigned long * val3,
+		       int * size)
+{
+  file_type   tmpFile;
   size_t      len;
   char      * p;
   int         i;
-  char      * retval = NULL;
-  char        fileHash[KEY_LEN+1];
-  file_type * tmpFile = SH_ALLOC(sizeof(file_type));
-  
-  save->size = 0;
-
-  if (0 == sh_hash_get_it (key, tmpFile, fileHash))
-    {
-      save->val0 = tmpFile->size;
-      save->val1 = tmpFile->mtime;
-      save->val2 = tmpFile->ctime;
-      save->val3 = tmpFile->atime;
-
-      sl_strlcpy(save->checksum, fileHash, KEY_LEN+1);
-
-      if (tmpFile->link_path && tmpFile->link_path[0] != '-')
-	{
-	  len = strlen(tmpFile->link_path);
+
+  *size = 0;
+
+  if (0 == sh_hash_get_it (key, &tmpFile))
+    {
+      *val1  = tmpFile.size;
+      *val2 = tmpFile.mtime;
+      *val3 = tmpFile.ctime;
+
+      if (tmpFile.linkpath[0] != '-')
+	{
+	  len = strlen(tmpFile.linkpath);
 
 	  p = SH_ALLOC((len/2)+1);
-	  i = sh_util_hextobinary (p, tmpFile->link_path, len);
+	  i = sh_util_hextobinary (p, tmpFile.linkpath, len);
 
 	  if (i == 0)
 	    {
-	      save->size = (len/2);
-	      p[save->size] = '\0';
-	      retval = p;
+	      *size = (len/2);
+	      p[*size] = '\0';
+	      return p;
 	    }
 	  else
 	    {
 	      SH_FREE(p);
-	      save->size = 0;
+	      *size = 0;
+	      return NULL;
 	    }
 	}
       else
 	{
-	  save->size = 0;
-	}
-    }
-  else
-    {
-      save->size = -1;
-      save->val0 = 0;
-      save->val1 = 0;
-      save->val2 = 0;
-      save->val3 = 0;
-    }
-  if (tmpFile->link_path) SH_FREE(tmpFile->link_path);
-  SH_FREE(tmpFile);
-  return retval;
+	  *size = 0;
+	  return NULL;
+	}
+    }
+  else
+    {
+      *size = -1;
+      *val1 =  0;
+      *val2 =  0;
+      *val3 =  0;
+      return NULL;
+    }
 }
 
@@ -1258,40 +1959,28 @@
 sh_file_t * sh_hash_push_int (file_type * buf, char * fileHash)
 {
-  sh_file_t    * fp = NULL;
+  sh_file_t    * fp;
   sh_filestore_t p;
-
-  size_t len;
+  long i;
   char * fullpath;
   char * linkpath;
-  char * attr_string = NULL;
-  char hashbuf[KEYBUF_SIZE];
 
   SL_ENTER(_("sh_hash_push_int"));
 
-  if (!buf)
-    SL_RETURN(NULL, _("sh_hash_push_int"));
-    
   fp = SH_ALLOC(sizeof(sh_file_t));
 
   p.mark = REC_MAGIC;
-  if (buf->attr_string)
-    p.mark |= REC_FLAGS_ATTR;
   sl_strlcpy(p.c_mode,   buf->c_mode,   11);
   sl_strlcpy(p.c_group,  buf->c_group,  GROUP_MAX+1);
   sl_strlcpy(p.c_owner,  buf->c_owner,  USER_MAX+1);
   sl_strlcpy(p.checksum, fileHash,      KEY_LEN+1);
-#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
+#if defined(__linux__)
   sl_strlcpy(p.c_attributes, buf->c_attributes, 13);
 #endif
 
-#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
+#if defined(__linux__)
   p.attributes  = (UINT32) buf->attributes;
-#else
-  p.attributes  = (UINT32) 0;
 #endif
   p.linkmode    = (UINT32) buf->linkmode;
   p.hardlinks   = (UINT32) buf->hardlinks;
-  p.dev   = (UINT64) buf->dev;
-  p.rdev  = (UINT64) buf->rdev;
   p.mode  = (UINT32) buf->mode;
   p.ino   = (UINT32) buf->ino;
@@ -1303,19 +1992,15 @@
   p.group = (UINT32) buf->group;
 
-  p.checkflags = (UINT32) buf->check_flags;
-
   memcpy( &(*fp).theFile, &p, sizeof(sh_filestore_t) );
-  fp->fflags    = 0;  /* init fflags */
+  fp->visited   = S_FALSE;
+  fp->reported  = S_FALSE;
+  fp->allignore = S_FALSE;
   fp->modi_mask = 0L;
 
-  if (buf->attr_string)
-    attr_string = sh_util_strdup(buf->attr_string);
-  fp->attr_string = attr_string;
-
-  len = sl_strlen(buf->fullpath);
-  if (len <= MAX_PATH_STORE) 
-    {
-      fullpath = SH_ALLOC(len+1);
-      sl_strlcpy(fullpath, buf->fullpath, len+1);
+  i = sl_strlen(buf->fullpath);
+  if (i <= MAX_PATH_STORE) 
+    {
+      fullpath = SH_ALLOC(i+ 1);
+      sl_strlcpy(fullpath, buf->fullpath, i+1);
     } 
   else 
@@ -1323,17 +2008,16 @@
       fullpath = SH_ALLOC(KEY_LEN + 1);
       sl_strlcpy(fullpath, 
-		 sh_tiger_hash (buf->fullpath, TIGER_DATA, len,
-				hashbuf, sizeof(hashbuf)), 
+		 sh_tiger_hash (buf->fullpath, TIGER_DATA, i), 
 		 KEY_LEN+1);
     }
   fp->fullpath  = fullpath;
 
-  if (buf->link_path)
+  if (buf->c_mode[0] == 'l')
     {  
-      len = sl_strlen(buf->link_path);
-      if (len <= MAX_PATH_STORE) 
-	{
-	  linkpath = SH_ALLOC(len+1);
-	  sl_strlcpy(linkpath, buf->link_path, len+1);
+      i = sl_strlen(buf->linkpath);
+      if (i <= MAX_PATH_STORE) 
+	{
+	  linkpath = SH_ALLOC(i+ 1);
+	  sl_strlcpy(linkpath, buf->linkpath, i+1);
 	} 
       else 
@@ -1341,6 +2025,5 @@
 	  linkpath = SH_ALLOC(KEY_LEN + 1);
 	  sl_strlcpy(linkpath, 
-		     sh_tiger_hash (buf->link_path, TIGER_DATA, len,
-				    hashbuf, sizeof(hashbuf)), 
+		     sh_tiger_hash (buf->linkpath, TIGER_DATA, i), 
 		     KEY_LEN+1);
 	}
@@ -1361,10 +2044,6 @@
 #endif
 
-#ifndef PRIu64
-#ifdef  HAVE_LONG_32
-#define PRIu64 "llu"
-#else
-#define PRIu64 "lu"
-#endif
+#ifndef PRIi64
+#define PRIi64 "lld"
 #endif
 
@@ -1378,8 +2057,8 @@
 #ifdef SH_USE_XML
   sl_snprintf(form_rval, 80, _("%s%s%s%s%s"), 
-	      _("size_old=\"%"), PRIu64, _("\" size_new=\"%"), PRIu64, "\" ");
+	      _("size_old=\"%"), PRIi64, _("\" size_new=\"%"), PRIi64, "\" ");
 #else
   sl_snprintf(form_rval, 80, _("%s%s%s%s%s"), 
-	      _("size_old=<%"), PRIu64, _(">, size_new=<%"), PRIu64, ">, ");
+	      _("size_old=<%"), PRIi64, _(">, size_new=<%"), PRIi64, ">, ");
 #endif
 
@@ -1398,27 +2077,17 @@
   char * format;
 
-  char * tmp = SH_ALLOC(SH_MSG_BUF);
-  char * msg = SH_ALLOC(SH_MSG_BUF);
+  char * tmp = SH_ALLOC(SH_BUFSIZE);
+  char * msg = SH_ALLOC(SH_BUFSIZE);
 
   tmp[0] = '\0';
   msg[0] = '\0';
 
-  if (report_checkflags != S_FALSE)
-    {
-      if (is_new)
-	format = _("checkflags_new=\"0%lo\" ");
-      else
-	format = _("checkflags_old=\"0%lo\" ");
-      sl_snprintf(tmp, SH_MSG_BUF, format,
-		  (unsigned long) theFile->check_flags);
-      sl_strlcat(msg, tmp, SH_MSG_BUF); 
-    }
-
-#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
+
+#if defined(__linux__)
   if (is_new)
     format = _("mode_new=\"%s\" attr_new=\"%s\" imode_new=\"%ld\" iattr_new=\"%ld\" ");
   else 
     format = _("mode_old=\"%s\" attr_old=\"%s\" imode_old=\"%ld\" iattr_old=\"%ld\" ");
-  sl_snprintf(tmp, SH_MSG_BUF, format,
+  sl_snprintf(tmp, SH_BUFSIZE, format,
 	      theFile->c_mode,
 	      theFile->c_attributes,
@@ -1432,48 +2101,34 @@
     format = _("mode_old=\"%s\" imode_old=\"%ld\" ");
 
-  sl_snprintf(tmp, SH_MSG_BUF, format,
+  sl_snprintf(tmp, SH_BUFSIZE, format,
 	      theFile->c_mode,
 	      (long) theFile->mode
 	      );
 #endif
-  sl_strlcat(msg, tmp, SH_MSG_BUF);
+  sl_strlcat(msg, tmp, SH_BUFSIZE);
 
   if (is_new)
-    format = _("hardlinks_new=\"%lu\" ");
-  else
-    format = _("hardlinks_old=\"%lu\" ");
-  sl_snprintf(tmp, SH_MSG_BUF, format,
+    format = _("hardlinks_new=\"%ld\" ");
+  else
+    format = _("hardlinks_old=\"%ld\" ");
+  sl_snprintf(tmp, SH_BUFSIZE, format,
 	      (unsigned long) theFile->hardlinks);
-  sl_strlcat(msg, tmp, SH_MSG_BUF); 
+  sl_strlcat(msg, tmp, SH_BUFSIZE); 
 
 
   if (is_new)
-    format = _("idevice_new=\"%lu\" ");
-  else
-    format = _("idevice_old=\"%lu\" ");
-  sl_snprintf(tmp, SH_MSG_BUF, format, (unsigned long) theFile->rdev);
-  sl_strlcat(msg, tmp, SH_MSG_BUF); 
+    format = _("idevice_new=\"%ld\" ");
+  else
+    format = _("idevice_old=\"%ld\" ");
+  sl_snprintf(tmp, SH_BUFSIZE, format, (unsigned long) theFile->rdev);
+  sl_strlcat(msg, tmp, SH_BUFSIZE); 
 
 
   if (is_new)
-    format = _("inode_new=\"%lu\" ");
-  else
-    format = _("inode_old=\"%lu\" ");
-  sl_snprintf(tmp, SH_MSG_BUF, format, (unsigned long) theFile->ino);
-  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-
-  /* 
-   * also report device for prelude
-   */
-#if defined(HAVE_LIBPRELUDE)
-  if (is_new)
-    format = _("dev_new=\"%lu,%lu\" ");
-  else
-    format = _("dev_old=\"%lu,%lu\" ");
-  sl_snprintf(tmp, SH_MSG_BUF, format,		      
-	      (unsigned long) major(theFile->dev),
-	      (unsigned long) minor(theFile->dev));
-  sl_strlcat(msg, tmp, SH_MSG_BUF);
-#endif
+    format = _("inode_new=\"%ld\" ");
+  else
+    format = _("inode_old=\"%ld\" ");
+  sl_snprintf(tmp, SH_BUFSIZE, format, (unsigned long) theFile->ino);
+  sl_strlcat(msg, tmp, SH_BUFSIZE); 
 
 
@@ -1482,7 +2137,7 @@
   else
     format = _("owner_old=\"%s\" iowner_old=\"%ld\" ");
-  sl_snprintf(tmp, SH_MSG_BUF, format,
+  sl_snprintf(tmp, SH_BUFSIZE, format,
 	      theFile->c_owner, (long) theFile->owner);
-  sl_strlcat(msg, tmp, SH_MSG_BUF); 
+  sl_strlcat(msg, tmp, SH_BUFSIZE); 
 
 
@@ -1491,74 +2146,58 @@
   else
     format = _("group_old=\"%s\" igroup_old=\"%ld\" ");
-  sl_snprintf(tmp, SH_MSG_BUF, format,
+  sl_snprintf(tmp, SH_BUFSIZE, format,
 	      theFile->c_group, (long) theFile->group);
-  sl_strlcat(msg, tmp, SH_MSG_BUF); 
+  sl_strlcat(msg, tmp, SH_BUFSIZE); 
 
 
   if (is_new)
-    sl_snprintf(tmp, SH_MSG_BUF, sh_hash_size_format(),
+    sl_snprintf(tmp, SH_BUFSIZE, sh_hash_size_format(),
 		(UINT64) 0, (UINT64) theFile->size);
   else
-    sl_snprintf(tmp, SH_MSG_BUF, sh_hash_size_format(),
+    sl_snprintf(tmp, SH_BUFSIZE, sh_hash_size_format(),
 		(UINT64) theFile->size, (UINT64) 0);
-  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-
-
-  (void) sh_unix_gmttime (theFile->ctime, timstr1c,  sizeof(timstr1c));
+  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+
+
+  sl_strlcpy (timstr1c, sh_unix_gmttime (theFile->ctime),   32);
   if (is_new)
-    sl_snprintf(tmp, SH_MSG_BUF, _("ctime_new=\"%s\" "), timstr1c);
-  else
-    sl_snprintf(tmp, SH_MSG_BUF, _("ctime_old=\"%s\" "), timstr1c);
-  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-
-  (void) sh_unix_gmttime (theFile->atime, timstr1a,  sizeof(timstr1a));
+    sl_snprintf(tmp, SH_BUFSIZE, _("ctime_new=\"%s\" "), timstr1c);
+  else
+    sl_snprintf(tmp, SH_BUFSIZE, _("ctime_old=\"%s\" "), timstr1c);
+  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+
+  sl_strlcpy (timstr1a, sh_unix_gmttime (theFile->atime),   32);
   if (is_new)
-    sl_snprintf(tmp, SH_MSG_BUF, _("atime_new=\"%s\" "), timstr1a);
-  else
-    sl_snprintf(tmp, SH_MSG_BUF, _("atime_old=\"%s\" "), timstr1a);
-  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-
-  (void) sh_unix_gmttime (theFile->mtime, timstr1m,  sizeof(timstr1m));
+    sl_snprintf(tmp, SH_BUFSIZE, _("atime_new=\"%s\" "), timstr1a);
+  else
+    sl_snprintf(tmp, SH_BUFSIZE, _("atime_old=\"%s\" "), timstr1a);
+  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+
+  sl_strlcpy (timstr1m, sh_unix_gmttime (theFile->mtime),   32);
   if (is_new)
-    sl_snprintf(tmp, SH_MSG_BUF, _("mtime_new=\"%s\" "), timstr1m);
-  else
-    sl_snprintf(tmp, SH_MSG_BUF, _("mtime_old=\"%s\" "), timstr1m);
-  sl_strlcat(msg, tmp, SH_MSG_BUF); 
+    sl_snprintf(tmp, SH_BUFSIZE, _("mtime_new=\"%s\" "), timstr1m);
+  else
+    sl_snprintf(tmp, SH_BUFSIZE, _("mtime_old=\"%s\" "), timstr1m);
+  sl_strlcat(msg, tmp, SH_BUFSIZE); 
 
   if (is_new)
-    sl_snprintf(tmp, SH_MSG_BUF, _("chksum_new=\"%s\" "), fileHash);
-  else
-    sl_snprintf(tmp, SH_MSG_BUF, _("chksum_old=\"%s\" "), fileHash);
-  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-
-  if (theFile->c_mode[0] == 'l' || 
-      (theFile->link_path != NULL && theFile->link_path[0] != '-'))
-    {
-      tmp_lnk     = sh_util_safe_name(theFile->link_path);
+    sl_snprintf(tmp, SH_BUFSIZE, _("chksum_new=\"%s\" "), fileHash);
+  else
+    sl_snprintf(tmp, SH_BUFSIZE, _("chksum_old=\"%s\" "), fileHash);
+  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+
+  if (theFile->c_mode[0] == 'l')
+    {
+      tmp_lnk     = sh_util_safe_name(theFile->linkpath);
       if (tmp_lnk)
 	{
 	  if (is_new)
-	    sl_snprintf(tmp, SH_MSG_BUF, _("link_new=\"%s\" "), tmp_lnk);
+	    sl_snprintf(tmp, SH_BUFSIZE, _("link_new=\"%s\" "), tmp_lnk);
 	  else
-	    sl_snprintf(tmp, SH_MSG_BUF, _("link_old=\"%s\" "), tmp_lnk);
+	    sl_snprintf(tmp, SH_BUFSIZE, _("link_old=\"%s\" "), tmp_lnk);
 	  SH_FREE(tmp_lnk);
-	  sl_strlcat(msg, tmp, SH_MSG_BUF);
+	  sl_strlcat(msg, tmp, SH_BUFSIZE);
 	} 
     }
-
-  if (theFile->attr_string)
-    {
-      tmp_lnk     = sh_util_safe_name(theFile->attr_string);
-      if (tmp_lnk)
-	{
-	  if (is_new)
-	    sl_snprintf(tmp, SH_MSG_BUF, _("acl_new=\"%s\" "), tmp_lnk);
-	  else
-	    sl_snprintf(tmp, SH_MSG_BUF, _("acl_old=\"%s\" "), tmp_lnk);
-	  SH_FREE(tmp_lnk);
-	  sl_strlcat(msg, tmp, SH_MSG_BUF);
-	} 
-    }
-
   
   SH_FREE(tmp);
@@ -1575,28 +2214,17 @@
   char * format;
 
-  char * tmp = SH_ALLOC(SH_MSG_BUF);
-  char * msg = SH_ALLOC(SH_MSG_BUF);
+  char * tmp = SH_ALLOC(SH_BUFSIZE);
+  char * msg = SH_ALLOC(SH_BUFSIZE);
 
   tmp[0] = '\0';
   msg[0] = '\0';
 
-  if (report_checkflags == S_TRUE)
-    {
-      if (is_new)
-	format = _("checkflags_new=<0%lo> ");
-      else
-	format = _("checkflags_old=<0%lo> ");
-      sl_snprintf(tmp, SH_MSG_BUF, format,
-		  (unsigned long) theFile->check_flags);
-      sl_strlcat(msg, tmp, SH_MSG_BUF); 
-    }
-
-
-#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
+
+#if defined(__linux__)
   if (is_new)
     format = _("mode_new=<%s>, attr_new=<%s>, imode_new=<%ld>, iattr_new=<%ld>, ");
   else 
     format = _("mode_old=<%s>, attr_old=<%s>, imode_old=<%ld>, iattr_old=<%ld>, ");
-  sl_snprintf(tmp, SH_MSG_BUF, format,
+  sl_snprintf(tmp, SH_BUFSIZE, format,
 	      theFile->c_mode,
 	      theFile->c_attributes,
@@ -1610,49 +2238,35 @@
     format = _("mode_old=<%s>, imode_old=<%ld>, ");
 
-  sl_snprintf(tmp, SH_MSG_BUF, format,
+  sl_snprintf(tmp, SH_BUFSIZE, format,
 	      theFile->c_mode,
 	      (long) theFile->mode
 	      );
 #endif
-  sl_strlcat(msg, tmp, SH_MSG_BUF);
+  sl_strlcat(msg, tmp, SH_BUFSIZE);
 
   if (is_new)
-    format = _("hardlinks_new=<%lu>, ");
-  else
-    format = _("hardlinks_old=<%lu>, ");
-  sl_snprintf(tmp, SH_MSG_BUF, format,
+    format = _("hardlinks_new=<%ld>, ");
+  else
+    format = _("hardlinks_old=<%ld>, ");
+  sl_snprintf(tmp, SH_BUFSIZE, format,
 	      (unsigned long) theFile->hardlinks);
-  sl_strlcat(msg, tmp, SH_MSG_BUF); 
+  sl_strlcat(msg, tmp, SH_BUFSIZE); 
 
 
   if (is_new)
-    format = _("idevice_new=<%lu>, ");
-  else
-    format = _("idevice_old=<%lu>, ");
-  sl_snprintf(tmp, SH_MSG_BUF, format, (unsigned long) theFile->rdev);
-  sl_strlcat(msg, tmp, SH_MSG_BUF); 
+    format = _("idevice_new=<%ld>, ");
+  else
+    format = _("idevice_old=<%ld>, ");
+  sl_snprintf(tmp, SH_BUFSIZE, format, (unsigned long) theFile->rdev);
+  sl_strlcat(msg, tmp, SH_BUFSIZE); 
 
 
   if (is_new)
-    format = _("inode_new=<%lu>, ");
-  else
-    format = _("inode_old=<%lu>, ");
-  sl_snprintf(tmp, SH_MSG_BUF, format, (unsigned long) theFile->ino);
-  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-
-
-  /* 
-   * also report device for prelude
-   */
-#if defined(HAVE_LIBPRELUDE)
-  if (is_new)
-    format = _("dev_new=<%lu,%lu>, ");
-  else
-    format = _("dev_old=<%lu,%lu>, ");
-  sl_snprintf(tmp, SH_MSG_BUF, format,		      
-	      (unsigned long) major(theFile->dev),
-	      (unsigned long) minor(theFile->dev));
-  sl_strlcat(msg, tmp, SH_MSG_BUF);
-#endif
+    format = _("inode_new=<%ld>, ");
+  else
+    format = _("inode_old=<%ld>, ");
+  sl_snprintf(tmp, SH_BUFSIZE, format, (unsigned long) theFile->ino);
+  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+
 
   if (is_new)
@@ -1660,7 +2274,7 @@
   else
     format = _("owner_old=<%s>, iowner_old=<%ld>, ");
-  sl_snprintf(tmp, SH_MSG_BUF, format,
+  sl_snprintf(tmp, SH_BUFSIZE, format,
 	      theFile->c_owner, (long) theFile->owner);
-  sl_strlcat(msg, tmp, SH_MSG_BUF); 
+  sl_strlcat(msg, tmp, SH_BUFSIZE); 
 
 
@@ -1669,74 +2283,59 @@
   else
     format = _("group_old=<%s>, igroup_old=<%ld>, ");
-  sl_snprintf(tmp, SH_MSG_BUF, format,
+  sl_snprintf(tmp, SH_BUFSIZE, format,
 	      theFile->c_group, (long) theFile->group);
-  sl_strlcat(msg, tmp, SH_MSG_BUF); 
+  sl_strlcat(msg, tmp, SH_BUFSIZE); 
 
 
   if (is_new)
-    sl_snprintf(tmp, SH_MSG_BUF, sh_hash_size_format(),
+    sl_snprintf(tmp, SH_BUFSIZE, sh_hash_size_format(),
 		(UINT64) 0, (UINT64) theFile->size);
   else
-    sl_snprintf(tmp, SH_MSG_BUF, sh_hash_size_format(),
+    sl_snprintf(tmp, SH_BUFSIZE, sh_hash_size_format(),
 		(UINT64) theFile->size, (UINT64) 0);
-  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-
-
-  (void) sh_unix_gmttime (theFile->ctime, timstr1c,  sizeof(timstr1c));
+  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+
+
+  sl_strlcpy (timstr1c, sh_unix_gmttime (theFile->ctime),   32);
   if (is_new)
-    sl_snprintf(tmp, SH_MSG_BUF, _("ctime_new=<%s>, "), timstr1c);
-  else
-    sl_snprintf(tmp, SH_MSG_BUF, _("ctime_old=<%s>, "), timstr1c);
-  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-
-  (void) sh_unix_gmttime (theFile->atime, timstr1a,  sizeof(timstr1a));
+    sl_snprintf(tmp, SH_BUFSIZE, _("ctime_new=<%s>, "), timstr1c);
+  else
+    sl_snprintf(tmp, SH_BUFSIZE, _("ctime_old=<%s>, "), timstr1c);
+  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+
+  sl_strlcpy (timstr1a, sh_unix_gmttime (theFile->atime),   32);
   if (is_new)
-    sl_snprintf(tmp, SH_MSG_BUF, _("atime_new=<%s>, "), timstr1a);
-  else
-    sl_snprintf(tmp, SH_MSG_BUF, _("atime_old=<%s>, "), timstr1a);
-  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-
-  (void) sh_unix_gmttime (theFile->mtime, timstr1m,  sizeof(timstr1m));
+    sl_snprintf(tmp, SH_BUFSIZE, _("atime_new=<%s>, "), timstr1a);
+  else
+    sl_snprintf(tmp, SH_BUFSIZE, _("atime_old=<%s>, "), timstr1a);
+  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+
+  sl_strlcpy (timstr1m, sh_unix_gmttime (theFile->mtime),   32);
   if (is_new)
-    sl_snprintf(tmp, SH_MSG_BUF, _("mtime_new=<%s>, "), timstr1m);
-  else
-    sl_snprintf(tmp, SH_MSG_BUF, _("mtime_old=<%s>, "), timstr1m);
-  sl_strlcat(msg, tmp, SH_MSG_BUF); 
+    sl_snprintf(tmp, SH_BUFSIZE, _("mtime_new=<%s>, "), timstr1m);
+  else
+    sl_snprintf(tmp, SH_BUFSIZE, _("mtime_old=<%s>, "), timstr1m);
+  sl_strlcat(msg, tmp, SH_BUFSIZE); 
 
   if (is_new)
-    sl_snprintf(tmp, SH_MSG_BUF, _("chksum_new=<%s>"), fileHash);
-  else
-    sl_snprintf(tmp, SH_MSG_BUF, _("chksum_old=<%s>"), fileHash);
-  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-
-  if (theFile->c_mode[0] == 'l' || 
-      (theFile->link_path != NULL && theFile->link_path[0] != '-'))
-    {
-      tmp_lnk     = sh_util_safe_name(theFile->link_path);
+    sl_snprintf(tmp, SH_BUFSIZE, _("chksum_new=<%s>"), fileHash);
+  else
+    sl_snprintf(tmp, SH_BUFSIZE, _("chksum_old=<%s>"), fileHash);
+  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+
+  if (theFile->c_mode[0] == 'l')
+    {
+      tmp_lnk     = sh_util_safe_name(theFile->linkpath);
       if (tmp_lnk)
 	{
 	  if (is_new)
-	    sl_snprintf(tmp, SH_MSG_BUF, _(", link_new=<%s> "), tmp_lnk);
+	    sl_snprintf(tmp, SH_BUFSIZE, _(", link_new=<%s> "), tmp_lnk);
 	  else
-	    sl_snprintf(tmp, SH_MSG_BUF, _(", link_old=<%s> "), tmp_lnk);
+	    sl_snprintf(tmp, SH_BUFSIZE, _(", link_old=<%s> "), tmp_lnk);
 	  SH_FREE(tmp_lnk);
-	  sl_strlcat(msg, tmp, SH_MSG_BUF);
+	  sl_strlcat(msg, tmp, SH_BUFSIZE);
 	} 
     }
   
-  if (theFile->attr_string)
-    {
-      tmp_lnk     = sh_util_safe_name(theFile->attr_string);
-      if (tmp_lnk)
-	{
-	  if (is_new)
-	    sl_snprintf(tmp, SH_MSG_BUF, _(", acl_new=<%s> "), tmp_lnk);
-	  else
-	    sl_snprintf(tmp, SH_MSG_BUF, _(", acl_old=<%s> "), tmp_lnk);
-	  SH_FREE(tmp_lnk);
-	  sl_strlcat(msg, tmp, SH_MSG_BUF);
-	} 
-    }
-
   SH_FREE(tmp);
   return (msg);
@@ -1753,8 +2352,6 @@
   if (p) 
     {
-      SH_MUTEX_LOCK(mutex_hash);
-      hashinsert (tab, p);
-      p->modi_mask = theFile->check_flags;
-      SH_MUTEX_UNLOCK(mutex_hash);
+      hashinsert (p);
+      p->modi_mask = theFile->check_mask;
     }
 
@@ -1762,108 +2359,4 @@
 }
 
-int sh_hash_is_null_file(file_type * theFile)
-{
-  if (theFile->hardlinks == SH_DEADFILE && theFile->mode  == 0 &&
-      theFile->ino == 0                 && theFile->ctime == 0)
-    {
-      return S_TRUE;
-    }
-  return S_FALSE;
-}
-
-int sh_hash_is_null_record(sh_filestore_t * theFile)
-{
-  if (theFile->hardlinks == SH_DEADFILE && theFile->mode  == 0 &&
-      theFile->ino == 0                 && theFile->ctime == 0)
-    {
-      return S_TRUE;
-    }
-  return S_FALSE;
-}
-
-void sh_hash_insert_null(char * str)
-{
-  file_type theFile = { 0, 0, {'\0'}, 0, 0, 0, 0, 0, 
-#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
-			0, {'\0'},
-#endif
-			{'\0'}, 0, {'\0'}, 0, {'\0'}, 
-			0, 0, 0, 0, 0, 0, 0, NULL,  0, {'\0'}, 0, NULL
-  }; /* clang compiler bails out on standard conforming init with just {0} */
-  char      fileHash[KEY_LEN+1];
-  char      hashbuf[KEYBUF_SIZE];
-
-  sl_strlcpy(fileHash, SH_KEY_NULL, sizeof(fileHash));
-  theFile.hardlinks = SH_DEADFILE;
-
-  if (sl_strlen(str) < PATH_MAX)
-    sl_strlcpy(theFile.fullpath, str, PATH_MAX);
-  else 
-     sl_strlcpy(theFile.fullpath, 
-		sh_tiger_hash(str, TIGER_DATA, sl_strlen(str),
-			      hashbuf, sizeof(hashbuf)),
-		PATH_MAX);
-
-  sh_hash_pushdata_memory(&theFile, fileHash);
-  return;
-}
-
-static int handle_notfound(int  log_severity, int class,
-			   file_type * theFile, char * fileHash)
-{
-  sh_file_t * p;
-  int         retval = 0;
-
-  if (!theFile)
-    return retval;
-  
-  if (S_FALSE == sh_ignore_chk_new(theFile->fullpath))
-    {
-      char * tmp = sh_util_safe_name(theFile->fullpath);
-      char * str;
-
-      sh_files_fixup_mask(class, &(theFile->check_flags));
-      str = all_items (theFile, fileHash, 1);
-      
-      if (!sh_global_check_silent)
-	sh_error_handle (log_severity, FIL__, __LINE__, 0, 
-			 MSG_FI_ADD2, 
-			 tmp, str);
-      ++sh.statistics.files_report;
-      SH_FREE(str);
-      SH_FREE(tmp);
-    }
-  
-  if (sh.flag.reportonce == S_TRUE)
-    SET_SH_FFLAG_REPORTED(theFile->file_reported);
-  
-  if (sh.flag.reportonce == S_TRUE && sh.flag.update == S_FALSE)
-    {
-      p = sh_hash_push_int(theFile, fileHash);
-      if (p)
-	{
-	  hashinsert (tab, p);
-	  p->modi_mask = theFile->check_flags;
-	  p->theFile.checkflags = p->modi_mask;
-	}
-    }
-  
-  else if (S_TRUE == sh.flag.update)
-    {
-      if (S_TRUE == sh_util_ask_update (theFile->fullpath))
-	{
-	  p = sh_hash_push_int(theFile, fileHash);
-	  if (p)
-	    {
-	      hashinsert (tab, p);
-	      p->modi_mask = theFile->check_flags;
-	      p->theFile.checkflags = p->modi_mask;
-	    }
-	}
-      else
-	retval = 1;
-    }
-  return retval;
-}
 
 /*****************************************************************
@@ -1881,4 +2374,6 @@
   char * tmp_lnk;
   char * tmp_lnk_old;
+
+  char * str;
 
   char timstr1c[32];
@@ -1889,29 +2384,17 @@
   char timstr2m[32];
   char linkHash[KEY_LEN+1];
-  char * linkComp;
   int  maxcomp;
-  volatile int  checksum_flag = 0;
 
   char change_code[16];
   int  i;
 
-  unsigned long modi_mask;
+  unsigned long modi_mask = 0;
 
   char log_policy[32];
-  volatile int  log_severity;
-  char hashbuf[KEYBUF_SIZE];
-  struct {
-    unsigned long oldflags;
-    unsigned long newflags;
-  } cf_report;
-
-  int  retval;
+  int  log_severity;
 
   SL_ENTER(_("sh_hash_compdata"));
 
-  if (!theFile)
-    SL_RETURN(0, _("sh_hash_compdata"));
-
- if (IsInit != 1) sh_hash_init();
+  if (IsInit != 1) sh_hash_init();
 
   if (severity_override < 0)
@@ -1924,9 +2407,4 @@
 
   /* --------  find the entry for the file ----------------       */
-
-  SH_MUTEX_LOCK(mutex_hash);
-
-  modi_mask = 0;
-  retval    = 0;
 
   if (sl_strlen(theFile->fullpath) <= MAX_PATH_STORE) 
@@ -1935,6 +2413,5 @@
     p = hashsearch( sh_tiger_hash(theFile->fullpath, 
 				  TIGER_DATA, 
-				  sl_strlen(theFile->fullpath),
-				  hashbuf, sizeof(hashbuf))
+				  sl_strlen(theFile->fullpath))
 		    );
 
@@ -1945,24 +2422,44 @@
   if (p == NULL) 
     {
-      retval = handle_notfound(log_severity, class, theFile, fileHash);
-      goto unlock_and_return;
-    }
-
-  /* ---------  Skip if we don't want to report changes. ------------
-   */
-  
-  if (S_TRUE == sh_ignore_chk_mod(theFile->fullpath))
-    {
-      MODI_SET(theFile->check_flags, MODI_NOCHECK);
-      p->modi_mask = theFile->check_flags;
-      p->theFile.checkflags = p->modi_mask;
-      goto unlock_and_return;
-    }
-
-  cf_report.oldflags = p->theFile.checkflags;
-  cf_report.newflags = theFile->check_flags;
-
-  p->modi_mask = theFile->check_flags;
-  p->theFile.checkflags = p->modi_mask;
+      if (sh.flag.reportonce == S_TRUE)
+	{
+	  p = sh_hash_push_int(theFile, fileHash);
+	  hashinsert (p);
+	  if (p)
+	    p->modi_mask = theFile->check_mask;
+
+	}
+
+      if (S_FALSE == sh_ignore_chk_new(theFile->fullpath))
+	{
+	  tmp = sh_util_safe_name(theFile->fullpath);
+
+	  str = all_items (theFile, fileHash, 1);
+	  sh_error_handle (log_severity, FIL__, __LINE__, 0, 
+			   MSG_FI_ADD2, 
+			   tmp, str);
+	  SH_FREE(str);
+
+	  SH_FREE(tmp);
+	}
+
+      if (p)
+	p->visited   = S_TRUE;
+      if (sh.flag.reportonce == S_TRUE)
+	theFile->reported = S_TRUE;
+	
+      if (sh.flag.isdaemon == S_FALSE && sh.flag.update == S_TRUE )
+	{
+	  if (S_FALSE == sh_util_ask_update (theFile->fullpath))
+	    {
+	      SL_RETURN(1, _("sh_hash_compdata"));
+	    } 
+	}
+      SL_RETURN(0, _("sh_hash_compdata"));
+    }
+  else
+    {
+      p->modi_mask = theFile->check_mask;
+    }
 
   /* initialize change_code */
@@ -1974,81 +2471,37 @@
 	theFile->fullpath, fileHash, p->theFile.checksum));
 
-  if ( (fileHash != NULL) &&
-       (strncmp (fileHash, p->theFile.checksum, KEY_LEN) != 0) && 
-       (theFile->check_flags & MODI_CHK) != 0)
-    {
-      checksum_flag = 1;
-      
-      if ((theFile->check_flags & MODI_SGROW) == 0)
-	{
-	  modi_mask |= MODI_CHK;
-	  change_code[0] = 'C';
-	  TPT ((0, FIL__, __LINE__, _("mod=<checksum>")));
-	}
-      else
-	{
-	  if (0 != strncmp (&fileHash[KEY_LEN + 1], p->theFile.checksum, KEY_LEN))
-	    {
-	      if (S_FALSE == sh_check_rotated_log (theFile->fullpath, (UINT64) p->theFile.size, 
-						   (UINT64) p->theFile.ino, p->theFile.checksum,
-						   p->theFile.checkflags))
-		{
-		  modi_mask |= MODI_CHK;
-		  change_code[0] = 'C';
-		  TPT ((0, FIL__, __LINE__, _("mod=<checksum>")));
-		}
-	      else
-		{
-		  /* logfile has been rotated */
-		  p->theFile.size  = theFile->size;
-		  p->theFile.ino   = theFile->ino;
-		  sl_strlcpy(p->theFile.checksum, fileHash, KEY_LEN+1);
-		}
-	    }
-	  else
-	    {
-	      p->theFile.size  = theFile->size;
-	      sl_strlcpy(p->theFile.checksum, fileHash, KEY_LEN+1);
-	    }
-	}
+  if ( (fileHash != NULL) && (p->theFile.checksum != NULL)   && 
+       (strncmp (fileHash, p->theFile.checksum, 50) != 0) && 
+       (theFile->check_mask & MODI_CHK) != 0)
+    {
+      modi_mask |= MODI_CHK;
+      change_code[0] = 'C';
+      TPT ((0, FIL__, __LINE__, _("mod=<checksum>")));
     } 
 
   if (p->theFile.c_mode[0] == 'l') 
     {
-      if (!(theFile->link_path) &&
-	  (theFile->check_flags & MODI_LNK) != 0)
-	{
-	  linkComp = NULL;
-	  modi_mask |= MODI_LNK;
-	  change_code[1] = 'L';
-	  TPT ((0, FIL__, __LINE__, _("mod=<link>")));
-	}
-      else
-	{
-	  if (sl_strlen(theFile->link_path) >= MAX_PATH_STORE) 
-	    {
-	      sl_strlcpy(linkHash, 
-			 sh_tiger_hash(theFile->link_path, 
-				       TIGER_DATA,
-				       sl_strlen(theFile->link_path),
-				       hashbuf, sizeof(hashbuf)), 
-			 sizeof(linkHash));
-	      linkComp = linkHash;
-	      maxcomp  = KEY_LEN;
-	    } 
-	  else 
-	    {
-	      linkComp = theFile->link_path;
-	      maxcomp  = MAX_PATH_STORE;
-	    }
-	  
-	  if ( sl_strncmp (linkComp, p->linkpath, maxcomp) != 0 &&
-	       (theFile->check_flags & MODI_LNK) != 0)
-	    {
-	      modi_mask |= MODI_LNK;
-	      change_code[1] = 'L';
-	      TPT ((0, FIL__, __LINE__, _("mod=<link>")));
-	    } 
-	}
+      if (sl_strlen(theFile->linkpath) >= MAX_PATH_STORE) 
+	{
+	  sl_strlcpy(linkHash, 
+		     sh_tiger_hash(theFile->linkpath, 
+				   TIGER_DATA,
+				   sl_strlen(theFile->linkpath)), 
+		     MAX_PATH_STORE+1);
+	  maxcomp = MAX_PATH_STORE;
+	} 
+      else 
+	{
+	  sl_strlcpy(linkHash, theFile->linkpath, KEY_LEN + 1);
+	  maxcomp = KEY_LEN;
+	}
+
+    if ( sl_strncmp (linkHash, p->linkpath, maxcomp) != 0 &&
+	 (theFile->check_mask & MODI_LNK) != 0)
+      {
+	modi_mask |= MODI_LNK;
+	change_code[1] = 'L';
+	TPT ((0, FIL__, __LINE__, _("mod=<link>")));
+      } 
     }
 
@@ -2057,5 +2510,5 @@
       if ( ( major(theFile->rdev) != major((dev_t)p->theFile.rdev) || 
 	     minor(theFile->rdev) != minor((dev_t)p->theFile.rdev) ) &&
-	   (theFile->check_flags & MODI_RDEV) != 0)
+	   (theFile->check_mask & MODI_RDEV) != 0)
 	{
 	  modi_mask |= MODI_RDEV;
@@ -2068,45 +2521,13 @@
    */
   if ( (UINT32) theFile->ino != (UINT32) p->theFile.ino  &&
-       (theFile->check_flags & MODI_INO) != 0)
-    {
-      if ((theFile->check_flags & MODI_SGROW) == 0)
-	{
-	  modi_mask |= MODI_INO;
-	  change_code[3] = 'I';
-	  TPT ((0, FIL__, __LINE__, _("mod=<inode>")));
-	}
-      else
-	{
-	  /* growing log, checksum ok but inode changed 
-	   */
-	  if (checksum_flag == 0)
-	    {
-	      if (S_FALSE == sh_check_rotated_log (theFile->fullpath, (UINT64) p->theFile.size, 
-						   (UINT64) p->theFile.ino, p->theFile.checksum,
-						   p->theFile.checkflags))
-		{
-		  modi_mask |= MODI_INO;
-		  change_code[3] = 'I';
-		  TPT ((0, FIL__, __LINE__, _("mod=<inode>")));
-		}
-	      else
-		{
-		  /* logfile has been rotated */
-		  p->theFile.size  = theFile->size;
-		  p->theFile.ino   = theFile->ino;
-		  sl_strlcpy(p->theFile.checksum, fileHash, KEY_LEN+1);
-		}
-	    }
-	  else
-	    {
-	      modi_mask |= MODI_INO;
-	      change_code[3] = 'I';
-	      TPT ((0, FIL__, __LINE__, _("mod=<inode>")));
-	    }
-	}
+       (theFile->check_mask & MODI_INO) != 0)
+    {
+      modi_mask |= MODI_INO;
+      change_code[3] = 'I';
+      TPT ((0, FIL__, __LINE__, _("mod=<inode>")));
     } 
     
   if ( theFile->hardlinks != (nlink_t) p->theFile.hardlinks &&
-       (theFile->check_flags & MODI_HLN) != 0)
+       (theFile->check_mask & MODI_HLN) != 0)
     {
       modi_mask |= MODI_HLN;
@@ -2117,18 +2538,9 @@
 
   if ( (  (theFile->mode != p->theFile.mode)
-#if defined(USE_ACL) || defined(USE_XATTR)
-	  || ( (sh_unix_check_selinux|sh_unix_check_acl) &&
-	       ( 
-		(theFile->attr_string == NULL && p->attr_string != NULL) ||
-		(theFile->attr_string != NULL && p->attr_string == NULL) ||
-		(theFile->attr_string != NULL && 0 != strcmp(theFile->attr_string, p->attr_string))
-		)
-	       )
-#endif
-#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
+#if defined(__linux__)
           || (theFile->attributes != p->theFile.attributes)
 #endif
-	  )
-       && (theFile->check_flags & MODI_MOD) != 0)
+        )
+       && (theFile->check_mask & MODI_MOD) != 0)
     {
       modi_mask |= MODI_MOD;
@@ -2138,5 +2550,5 @@
        * report link path if switch link/no link 
        */
-      if ((theFile->check_flags & MODI_LNK) != 0 &&
+      if ((theFile->check_mask & MODI_LNK) != 0 &&
 	  (theFile->c_mode[0] != p->theFile.c_mode[0]) &&
 	  (theFile->c_mode[0] == 'l' || p->theFile.c_mode[0] == 'l'))
@@ -2149,5 +2561,5 @@
 
   if ( theFile->owner != (uid_t) p->theFile.owner &&
-       (theFile->check_flags & MODI_USR) != 0)
+       (theFile->check_mask & MODI_USR) != 0)
     {
       modi_mask |= MODI_USR;
@@ -2157,5 +2569,5 @@
 
   if ( theFile->group != (gid_t) p->theFile.group &&
-       (theFile->check_flags & MODI_GRP) != 0)
+       (theFile->check_mask & MODI_GRP) != 0)
     {
       modi_mask |= MODI_GRP;
@@ -2166,5 +2578,5 @@
   
   if ( theFile->mtime != (time_t) p->theFile.mtime &&
-       (theFile->check_flags & MODI_MTM) != 0)
+       (theFile->check_mask & MODI_MTM) != 0)
     {
       modi_mask |= MODI_MTM;
@@ -2173,6 +2585,6 @@
     } 
   
-  if ( (theFile->check_flags & MODI_ATM) != 0 &&
-       theFile->atime != (time_t) p->theFile.atime)
+  if ( theFile->atime != (time_t) p->theFile.atime &&
+       (theFile->check_mask & MODI_ATM) != 0)
     {
       modi_mask |= MODI_ATM;
@@ -2186,5 +2598,5 @@
    */
   if ( theFile->ctime != (time_t) p->theFile.ctime &&
-       (theFile->check_flags & MODI_CTM) != 0)
+       (theFile->check_mask & MODI_CTM) != 0)
     {
       modi_mask |= MODI_CTM;
@@ -2194,24 +2606,20 @@
 
   if ( theFile->size != (off_t) p->theFile.size &&
-       (theFile->check_flags & MODI_SIZ) != 0)
-    {
-      if ((theFile->check_flags & MODI_SGROW) == 0 || 
-	  theFile->size < (off_t) p->theFile.size)
+       (theFile->check_mask & MODI_SIZ) != 0)
+    {
+      if (class == SH_LEVEL_LOGGROW && theFile->size < (off_t) p->theFile.size)
 	{
 	  modi_mask |= MODI_SIZ;
 	  change_code[9] = 'S';
 	  TPT ((0, FIL__, __LINE__, _("mod=<size>")));
-	}
+	} 
+      else if (class != SH_LEVEL_LOGGROW)
+	{ 
+	  modi_mask |= MODI_SIZ;
+	  change_code[9] = 'S';
+	  TPT ((0, FIL__, __LINE__, _("mod=<size>")));
+	} 
     }
   change_code[10] = '\0';
-
-  /* --- Directories special case ---
-   */
-  if (p->theFile.c_mode[0] == 'd'                               &&
-      0 == (modi_mask & ~(MODI_SIZ|MODI_ATM|MODI_CTM|MODI_MTM)) && 
-      sh_loosedircheck == S_TRUE)
-    {
-      modi_mask = 0;
-    }
 
   /* --- Report full details. ---
@@ -2219,5 +2627,5 @@
   if (modi_mask != 0 && sh.flag.fulldetail == S_TRUE)
     {
-      if ((theFile->check_flags & MODI_ATM) == 0)
+      if ((theFile->check_mask & MODI_ATM) == 0)
 	modi_mask = MASK_READONLY_;
       else
@@ -2227,74 +2635,47 @@
   /* --- Report on modified files. ---
    */
-  if (modi_mask != 0 && (!SH_FFLAG_REPORTED_SET(p->fflags)))
+  if (modi_mask != 0 && p->reported == S_FALSE)
     { 
-      tmp = SH_ALLOC(SH_MSG_BUF);
-      msg = SH_ALLOC(SH_MSG_BUF);
+      tmp = SH_ALLOC(SH_BUFSIZE);
+      msg = SH_ALLOC(SH_BUFSIZE);
       msg[0] = '\0';
 
-      sh_files_fixup_mask(class, &(cf_report.newflags));
-
-      if ( (report_checkflags != S_FALSE) && (cf_report.oldflags != cf_report.newflags))
-	{
-	  sl_snprintf(tmp, SH_MSG_BUF,
-#ifdef SH_USE_XML
-		      _("checkflags_old=\"0%lo\" checkflags_new=\"0%lo\" "),
-#else
-		      _("checkflags_old=<0%lo>, checkflags_new=<0%lo>, "),
-#endif
-		      cf_report.oldflags,  cf_report.newflags);
-	  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-	}
-
       if (   ((modi_mask & MODI_MOD) != 0)
-#if defined(HAVE_LIBPRELUDE)
-	     || ((modi_mask & MODI_USR) != 0)
-	     || ((modi_mask & MODI_GRP) != 0)
+#if defined(HAVE_LIBPRELUDE) && defined(HAVE_LIBPRELUDE_9)
+	  || ((modi_mask & MODI_USR) != 0)
+	  || ((modi_mask & MODI_GRP) != 0)
 #endif
 	     )
 	{
-#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
-	  sl_snprintf(tmp, SH_MSG_BUF, 
+#if defined(__linux__)
+	  sl_snprintf(tmp, SH_BUFSIZE, 
 #ifdef SH_USE_XML
 		      _("mode_old=\"%s\" mode_new=\"%s\" attr_old=\"%s\" attr_new=\"%s\" imode_old=\"%ld\" imode_new=\"%ld\" iattr_old=\"%ld\" iattr_new=\"%ld\" "),
 #else
-		      _("mode_old=<%s>, mode_new=<%s>, attr_old=<%s>, attr_new=<%s>, imode_old=<%ld>, imode_new=<%ld>, iattr_old=<%ld>, iattr_new=<%ld>, "),
+		      _("mode_old=<%s>, mode_new=<%s>, attr_old=<%s>, attr_new=<%s>, "),
 #endif
 		      p->theFile.c_mode, theFile->c_mode,
-		      p->theFile.c_attributes, theFile->c_attributes,
-
-		      (long) p->theFile.mode, (long) theFile->mode,
+		      p->theFile.c_attributes, theFile->c_attributes
+#ifdef SH_USE_XML
+		      , (long) p->theFile.mode, (long) theFile->mode,
 		      (long) p->theFile.attributes, 
 		      (long) theFile->attributes
+#endif
 		      );
 #else
 #ifdef SH_USE_XML
-	  sl_snprintf(tmp, SH_MSG_BUF, 
+	  sl_snprintf(tmp, SH_BUFSIZE, 
 		      _("mode_old=\"%s\" mode_new=\"%s\" imode_old=\"%ld\" imode_new=\"%ld\" "),
-		      p->theFile.c_mode, theFile->c_mode,
-		      (long) p->theFile.mode, (long) theFile->mode);
-#else
-	  sl_snprintf(tmp, SH_MSG_BUF, _("mode_old=<%s>, mode_new=<%s>, "),
-		      p->theFile.c_mode, theFile->c_mode);
-#endif
-#endif
-	  sl_strlcat(msg, tmp, SH_MSG_BUF);
-
-#if defined(USE_ACL) || defined(USE_XATTR)
-	  if (theFile->attr_string != NULL || p->attr_string != NULL)
-	    {
-	      sl_snprintf(tmp, SH_MSG_BUF, 
+#else
+		      sl_snprintf(tmp, SH_BUFSIZE, _("mode_old=<%s>, mode_new=<%s>, "),
+#endif
+		      p->theFile.c_mode, theFile->c_mode
 #ifdef SH_USE_XML
-			  _("acl_old=\"%s\" acl_new=\"%s\" "),
-#else
-			  _("acl_old=<%s>, acl_new=<%s>, "),
-#endif
-			  (p->attr_string)       ? p->attr_string       : _("none"), 
-			  (theFile->attr_string) ? theFile->attr_string : _("none"));
-	      
-	      sl_strlcat(msg, tmp, SH_MSG_BUF);
-	    }
-#endif
-
+		      , (long) p->theFile.mode, (long) theFile->mode
+#endif
+		      );
+#endif
+	  sl_strlcat(msg, tmp, SH_BUFSIZE);
+#ifdef REPLACE_OLD
 	  if ((modi_mask & MODI_MOD) != 0)
 	    {
@@ -2307,50 +2688,37 @@
 		  sl_strlcpy(p->theFile.c_mode, theFile->c_mode, 11);
 		  p->theFile.mode = theFile->mode;
-#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
+#if defined(__linux__)
 		  sl_strlcpy(p->theFile.c_attributes,theFile->c_attributes,16);
 		  p->theFile.attributes = theFile->attributes;
 #endif
-#if defined(USE_ACL) || defined(USE_XATTR)
-		  if      (p->attr_string == NULL && theFile->attr_string != NULL)
-		    { p->attr_string = sh_util_strdup (theFile->attr_string); }
-		  else if (p->attr_string != NULL && theFile->attr_string == NULL)
-		    { SH_FREE(p->attr_string); p->attr_string = NULL; }
-		  else if (theFile->attr_string != NULL && p->attr_string != NULL)
-		    { 
-		      if (0 != strcmp(theFile->attr_string, p->attr_string))
-			{
-			  SH_FREE(p->attr_string);
-			  p->attr_string = sh_util_strdup (theFile->attr_string);
-			}
-		    }
-#endif
 		}
 	    }
-
+#endif
 	}
 
       if ((modi_mask & MODI_HLN) != 0)
 	{
-	  sl_snprintf(tmp, SH_MSG_BUF, 
+	  sl_snprintf(tmp, SH_BUFSIZE, 
 #ifdef SH_USE_XML
-		      _("hardlinks_old=\"%lu\" hardlinks_new=\"%lu\" "),
-#else
-		      _("hardlinks_old=<%lu>, hardlinks_new=<%lu>, "),
+		      _("hardlinks_old=\"%ld\" hardlinks_new=\"%ld\" "),
+#else
+		      _("hardlinks_old=<%ld>, hardlinks_new=<%ld>, "),
 #endif
 		      (unsigned long) p->theFile.hardlinks, 
 		      (unsigned long) theFile->hardlinks);
-	  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-
+	  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+#ifdef REPLACE_OLD
 	  if (sh.flag.reportonce == S_TRUE && sh.flag.update == S_FALSE)
 	    p->theFile.hardlinks = theFile->hardlinks;
+#endif
 	}
 
       if ((modi_mask & MODI_RDEV) != 0)
 	{
-	  sl_snprintf(tmp, SH_MSG_BUF,
+	  sl_snprintf(tmp, SH_BUFSIZE,
 #ifdef SH_USE_XML 
-		      _("device_old=\"%lu,%lu\" device_new=\"%lu,%lu\" idevice_old=\"%lu\" idevice_new=\"%lu\" "),
-#else
-		      _("device_old=<%lu,%lu>, device_new=<%lu,%lu>, "),
+		      _("device_old=\"%ld,%ld\" device_new=\"%ld,%ld\" idevice_old=\"%ld\" idevice_new=\"%ld\" "),
+#else
+		      _("device_old=<%ld,%ld>, device_new=<%ld,%ld>, "),
 #endif
 		      (unsigned long) major(p->theFile.rdev), 
@@ -2363,72 +2731,46 @@
 #endif
 		      );
-	  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-
+	  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+#ifdef REPLACE_OLD
 	  if (sh.flag.reportonce == S_TRUE && sh.flag.update == S_FALSE)
 	    p->theFile.rdev = theFile->rdev;
+#endif
 	}
 
       if ((modi_mask & MODI_INO) != 0)
 	{
-	  sl_snprintf(tmp, SH_MSG_BUF,
+	  sl_snprintf(tmp, SH_BUFSIZE,
 #ifdef SH_USE_XML 
-		      _("inode_old=\"%lu\" inode_new=\"%lu\" "),
-#else
-		      _("inode_old=<%lu>, inode_new=<%lu>, "),
+		      _("inode_old=\"%ld\" inode_new=\"%ld\" "),
+#else
+		      _("inode_old=<%ld>, inode_new=<%ld>, "),
 #endif
 		      (unsigned long) p->theFile.ino, 
 		      (unsigned long) theFile->ino);
-	  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-
+	  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+#ifdef REPLACE_OLD
 	  if (sh.flag.reportonce == S_TRUE && sh.flag.update == S_FALSE)
-	    {
-	      p->theFile.ino = theFile->ino;
-	      p->theFile.dev = theFile->dev;
-	    }
-	}
-
-
-      /* 
-       * also report device for prelude
-       */
-#if defined(HAVE_LIBPRELUDE)
-      if ((modi_mask & MODI_INO) != 0)
-	{
-	  sl_snprintf(tmp, SH_MSG_BUF,
-#ifdef SH_USE_XML 
-		      _("dev_old=\"%lu,%lu\" dev_new=\"%lu,%lu\" "),
-#else
-		      _("dev_old=<%lu,%lu>, dev_new=<%lu,%lu>, "),
-#endif
-		      (unsigned long) major(p->theFile.dev),
-		      (unsigned long) minor(p->theFile.dev),
-		      (unsigned long) major(theFile->dev),
-		      (unsigned long) minor(theFile->dev)
+	    p->theFile.ino = theFile->ino;
+#endif
+	}
+
+      if (   ((modi_mask & MODI_USR) != 0)
+#if defined(HAVE_LIBPRELUDE) && defined(HAVE_LIBPRELUDE_9)
+	  || ((modi_mask & MODI_MOD) != 0)
+#endif
+	  )
+	{
+#ifdef SH_USE_XML
+	  sl_snprintf(tmp, SH_BUFSIZE, _("owner_old=\"%s\" owner_new=\"%s\" iowner_old=\"%ld\" iowner_new=\"%ld\" "),
+#else
+	  sl_snprintf(tmp, SH_BUFSIZE, _("owner_old=<%s>, owner_new=<%s>, "),
+#endif
+		      p->theFile.c_owner, theFile->c_owner
+#ifdef SH_USE_XML
+		      , (long) p->theFile.owner, (long) theFile->owner
+#endif
 		      );
-	  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-
-	  if (sh.flag.reportonce == S_TRUE && sh.flag.update == S_FALSE)
-	    p->theFile.dev = theFile->dev;
-	}
-#endif
-
-      if (   ((modi_mask & MODI_USR) != 0)
-#if defined(HAVE_LIBPRELUDE)
-	  || ((modi_mask & MODI_MOD) != 0)
-#endif
-	  )
-	{
-#ifdef SH_USE_XML
-	  sl_snprintf(tmp, SH_MSG_BUF, 
-		      _("owner_old=\"%s\" owner_new=\"%s\" iowner_old=\"%ld\" iowner_new=\"%ld\" "),
-#else
-	  sl_snprintf(tmp, SH_MSG_BUF, 
-		      _("owner_old=<%s>, owner_new=<%s>, iowner_old=<%ld>, iowner_new=<%ld>, "),
-#endif
-		      p->theFile.c_owner, theFile->c_owner, 
-		      (long) p->theFile.owner, (long) theFile->owner
-		      );
-	  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-
+	  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+#ifdef REPLACE_OLD
 	  if ((modi_mask & MODI_USR) != 0) {
 	    if (sh.flag.reportonce == S_TRUE && sh.flag.update == S_FALSE)
@@ -2438,8 +2780,9 @@
 	      }
 	  }
+#endif
 	}
 
       if (   ((modi_mask & MODI_GRP) != 0)
-#if defined(HAVE_LIBPRELUDE)
+#if defined(HAVE_LIBPRELUDE) && defined(HAVE_LIBPRELUDE_9)
 	  || ((modi_mask & MODI_MOD) != 0)
 #endif
@@ -2447,17 +2790,14 @@
 	{
 #ifdef SH_USE_XML
-	  sl_snprintf(tmp, SH_MSG_BUF, 
-		      _("group_old=\"%s\" group_new=\"%s\" igroup_old=\"%ld\" igroup_new=\"%ld\" "),
+	  sl_snprintf(tmp, SH_BUFSIZE, _("group_old=\"%s\" group_new=\"%s\" igroup_old=\"%ld\" igroup_new=\"%ld\" "),
 		      p->theFile.c_group, theFile->c_group,
 		      (long) p->theFile.group, (long) theFile->group);
 #else
-	  sl_snprintf(tmp, SH_MSG_BUF, 
-		      _("group_old=<%s>, group_new=<%s>, igroup_old=<%ld>, igroup_new=<%ld>, "),
-		      p->theFile.c_group, theFile->c_group,
-		      (long) p->theFile.group, (long) theFile->group);
-#endif
-
-	  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-
+	  sl_snprintf(tmp, SH_BUFSIZE, _("group_old=<%s>, group_new=<%s>, "),
+		      p->theFile.c_group, theFile->c_group);
+#endif
+
+	  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+#ifdef REPLACE_OLD
           if ((modi_mask & MODI_GRP) != 0) {
 	    if (sh.flag.reportonce == S_TRUE && sh.flag.update == S_FALSE)
@@ -2467,66 +2807,71 @@
 	      }
 	  }
+#endif
 	}
 
       if ((modi_mask & MODI_SIZ) != 0)
 	{
-	  sl_snprintf(tmp, SH_MSG_BUF, sh_hash_size_format(),
+	  sl_snprintf(tmp, SH_BUFSIZE, sh_hash_size_format(),
 		      (UINT64) p->theFile.size, 
 		      (UINT64) theFile->size);
-	  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-
+	  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+#ifdef REPLACE_OLD
 	  if (sh.flag.reportonce == S_TRUE && sh.flag.update == S_FALSE)
 	    p->theFile.size = theFile->size;
+#endif
 	}
 
       if ((modi_mask & MODI_CTM) != 0)
 	{
-	  (void) sh_unix_gmttime (p->theFile.ctime, timstr1c, sizeof(timstr1c));
-	  (void) sh_unix_gmttime (theFile->ctime,   timstr2c, sizeof(timstr2c));
+	  sl_strlcpy (timstr1c, sh_unix_gmttime (p->theFile.ctime), 32);
+	  sl_strlcpy (timstr2c, sh_unix_gmttime (theFile->ctime),   32);
 #ifdef SH_USE_XML
-	  sl_snprintf(tmp, SH_MSG_BUF, _("ctime_old=\"%s\" ctime_new=\"%s\" "),
+	  sl_snprintf(tmp, SH_BUFSIZE, _("ctime_old=\"%s\" ctime_new=\"%s\" "),
 		      timstr1c, timstr2c);
 #else
-	  sl_snprintf(tmp, SH_MSG_BUF, _("ctime_old=<%s>, ctime_new=<%s>, "),
+	  sl_snprintf(tmp, SH_BUFSIZE, _("ctime_old=<%s>, ctime_new=<%s>, "),
 		      timstr1c, timstr2c);
 #endif
-	  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-
+	  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+#ifdef REPLACE_OLD
 	  if (sh.flag.reportonce == S_TRUE && sh.flag.update == S_FALSE)
 	    p->theFile.ctime = theFile->ctime;
+#endif
 	}
 
       if ((modi_mask & MODI_ATM) != 0)
 	{
-	  (void) sh_unix_gmttime (p->theFile.atime, timstr1a, sizeof(timstr1a));
-	  (void) sh_unix_gmttime (theFile->atime,   timstr2a, sizeof(timstr2a));
+	  sl_strlcpy (timstr1a, sh_unix_gmttime (p->theFile.atime), 32);
+	  sl_strlcpy (timstr2a, sh_unix_gmttime (theFile->atime),   32);
 #ifdef SH_USE_XML
-	  sl_snprintf(tmp, SH_MSG_BUF, _("atime_old=\"%s\" atime_new=\"%s\" "),
+	  sl_snprintf(tmp, SH_BUFSIZE, _("atime_old=\"%s\" atime_new=\"%s\" "),
 		      timstr1a, timstr2a);
 #else
-	  sl_snprintf(tmp, SH_MSG_BUF, _("atime_old=<%s>, atime_new=<%s>, "),
+	  sl_snprintf(tmp, SH_BUFSIZE, _("atime_old=<%s>, atime_new=<%s>, "),
 		      timstr1a, timstr2a);
 #endif
-	  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-
+	  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+#ifdef REPLACE_OLD
 	  if (sh.flag.reportonce == S_TRUE && sh.flag.update == S_FALSE)
 	    p->theFile.atime = theFile->atime;
+#endif
 	}
 
       if ((modi_mask & MODI_MTM) != 0)
 	{
-	  (void) sh_unix_gmttime (p->theFile.mtime, timstr1m, sizeof(timstr1m));
-	  (void) sh_unix_gmttime (theFile->mtime,   timstr2m, sizeof(timstr2m));
+	  sl_strlcpy (timstr1m, sh_unix_gmttime (p->theFile.mtime), 32);
+	  sl_strlcpy (timstr2m, sh_unix_gmttime (theFile->mtime),   32);
 #ifdef SH_USE_XML
-	  sl_snprintf(tmp, SH_MSG_BUF, _("mtime_old=\"%s\" mtime_new=\"%s\" "),
+	  sl_snprintf(tmp, SH_BUFSIZE, _("mtime_old=\"%s\" mtime_new=\"%s\" "),
 		      timstr1m, timstr2m);
 #else
-	  sl_snprintf(tmp, SH_MSG_BUF, _("mtime_old=<%s>, mtime_new=<%s>, "),
+	  sl_snprintf(tmp, SH_BUFSIZE, _("mtime_old=<%s>, mtime_new=<%s>, "),
 		      timstr1m, timstr2m);
 #endif
-	  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-
+	  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+#ifdef REPLACE_OLD
 	  if (sh.flag.reportonce == S_TRUE && sh.flag.update == S_FALSE)
 	    p->theFile.mtime = theFile->mtime;
+#endif
 	}
 
@@ -2534,5 +2879,5 @@
       if ((modi_mask & MODI_CHK) != 0)
 	{
-	  sl_snprintf(tmp, SH_MSG_BUF, 
+	  sl_snprintf(tmp, SH_BUFSIZE, 
 #ifdef SH_USE_XML
 		      _("chksum_old=\"%s\" chksum_new=\"%s\" "),
@@ -2541,96 +2886,52 @@
 #endif
 		      p->theFile.checksum, fileHash);
-	  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-
+	  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+#ifdef REPLACE_OLD
 	  if (sh.flag.reportonce == S_TRUE && sh.flag.update == S_FALSE)
-	    {
-	      sl_strlcpy(p->theFile.checksum, fileHash, KEY_LEN+1);
-	      if ((theFile->check_flags & MODI_SGROW) != 0)	      
-		p->theFile.size  = theFile->size;
-	    }
-
-
-	  if (theFile->c_mode[0] != 'l' && theFile->link_path &&
-	      strlen(theFile->link_path) > 2)
-	    modi_mask |= MODI_LNK;
-	}
-
-
-      if ((modi_mask & MODI_LNK) != 0 /* && theFile->c_mode[0] == 'l' */)
-	{
-	  if (theFile->link_path)
-	    tmp_lnk     = sh_util_safe_name(theFile->link_path);
-	  else
-	    tmp_lnk     = sh_util_strdup("-");
-	  if (p->linkpath)
-	    tmp_lnk_old = sh_util_safe_name(p->linkpath);
-	  else
-	    tmp_lnk_old = sh_util_strdup("-");
+	    sl_strlcpy(p->theFile.checksum, fileHash, KEY_LEN+1);
+#endif
+	}
+
+
+      if ((modi_mask & MODI_LNK) != 0 && theFile->c_mode[0] == 'l')
+	{
+	  tmp_lnk     = sh_util_safe_name(theFile->linkpath);
+	  tmp_lnk_old = sh_util_safe_name(p->linkpath);
 #ifdef SH_USE_XML
-	  sl_snprintf(tmp, SH_MSG_BUF, _("link_old=\"%s\" link_new=\"%s\" "),
+	  sl_snprintf(tmp, SH_BUFSIZE, _("link_old=\"%s\" link_new=\"%s\" "),
 		      tmp_lnk_old, tmp_lnk);
 #else
-	  sl_snprintf(tmp, SH_MSG_BUF, _("link_old=<%s>, link_new=<%s>, "),
+	  sl_snprintf(tmp, SH_BUFSIZE, _("link_old=<%s>, link_new=<%s>"),
 		      tmp_lnk_old, tmp_lnk);
 #endif
 	  SH_FREE(tmp_lnk);
 	  SH_FREE(tmp_lnk_old);
-	  sl_strlcat(msg, tmp, SH_MSG_BUF); 
-
+	  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+#ifdef REPLACE_OLD
 	  if (sh.flag.reportonce == S_TRUE && sh.flag.update == S_FALSE)
 	    {
 	      if (p->linkpath != NULL)
 		SH_FREE(p->linkpath);
-	      if (!(theFile->link_path))
-		p->linkpath = sh_util_strdup("-");
-	      else
-		p->linkpath = sh_util_strdup(theFile->link_path);
+	      p->linkpath = (char*)SH_ALLOC (sl_strlen(theFile->linkpath) + 1);
+	      sl_strlcpy(p->linkpath, theFile->linkpath, 
+			 sl_strlen(theFile->linkpath) + 1);
 	    }
-	}
-
-      if (MODI_AUDIT_ENABLED(theFile->check_flags))
-	{
-	  char result[256];
-	  
-	  sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, 
-			   0, MSG_E_SUBGPATH,
-			   _("Fetching audit record"),
-			   _("sh_hash"),  theFile->fullpath );
-
-	  if (NULL != sh_audit_fetch (theFile->fullpath, theFile->mtime, theFile->ctime, theFile->atime,
-				      result, sizeof(result)))
-	    {
-#ifdef SH_USE_XML
-	      sl_strlcat(msg, _("obj=\""), SH_MSG_BUF);
-#else
-	      sl_strlcat(msg, _("obj=<"), SH_MSG_BUF);
-#endif
-
-	      sl_strlcat(msg, result, SH_MSG_BUF);
-
-#ifdef SH_USE_XML
-	      sl_strlcat(msg, _("\" "), SH_MSG_BUF);
-#else
-	      sl_strlcat(msg, _(">"), SH_MSG_BUF);
-#endif
-	    } 
-	}
-
-      /****************************************************
-       *
-       * REPORT on file change
-       *
-       ****************************************************/
+#endif
+	}
+
+
       tmp_path = sh_util_safe_name(theFile->fullpath);
-      if (!sh_global_check_silent)
-	sh_error_handle(log_severity, FIL__, __LINE__, 
-			(long) modi_mask, MSG_FI_CHAN,
-			(policy_override == NULL) ? _(policy[class]):log_policy,
-			change_code, tmp_path, msg);
-      ++sh.statistics.files_report;
+      sh_error_handle(log_severity, FIL__, __LINE__, 
+		      (long) modi_mask, MSG_FI_CHAN,
+		      (policy_override == NULL) ? _(policy[class]):log_policy,
+		      change_code, tmp_path, msg);
 
       SH_FREE(tmp_path);
       SH_FREE(tmp);
       SH_FREE(msg);
+
+#ifndef REPLACE_OLD
+      p->reported = S_TRUE;
+#endif
 
       if (S_TRUE  == sh.flag.update)
@@ -2643,40 +2944,17 @@
 	      sl_strlcpy(theFile->c_mode, p->theFile.c_mode, 11);
 	      theFile->mode  =  p->theFile.mode;
-#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
+#if defined(__linux__)
 	      sl_strlcpy(theFile->c_attributes, p->theFile.c_attributes, 16);
 	      theFile->attributes =  p->theFile.attributes;
-#endif
-#if defined(USE_ACL) || defined(USE_XATTR)
-	      if      (theFile->attr_string == NULL && p->attr_string != NULL)
-		{ theFile->attr_string = sh_util_strdup (p->attr_string); }
-	      else if (theFile->attr_string != NULL && p->attr_string == NULL)
-		{ SH_FREE(theFile->attr_string); theFile->attr_string = NULL; }
-	      else if (theFile->attr_string != NULL && p->attr_string != NULL)
-		{ 
-		  if (0 != strcmp(theFile->attr_string, p->attr_string))
-		    {
-		      SH_FREE(theFile->attr_string);
-		      theFile->attr_string = sh_util_strdup (p->attr_string);
-		    }
-		}
 #endif
 	      
 	      if (theFile->c_mode[0] == 'l') /* c_mode is already copied */
 		{
-		  if (theFile->link_path)
-		    SH_FREE(theFile->link_path);
-		  if (p->linkpath)
-		    theFile->link_path = sh_util_strdup(p->linkpath);
-		  else
-		    theFile->link_path = sh_util_strdup("-");
+		  sl_strlcpy(theFile->linkpath, p->linkpath, PATH_MAX);
 		}
 	      else
 		{
-		  if (theFile->link_path)
-		    SH_FREE(theFile->link_path);
-		  if (p->linkpath)
-		    theFile->link_path = sh_util_strdup(p->linkpath);
-		  else
-		    theFile->link_path = NULL;
+		  theFile->linkpath[0] = '-';
+		  theFile->linkpath[1] = '\0';
 		}
 	      
@@ -2699,10 +2977,8 @@
 	      theFile->hardlinks = p->theFile.hardlinks;
 	      
-	      SET_SH_FFLAG_VISITED(p->fflags);
-	      CLEAR_SH_FFLAG_CHECKED(p->fflags);
-	      retval = 1;
-	      goto unlock_and_return;
+	      p->visited = S_TRUE;
+	      SL_RETURN(1, _("sh_hash_compdata"));
 	    }
-	  else /* if (sh.flag.reportonce == S_TRUE) */
+	  else if (sh.flag.reportonce == S_TRUE)
 	    {
 	      /* we replace the data in the in-memory copy of the
@@ -2712,34 +2988,27 @@
 	      sl_strlcpy(p->theFile.c_mode, theFile->c_mode, 11);
 	      p->theFile.mode  =  theFile->mode;
-#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
+#if defined(__linux__)
 	      sl_strlcpy(p->theFile.c_attributes, theFile->c_attributes, 16);
 	      p->theFile.attributes = theFile->attributes;
 #endif
-#if defined(USE_ACL) || defined(USE_XATTR)
-	      if      (p->attr_string == NULL && theFile->attr_string != NULL)
-		{ p->attr_string = sh_util_strdup (theFile->attr_string); }
-	      else if (p->attr_string != NULL && theFile->attr_string == NULL)
-		{ SH_FREE(p->attr_string); p->attr_string = NULL; }
-	      else if (theFile->attr_string != NULL && p->attr_string != NULL)
-		{ 
-		  if (0 != strcmp(theFile->attr_string, p->attr_string))
-		    {
-		      SH_FREE(p->attr_string);
-		      p->attr_string = sh_util_strdup (theFile->attr_string);
-		    }
-		}
-#endif
 	      
-	      if (theFile->c_mode[0] == 'l' || theFile->link_path)
+	      if (theFile->c_mode[0] == 'l')
 		{
                   if (p->linkpath != NULL)
 		    SH_FREE(p->linkpath);
-		  p->linkpath = sh_util_strdup(theFile->link_path);
+		  p->linkpath = SH_ALLOC(1 + strlen(theFile->linkpath));
+		  sl_strlcpy(p->linkpath, theFile->linkpath, 
+			     1 + strlen(theFile->linkpath));
 		}
 	      else
 		{
-	          if (p->linkpath != NULL)
-		    SH_FREE(p->linkpath);
-		  p->linkpath = sh_util_strdup("-");
+	          if (p->linkpath != NULL) {
+		    p->linkpath[0] = '-';
+		    p->linkpath[1] = '\0';
+                  } else {
+		    p->linkpath = SH_ALLOC(2);
+		    p->linkpath[0] = '-';
+                    p->linkpath[1] = '\0';
+                  }
 		}
 	      
@@ -2765,11 +3034,7 @@
     }
 
-  SET_SH_FFLAG_VISITED(p->fflags);
-  CLEAR_SH_FFLAG_CHECKED(p->fflags);
-
- unlock_and_return:
-  ; /* 'label at end of compound statement */
-  SH_MUTEX_UNLOCK(mutex_hash);
-  SL_RETURN(retval, _("sh_hash_compdata"));
+  p->visited = S_TRUE;
+
+  SL_RETURN(0, _("sh_hash_compdata"));
 }
 
@@ -2779,266 +3044,91 @@
   int         i;
 
-  SL_ENTER(_("hash_full_tree"));
+  SL_ENTER(_("sh_hash_compdata"));
 
   if (IsInit != 1) 
-    SL_RETURN(0, _("hash_full_tree"));
-
-  SH_MUTEX_LOCK_UNSAFE(mutex_hash);
+    SL_RETURN(0, _("sh_hash_compdata"));
+
   for (i = 0; i < TABSIZE; ++i)
     {
       for (p = tab[i]; p; p = p->next)
-	CLEAR_SH_FFLAG_ALLIGNORE(p->fflags);
-    }
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_hash);
-  SL_RETURN (0, _("hash_full_tree"));
+	p->allignore  = S_FALSE;
+    }
+  SL_RETURN (0, _("sh_hash_compdata"));
 } 
 
-#if !defined(SH_CUTEST)
-static 
-#endif
-int hash_remove_tree_test(char * s, char * fullpath, size_t len_s)
-{
-  size_t       len_p;
-  char      *  test;
-
-  len_p = strlen(fullpath);
-  
-  if (len_p >= len_s)
-    {
-      if (0 == strncmp(s, fullpath, len_s)) 
-	{ 
-	  if (len_p > len_s)
-	    {
-	      /* continue if not inside directory;
-	       * len_s > 1 because everything is inside '/' 
-	       */
-	      if ((len_s > 1) && (fullpath[len_s] != '/'))
-		return S_FALSE;
-
-	      test = sh_files_find_mostspecific_dir(fullpath);
-	      
-	      if (test && 0 != strcmp(test, s)) {
-		/* There is a more specific directory, continue */
-		return S_FALSE;
-	      }
-	      
-	      if (NULL == sh_files_findfile(fullpath)) {
-		/* SET_SH_FFLAG_ALLIGNORE(p->fflags); */
-		return S_TRUE;
-	      }
-	    }
-	  else /* len_p == len */
-	    {
-	      /* it is 's' itself, mark and continue 
-	       * unless there is a policy for the inode itself
-	       */
-	      if (NULL == sh_files_findfile(fullpath)) {
-		/* SET_SH_FFLAG_ALLIGNORE(p->fflags); */
-		return S_TRUE;
-	      }
-	      else {
-		return S_FALSE;
-	      }
-	    }
-
-	} /* if path is in tree */
-    } /* if path is possibly in tree */
-  return S_FALSE;
-}
-
 
 int hash_remove_tree (char * s) 
 {
-  sh_file_t *  p;
-  size_t       len_s;
-  unsigned int i;
+  sh_file_t * p;
+  int         len;
+  int         i;
 
   SL_ENTER(_("hash_remove_tree"));
 
-  if (!s || *s == '\0')
+  if (!s)
     SL_RETURN ((-1), _("hash_remove_tree"));
-
-  len_s = sl_strlen(s);
+  else
+    len = sl_strlen(s);
 
   if (IsInit != 1) 
     sh_hash_init();
 
-  SH_MUTEX_LOCK_UNSAFE(mutex_hash);
   for (i = 0; i < TABSIZE; ++i)
     {
       for (p = tab[i]; p; p = p->next)
 	{
-	  if (p->fullpath)
-	    {
-	      /* if (0 == strncmp(s, p->fullpath, len_s)) *//* old */
-	      if (S_TRUE == hash_remove_tree_test(s, p->fullpath, len_s)) {
-		SET_SH_FFLAG_ALLIGNORE(p->fflags);
-		MODI_SET(p->theFile.checkflags, MODI_ALLIGNORE);
-	      }
-	    } /* if path is not null */
-
-	}
-    }
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_hash);
+	  if (sl_strncmp(s, p->fullpath, len) == 0)
+	    { 
+	      p->allignore  = S_TRUE;
+	    }
+	}
+    }
   SL_RETURN ((0), _("hash_remove_tree"));
 } 
 
+#if TIME_WITH_SYS_TIME
+#include <sys/time.h>
+#include <time.h>
+#else
 #if HAVE_SYS_TIME_H
 #include <sys/time.h>
-#endif
+#else
 #include <time.h>
-
-static int ListFullDetail    = S_FALSE;
+#endif
+#endif
+
+static int ListFullDetail = S_FALSE;
 static int ListWithDelimiter = S_FALSE;
-static char * ListFile       = NULL;
-
-int set_list_file (const char * c)
-{
-  ListFile = sh_util_strdup(c);
-  return 0;
-}
-char * get_list_file()
-{
-  return ListFile;
-}
-
-int set_full_detail (const char * c)
-{
-  (void) c;
+
+int set_full_detail (char * c)
+{
   ListFullDetail = S_TRUE;
-  return 0;
+  /* warning: unused parameter `c' */
+  if (c)
+    return 0;
+  else
+    return 0;
 }
  
-int set_list_delimited (const char * c)
-{
-  (void) c;
+int set_list_delimited (char * c)
+{
   ListFullDetail = S_TRUE;
   ListWithDelimiter = S_TRUE;
-  return 0;
-}
-
-/* Always quote the string, except if it is empty. Quote quotes by
- * doubling them.
- */
-char * csv_escape(const char * str)
-{
-  const  char * p = str;
-  const  char * q;
-
-  size_t size       = 0;
-  size_t flag_quote = 0;
-
-  char * new;
-  char * pnew;
-
-  if (p)
-    {
-
-      while (*p) 
-	{
-	  if (*p == '"')
-	    ++flag_quote;
-	  
-	  ++size; ++p;
-	}
-
-      if (sl_ok_adds(size, flag_quote))
-	size += flag_quote;      /* double each quote */
-      else
-	return NULL;
-
-      if (sl_ok_adds(size, 3))
-	size += 3; /* two quotes and terminating null */
-      else
-	return NULL;
-      
-      new = SH_ALLOC(size);
-      
-      if (flag_quote != 0)
-	{
-	  new[0] = '"';
-	  pnew = &new[1];
-	  q    = str;
-	  while (*q)
-	    {
-	      *pnew = *q;
-	      if (*pnew == '"')
-		{
-		  ++pnew; *pnew = '"';
-		}
-	      ++pnew; ++q;
-	    }
-	  *pnew = '"'; ++pnew;
-	  *pnew = '\0';
-	}
-      else
-	{
-	  if (size > 3) 
-	    {
-	      new[0] = '"';
-	      sl_strlcpy (&new[1], str, size-1);
-	      new[size-2] = '"';
-	      new[size-1] = '\0';
-	    }
-	  else
-	    {
-	      new[0] = '\0';
-	    }
-	}
-
-      return new;
-    }
-  return NULL;
-}
-
-int isHexKey(char * s)
-{
-  int i;
-  
-  for (i = 0; i < KEY_LEN; ++i)
-    {
-      if (*s)
-	{
-	  if ((*s >= '0' && *s <= '9') ||
-	      (*s >= 'A' && *s <= 'F') ||
-	      (*s >= 'a' && *s <= 'f'))
-	    {
-	      ++s;
-	      continue;
-	    }
-	}
-      return S_FALSE;
-    }
-  return S_TRUE;
+  /* warning: unused parameter `c' */
+  if (c)
+    return 0;
+  else
+    return 0;
 }
  
-#include "sh_checksum.h"
-
-static char * KEYBUFtolower (char * s, char * result)
-{
-  char * r = result;
-  if (s)
-    {
-      for (; *s; ++s)
-	{ 
-	  *r = tolower((unsigned char) *s); ++r;
-	}
-      *r = '\0';
-    }
-  return result;
-}
-
 void sh_hash_list_db_entry_full_detail (sh_file_t * p)
 {
   char * tmp;
-  char * esc;
   char   str[81];
-  char   hexdigest[SHA256_DIGEST_STRING_LENGTH];
-  char   keybuffer[KEYBUF_SIZE];
 
   if (ListWithDelimiter == S_TRUE)
     {
-      printf(_("%7ld, %7ld, %10s, %5d, %12s, %5d, %3d, %-8s, %5d, %-8s, %5d, "),
-	     (unsigned long) p->theFile.ino, (unsigned long) p->theFile.dev,
+      printf(_("%7ld, %10s, %5d, %12s, %5d, %3d, %-8s, %5d, %-8s, %5d, "),
+	     (unsigned long) p->theFile.ino,
 	     p->theFile.c_mode, (int) p->theFile.mode,
 	     p->theFile.c_attributes, (int) p->theFile.attributes,
@@ -3049,6 +3139,6 @@
   else
     {
-      printf(_("%7ld %7ld %10s %5d %12s %5d %3d %-8s %5d %-8s %5d "),
-	     (unsigned long) p->theFile.ino, (unsigned long) p->theFile.dev,
+      printf(_("%7ld %10s %5d %12s %5d %3d %-8s %5d %-8s %5d "),
+	     (unsigned long) p->theFile.ino,
 	     p->theFile.c_mode, (int) p->theFile.mode,
 	     p->theFile.c_attributes, (int) p->theFile.attributes,
@@ -3059,7 +3149,7 @@
 
   if ('c' == p->theFile.c_mode[0] || 'b' == p->theFile.c_mode[0])
-    sl_snprintf(str, sizeof(str), "%"PRIu64, p->theFile.rdev);
-  else
-    sl_snprintf(str, sizeof(str), "%"PRIu64, p->theFile.size);
+    sl_snprintf(str, 80, "%"PRIi64, p->theFile.rdev);
+  else
+    sl_snprintf(str, 80, "%"PRIi64, p->theFile.size);
 
   printf( _(" %8s"), str);
@@ -3067,74 +3157,34 @@
     putchar(',');
 
-  printf( _(" %s"), sh_unix_gmttime (p->theFile.ctime, str, sizeof(str)));
+  printf( _(" %s"), sh_unix_gmttime (p->theFile.ctime));
   if (ListWithDelimiter == S_TRUE)
     putchar(',');
-  printf( _(" %s"), sh_unix_gmttime (p->theFile.mtime, str, sizeof(str)));
+  printf( _(" %s"), sh_unix_gmttime (p->theFile.mtime));
   if (ListWithDelimiter == S_TRUE)
     putchar(',');
-  printf( _(" %s"), sh_unix_gmttime (p->theFile.atime, str, sizeof(str)));
+  printf( _(" %s"), sh_unix_gmttime (p->theFile.atime));
   if (ListWithDelimiter == S_TRUE)
     putchar(',');
-
-  if (isHexKey(p->theFile.checksum))
-    printf( _(" %s"), KEYBUFtolower(p->theFile.checksum, keybuffer));
-  else
-    printf( _(" %s"), SHA256_Base2Hex(p->theFile.checksum, hexdigest));
+  printf( _(" %s"), p->theFile.checksum);
   if (ListWithDelimiter == S_TRUE)
     putchar(',');
 
   tmp = sh_util_safe_name(p->fullpath);
-  if (ListWithDelimiter != S_TRUE)
-    {
-      printf( _(" %s"), tmp);
-    }
-  else
-    {
-      esc = csv_escape(tmp);
-      printf( _(" %s,"), (esc != NULL) ? esc : _("(null)"));
-      if (esc)
-	SH_FREE(esc);
-    }
+  printf( _(" %s"), tmp);
   SH_FREE(tmp);
+  if (ListWithDelimiter == S_TRUE)
+    putchar(',');
 
   if ('l' == p->theFile.c_mode[0])
     {
       tmp = sh_util_safe_name(p->linkpath);
-      if (ListWithDelimiter != S_TRUE)
-	{
-	  printf(_(" -> %s"), tmp);
-	}
+      if (ListWithDelimiter == S_TRUE)
+	printf(_(" %s\n"), tmp);
       else
-	{
-	  esc = csv_escape(tmp);
-	  printf( _(" %s,"), (esc != NULL) ? esc : _("(null)"));
-	  if (esc)
-	    SH_FREE(esc);
-	}
+	printf(_(" -> %s\n"), tmp);
       SH_FREE(tmp);
     }
-
-  if (p->attr_string)
-    {
-      tmp = sh_util_safe_name(p->attr_string);
-      if (ListWithDelimiter != S_TRUE) 
-	{
-	  printf(_(" %s"), tmp);
-	}
-      else
-	{
-	  esc = csv_escape(tmp);
-	  printf( _(" %s"), (esc != NULL) ? esc : _("(null)"));
-	  if (esc)
-	    SH_FREE(esc);
-	}
-      SH_FREE(tmp);
-    }
-  else
-    {
-      if (ListWithDelimiter == S_TRUE)
-	printf("%s",_(" no_attr"));
-    }
-  putchar('\n');
+  else
+    printf("\n");
 
   return;
@@ -3148,49 +3198,9 @@
   time_t now  = time(NULL);
   time_t then = (time_t) p->theFile.mtime;
-  struct tm   * time_ptr;
-
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GMTIME_R)
-  struct tm     time_tm;
-#endif
-
-  if (ListFullDetail != S_FALSE)
-    {
-      sh_hash_list_db_entry_full_detail (p);
-      return;
-    }
-
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GMTIME_R)
-  time_ptr = gmtime_r(&then, &time_tm);
-  if (!time_ptr)
-    return;
-  strftime(thetime, 127, _("%b %d  %Y"), time_ptr);
-  time_ptr = gmtime_r(&now,  &time_tm);
-  if (!time_ptr)
-    return;
-  strftime(nowtime, 127, _("%b %d  %Y"), time_ptr);
+
+  strftime(thetime, 127, _("%b %d  %Y"), gmtime(&then));
+  strftime(nowtime, 127, _("%b %d  %Y"), gmtime(&now));
   if (0 == strncmp(&nowtime[7], &thetime[7], 4))
-    {
-      time_ptr = gmtime_r(&then, &time_tm);
-      if (!time_ptr)
-	return;
-      strftime(thetime, 127, _("%b %d %H:%M"), time_ptr);
-    }
-#else
-  time_ptr = gmtime(&then);
-  if (!time_ptr)
-    return;
-  strftime(thetime, 127, _("%b %d  %Y"), time_ptr);
-  time_ptr = gmtime(&now);
-  if (!time_ptr)
-    return;
-  strftime(nowtime, 127, _("%b %d  %Y"), time_ptr);
-  if (0 == strncmp(&nowtime[7], &thetime[7], 4))
-    {
-      time_ptr = gmtime(&then);
-      if (!time_ptr)
-	return;
-      strftime(thetime, 127, _("%b %d %H:%M"), time_ptr);
-    }
-#endif
+    strftime(thetime, 127, _("%b %d %H:%M"), gmtime(&then));
 
   tmp = sh_util_safe_name(p->fullpath);
@@ -3223,48 +3233,67 @@
 }
 
-#ifdef HAVE_LIBZ
-#include <zlib.h>
-#endif    
-
-int sh_hash_printcontent(char * linkpath)
-{
-#ifdef HAVE_LIBZ
-  unsigned char * decoded;
-  unsigned char * decompressed = NULL;
-  size_t dlen;
-  unsigned long clen;
-  unsigned long clen_o;
-  int    res;
-
-  if (linkpath && *linkpath != '-')
-    {
-      dlen = sh_util_base64_dec_alloc (&decoded, 
-				       (unsigned char *)linkpath, 
-				       strlen(linkpath));
-
-      clen = dlen * 2 + 1;
-
-      do {
-	if (decompressed)
-	  SH_FREE(decompressed);
-	clen += dlen; clen_o = clen;
-	decompressed = SH_ALLOC(clen);
-	res = uncompress(decompressed, &clen, decoded, dlen);
-	if (res == Z_MEM_ERROR)
-	  { fprintf(stderr, "%s",_("Error: Not enough memory\n")); return -1; }
-	if (res == Z_DATA_ERROR)
-	  { fprintf(stderr, "%s",_("Error: Data corrupt or incomplete\n")); return -1; }
-      } while (res == Z_BUF_ERROR || clen == clen_o);
-
-      decompressed[clen] = '\0';
-      fputs( (char*) decompressed, stdout);
-      SH_FREE(decompressed);
-      return 0;
-    }
-#else
-  (void) linkpath;
-#endif
-  fprintf(stderr, "%s",_("Error: No data available\n")); 
-  return -1;
+int sh_hash_list_db (char * db_file)
+{
+  sh_file_t * p;
+  SL_TICKET fd;
+  char * line;
+
+  if (!db_file)
+    {
+      _exit(EXIT_FAILURE);
+      return -1; 
+    }
+  if (sl_is_suid())
+    {
+      fprintf(stderr, _("ERROR: insufficient privilege\n"));
+      _exit (EXIT_FAILURE);
+      return -1; /* for Mac OSX compiler */
+    }
+  if (0 == strcmp(db_file, _("default")))
+    db_file = file_path('D', 'W');
+  if (!db_file)
+    {
+      _exit(EXIT_FAILURE);
+      return -1; 
+    }
+
+  line = SH_ALLOC(MAX_PATH_STORE+1);
+
+  if ( SL_ISERROR(fd = sl_open_read(db_file, SL_YESPRIV))) 
+    {
+      fprintf(stderr, _("ERROR: can't open %s for read (errnum = %ld)\n"), 
+	      db_file, fd);
+      _exit(EXIT_FAILURE);
+      return -1; 
+    }
+
+  /* fast forward to start of data
+   */
+  sh_hash_setdataent(fd, line, MAX_PATH_STORE, db_file);
+
+  while (1) 
+    {
+      p = sh_hash_getdataent (fd, line, MAX_PATH_STORE);
+      if ((p != NULL) && (p->fullpath[0] != 'K'))
+	{
+	  if (ListFullDetail == S_FALSE)
+	    sh_hash_list_db_entry (p); 
+	  else
+	    sh_hash_list_db_entry_full_detail (p); 
+	}
+      else if (p == NULL)
+	{
+	  break;
+	}
+    }
+
+  if (line != NULL)
+    SH_FREE(line);
+  sl_close (fd);
+
+  fflush(NULL);
+
+  _exit(EXIT_SUCCESS);
+  return 0; 
 }
 
Index: trunk/src/sh_html.c
===================================================================
--- trunk/src/sh_html.c	(revision 591)
+++ trunk/src/sh_html.c	(revision 1)
@@ -24,10 +24,14 @@
 #include <stdlib.h>
 
+#if TIME_WITH_SYS_TIME
+#include <sys/time.h>
+#include <time.h>
+#else
 #if HAVE_SYS_TIME_H
 #include <sys/time.h>
+#else
+#include <time.h>
 #endif
-#include <time.h>
-
-#include <unistd.h>
+#endif
 
 
@@ -36,5 +40,5 @@
 
 #include "samhain.h"
-#include "sh_xfer.h"
+#include "sh_forward.h"
 #include "sh_error.h"
 #include "sh_unix.h"
@@ -86,7 +90,4 @@
   time_t    now;
   struct tm   * time_ptr;
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
-  struct tm    time_tm;
-#endif
 
   char    * formatted;
@@ -101,5 +102,5 @@
   if (p)
     {
-      fd = sl_open_read (FIL__, __LINE__, p, SL_YESPRIV);
+      fd = sl_open_read (p, SL_YESPRIV);
       SH_FREE(p);
     }
@@ -107,5 +108,5 @@
   if (!SL_ISERROR(fd))
     {
-      while (!SL_ISERROR(status) && sh_unix_getline (fd, line, sizeof(line)) > 0) 
+      while (!SL_ISERROR(status) && sh_unix_getline (fd, line, 511) > 0) 
 	{
 	  formatted = replace_stat (line);
@@ -120,5 +121,5 @@
   else
     {
-      qstr = sh_util_basename(DEFAULT_HTML_FILE);
+      qstr = sh_util_filename(DEFAULT_HTML_FILE);
       if (qstr != NULL)
 	{
@@ -147,23 +148,11 @@
       if (!SL_ISERROR(status))
 	{
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
-	  time_ptr   = localtime_r (&(server_status.start), &time_tm);
-#else
 	  time_ptr   = localtime (&(server_status.start));
-#endif
 	  if (time_ptr != NULL) 
-	    strftime (ts1, 80, _("%d-%m-%Y %H:%M:%S"), time_ptr);
-	  else
-	    sl_strlcpy(ts1, _("01-01-1970 00:00:00"), sizeof(ts1));
+	    status = strftime (ts1, 80, _("%d-%m-%Y %H:%M:%S"), time_ptr);
 	  now = time(NULL);
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
-	  time_ptr   = localtime_r (&now, &time_tm);
-#else
 	  time_ptr   = localtime (&now);
-#endif
 	  if (time_ptr != NULL) 
-	    strftime (ts2, 80, _("%d-%m-%Y %H:%M:%S"), time_ptr);
-	  else
-	    sl_strlcpy(ts2, _("01-01-1970 00:00:00"), sizeof(ts2));
+	    status = strftime (ts2, 80, _("%d-%m-%Y %H:%M:%S"), time_ptr);
 
 	  sl_snprintf(outline, 1023, 
@@ -185,13 +174,7 @@
 	  if (server_status.last > (time_t) 0)
 	    {
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
-	      time_ptr   = localtime_r (&(server_status.last), &time_tm);
-#else
 	      time_ptr   = localtime (&(server_status.last));
-#endif
 	      if (time_ptr != NULL) 
-		strftime (ts1, 80, _("%d-%m-%Y %H:%M:%S"), time_ptr);
-	      else
-		sl_strlcpy(ts1, _("01-01-1970 00:00:00"), sizeof(ts1));
+		status = strftime (ts1, 80, _("%d-%m-%Y %H:%M:%S"), time_ptr);
 	      sl_snprintf(outline, 1023, 
 			  _("<p>Last connection at %s</p>"), 
@@ -228,5 +211,5 @@
   if (p)
     {
-      fd = sl_open_read (FIL__, __LINE__, p, SL_YESPRIV);
+      fd = sl_open_read (p, SL_YESPRIV);
       SH_FREE(p);
     }
@@ -234,5 +217,5 @@
   if (!SL_ISERROR(fd))
     {
-      while (!SL_ISERROR(status) && sh_unix_getline (fd, line, sizeof(line)) > 0) 
+      while (!SL_ISERROR(status) && sh_unix_getline (fd, line, 511) > 0) 
 	{
 	  status = sl_write_line (ticket, line, sl_strlen(line));
@@ -278,5 +261,5 @@
 
 static
-int sh_html_get_entry (void)
+int sh_html_get_entry ()
 {
   long      retval = SL_ENONE;
@@ -301,10 +284,10 @@
   if (p)
     {
-      fd = sl_open_read (FIL__, __LINE__, p, SL_YESPRIV);
+      fd = sl_open_read (p, SL_YESPRIV);
       SH_FREE(p);
     }
   if (!SL_ISERROR(fd))
     {
-      while (!SL_ISERROR(retval) && sh_unix_getline (fd, line, sizeof(line)) > 0) 
+      while (!SL_ISERROR(retval) && sh_unix_getline (fd, line, 511) > 0) 
 	{
 	  line_size = sl_strlen(line);
@@ -312,28 +295,22 @@
 	  if (entry_orig != NULL)
 	    {
-	      char * ptr = realloc(entry_orig,           /* free() ok     */
-				   entry_size + line_size + 1);
-	      if (ptr) {
-		entry_orig = ptr;
-		add_size = line_size; 
-	      } else {
-		{ free(entry_orig); entry_orig = NULL; }
-	      }
+	      entry_orig = realloc(entry_orig,           /* free() ok     */
+				   entry_size + line_size);
+	      if (entry_orig) { add_size = line_size; }
 	    }
 	  else
 	    {
-	      entry_orig = calloc(1, line_size + 1);        /* free() ok     */
-	      if (entry_orig) { entry_orig[0] = '\0'; add_size = line_size; }
+	      entry_orig = malloc(line_size + 1);        /* free() ok     */
+	      if (entry_orig) { entry_orig[0] = '\0'; add_size = line_size + 1; }
 	    }
 	  if (!entry_orig)
 	    {
 	      entry_size = 0;
-	      /* add_size   = 0; *//* never read */
+	      add_size   = 0;
 	      SL_RETURN( 0, _("sh_html_get_entry"));
 	    }
 
-	  sl_strlcat(&entry_orig[entry_size], line, line_size + 1);
+	  strcat(&entry_orig[entry_size], line);         /* known to fit  */
 	  entry_size += add_size;
-	  SH_VALIDATE_EQ(entry_orig[entry_size], '\0');
 	}
       sl_close(fd);
@@ -396,6 +373,6 @@
 int comp_arr (const void * ao, const void * bo)
 {
-  const sort_arr * a;
-  const sort_arr * b;
+  sort_arr * a;
+  sort_arr * b;
 
   if (ao == NULL && bo == NULL)
@@ -406,6 +383,6 @@
     return (1);
 
-  a = (const sort_arr *) ao;
-  b = (const sort_arr *) bo;
+  a = (sort_arr *) ao;
+  b = (sort_arr *) bo;
 
   return ((-1) * sl_strcmp(a->tim, b->tim));
@@ -417,5 +394,5 @@
   int status;
   int clt_status;
-  unsigned int i, n;
+  int i, n;
 
   SL_ENTER(_("sh_html_print_one"));
@@ -487,5 +464,5 @@
 
 
-  fd = sl_open_write_trunc (FIL__, __LINE__, DEFAULT_HTML_FILE, SL_YESPRIV);
+  fd = sl_open_write_trunc (DEFAULT_HTML_FILE, SL_YESPRIV);
 
   if (SL_ISERROR(fd))
@@ -509,30 +486,4 @@
 }
 
-int sh_html_zero()
-{
-  long fd;
-
-  SL_ENTER(_("sh_html_zero"));
-
-  if (0 != (fd = tf_trust_check (DEFAULT_HTML_FILE, SL_YESPRIV)))
-    {
-      SL_RETURN((-1), _("sh_html_zero"));
-    }
-
-  fd = sl_open_write_trunc (FIL__, __LINE__, DEFAULT_HTML_FILE, SL_YESPRIV);
-
-  if (SL_ISERROR(fd))
-    {
-     SL_RETURN((-1), _("sh_html_zero"));
-    }
-
-  sh_html_head(fd);
-  sh_html_foot(fd);
-
-  sl_close(fd);
-
-  SL_RETURN((0), _("sh_html_zero"));
-}
-
 /* SH_WITH_SERVER */
 #endif
Index: trunk/src/sh_ignore.c
===================================================================
--- trunk/src/sh_ignore.c	(revision 591)
+++ trunk/src/sh_ignore.c	(revision 1)
@@ -33,6 +33,4 @@
 #endif
 
-#include <string.h>
-
 #include "samhain.h"
 #include "sh_mem.h"
@@ -40,6 +38,4 @@
 
 #define FIL__ _("sh_ignore.c")
-
-#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
 
 struct sh_ignore_list {
@@ -55,13 +51,9 @@
 static struct sh_ignore_list * sh_del_ign = NULL;
 static struct sh_ignore_list * sh_new_ign = NULL;
-static struct sh_ignore_list * sh_mod_ign = NULL;
 
 static struct sh_ignore_list * sh_ignore_add_int(struct sh_ignore_list * list, 
-						 const char * addpath)
+						 char * addpath)
 {
   struct sh_ignore_list * new;
-  char                  * reg_expr;
-  size_t                  len;
-
 #ifdef HAVE_REGEX_H
   int                     status = -1;
@@ -73,18 +65,13 @@
   SL_ENTER(_("sh_ignore_add"));
 
-  if ( (addpath == NULL) || (sl_ok_adds(2, strlen(addpath)) == S_FALSE) )
+  if (addpath == NULL)
     {
       SL_RETURN(list, _("sh_ignore_add"));
     }
 
-  new      = SH_ALLOC(sizeof(struct sh_ignore_list));
-
-  len      = 2 + strlen(addpath);
-  reg_expr = SH_ALLOC(len);
-  sl_strlcpy(reg_expr,     "^", len);
-  sl_strlcat(reg_expr, addpath, len);
+  new  = SH_ALLOC(sizeof(struct sh_ignore_list));
 
 #ifdef HAVE_REGEX_H
-  status = regcomp(&(new->preg), reg_expr, REG_NOSUB|REG_EXTENDED);
+  status = regcomp(&(new->preg), addpath, REG_NOSUB|REG_EXTENDED);
   if (status != 0)  
     {
@@ -93,8 +80,7 @@
       errbuf[BUFSIZ] = '\0';
       sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_REGEX,
-                       errbuf, reg_expr);
+                       errbuf, addpath);
       SH_FREE(errbuf);
       SH_FREE(new);
-      SH_FREE(reg_expr);
       SL_RETURN(list, _("sh_ignore_add"));
     }
@@ -105,5 +91,4 @@
 #endif
 
-  SH_FREE(reg_expr);
   new->next = list;
 
@@ -111,5 +96,5 @@
 }
 
-int sh_ignore_add_del (const char * addpath)
+int sh_ignore_add_del (char * addpath)
 {
   if ((addpath == NULL) || (addpath[0] != '/'))
@@ -121,5 +106,5 @@
 }
 
-int sh_ignore_add_new (const char * addpath)
+int sh_ignore_add_new (char * addpath)
 {
   if ((addpath == NULL) || (addpath[0] != '/'))
@@ -128,14 +113,4 @@
     }
   sh_new_ign = sh_ignore_add_int (sh_new_ign, addpath);
-  return 0;
-}
-
-int sh_ignore_add_mod (const char * addpath)
-{
-  if ((addpath == NULL) || (addpath[0] != '/'))
-    {
-      return -1;
-    }
-  sh_mod_ign = sh_ignore_add_int (sh_mod_ign, addpath);
   return 0;
 }
@@ -182,10 +157,5 @@
 }
 
-int sh_ignore_chk_mod (const char * chkpath)
-{
-  return (sh_ignore_chk_int(sh_mod_ign, chkpath));
-}
-
-int sh_ignore_clean (void)
+int sh_ignore_clean ()
 {
   struct sh_ignore_list * new;
@@ -219,112 +189,10 @@
     }
 
-  new = sh_mod_ign;
-
-  while (new)
-    {
-      sh_mod_ign = new->next;
-#ifdef HAVE_REGEX_H
-      regfree (&(new->preg));
-#else
-      SH_FREE(new->path);
-#endif
-      SH_FREE(new);
-      new        = sh_mod_ign;
-    }
-
   return 0;
 }
-#endif
 
-#ifdef SH_CUTEST
-#include "CuTest.h"
 
-void Test_ignore_ok (CuTest *tc) {
-#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
 
-  int ret; 
 
-  CuAssertTrue(tc, NULL == sh_del_ign);
-  CuAssertTrue(tc, NULL == sh_new_ign);
-  CuAssertTrue(tc, NULL == sh_mod_ign);
- 
-  ret = sh_ignore_add_del ("/var/log/foo/.*");
-  CuAssertTrue(tc, 0 == ret);
 
-  CuAssertPtrNotNull(tc, sh_del_ign);
-  CuAssertTrue(tc, NULL == sh_new_ign);
-  CuAssertTrue(tc, NULL == sh_mod_ign);
 
-  ret = sh_ignore_chk_del ("/var/log/foo/test");
-  CuAssertTrue(tc, S_TRUE == ret);
-  
-  ret = sh_ignore_chk_del ("/var/log/footest");
-  CuAssertTrue(tc, S_FALSE == ret);
-
-  ret = sh_ignore_chk_del ("/my/var/log/footest");
-  CuAssertTrue(tc, S_FALSE == ret);
-
-  ret = sh_ignore_chk_del ("/my/var/log/foo/test");
-  CuAssertTrue(tc, S_FALSE == ret);
-
-  sh_ignore_clean();
-  CuAssertTrue(tc, NULL == sh_del_ign);
-  CuAssertTrue(tc, NULL == sh_new_ign);
-  CuAssertTrue(tc, NULL == sh_mod_ign);
- 
-  ret = sh_ignore_add_new ("/var/log/foo/.*");
-  CuAssertTrue(tc, 0 == ret);
-
-  CuAssertPtrNotNull(tc, sh_new_ign);
-  CuAssertTrue(tc, NULL == sh_del_ign);
-  CuAssertTrue(tc, NULL == sh_mod_ign);
-
-  ret = sh_ignore_chk_new ("/var/log/foo/test");
-  CuAssertTrue(tc, S_TRUE == ret);
-  
-  ret = sh_ignore_chk_new ("/var/log/footest");
-  CuAssertTrue(tc, S_FALSE == ret);
-
-  ret = sh_ignore_chk_new ("/my/var/log/footest");
-  CuAssertTrue(tc, S_FALSE == ret);
-
-  ret = sh_ignore_chk_new ("/my/var/log/foo/test");
-  CuAssertTrue(tc, S_FALSE == ret);
-
-  sh_ignore_clean();
-  CuAssertTrue(tc, NULL == sh_new_ign);
-  CuAssertTrue(tc, NULL == sh_del_ign);
-  CuAssertTrue(tc, NULL == sh_mod_ign);
-
-  ret = sh_ignore_add_mod ("/var/log/foo/.*");
-  CuAssertTrue(tc, 0 == ret);
-
-  CuAssertPtrNotNull(tc, sh_mod_ign);
-  CuAssertTrue(tc, NULL == sh_del_ign);
-  CuAssertTrue(tc, NULL == sh_new_ign);
-
-  ret = sh_ignore_chk_mod ("/var/log/foo/test");
-  CuAssertTrue(tc, S_TRUE == ret);
-  
-  ret = sh_ignore_chk_mod ("/var/log/footest");
-  CuAssertTrue(tc, S_FALSE == ret);
-
-  ret = sh_ignore_chk_mod ("/my/var/log/footest");
-  CuAssertTrue(tc, S_FALSE == ret);
-
-  ret = sh_ignore_chk_mod ("/my/var/log/foo/test");
-  CuAssertTrue(tc, S_FALSE == ret);
-
-  sh_ignore_clean();
-  CuAssertTrue(tc, NULL == sh_new_ign);
-  CuAssertTrue(tc, NULL == sh_del_ign);
-  CuAssertTrue(tc, NULL == sh_mod_ign);
-
-#else
-  (void) tc; /* fix compiler warning */
-#endif
-  return;
-}
-/* #ifdef SH_CUTEST */
-#endif
-
Index: trunk/src/sh_inotify.c
===================================================================
--- trunk/src/sh_inotify.c	(revision 591)
+++ 	(revision )
@@ -1,1052 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 2009 Rainer Wichmann                                      */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-#include "config_xor.h"
-
-#if defined(HAVE_SYS_INOTIFY_H)
-
-#if defined(GCC_VERSION_MAJOR) && !defined(__clang__)
-#if (GCC_VERSION_MAJOR > 4) || ((GCC_VERSION_MAJOR == 4) && (GCC_VERSION_MINOR > 8))
-#pragma GCC diagnostic ignored "-Wclobbered"
-#endif
-#endif
-
-#undef  FIL__
-#define FIL__  _("sh_inotify.c")
-
-/* printf */
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/inotify.h>
-#include <errno.h>
-#include <unistd.h>
-#include <fcntl.h>
-
-#include "samhain.h"
-#include "sh_pthread.h"
-#include "sh_calls.h"
-#include "sh_inotify.h"
-#include "sh_mem.h"
-#include "sh_utils.h"
-#include "slib.h"
-
-/**************************************************
- *
- * Make the inotify fd thread-specific by 
- * encapsulating it in get/set functions:
- * sh_get_inotify_fd() / sh_set_inotify_fd()
- *
- **************************************************/
-
-#if defined(HAVE_PTHREAD)
-
-SH_MUTEX_STATIC(mutex_list_dormant, PTHREAD_MUTEX_INITIALIZER);
-SH_MUTEX_STATIC(mutex_watches,      PTHREAD_MUTEX_INITIALIZER);
-
-static pthread_key_t  inotify_key;
-static pthread_once_t inotify_key_once = PTHREAD_ONCE_INIT;
-
-static void make_inotify_key()
-{
-    (void) pthread_key_create(&inotify_key, free);
-}
-
-static int sh_get_inotify_fd()
-{
-  void * ptr;
-  int  * fp;
-
-  (void) pthread_once(&inotify_key_once, make_inotify_key);
- 
-  if ((ptr = pthread_getspecific(inotify_key)) == NULL) 
-    {
-      ptr = calloc(1,sizeof(int));
-      if (ptr)
-	{
-	  fp  = (int*) ptr;
-	  *fp = -1;
-	  (void) pthread_setspecific(inotify_key, ptr);
-	}
-      else
-	{
-	  return -1;
-	}
-    }
-  else 
-    {
-      fp  = (int*) ptr;
-    }
-  return *fp;
-}
-
-static void sh_set_inotify_fd(int fd)
-{
-  int  * fp;
-
-  fp = (int*) pthread_getspecific(inotify_key);
-  if (fp)
-    *fp = fd;
-  return;
-}
-
-/* !defined(HAVE_PTHREAD) */
-#else
-
-static int sh_inotify_fd = -1;
-
-static inline int sh_get_inotify_fd()
-{
-  return sh_inotify_fd;
-}
-
-static inline void sh_set_inotify_fd(int fd)
-{
-  sh_inotify_fd = fd;
-}
-
-#endif
-
-/*--- nothing thread-related below this point --- */
-
-#include "zAVLTree.h"
-
-typedef struct 
-{
-  int    watch;
-  short  flag;
-  short  type;
-  int    class;
-  int    rdepth;
-  unsigned long check_flags;
-  char * file;
-} sh_watch;
-
-/**************************************************
- *
- * Get inotify fd, initialize inotify if necessary
- *
- **************************************************/
-#define SH_INOTIFY_FAILED -2
-
-static int sh_inotify_getfd()
-{
-  int ifd = sh_get_inotify_fd();
-
-  if (ifd >= 0)
-    {
-      return ifd;
-    }
-
-  else if (ifd == SH_INOTIFY_FAILED)
-    {
-      return -1;
-    }
-
-  else /* if (ifd == -1) */
-    {
-#if defined(HAVE_INOTIFY_INIT1)
-      ifd = inotify_init1(IN_CLOEXEC);
-#else
-      ifd = inotify_init();
-      if (ifd >= 0)
-	{
-	  long sflags;
-
-	  sflags = retry_fcntl(FIL__, __LINE__, ifd, F_GETFD, 0);
-	  retry_fcntl(FIL__, __LINE__, ifd, F_SETFD, sflags|FD_CLOEXEC);
-	}
-#endif
-
-      if (ifd < 0)
-	{
-	  sh_set_inotify_fd(SH_INOTIFY_FAILED);
-	  return -1;
-	}
-
-      sh_set_inotify_fd(ifd);
-      return ifd;
-    }
-}
-
-/**************************************************
- *
- * Public function:
- *  int sh_inotify_wait_for_change(char * filename,
- *                                 int watch,
- *                                 int * errnum,
- *                                 int   waitsec);
- * Returns: watch, if nonnegative
- *          -1 on error or reopen required
- *             (check errnum != 0)
- *
- * Caller needs to keep track of watch descriptor
- *
- **************************************************/
-
-#define SH_INOTIFY_REOPEN 0
-#define SH_INOTIFY_MODIFY 1
-
-void sh_inotify_init(sh_watches * watches)
-{
-  SH_MUTEX_LOCK_UNSAFE(mutex_watches);
-  watches->list_of_watches = NULL;
-  watches->count           = 0;
-  watches->max_count       = 0;
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_watches);
-
-  SH_MUTEX_LOCK_UNSAFE(mutex_list_dormant);
-  watches->dormant_watches = NULL;
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_list_dormant);
-
-  return;
-}
-
-ssize_t sh_inotify_read(char * buffer, size_t count)
-{
-  ssize_t len = -1;
-  int     ifd = sh_inotify_getfd();
-
-  do {
-    len = read (ifd, buffer, count);
-  } while (len < 0 && (errno == EINTR || errno == EAGAIN));
-
-  return len;
-}
-
-ssize_t sh_inotify_read_timeout(char * buffer, size_t count, int timeout)
-{
-  ssize_t len;
-  int     ifd = sh_inotify_getfd();
-
-  len = sl_read_timeout_fd_once (ifd, buffer, count, timeout, S_FALSE);
-
-  return len;
-}
-
-
-static void sh_inotify_free_watch(void * item)
-{
-  sh_watch * this = (sh_watch *) item;
-
-  if (this->file)
-    SH_FREE(this->file);
-  SH_FREE(this);
-  return;
-}
-
-static sh_watch * sh_inotify_create_watch(const char * file, 
-					  int nwatch, int flag)
-{
-  sh_watch * this = SH_ALLOC(sizeof(sh_watch));
-
-  this->file  = sh_util_strdup_track(file, __FILE__, __LINE__);
-  this->watch = nwatch;
-  this->flag  = flag;
-  return this;
-}
-
-/********** List Handling ******************/
-
-struct sh_inotify_litem
-{
-  sh_watch * watch;
-  struct sh_inotify_litem * next;
-};
-
-static void sh_inotify_listitem_destroy(struct sh_inotify_litem * this)
-{
-  if (this)
-    SH_FREE(this);
-  return;
-}
-
-/* No Mutex in the list cursor functions, must be in the caller
- * function...
- */
-typedef struct {
-  struct sh_inotify_litem *prenode;
-  struct sh_inotify_litem *curnode;
-} sh_inotify_listCursor;
-
-static sh_watch * sh_inotify_list_first(sh_inotify_listCursor * listcursor, 
-					sh_watches * watches)
-{
-  listcursor->prenode = watches->dormant_watches;
-  listcursor->curnode = watches->dormant_watches;
-
-  if (listcursor->curnode)
-    return listcursor->curnode->watch;
-  return NULL;
-}
-
-static sh_watch * sh_inotify_list_next(sh_inotify_listCursor * listcursor, 
-				       sh_watches * watches)
-{
-  (void) watches;
-
-  listcursor->prenode = listcursor->curnode;
-
-  if (listcursor->curnode)
-    {
-      listcursor->curnode = listcursor->curnode->next;
-      if (listcursor->curnode)
-	return listcursor->curnode->watch;
-      else
-	return NULL;
-    }
-
-  return NULL;
-}
-
-static sh_watch * sh_inotify_list_del_cur(sh_inotify_listCursor * listcursor, 
-					  sh_watches * watches)
-{
-  sh_watch * ret = NULL;
-
-  if (listcursor->curnode)
-    {
-      struct sh_inotify_litem * this = listcursor->curnode;
-
-      if (listcursor->prenode == this)
-	{
-	  watches->dormant_watches = this->next;
-
-	  listcursor->prenode = watches->dormant_watches;
-	  listcursor->curnode = watches->dormant_watches;
-	}
-      else
-	{
-	  listcursor->prenode->next = this->next;
-	  listcursor->curnode       = this->next;
-	}
-      if (listcursor->curnode)
-	ret = listcursor->curnode->watch;
-      else
-	ret = NULL;
-      sh_inotify_listitem_destroy(this);
-    }
-  return ret;
-}
-
-static int sh_inotify_add_dormant(sh_watches * watches, sh_watch * item)
-{
-  struct sh_inotify_litem * this;
-
-  SH_MUTEX_LOCK(mutex_list_dormant);
-  this = SH_ALLOC(sizeof(struct sh_inotify_litem));
-
-  this->watch = item;
-  this->next  = (struct sh_inotify_litem *) watches->dormant_watches;
-  
-  watches->dormant_watches = this;
-  SH_MUTEX_UNLOCK(mutex_list_dormant);
-  return 0;
-}
-
-static void * sh_dummy_popret = NULL;
-
-char * sh_inotify_pop_dormant(sh_watches * watches, 
-			      int * class, unsigned long * check_flags, 
-			      int * type, int * rdepth)
-{
-  char * popret = NULL;
-  struct sh_inotify_litem * this;
-
-  /* Take the address to keep gcc from putting it into a register. 
-   * Avoids the 'clobbered by longjmp' warning. 
-   */
-  sh_dummy_popret = (void *) &popret;
-
-  SH_MUTEX_LOCK(mutex_list_dormant);
-
-  this = (struct sh_inotify_litem *) watches->dormant_watches;
-
-  if (this)
-    {
-      *class  = this->watch->class;
-      *type   = this->watch->type;
-      *rdepth = this->watch->rdepth;
-      *check_flags = this->watch->check_flags;
-      popret  = sh_util_strdup_track(this->watch->file, __FILE__, __LINE__);
-
-      watches->dormant_watches = this->next;
-
-      sh_inotify_free_watch(this->watch);
-      SH_FREE(this);
-    }
-  SH_MUTEX_UNLOCK(mutex_list_dormant);
-
-  sh_dummy_popret = NULL;
-  return popret;
-}
-
-void sh_inotify_purge_dormant(sh_watches * watches)
-{
-  struct sh_inotify_litem * this;
-
-  SH_MUTEX_LOCK(mutex_list_dormant);
-  this = (struct sh_inotify_litem *) watches->dormant_watches;
-
-  watches->dormant_watches = NULL;
-
-  while (this)
-    {
-      struct sh_inotify_litem * cur = this;
-      
-      this = this->next;
-
-      sh_inotify_free_watch(cur->watch);
-      SH_FREE(cur);
-    }
-  SH_MUTEX_UNLOCK(mutex_list_dormant);
-  return;
-}
-
-/********** End List Handling **************/
-
-static zAVLKey sh_inotify_getkey(void const *item)
-{
-  return (&((const sh_watch *)item)->watch);
-}
-
-void sh_inotify_close()
-{
-  int     ifd = sh_inotify_getfd();
-
-  if (ifd >= 0)
-    close(ifd);
-  sh_set_inotify_fd(-1);
-
-  return;
-}
-
-  
-/* This function removes all watches from the list,
- * and closes the inode file descriptor in this thread.
- */
-void sh_inotify_remove(sh_watches * watches)
-{
-  zAVLTree   * all_watches;
-
-  SH_MUTEX_LOCK(mutex_watches);
-  all_watches = (zAVLTree *)(watches->list_of_watches);
-
-  if (all_watches)
-    zAVLFreeTree(all_watches, sh_inotify_free_watch);
-
-  watches->list_of_watches = NULL;
-  watches->count = 0;
-  SH_MUTEX_UNLOCK(mutex_watches);
-
-  sh_inotify_close();
-  return;
-}
-
-static int index_watched_file(char * filename, sh_watches * watches)
-{
-  sh_watch   * item;
-  zAVLCursor   avlcursor;
-  zAVLTree   * all_watches = (zAVLTree *)(watches->list_of_watches);
-
-  if (all_watches)
-    {
-      for (item = (sh_watch *) zAVLFirst(&avlcursor, all_watches); item;
-	   item = (sh_watch *) zAVLNext(&avlcursor))
-	{
-	  if (item->file)
-	    {
-	      if (0 == strcmp(filename, item->file))
-		return item->watch;
-	    }
-	}
-    }
-  return -1;
-}
-
-#if !defined(IN_DONT_FOLLOW)
-#define IN_DONT_FOLLOW 0
-#endif
-
-#define SH_INOTIFY_FILEFLAGS \
-  (IN_ATTRIB|IN_MODIFY|IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT|IN_DONT_FOLLOW)
-#define SH_INOTIFY_DIRFLAGS \
-  (SH_INOTIFY_FILEFLAGS|IN_DELETE|IN_CREATE|IN_MOVED_FROM|IN_MOVED_TO)
-
-#define SH_INOTIFY_FLAGS (SH_INOTIFY_FILEFLAGS|SH_INOTIFY_DIRFLAGS)
-
-
-/* Create an item and put it on the 'dormant' list for later watch creation 
- */
-int sh_inotify_add_watch_later(const char * filename, sh_watches * watches, 
-			       int * errnum,
-			       int class, unsigned long check_flags, int type, 
-			       int rdepth)
-{
-  sh_watch   * item;
-
-  item = sh_inotify_create_watch(filename, -1, /* flag */ 0);
-
-  item->class      = class;
-  item->type       = (short) type;
-  item->rdepth     = (short) rdepth;
-  item->check_flags = check_flags;
-
-  sh_inotify_add_dormant(watches, item);
-  if (errnum)
-    *errnum = 0;
-
-  return 0;
-}
-	  
-int sh_inotify_rm_watch (sh_watches * watches, sh_watches * save, int wd)
-{
-  int ifd = sh_get_inotify_fd();
-
-  if (watches)
-    {
-      sh_watch   * item;
-  
-      SH_MUTEX_LOCK(mutex_watches);
-      item = zAVLSearch(watches->list_of_watches, &wd);
-      
-      if (item)
-	{
-	  zAVLDelete(watches->list_of_watches, &wd);
-	  if (save) /* optionally save the item */
-	    {
-	      item->watch = -1;
-	      sh_inotify_add_dormant(save, item);
-	    }
-	  else
-	    {
-	      sh_inotify_free_watch(item);
-	    }
-	}
-      SH_MUTEX_UNLOCK(mutex_watches);
-    }
-  return inotify_rm_watch(ifd, wd);
-}
-
-#if (defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)) 
-static void * sh_dummy_litem;
-
-int sh_inotify_recheck_watches (sh_watches * watches, sh_watches * save)
-{
-  sh_watch   * litem;
-  sh_inotify_listCursor listcursor;
-  int ifd = sh_get_inotify_fd();
-
-  extern void sh_fInotify_report_add(char * path, 
-				     int class, unsigned long check_flags);
-
-  sh_dummy_litem = (void*) &litem;
-
-  /* -- Check dormant watches for reopening.
-   */
-  SH_MUTEX_LOCK(mutex_list_dormant);
-  
-  litem = sh_inotify_list_first(&listcursor, save);
-
-  while (litem)
-    {
-    have_next:
-
-      /* sh_inotify_list_del_cur may return NULL */
-      if (litem && litem->file && litem->watch == -1)
-	{
-	  litem->watch = inotify_add_watch (ifd, litem->file, 
-					    SH_INOTIFY_FLAGS);
-	  
-	  if (litem->watch >= 0)
-	    {
-	      SH_MUTEX_LOCK(mutex_watches);
-	      if (watches->list_of_watches)
-		zAVLInsert(watches->list_of_watches, litem);
-	      SH_MUTEX_UNLOCK(mutex_watches);
-
-	      sh_fInotify_report_add(litem->file, litem->class, litem->check_flags);
-
-	      litem = sh_inotify_list_del_cur(&listcursor, save);
-	      
-	      goto have_next;
-	    }
-	}
-      litem = sh_inotify_list_next(&listcursor, save);
-    }
-  SH_MUTEX_UNLOCK(mutex_list_dormant);
-  return 0;
-}
-#endif
-
-/* This function is idempotent; it will add the watch only once 
- */
-int sh_inotify_add_watch(char * filename, sh_watches * watches, int * errnum,
-			 int class, unsigned long check_flags, int type, int rdepth)
-{
-  volatile int retval = 0;
-
-  SH_MUTEX_LOCK(mutex_watches);
-
-  *errnum = 0;
-
-  if (filename)
-    {
-      int nwatch;
-      sh_watch   * item;
-      int index = index_watched_file(filename, watches);
-      
-      if (index < 0)
-	{
-	  int     ifd = sh_inotify_getfd();
-
-	  /*************************************
-
-	  if (watches->count == SH_INOTIFY_MAX)
-	    {
-#ifdef EMFILE
-	      *errnum = EMFILE;
-#else
-	      *errnum = 24;
-#endif
-	      return -1;
-	    }
-	  **************************************/
-
-	  nwatch = inotify_add_watch (ifd, filename, 
-				      SH_INOTIFY_FLAGS);
-	  if (nwatch < 0)
-	    {
-	      *errnum = errno;
-	      retval = -1;
-	      goto retpoint;
-	    }
-
-	  item = sh_inotify_create_watch(filename, nwatch, /* flag */ 0);
-
-	  item->class      = class;
-	  item->type       = type;
-	  item->rdepth     = rdepth;
-	  item->check_flags = check_flags;
-	  
-	  if (NULL == watches->list_of_watches)
-	    watches->list_of_watches = zAVLAllocTree (sh_inotify_getkey, 
-						      zAVL_KEY_INT);
- 
-	  if (watches->list_of_watches)
-	    {
-	      *errnum =  zAVLInsert((zAVLTree *)(watches->list_of_watches), 
-				    item);
-
-	      if (*errnum != 0)
-		{
-		  /* zAVLInsert returns -1 on malloc() error and 3 if
-		   * the node already exists. 
-		   */
-		  *errnum = (*errnum == -1) ? ENOMEM : EEXIST;
-		  sh_inotify_free_watch(item);
-		  retval = -1;
-		  goto retpoint;
-		}
-	    }
-	  else
-	    {
-	      *errnum = ENOMEM;
-	      sh_inotify_free_watch(item);
-	      retval = -1;
-	      goto retpoint;
-	    }
-
-	  ++(watches->count);
-	}
-      else if (type == SH_INOTIFY_DIR) /* watch exists */
-	{
-	  /* This covers the case that a directory has been added,
-	   * but is watched as file at first because it is also
-	   * specified as file in the config.
-	   */
-	  item = zAVLSearch(watches->list_of_watches, &index);
-
-	  if (item && item->type == SH_INOTIFY_FILE)
-	    {
-	      item->type = SH_INOTIFY_DIR;
-	    }
-	}
-    }
- retpoint:
-  ; /* 'label at end of compound statement' */
-  SH_MUTEX_UNLOCK(mutex_watches);
-  return retval;
-}
-
-static void * sh_dummy_sret = NULL;
-
-char * sh_inotify_search_item(sh_watches * watches, int watch, 
-			      int * class, unsigned long * check_flags, 
-			      int * type, int * rdepth)
-{
-  sh_watch * item;
-  char     * sret = NULL;
-
-  /* Take the address to keep gcc from putting it into a register. 
-   * Avoids the 'clobbered by longjmp' warning. 
-   */
-  sh_dummy_sret = (void *) &sret;
-
-  SH_MUTEX_LOCK(mutex_watches);
-  item = zAVLSearch(watches->list_of_watches, &watch);
-
-  if (item)
-    {
-      *class      = item->class;
-      *check_flags = item->check_flags;
-      *type       = item->type;
-      *rdepth     = item->rdepth;
-      sret = sh_util_strdup_track(item->file, __FILE__, __LINE__);
-    }
-  SH_MUTEX_UNLOCK(mutex_watches);
-  return sret;
-}
-
-static void * sh_dummy_litem = NULL;
-
-int sh_inotify_wait_for_change(char * filename, sh_watches * watches, 
-			       int  * errnum, int waitsec)
-{
-  sh_watch   * litem;
-  sh_watch   * zitem;
-  int          ifd = sh_inotify_getfd();
-
-  /* Take the address to keep gcc from putting it into a register. 
-   * Avoids the 'clobbered by longjmp' warning. 
-   */
-  sh_dummy_litem = (void*) &litem;
-
-  *errnum = 0;
-
- start_it:
-
-  if (ifd >= 0)
-    {
-      volatile ssize_t  i  = 0;
-      ssize_t len = -1;
-      int  flag = 0;
-      char buffer[1024];
-
-      sh_inotify_listCursor listcursor;
-
-      /* -- Add watch if required 
-       */
-      if (filename)
-	{
-	  if (sh_inotify_add_watch(filename, watches, errnum, 
-				   0, 0, SH_INOTIFY_FILE, 0) < 0)
-	    {
-	      retry_msleep(waitsec, 0);
-	      return -1;
-	    }
-	}
-
-      /* -- Check dormant watches for reopening.
-       */
-      SH_MUTEX_LOCK(mutex_list_dormant);
-
-      for (litem = sh_inotify_list_first(&listcursor, watches); litem;
-	   litem = sh_inotify_list_next(&listcursor, watches))
-	{
-	have_next:
-	  /* sh_inotify_list_del_cur may return NULL */
-	  if (litem && litem->file && litem->watch == -1)
-	    {
-	      litem->watch = inotify_add_watch (ifd, litem->file, 
-						SH_INOTIFY_FLAGS);
-
-	      if (litem->watch >= 0)
-		{
-		  SH_MUTEX_LOCK(mutex_watches);
-		  if (watches->list_of_watches)
-		    zAVLInsert(watches->list_of_watches, litem);
-		  SH_MUTEX_UNLOCK(mutex_watches);
-		  litem = sh_inotify_list_del_cur(&listcursor, watches);
-		  goto have_next;
-		}
-	    }
-	}
-      SH_MUTEX_UNLOCK(mutex_list_dormant);
-
-
-      /* -- Blocking read on inotify file descriptor
-       */
-      len = sh_inotify_read(buffer, sizeof(buffer));
-
-      if (len > 0)
-	{
-	  struct inotify_event *event;
-
-	  i = 0;
-	  
-	  while (i < len) {
-
-	    event = (struct inotify_event *) &buffer[i];
-
-	    SH_MUTEX_LOCK(mutex_watches);
-	    zitem = zAVLSearch(watches->list_of_watches, &(event->wd));
-
-	    if (zitem)
-	      {
-		if (event->mask & IN_MODIFY)
-		  {
-		    zitem->flag |= SH_INOTIFY_MODIFY;
-		    flag |= SH_INOTIFY_MODIFY;
-		  }
-		else if (event->mask & IN_DELETE_SELF || 
-			 event->mask & IN_UNMOUNT     || 
-			 event->mask & IN_MOVE_SELF   )
-		  {
-		    zitem->flag |= SH_INOTIFY_REOPEN;
-		    (void) inotify_rm_watch(ifd, zitem->watch);
-		    zAVLDelete(watches->list_of_watches, zitem);
-		    sh_inotify_add_dormant(watches, zitem);
-		    zitem->watch    = -1;
-		    flag |= SH_INOTIFY_REOPEN;
-		  }
-	      }
-	    SH_MUTEX_UNLOCK(mutex_watches);
-	    
-	    i += sizeof (struct inotify_event) + event->len;
-	  }
-	}
-      else if (len == -1)
-	{
-	  *errnum = errno;
-	  retry_msleep(waitsec, 0);
-
-	  return -1;
-	}
-
-      if (flag & SH_INOTIFY_REOPEN)
-	{
-	  if (flag & SH_INOTIFY_MODIFY)
-	    return 0;
-	  else
-	    goto start_it;
-	}
-
-      return 0;
-    }
-
-  /* Inotify not working, sleep
-   */
-  retry_msleep(waitsec, 0);
-
-  *errnum = 0;
-  return -1;
-}
-
-
-/* !defined(HAVE_SYS_INOTIFY_H) */
-#else
-
-#include "sh_calls.h"
-#include "sh_inotify.h"
-
-void sh_inotify_remove(sh_watches * watches)
-{
-  (void) watches;
-  return;
-}
-
-int sh_inotify_wait_for_change(char * filename, sh_watches * watches,
-			       int *  errnum, int waitsec)
-{
-  (void) filename;
-  (void) watches;
-
-  /* Inotify not working, sleep for waitsec seconds
-   */
-  retry_msleep(waitsec, 0);
-
-  if (errnum)
-    *errnum = 0;
-  return -1;
-}
-
-int sh_inotify_add_watch(char * filename, sh_watches * watches, int  * errnum,
-			 int class, unsigned long check_flags, int type, int rdepth)
-{
-  (void) filename;
-  (void) watches;
-  (void) class;
-  (void) check_flags;
-  (void) type;
-  (void) rdepth;
-
-  if (errnum)
-    *errnum = 0;
-  return 0;
-}
-
-int sh_inotify_add_watch_later(const char * filename, sh_watches * watches, 
-			       int  * errnum,
-			       int class, unsigned long check_flags, int type, int rdepth)
-{
-  (void) filename;
-  (void) watches;
-  (void) class;
-  (void) check_flags;
-  (void) type;
-  (void) rdepth;
-
-  if (errnum)
-    *errnum = 0;
-  return 0;
-}
-
-#endif
-
-#ifdef SH_CUTEST
-#include "CuTest.h"
-void Test_inotify(CuTest *tc) {
-#if defined(HAVE_SYS_INOTIFY_H) && (defined(SH_WITH_CLIENT) || defined(SH_STANDALONE))
-
-  int          ret;
-  sh_watches   twatch = SH_INOTIFY_INITIALIZER;
-  sh_watch   * litem;
-  sh_inotify_listCursor listcursor;
-  char * p;
-  int class;
-  int type;
-  int rdepth;
-  unsigned long check_flags;
-  int           nrun = 0;
-
-  sh_watch aw1 = { -1, 0, 0, 1, 99, 1, "a1" };
-  sh_watch aw2 = { -1, 0, 0, 2, 99, 1, "a2" };
-  sh_watch aw3 = {  2, 0, 0, 3, 99, 1, "a3" };
-  sh_watch aw4 = { -1, 0, 0, 4, 99, 1, "a4" };
-  sh_watch aw5 = {  5, 0, 0, 5, 99, 1, "a5" };
-
-  do {
-
-    int          count = 0;
-
-    sh_watch * w1 = SH_ALLOC(sizeof(sh_watch));
-    sh_watch * w2 = SH_ALLOC(sizeof(sh_watch));
-    sh_watch * w3 = SH_ALLOC(sizeof(sh_watch));
-    sh_watch * w4 = SH_ALLOC(sizeof(sh_watch));
-    sh_watch * w5 = SH_ALLOC(sizeof(sh_watch));
-
-    memcpy(w1, &aw1, sizeof(sh_watch));
-    w1->file = sh_util_strdup(aw1.file);
-    memcpy(w2, &aw2, sizeof(sh_watch));
-    w2->file = sh_util_strdup(aw2.file);
-    memcpy(w3, &aw3, sizeof(sh_watch));
-    w3->file = sh_util_strdup(aw3.file);
-    memcpy(w4, &aw4, sizeof(sh_watch));
-    w4->file = sh_util_strdup(aw4.file);
-    memcpy(w5, &aw5, sizeof(sh_watch));
-    w5->file = sh_util_strdup(aw5.file);
-    
-    ret = sh_inotify_add_dormant(&twatch, w1);
-    CuAssertIntEquals(tc, ret, 0);
-    ret = sh_inotify_add_dormant(&twatch, w2);
-    CuAssertIntEquals(tc, ret, 0);
-    ret = sh_inotify_add_dormant(&twatch, w3);
-    CuAssertIntEquals(tc, ret, 0);
-    ret = sh_inotify_add_dormant(&twatch, w4);
-    CuAssertIntEquals(tc, ret, 0);
-    ret = sh_inotify_add_dormant(&twatch, w5);
-    CuAssertIntEquals(tc, ret, 0);
-    
-    /* -- Check dormant watches for reopening.
-     */
-    for (litem = sh_inotify_list_first(&listcursor, &twatch); litem;
-	 litem = sh_inotify_list_next(&listcursor, &twatch))
-      {
-      have_next:
-	
-	/* sh_inotify_list_del_cur may return NULL */
-	if (litem)
-	  {
-	    ++count;
-	    
-	    if (litem->file && litem->watch == -1)
-	      {
-		
-		switch (litem->class)
-		  {
-		  case 1:
-		    CuAssertStrEquals(tc, litem->file, "a1");
-		    break;
-		  case 2:
-		    CuAssertStrEquals(tc, litem->file, "a2");
-		    break;
-		  case 3:
-		    CuAssertStrEquals(tc, litem->file, "deadbeef");
-		    break;
-		  case 4:
-		    CuAssertStrEquals(tc, litem->file, "a4");
-		    break;
-		  case 5:
-		    CuAssertStrEquals(tc, litem->file, "deadbeef");
-		    break;
-		  default:
-		    CuAssertStrEquals(tc, litem->file, "deadbeef");
-		  }
-		litem = sh_inotify_list_del_cur(&listcursor, &twatch);
-		goto have_next;
-	      }
-	    switch (litem->class)
-	      {
-	      case 3:
-		CuAssertStrEquals(tc, litem->file, "a3");
-		break;
-	      case 5:
-		CuAssertStrEquals(tc, litem->file, "a5");
-		break;
-	      default:
-		CuAssertStrEquals(tc, litem->file, "foobar");
-	      }      
-	  }
-      }
-    
-    CuAssertIntEquals(tc, count, 5);
-    
-    p = sh_inotify_pop_dormant(&twatch, &class, &check_flags, &type, &rdepth);
-    CuAssertStrEquals(tc, p, "a5");
-    
-    p = sh_inotify_pop_dormant(&twatch, &class, &check_flags, &type, &rdepth);
-    CuAssertStrEquals(tc, p, "a3");
-    CuAssertIntEquals(tc, class, 3);
-    
-    p = sh_inotify_pop_dormant(&twatch, &class, &check_flags, &type, &rdepth);
-    CuAssertTrue(tc, NULL == p);
-    CuAssertTrue(tc, NULL == twatch.dormant_watches);
-
-    ++nrun;
-
-  } while (nrun < 100);
-
-#else
-  (void) tc;
-#endif
-
-  return;
-}
-#endif
Index: trunk/src/sh_ipvx.c
===================================================================
--- trunk/src/sh_ipvx.c	(revision 591)
+++ 	(revision )
@@ -1,603 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 2010 Rainer Wichmann                                      */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-#include "config_xor.h"
-
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netdb.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <string.h>
-#include <ctype.h>
-
-#undef  FIL__
-#define FIL__  _("sh_ipvx.c")
-
-#include "samhain.h"
-#define SH_NEED_GETHOSTBYXXX
-#include "sh_static.h"
-#include "sh_pthread.h"
-#include "sh_utils.h"
-#include "sh_ipvx.h"
-
-static int sh_ipvx_is_ipv4 (const char * addr)
-{
-  int j;
-  int count = 0;
-  int len = sl_strlen(addr);
-  unsigned int a, b, c, d;
-  
-  if (len > 15) /* 3*4 + 3 dots */
-    return (1 == 0);
-
-  for (j = 0; j < len; ++j) {
-    if ( (addr[j] < '0' || addr[j] > '9') && addr[j] != '.')
-      return (1 == 0);
-    if (addr[j] == '.') ++count;
-  }
-
-  if (count != 3)
-    return (1 == 0);
-
-  if (sscanf(addr, "%3u.%3u.%3u.%3u", &a, &b, &c, &d) != 4)
-    return( 1 == 0 );
-
-  if ((a|b|c|d) > 255) 
-    return( 1 == 0 );
-
-  return (1 == 1);
-}
-
-#if defined(USE_IPVX)
-static int sh_ipvx_is_ipv6 (const char * addr)
-{
-  int j, k = 0;
-  char c;
-  int len = sl_strlen(addr);
-  
-  for (j = 0; j < len; ++j) {
-    c = addr[j];
-    if (( c < '0' || c > '9' ) &&
-	( c < 'a' || c > 'f' ) &&
-	( c < 'A' || c > 'F' ) &&
-	( c != ':') && ( c != '.'))
-      return (1 == 0);
-    else if (c == ':')
-      ++k;
-    else if (c == '.' && k < 2)
-      return (1 == 0); /* ::ffff:ipv4 */
-  }
-  if (k < 2 || k > 7)
-    return (1 == 0);
-
-  return (1 == 1);
-}
-#endif
-
-
-int sh_ipvx_is_numeric (const char * addr)
-{
-#if defined(USE_IPVX)
-  if (!sh_ipvx_is_ipv4(addr))
-    return sh_ipvx_is_ipv6(addr);
-  else
-    return (1 == 1);
-#else
-  return sh_ipvx_is_ipv4(addr);
-#endif
-}
-
-int sh_ipvx_isany (struct sh_sockaddr * a)
-{
-#if defined(HOST_IS_CYGWIN)
-  /* 
-   * Cygwin implementation gives 'missing braces around initializer'
-   * warning, thus replace it with correct version.
-   */
-#undef IN6ADDR_ANY_INIT
-#define IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
-#endif
-
-#if defined(USE_IPVX)
-  struct in6_addr anyaddr = IN6ADDR_ANY_INIT; 
-#endif
-
-  switch (a->ss_family)
-    {
-    case AF_INET:
-      if ((a->sin).sin_addr.s_addr == INADDR_ANY)
-	return 1;
-      break;
-#if defined(USE_IPVX)
-    case AF_INET6:
-      if (0 == memcmp(&((a->sin6).sin6_addr.s6_addr), &anyaddr, 16))
-	return 1;
-      break;
-#endif
-    }
-  return 0;
-}
-
-int sh_ipvx_cmp (struct sh_sockaddr * a, struct sh_sockaddr * b)
-{
-  if (a->ss_family != b->ss_family)
-    return 1;
-  
-  switch (a->ss_family)
-    {
-    case AF_INET:
-      return memcmp(&((a->sin).sin_addr.s_addr), &((b->sin).sin_addr.s_addr), 4);
-      break;
-#if defined(USE_IPVX)
-    case AF_INET6:
-      return memcmp(&((a->sin6).sin6_addr.s6_addr), &((b->sin6).sin6_addr.s6_addr), 16);
-      break;
-#endif
-    }
-  return 1;
-}
-
-int sh_ipvx_ntoa (char * name, size_t name_size, struct sh_sockaddr * ss)
-{
-#if defined(USE_IPVX)
-  int len = (ss->ss_family == AF_INET) ? 
-    sizeof(struct sockaddr_in) :
-    sizeof(struct sockaddr_in6);
-
-  int ret = getnameinfo(sh_ipvx_sockaddr_cast(ss), len,
-			name, name_size, NULL, 0, NI_NUMERICHOST);
-
-  if (ret != 0 && name_size > 0)
-    {
-      name[name_size-1] = '\0';
-
-      if (!sh_ipvx_is_numeric(name))
-	{
-	  if (name_size > 7) {
-	    name[0] = '0'; name[1] = '.'; name[2] = '0'; name[3] = '.'; 
-	    name[4] = '0'; name[5] = '.'; name[6] = '0'; name[7] = '\0';
-	  } else {
-	    name[0] = '\0';
-	  }
-	}
-    } 
-  return ret;
-#else
-  char * p = inet_ntoa((ss->sin).sin_addr);
-  sl_strlcpy(name, p, name_size);
-  return 0;
-#endif
-}
-
-struct sockaddr * sh_ipvx_sockaddr_cast (struct sh_sockaddr * ss)
-{
-#if defined(USE_IPVX)
-  if (ss->ss_family == AF_INET6)
-    return (struct sockaddr *) &(ss->sin6);
-#endif
-  return (struct sockaddr *) &(ss->sin);
-}
-
-char * sh_ipvx_print_sockaddr (struct sockaddr * sa, int sa_family)
-{
-  struct sh_sockaddr ss;
-  static char ipbuf[SH_IP_BUF];
-
-  memset(&ss, 0, sizeof(struct sh_sockaddr));
-  sh_ipvx_save(&ss, sa_family, sa);
-  sh_ipvx_ntoa (ipbuf, sizeof(ipbuf), &ss);
-  return ipbuf;
-}
-
-void sh_ipvx_save(struct sh_sockaddr * ss, int sa_family, struct sockaddr * sa)
-{
-  switch (sa_family)
-    {
-    case AF_INET:
-      ss->ss_family = AF_INET;
-      memcpy(&(ss->sin), (struct sockaddr_in*) sa, sizeof(struct sockaddr_in));
-      break;
-#if defined(USE_IPVX)
-    case AF_INET6:
-      ss->ss_family = AF_INET6;
-      memcpy(&(ss->sin6), (struct sockaddr_in6*) sa, sizeof(struct sockaddr_in6));
-      break;
-#endif
-    default:
-      break;
-    }
-  return;
-}
-
-int sh_ipvx_set_port(struct sh_sockaddr * ss, int port)
-{
-#if defined(USE_IPVX)
-
-  switch (ss->ss_family)
-    {
-    case AF_INET:
-      (ss->sin).sin_family = AF_INET;
-      (ss->sin).sin_port = htons (port);
-      break;
-    case AF_INET6:
-      (ss->sin6).sin6_family = AF_INET6;
-      (ss->sin6).sin6_port = htons (port);
-      break;
-    }
-  return 0;
-#else
-  (ss->sin).sin_family = AF_INET;
-  (ss->sin).sin_port = htons (port);
-  return 0;
-#endif
-}
-
-int sh_ipvx_get_port(struct sh_sockaddr * sa)
-{
-  int port = 0;
-#if defined(USE_IPVX)
-
-  switch (sa->ss_family)
-    {
-    case AF_INET:
-      port = ntohs((sa->sin).sin_port);
-      break;
-    case AF_INET6:
-      port = ntohs((sa->sin6).sin6_port);
-      break;
-    }
-#else
-  port = ntohs((sa->sin).sin_port);
-#endif
-  return port;
-}
-
-int sh_ipvx_aton (const char * name, struct sh_sockaddr * ss)
-{
-#if defined(USE_IPVX)
-  int             ret;
-  struct addrinfo hints;
-  struct addrinfo *res;
-
-  memset(&hints, 0, sizeof(hints));
-  hints.ai_family = PF_UNSPEC;
-  hints.ai_flags  = AI_NUMERICHOST;
-  ret = getaddrinfo(name, NULL, &hints, &res);
-
-  if (ret)
-    return 0;
-
-  memset(ss, 0, sizeof(struct sh_sockaddr));
-  switch(res->ai_family)
-    {
-    case AF_INET:
-      memcpy(&(ss->sin), res->ai_addr, sizeof(struct sockaddr_in));
-      ss->ss_family = AF_INET;
-      break;
-    case AF_INET6:
-      memcpy(&(ss->sin6), res->ai_addr, sizeof(struct sockaddr_in6));
-      ss->ss_family = AF_INET6;
-      break;
-    default:
-      return 0;
-      break;
-    }
-  freeaddrinfo(res);
-  return 1;
-#else
-  int ret = inet_aton(name, &((ss->sin).sin_addr));
-  ss->ss_family = AF_INET;
-  return ret;
-#endif
-}
-
-#if !defined(USE_IPVX)
-static const char * sh_ipvx_h_name (struct hostent * host_entry)
-{
-  char ** p;
-  if (strchr(host_entry->h_name, '.')) {
-    return host_entry->h_name;
-  } else {
-    for (p = host_entry->h_aliases; (p && *p); ++p) {
-      if (strchr(*p, '.'))
-	return *p;
-    }
-  }
-  return host_entry->h_name;
-}
-#endif
-
-static char * sh_tolower (char * s)
-{
-  char * ret = s;
-  if (s)
-    {
-      for (; *s; ++s)
-	{ 
-	  *s = tolower((unsigned char) *s);
-	}
-    }
-  return ret;
-}
-
-void * sh_dummy_341_out;
-
-char * sh_ipvx_canonical(const char * hostname, char * numeric, size_t nlen)
-{
-  volatile int    flag = 0;
-  char            *out = NULL;
-#if defined(USE_IPVX)
-  struct addrinfo hints;
-  struct addrinfo *res;
-  struct sockaddr *sa;
-  int             salen;
-  int             err;
-  struct sh_sockaddr  ss;
-  const char * host;
-  char hostbuf[SH_BUFSIZE];
-
-  SL_ENTER(_("sh_ipvx_canonical"));
-  
-  numeric[0] = '\0';
-
-  sh_dummy_341_out = (void *) &out;
- 
-  if (sh_ipvx_is_numeric(hostname))
-    {
-      sh_ipvx_aton (hostname, &ss);
-      if (0 == getnameinfo(sh_ipvx_sockaddr_cast(&ss), SH_SS_LEN(ss),
-			   hostbuf, sizeof(hostbuf), NULL, 0, NI_NAMEREQD))
-	host = hostbuf;
-      else
-	host = hostname;
-    }
-  else
-    {
-      host = hostname;
-    }
- 
-  memset(&hints, 0, sizeof(hints));
-  hints.ai_family = PF_UNSPEC;
-#if defined(AI_CANONNAME)
-  hints.ai_flags = AI_CANONNAME;
-#endif 
-
-  err = getaddrinfo(host, NULL, &hints, &res);
-  if (err == 0)
-    {
-      struct addrinfo * res_orig = res;
-
-#if defined(AI_CANONNAME)
-      if (res->ai_canonname && strlen(res->ai_canonname) > 0)
-	{
-	  out = sh_util_strdup(res->ai_canonname);
-	  sh_tolower (out);
-	  if (strchr(out, '.'))
-	    flag = 1;
-	}
-#endif
-
-      sa = res->ai_addr;
-      salen = res->ai_addrlen;
-      getnameinfo(sa, salen,
-		  numeric, nlen, NULL, 0, NI_NUMERICHOST);
-
-      if (!flag)
-	out = SH_ALLOC(SH_PATHBUF);
-
-      while (res && !flag) 
-	{
-	  sa = res->ai_addr;
-	  salen = res->ai_addrlen;
-
-	  getnameinfo(sa, salen,
-		      out, SH_PATHBUF, NULL, 0, 0);
-	  sh_tolower (out);
-	  if (strchr(out, '.'))
-	    flag = 1;
-	  
-	  res = res->ai_next;
-	}
-      
-      freeaddrinfo(res_orig);
-    }
-#else
-  struct hostent     *he;
-  struct sh_sockaddr  ss;
-  volatile int        isNum = 0;
-  struct sockaddr_in *sin;
-
-  SL_ENTER(_("sh_ipvx_canonical"));
-
-  numeric[0] = '\0';
-
-  sh_dummy_341_out = (void *) &out;
-
-  if (sh_ipvx_is_numeric(hostname))
-    {
-      sh_ipvx_aton (hostname, &ss);
-      isNum = 1;
-    }
- 
-
-  SH_MUTEX_LOCK(mutex_resolv);
-
-  if (isNum == 0)
-    {
-      he = sh_gethostbyname(hostname);
-    }
-  else
-    {
-      sin = (struct sockaddr_in *) sh_ipvx_sockaddr_cast(&ss);
-      he = sh_gethostbyaddr(&(sin->sin_addr), sizeof(sin->sin_addr), AF_INET);
-    }
-
-  if (he != NULL)
-    {
-      out = sh_util_strdup(sh_ipvx_h_name(he));
-      sh_tolower (out);
-      sl_strlcpy (numeric, 
-		  inet_ntoa (*(struct in_addr *) he->h_addr), 
-		  nlen);
-      flag = 1;
-    }
-  SH_MUTEX_UNLOCK(mutex_resolv);
-#endif
-
-  if (flag) {
-    SL_RETURN(out, _("sh_ipvx_canonical"));
-  }
-  
-  if (out)
-    SH_FREE(out);
-  if (numeric[0] == '\0')
-    sl_strlcpy (numeric, _("0.0.0.0"), nlen);
-  SL_RETURN(NULL, _("sh_ipvx_canonical"));
-}
-
-char * sh_ipvx_addrtoname(struct sh_sockaddr * ss)
-{
-#if defined(USE_IPVX)
-  char namebuf[SH_BUFSIZE];
-
-  if (getnameinfo(sh_ipvx_sockaddr_cast(ss), SH_SSP_LEN(ss),
-		  namebuf, sizeof(namebuf), NULL, 0, NI_NAMEREQD) != 0)
-    {
-      return NULL;
-    }
-  return sh_util_strdup(namebuf);
-#else
-  struct sockaddr_in *sin;
-  struct hostent *he;
-
-  sin = (struct sockaddr_in *) sh_ipvx_sockaddr_cast(ss);
-
-  he = sh_gethostbyaddr(&(sin->sin_addr), sizeof(sin->sin_addr), AF_INET);
-
-  if (he && he->h_name)
-    {
-      return sh_util_strdup(he->h_name);
-    }
-
-  return NULL;
-#endif
-}
-
-int sh_ipvx_reverse_check_ok (char * peer, int port, struct sh_sockaddr * ss)
-{
-#if defined(USE_IPVX)
-  struct addrinfo *res;
-  struct addrinfo hints;
-  char            sport[32];
-  struct addrinfo *p;
-
-  SL_ENTER(_("sh_ipvx_reverse_check_ok"));
-
-  sl_snprintf(sport, sizeof(sport), "%d", port);
-
-  memset(&hints, 0, sizeof(hints));
-  hints.ai_socktype = SOCK_STREAM;
-  hints.ai_flags = AI_ADDRCONFIG;
-
-  if (getaddrinfo(peer, sport, &hints, &res) != 0)
-    {
-      SL_RETURN((0), _("sh_ipvx_reverse_check_ok")); 
-    }
-  
-  p = res;
-  while (p != NULL)
-    {
-      if (ss->ss_family == p->ai_family)
-	{
-	  struct sh_sockaddr pp;
-
-	  char dst1[SH_IP_BUF];
-	  char dst2[SH_IP_BUF];
-
-	  sh_ipvx_save(&pp, p->ai_family, p->ai_addr);
-
-	  sh_ipvx_ntoa (dst1, sizeof(dst1), &pp);
-	  sh_ipvx_ntoa (dst2, sizeof(dst2),  ss);
-
-	  if (0 == sl_strcmp(dst1, dst2))
-	    {
-	      SL_RETURN((1), _("sh_ipvx_reverse_check_ok")); 
-	    }
-	}
-      p = p->ai_next;
-    }
-  freeaddrinfo(res);
-#else
-  struct hostent * he;
-  char          ** p;
-  struct sockaddr_in * sin = (struct sockaddr_in *) sh_ipvx_sockaddr_cast(ss);
-
-  (void) port;
-
-  SL_ENTER(_("sh_ipvx_reverse_check_ok"));
-
-  he = sh_gethostbyname(peer);
-  if (he != NULL)
-    {
-      for (p = he->h_addr_list; *p; ++p)
-	{
-	  if (0 == memcmp (*p, &(sin->sin_addr), sizeof(in_addr_t)) ) {
-	    SL_RETURN((1), _("sh_ipvx_reverse_check_ok")); }
-	}
-    }
-#endif
-  SL_RETURN((0), _("sh_ipvx_reverse_check_ok"));
-}
-
-#ifdef SH_CUTEST
-#include <stdlib.h>
-#include "CuTest.h"
-
-void Test_ipvx (CuTest *tc) {
-
-  int result;
-
-  result = sh_ipvx_is_ipv4("123456789");
-  CuAssertTrue(tc, result == FALSE);
-
-  result = sh_ipvx_is_ipv4("123456789123...");
-  CuAssertTrue(tc, result == FALSE);
-
-  result = sh_ipvx_is_ipv4("123.456.789.123");
-  CuAssertTrue(tc, result == FALSE);
-
-  result = sh_ipvx_is_ipv4("123.156.189.254");
-  CuAssertTrue(tc, result == TRUE);
-
-  result = sh_ipvx_is_ipv4("255.255.255.255");
-  CuAssertTrue(tc, result == TRUE);
-
-  result = sh_ipvx_is_ipv4("0.0.0.0");
-  CuAssertTrue(tc, result == TRUE);
-
-  result = sh_ipvx_is_ipv4("0022.156.189.254");
-  CuAssertTrue(tc, result == FALSE);
-
-  result = sh_ipvx_is_ipv4("999999999.1.1.2");
-  CuAssertTrue(tc, result == FALSE);
-
-}
-#endif
-
Index: trunk/src/sh_kern.c
===================================================================
--- trunk/src/sh_kern.c	(revision 1)
+++ trunk/src/sh_kern.c	(revision 1)
@@ -0,0 +1,1897 @@
+/* SAMHAIN file system integrity testing                                   */
+/* Copyright (C) 2001 Rainer Wichmann                                      */
+/*                                                                         */
+/*  This program is free software; you can redistribute it                 */
+/*  and/or modify                                                          */
+/*  it under the terms of the GNU General Public License as                */
+/*  published by                                                           */
+/*  the Free Software Foundation; either version 2 of the License, or      */
+/*  (at your option) any later version.                                    */
+/*                                                                         */
+/*  This program is distributed in the hope that it will be useful,        */
+/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
+/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
+/*  GNU General Public License for more details.                           */
+/*                                                                         */
+/*  You should have received a copy of the GNU General Public License      */
+/*  along with this program; if not, write to the Free Software            */
+/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
+
+
+#include "config_xor.h"
+
+#define SH_SYSCALL_CODE
+
+#ifdef HOST_IS_I86LINUX
+#define SH_IDT_TABLE
+#define SH_PROC_CHECK
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <limits.h>
+#include <sys/wait.h>
+#include <signal.h>
+
+
+#ifdef SH_USE_KERN
+
+#undef  FIL__
+#define FIL__  _("sh_kern.c")
+
+#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) 
+
+#if TIME_WITH_SYS_TIME
+#include <sys/time.h>
+#include <time.h>
+#else
+#if HAVE_SYS_TIME_H
+#include <sys/time.h>
+#else
+#include <time.h>
+#endif
+#endif
+
+
+#include "samhain.h"
+#include "sh_utils.h"
+#include "sh_error.h"
+#include "sh_modules.h"
+#include "sh_kern.h"
+#include "sh_ks_xor.h"
+
+#include "sh_unix.h"
+#include "sh_hash.h"
+
+
+
+sh_rconf sh_kern_table[] = {
+  {
+    N_("severitykernel"),
+    sh_kern_set_severity
+  },
+  {
+    N_("kernelcheckactive"),
+    sh_kern_set_activate
+  },
+  {
+    N_("kernelcheckinterval"),
+    sh_kern_set_timer
+  },
+  {
+    N_("kernelcheckidt"),
+    sh_kern_set_idt
+  },
+  {
+    N_("kernelsystemcall"),
+    sh_kern_set_sc_addr
+  },
+  {
+    N_("kernelsyscalltable"),
+    sh_kern_set_sct_addr
+  },
+  {
+    N_("kernelprocrootlookup"),
+    sh_kern_set_proc_root_lookup
+  },
+ {
+    N_("kernelprocrootiops"),
+    sh_kern_set_proc_root_iops
+  },
+  {
+    N_("kernelprocroot"),
+    sh_kern_set_proc_root
+  },
+  {
+    NULL,
+    NULL
+  },
+};
+
+
+static time_t  lastcheck;
+static int     ShKernActive   = S_TRUE;
+static int     ShKernInterval = 300;
+static int     ShKernSeverity = SH_ERR_SEVERE;
+static int     ShKernDelay    = 100; /* milliseconds */
+static int     ShKernIDT      = S_TRUE;
+
+/* The address of system_call
+ */
+#ifdef SH_SYS_CALL_ADDR
+static unsigned long system_call_addr = SH_SYS_CALL_ADDR;
+#else
+static unsigned long system_call_addr = 0;
+#endif
+
+/* The address of the sys_call_table
+ */
+#ifdef SH_SYS_CALL_TABLE
+static unsigned int  kaddr = SH_SYS_CALL_TABLE;
+#else
+static unsigned int  kaddr = 0;
+#endif
+
+#ifdef PROC_ROOT_LOC
+static unsigned long proc_root = PROC_ROOT_LOC;
+#else
+static unsigned long proc_root = 0;
+#endif
+#ifdef PROC_ROOT_IOPS_LOC
+static unsigned long proc_root_iops = PROC_ROOT_IOPS_LOC;
+#else
+static unsigned long proc_root_iops = 0;
+#endif
+#ifdef PROC_ROOT_LOOKUP_LOC
+static unsigned long proc_root_lookup = PROC_ROOT_LOOKUP_LOC;
+#else
+static unsigned long proc_root_lookup = 0;
+#endif
+
+int sh_kern_null()
+{
+  return 0;
+}
+
+#ifdef SH_IDT_TABLE
+
+#include <asm/segment.h>
+
+#define SH_MAXIDT   256
+unsigned char sh_idt_table[SH_MAXIDT * 8];
+char * sh_strseg(unsigned short segment)
+{
+  switch (segment) {
+  case __KERNEL_CS:
+    return _("KERNEL_CS");
+  case __KERNEL_DS:
+    return _("KERNEL_DS");
+  case __USER_CS:
+    return _("USER_CS");
+  case __USER_DS:
+    return _("USER_DS");
+  default:
+    return _("unknown");
+  }
+}
+/* ifdef SH_IDT_TABLE */
+#endif
+
+static char * sh_kern_charhex( unsigned char i )
+{
+  static char i2h[2];
+  int j, k;
+
+  j = i / 16;
+  k = i - (j*16);
+
+  if (j < 10) i2h[0] = '0'+j;
+  else        i2h[0] = 'A'+(j-10);
+  
+  if (k < 10) i2h[1] = '0'+k;
+  else        i2h[1] = 'A'+(k-10);
+
+  return i2h;
+}
+
+static void sh_kern_push2db (char * name, unsigned long addr, 
+			     unsigned long code1, unsigned long code2,
+			     unsigned char * code, int size)
+{
+  file_type   tmpFile;
+  int         i = 0;
+  char      * p;
+
+  sl_strlcpy(tmpFile.fullpath, name, PATH_MAX);
+  tmpFile.size  = addr;
+  tmpFile.mtime = code1;
+  tmpFile.ctime = code2;
+
+  tmpFile.atime = 0;
+  tmpFile.mode  = 0;
+  tmpFile.owner = 0;
+  tmpFile.group = 0;
+  sl_strlcpy(tmpFile.c_owner, _("root"), 5);
+  sl_strlcpy(tmpFile.c_group, _("root"), 5);
+
+  if ((code != NULL) && (size < (PATH_MAX/2)-1))
+    {
+      tmpFile.c_mode[0] = 'l';  
+      tmpFile.c_mode[1] = 'r'; tmpFile.c_mode[2]  = 'w';
+      tmpFile.c_mode[3] = 'x'; tmpFile.c_mode[4]  = 'r'; 
+      tmpFile.c_mode[5] = 'w'; tmpFile.c_mode[6]  = 'x'; 
+      tmpFile.c_mode[7] = 'r'; tmpFile.c_mode[8]  = 'w'; 
+      tmpFile.c_mode[9] = 'x'; tmpFile.c_mode[10] = '\0'; 
+      for (i = 0; i < size; ++i)
+	{
+	  p = sh_kern_charhex (code[i]);
+	  tmpFile.linkpath[2*i]   = p[0];
+	  tmpFile.linkpath[2*i+1] = p[1];
+	  tmpFile.linkpath[2*i+2] = '\0';
+	}
+    }
+  else
+    {
+      tmpFile.c_mode[0] = '-';  
+      tmpFile.c_mode[1] = '-'; tmpFile.c_mode[2]  = '-';
+      tmpFile.c_mode[3] = '-'; tmpFile.c_mode[4]  = '-'; 
+      tmpFile.c_mode[5] = '-'; tmpFile.c_mode[6]  = '-'; 
+      tmpFile.c_mode[7] = '-'; tmpFile.c_mode[8]  = '-'; 
+      tmpFile.c_mode[9] = '-'; tmpFile.c_mode[10] = '\0'; 
+      tmpFile.linkpath[0] = '-'; tmpFile.linkpath[1] = '\0';
+    }
+
+  if (sh.flag.checkSum == SH_CHECK_CHECK && sh.flag.update == S_TRUE)
+    sh_hash_pushdata_memory (&tmpFile, 
+			     _("000000000000000000000000000000000000000000000000"));
+  else
+    sh_hash_pushdata (&tmpFile, 
+		      _("000000000000000000000000000000000000000000000000"));
+  return;
+}
+
+extern int sh_util_hextobinary (char * binary, char * hex, int bytes);
+
+static char * sh_kern_db2pop (char * name, unsigned long * addr, 
+			      unsigned long * code1, unsigned long * code2,
+			      int * size)
+{
+  file_type   tmpFile;
+  char      * p;
+  int         i;
+
+  if (0 == sh_hash_get_it (name, &tmpFile))
+    {
+      *addr  = tmpFile.size;
+      *code1 = tmpFile.mtime;
+      *code2 = tmpFile.ctime;
+
+      if (tmpFile.linkpath[0] != '-')
+	{
+	  p = SH_ALLOC(PATH_MAX);
+	  i = sh_util_hextobinary (p, tmpFile.linkpath, 
+				   strlen(tmpFile.linkpath));
+	  if (i == 0)
+	    {
+	      *size = (strlen(tmpFile.linkpath)/2);
+	      p[*size] = '\0';
+	      return p;
+	    }
+	  else
+	    {
+	      SH_FREE(p);
+	      *size = 0;
+	      return NULL;
+	    }
+	}
+      else
+	{
+	  *size = 0;
+	  return NULL;
+	}
+    }
+  else
+    {
+      *size = 0;
+      *addr = 0;
+      return NULL;
+    }
+}
+
+char * sh_kern_db_syscall (int num, char * prefix,
+			   void * in_name, unsigned long * addr,
+			   unsigned int * code1, unsigned int * code2,
+			   int * size, int direction)
+{
+  char          path[128];
+  char        * p = NULL;
+  unsigned long x1, x2;
+  char        * name = (char *) in_name;
+
+  sl_snprintf(path, 128, "K_%s_%04d", prefix, num);
+
+  if (direction == 0) 
+    {
+      x1 = *code1;
+      x2 = *code2;
+
+      sh_kern_push2db (path, *addr, x1, x2,
+		       name, (name == NULL) ? 0 : (*size));
+    }
+  else
+    {
+      p = sh_kern_db2pop (path, addr,  &x1, &x2, size);
+      *code1 = (unsigned int) x1;
+      *code2 = (unsigned int) x2;
+    }
+  return p;
+}
+ 
+#ifdef HOST_IS_LINUX
+
+int sh_kern_data_init ()
+{
+  unsigned long store0 = 0;
+  unsigned int  store1 = 0, store2 = 0;
+  int           datasize, i, j;
+  char        * databuf;
+
+#ifdef SH_SYSCALL_CODE
+  /* system_call code
+   */
+  databuf = sh_kern_db_syscall (0, _("system_call"), 
+				NULL, &store0, &store1, &store2,
+				&datasize, 1);
+  if (datasize == sizeof(system_call_code))
+    {
+      memcpy (system_call_code, databuf, sizeof(system_call_code));
+      SH_FREE(databuf);
+    }
+  else
+    {
+      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
+		      _("system_call_code not found in database"), 
+		      _("sh_kern_data_init"));
+      return -1;
+    }
+#endif
+
+  /* syscall address and code
+   */ 
+  for (i = 0; i < SH_MAXCALLS; ++i) 
+    {
+      databuf = sh_kern_db_syscall (i, _("syscall"), 
+				    NULL, &store0, &store1, &store2,
+				    &datasize, 1);
+      sh_syscalls[i].addr = store0;
+      if (store0 == 0) {
+	sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, i, MSG_E_SUBGEN,
+			_("syscall address not found in database"), 
+			_("sh_kern_data_init"));
+	return -1;
+      }
+#ifdef SH_SYSCALL_CODE
+      sh_syscalls[i].code[0] = (unsigned int) store1; 
+      sh_syscalls[i].code[1] = (unsigned int) store2;
+      if ((store1 == 0) || (store2 == 0)) {
+	sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, i, MSG_E_SUBGEN,
+			_("syscall code not found in database"), 
+			_("sh_kern_data_init"));
+	/* fprintf(stderr, "Syscall #%d\n", i); */
+	/* return -1; */
+      }
+#endif 
+      if (databuf != NULL) {
+	SH_FREE(databuf);
+      }
+      
+    }
+
+#ifdef SH_IDT_TABLE
+  if (ShKernIDT == S_TRUE)
+    {
+      for (j = 0; j < SH_MAXIDT; ++j) 
+	{
+	  databuf = sh_kern_db_syscall (j, _("idt_table"), 
+					NULL, 
+					&store0, &store1, &store2,
+					&datasize, 1);
+	  if (datasize == 8) {
+	    memcpy(&idt_table[j*8], databuf, 8);
+	    SH_FREE(databuf);
+	  } else {
+	    sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, j, MSG_E_SUBGEN,
+			    _("idt table not found in database"), 
+			    _("sh_kern_data_init"));
+	    return -1;
+	  }
+	}
+    }
+#endif
+
+  return 0;
+}
+
+#ifdef SH_PROC_CHECK
+struct inode_operations {
+  int (*create) (int *,int *,int);
+  int * (*lookup) (int *,int *);
+  int (*link) (int *,int *,int *);
+  int (*unlink) (int *,int *);
+  int (*symlink) (int *,int *,const char *);
+  int (*mkdir) (int *,int *,int);
+  int (*rmdir) (int *,int *);
+  int (*mknod) (int *,int *,int,int);
+  int (*rename) (int *, int *,
+                 int *, int *);
+  int (*readlink) (int *, char *,int);
+  int (*follow_link) (int *, int *);
+  void (*truncate) (int *);
+  int (*permission) (int *, int);
+  int (*revalidate) (int *);
+  /*
+    int (*setattr) (int *, int *);
+    int (*getattr) (int *, int *);
+    int (*setxattr) (int *, const char *, void *, size_t, int);
+    ssize_t (*getxattr) (int *, const char *, void *, size_t);
+    ssize_t (*listxattr) (int *, char *, size_t);
+    int (*removexattr) (int *, const char *);
+  */
+};
+
+struct file_operations {
+  int (*create) (int *,int *,int);
+};
+
+struct proc_dir_entry {
+  unsigned short low_ino;
+  unsigned short namelen;
+  const char * name;
+  mode_t mode;
+  nlink_t nlink;
+  uid_t uid;
+  gid_t gid;
+  unsigned long size; 
+  struct inode_operations * proc_iops;
+  struct file_operations * proc_fops;
+  /*
+  get_info_t *get_info;
+  struct module *owner;
+  struct proc_dir_entry *next, *parent, *subdir;
+  void *data;
+  read_proc_t *read_proc;
+  write_proc_t *write_proc;
+  atomic_t count;         
+  int deleted;  
+  */          
+};
+#endif
+
+int sh_kern_check_internal ()
+{
+  static int is_init = 0;
+  int kd;
+  int res;
+  pid_t mpid;
+  int mpipe[2];
+  int i, j, status = 0;
+  /* unsigned int kaddr; */
+  unsigned long kmem_call_table[512];
+
+#ifdef SH_PROC_CHECK
+  struct inode_operations proc_root_inode;
+  struct proc_dir_entry proc_root_dir;
+#endif
+
+#ifdef SH_SYSCALL_CODE
+  unsigned int kmem_code_table[512][2];
+#endif
+#ifdef SH_IDT_TABLE
+  unsigned char  buf[6];
+  unsigned short idt_size;
+  unsigned long  idt_addr;
+  /* int            k, curr_keep = 0; */
+  unsigned short idt_offset_lo, idt_offset_hi, idt_selector;
+  unsigned char  idt_reserved, idt_flag;
+  unsigned short sh_idt_offset_lo, sh_idt_offset_hi, sh_idt_selector;
+  unsigned char  sh_idt_reserved, sh_idt_flag;
+  int            dpl;
+  unsigned long  idt_iaddr;
+  int            sh_dpl;
+  unsigned long  sh_idt_iaddr;
+  char           idt_type, sh_idt_type;
+#endif
+
+  unsigned char new_system_call_code[256];
+
+#ifdef SH_USE_LKM
+  static int check_getdents      = 0;
+  /* #ifdef __NR_getdents64 */
+  static int check_getdents64    = 0;
+  /* #endif */
+  static int copy_if_next        = -1;
+  static int copy_if_next_64     = -1;
+#endif
+
+  unsigned long store0;
+  unsigned int  store1, store2;
+  int           datasize;
+  int           mod_syscall_addr = 0;
+  int           mod_syscall_code = 0;
+  UINT64        size_old  = 0, size_new = 0;
+  UINT64        mtime_old = 0, mtime_new = 0;
+  UINT64        ctime_old = 0, ctime_new = 0;
+  char          tmp[128];
+  char          msg[2*SH_BUFSIZE];
+  char timstr_o[32];
+  char timstr_n[32];
+  char * p;
+  int    k;
+  char * linkpath_old;
+  char * linkpath_new;
+
+  int           max_system_call = (SYS_CALL_LOC < 128) ? 128 : SYS_CALL_LOC;
+
+  SL_ENTER(_("sh_kern_check_internal"));
+
+  
+  if (is_init == 0)
+    {
+      if (sh.flag.checkSum != SH_CHECK_INIT && sh.flag.update != S_TRUE)
+	{
+	  if (0 == sh_kern_data_init()) {
+	    is_init = 1;
+	  } else {
+	    sh_error_handle (ShKernSeverity, FIL__, __LINE__, status, 
+			     MSG_E_SUBGEN,
+			     _("could not initialize - switching off"),
+			     _("kern_check_internal") );
+	    ShKernActive = S_FALSE;
+	    SL_RETURN( (-1), _("sh_kern_check_internal"));
+	  }
+	}
+      else if ((sh.flag.checkSum == SH_CHECK_INIT || 
+		sh.flag.checkSum == SH_CHECK_CHECK) && 
+	       (sh.flag.update == S_TRUE))
+	{
+	  if (0 == sh_kern_data_init()) {
+	    is_init = 1;
+	  } else {
+	    sh_error_handle (SH_ERR_WARN, FIL__, __LINE__, status, 
+			     MSG_E_SUBGEN,
+			     _("no or incomplete data in baseline database"),
+			     _("kern_check_internal") );
+	  }
+	}
+    }
+
+  /*
+   * kaddr is the address of the sys_call_table
+   */
+
+  if (kaddr == (unsigned int) -1)
+    {
+      sh_error_handle (ShKernSeverity, FIL__, __LINE__, status, MSG_E_SUBGEN,
+		       _("no address for sys_call_table - switching off"),
+		       _("kern_check_internal") );
+      ShKernActive = S_FALSE;
+      SL_RETURN( (-1), _("sh_kern_check_internal"));
+    }
+  
+  kd = aud_open(FIL__, __LINE__, SL_YESPRIV, _("/dev/kmem"), O_RDONLY, 0);
+  
+  if (kd < 0)
+    {
+      status = errno;
+      sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
+		       _("error opening /dev/kmem"),
+		       _("kern_check_internal") );
+      SL_RETURN( (-1), _("sh_kern_check_internal"));
+    }
+
+  status = aud_pipe(FIL__, __LINE__, mpipe);
+
+  if (status == 0)
+    {
+      mpid = aud_fork(FIL__, __LINE__);
+
+      switch (mpid) 
+	{
+	case -1:
+	  status = -1;
+	  break;
+	case 0:                       /* child */
+	  status = close(mpipe[0]);
+	  setpgid(0, 0);
+	  
+	  /* Seek to the system call table (at kaddr) and read it into
+	   * the kmem_call_table array
+	   */
+	  if(status == 0)
+	    {
+	      if(lseek(kd, (off_t)kaddr, SEEK_SET) == -1)
+		{
+		  status = -2;
+		}
+	    }
+	  if(status == 0)
+	    {
+	      retry_msleep (0, ShKernDelay); /* milliseconds */
+	      if (sizeof(kmem_call_table) != 
+		  read(kd, &kmem_call_table, sizeof(kmem_call_table)))
+		{
+		  status = -3;
+		}
+	    }
+
+#ifdef SH_SYSCALL_CODE
+	  /* 
+	   * Seek to the system call address (at sh_syscalls[j].addr) and 
+	   * read first 8 bytes into kmem_code_table[j][] (2 * unsigned int)
+	   */
+	  if(status == 0)
+	    {
+	      memset(kmem_code_table, 0, sizeof(kmem_code_table));
+	      for (j = 0; j < SH_MAXCALLS; ++j) 
+		{
+
+		  if (sh_syscalls[j].addr == 0UL) {
+		    sh_syscalls[j].addr = kmem_call_table[j];
+		  }
+
+		  if (sh_syscalls[j].name == NULL || 
+		      sh_syscalls[j].addr == 0UL)
+		    break;
+
+		  if ((sh.flag.checkSum == SH_CHECK_INIT || 
+		       sh.flag.checkSum == SH_CHECK_CHECK) && 
+		      (sh.flag.update == S_TRUE))
+		    {
+		      lseek (kd, kmem_call_table[j], SEEK_SET);
+		    }
+		  else
+		    {
+		      lseek (kd, sh_syscalls[j].addr, SEEK_SET);
+		    }
+		  read  (kd, &(kmem_code_table[j][0]), 
+			 2 * sizeof(unsigned int));
+		}
+	    }
+#endif
+
+#ifdef SH_IDT_TABLE
+	  if(status == 0)
+	    {
+	      /* 
+	       * Get the address and size of Interrupt Descriptor Table,
+	       * and read the content into sh_idt_table[]
+	       */
+	      __asm__ volatile ("sidt %0": "=m" (buf));
+	      idt_size = *((unsigned short *) &buf[0]);
+	      idt_addr = *((unsigned long *)  &buf[2]);
+	      idt_size = (idt_size + 1)/8;
+
+	      if (idt_size > SH_MAXIDT)
+		idt_size = SH_MAXIDT;
+
+	      memset(sh_idt_table, '\0', SH_MAXIDT*8);
+	      lseek (kd, idt_addr, SEEK_SET);
+	      read  (kd, sh_idt_table, idt_size*8);
+	    }
+#endif
+
+	  /* 
+	   * Seek to the system_call address (at system_call_addr) and 
+	   * read first 256 bytes into new_system_call_code[]
+	   *
+	   * system_call_addr is defined in the include file.
+	   */
+	  if(status == 0)
+	    {
+	      lseek (kd, system_call_addr, SEEK_SET);
+	      read  (kd, new_system_call_code, 256);
+	    }
+
+
+	  /* 
+	   * Seek to proc_root and read the structure.
+	   * Seek to proc_root_inode_operations and get the structure.
+	   */
+#ifdef SH_PROC_CHECK
+	  if(status == 0)
+	    {
+	      lseek (kd, proc_root, SEEK_SET);
+	      read  (kd, &proc_root_dir, sizeof(proc_root_dir));
+	      lseek (kd, proc_root_iops, SEEK_SET);
+	      read  (kd, &proc_root_inode, sizeof(proc_root_inode));
+	    }
+#endif
+
+	  if(status == 0)
+	    {
+	      status = 
+		write(mpipe[1], &kmem_call_table, sizeof(kmem_call_table));
+#ifdef SH_SYSCALL_CODE
+	      if(status > 0)
+		{
+		  status = 
+		    write(mpipe[1], &kmem_code_table, sizeof(kmem_code_table));
+		}
+#endif
+#ifdef SH_IDT_TABLE
+	      if(status > 0)
+		{
+		  status = 
+		    write(mpipe[1], &sh_idt_table, sizeof(sh_idt_table));
+		}
+#endif
+	      if(status > 0)
+		{
+		  status = 
+		    write(mpipe[1], new_system_call_code, 256);
+		}
+#ifdef SH_PROC_CHECK
+	      if(status > 0)
+		{
+		  status = 
+		    write(mpipe[1], &proc_root_dir, sizeof(proc_root_dir));
+		}
+	      if(status > 0)
+		{
+		  status = 
+		    write(mpipe[1], &proc_root_inode, sizeof(proc_root_inode));
+		}
+#endif
+	    }
+	  _exit( (status >= 0) ? 0 : status);
+	  break;
+	  
+	default:
+	  close (mpipe[1]);
+	  close (kd);
+	  retry_msleep (0, ShKernDelay); /* milliseconds */
+	  if (sizeof(kmem_call_table) != 
+	      read(mpipe[0], &kmem_call_table, sizeof(kmem_call_table)))
+	    status = -4;
+	  else
+	    status = 0;
+
+#ifdef SH_SYSCALL_CODE
+	  if(status == 0)
+	    {
+	      if (sizeof(kmem_code_table) != 
+		  read(mpipe[0], &kmem_code_table, sizeof(kmem_code_table)))
+		status = -5;
+	      else
+		status = 0;
+	    }
+#endif	  
+
+#ifdef SH_IDT_TABLE
+	  if(status == 0)
+	    {
+	      memset(sh_idt_table, '\0', SH_MAXIDT*8);
+	      if (sizeof(sh_idt_table) != 
+		  read(mpipe[0], &sh_idt_table, sizeof(sh_idt_table)))
+		status = -5;
+	      else
+		status = 0;
+	    }
+#endif	  
+
+	  if(status == 0)
+	    {
+	      if (256 != read(mpipe[0], new_system_call_code, 256))
+		status = -6;
+	      else
+		status = 0;
+	    }
+
+#ifdef SH_PROC_CHECK
+	  if(status == 0)
+	    {
+	      if (sizeof(proc_root_dir) !=
+		  read(mpipe[0], &proc_root_dir, sizeof(proc_root_dir)))
+		status = -7;
+	      else
+		status = 0;
+	    }
+	  if(status == 0)
+	    {
+	      if (sizeof(proc_root_inode) !=
+		  read(mpipe[0], &proc_root_inode, sizeof(proc_root_inode)))
+		status = -8;
+	      else
+		status = 0;
+	    }
+#endif
+
+	  if (status < 0)
+	    res = waitpid(mpid, NULL,    WNOHANG|WUNTRACED);
+	  else 
+	    {
+	      res = waitpid(mpid, &status, WNOHANG|WUNTRACED);
+	      if (res == 0 && 0 != WIFEXITED(status))
+		status = WEXITSTATUS(status);
+	    }
+	  close (mpipe[0]);
+	  if (res <= 0)
+	    {
+	      aud_kill(FIL__, __LINE__, mpid, 9);
+	      waitpid(mpid, NULL, 0);
+	    }
+	  break;
+	}
+    }
+
+  if ( status < 0)
+    {
+      sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
+		       _("error reading from /dev/kmem"),
+		       _("kern_check_internal") );
+      SL_RETURN( (-1), _("sh_kern_check_internal"));
+    }
+
+  /* Check the proc_root inode.
+   *
+   * This will detect adore-ng.
+   */
+  if ( (unsigned int) *proc_root_inode.lookup != proc_root_lookup)
+    {
+      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_KERN_PROC,
+		       _("proc_root_inode_operations.lookup != proc_root_lookup"));
+    }
+  else if ( ( ((unsigned int) * &proc_root_dir.proc_iops) != proc_root_iops) &&
+       (proc_root_dir.size != proc_root_iops))
+    {
+      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_KERN_PROC,
+		       _("proc_root.proc_iops != proc_root_inode_operations"));
+    }
+
+  /* Check the system_call syscall gate.
+   *
+   * Stored(old) is system_call_code[]
+   */
+  if (sh.flag.checkSum == SH_CHECK_INIT)
+    {
+      store0 = 0; store1 = 0; store2 = 0;
+      datasize = sizeof(system_call_code);
+      sh_kern_db_syscall (0, _("system_call"), 
+			  new_system_call_code, &store0, &store1, &store2,
+			  &datasize, 0);
+    }
+
+  if ((sh.flag.checkSum != SH_CHECK_INIT) || 
+      (sh.flag.update == S_TRUE && is_init == 1))
+    {
+      for (i = 0; i < (max_system_call + 4); ++i) 
+	{
+	  if (system_call_code[i] != new_system_call_code[i])
+	    {
+#ifdef SH_USE_XML
+	      sl_snprintf(tmp, 128, "path=\"K_%s_%04d\" ", 
+			  _("system_call"), 0);
+#else
+	      sl_snprintf(tmp, 128, "path=<K_%s_%04d>, ", 
+			  _("system_call"), 0);
+#endif
+	      sl_strlcpy(msg, tmp, SH_BUFSIZE);
+
+	      linkpath_old = SH_ALLOC(520);
+	      linkpath_new = SH_ALLOC(520);
+	      for (k = 0; k < 256; ++k)
+		{
+		  p = sh_kern_charhex (system_call_code[k]);
+		  linkpath_old[2*k]   = p[0];
+		  linkpath_old[2*k+1] = p[1];
+		  linkpath_old[2*k+2] = '\0';
+		}
+	      for (k = 0; k < 256; ++k)
+		{
+		  p = sh_kern_charhex (new_system_call_code[k]);
+		  linkpath_new[2*k]   = p[0];
+		  linkpath_new[2*k+1] = p[1];
+		  linkpath_new[2*k+2] = '\0';
+		}
+#ifdef SH_USE_XML
+	      sl_strlcat(msg, _("link_old=\""),    2*SH_BUFSIZE);
+	      sl_strlcat(msg, linkpath_old,        2*SH_BUFSIZE);
+	      sl_strlcat(msg, _("\" link_new=\""), 2*SH_BUFSIZE);
+	      sl_strlcat(msg, linkpath_new,        2*SH_BUFSIZE);
+	      sl_strlcat(msg, _("\""),             2*SH_BUFSIZE);
+#else
+	      sl_strlcat(msg, _("link_old=<"),     2*SH_BUFSIZE);
+	      sl_strlcat(msg, linkpath_old,        2*SH_BUFSIZE);
+	      sl_strlcat(msg, _(">, link_new=<"),   2*SH_BUFSIZE);
+	      sl_strlcat(msg, linkpath_new,        2*SH_BUFSIZE);
+	      sl_strlcat(msg, _(">"),              2*SH_BUFSIZE);
+#endif
+
+	      sh_error_handle (ShKernSeverity, FIL__, __LINE__, 
+			       status, MSG_KERN_GATE,
+			       new_system_call_code[i], 0,
+			       system_call_code[i], 0,
+			       0, _("system_call (interrupt handler)"),
+			       msg);
+	      
+	      SH_FREE(linkpath_old);
+	      SH_FREE(linkpath_new);
+
+	      for (j = 0; j < (max_system_call + 4); ++j)
+		system_call_code[j] = new_system_call_code[j];
+	      break;
+	    }
+	}
+    }
+  
+  /* Check the individual syscalls
+   *
+   * Stored(old) is sh_syscalls[] array.
+   */
+  if (sh.flag.checkSum == SH_CHECK_INIT)
+    {
+      for (i = 0; i < SH_MAXCALLS; ++i) 
+	{
+	  store0 = kmem_call_table[i]; 
+#ifdef SH_SYSCALL_CODE
+	  store1 = kmem_code_table[i][0]; store2 = kmem_code_table[i][1];
+#else
+	  store1 = 0; store2 = 0;
+#endif
+	  sh_kern_db_syscall (i, _("syscall"), 
+			      NULL, &store0, &store1, &store2,
+			      0, 0);
+	}
+    }
+
+  if ((sh.flag.checkSum != SH_CHECK_INIT) || 
+      (sh.flag.update == S_TRUE && is_init == 1))
+    {
+      for (i = 0; i < SH_MAXCALLS; ++i) 
+	{
+	  if (sh_syscalls[i].name == NULL /* || sh_syscalls[i].addr == 0UL */)
+	    break;
+
+#ifdef SH_USE_LKM
+	  if (sh_syscalls[i].addr != kmem_call_table[i])
+	    {
+	      if (check_getdents == 0 && 
+		  0 == strcmp(_(sh_syscalls[i].name), _("sys_getdents")))
+		{
+		  check_getdents = 1;
+		  sh_error_handle (SH_ERR_WARN, FIL__, __LINE__, 
+				   status, MSG_E_SUBGEN,
+				   _("Modified kernel syscall (expected)."),
+				   _(sh_syscalls[i].name) );
+		  copy_if_next = i;
+		  sh_syscalls[i].addr = kmem_call_table[i];
+		  continue;
+		}
+	      /* #ifdef __NR_getdents64 */
+	      else if  (check_getdents64 == 0 && 
+			0 == strcmp(_(sh_syscalls[i].name), 
+				    _("sys_getdents64")))
+		{
+		  check_getdents64 = 1;
+		  sh_error_handle (SH_ERR_WARN, FIL__, __LINE__, 
+				   status, MSG_E_SUBGEN,
+				   _("Modified kernel syscall (expected)."),
+				   _(sh_syscalls[i].name) );
+		  copy_if_next_64 = i;
+		  sh_syscalls[i].addr = kmem_call_table[i];
+		  continue;
+		}
+	      /* #endif */
+	      else
+		{
+		  size_old = sh_syscalls[i].addr;
+		  size_new = kmem_call_table[i];
+		  mod_syscall_addr = 1;
+		  /*
+		  sh_error_handle (ShKernSeverity, FIL__, __LINE__, 
+				   status, MSG_KERN_POLICY,
+				   kmem_call_table[i],
+				   sh_syscalls[i].addr,
+				   i, _(sh_syscalls[i].name)
+				   );
+		  */
+		}
+	      sh_syscalls[i].addr = kmem_call_table[i];
+	    }
+#else
+	  if (sh_syscalls[i].addr != kmem_call_table[i])
+	    {
+	      size_old = sh_syscalls[i].addr;
+	      size_new = kmem_call_table[i];
+	      mod_syscall_addr = 1;
+	      /*
+	      sh_error_handle (ShKernSeverity, FIL__, __LINE__, 
+			       status, MSG_KERN_POLICY,
+			       kmem_call_table[i],
+			       sh_syscalls[i].addr,
+			       i, _(sh_syscalls[i].name)
+			       );
+	      */
+	      sh_syscalls[i].addr = kmem_call_table[i];
+	    }
+#endif
+
+
+	  /* Check the code at syscall address
+	   *
+	   * Stored(old) is sh_syscalls[]
+	   */
+#ifdef SH_SYSCALL_CODE
+	  if ( (mod_syscall_addr == 0) && 
+	       ((sh_syscalls[i].code[0] != kmem_code_table[i][0]) || 
+		(sh_syscalls[i].code[1] != kmem_code_table[i][1]))
+	       )
+	    {
+	      mtime_old = sh_syscalls[i].code[0];
+	      mtime_new = kmem_code_table[i][0];
+	      ctime_old = sh_syscalls[i].code[1];
+	      ctime_new = kmem_code_table[i][1];
+	      mod_syscall_code = 1;
+
+#ifdef SH_USE_LKM
+	      if (i == copy_if_next)
+		{
+		  mod_syscall_code =  0;
+		  copy_if_next     = -1;
+		}
+	      if (i == copy_if_next_64)
+		{
+		  mod_syscall_code =  0;
+		  copy_if_next_64  = -1;
+		}
+#endif
+
+	      /*
+	      sh_error_handle (ShKernSeverity, FIL__, __LINE__, 
+			       status, MSG_KERN_POL_CO,
+			       kmem_code_table[i][0],  kmem_code_table[i][1],
+			       sh_syscalls[i].code[0], sh_syscalls[i].code[1],
+			       i, _(sh_syscalls[i].name)
+			       );
+	      */
+	      sh_syscalls[i].code[0] = kmem_code_table[i][0];
+	      sh_syscalls[i].code[1] = kmem_code_table[i][1];
+	    }
+#endif
+	  /*
+	   * Build the error message, if something has been
+	   * detected.
+	   */
+	  if ((mod_syscall_addr != 0) || (mod_syscall_code != 0))
+	    {
+#ifdef SH_USE_XML
+	      sl_snprintf(tmp, 128, "path=\"K_%s_%04d\" ", 
+			  _("syscall"), i);
+#else
+	      sl_snprintf(tmp, 128, "path=<K_%s_%04d>, ", 
+			  _("syscall"), i);
+#endif
+	      sl_strlcpy(msg, tmp, SH_BUFSIZE);
+
+	      if (mod_syscall_addr != 0)
+		{
+		  sl_snprintf(tmp, 128, sh_hash_size_format(),
+			      size_old, size_new);
+		  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+		}
+	      if (mod_syscall_code != 0)
+		{
+		  sl_strlcpy (timstr_o, sh_unix_gmttime (ctime_old), 32);
+		  sl_strlcpy (timstr_n, sh_unix_gmttime (ctime_new), 32);
+#ifdef SH_USE_XML
+		  sl_snprintf(tmp, 128, 
+			      _("ctime_old=\"%s\" ctime_new=\"%s\" "), 
+			      timstr_o, timstr_n);
+#else
+		  sl_snprintf(tmp, 128, 
+			      _("ctime_old=<%s>, ctime_new=<%s>, "), 
+			      timstr_o, timstr_n);
+#endif
+		  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+		  sl_strlcpy (timstr_o, sh_unix_gmttime (mtime_old), 32);
+		  sl_strlcpy (timstr_n, sh_unix_gmttime (mtime_new), 32);
+#ifdef SH_USE_XML
+		  sl_snprintf(tmp, 128, 
+			      _("mtime_old=\"%s\" mtime_new=\"%s\" "), 
+			      timstr_o, timstr_n);
+#else
+		  sl_snprintf(tmp, 128, 
+			      _("mtime_old=<%s>, mtime_new=<%s> "), 
+			      timstr_o, timstr_n);
+#endif
+		  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+		}
+	      sh_error_handle (ShKernSeverity, FIL__, __LINE__, 
+			       status, MSG_KERN_SYSCALL,
+			       i, _(sh_syscalls[i].name), msg);
+	      mod_syscall_addr = 0;
+	      mod_syscall_code = 0;
+	    }
+	}
+    }
+
+#ifdef SH_IDT_TABLE
+  if (ShKernIDT == S_TRUE)
+    {
+      if (sh.flag.checkSum == SH_CHECK_INIT)
+	{
+	  datasize = 8;
+	  for (j = 0; j < SH_MAXIDT; ++j) 
+	    {
+	      sh_kern_db_syscall (j, _("idt_table"), 
+				  &sh_idt_table[j*8], 
+				  &store0, &store1, &store2,
+				  &datasize, 0);
+	    }
+	}
+
+      if ((sh.flag.checkSum != SH_CHECK_INIT) || 
+	  (sh.flag.update == S_TRUE && is_init == 1))
+	{
+	  /* Check the Interrupt Descriptor Table
+	   *
+	   * Stored(old) is idt_table[]
+	   */
+	  for (j = 0; j < SH_MAXIDT; ++j)
+	    {
+	      i = j * 8;
+	  
+	      sh_idt_offset_lo = *((unsigned short *) &sh_idt_table[i]);
+	      sh_idt_selector  = *((unsigned short *) &sh_idt_table[i+2]);
+	      sh_idt_reserved  = (unsigned char) sh_idt_table[i+4];
+	      sh_idt_flag      = (unsigned char) sh_idt_table[i+5];
+	      sh_idt_offset_hi = *((unsigned short *) &sh_idt_table[i+6]);
+	      sh_idt_iaddr = (unsigned long)(sh_idt_offset_hi << 16) 
+		+ sh_idt_offset_lo;
+	      
+	      if (sh_idt_iaddr == 0)
+		{
+		  sh_idt_table[i+2] = '\0';
+		  sh_idt_table[i+3] = '\0';
+		  sh_idt_table[i+5] = '\0';
+
+		  idt_offset_lo = *((unsigned short *) &idt_table[i]);
+		  idt_offset_hi = *((unsigned short *) &idt_table[i+6]);
+		  idt_iaddr = (unsigned long)(idt_offset_hi << 16) 
+		    + idt_offset_lo;
+		  if (idt_iaddr == 0)
+		    {
+		      idt_table[i+2] = '\0';
+		      idt_table[i+3] = '\0';
+		      idt_table[i+5] = '\0';
+		    }
+		  
+		}
+	  
+	      if (memcmp(&sh_idt_table[i], &idt_table[i], 8) != 0)
+		{
+		  
+		  idt_offset_lo = *((unsigned short *) &idt_table[i]);
+		  idt_selector  = *((unsigned short *) &idt_table[i+2]);
+		  idt_reserved  = (unsigned char) idt_table[i+4];
+		  idt_flag      = (unsigned char) idt_table[i+5];
+		  idt_offset_hi = *((unsigned short *) &idt_table[i+6]);
+		  idt_iaddr = (unsigned long)(idt_offset_hi << 16) 
+		    + idt_offset_lo;
+	      
+		  if (idt_iaddr != 0)
+		    {
+		      if (idt_flag & 64) { dpl = 3; }
+		      else               { dpl = 0; }
+		      if (idt_flag & 1)  { 
+			if (dpl == 3) idt_type = 'S'; 
+			else idt_type = 'T'; }
+		      else               { idt_type = 'I'; }
+		    }
+		  else { dpl = -1; idt_type = 'U'; }
+		  
+		  if (sh_idt_iaddr != 0)
+		    {
+		      if (sh_idt_flag & 64) { sh_dpl = 3; }
+		      else               { sh_dpl = 0; }
+		      if (sh_idt_flag & 1)  { 
+			if (sh_dpl == 3) sh_idt_type = 'S'; 
+			else sh_idt_type = 'T'; }
+		      else               { sh_idt_type = 'I'; }
+		    }
+		  else { sh_dpl = -1; sh_idt_type = 'U'; }
+		  
+#ifdef SH_USE_XML
+		  sl_snprintf(tmp, 128, "path=\"K_%s_%04d\" ", 
+			      _("idt_table"), j);
+#else
+		  sl_snprintf(tmp, 128, "path=<K_%s_%04d> ", 
+			      _("idt_table"), j);
+#endif
+		  sl_strlcpy(msg, tmp, SH_BUFSIZE);
+
+		  linkpath_old = SH_ALLOC(32);
+		  linkpath_new = SH_ALLOC(32);
+		  for (k = 0; k < 8; ++k)
+		    {
+		      p = sh_kern_charhex (idt_table[i+k]);
+		      linkpath_old[2*k]   = p[0];
+		      linkpath_old[2*k+1] = p[1];
+		      linkpath_old[2*k+2] = '\0';
+		    }
+		  for (k = 0; k < 8; ++k)
+		    {
+		      p = sh_kern_charhex (sh_idt_table[i+k]);
+		      linkpath_new[2*k]   = p[0];
+		      linkpath_new[2*k+1] = p[1];
+		      linkpath_new[2*k+2] = '\0';
+		    }
+#ifdef SH_USE_XML
+		  sl_snprintf(tmp, 128,
+			      _("link_old=\"%s\" link_new=\"%s\" "), 
+			      linkpath_old, linkpath_new);
+#else
+		  sl_snprintf(tmp, 128,
+			      _("link_old=<%s> link_new=<%s> "), 
+			      linkpath_old, linkpath_new);
+#endif
+		  sl_strlcat(msg, tmp, SH_BUFSIZE);
+
+		  sh_error_handle (ShKernSeverity, FIL__, __LINE__, 
+				   status, MSG_KERN_IDT,
+				   j, 
+				   sh_idt_iaddr, sh_strseg(sh_idt_selector), 
+				   (int) sh_dpl, sh_idt_type, 
+				   idt_iaddr, sh_strseg(idt_selector),
+				   (int) dpl, idt_type, msg);
+		  
+		  SH_FREE(linkpath_old);
+		  SH_FREE(linkpath_new);
+
+		  memcpy(&idt_table[i], &sh_idt_table[i], 8);
+		}
+	    }
+	}
+    }
+#endif
+
+  SL_RETURN( (0), _("sh_kern_check_internal"));
+}
+/* ifdef HOST_IS_LINUX */
+#else
+
+#include <err.h>
+#include <kvm.h>
+#include <nlist.h>
+
+/* not OpenBSD */
+#if defined(HOST_IS_FREEBSD)
+#include <sys/sysent.h>
+#endif
+
+#include <sys/syscall.h>
+#ifndef  SYS_MAXSYSCALL
+#define  SYS_MAXSYSCALL	512
+#endif
+
+#ifdef __OpenBSD__
+struct proc;
+struct sysent {
+	short sy_narg;
+	short sy_argsize;
+	int   (*sy_call)(struct proc *, void *, register_t *);
+};
+#endif
+
+int sh_kern_data_init ()
+{
+  unsigned long store0 = 0;
+  unsigned int  store1 = 0, store2 = 0;
+  int           datasize, i;
+  char        * databuf = NULL;
+
+  /* syscall address and code
+   */ 
+  for (i = 0; i < SH_MAXCALLS; ++i) 
+    {
+      databuf = sh_kern_db_syscall (i, _("syscall"), 
+				    NULL, &store0, &store1, &store2,
+				    &datasize, 1);
+      sh_syscalls[i].addr = store0;
+      if (databuf != NULL) { SH_FREE(databuf); }
+      if (store0 == 0) {
+	sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
+			_("syscall address not found in database"), 
+			_("sh_kern_data_init"));
+	return -1;
+      }
+
+      sh_syscalls[i].code[0] = (unsigned int) store1; 
+      sh_syscalls[i].code[1] = (unsigned int) store2;
+      if ((store1 == 0) || (store2 == 0)) {
+	sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
+			_("syscall code not found in database"), 
+			_("sh_kern_data_init"));
+	return -1;
+      }
+
+    }
+
+  return 0;
+}
+
+int sh_kern_check_internal ()
+{
+  struct sysent  sy;
+  kvm_t * kd;
+  int     i;
+  int     status = -1;
+  char    errbuf[_POSIX2_LINE_MAX+1];
+  struct  nlist * sys_list;
+  struct  nlist list[2];
+
+  unsigned long offset = 0L;
+  unsigned int  syscall_code[2];  /* 8 bytes */
+  unsigned long syscall_addr;
+
+  unsigned long store0 = 0;
+  unsigned int  store1 = 0, store2 = 0;
+
+  UINT64        size_old  = 0, size_new = 0;
+  UINT64        mtime_old = 0, mtime_new = 0;
+  UINT64        ctime_old = 0, ctime_new = 0;
+  char          tmp[128];
+  char          msg[2*SH_BUFSIZE];
+  char timstr_o[32];
+  char timstr_n[32];
+
+  static int is_init = 0;
+
+  SL_ENTER(_("sh_kern_check_internal"));
+
+  if (is_init == 0)
+    { 
+      if (sh.flag.checkSum != SH_CHECK_INIT && sh.flag.update != S_TRUE)
+	{
+	  if (0 == sh_kern_data_init()) {
+	    is_init = 1;
+	  } else {
+	    sh_error_handle (ShKernSeverity, FIL__, __LINE__, status, 
+			     MSG_E_SUBGEN,
+			     _("could not initialize - switching off"),
+			     _("kern_check_internal") );
+	    ShKernActive = S_FALSE;
+	    SL_RETURN( (-1), _("sh_kern_check_internal"));
+	  }
+	}
+      else if ((sh.flag.checkSum == SH_CHECK_INIT ||
+		sh.flag.checkSum == SH_CHECK_CHECK) && 
+	       (sh.flag.update == S_TRUE))
+	{	
+	  if (0 == sh_kern_data_init()) {
+	    is_init = 1;
+	  } else {
+	    sh_error_handle (ShKernSeverity, FIL__, __LINE__, status, 
+			     MSG_E_SUBGEN,
+			     _("no or incomplete data in baseline database"),
+			     _("kern_check_internal") );
+	  }
+	}
+    }
+
+  /* defined, but not used
+   */
+  ShKernDelay    = 0;
+   
+  list[0].n_name = "_sysent";
+  list[1].n_name = NULL;
+
+  kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
+  if (!kd)
+    {
+      sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
+		       errbuf,
+		       _("kvm_openfiles") );
+      SL_RETURN( (-1), _("sh_kern_check_internal"));
+    }
+
+  i = kvm_nlist(kd, list);
+  if (i == -1)
+    {
+      sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
+		       kvm_geterr(kd),
+		       _("kvm_nlist (_sysent)") );
+      kvm_close(kd);
+      SL_RETURN( (-1), _("sh_kern_check_internal"));
+    }
+
+  sys_list = SH_ALLOC((SYS_MAXSYSCALL+1) * sizeof(struct nlist));
+
+  for (i = 0; i < SH_MAXCALLS; ++i)
+    sys_list[i].n_name = sh_syscalls[i].name;
+  sys_list[SH_MAXCALLS].n_name = NULL;
+
+  i = kvm_nlist(kd, sys_list);
+  if (i == -1)
+    {
+      sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
+		       kvm_geterr(kd),
+		       _("kvm_nlist (syscalls)") );
+      kvm_close(kd);
+      SH_FREE(sys_list);
+      SL_RETURN( (-1), _("sh_kern_check_internal"));
+    }
+  else if (i > 0)
+    {
+      sl_snprintf(tmp, 128,
+                  _("%d invalid syscalls"), i);
+      /*
+      for (i = 0; i < SH_MAXCALLS; ++i) {
+        if (sys_list[i].n_type == 0 && sys_list[i].n_value == 0)
+          fprintf(stderr, "invalid: [%3d] %s\n", i, sh_syscalls[i].name);
+      }
+      */
+      sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, status, MSG_E_SUBGEN,
+                       tmp,
+                       _("kvm_nlist (syscalls)") );
+    }
+
+  /* Check the individual syscalls
+   *
+   * Stored(old) is sh_syscalls[] array.
+   */
+  if (sh.flag.checkSum == SH_CHECK_INIT)
+    {
+      for (i = 0; i < SH_MAXCALLS; ++i) 
+	{
+	  if (sh_syscalls[i].name == NULL)
+	    {
+	      sl_snprintf(tmp, 128, 
+			  _("too few entries in sh_syscalls[]: have %d, expect %d"), 
+			  i, SH_MAXCALLS);
+
+	      sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
+			       tmp,
+			       _("sh_kern_check_internal") );
+	      break;
+	    }
+
+	  /* read address of syscall from sysent table
+	   */
+	  offset = list[0].n_value + (i*sizeof(struct sysent));
+	  if (kvm_read(kd, offset, &sy, sizeof(struct sysent)) < 0)
+	    {
+	      sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
+			       kvm_geterr(kd),
+			       _("kvm_read (syscall table)") );
+	      kvm_close(kd);
+	      SH_FREE(sys_list);
+	      SL_RETURN( (-1), _("sh_kern_check_internal"));
+	    }
+	  syscall_addr = (unsigned long) sy.sy_call;
+	  store0 = syscall_addr;
+	  
+	  /* read the syscall code
+	   */
+	  if(kvm_read(kd, (unsigned int) sy.sy_call, &(syscall_code[0]), 
+		      2 * sizeof(unsigned int)) < 0)
+	    {
+	      sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
+			       kvm_geterr(kd),
+			       _("kvm_read (syscall code)") );
+	      kvm_close(kd);
+	      SH_FREE(sys_list);
+	      SL_RETURN( (-1), _("sh_kern_check_internal"));
+	    }
+	  store1 = syscall_code[0]; store2 = syscall_code[1];
+	  
+	  sh_kern_db_syscall (i, _("syscall"), 
+			      NULL, &store0, &store1, &store2,
+			      0, 0);
+	}
+    }
+
+  if ((sh.flag.checkSum != SH_CHECK_INIT) || 
+      (sh.flag.update == S_TRUE && is_init == 1))
+    {
+      for (i = 0; i < SH_MAXCALLS; ++i)
+	{
+	  if (sh_syscalls[i].name == NULL)
+	    {
+	      sl_snprintf(tmp, 128, 
+			  _("too few entries in sh_syscalls[]: have %d, expect %d"), 
+			  i, SH_MAXCALLS);
+
+	      sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
+			       tmp,
+			       _("sh_kern_check_internal") );
+	      break;
+	    }
+	  
+	  /* read address of syscall from sysent table
+	   */
+	  offset = list[0].n_value + (i*sizeof(struct sysent));
+	  if (kvm_read(kd, offset, &sy, sizeof(struct sysent)) < 0)
+	    {
+	      sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
+			       kvm_geterr(kd),
+			       _("kvm_read (syscall table)") );
+	      kvm_close(kd);
+	      SH_FREE(sys_list);
+	      SL_RETURN( (-1), _("sh_kern_check_internal"));
+	    }
+	  syscall_addr = (unsigned long) sy.sy_call;
+	  
+	  if (sh_syscalls[i].addr != syscall_addr)
+	    {
+#ifdef SH_USE_XML
+	      sl_snprintf(tmp, 128, "path=\"K_%s_%04d\" ", 
+			  _("syscall"), i);
+#else
+	      sl_snprintf(tmp, 128, "path=<K_%s_%04d>, ", 
+			  _("syscall"), i);
+#endif
+	      sl_strlcpy(msg, tmp, SH_BUFSIZE);
+
+	      size_old = sh_syscalls[i].addr; 
+	      size_new = syscall_addr;
+	      sl_snprintf(tmp, 128, sh_hash_size_format(),
+			  size_old, size_new);
+	      sl_strlcat(msg, tmp, SH_BUFSIZE);
+ 
+	      sh_error_handle (ShKernSeverity, FIL__, __LINE__, 
+			       status, MSG_KERN_SYSCALL,
+			       /*
+			       syscall_addr,
+			       sh_syscalls[i].addr,
+			       */
+			       i, _(sh_syscalls[i].name),
+			       msg);
+	      sh_syscalls[i].addr = syscall_addr;
+	    }
+	  else
+	    {    
+	      /* read the syscall code
+	       */
+	      if(kvm_read(kd, (unsigned int) sy.sy_call, &(syscall_code[0]), 
+			  2 * sizeof(unsigned int)) < 0)
+		{
+		  sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
+				   kvm_geterr(kd),
+				   _("kvm_read (syscall code)") );
+		  kvm_close(kd);
+		  SH_FREE(sys_list);
+		  SL_RETURN( (-1), _("sh_kern_check_internal"));
+		}
+	      
+	      if (sh_syscalls[i].code[0] != syscall_code[0] || 
+		  sh_syscalls[i].code[1] != syscall_code[1])
+		{
+		  mtime_old = sh_syscalls[i].code[0];
+		  mtime_new = syscall_code[0];
+		  ctime_old = sh_syscalls[i].code[1];
+		  ctime_new = syscall_code[1];
+
+#ifdef SH_USE_XML
+		  sl_snprintf(tmp, 128, "path=\"K_%s_%04d\" ", 
+			      _("syscall"), i);
+#else
+		  sl_snprintf(tmp, 128, "path=<K_%s_%04d>, ", 
+			      _("syscall"), i);
+#endif
+		  sl_strlcpy(msg, tmp, SH_BUFSIZE);
+
+		  sl_strlcpy (timstr_o, sh_unix_gmttime (ctime_old), 32);
+		  sl_strlcpy (timstr_n, sh_unix_gmttime (ctime_new), 32);
+#ifdef SH_USE_XML
+		  sl_snprintf(tmp, 128, 
+			      _("ctime_old=\"%s\" ctime_new=\"%s\" "), 
+			      timstr_o, timstr_n);
+#else
+		  sl_snprintf(tmp, 128, 
+			      _("ctime_old=<%s>, ctime_new=<%s>, "), 
+			      timstr_o, timstr_n);
+#endif
+		  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+		  sl_strlcpy (timstr_o, sh_unix_gmttime (mtime_old), 32);
+		  sl_strlcpy (timstr_n, sh_unix_gmttime (mtime_new), 32);
+#ifdef SH_USE_XML
+		  sl_snprintf(tmp, 128, 
+			      _("mtime_old=\"%s\" mtime_new=\"%s\" "), 
+			      timstr_o, timstr_n);
+#else
+		  sl_snprintf(tmp, 128, 
+			      _("mtime_old=<%s>, mtime_new=<%s> "), 
+			      timstr_o, timstr_n);
+#endif
+		  sl_strlcat(msg, tmp, SH_BUFSIZE); 
+
+		  sh_error_handle (ShKernSeverity, FIL__, __LINE__, 
+				   status, MSG_KERN_SYSCALL,
+				   /*
+				   syscall_code[0],  syscall_code[1],
+				   sh_syscalls[i].code[0], sh_syscalls[i].code[1],
+				   */
+				   i, _(sh_syscalls[i].name),
+				   msg);
+		  sh_syscalls[i].code[0] = syscall_code[0];
+		  sh_syscalls[i].code[1] = syscall_code[1];
+		}
+	    }
+	}
+    }
+  SH_FREE(sys_list);
+  if(kvm_close(kd) < 0)
+    {
+      sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
+                       kvm_geterr(kd),
+                       _("kvm_close") );
+      exit(EXIT_FAILURE);
+    }
+
+  SL_RETURN( (0), _("sh_kern_check_internal"));
+}
+
+#endif
+
+/*************
+ *
+ * module init
+ *
+ *************/
+#if defined(HOST_IS_LINUX)
+#include <sys/utsname.h>
+#endif
+
+static int AddressReconf = 0;
+
+int sh_kern_init ()
+{
+#if defined(HOST_IS_LINUX)
+  struct utsname buf;
+  char         * str;
+#endif
+
+  SL_ENTER(_("sh_kern_init"));
+  if (ShKernActive == S_FALSE)
+    SL_RETURN( (-1), _("sh_kern_init"));
+
+#if defined(HOST_IS_LINUX)
+  uname(&buf);
+
+  if ((AddressReconf < 5) && (0 != strcmp(SH_KERNEL_VERSION, buf.release)))
+    {
+      str = SH_ALLOC(256);
+      sl_snprintf(str, 256, 
+		  "Compiled for kernel %s, but current kernel is %s, and kernel addresses have not been re-configured",
+		  SH_KERNEL_VERSION, buf.release);
+      sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, EINVAL, MSG_E_SUBGEN,
+		       str,
+		       _("kern_check") );
+      SH_FREE(str);
+      ShKernActive = S_FALSE;
+      SL_RETURN( (-1), _("sh_kern_init"));
+    }
+#endif
+
+  lastcheck  = time (NULL);
+  if (sh.flag.checkSum != SH_CHECK_INIT)
+    {
+      sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
+		       _("Checking kernel syscalls"),
+		       _("kern_check") );
+    }
+  sh_kern_check_internal ();
+  SL_RETURN( (0), _("sh_kern_init"));
+}
+
+/*************
+ *
+ * module cleanup
+ *
+ *************/
+int sh_kern_end ()
+{
+  return (0);
+}
+
+
+/*************
+ *
+ * module timer
+ *
+ *************/
+int sh_kern_timer (time_t tcurrent)
+{
+  if (ShKernActive == S_FALSE)
+    return 0;
+
+  if ((int) (tcurrent - lastcheck) >= ShKernInterval)
+    {
+      lastcheck  = tcurrent;
+      return (-1);
+    }
+  return 0;
+}
+
+/*************
+ *
+ * module check
+ *
+ *************/
+int sh_kern_check ()
+{
+  sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, EINVAL, MSG_E_SUBGEN,
+		   _("Checking kernel syscalls"),
+		   _("kern_check") );
+  return (sh_kern_check_internal ());
+}
+
+/*************
+ *
+ * module setup
+ *
+ *************/
+
+int sh_kern_set_severity  (char * c)
+{
+  char tmp[32];
+  tmp[0] = '='; tmp[1] = '\0';
+  sl_strlcat (tmp, c, 32);
+  sh_error_set_level (tmp, &ShKernSeverity);
+  return 0;
+}
+
+int sh_kern_set_timer (char * c)
+{
+  long val;
+
+  SL_ENTER(_("sh_kern_set_timer"));
+
+  val = strtol (c, (char **)NULL, 10);
+  if (val <= 0)
+    sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
+                      _("kern timer"), c);
+
+  val = (val <= 0 ? 60 : val);
+
+  ShKernInterval = (time_t) val;
+  SL_RETURN( 0, _("sh_kern_set_timer"));
+}
+
+int sh_kern_set_activate (char * c)
+{
+  int i;
+  SL_ENTER(_("sh_kern_set_activate"));
+  i = sh_util_flagval(c, &ShKernActive);
+  SL_RETURN(i, _("sh_kern_set_activate"));
+}
+
+int sh_kern_set_idt (char * c)
+{
+  int i;
+  SL_ENTER(_("sh_kern_set_idt"));
+  i = sh_util_flagval(c, &ShKernIDT);
+  SL_RETURN(i, _("sh_kern_set_idt"));
+}
+
+int sh_kern_set_sc_addr (char * c)
+{
+  char * endptr;
+  unsigned long value;
+
+  SL_ENTER(_("sh_kern_set_sc_addr"));
+  errno = 0;
+  value = strtoul(c, &endptr, 16);
+  if ((ULONG_MAX == value) && (errno == ERANGE))
+    {
+      SL_RETURN((-1), _("sh_kern_set_sc_addr"));
+    }
+  if ((*c == '\0') || (*endptr != '\0'))
+    {
+      SL_RETURN((-1), _("sh_kern_set_sc_addr"));
+    }
+  system_call_addr = value;
+  ++AddressReconf;
+  SL_RETURN((0), _("sh_kern_set_sc_addr"));
+}
+
+int sh_kern_set_sct_addr (char * c)
+{
+  char * endptr;
+  unsigned long value;
+
+  SL_ENTER(_("sh_kern_set_sct_addr"));
+  errno = 0;
+  value = strtoul(c, &endptr, 16);
+  if ((ULONG_MAX == value) && (errno == ERANGE))
+    {
+      SL_RETURN((-1), _("sh_kern_set_sct_addr"));
+    }
+  if ((*c == '\0') || (*endptr != '\0'))
+    {
+      SL_RETURN((-1), _("sh_kern_set_sct_addr"));
+    }
+  kaddr = (unsigned int) value;
+  ++AddressReconf;
+  SL_RETURN((0), _("sh_kern_set_sct_addr"));
+}
+
+int sh_kern_set_proc_root (char * c)
+{
+  char * endptr;
+  unsigned long value;
+
+  SL_ENTER(_("sh_kern_set_proc_root"));
+  errno = 0;
+  value = strtoul(c, &endptr, 16);
+  if ((ULONG_MAX == value) && (errno == ERANGE))
+    {
+      SL_RETURN((-1), _("sh_kern_set_proc_root"));
+    }
+  if ((*c == '\0') || (*endptr != '\0'))
+    {
+      SL_RETURN((-1), _("sh_kern_set_proc_root"));
+    }
+  
+  proc_root = value;
+  ++AddressReconf;
+  SL_RETURN((0), _("sh_kern_set_proc_root"));
+}
+
+int sh_kern_set_proc_root_iops (char * c)
+{
+  char * endptr;
+  unsigned long value;
+
+  SL_ENTER(_("sh_kern_set_proc_root_iops"));
+  errno = 0;
+  value = strtoul(c, &endptr, 16);
+  if ((ULONG_MAX == value) && (errno == ERANGE))
+    {
+      SL_RETURN((-1), _("sh_kern_set_proc_root_iops"));
+    }
+  if ((*c == '\0') || (*endptr != '\0'))
+    {
+      SL_RETURN((-1), _("sh_kern_set_proc_root_iops"));
+    }
+  
+  proc_root_iops = value;
+  ++AddressReconf;
+  SL_RETURN((0), _("sh_kern_set_proc_root_iops"));
+}
+
+int sh_kern_set_proc_root_lookup (char * c)
+{
+  char * endptr;
+  unsigned long value;
+
+  SL_ENTER(_("sh_kern_set_proc_root_lookup"));
+  errno = 0;
+  value = strtoul(c, &endptr, 16);
+  if ((ULONG_MAX == value) && (errno == ERANGE))
+    {
+      SL_RETURN((-1), _("sh_kern_set_proc_root_lookup"));
+    }
+  if ((*c == '\0') || (*endptr != '\0'))
+    {
+      SL_RETURN((-1), _("sh_kern_set_proc_root_lookup"));
+    }
+  proc_root_lookup = value;
+  ++AddressReconf;
+  SL_RETURN((0), _("sh_kern_set_proc_root_lookup"));
+}
+
+#endif
+
+/* #ifdef SH_USE_UTMP */
+#endif
+
+
+
Index: trunk/src/sh_log_check.c
===================================================================
--- trunk/src/sh_log_check.c	(revision 591)
+++ 	(revision )
@@ -1,1523 +1,0 @@
-
-#include "config_xor.h"
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-
-#ifdef HAVE_DIRENT_H
-#include <dirent.h>
-#define NAMLEN(dirent) sl_strlen((dirent)->d_name)
-#else
-#define dirent direct
-#define NAMLEN(dirent) (dirent)->d_namlen
-#ifdef HAVE_SYS_NDIR_H
-#include <sys/ndir.h>
-#endif
-#ifdef HAVE_SYS_DIR_H
-#include <sys/dir.h>
-#endif
-#ifdef HAVE_NDIR_H
-#include <ndir.h>
-#endif
-#endif
-
-#if !defined(O_NONBLOCK)
-#if defined(O_NDELAY)
-#define O_NONBLOCK  O_NDELAY
-#else
-#define O_NONBLOCK  0
-#endif
-#endif
-
-#ifdef USE_LOGFILE_MONITOR
-
-#undef  FIL__
-#define FIL__  _("sh_log_check.c")
-
-/* Debian/Ubuntu: libpcre2-dev */
-#define PCRE2_CODE_UNIT_WIDTH 8
-#ifdef HAVE_PCRE2_PCRE2_H
-#include <pcre2/pcre2.h>
-#else
-#include <pcre2.h>
-#endif
-
-#include "samhain.h"
-#include "sh_pthread.h"
-#include "sh_utils.h"
-#include "sh_unix.h"
-#include "sh_string.h"
-#include "sh_log_check.h"
-#include "sh_log_evalrule.h"
-#include "sh_log_correlate.h"
-#include "sh_log_mark.h"
-#include "sh_log_repeat.h"
-#include "sh_extern.h"
-
-/* List of supported logfile types, format is
- * { 
- *   "TYPE_CODE", 
- *   Reader_Callback_Function, 
- *   Parser_Callback_function,
- *   Evaluate_Callback_Function 
- * }
- * If Reader_Callback_Function is NULL, the default (line-oriented
- * text file) reader is used.
- */
-struct sh_logfile_type sh_logtypes_def[] = {
-    {  "SYSLOG", NULL,            sh_parse_syslog, NULL },
-    {  "SAMBA",  sh_read_samba,   sh_parse_samba,  NULL },
-    {  "APACHE", NULL,            sh_parse_apache, sh_eval_fileinfo_apache },
-#if defined(HAVE_SYS_ACCT_H)
-    {  "PACCT",  sh_read_pacct,   sh_parse_pacct,  NULL },
-#endif
-    {  "SHELL",  sh_read_shell,   sh_parse_shell,  NULL },
-};
-
-/* -------------------------- Internal Stuff -------------------------- */
-
-struct logfile_record {
-  dev_t  device_id;
-  ino_t  inode;
-  fpos_t offset;
-};
-
-static int    do_checkpoint_cleanup = S_FALSE;
-
-static char * save_dir = NULL;
-
-static const char * get_save_dir(void)
-{
-  int retval;
-
-  if (!save_dir)
-    {
-      save_dir = sh_util_strdup(DEFAULT_DATAROOT);
-
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      retval = tf_trust_check (save_dir, SL_YESPRIV);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-
-      if (retval != 0)
-	{
-	  return(NULL);
-	}
-    }
-  return save_dir;
-}
-
-static void clean_dir()
-{
-  DIR * dir;
-  struct dirent * entry;
-
-  const char * dirpath;
-
-  if (S_FALSE == do_checkpoint_cleanup)
-    return;
-
-  dirpath = get_save_dir();
-
-  if (dirpath)
-    {
-      dir = opendir(dirpath);
-      if (dir)
-	{
-	  unsigned long a, b;
-	  int retval;
-	  char c;
-	  size_t dlen = strlen(dirpath) + 1;
-	  time_t now  = time(NULL);
-
-	  while (NULL != (entry = readdir(dir)))
-	    {
-	      retval = sscanf(entry->d_name, "%lu_%lu%c", &a, &b, &c);
-
-	      if (2 == retval)
-		{
-		  struct stat buf;
-		  char * path;
-		  size_t  plen = strlen(entry->d_name) + 1;
-
-		  if (S_TRUE == sl_ok_adds(plen, dlen))
-		    {
-		      plen += dlen;
-		      path = SH_ALLOC(plen);
-		      (void) sl_snprintf(path, plen, "%s/%s", 
-					 dirpath,entry->d_name);
-
-		      if (0 == retry_lstat(FIL__, __LINE__, path, &buf) &&
-			  S_ISREG(buf.st_mode))
-			{
-			  if (buf.st_mtime < now && 
-			      (now - buf.st_mtime) > 2592000) /* 30 days */
-			    {
-			      if (0 == tf_trust_check (path, SL_YESPRIV))
-				{
-				  unlink(path);
-				}
-			    }
-			}
-		    }
-		}
-	    }
-	  closedir(dir);
-	}
-    }
-}
-
-static char * build_path (struct sh_logfile * record)
-{
-  size_t plen;
-  char * path = NULL;
-  const char * dir = get_save_dir();
-
-  if (dir)
-    {
-      plen = strlen(dir);
-
-      if (S_TRUE == sl_ok_adds(plen, 130))
-	{
-	  plen += 130; /* 64 + 64 + 2 */
-	  path = SH_ALLOC(plen);
-	  (void) sl_snprintf(path, plen, "%s/%lu_%lu", dir,
-			     (unsigned long) record->device_id, 
-			     (unsigned long) record->inode);
-	}
-    }
-
-  return path;
-}
-
-static void save_pos (struct sh_logfile * record)
-{
-  char * path;
-  FILE * fd;
-  mode_t mask;
-  struct logfile_record save_rec;
-
-  path = build_path(record);
-
-  if (path)
-    {
-      if (0 != sh_unix_check_piddir (path))
-	{
-	  SH_FREE(path);
-	  return;
-	}
-
-      mask = umask(S_IWGRP | S_IWOTH);
-      fd = fopen(path, "wb");
-      (void) umask(mask);
-
-      if (fd)
-	{
-	  save_rec.device_id = record->device_id;
-	  save_rec.inode     = record->inode;
-	  memcpy(&(save_rec.offset), &(record->offset), sizeof(fpos_t));
-	  if (1 != fwrite(&save_rec, sizeof(struct logfile_record), 1, fd))
-	    {
-	      (void) sl_fclose(FIL__, __LINE__, fd);
-	      (void) remove(path);
-	    }
-	  else
-	    {
-	      (void) sl_fclose(FIL__, __LINE__, fd);
-	    }
-	}
-      SH_FREE(path);
-    }
-  return;
-}
-
-static int read_pos (struct sh_logfile * record)
-{
-  int    retval = 0;
-  char * path;
-  FILE * fd;
-  struct logfile_record save_rec;
-
-  path = build_path(record);
-
-  if (path)
-    {
-      fd = fopen(path, "rb");
-      if (fd)
-	{
-	  if (1 == fread(&save_rec, sizeof(struct logfile_record), 1, fd))
-	    {
-	      if (save_rec.device_id == record->device_id &&
-		  save_rec.inode     == record->inode)
-		{
-		  memcpy(&(record->offset),&(save_rec.offset),sizeof(fpos_t));
-		  retval = 1;
-		}
-	    }
-	  (void) sl_fclose(FIL__, __LINE__, fd);
-	}
-      SH_FREE(path);
-    }
-  return retval;
-}
-
-/*@null@*/ static struct sh_logfile * sh_watched_logs = NULL;
-
-int sh_add_watch (const char * str)
-{
-  char * filename;
-
-  unsigned int    i;
-  unsigned int    defsize;
-  struct sh_logfile_type * log_type = NULL;
-  struct sh_logfile * thisfile;
-  struct stat buf;
-
-  unsigned int nfields = 3; /* logtype:path[:regex] */
-  size_t       lengths[3];
-  char *       new = sh_util_strdup(str);
-  char **      splits = split_array(new, &nfields, ':', lengths);
-
-  if (nfields < 2 || (lengths[0] == 0 || lengths[0] >= SH_MAX_LCODE_SIZE || lengths[1] == 0))
-    {
-      sh_string * msg =  sh_string_new(0);
-      sh_string_add_from_char(msg, _("Format error: "));
-      sh_string_add_from_char(msg, str);
-      
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		      sh_string_str(msg),
-		      _("sh_add_watch"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      sh_string_destroy(&msg);
-
-      SH_FREE(new);
-      return -2;
-    }
-
-  defsize = 
-    (unsigned int) (sizeof(sh_logtypes_def)/sizeof(struct sh_logfile_type));
-
-  for (i = 0; i < defsize; ++i)
-    {
-      if (0 == strcmp(splits[0], sh_logtypes_def[i].code))
-	{
-	  log_type = &(sh_logtypes_def[i]);
-	  break;
-	}
-    }
-
-  if (log_type == NULL)
-    {
-      sh_string * msg =  sh_string_new(0);
-      sh_string_add_from_char(msg, _("Unsupported log type: "));
-      sh_string_add_from_char(msg, splits[0]);
-      
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		      sh_string_str(msg),
-		      _("sh_add_watch"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      sh_string_destroy(&msg);
-
-      SH_FREE(new);
-      return -3;
-    }
-
-  if (splits[1][0] != '/' && 0 != strcmp(splits[0], _("SHELL")))
-    {
-      sh_string * msg =  sh_string_new(0);
-      sh_string_add_from_char(msg, _("Logfile path not absolute: "));
-      sh_string_add_from_char(msg, splits[1]);
-      
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		      sh_string_str(msg),
-		      _("sh_add_watch"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      sh_string_destroy(&msg);
-
-      SH_FREE(new);
-      return -4;
-    }
-
-  filename = /*@i@*/sh_util_strdup(splits[1]);
-  thisfile = SH_ALLOC(sizeof(struct sh_logfile));
-
-  thisfile->filename     = filename;
-  if (0 == strcmp(splits[0], _("SHELL")))
-    thisfile->flags        = SH_LOGFILE_NOFILE;
-  else
-    thisfile->flags        = SH_LOGFILE_REWIND;
-  thisfile->inode        = 0;
-  thisfile->device_id    = 0;
-  thisfile->fp           = NULL;
-  if (log_type->get_record)
-    thisfile->get_record   = log_type->get_record;
-  else
-    thisfile->get_record   = sh_default_reader;
-  thisfile->parse_record = log_type->parse_record;
-
-  /* An optional regex for parsing the file. The result
-   * 'fileinfo' should contain info about host/time position.
-   */
-  if (log_type->eval_fileinfo)
-    {
-      if (nfields == 3 && lengths[2] > 0)
-	{
-	  thisfile->fileinfo     = log_type->eval_fileinfo(splits[2]);
-
-	  if (thisfile->fileinfo == NULL)
-	    {
-	      sh_string * msg =  sh_string_new(0);
-	      sh_string_add_from_char(msg, _("Logfile format description not recognized: "));
-	      sh_string_add_from_char(msg, splits[2]);
-	      
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-			      sh_string_str(msg),
-			      _("sh_add_watch"));
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	      sh_string_destroy(&msg);
-
-	      SH_FREE(filename);
-	      SH_FREE(thisfile);
-	      SH_FREE(new);
-	      return -1;
-	    }
-	}
-      else
-	{
-	  sh_string * msg =  sh_string_new(0);
-	  sh_string_add_from_char(msg, _("Logfile format description missing: "));
-	  sh_string_add_from_char(msg, splits[1]);
-	  
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-			  sh_string_str(msg),
-			  _("sh_add_watch"));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  sh_string_destroy(&msg);
-
-	  SH_FREE(filename);
-	  SH_FREE(thisfile);
-	  SH_FREE(new);
-	  return -1;
-	}
-    }
-  else
-    {
-      thisfile->fileinfo     = NULL;
-    }
-  thisfile->next         = sh_watched_logs;
-
-  /* Try reading saved offset. On success clear rewind flag.
-   */
-  if ((thisfile->flags & SH_LOGFILE_NOFILE) == 0)
-    {
-      if (0 == stat(thisfile->filename, &buf))
-	{
-	  if (S_ISREG(buf.st_mode)
-#ifdef S_ISLNK
-	      || S_ISLNK(buf.st_mode)
-#endif 
-	      )
-	    {
-	      thisfile->inode     = buf.st_ino;
-	      thisfile->device_id = buf.st_dev;
-	      
-	      if (0 != read_pos(thisfile))
-		{
-		  thisfile->flags &= ~SH_LOGFILE_REWIND;
-		}
-	    }
-	  else if (S_ISFIFO(buf.st_mode))
-	    {
-	      thisfile->inode      = buf.st_ino;
-	      thisfile->device_id  = buf.st_dev;
-	      thisfile->flags     |= SH_LOGFILE_PIPE;
-	    }
-	}
-      else
-	{
-	  sh_string * msg =  sh_string_new(0);
-	  sh_string_add_from_char(msg, _("Logfile is not a regular file, link, or named pipe: "));
-	  sh_string_add_from_char(msg, splits[1]);
-	  
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-			  sh_string_str(msg),
-			  _("sh_add_watch"));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  sh_string_destroy(&msg);
-	  
-	  SH_FREE(filename);
-	  SH_FREE(thisfile);
-	  SH_FREE(new);
-	  return -1;
-	}
-    }
-
-  sh_watched_logs        = thisfile;
-
-  SH_FREE(new);
-  return 0;
-}
-
-void sh_dump_watches()
-{
-  struct sh_logfile * thisfile;
-
-  while (sh_watched_logs)
-    {
-      thisfile        = sh_watched_logs;
-      sh_watched_logs = thisfile->next;
-
-      if ((thisfile->flags & SH_LOGFILE_NOFILE) == 0 &&
-	  (thisfile->flags & SH_LOGFILE_PIPE) == 0)
-	{
-	  save_pos(thisfile);
-	}
-
-      if ((thisfile->flags & SH_LOGFILE_NOFILE) == 0)
-	{
-	  if (thisfile->fp)
-	    sl_fclose(FIL__, __LINE__, thisfile->fp);
-	}
-
-      if (thisfile->filename)
-	SH_FREE(thisfile->filename);
-      SH_FREE(thisfile);
-    }
-  return;
-}
-
-/* This variable is not used anywhere. It only exist
- * to assign &new to them, which keeps gcc from
- * putting it into a register, and avoids the 'clobbered
- * by longjmp' warning. And no, 'volatile' proved insufficient.
- */
-void * sh_dummy_502_thisfile = NULL;
-
-void sh_check_watches()
-{
-  struct sh_logrecord * logrecord;
-  struct sh_logfile * thisfile = sh_watched_logs;
-  sh_string * record = sh_string_new(0);
-  char * tmp;
-
-  /* Take the address to keep gcc from putting them into registers. 
-   * Avoids the 'clobbered by longjmp' warning. 
-   */
-  sh_dummy_502_thisfile = (void*) &thisfile;
-
-  while (thisfile)
-    {
-      volatile size_t count = 0;
-
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      tmp = sh_util_safe_name (thisfile->filename);
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_LOGMON_CHKS,
-		      tmp);
-      SH_FREE(tmp);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-
-      for (;;) {
-
-	record = thisfile->get_record(record, thisfile);
-
-	if (record)
-	  {
-	    logrecord = thisfile->parse_record(record, thisfile->fileinfo);
-	    ++count;
-
-	    if (logrecord)
-	      {
-		logrecord->filename = thisfile->filename;
-		
-		/* Don't report if 'init', just set file pointer
-		 */
-		if (sh.flag.checkSum != SH_CHECK_INIT)
-		  {
-		    sh_eval_process_msg(logrecord);
-		  }
-
-		if (logrecord->message) 
-		  sh_string_destroy(&(logrecord->message));
-		if (logrecord->host)
-		  sh_string_destroy(&(logrecord->host));
-		if (logrecord->timestr)
-		  sh_string_destroy(&(logrecord->timestr));
-		SH_FREE(logrecord);
-	      }
-	  }
-	else
-	  {
-	    record = sh_string_new(0);
-	    break;
-	  }
-      }
-
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      tmp = sh_util_safe_name (thisfile->filename);
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_LOGMON_CHKE,
-		      tmp, (unsigned long)count);
-      SH_FREE(tmp);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-
-      thisfile = thisfile->next;
-    }
-  sh_string_destroy(&record);
-  return;
-}
-
-/********************************************************
- * Search rotated logfile
- */
-extern char * sh_rotated_log_search(const char * path, struct stat * buf);
-
-
-/* Open file, position at stored offset
- */
-int sh_open_for_reader (struct sh_logfile * logfile)
-{
-  struct stat buf;
-  sh_string * filename;
-
-  /* check whether file exists, get inode to check for
-   * logfile rotation
-   */
-  if (0 != retry_stat(FIL__, __LINE__, logfile->filename, &buf))
-    {
-      char * tmp;
-
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      tmp = sh_util_safe_name (logfile->filename);
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_LOGMON_MISS,
-		      tmp);
-      SH_FREE(tmp);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-
-      memset (&(logfile->offset), 0, sizeof(fpos_t));
-      logfile->flags |= SH_LOGFILE_REWIND;
-      return 0;
-    }
-
-  filename = sh_string_new(0);
-  (void) sh_string_set_from_char (filename, logfile->filename);
-
-  /* detect and handle logfile rotation
-   */
-  if (logfile->inode != buf.st_ino && 
-      logfile->inode != 0 &&
-      !S_ISFIFO(buf.st_mode))
-    {
-      /* Case 1) We have dealt with the moved file already.
-       *         Clear the moved flag, set the rewind flag,
-       *         fix logfile->inode.
-       */
-      if ((logfile->flags & SH_LOGFILE_MOVED) != 0)
-	{
-	  /* done with rotated file, start with current file
-	   */
-	  memset (&(logfile->offset), 0, sizeof(fpos_t));
-	  logfile->flags    |= SH_LOGFILE_REWIND;
-	  logfile->flags    &= ~SH_LOGFILE_MOVED;
-	  logfile->inode     = buf.st_ino;
-	  logfile->device_id = buf.st_dev;
-	}
-
-      /* Case 2) Searching for rotated file. 
-       *         If found:     set the moved flag, fix path for fopen.
-       *         If not found: set the rewind flag, fix logfile->inode.
-       */
-      else
-	{
-	  char *oldfile = sh_rotated_log_search(logfile->filename, &buf);
-
-	  if (NULL != oldfile)
-	    {
-	      (void) sh_string_set_from_char (filename, oldfile);
-	      SH_FREE(oldfile);
-	      logfile->flags |= SH_LOGFILE_MOVED;
-	    }
-	  else
-	    {
-	      memset (&(logfile->offset), 0, sizeof(fpos_t));
-	      logfile->flags    |= SH_LOGFILE_REWIND;
-	      logfile->inode     = buf.st_ino;
-	      logfile->device_id = buf.st_dev;
-	    }
-	}
-    }
-
-  /* open file
-   */
-  if (!S_ISFIFO(buf.st_mode))
-    {
-      logfile->fp = fopen(filename->str, "r");
-    }
-  else
-    {
-      int fd_temp = open (filename->str, O_RDONLY|O_NONBLOCK);
-
-      if (fd_temp >= 0)
-	{
-	  logfile->fp = fdopen(fd_temp, "r");
-	}
-    }
-
-  if (!logfile->fp)
-    {
-      char * tmp;
-
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      tmp = sh_util_safe_name (logfile->filename);
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_LOGMON_EOPEN,
-		      tmp);
-      SH_FREE(tmp);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-
-      sh_string_destroy(&filename);
-      return 0;
-    }
-
-  sh_string_destroy(&filename);
-  
-  if ((logfile->flags & SH_LOGFILE_PIPE) == 0)
-    {
-      if ((logfile->flags & SH_LOGFILE_REWIND) != 0)
-	{
-	  rewind(logfile->fp);
-	  fgetpos(logfile->fp, &(logfile->offset));
-	  logfile->flags &= ~SH_LOGFILE_REWIND;
-	}
-      else
-	{
-	  /* file too short
-	   */
-	  if (0 != fsetpos(logfile->fp, &(logfile->offset)))
-	    {
-	      rewind(logfile->fp);
-	      fgetpos(logfile->fp, &(logfile->offset));
-	    }
-	}
-    }
-
-  return 1;
-}
-
-/******************************************************
- *  Default reader for ascii text files 
- */
-sh_string * sh_default_reader (sh_string * s, struct sh_logfile * logfile)
-{
-  volatile int         status;
-  char * tmp;
-
- start_read:
-
-  if (logfile->fp)
-    {
-      /* Result cannot be larger than 8192, thus cast is ok
-       */
-      status = (int) sh_string_read(s, logfile->fp, 8192);
-      if (status <= 0)
-	{
-	  fgetpos(logfile->fp, &(logfile->offset));
-	  sl_fclose(FIL__, __LINE__, logfile->fp);
-	  logfile->fp = NULL;
-	  sh_string_destroy(&s);
-	  if (status == 0 || (logfile->flags & SH_LOGFILE_PIPE) != 0)
-	    {
-	      return NULL;
-	    }
-
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  tmp = sh_util_safe_name (logfile->filename);
-	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_LOGMON_EREAD,
-			  tmp);
-	  SH_FREE(tmp);
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-
-	  return NULL;
-	}
-      return s;
-    }
-
-  if (0 != sh_open_for_reader(logfile))
-    goto start_read;
-
-  return NULL;
-}
-
-struct task_entry
-{
-  sh_tas_t task;
-  struct task_entry * next;
-};
-
-static struct task_entry * tasklist = NULL;
-
-static struct task_entry * task_find(FILE * fp)
-{
-  struct task_entry * entry = tasklist;
-  while (entry)
-    {
-      if (entry->task.pipe == fp)
-	return (entry);
-      entry = entry->next;
-    }
-  return NULL;
-}
- 
-static void task_remove(struct task_entry * task)
-{
-  struct task_entry * entry = tasklist;
-  struct task_entry * prev  = tasklist;
-
-  while (entry)
-    {
-      if (entry == task)
-	{
-	  if (entry == tasklist)
-	    {
-	      tasklist = entry->next;
-	      SH_FREE(entry);
-	      return;
-	    }
-	  else
-	    {
-	      prev->next = entry->next;
-	      SH_FREE(entry);
-	      return;
-	    }
-	}
-      prev  = entry;
-      entry = entry->next;
-    }
-  return;
-}
- 
-static void task_add(struct task_entry * entry)
-{
-  entry->next = tasklist;
-  tasklist    = entry;
-  return;
-}
- 
-sh_string * sh_command_reader (sh_string * s, struct sh_logfile * logfile)
-{
-  struct task_entry * entry;
-
-  volatile int        status;
-  char * tmp;
-
- start_read:
-
-  if (logfile->fp)
-    {
-      /* Result cannot be larger than 8192, thus cast is ok
-       */
-      status = (int) sh_string_read(s, logfile->fp, 8192);
-
-      if (status <= 0)
-	{
-	  entry = task_find(logfile->fp);
-	  sh_ext_pclose (&(entry->task));
-	  task_remove(entry);
-
-	  logfile->fp = NULL;
-	  sh_string_destroy(&s);
-
-	  if (status == 0)
-	    {
-	      return NULL;
-	    }
-
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  tmp = sh_util_safe_name (logfile->filename);
-	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_LOGMON_EREAD,
-			  tmp);
-	  SH_FREE(tmp);
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-
-	  return NULL;
-	}
-      return s;
-    }
-
-  entry = SH_ALLOC(sizeof(struct task_entry));
-
-  status = sh_ext_popen_init (&(entry->task), logfile->filename, NULL, NULL);
-  if (0 == status)
-    {
-      task_add(entry);
-      logfile->fp = entry->task.pipe;
-      goto start_read;
-    }
-  else
-    {
-      SH_FREE(entry);
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, status, MSG_E_SUBGEN, 
-                      _("Could not open pipe"), _("sh_command reader"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-    }
-
-  return NULL;
-}
-
-/******************************************************
- *  Reader for continued text files 
- */
-sh_string * sh_cont_reader (sh_string * s, struct sh_logfile * logfile, char*cont)
-{
-  int         status;
-  char      * tmp;
-  sh_string * str;
-  int         remain = 8192;
-  int         count  = 0;
-
-  if (!sh_string_truncate(s, 0))
-    return NULL;
-
- start_read:
-
-  if (logfile->fp)
-    {
-      str = sh_string_new(0);
-
-      /* Result cannot be larger than 8192, thus cast is ok
-       */
-      status = (int) sh_string_read(str, logfile->fp, 8192);
-
-      if (status > 0)
-	{
-	  
-	  do {
-	    s       = sh_string_add (s, str);
-	    count  += status;
-	    remain -= status;
-
-	    if (remain <= 0)
-	      {
-		return s;
-	      }
-
-	    status = (int) sh_string_read_cont(str, logfile->fp, count, cont);
-
-	    if (status == 0)
-	      {
-		return s;
-	      }
-	  }
-	  while (status > 0);
-	}
-
-      if (status <= 0)
-	{
-	  fgetpos(logfile->fp, &(logfile->offset));
-	  sl_fclose(FIL__, __LINE__, logfile->fp);
-	  logfile->fp = NULL;
-	  sh_string_destroy(&s);
-	  if (status == 0 || (logfile->flags & SH_LOGFILE_PIPE) != 0)
-	    {
-	      return NULL;
-	    }
-
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  tmp = sh_util_safe_name (logfile->filename);
-	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_LOGMON_EREAD,
-			  tmp);
-	  SH_FREE(tmp);
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-
-	  return NULL;
-	}
-
-      return s;
-    }
-
-  if (0 != sh_open_for_reader(logfile))
-    goto start_read;
-
-  return NULL;
-}
-
-/******************************************************
- *  Reader for binary files 
- */
-sh_string * sh_binary_reader (void * s, size_t size, 
-			      struct sh_logfile * logfile)
-{
-  size_t         status;
-
- start_read:
-
-  if (logfile->fp)
-    {
-
-      status = fread(s, size, 1, logfile->fp);
-
-      if (status != 1)
-	{
-	  memset(s, 0, size);
-	  if (ferror(logfile->fp) && (logfile->flags & SH_LOGFILE_PIPE) == 0)
-	    {
-	      char * tmp;
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      tmp = sh_util_safe_name (logfile->filename);
-	      sh_error_handle((-1), FIL__, __LINE__, errno, MSG_LOGMON_EREAD,
-			      tmp);
-	      SH_FREE(tmp);
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	    }
-	  fgetpos(logfile->fp, &(logfile->offset));
-	  sl_fclose(FIL__, __LINE__, logfile->fp);
-	  logfile->fp = NULL;
-	  return NULL;
-	}
-      return s;
-    }
-
-  if (0 != sh_open_for_reader(logfile))
-    goto start_read;
-
-  return NULL;
-}
-
-
-
-/**********************************************************
- *
- * Utilities
- *
- **********************************************************/
-
-/* Return current year, unless that would result
- * in a date far in the future. If that happens,
- * return last year.
- */
-static int year_guess (struct tm * btime)
-{
-  int           year;
-  struct tm     ts;
-  time_t        now    = time(NULL);
-  time_t        check;
-
-  memcpy(&ts, localtime(&now), sizeof(struct tm));
-  year = ts.tm_year;
-
-  /* Check result to detect year wrap
-   * (logfile entry from last year).
-   */
-  btime->tm_year = year;
-  check = mktime(btime);
-  if (check > (now + (86400*30)))
-    --year;
-
-  return year;
-}
-
-time_t conv_timestamp (struct tm * btime, 
-		       struct tm * old_tm, time_t * old_time)
-{
-  time_t timestamp;
-  long   offtime;
-
-  /* timestamp - mktime is slooow, thus cache result
-   */
-  if (btime->tm_isdst == old_tm->tm_isdst &&
-      btime->tm_year  == old_tm->tm_year  &&
-      btime->tm_mon   == old_tm->tm_mon   &&
-      btime->tm_mday  == old_tm->tm_mday)
-    {
-      offtime = 
-	(btime->tm_hour - old_tm->tm_hour) * 3600 +
-	(btime->tm_min  - old_tm->tm_min)  * 60   +
-	(btime->tm_sec  - old_tm->tm_sec);
-
-      *old_time += offtime;
-      memcpy(old_tm, btime, sizeof(struct tm));
-      timestamp = *old_time;
-    }
-  else
-    {
-      int year_btime = btime->tm_year;
-
-      if (btime->tm_year == 0)
-	btime->tm_year = year_guess(btime);
-      timestamp = mktime(btime);
-      btime->tm_year = year_btime;
-      *old_time  = timestamp;
-      memcpy(old_tm, btime, sizeof(struct tm));
-    }
-  return timestamp;
-}
-
-/*********************************************************
- *
- * MODULE STUFF
- *
- *********************************************************/
-#include "sh_modules.h"
-
-SH_MUTEX_STATIC(mutex_logmon_check, PTHREAD_MUTEX_INITIALIZER);
-
-static int ShLogmonActive        = S_FALSE;
-#define SH_LOGMON_INTERVAL 10
-static time_t sh_logmon_interval = SH_LOGMON_INTERVAL;
-
-int sh_log_check_init (struct mod_type * arg)
-{
-#if !defined(HAVE_PTHREAD)
-  (void) arg;
-#endif
-
-  if (ShLogmonActive == S_FALSE)
-    return SH_MOD_FAILED;
-#ifdef HAVE_PTHREAD
-  if (arg != NULL && arg->initval < 0 &&
-      (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
-    {
-      if (0 == sh_pthread_create(sh_threaded_module_run, (void *)arg))
-	return SH_MOD_THREAD;
-      else
-	return SH_MOD_FAILED;
-    }
-  else if (arg != NULL && arg->initval == SH_MOD_THREAD &&
-	   (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
-    {
-      return SH_MOD_THREAD;
-    }
-#endif
-  if (sh_watched_logs != NULL)
-    return 0;
-
-  return -1;
-}
-
-int sh_log_check_timer(time_t tcurrent) 
-{
-  static time_t lastcheck = 0;
-
-  SL_ENTER(_("sh_log_check_timer"));
-  if ((time_t) (tcurrent - lastcheck) >= sh_logmon_interval)
-    {
-      lastcheck  = tcurrent;
-      SL_RETURN((-1), _("sh_log_check_timer"));
-    }
-  SL_RETURN(0, _("sh_log_check_timer"));
-}
-
-
-int sh_log_check_check(void) 
-{
-  int status = 0;
-
-  SL_ENTER(_("sh_log_check_check"));
-
-  SH_MUTEX_LOCK(mutex_logmon_check);
-
-  status = 0;
-
-  if( ShLogmonActive != S_FALSE )
-    {
-      sh_check_watches();
-      sh_keep_match();
-      sh_log_mark_check();
-      clean_dir();
-    }
-  SH_MUTEX_UNLOCK(mutex_logmon_check);
-
-  SL_RETURN(status, _("sh_log_check_check"));
-}
-
-int sh_log_check_reconf(void) 
-{
-  int status = 0;
-
-  SL_ENTER(_("sh_log_check_check"));
-
-  SH_MUTEX_LOCK(mutex_logmon_check);
-
-  ShLogmonActive     = S_FALSE;
-  sh_logmon_interval = SH_LOGMON_INTERVAL;
-  sh_dump_watches();
-  sh_eval_cleanup();
-
-  SH_MUTEX_UNLOCK(mutex_logmon_check);
-
-  SL_RETURN(status, _("sh_log_check_check"));
-}
-
-int sh_log_check_cleanup(void) 
-{
-  sh_log_mark_destroy();
-  return sh_log_check_reconf();
-}
-
-/*********************  OPTIONS **********************/
-
-static int sh_logmon_set_active  (const char *str);
-static int sh_logmon_set_clean  (const char *str);
-static int sh_logmon_set_interval(const char *str);
-static int sh_logmon_add_watch (const char * str);
-static int sh_logmon_add_group (const char * str);
-static int sh_logmon_end_group (const char * str);
-static int sh_logmon_add_host  (const char * str);
-static int sh_logmon_end_host  (const char * str);
-static int sh_logmon_add_queue (const char * str);
-static int sh_logmon_add_rule  (const char * str);
-extern int sh_set_hidepid(const char *s);
-static int sh_logmon_set_save_dir(const char *s);
-
-sh_rconf sh_log_check_table[] = {
-    {
-        N_("logmonactive"),
-        sh_logmon_set_active,
-    },
-    {
-        N_("logmoninterval"),
-        sh_logmon_set_interval,
-    },
-    {
-        N_("logmonclean"),
-        sh_logmon_set_clean,
-    },
-    {
-        N_("logmonwatch"),
-        sh_logmon_add_watch,
-    },
-    {
-        N_("logmonqueue"),
-        sh_logmon_add_queue,
-    },
-    {
-        N_("logmongroup"),
-        sh_logmon_add_group,
-    },
-    {
-        N_("logmonendgroup"),
-        sh_logmon_end_group,
-    },
-    {
-        N_("logmonhost"),
-        sh_logmon_add_host,
-    },
-    {
-        N_("logmonendhost"),
-        sh_logmon_end_host,
-    },
-    {
-        N_("logmonrule"),
-        sh_logmon_add_rule,
-    },
-    {
-        N_("logmonhidepid"),
-        sh_set_hidepid,
-    },
-    {
-        N_("logmonsavedir"),
-        sh_logmon_set_save_dir,
-    },
-    {
-        N_("logmonmarkseverity"),
-        sh_log_set_mark_severity,
-    },
-    {
-        N_("logmonburstthreshold"),
-        sh_repeat_set_trigger,
-    },
-    {
-        N_("logmonburstqueue"),
-        sh_repeat_set_queue,
-    },
-    {
-        N_("logmonburstcron"),
-        sh_repeat_set_cron,
-    },
-    {
-        N_("logmondeadtime"),
-        sh_keep_deadtime,
-    },
-    {
-        NULL,
-        NULL
-    }
-};
-
-/* Decide if we're active.
- */
-static int sh_logmon_set_active(const char *str) 
-{
-  int value;
-    
-  SL_ENTER(_("sh_logmon_set_active"));
-
-  value = sh_util_flagval(str, &ShLogmonActive);
-
-  SL_RETURN((value), _("sh_logmon_set_active"));
-}
-
-/* Decide if we're active.
- */
-static int sh_logmon_set_clean(const char *str) 
-{
-  int value;
-    
-  SL_ENTER(_("sh_logmon_set_active"));
-
-  value = sh_util_flagval(str, &do_checkpoint_cleanup);
-
-  SL_RETURN((value), _("sh_logmon_set_active"));
-}
-
-static int sh_logmon_set_save_dir(const char *str) 
-{
-  int retval = -1;
-    
-  SL_ENTER(_("sh_logmon_set_save_dir"));
-
-  if (str && str[0] == '/')
-    {
-      if (save_dir)
-	{
-	  SH_FREE(save_dir);
-	  save_dir = NULL;
-	}
-      save_dir = sh_util_strdup(str);
-      retval = 0;
-    }
-
-  SL_RETURN((retval), _("sh_logmon_set_save_dir"));
-}
-
-static int sh_logmon_set_interval (const char * c)
-{
-  int retval = 0;
-  long val;
-
-  SL_ENTER(_("sh_logmon_set_interval"));
-  val = strtol (c, (char **)NULL, 10);
-  if (val <= 0)
-    {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
-		       _("log monitoring interval"), c);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      retval = -1;
-    }
-  else
-    {
-      sh_logmon_interval = (time_t) val;
-    }
-  SL_RETURN(retval, _("sh_logmon_set_interval"));
-}
-
-/* Add a watch on a logfile.
- * Format: TYPE : Filename [: File_Format]
- */
-static int sh_logmon_add_watch (const char * str)
-{
-  return sh_add_watch(str);
-}
-
-/* Add a host.
- * Format: Name_Regex
- */
-static int sh_logmon_add_host (const char * str)
-{
-  return sh_eval_hadd(str);
-}
-
-/* End a host.
- * Format: Name
- */
-static int sh_logmon_end_host (const char * str)
-{
-  (void) str;
-  return sh_eval_hend(NULL);
-}
-
-/* Add a group of rules. 
- * Groups can be under hosts, but not vice versa.
- * Format: Name : Prefix_Regex
- */
-static int sh_logmon_add_group (const char * str)
-{
-  return sh_eval_gadd(str);
-}
-
-/* End a group of rules.
- * Format: Name
- */
-static int sh_logmon_end_group (const char * str)
-{
-  (void) str;
-  return sh_eval_gend(NULL);
-}
-
-/* Define a reporting queue.
- * Format: Label : [Interval] : TYPE : Severity[:alias]
- * TYPE must be 'report' or 'sum'
- * Interval is ignored for TYPE='report'
- */
-static int sh_logmon_add_queue (const char * str)
-{
-  return sh_eval_qadd(str);
-}
-
-/* Define a check rule.
- * Format: [KEEP(seconds,label):]Queue_Label : Regex
- * KEEP indicates that we keep the label, to perform
- *      correlation matching
- */
-static int sh_logmon_add_rule (const char * str)
-{
-  return sh_eval_radd(str);
-}
-
-
-#if 0
-
-/* >>>>>>>>>>>  MAIN <<<<<<<<<<<<<<<<<<< */
-
-int main (int argc, char * argv[])
-{
-  int status, i;
-  FILE * fp;
-  sh_string * s = NULL;
-  static char template[] = "/tmp/xtest.XXXXXX";
-
-  /* pacct */
-  status = sh_add_watch("PACCT:/var/log/account/pacct");
-  sh_check_watches();
-  sh_dump_watches();
-  exit(0);
-
-  /* apache log */
-  sh_eval_gadd("four_o_four:404");
-  sh_eval_qadd("test:1:sum:7");
-  sh_eval_radd("test:^(\\d+.\\d+.\\d+.\\d+).*");
-  sh_eval_gend(NULL);
-  sh_eval_radd("trash:.*");
-  status = sh_add_watch("APACHE:/var/log/apache2/access.log:combined");
-  sh_check_watches();
-  sh_dump_watches();
-  exit(0);
-
-  /* logfile */
-  sh_set_hidepid(1);
-  sh_eval_hadd("hslxmsrv1");
-  sh_eval_gadd("postfix:postfix");
-  sh_eval_qadd("test::report:7");
-  sh_eval_radd("test:postfix/smtpd: disconnect from localhost.*");
-  sh_eval_radd("trash:postfix/smtpd: disconnect.*");
-  sh_eval_hadd("hspc05");
-  sh_eval_gadd("cron:CRON");
-  sh_eval_qadd("test:1:sum:7");
-  sh_eval_radd("test:CRON: PAM adding faulty module: (/lib/security/.*.so)");
-  sh_eval_radd("trash:.*");
-  status = sh_add_watch("SYSLOG:/var/log/messages");
-  sh_check_watches();
-
-  sh_dump_watches();
-  exit(0);
-
-  printf("%d types\n",
-	 (int) (sizeof(sh_logtypes_def)/sizeof(struct sh_logfile_type)));
-
-  /* test sh_add_watch 
-   */
-  status = sh_add_watch("");
-  printf("%2d: zero length, expect -1\n", status);
-  status = sh_add_watch(NULL);
-  printf("%2d: NULL, expect -2\n", status);
-  status = sh_add_watch("0123456789012345:/var/log/messages");
-  printf("%2d: long, expect -2\n", status);
-  status = sh_add_watch("012345678901234:/var/log/messages");
-  printf("%2d: exact length, expect -3\n", status);
-  status = sh_add_watch("01234567890123:56789");
-  printf("%2d: short length, expect -3\n", status);
-  status = sh_add_watch("SYSLOG:var/log/messages");
-  printf("%2d: short badpath, expect -4\n", status);
-  status = sh_add_watch("SYSLOG:/var/log/messages");
-  /* status = sh_add_watch("SYSLOG:/var/log/dpkg.log.1"); */
-  printf("%2d: short path ok, expect 0\n", status);
-
-  /* test sh_string_read 
-   */
-  s = sh_string_new();
-
-  status = /*@i@*/mkstemp(template);
-
-  if (status < 0) {
-    fprintf(stderr, "error in mkstemp!\n"); exit(EXIT_FAILURE); }
-
-  fp = fdopen(status, "r+");
-  if (!fp) {
-    fprintf(stderr, "error in fdopen!\n"); exit(EXIT_FAILURE); }
-
-  for (i = 0; i <  80; ++i) { fputc ('a', fp); } fputc ('\n', fp); /* 0 */
-  for (i = 0; i < 118; ++i) { fputc ('a', fp); } fputc ('\n', fp); /* 1 */
-  for (i = 0; i < 119; ++i) { fputc ('a', fp); } fputc ('\n', fp); /* 2 */
-  for (i = 0; i < 120; ++i) { fputc ('a', fp); } fputc ('\n', fp); /* 3 */
-  for (i = 0; i < 121; ++i) { fputc ('a', fp); } fputc ('\n', fp); /* 4 */
-  for (i = 0; i < 238; ++i) { fputc ('a', fp); } fputc ('\n', fp);
-  for (i = 0; i < 239; ++i) { fputc ('a', fp); } fputc ('\n', fp);
-  for (i = 0; i < 240; ++i) { fputc ('a', fp); } fputc ('\n', fp);
-  for (i = 0; i < 241; ++i) { fputc ('a', fp); } fputc ('\n', fp);
-
-  rewind(fp);
-
-  for (i = 0; i < 9; ++i)
-    {
-      status = (int) sh_string_read(s, fp, 120);
-      printf("%d: status = %d, len = %d, size = %d\n",
-	     i, status, (int)s->len, (int)s->siz);
-      if (status == -2)
-	(void) sh_string_read(s, fp, 240);
-      else
-	printf("%s\n", s->str);
-    }
-
-  rewind(fp);
-
-  (void) sh_string_truncate(s, 0);
-
-  for (i = 0; i < 9; ++i)
-    {
-      status = (int) sh_string_read(s, fp, 240);
-      printf("%d: status = %d, len = %d, size = %d\n",
-	     i, status, (int)s->len, (int)s->siz);
-      if (status == -2)
-	(void) sh_string_read(s, fp, 240);
-      else
-	{
-	  for (status = 0; status < (int)s->len; ++status)
-	    {
-	      if (s->str[status] != 'a')
-		{
-		  break;
-		}
-	    }
-	  printf("%d %s\n", status, s->str);
-	}
-    }
-
-  sl_fclose(FIL__, __LINE__, fp); remove(template);
-
-
-
-  return 0;
-}
-#endif
-
-/* #ifdef USE_LOGFILE_MONITOR */
-#endif
-
Index: trunk/src/sh_log_correlate.c
===================================================================
--- trunk/src/sh_log_correlate.c	(revision 591)
+++ 	(revision )
@@ -1,329 +1,0 @@
-#include "config_xor.h"
-
-#ifdef USE_LOGFILE_MONITOR
-
-#undef  FIL__
-#define FIL__  _("sh_log_correlate.c")
-
-#include <string.h>
-#include <time.h>
-
-/* Debian/Ubuntu: libpcre2-dev */
-#define PCRE2_CODE_UNIT_WIDTH 8
-#ifdef HAVE_PCRE2_PCRE2_H
-#include <pcre2/pcre2.h>
-#else
-#include <pcre2.h>
-#endif
-
-
-#include "samhain.h"
-#include "sh_pthread.h"
-#include "sh_utils.h"
-#include "sh_string.h"
-#include "sh_log_check.h"
-#include "sh_log_evalrule.h"
-
-extern int flag_err_debug;
-
-/*--------------------------------------------------------------
- *
- *   Event correlation
- *
- *--------------------------------------------------------------*/
-
-/* For each even to be correlated, we keep a label in a list. We
- * then build a string from the (time-sorted) list of labels, and
- * match this string against a regular expression.
- */
-
-/* -- The list of labels kept in memory ----------------------- */
-
-struct sh_keep
-{
-  sh_string       * label;           /* label of keep rule      */
-  unsigned long     delay;           /* valid delay             */
-  time_t            last;            /* seen at                 */
-  struct sh_keep *  next; 
-};
-
-static struct sh_keep * keeplist  = NULL;
-static struct sh_keep * keeplast  = NULL;
-static unsigned long    keepcount = 0;
-
-static void sh_keep_free(void * item)
-{
-  struct sh_keep * keep = (struct sh_keep *) item;
-
-  if (!keep)
-    return;
-  sh_string_destroy(&(keep->label));
-  SH_FREE(keep);
-}
-
-void sh_keep_destroy()
-{
-  struct sh_keep * keep;
-
-  while (keeplist)
-    {
-      keep = keeplist;
-      keeplist = keep->next;
-      sh_keep_free(keep);
-      --keepcount;
-    }
-  keeplist  = NULL;
-  keeplast  = NULL;
-  keepcount = 0;
-}
-
-int sh_keep_add(sh_string * label, unsigned long delay, time_t last)
-{
-  struct sh_keep * keep = SH_ALLOC(sizeof(struct sh_keep));
-
-  keep->label = sh_string_copy(label);
-  keep->delay = delay;
-  keep->last  = last;
-  keep->next  = NULL;
-
-  if (keeplast && keeplist)
-    {
-      keeplast->next = keep;
-      keeplast       = keep;
-    }
-  else
-    {
-      keeplist = keep;
-      keeplast = keeplist;
-    }
-  ++keepcount;
-  return 0;
-}
-
-int sh_keep_comp(const void * a, const void * b)
-{
-  return ( (int)(((const struct sh_keep *)a)->last) - 
-	   (int)(((const struct sh_keep *)b)->last) );
-}
-
-/* -- Sort the kept labels and build a string ----------------- */
-
-static sh_string * sh_keep_eval()
-{
-  unsigned long count   = 0;
-  sh_string * res       = NULL;
-  time_t now            = time(NULL);
-  struct sh_keep * keep = keeplist;
-  struct sh_keep * prev = keeplist;
-  struct sh_keep * arr;
-
-  if (keepcount > 0)
-    {
-      arr = SH_ALLOC (keepcount * sizeof(struct sh_keep));
-
-      while (count < keepcount && keep)
-	{
-	  if ((now >= keep->last) && 
-	      ((unsigned long)(now - keep->last) <= keep->delay))
-	    {
-	      memcpy(&(arr[count]), keep, sizeof(struct sh_keep));
-	      ++count;
-	      prev = keep;
-	      keep = keep->next;
-	    }
-	  else /* Too old or in future, delete it */
-	    {
-	      if (keep != keeplist)
-		{
-		  prev->next = keep->next;
-		  sh_keep_free(keep);
-		  keep = prev->next;
-		  --keepcount;
-		}
-	      else /* list head */
-		{
-		  keeplist = keep->next;
-		  prev     = keeplist;
-		  sh_keep_free(keep);
-		  keep     = keeplist;
-		  --keepcount;
-		}
-	    }
-	}
-
-      if (count > 0)
-	{
-	  unsigned long i;
-	  qsort(arr, count, sizeof(struct sh_keep), sh_keep_comp);
-	  res = sh_string_copy(arr[0].label);
-	  for (i = 1; i < count; ++i)
-	    res = sh_string_add(res, arr[i].label);
-	}
-      SH_FREE(arr);
-    }
-
-  return res;
-}
-
-/* -- Match the string against correlation rules -------------- */
-
-struct sh_mkeep
-{
-  sh_string       * label;           /* label of match rule     */
-  pcre2_code      * rule;            /* compiled regex for rule */
-  time_t            reported;        /* last reported           */
-  struct sh_qeval * queue;           /* assigned queue          */
-  struct sh_mkeep * next; 
-};
-
-struct sh_mkeep * mkeep_list = NULL;
-unsigned long     mkeep_deadtime = 60;
-
-int sh_keep_deadtime (const char * str)
-{
-  unsigned long  value;
-  char * foo;
-
-  value = (size_t) strtoul(str, &foo, 0);
-
-  if (*foo == '\0') {
-    mkeep_deadtime = value;
-    return 0;
-  }
-  return -1;
-}
-
-int sh_keep_match_add(const char * str, const char * queue, 
-		      const char * pattern)
-{
-  unsigned int nfields = 1; /* seconds:label */
-  size_t       lengths[1];
-  char *       new    = sh_util_strdup(str);
-  char **      splits = split_array_braced(new, _("CORRELATE"), 
-					   &nfields, lengths);
-
-  if (nfields == 1 && lengths[0] > 0)
-    {
-      struct sh_mkeep * mkeep = SH_ALLOC(sizeof(struct sh_mkeep));
-      int               error;
-      size_t            erroffset;
-      struct sh_qeval * rqueue = NULL;
-
-      mkeep->rule = pcre2_compile((PCRE2_SPTR8)pattern, PCRE2_ZERO_TERMINATED, PCRE2_NO_AUTO_CAPTURE, 
-				  &error, &erroffset, NULL);
-      if (!(mkeep->rule))
-	{
-	  sh_string * msg =  sh_string_new(0);
-	  sh_string_add_from_char(msg, _("Bad regex: "));
-	  sh_string_add_from_char(msg, pattern);
-	  
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-			  sh_string_str(msg),
-			  _("sh_keep_match_add"));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  sh_string_destroy(&msg);
-	  
-	  SH_FREE(splits);
-	  SH_FREE(mkeep);
-	  SH_FREE(new);
-	  return -1;
-	}
-
-      if (0 != strcmp(queue, _("trash")))
-	{
-
-	  rqueue = sh_log_find_queue(queue);
-	  if (!rqueue)
-	    {
-	      pcre2_code_free(mkeep->rule);
-	      SH_FREE(splits);
-	      SH_FREE(mkeep);
-	      SH_FREE(new);
-	      return -1;
-	    }
-	}
-
-      mkeep->queue = rqueue;
-      mkeep->label = sh_string_new_from_lchar(splits[0], strlen(splits[0]));
-      mkeep->reported = 0;
-      mkeep->next  = mkeep_list;
-      mkeep_list   = mkeep;
-    }
-  SH_FREE(new);
-  return 0;
-}
-
-void sh_keep_match_del()
-{
-  struct sh_mkeep * mkeep = mkeep_list;
-  while (mkeep)
-    {
-      mkeep_list = mkeep->next;
-      sh_string_destroy(&(mkeep->label));
-      pcre2_code_free(mkeep->rule);
-      mkeep = mkeep_list;
-    }
-  mkeep_list = NULL;
-}
-
-static struct sh_mkeep ** dummy_mkeep;
-
-void sh_keep_match()
-{
-  if (mkeep_list)
-    {
-      sh_string       * res = sh_keep_eval();
-
-      if (res)
-	{
-	  struct sh_mkeep * mkeep = mkeep_list;
-
-	  dummy_mkeep = &mkeep;
-
-	  while (mkeep)
-	    {
-	      pcre2_match_data * match_data = pcre2_match_data_create_from_pattern(mkeep->rule, NULL);
-	      
-	      int val = pcre2_match(mkeep->rule, 
-				    (PCRE2_SPTR8) sh_string_str(res), (int)sh_string_len(res), 0,
-				    0, match_data, NULL);
-	      
-	      pcre2_match_data_free(match_data);
-	      
-	      if (val > 0)
-		{
-		  sh_string * alias;
-		  time_t      now = time(NULL);
-
-		  if ((mkeep->reported < now) &&
-		      (mkeep_deadtime < (unsigned int)(now - mkeep->reported)))
-		    {
-		      mkeep->reported = now;
-
-		      SH_MUTEX_LOCK(mutex_thread_nolog);
-		      sh_error_handle (mkeep->queue->severity, FIL__, __LINE__, 0, 
-				       MSG_LOGMON_COR, sh_string_str(mkeep->label),
-				       val);
-
-		      alias = mkeep->queue->alias;
-		      if (alias)
-			{
-			  sh_error_mail (sh_string_str(alias), 
-					 mkeep->queue->severity, FIL__, __LINE__, 0, 
-					 MSG_LOGMON_COR, sh_string_str(mkeep->label),
-					 val);
-			}
-		      
-		      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-		    }
-		}
-	      mkeep = mkeep->next;
-	    }
-	  sh_string_destroy(&res);
-	}
-    }
-  return;
-}
-
-#endif
Index: trunk/src/sh_log_evalrule.c
===================================================================
--- trunk/src/sh_log_evalrule.c	(revision 591)
+++ 	(revision )
@@ -1,1280 +1,0 @@
-
-#include "config_xor.h"
-
-#include <stdio.h>
-#include <stdarg.h>
-#include <string.h>
-#include <ctype.h>
-#include <time.h>
-#include <limits.h>
-#include <sys/types.h>
-
-#ifdef USE_LOGFILE_MONITOR
-
-#undef  FIL__
-#define FIL__  _("sh_log_evalrule.c")
-
-/* Debian/Ubuntu: libpcre2-dev */
-#define PCRE2_CODE_UNIT_WIDTH 8
-#ifdef HAVE_PCRE2_PCRE2_H
-#include <pcre2/pcre2.h>
-#else
-#include <pcre2.h>
-#endif
-
-#ifndef PCRE2_NO_AUTO_CAPTURE
-#define PCRE2_NO_AUTO_CAPTURE 0
-#endif
-
-#include "samhain.h"
-#include "sh_pthread.h"
-#include "sh_utils.h"
-#include "sh_string.h"
-#include "sh_log_check.h"
-#include "sh_log_evalrule.h"
-#include "sh_log_correlate.h"
-#include "sh_log_mark.h"
-#include "sh_log_repeat.h"
-#include "zAVLTree.h"
-
-extern int flag_err_debug;
-
-/* #define DEBUG_EVALRULES */
-
-#ifdef DEBUG_EVALRULES
-static void DEBUG(const char *fmt, ...)
-{
-  va_list ap;
-  va_start(ap, fmt);
-  vfprintf(stderr, fmt, ap); /* flawfinder: ignore *//* we control fmt string */
-  va_end(ap);
-  return;
-}
-#else
-static void DEBUG(const char *fmt, ...)
-{
-  (void) fmt;
-  return;
-}
-#endif
-
-struct sh_ceval    /* Counter for summarizing    */
-{
-  sh_string   * hostname;
-  sh_string   * counted_str;
-  sh_string   * filename;
-  unsigned long count;
-  time_t        start;
-  time_t        interval;
-};
-
-void sh_ceval_free(void * item)
-{
-  struct sh_ceval * counter = (struct sh_ceval *) item;
-  if (!counter)
-    return;
-  sh_string_destroy(&(counter->hostname));
-  sh_string_destroy(&(counter->counted_str));
-  sh_string_destroy(&(counter->filename));
-  SH_FREE(counter);
-}
-
-enum {
-  RFL_ISRULE  = 1 << 0,
-  RFL_ISGROUP = 1 << 1,
-  RFL_KEEP    = 1 << 2,
-  RFL_MARK    = 1 << 3
-};
-
-
-/*--------------------------------------------------------------
- *
- *   Adding rules/groups/hosts
- *
- *--------------------------------------------------------------*/
-
-struct sh_geval  /* Group of rules (may be a single rule) */
-{
-  sh_string        * label;           /* label for this group    */
-  pcre2_code       * rule;            /* compiled regex for rule */
-
-  pcre2_match_data * match_data;      /* captured substrings     */
-  int                ovecnum;         /* how many captured       */
-  int                captures;        /* (captures+1)*3 required */
-  int                flags;           /* bit flags               */
-  unsigned long      delay;           /* delay for keep rules    */
-  zAVLTree         * counterlist;     /* counters if EVAL_SUM    */
-  struct sh_qeval  * queue;           /* queue for this rule     */
-  struct sh_geval  * nextrule;        /* next rule in this group */
-  struct sh_geval  * next;            /* next group of rules     */
-  struct sh_geval  * gnext;           /* grouplist next          */
-};
-
-struct sh_heval  /* host-specific rules */
-{
-  pcre2_code      * hostname;        /* compiled regex for hostname */
-  struct sh_geval * rulegroups;      /* list of group of rules      */
-  struct sh_heval * next;
-};
-
-static struct sh_heval * hostlist  = NULL;
-static struct sh_qeval * queuelist = NULL;
-static struct sh_geval * grouplist = NULL;
-
-/* These flags are set if we are within 
- * the define of a host/rule group.
- */
-static struct sh_heval * host_open  = NULL;
-static struct sh_geval * group_open = NULL;
-
-int sh_eval_gend (const char * str)
-{
-  (void) str;
-  if (group_open) {
-    group_open = NULL;
-    return 0;
-  }
-  return -1;
-}
-
-int sh_eval_gadd (const char * str)
-{
-  struct sh_geval * ng;
-  struct sh_geval * tmp;
-  pcre2_code * group;
-
-  int          error;
-  PCRE2_SIZE   erroffset;
-  unsigned int nfields = 2;
-  size_t       lengths[2];
-  
-  char *       new = sh_util_strdup(str);
-  char **      splits = split_array(new, &nfields, ':', lengths);
-
-  /* group is label:regex
-   */
-
-  if (group_open)
-    group_open = NULL;
-
-  if (nfields != 2)
-    {
-      SH_FREE(splits);
-      SH_FREE(new);
-      return -1;
-    }
-
-  group = pcre2_compile((PCRE2_SPTR8)splits[1], lengths[1], PCRE2_NO_AUTO_CAPTURE, 
-		       &error, &erroffset, NULL);
-  if (!group)
-    {
-      sh_string * msg =  sh_string_new(0);
-      sh_string_add_from_char(msg, _("Bad regex: "));
-      sh_string_add_from_char(msg, splits[1]);
-      
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		      sh_string_str(msg),
-		      _("sh_eval_gadd"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      sh_string_destroy(&msg);
-      
-      SH_FREE(splits);
-      SH_FREE(new);
-      return -1;
-    }
-
-  ng = SH_ALLOC(sizeof(struct sh_geval));
-  memset(ng, 0, sizeof(struct sh_geval));
-
-  ng->label       = sh_string_new_from_lchar(splits[0], lengths[0]);
-  ng->flags       = RFL_ISGROUP;
-
-  ng->rule        = group;
-
-  ng->match_data  = pcre2_match_data_create_from_pattern(group, NULL);
-  ng->ovecnum     = 0;
-  ng->captures    = 0;
-  ng->counterlist = NULL;
-  ng->queue       = NULL;
-  ng->nextrule    = NULL;
-  ng->next        = NULL;
-  ng->gnext       = NULL;
-
-  if (!host_open)
-    {
-      if (0 != sh_eval_hadd("^.*"))
-	{
-	  pcre2_code_free(group);
-	  pcre2_match_data_free(ng->match_data);
-	  sh_string_destroy(&(ng->label));
-	  SH_FREE(splits);
-	  SH_FREE(new);
-	  SH_FREE(ng);
-	  return -1;
-	}
-    }
-
-  /* 
-   * Insert at end, to keep user-defined order 
-   */ 
-
-  if (host_open)
-    {
-      if (grouplist) 
-	{
-	  tmp = grouplist; 
-	  while (tmp->gnext != NULL) { tmp = tmp->gnext; }
-	  tmp->gnext = ng;
-	} else {
-	  grouplist = ng;
-        }
-
-
-      /* 
-       * If there is an open host group, add it to its
-       * rulegroups
-       */
-
-      if (host_open->rulegroups) 
-	{
-	  tmp = host_open->rulegroups; 
-	  while (tmp->next != NULL) { tmp = tmp->next; }
-	  tmp->next = ng;
-	} else {
-	  host_open->rulegroups = ng;
-        }
-    }
-
-  group_open = ng;
-  SH_FREE(splits);
-  SH_FREE(new);
-  return 0;
-}
-
-int sh_eval_hend (const char * str)
-{
-  (void) str;
-  if (host_open) {
-    host_open = NULL;
-    return 0;
-  }
-  return -1;
-}
-
-int sh_eval_hadd (const char * str)
-{
-  struct sh_heval * nh;
-  struct sh_heval * tmp;
-  pcre2_code     *  host;
-
-  int               error;
-  PCRE2_SIZE        erroffset;
-
-  if (host_open)
-    host_open = NULL;
-
-  host = pcre2_compile((PCRE2_SPTR8)str, PCRE2_ZERO_TERMINATED, PCRE2_NO_AUTO_CAPTURE, 
-		       &error, &erroffset, NULL);
-  if (!host)
-    {
-      sh_string * msg =  sh_string_new(0);
-      sh_string_add_from_char(msg, _("Bad regex: "));
-      sh_string_add_from_char(msg, str);
-      
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		      sh_string_str(msg),
-		      _("sh_eval_hadd"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      sh_string_destroy(&msg);
-
-      return -1;
-    }
-
-  nh = SH_ALLOC(sizeof(struct sh_heval));
-  memset(nh, 0, sizeof(struct sh_heval));
-
-  nh->hostname = host;
-  nh->rulegroups = NULL;
-
-  /* 
-   * Insert at end, to keep user-defined order 
-   */ 
-  nh->next = NULL;
-  if (hostlist) {
-    tmp = hostlist; 
-    while (tmp->next != NULL) { tmp = tmp->next; }
-    tmp->next = nh;
-  } else {
-    hostlist = nh;
-  }
-  host_open = nh;
-
-  return 0;
-}
-
-int sh_eval_qadd (const char * str)
-{
-  struct sh_qeval * nq;
-  int     severity;
-  unsigned int nfields = 5; /* label:interval:(report|sum):severity[:alias] */
-  size_t  lengths[5];
-  char *  new = sh_util_strdup(str);
-  char ** splits = split_array(new, &nfields, ':', lengths);
-
-  if (nfields < 4)
-    {
-      SH_FREE(splits);
-      SH_FREE(new);
-      return -1;
-    }
-
-  if (strcmp(splits[2], _("sum")) && strcmp(splits[2], _("report")))
-    {
-      SH_FREE(splits);
-      SH_FREE(new);
-      return -1;
-    }
-
-  if (!strcmp(splits[2], _("sum")) && atoi(splits[1]) < 0)
-    {
-      SH_FREE(splits);
-      SH_FREE(new);
-      return -1;
-    }
-  
-  if (!strcmp(splits[1], _("trash"))) /* predefined, reserved */
-    {
-      SH_FREE(splits);
-      SH_FREE(new);
-      return -1;
-    }
-  
-  severity = sh_error_convert_level (splits[3]);
-  if (severity < 0)
-    {
-      SH_FREE(splits);
-      SH_FREE(new);
-      return -1;
-    }
-
-  nq = SH_ALLOC(sizeof(struct sh_qeval));
-  memset(nq, 0, sizeof(struct sh_qeval));
-
-  nq->label = sh_string_new_from_lchar(splits[0], lengths[0]);
-  nq->alias = NULL;
-
-  DEBUG("debug: splits[2] = %s, policy = %d\n",splits[2],nq->policy); 
-  if (0 == strcmp(splits[2], _("report"))) {
-    nq->policy   = EVAL_REPORT;
-    nq->interval = 0;
-  }
-  else {
-    nq->policy   = EVAL_SUM;
-    nq->interval = (time_t) atoi(splits[1]);
-  }
-
-  nq->severity = severity;
-
-  if (nfields == 5)
-    {
-      nq->alias = sh_string_new_from_lchar(splits[4], lengths[4]);
-    }
-
-  nq->next     = queuelist;
-  queuelist    = nq;
-
-  SH_FREE(splits);
-  SH_FREE(new);
-  return 0;
-}
-
-struct sh_qeval * sh_log_find_queue(const char * str)
-{
-  struct sh_qeval * retval = queuelist;
-
-  if (!str)
-    return NULL;
-
-  while (retval)
-    {
-      if (0 == strcmp(str, sh_string_str(retval->label)))
-	break;
-      retval = retval->next;
-    }
-  return retval;
-}
-
-int sh_log_lookup_severity(const char * str)
-{
-  struct sh_qeval * queue;
-
-  if (str)
-    {
-      if (0 != strcmp(str, _("trash")))
-	{
-	  queue = sh_log_find_queue(str);
-	  
-	  if (queue)
-	    return queue->severity;
-	}
-    }
-  return SH_ERR_SEVERE;
-}
-
-sh_string * sh_log_lookup_alias(const char * str)
-{
-  struct sh_qeval * queue;
-
-  if (str)
-    {
-      if (0 != strcmp(str, _("trash")))
-	{
-	  queue = sh_log_find_queue(str);
-	  
-	  if (queue)
-	    return queue->alias;
-	}
-    }
-  return NULL;
-}
-
-
-static char * get_label_and_time(const char * inprefix, char * str, 
-				 unsigned long * seconds)
-{
-  char       * res    = NULL;
-  char       * endptr = NULL;
-
-  unsigned int nfields = 2; /* seconds:label */
-  size_t       lengths[2];
-  char *       prefix = sh_util_strdup(inprefix);
-  char *       new    = sh_util_strdup(str);
-  char **      splits = split_array_braced(new, prefix, &nfields, lengths);
-
-  if (splits && nfields == 2 && lengths[0] > 0 && lengths[1] > 0)
-    {
-      *seconds = strtoul(splits[0], &endptr, 10);
-      if ((*endptr == '\0') && (endptr != splits[0]) && (*seconds != ULONG_MAX))
-	{
-	  res = sh_util_strdup(splits[1]);
-	}
-    }
-  if (splits)
-    SH_FREE(splits);
-  SH_FREE(new);
-  SH_FREE(prefix);
-  return res;
-}
-
-struct sh_qeval ** sh_dummy_472_queue;
-char            ** sh_dummy_473_dstr;
-
-int sh_eval_radd (const char * str)
-{
-  struct sh_geval * nr;
-  struct sh_geval * tmp;
-  struct sh_qeval * queue;
-  pcre2_code    *  rule;
-
-  int          error;
-  PCRE2_SIZE   erroffset;
-  unsigned int captures = 0;
-  unsigned int nfields = 2; /* queue:regex */
-  size_t       lengths[3];
-  char *       new    = sh_util_strdup(str);
-  char **      splits;
-
-  int           qpos  = 0;
-  volatile int  rpos  = 1;
-  unsigned long dsec  = 0;
-  char *        dstr  = NULL;
-  char *        s     = new;
-  volatile char pflag = '-';
-
-  if (s == NULL)
-    return -1;
-  /* cppcheck-suppress uninitdata */
-  while ( *s && isspace((int)*s) ) ++s;
-  if (0 == strncmp(s, _("KEEP"), 4)      || 
-      0 == strncmp(s, _("CORRELATE"), 9) ||
-      0 == strncmp(s, _("MARK"), 4))
-    {
-      pflag   = s[0];
-      nfields = 3;
-    }
-
-  splits = split_array(new, &nfields, ':', lengths);
-
-  sh_dummy_472_queue = &queue;
-  sh_dummy_473_dstr  = &dstr;
-
-  queue = NULL;
-
-  if (nfields < 2 || nfields > 3)
-    {
-      SH_FREE(splits);
-      SH_FREE(new);
-      return -1;
-    }
-
-  if (nfields == 3)
-    {
-      if (pflag == 'K')
-	{
-	  /* KEEP(nsec,label):queue:regex
-	   */
-	  dstr = get_label_and_time(_("KEEP"), splits[0], &dsec);
-	  if (!dstr)
-	    {
-	      SH_FREE(splits);
-	      SH_FREE(new);
-	      return -1;
-	    }
-	}
-      else if (pflag == 'C')
-	{
-	  /* CORRELATE(description):queue:regex 
-	   */
-	  int retval = sh_keep_match_add(splits[0], splits[1], splits[2]);
-	  SH_FREE(splits);
-	  SH_FREE(new);
-	  return retval;
-	}
-      else if (pflag == 'M')
-	{
-	  /* MARK(description, interval):queue:regex 
-	   */
-	  int retval = -1;
-
-	  dstr = get_label_and_time(_("MARK"), splits[0], &dsec);
-	  if (dstr)
-	    {
-	      retval = sh_log_mark_add(dstr, dsec, splits[1]);
-	    }
-	  if (retval != 0)
-	    {
-	      SH_FREE(splits);
-	      SH_FREE(new);
-	      return retval;
-	    }
-	}
-      ++qpos; ++rpos;
-    }
-
-  if (0 != strcmp(splits[qpos], _("trash")))
-    {
-      queue = sh_log_find_queue(splits[qpos]);
-      if (!queue)
-	{
-	  SH_FREE(splits);
-	  SH_FREE(new);
-	  return -1;
-	}
-    }
-
-  rule = pcre2_compile((PCRE2_SPTR8)splits[rpos], lengths[rpos], 0, 
-		       &error, &erroffset, NULL);
-  if (!rule)
-    {
-      sh_string * msg =  sh_string_new(0);
-      sh_string_add_from_char(msg, _("Bad regex: "));
-      sh_string_add_from_char(msg, splits[rpos]);
-      
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		      sh_string_str(msg),
-		      _("sh_eval_radd"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      sh_string_destroy(&msg);
-
-      SH_FREE(splits);
-      SH_FREE(new);
-      return -1;
-    }
-
-  pcre2_pattern_info(rule, PCRE2_INFO_CAPTURECOUNT, &captures);
-
-  if (flag_err_debug == S_TRUE)
-    {
-      char * emsg = SH_ALLOC(SH_ERRBUF_SIZE);
-      if (dstr)
-	sl_snprintf(emsg,  SH_ERRBUF_SIZE, _("Adding rule: |%s| with %u captures, keep(%lu,%s)"), 
-		    splits[rpos], captures, dsec, dstr);
-      else
-	sl_snprintf(emsg,  SH_ERRBUF_SIZE, _("Adding rule: |%s| with %u captures"), 
-		    splits[rpos], captures);
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		      emsg, _("sh_eval_radd"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      SH_FREE(emsg);
-    }
-
-  DEBUG("adding rule: |%s| with %u captures\n", splits[rpos], captures);
-
-  SH_FREE(splits);
-  SH_FREE(new);
-
-  nr = SH_ALLOC(sizeof(struct sh_geval));
-  memset(nr, 0, sizeof(struct sh_geval));
-
-  nr->label       = NULL;
-  nr->flags       = RFL_ISRULE;
-  nr->delay       = 0;
-
-  nr->rule        = rule;
-
-  nr->captures    = captures;
-  nr->match_data  = pcre2_match_data_create_from_pattern(rule, NULL);
-  nr->ovecnum     = 0;
-  nr->counterlist = NULL;
-  nr->queue       = queue;
-  nr->nextrule    = NULL;
-  nr->next        = NULL;
-  nr->gnext       = NULL;
-
-
-  if (pflag == 'K')
-    {
-      nr->label   = sh_string_new_from_lchar(dstr, sl_strlen(dstr));
-      nr->flags  |= RFL_KEEP;
-      nr->delay   = dsec;
-      SH_FREE(dstr);
-    }
-  else if (pflag == 'M')
-    {
-      nr->label   = sh_string_new_from_lchar(dstr, sl_strlen(dstr));
-      nr->flags  |= RFL_MARK;
-      nr->delay   = dsec;
-      SH_FREE(dstr);
-    }
-
-  /* 
-   * If there is an open group, add it to its
-   * rules
-   */
-  if (group_open)
-    {
-      if (flag_err_debug == S_TRUE)
-	{
-	  char * emsg = SH_ALLOC(SH_ERRBUF_SIZE);
-	  sl_snprintf(emsg,  SH_ERRBUF_SIZE, _("Adding rule to group |%s|"), 
-		      sh_string_str(group_open->label));
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-			  emsg, _("sh_eval_radd"));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  SH_FREE(emsg);
-	}
-
-      DEBUG("adding rule to group |%s|\n", sh_string_str(group_open->label));
-
-      if (group_open->nextrule) 
-	{
-	  tmp = group_open->nextrule; 
-	  while (tmp->nextrule != NULL) { tmp = tmp->nextrule; } /* next -> nextrule */
-	  tmp->nextrule = nr;                                    /* next -> nextrule */
-	} else {
-	  group_open->nextrule = nr;
-	}
-    }
-
-  /* 
-   * ..else, add it to the currently open host (open the
-   * default host, if there is no open one)
-   */
-  else
-    {
-      if (!host_open)
-	{
-	  if (0 != sh_eval_hadd("^.*"))
-	    {
-	      if (nr->label)
-		sh_string_destroy(&(nr->label));
-	      if (nr->rule)
-		pcre2_code_free(nr->rule);
-	      if (nr->match_data)
-		pcre2_match_data_free(nr->match_data);
-	      SH_FREE(nr);
-	      return -1;
-	    }
-	}
-
-      if (host_open)
-	{
-	  /* 
-	   * Add rule as member to grouplist, to facilitate cleanup
-	   */
-
-	  DEBUG("adding solitary rule to grouplist\n");
-
-	  if (grouplist) 
-	    {
-	      tmp = grouplist; 
-	      while (tmp->gnext != NULL) { tmp = tmp->gnext; }
-	      tmp->gnext = nr;
-	    } else {
-	      grouplist = nr;
-	    }
-
-
-	  /* 
-	   * Add rule to host rulegroups
-	   */
-	  DEBUG("adding solitary rule to host rulegroups\n");
-
-	  if (host_open->rulegroups) 
-	    {
-	      /* Second, third, ... rule go to host_open->rulegroups->next,
-	       * since test_grules() iterates over nextrules
-	       */
-	      tmp = host_open->rulegroups; 
-	      while (tmp->next != NULL) { tmp = tmp->next; }
-	      tmp->next = nr;
-	    } 
-	  else 
-	    {
-	      /* First rule goes to host_open->rulegroups */
-	      host_open->rulegroups = nr;
-	    }
-	}
-      else
-	{
-	  if (nr->label)
-	    sh_string_destroy(&(nr->label));
-	  if (nr->rule)
-	    pcre2_code_free(nr->rule);
-	  if (nr->match_data)
-	    pcre2_match_data_free(nr->match_data);
-	  SH_FREE(nr);
-	  return -1;
-	}
-    }
-
-  return 0;
-}
-
-void sh_eval_cleanup()
-{
-  struct sh_geval * gtmp;
-  struct sh_qeval * qtmp;
-  struct sh_heval * htmp;
-
-  while (grouplist)
-    {
-      gtmp      = grouplist;
-      grouplist = gtmp->gnext;
-
-      if (gtmp->label)
-	sh_string_destroy(&(gtmp->label));
-      if (gtmp->rule)
-	pcre2_code_free(gtmp->rule);
-      if (gtmp->match_data)
-	pcre2_match_data_free(gtmp->match_data);
-      if (gtmp->counterlist)
-	zAVLFreeTree(gtmp->counterlist, sh_ceval_free);
-
-#if 0
-      while (gtmp->nextrule)
-	{
-	  tmp            = gtmp->nextrule;
-	  gtmp->nextrule = tmp->nextrule;
-
-	  if (tmp->rule)
-	    pcre2_code_free(tmp->rule);
-	  if (tmp->counterlist)
-	    zAVLFreeTree(tmp->counterlist, sh_ceval_free);
-	  if (tmp->ovector)
-	    SH_FREE(tmp->ovector);
-	  SH_FREE(tmp);
-	}
-#endif
-      SH_FREE(gtmp);
-    }
-
-  qtmp = queuelist;
-  while (qtmp)
-    {
-      if (qtmp->label)      sh_string_destroy(&(qtmp->label));
-      queuelist = qtmp->next;
-      SH_FREE(qtmp);
-      qtmp = queuelist;
-    }
-
-  htmp = hostlist;
-  while (htmp)
-    {
-      if (htmp->hostname)
-	pcre2_code_free(htmp->hostname);
-      if (htmp->rulegroups)
-	htmp->rulegroups = NULL;
-      hostlist = htmp->next;
-      htmp->next = NULL;
-      SH_FREE(htmp);
-      htmp = hostlist;
-    }
-
-  hostlist   = NULL;
-  queuelist  = NULL;
-  grouplist  = NULL;
-
-  host_open  = NULL;
-  group_open = NULL;
-
-  sh_keep_destroy();
-  sh_keep_match_del();
-
-  return;
-}
-
-/**********************************************************************
- *
- * Actual rule processing
- *
- **********************************************************************/ 
-
-/* Test a list of rules against msg; return matched rule, with ovector 
- * filled in
- */
-struct sh_geval ** sh_dummy_828_rule;
-
-static struct sh_geval * test_rule (struct sh_geval * rule, sh_string *msg, time_t tstamp)
-{
-  int res; 
-  volatile int    count;
-  volatile time_t timestamp = tstamp;
-
-  sh_dummy_828_rule = &rule;
-
-  if (!rule)
-    DEBUG("debug: (NULL) rule\n");
-
-  if (rule && sh_string_len(msg) < (size_t)INT_MAX)
-    {
-      count = 1;
-      do {
-
-	if (flag_err_debug == S_TRUE)
-	  {
-	    char * emsg = SH_ALLOC(SH_ERRBUF_SIZE);
-	    sl_snprintf(emsg,  SH_ERRBUF_SIZE, _("Check rule %d for |%s|"), 
-			count, sh_string_str(msg));
-	    SH_MUTEX_LOCK(mutex_thread_nolog);
-	    sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-			    emsg, _("test_rule"));
-	    SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	    SH_FREE(emsg);
-	  }
-
-	DEBUG("debug: check rule %d for <%s>\n", count, msg->str);
-	
-	res = pcre2_match(rule->rule, 
-			  (PCRE2_SPTR8)sh_string_str(msg), (int)sh_string_len(msg), 0,
-			  0,   rule->match_data, NULL);
-
-	if (res > 0)
-	  {
-	    rule->ovecnum = pcre2_get_ovector_count(rule->match_data);
-
-	    if (flag_err_debug == S_TRUE)
-	      {
-		char * emsg = SH_ALLOC(SH_ERRBUF_SIZE);
-		if ( rule->flags & RFL_KEEP )
-		  sl_snprintf(emsg,  SH_ERRBUF_SIZE, _("Rule %d matches, result = %d (keep)"), 
-			      count, res);
-		else if ( rule->flags & RFL_MARK )
-		  sl_snprintf(emsg,  SH_ERRBUF_SIZE, _("Rule %d matches, result = %d (mark)"), 
-			      count, res);
-		else
-		  sl_snprintf(emsg,  SH_ERRBUF_SIZE, _("Rule %d matches, result = %d"), 
-			      count, res);
-		SH_MUTEX_LOCK(mutex_thread_nolog);
-		sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-				emsg, _("test_rule"));
-		SH_MUTEX_UNLOCK(mutex_thread_nolog);
-		SH_FREE(emsg);
-	      }
-
-	    if ( rule->flags & RFL_KEEP )
-	      {
-		DEBUG("debug: rule %d matches (keep), timestamp = %lu\n", count, timestamp);
-		sh_keep_add(rule->label, rule->delay, 
-			    timestamp == 0 ? time(NULL) : timestamp);
-	      }
-
-	    else if ( rule->flags & RFL_MARK )
-	      {
-		DEBUG("debug: rule %d matches (mark)\n", count);
-		sh_log_mark_update(rule->label,
-				   timestamp == 0 ? time(NULL) : timestamp);
-	      }
-
-	    break; /* return the matching rule; ovector is filled in */
-	  }
-
-	if (flag_err_debug == S_TRUE)
-	  {
-	    char * emsg = SH_ALLOC(SH_ERRBUF_SIZE);
-	    sl_snprintf(emsg,  SH_ERRBUF_SIZE, _("Rule %d did not match"), 
-			count);
-	    SH_MUTEX_LOCK(mutex_thread_nolog);
-	    sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-			    emsg, _("test_rule"));
-	    SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	    SH_FREE(emsg);
-	  }
-	DEBUG("debug: rule %d did not match\n", count);
-
-	rule = rule->nextrule; ++count;
-      } while (rule);
-    }
-  if (!rule)
-    DEBUG("debug: no match found\n");
-  /* If there was no match, this is NULL */
-  sh_dummy_828_rule = NULL;
-  return rule;
-}
-  
-/* Test a (struct sh_geval *), which may be single rule or a group of rules,
- * against msg
- */
-struct sh_geval ** sh_dummy_928_result;
-struct sh_geval ** sh_dummy_929_group;
-
-static struct sh_geval * test_grules (struct sh_heval * host, 
-				      sh_string       * msg,
-				      time_t            timestamp)
-{
-  struct sh_geval * result = NULL;
-  struct sh_geval * group  = host->rulegroups;
-
-  sh_dummy_928_result = &result;
-  sh_dummy_929_group  = &group;
-
-  if (group && sh_string_len(msg) < (size_t)INT_MAX)
-    {
-      DEBUG("debug: if group\n");
-      do {
-	if( (group->label != NULL) && (0 != (group->flags & RFL_ISGROUP))) 
-	  {
-	    /* this is a rule group */
-
-	    if (flag_err_debug == S_TRUE)
-	      {
-		char * emsg = SH_ALLOC(SH_ERRBUF_SIZE);
-		sl_snprintf(emsg,  SH_ERRBUF_SIZE, _("Checking group |%s| of rules against |%s|"), 
-			    sh_string_str(group->label), sh_string_str(msg));
-		SH_MUTEX_LOCK(mutex_thread_nolog);
-		sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-				emsg, _("test_rule"));
-		SH_MUTEX_UNLOCK(mutex_thread_nolog);
-		SH_FREE(emsg);
-	      }
-
-	    DEBUG("debug: if group->label %s\n", sh_string_str(group->label));
-	    
-	    if (pcre2_match(group->rule,
-			    (PCRE2_SPTR8)sh_string_str(msg), (int) sh_string_len(msg), 0,
-			    0, NULL, NULL) > 0)
-	      {
-		result = test_rule(group->nextrule, msg, timestamp);
-		if (result)
-		  break;
-	      }
-	  }
-	else
-	  {
-	    /* If there is no group label, the 'group' is actually a solitary 
-	     * rule (not within any group).
-	     */
-
-	    if (flag_err_debug == S_TRUE)
-	      {
-		char * emsg = SH_ALLOC(SH_ERRBUF_SIZE);
-		sl_snprintf(emsg,  SH_ERRBUF_SIZE, _("Checking solitary rules"));
-		SH_MUTEX_LOCK(mutex_thread_nolog);
-		sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-				emsg, _("test_rule"));
-		SH_MUTEX_UNLOCK(mutex_thread_nolog);
-		SH_FREE(emsg);
-	      }
-
-	    DEBUG("debug: else (single rule)\n");
-	    result = test_rule(group, msg, timestamp);
-	    if (result)
-	      break;
-	  }
-	group = group->next; /* next group of rules */
-      } while (group);
-    }
-
-  sh_dummy_928_result = NULL;
-  sh_dummy_929_group  = NULL;
-  return result;
-}
-
-/* Top-level find_rule() function
- */
-static struct sh_geval * find_rule (sh_string *host, 
-				    sh_string *msg,
-				    time_t     timestamp)
-{
-  struct sh_geval * result = NULL;
-  struct sh_heval * hlist  = hostlist;
-
-  if (hlist && sh_string_len(host) < (size_t)INT_MAX)
-    {
-      do {
-	if (pcre2_match(hlist->hostname, 
-			(PCRE2_SPTR8)sh_string_str(host), (int) sh_string_len(host), 0, 
-			0, NULL, NULL) > 0)
-	  {
-	    /* matching host, check rules/groups of rules */
-	    result = test_grules(hlist, msg, timestamp);
-	    if (result)
-	      break;
-	  }
-	hlist = hlist->next;
-      } while (hlist);
-    }
-  return result;
-}
-
-/* copy the message and replace captured substrings with '___'
- */
-static sh_string * replace_captures(const sh_string * message, 
-				    size_t * ovector, int ovecnum)
-{
-  sh_string * retval = sh_string_new_from_lchar(sh_string_str(message), 
-						sh_string_len(message));
-
-  if (ovecnum > 1 && (ovector[0] < ovector[1])) /* guard against patterns using \K */
-    {
-      retval = sh_string_replace(retval, &(ovector[2]), (ovecnum-1), "___", 3);
-    }
-  return retval;
-}
-
-static void msg_report(int severity, const sh_string * alias, 
-		       struct sh_geval * rule, struct sh_logrecord * record)
-{
-  char      * tmp;
-  char      * msg;
-  sh_string * mmm = NULL;
-  char      * ttt;
-
-
-  SH_MUTEX_LOCK(mutex_thread_nolog);
-  if (rule) {
-    mmm = replace_captures(record->message, pcre2_get_ovector_pointer(rule->match_data), 
-			   rule->ovecnum);
-    rule->ovecnum = 0;
-    msg = sh_util_safe_name_keepspace (sh_string_str(mmm));
-  }
-  else {
-    msg = sh_util_safe_name_keepspace (sh_string_str(record->message));
-  }
-  tmp = sh_util_safe_name_keepspace (record->filename);
-  ttt = sh_util_safe_name_keepspace (sh_string_str(record->timestr));
-  sh_error_handle (severity, FIL__, __LINE__, 0, MSG_LOGMON_REP,
-		   msg,
-		   ttt,
-		   sh_string_str(record->host),
-		   tmp);
-  if (alias)
-    {
-      sh_error_mail (sh_string_str(alias),
-		     severity, FIL__, __LINE__, 0, MSG_LOGMON_REP,
-		     msg,
-		     ttt,
-		     sh_string_str(record->host),
-		     tmp);
-    }
-  SH_FREE(ttt);
-  SH_FREE(msg);
-  SH_FREE(tmp);
-  if (mmm)
-    sh_string_destroy(&mmm);
-  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-}
-
-static void sum_report(int severity, const sh_string * alias,
-		       sh_string * host, sh_string * message, sh_string * path)
-{
-  char * tmp;
-  char * msg;
-
-  SH_MUTEX_LOCK(mutex_thread_nolog);
-  tmp = sh_util_safe_name_keepspace (sh_string_str(path));
-  msg = sh_util_safe_name_keepspace (sh_string_str(message));
-  sh_error_handle (severity, FIL__, __LINE__, 0, MSG_LOGMON_SUM,
-		   msg,
-		   sh_string_str(host), 
-		   tmp);
-  if (alias)
-    {
-      sh_error_mail (sh_string_str(alias),
-		     severity, FIL__, __LINE__, 0, MSG_LOGMON_SUM,
-		     msg,
-		     sh_string_str(host),
-		     tmp);
-    }
-  SH_FREE(msg);
-  SH_FREE(tmp);
-  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-}
-
-static zAVLKey sh_eval_getkey(void const *item)
-{
-  return ((const struct sh_ceval *)item)->hostname->str;
-}
-
-/* Find the counter, or initialize one if there is none already
- */
-static struct sh_ceval * find_counter(struct sh_geval * rule, 
-				      sh_string * host, time_t interval)
-{
-  struct sh_ceval * counter;
-
-  if (!(rule->counterlist))
-    {
-      DEBUG("debug: allocate new counterlist AVL tree\n");
-      rule->counterlist = zAVLAllocTree(sh_eval_getkey, zAVL_KEY_STRING);
-    }
-
-  counter = (struct sh_ceval *) zAVLSearch (rule->counterlist, 
-					    sh_string_str(host));
-
-  if (!counter)
-    {
-      DEBUG("debug: no counter found\n");
-
-      counter = SH_ALLOC(sizeof(struct sh_ceval));
-      memset(counter, 0, sizeof(struct sh_ceval));
-
-      counter->hostname    = sh_string_new_from_lchar(sh_string_str(host), 
-						      sh_string_len(host));
-      counter->counted_str = NULL;
-      counter->filename    = NULL;
-      counter->count       = 0;
-      counter->start       = time(NULL);
-      counter->interval    = interval;
-
-      zAVLInsert(rule->counterlist, counter);
-    }
-  return counter;
-		       
-}
-
-
-/* process the counter for a SUM rule
- */
-static int  process_counter(struct sh_ceval * counter, 
-			    struct sh_geval * rule,  
-			    struct sh_logrecord * record)
-{
-  int retval = -1;
-  time_t  now;
-
-  if (!(counter->counted_str))
-    {
-      counter->counted_str = replace_captures(record->message,  pcre2_get_ovector_pointer(rule->match_data), 
-					      rule->ovecnum);
-      rule->ovecnum        = 0;
-      counter->filename    = sh_string_new_from_lchar(record->filename,
-						      strlen(record->filename));
-      DEBUG("debug: counted_str after replace: %s\n", 
-	    sh_string_str(counter->counted_str)); 
-    }
-
-  ++(counter->count);
-  now = time(NULL); now -= counter->start;
-  DEBUG("debug: count %lu, interval %lu, time %lu\n", 
-	counter->count, counter->interval, now);
-  if (now >= counter->interval)
-    {
-      DEBUG("debug: report count\n");
-      sum_report(rule->queue->severity, rule->queue->alias,
-		 counter->hostname, counter->counted_str, counter->filename);
-      counter->start = time(NULL);
-      counter->count = 0;
-    }
-  return retval;
-}
-
-/* Process a rule
- */
-static int  process_rule(struct sh_geval * rule, struct sh_logrecord * record)
-{
-  int retval = -1;
-  struct sh_qeval * queue = rule->queue;
-
-  if (queue)
-    {
-      DEBUG("debug: queue policy = %d found\n", queue->policy);
-      if (queue->policy == EVAL_REPORT)
-	{
-	  DEBUG("debug: EVAL_REPORT host: %s, message: %s\n",
-		 sh_string_str(record->host), 
-		 sh_string_str(record->message));
-	  msg_report(queue->severity, queue->alias, rule, record);
-	  retval = 0;
-	}
-      else if (queue->policy == EVAL_SUM)
-	{
-	  
-	  struct sh_ceval * counter = 
-	    find_counter(rule, record->host, queue->interval);
-	  DEBUG("debug: EVAL_SUM host: %s, message: %s\n",
-		 sh_string_str(record->host),
-		 sh_string_str(record->message));
-	  if (counter)
-	    {
-	      DEBUG("debug: counter found\n");
-	      retval = process_counter(counter, rule, record);
-	    }
-	}
-    }
-  else
-    {
-      DEBUG("debug: no queue found -- trash\n");
-      /* No queue means 'trash' */
-      retval = 0;
-    }
-  return retval;
-}
-
-#define DEFAULT_SEVERITY (-1)
-
-int sh_eval_process_msg(struct sh_logrecord * record)
-{
-  static unsigned long i = 0;
-  if (record)
-    {
-      struct sh_geval * rule = find_rule (record->host,
-					  record->message,
-					  record->timestamp);
-
-      if (rule)
-	{
-	  DEBUG("debug: (%lu) rule found\n", i); ++i;
-	  return process_rule(rule, record);
-	}
-      else
-	{
-	  DEBUG("debug: (%lu) no rule found\n", i); ++i;
-	  msg_report(DEFAULT_SEVERITY, NULL, NULL, record);
-	}
-
-      sh_repeat_message_check(record->host, 
-			      record->message, 
-			      record->timestamp);
-			      
-      return 0;
-    }
-  return -1;
-}
-
-#endif
Index: trunk/src/sh_log_mark.c
===================================================================
--- trunk/src/sh_log_mark.c	(revision 591)
+++ 	(revision )
@@ -1,250 +1,0 @@
-#include "config_xor.h"
-
-#ifdef USE_LOGFILE_MONITOR
-
-#include <string.h>
-#include <time.h>
-
-#undef  FIL__
-#define FIL__  _("sh_log_mark.c")
-
-
-#include "samhain.h"
-#include "sh_pthread.h"
-#include "sh_mem.h"
-#include "sh_string.h"
-#include "sh_error_min.h"
-#include "sh_log_check.h"
-#include "sh_log_evalrule.h"
-#include "zAVLTree.h"
-
-/* #define DEBUG_MARK */
-
-#ifdef DEBUG_MARK
-static void DEBUG(const char *fmt, ...)
-{
-  va_list ap;
-  va_start(ap, fmt);
-  vfprintf(stderr, fmt, ap); /* flawfinder: ignore *//* we control fmt string */
-  va_end(ap);
-  return;
-}
-#else
-static void DEBUG(const char *fmt, ...)
-{
-  (void) fmt;
-  return;
-}
-#endif
-
-static zAVLTree * marklist = NULL;
-
-struct sh_mark_event
-{
-  sh_string   * label;
-  sh_string   * queue_id;
-  time_t        last_seen;
-  time_t        interval;
-  time_t        delay;
-  time_t        last_reported;
-};
-
-static void sh_marklist_free(void * item)
-{
-  struct sh_mark_event * event = (struct sh_mark_event *) item;
-  if (!event)
-    return;
-  sh_string_destroy(&(event->label));
-  sh_string_destroy(&(event->queue_id));
-  SH_FREE(event);
-  return;
-}
-
-void sh_log_mark_destroy()
-{
-  zAVLFreeTree(marklist, sh_marklist_free);
-}
-
-static zAVLKey sh_log_mark_getkey(void const *item)
-{
-  return ((const struct sh_mark_event *)item)->label->str;
-}
-
-int sh_log_mark_add (const char * label, time_t interval, const char * qlabel)
-{
-  struct sh_mark_event * event;
-
-  if (!(marklist))
-    {
-      marklist = zAVLAllocTree(sh_log_mark_getkey, zAVL_KEY_STRING);
-    }
-
-  event = (struct sh_mark_event *) zAVLSearch(marklist, label);
-  if (event)
-    {
-      event->interval     = interval;
-      sh_string_destroy(&(event->queue_id));
-      event->queue_id     = sh_string_new_from_lchar(qlabel, strlen(qlabel));
-      return 0;
-    }
-
-  event = SH_ALLOC(sizeof(struct sh_mark_event));
-
-  event->last_seen      = time(NULL);
-  event->interval       = interval;
-  event->delay          = 0;
-  event->last_reported  = 0;
-  event->label          = sh_string_new_from_lchar(label, strlen(label));
-  event->queue_id       = sh_string_new_from_lchar(qlabel, strlen(qlabel));
-
-  if (0 != zAVLInsert(marklist, event))
-    {
-      sh_marklist_free(event);
-      return -1;
-    }
-  return 0;
-}
-
-void sh_log_mark_update (sh_string * label, time_t timestamp)
-{
-  struct sh_mark_event * event = 
-    (struct sh_mark_event *) zAVLSearch (marklist, sh_string_str(label));
-
-  DEBUG("debug: running mark update for %s\n", sh_string_str(label));
- 
-  if (event)
-    {
-      DEBUG("debug: updating, timestamp %lu, last_seen %lu, interval %d\n",
-	    (unsigned long)timestamp, (unsigned long) event->last_seen,
-	    (int)event->interval);
-
-      if ((timestamp > event->last_seen) && 
-	  (event->interval < (timestamp - event->last_seen)) &&
-	  (timestamp > event->last_reported) && 
-	  (event->interval < (timestamp - event->last_reported)))
-	{
-	  event->delay        = timestamp - event->last_seen;
-	  DEBUG("debug: updating delay to %d\n", (int) event->delay);
-	}
-      event->last_seen    = timestamp;
-    }
-  return;
-}
-
-/* This should allow to get all overdue labels with a for loop like:
- *   for (label = sh_log_mark_first(); label; label = sh_log_mark_next()) {} 
- */
-
-static zAVLCursor mark_cursor;
-
-static struct sh_mark_event * sh_log_mark_cursor(time_t * delay, time_t now, 
-						 struct sh_mark_event * event)
-{
-  while (event)
-    {
-      DEBUG("debug: echeck, delay %d, now %lu, last_seen %lu, reported %lu\n",
-	    (int) event->delay,
-	    (unsigned long)now, (unsigned long) event->last_seen,
-	    (unsigned long)event->last_reported);
-      if (event->delay > 0)
-	{
-	  DEBUG("debug: event delay > 0, value %d\n", (int) event->delay);
-	  *delay = event->delay;
-	  event->delay = 0;
-	  event->last_reported = time(NULL);
-	  return event;
-	}
-      else if ((now > event->last_seen) && 
-	       (now > event->last_reported) &&
-	       (event->interval < (now - event->last_seen)) &&
-	       (event->interval < (now - event->last_reported))
-	       )
-	{
-	  DEBUG("debug: event delay 0, now %lu, last_seen %lu, reported %lu\n",
-		(unsigned long)now, (unsigned long) event->last_seen,
-		(unsigned long)event->last_reported);
-	  *delay = now - event->last_seen;
-	  event->delay = 0;
-	  /* Subtract 1 sec to prevent accumulation of the
-	   * one second offset. */
-	  event->last_reported = time(NULL) - 1;
-	  return event;
-	}
-      event = (struct sh_mark_event *) zAVLNext(&mark_cursor);
-    }
-
-  return NULL;
-}
-
-struct sh_mark_event * sh_log_mark_first(time_t * delay, time_t now)
-{
-  struct sh_mark_event * event = 
-    (struct sh_mark_event *) zAVLFirst(&mark_cursor, marklist);
-  
-  return sh_log_mark_cursor (delay, now, event);
-}
-
-struct sh_mark_event * sh_log_mark_next(time_t * delay, time_t now)
-{
-  struct sh_mark_event * event = 
-    (struct sh_mark_event *) zAVLNext(&mark_cursor);
-  
-  return sh_log_mark_cursor (delay, now, event);
-}
-
-static int sh_mark_default_severity = SH_ERR_SEVERE;
-
-int sh_log_set_mark_severity (const char * str)
-{
-  int val = sh_error_convert_level(str);
-  if (val < 0)
-    return -1;
-  sh_mark_default_severity = val;
-  return 0;
-}
-
-static struct sh_mark_event ** dummy_event;
-
-void sh_log_mark_check()
-{
-  struct sh_mark_event * event;
-  time_t now = time(NULL);
-  time_t delay;
-
-  /* variable 'event' might be clobbered by 'longjmp' or 'vfork'
-   */
-  dummy_event = &event;
-
-  DEBUG("debug: running mark check\n"); 
-  for (event = sh_log_mark_first(&delay, now); event; 
-       event = sh_log_mark_next (&delay, now)) 
-    {
-      int severity;
-      sh_string * alias;
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-
-      severity = sh_log_lookup_severity(sh_string_str(event->queue_id));
-      if (severity < 0)
-	severity = sh_mark_default_severity;
-
-      DEBUG("debug: mark check: queue %s, severity %d\n", 
-	    sh_string_str(event->queue_id), severity); 
-      sh_error_handle (severity, 
-		       FIL__, __LINE__, 0, MSG_LOGMON_MARK, 
-		       sh_string_str(event->label), 
-		       (unsigned long) delay);
-      alias = sh_log_lookup_alias(sh_string_str(event->queue_id));
-      if (alias)
-	{
-	  sh_error_mail (sh_string_str(alias), severity, 
-			 FIL__, __LINE__, 0, MSG_LOGMON_MARK, 
-			 sh_string_str(event->label), 
-			 (unsigned long) delay);
-	}
-
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-    }
-  return;
-}
-
-#endif
Index: trunk/src/sh_log_parse_apache.c
===================================================================
--- trunk/src/sh_log_parse_apache.c	(revision 591)
+++ 	(revision )
@@ -1,472 +1,0 @@
-/**************************************
- **
- ** PARSER RULES
- **
- ** (a) must set record->host 
- **     (eventually to dummy value)
- **
- ** (b) must set record->prefix
- **     (itoa(status)) 
- **
- **
- **************************************/
-
-/* for strptime */
-#define _XOPEN_SOURCE 500
-
-#include "config_xor.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include <time.h>
-
-#ifdef USE_LOGFILE_MONITOR
-
-#undef  FIL__
-#define FIL__  _("sh_log_parse_apache.c")
-
-/* Debian/Ubuntu: libpcre2-dev */
-#define PCRE2_CODE_UNIT_WIDTH 8
-#ifdef HAVE_PCRE2_PCRE2_H
-#include <pcre2/pcre2.h>
-#else
-#include <pcre2.h>
-#endif
-
-#include "samhain.h"
-#include "sh_pthread.h"
-#include "sh_log_check.h"
-#include "sh_utils.h"
-#include "sh_string.h"
-
-extern int flag_err_debug;
-
-struct sh_fileinfo_apache {
-  pcre2_code * line_regex;
-  PCRE2_SIZE * line_ovector;         /* captured substrings     */
-  int          line_ovecnum;         /* how many captured       */
-  
-  int    pos_host;
-  int    pos_status;
-  int    pos_time;
-  char * format_time;
-};
-
-static const char lf_error0[]    = N_("%error");
-static const char lf_common0[]   = N_("%h %l %u %t \"%r\" %>s %b");
-static const char lf_combined0[] = N_("%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"");
-
-/* This variable is not used anywhere. It only exist
- * to assign &new to them, which keeps gcc from
- * putting it into a register, and avoids the 'clobbered
- * by longjmp' warning. And no, 'volatile' proved insufficient.
- */
-void * sh_dummy_65_new = NULL;
-void * sh_dummy_66_fti = NULL;
-void * sh_dummy_67_ftr = NULL;
-
-void * sh_eval_fileinfo_apache(char * str)
-{
-  struct sh_fileinfo_apache * result = NULL;
-  unsigned int i, quotes;
-  unsigned int nfields = 64;
-  size_t       lengths[64];
-  char *       new = NULL;
-  char **      splits;
-  char *       token;
-  sh_string  * re_string;
-  char *       p;
-  volatile int          p_host = -1;
-  volatile int          p_status = -1;
-  volatile int          p_time = -1;
-  char                * f_time = NULL;
-  int          error;
-  size_t       erroffset;
-  
-  /* Take the address to keep gcc from putting them into registers. 
-   * Avoids the 'clobbered by longjmp' warning. 
-   */
-  sh_dummy_65_new = (void*) &new;
-  sh_dummy_66_fti = (void*) &f_time;
-  sh_dummy_67_ftr = (void*) &result;
-
-  if (0 == strncmp("common", str, 6))
-    {
-      new    = sh_util_strdup(_(lf_common0));
-    }
-  else if (0 == strncmp("combined", str, 8))
-    {
-      new    = sh_util_strdup(_(lf_combined0));
-    }
-  else if (0 == strncmp("error", str, 8))
-    {
-      new    = sh_util_strdup(_(lf_error0));
-    }
-  else
-    {
-      new    = sh_util_strdup(str);
-    }
-
-  if (flag_err_debug == S_TRUE)
-    {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		      new,
-		      _("eval_fileinfo"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-    }
-
-  splits = split_array_ws(new, &nfields, lengths);
-
-  if (nfields < 1)
-    {
-      SH_FREE(splits);
-      SH_FREE(new);
-      return NULL;
-    }
-
-  /* Build the regex string re_string
-   */
-  re_string =  sh_string_new(0);
-  sh_string_add_from_char(re_string, "^");
-
-  for (i = 0; i < nfields; ++i)
-    {
-
-      if (i > 0)
-	sh_string_add_from_char(re_string, " ");
-
-      if (splits[i][0] != '"')
-	quotes = 0;
-      else
-	quotes = 1;
-
-      if (quotes && lengths[i] > 1 && splits[i][lengths[i]-1] == '"')
-	{
-	  splits[i][lengths[i]-1] = '\0'; /* cut trailing quote */
-	  token = &(splits[i][1]);
-	} else {
-	  token = splits[i];
-	}
-
-      if(quotes)
-	{
-	  if(strcmp(token, "%r") == 0 || 
-	     strstr(token, _("{Referer}")) != NULL || 
-             strstr(token, _("{User-Agent}")) != NULL ||
-	     strstr(token, _("{X-Forwarded-For}")) != NULL )
-	    {
-	      /*
-	      p = "\"([^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\"";
-	      sh_string_add_from_char(re_string, p);
-	      */
-	      sh_string_add_from_char(re_string, "\"([^");
-	      sh_string_add_from_char(re_string, "\"\\\\");
-	      sh_string_add_from_char(re_string, "]*");
-	      sh_string_add_from_char(re_string, "(?:");
-	      sh_string_add_from_char(re_string, "\\\\.");
-	      sh_string_add_from_char(re_string, "[^\"");
-	      sh_string_add_from_char(re_string, "\\\\]*");
-	      sh_string_add_from_char(re_string, ")*)\"");
-	    }    
-	  else
-	    {
-	      sh_string_add_from_char(re_string, "(");
-	      sh_string_add_from_char(re_string, "\\S+");
-	      sh_string_add_from_char(re_string, ")");
-	    }
-	}
-      else if (token[0] == 'R' && token[1] == 'E' && token[2] == '{' && token[strlen(token)-1] == '}') 
-	{
-	  char * lb =  strchr(token, '{');
-	  char * rb = strrchr(token, '}');
-
-	  if (lb && rb)
-	    {
-	      ++lb; *rb = '\0';
-	      sh_string_add_from_char(re_string, lb);
-	    }
-	}
-      else if (token[0] == '%' && token[strlen(token)-1] == 't') 
-	{
-	  char * lb = strchr(token, '{');
-	  char * rb = strchr(token, '}');
-
-	  sh_string_add_from_char(re_string, "\\[");
-	  sh_string_add_from_char(re_string, "([^");
-	  sh_string_add_from_char(re_string, "(\\]");
-	  sh_string_add_from_char(re_string, "]+)");
-	  sh_string_add_from_char(re_string, "\\]");
-
-	  p_time = i+1;
-	  if (lb && rb)
-	    {
-	      ++lb; *rb = '\0';
-	      f_time = sh_util_strdup(lb);
-	    }
-	  else
-	    {
-	      f_time = sh_util_strdup(_("%d/%b/%Y:%T"));
-	    }
-	}
-      else if (token[0] == '%' && token[1] == 'e' && 0 == strcmp(token, _("%error"))) 
-	{
-	  sh_string_add_from_char(re_string, "\\[");
-	  sh_string_add_from_char(re_string, "([^");
-	  sh_string_add_from_char(re_string, "]");
-	  sh_string_add_from_char(re_string, "]+)");
-	  sh_string_add_from_char(re_string, "\\]");
-
-	  p_time = i+1; f_time = sh_util_strdup(_("%a %b %d %T %Y")); ++i;
-	  sh_string_add_from_char(re_string, " ");
-
-	  sh_string_add_from_char(re_string, "\\[");
-	  sh_string_add_from_char(re_string, "([^");
-	  sh_string_add_from_char(re_string, "]");
-	  sh_string_add_from_char(re_string, "]+)");
-	  sh_string_add_from_char(re_string, "\\]");
-
-	  p_status = i+1;
-	  sh_string_add_from_char(re_string, " ");
-
-	  p = "(.+)";
-	  sh_string_add_from_char(re_string, p);
-
-	  nfields = 3;
-
-	  break;
-	}
-      else
-	{
-	  sh_string_add_from_char(re_string, "(");
-	  sh_string_add_from_char(re_string, "\\S+");
-	  sh_string_add_from_char(re_string, ")");
-	  if (token[0] == '%' && token[strlen(token)-1] == 's')
-	    p_status = i+1;
-	  else if (token[0] == '%' && token[strlen(token)-1] == 'v')
-	    p_host = i+1;
-	}
-    }
-  sh_string_add_from_char(re_string, "$");
-
-  if (flag_err_debug == S_TRUE)
-    {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		      sh_string_str(re_string),
-		      _("eval_fileinfo"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-    }
-
-  result = SH_ALLOC(sizeof(struct sh_fileinfo_apache));
-  result->line_regex = pcre2_compile((PCRE2_SPTR8)sh_string_str(re_string), sh_string_len(re_string), 0, 
-				    &error, &erroffset, NULL);
-  if (!(result->line_regex))
-    {
-      sh_string * msg =  sh_string_new(0);
-      sh_string_add_from_char(msg, _("Bad regex: "));
-      sh_string_add_from_char(msg, sh_string_str(re_string));
-      
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		      sh_string_str(msg),
-		      _("eval_fileinfo"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-
-      SH_FREE(result);
-      SH_FREE(splits);
-      SH_FREE(new);   
-      sh_string_destroy(&msg);
-      sh_string_destroy(&re_string);
-
-      return NULL;
-    }
-  sh_string_destroy(&re_string);
-
-  result->line_ovector  = NULL;
-  result->line_ovecnum  = nfields;
-  result->pos_host      = p_host;
-  result->pos_status    = p_status;
-  result->pos_time      = p_time;
-  result->format_time   = f_time;
-
-  SH_FREE(splits);
-  SH_FREE(new);
-  return (void*)result;
-}
-
-struct sh_logrecord * sh_parse_apache (sh_string * logline, void * fileinfo)
-{
-  static struct tm old_tm;
-  static time_t    old_time;
-
-  char         tstr[128];
-  char         sstr[128];
-  char       * hstr;
-  int          res;
-  unsigned char **hstr_addr = (unsigned char **) &hstr;
-  size_t       hstr_len;
-
-  struct sh_fileinfo_apache * info = (struct sh_fileinfo_apache *) fileinfo;
-
-  pcre2_match_data * match_data = NULL;
-  
-  if (sh_string_len(logline) > 0 && flag_err_debug == S_TRUE)
-    {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		      sh_string_str(logline),
-		      _("sh_parse_apache"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-    }
-
-  if (logline == NULL || info == NULL)
-    {
-      return NULL;
-    }
-
-  match_data = pcre2_match_data_create_from_pattern(info->line_regex, NULL);
-  
-  res = pcre2_match(info->line_regex,
-		    (PCRE2_SPTR8)sh_string_str(logline), (int)sh_string_len(logline), 0,
-		    0, match_data, NULL);
-
-  if (res == 1+info->line_ovecnum) /* successful match */
-    {
-      struct sh_logrecord * record;
-      time_t timestamp = 0;
-      size_t size;
-
-      info->line_ovector = pcre2_get_ovector_pointer(match_data);
-
-      if (info->line_ovector[0] > info->line_ovector[1]) /* guard against patterns using \K */
-	{
-	  char msg[128];
-	  sl_snprintf(msg, sizeof(msg), _("Matching error: start > end"));
-	  
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-			  msg,
-			  _("sh_parse_apache"));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  goto the_end;
-	}
-      
-      if (info->pos_time > 0)
-	{
-	  size = sizeof(tstr);
-	  res = pcre2_substring_copy_bynumber(match_data,  info->pos_time,
-					      (PCRE2_UCHAR8 *)tstr, &size);
-	  if (res != 0)
-	    goto corrupt;
-	}
-      else
-	{
-	  res = -1;
-	  timestamp = 0;
-	  info->format_time = sh_util_strdup(_("%d/%b/%Y:%T"));
-	  sl_strlcpy(tstr, _("01/Jan/1970:00:00:00"), sizeof(tstr));
-	}
-
-      if (res == 0)
-	{
-	  struct tm btime;
-	  char * ptr = NULL;
-
-	  memset(&btime, 0, sizeof(struct tm));
-	  btime.tm_isdst = -1;
-	  
-	  /* example: 01/Jun/2008:07:55:28 +0200 */
-
-	  ptr = /*@i@*/strptime(tstr, info->format_time, &btime);
-
-	  if (ptr)
-	    {
-	      timestamp = conv_timestamp(&btime, &old_tm, &old_time);
-	    }
-	  else
-	    goto corrupt;
-	}
-
-      if (info->pos_status > 0)
-	{
-	  size = sizeof(sstr);
-	  res = pcre2_substring_copy_bynumber(match_data,  info->pos_status,
-					      (PCRE2_UCHAR8 *)sstr, &size);
-	  if (res != 0)
-	    goto corrupt;
-	}
-      else
-	{
-	  sl_strlcpy(sstr, _("000"), sizeof(sstr));
-	}
-
-      if (info->pos_host > 0)
-	{
-	  res = pcre2_substring_get_bynumber(match_data, info->pos_host,
-					     hstr_addr, &hstr_len);
-	  if (res != 0)
-	    goto corrupt;
-	}
-      else
-	{
-	  hstr = NULL;
-	}
-
-      record = SH_ALLOC(sizeof(struct sh_logrecord));
-      
-      record->timestamp = timestamp;
-      record->timestr   = sh_string_new_from_lchar(tstr, strlen(tstr));
-
-      if (hstr)
-	record->host = sh_string_new_from_lchar(hstr, hstr_len);
-      else
-	record->host = sh_string_new_from_lchar(sh.host.name, strlen(sh.host.name));
-
-      record->message   = sh_string_new_from_lchar(sh_string_str(logline), 
-						   sh_string_len(logline));
-      record->pid       = PID_INVALID;
-
-      /* does nothing if hstr == NULL */
-      pcre2_substring_free((PCRE2_UCHAR8 *)hstr);
-
-      pcre2_match_data_free(match_data);
-      return record;
-    }
-  else
-    {
-      char msg[128];
-      sl_snprintf(msg, sizeof(msg), _("Matching error: %d"), res);
-      
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		      msg,
-		      _("sh_parse_apache"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-    }
-
-  /* Corrupted logline */
- corrupt:
-
-  {
-    sh_string * msg =  sh_string_new(0);
-    sh_string_add_from_char(msg, _("Corrupt logline: "));
-    sh_string_add_from_char(msg, sh_string_str(logline));
-    
-    SH_MUTEX_LOCK(mutex_thread_nolog);
-    sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		    sh_string_str(msg),
-		    _("sh_parse_apache"));
-    SH_MUTEX_UNLOCK(mutex_thread_nolog);
-    sh_string_destroy(&msg);
-  }
-  
- the_end:
-  pcre2_match_data_free(match_data);
-  return NULL;
-}
-
-/* USE_LOGFILE_MONITOR */
-#endif
Index: trunk/src/sh_log_parse_generic.c
===================================================================
--- trunk/src/sh_log_parse_generic.c	(revision 591)
+++ 	(revision )
@@ -1,120 +1,0 @@
-/**************************************
- **
- ** PARSER RULES
- **
- ** (a) must set record->host 
- **     (eventually to dummy value)
- **
- ** (b) must set record->prefix
- **     (itoa(status)) 
- **
- **
- **************************************/
-
-#include "config_xor.h"
-
-#ifdef USE_LOGFILE_MONITOR
-
-#undef  FIL__
-#define FIL__  _("sh_log_parse_apache.c")
-
-#include <string.h>
-#include <time.h>
-
-/* Debian/Ubuntu: libpcre2-dev */
-#define PCRE2_CODE_UNIT_WIDTH 8
-#ifdef HAVE_PCRE2_PCRE2_H
-#include <pcre2/pcre2.h>
-#else
-#include <pcre2.h>
-#endif
-
-#include "samhain.h"
-#include "sh_log_check.h"
-#include "sh_string.h"
-
-struct sh_fileinfo_generic {
-  pcre2_code        * line_regex;
-  pcre2_match_data  * line_match_data;      /* captured substrings     */
-  int                 line_ovecnum;         /* how many captured       */
-  
-  int    pos_host;
-  int    pos_status;
-  int    pos_time;
-  char * format_time;
-};
-
-static void default_time (struct sh_logrecord * record)
-{
-  struct tm   ts;
-  struct tm * ts_ptr;
-  char   tmp[80];
-  size_t len;
-
-  record->timestamp = time(NULL);
-  
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
-  ts_ptr = localtime_r (&(record->timestamp), &ts);
-#else
-  ts_ptr = localtime(&(record->timestamp));
-  if (ts_ptr)
-    memcpy(&ts, ts_ptr, sizeof(struct tm));
-#endif
-  if (ts_ptr)
-    len = strftime(tmp, sizeof(tmp), _("%Y-%m-%dT%H:%M:%S"), &ts);
-  else
-    {
-      sl_strlcpy(tmp, _("1970-01-01T00:00:00"), sizeof(tmp));
-      len = strlen(tmp);
-    } 
-  record->timestr   = sh_string_new_from_lchar(tmp, len);
-
-  return;
-}
-
-static void default_host (struct sh_logrecord * record)
-{
-  record->host      = sh_string_new_from_lchar(sh.host.name, strlen(sh.host.name));
-  return;
-}
-
-sh_string * sh_read_shell (sh_string * record, struct sh_logfile * logfile)
-{
-  return sh_command_reader (record, logfile);
-}
-
-struct sh_logrecord * sh_parse_shell (sh_string * logline, void * fileinfo)
-{
-  (void) fileinfo;
-
-  if (logline)
-    {
-      struct sh_logrecord * record = SH_ALLOC(sizeof(struct sh_logrecord));
-
-      default_time(record);
-      default_host(record);
-
-      record->message   = sh_string_new_from_lchar(sh_string_str(logline), 
-						   sh_string_len(logline));
-      record->pid       = PID_INVALID;
-      return record;
-    }
-  return NULL;
-}
-
-void * sh_eval_fileinfo_generic(char * str)
-{
-  (void) str;
-
-  return NULL;
-}
-
-struct sh_logrecord * sh_parse_generic (sh_string * logline, void * fileinfo)
-{
-  (void) logline;
-  (void) fileinfo;
-
-  return NULL;
-}
-
-#endif
Index: trunk/src/sh_log_parse_pacct.c
===================================================================
--- trunk/src/sh_log_parse_pacct.c	(revision 591)
+++ 	(revision )
@@ -1,357 +1,0 @@
-/**************************************
- **
- ** PARSER RULES
- **
- ** (a) must set record->host 
- **     (eventually to dummy value)
- **
- ** (b) must set record->prefix
- **     (command) 
- **
- **
- **************************************/
-
-/* Based on the GNU Accounting Utilities, which is distributed with the
- * following copyright: 
- */
-
-/* Copyright (C) 1993, 1996, 1997, 2003, 2005 Free Software Foundation, Inc.
- *
- * This file is part of the GNU Accounting Utilities
- *
- * The GNU Accounting Utilities are free software; you can redistribute
- * them and/or modify them under the terms of the GNU General Public
- * License as published by the Free Software Foundation; either version
- * 2, or (at your option) any later version.
- *
- * The GNU Accounting Utilities are distributed in the hope that they will
- * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
- * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with the GNU Accounting Utilities; see the file COPYING.  If
- * not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
- * MA 02139, USA.  */
-
-#include "config_xor.h"
-
-#include <ctype.h>
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <pwd.h>
-#include <dirent.h>
-
-#if defined(USE_LOGFILE_MONITOR) && defined(HAVE_SYS_ACCT_H)
-
-#include <sys/acct.h>
-
-#include "samhain.h"
-#include "sh_pthread.h"
-#include "sh_log_check.h"
-#include "sh_utils.h"
-#include "sh_string.h"
-
-#undef  FIL__
-#define FIL__  _("sh_log_parse_pacct.c")
-
-extern int flag_err_debug;
-
-#ifndef ACCT_COMM
-#define ACCT_COMM 16
-#endif
-#ifndef AHZ
-#define AHZ 100
-#endif
-
-#if defined(ACUTIME_COMPT) || defined(ACSTIME_COMPT) || defined(ACETIME_COMPT)
-static double comp_t_2_double (comp_t ct)
-{
-  unsigned long out = 0;
-
-  out = ct & 017777;
-  ct >>= 13;
-
-  while (ct) {
-    ct--;
-    out <<= 3;
-  }
-  
-  return (double) out;
-}
-#endif
-
-#ifdef ACUTIME_COMPT
-# define ACUTIME_2_DOUBLE(x) (comp_t_2_double(x))
-#else
-# define ACUTIME_2_DOUBLE(x) ((double)(x))
-#endif
-
-#ifdef ACSTIME_COMPT
-# define ACSTIME_2_DOUBLE(x) (comp_t_2_double(x))
-#else
-# define ACSTIME_2_DOUBLE(x) ((double)(x))
-#endif
-
-#ifdef ACETIME_COMPT
-# define ACETIME_2_DOUBLE(x) (comp_t_2_double(x))
-#else
-# define ACETIME_2_DOUBLE(x) ((double)(x))
-#endif
-
-
-static void expand_flags(char flag, char * out)
-{
-  int i = 0;
-
-#define	BIT(flg, ch)	if (flag & flg) out[i] = ch; else out[i] = ' '; ++i
-
-  BIT(ASU, 'S');
-  BIT(AFORK, 'F');
-#ifdef ACOMPAT
-  BIT(ACOMPAT, 'C');
-#endif
-#ifdef ACORE
-  BIT(ACORE, 'D');
-#endif
-#ifdef AXSIG
-  BIT(AXSIG, 'X');
-#endif
-
-  out[i] = '\0';
-  return;
-}
-
-static char * uid_name (int uid)
-{
-  static int  userid   = 0;
-  static char user[16] = "";
-
-  if (uid == userid && user[0] != '\0')
-    {
-      return user;
-    }
-  else
-    {
-      struct passwd *thispw = getpwuid (uid);
-      if (thispw)
-	sl_strlcpy (user, thispw->pw_name, sizeof(user));
-      else
-	sl_snprintf(user, sizeof(user), "%d", uid);
-      user[sizeof(user)-1] = '\0';
-      userid = uid;
-    }
-  return user;
-}
-
-struct dev_struct {
-  char * device;
-  long   dev_id;
-  struct dev_struct * next;
-};
-static struct dev_struct * devicelist = NULL;
-
-static void add_devices(const char * dir)
-{
-  DIR *  mdir;
-  char   dirl[256];
-
-  sl_strlcpy(dirl, dir, sizeof(dirl));
-  dirl[sizeof(dirl)-1] = '\0';
-
-  mdir = opendir(dir);
-  
-  if (mdir)
-    {
-      char * path;
-      size_t len;
-      struct dirent * dent;
-      struct stat buf;
-
-      while (NULL != (dent = readdir(mdir)))
-	{
-	  if (0 == strcmp(dent->d_name, "."))
-	    continue;
-	  if (0 == strcmp(dent->d_name, ".."))
-	    continue;
-	  len = strlen(dir) + strlen(dent->d_name) + 2;
-	  path = SH_ALLOC(len);
-	  snprintf(path, len, "%s/%s", dir, dent->d_name);
-	  if (0 == lstat(path, &buf) && S_ISCHR(buf.st_mode))
-	    {
-	      struct dev_struct * dstruct;
-	      dstruct = SH_ALLOC(sizeof(struct dev_struct));
-	      /* eliminate leading '/dev/' */
-	      memmove(path, &path[5], strlen(path)-4); 
-	      dstruct->device = path;
-	      dstruct->dev_id = buf.st_rdev;
-	      dstruct->next   = devicelist;
-	      devicelist      = dstruct;
-	    }
-	  else
-	    {
-	      SH_FREE(path);
-	    }
-	}
-      closedir(mdir);
-    }
-  return;
-}
-
-static char * dev_name(long tty)
-{
-  struct dev_struct * dstruct;
-
-  if (!devicelist)
-    {
-      add_devices("/dev");
-      add_devices("/dev/pts");
-      add_devices("/dev/pty");
-      add_devices("/dev/ptym");
-    }
-
-  dstruct = devicelist;
-  while (dstruct)
-    {
-      if (dstruct->dev_id == tty)
-	return dstruct->device;
-      dstruct = dstruct->next;
-    }
-  return "??";
-}
-
-#if defined(__linux__) && defined(HAVE_ACCT_V3)
-#  define STRUCT_ACCT struct acct_v3
-#elif defined(__FreeBSD__) && defined(HAVE_ACCTV2)
-#  define STRUCT_ACCT struct acctv2
-#else
-#  define STRUCT_ACCT struct acct
-#endif
-
-/* This looks strange, but it's real ANSI C. */
-extern STRUCT_ACCT pacct_rd_never_used;
-#define COMM_LEN ((int) sizeof (pacct_rd_never_used.ac_comm))
-
-sh_string * sh_read_pacct (sh_string * record, struct sh_logfile * logfile)
-{
-  STRUCT_ACCT rec;
-
-  if (NULL != sh_binary_reader ((void*) &rec, sizeof(STRUCT_ACCT), logfile))
-    {
-      time_t btime = (time_t) rec.ac_btime;
-      double ut    = ACUTIME_2_DOUBLE (rec.ac_utime);
-      double st    = ACSTIME_2_DOUBLE (rec.ac_stime);
-      char   fl[6];
-      char   comm[COMM_LEN+1];
-      int    i;
-      char   out[64+COMM_LEN+1+5+8+8+32+4+19+7]; /* see printf format below */
-      
-#if defined(ac_flagx)
-      /* cppcheck-suppress syntaxError */
-      expand_flags(rec.ac_flagx, fl);
-#else
-      expand_flags(rec.ac_flag,  fl);
-#endif
-      
-      /* ac_comm may not be null terminated
-       */
-      for (i = 0; i < COMM_LEN; i++)
-	{
-	  if (rec.ac_comm[i] == '\0')
-	    {
-	      comm[i] = '\0';
-	      break;
-	    }
-	  if (! isprint (rec.ac_comm[i]))
-	    comm[i] = '?';
-	  else
-	    comm[i] = rec.ac_comm[i];
-	}
-      comm[COMM_LEN] = '\0';
-
-      sl_snprintf (out, sizeof(out),
-		   "%ld:%-*.*s %5.5s %-8.8s %-8.8s %6.2f secs %-19.19s",
-		   btime,
-		   COMM_LEN, COMM_LEN, comm, fl, 
-		   uid_name(rec.ac_uid), 
-		   dev_name((long)rec.ac_tty),
-		   ((ut + st) / (double) AHZ),
-		   ctime (&btime));
-
-
-      sh_string_set_from_char(record, out);
-      return record;
-    }
-
-  if (record)
-    sh_string_destroy(&record);
-  return NULL;
-}
-
-void * sh_dummy_294_record = NULL;
-
-struct sh_logrecord * sh_parse_pacct (sh_string * logline, void * fileinfo)
-{
-  char * p;
-  char * endptr;
-  unsigned long ltime;
-  struct sh_logrecord * record = NULL;
-
-  (void) fileinfo;
-
-  sh_dummy_294_record = (void *) &record;
-
-  if (sh_string_len(logline) > 0 && flag_err_debug == S_TRUE)
-    {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		      sh_string_str(logline),
-		      _("sh_parse_pacct"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-    }
-
-  sh_dummy_294_record = NULL;
-  
-  p = strchr(sh_string_str(logline), ':');
-
-  if (!p || p == sh_string_str(logline))
-    return NULL;
-
-  ltime = strtoul(sh_string_str(logline), &endptr, 10);
-  if (p != endptr)
-    return NULL;
-		  
-  ++p; /* points to first char of pacct record */
-  
-  if (*p != '\0')
-    {
-      size_t lengths[7];
-      unsigned int  fields = 7;
-      char ** array;
-      sh_string * message = sh_string_new_from_lchar(p, strlen(p));
-      array = split_array_ws(p, &fields, lengths);
-
-      if (fields == 7)
-	{
-	  record = SH_ALLOC(sizeof(struct sh_logrecord));
-
-	  record->timestamp = ltime;
-	  record->timestr   = sh_string_new_from_lchar(array[6], lengths[6]);
-	  record->message   = message;
-	  record->pid       = 0;
-	  record->host      = sh_string_new_from_lchar(sh.host.name, strlen(sh.host.name));
-	}
-      else
-	{
-	  sh_string_destroy(&message);
-	}
-      SH_FREE(array);
-    }
-  return record;
-}
-/* USE_LOGFILE_MONITOR */
-#endif
Index: trunk/src/sh_log_parse_samba.c
===================================================================
--- trunk/src/sh_log_parse_samba.c	(revision 591)
+++ 	(revision )
@@ -1,104 +1,0 @@
-/**************************************
- **
- ** PARSER RULES
- **
- ** (a) must set record->host 
- **     (eventually to dummy value)
- **
- ** (b) must set record->prefix
- **     (command) 
- **
- **
- **************************************/
-
-/* for strptime */
-#define _XOPEN_SOURCE
-
-#include "config_xor.h"
-#include <string.h>
-
-#if defined(HOST_IS_SOLARIS)
-/* For 'struct timeval' in <sys/time.h> */
-#define __EXTENSIONS__
-#endif
-
-#include <time.h>
-
-#if defined(USE_LOGFILE_MONITOR)
-
-#include "samhain.h"
-#include "sh_pthread.h"
-#include "sh_log_check.h"
-#include "sh_string.h"
-
-#undef  FIL__
-#define FIL__  _("sh_log_parse_samba.c")
-
-
-sh_string * sh_read_samba (sh_string * record, struct sh_logfile * logfile)
-{
-  return sh_cont_reader (record, logfile, " \t");
-}
-
-struct sh_logrecord * sh_parse_samba (sh_string * logline, void * fileinfo)
-{
-  static struct tm old_tm;
-  static time_t    old_time;
-
-  struct sh_logrecord * record = NULL;
-
-  static const char *    format0_1 = N_("[%Y/%m/%d %T");
-  static char   format_1[16]; 
-  static int    format_init = 0;
-
-  (void) fileinfo;
-
-  if (!format_init)
-    {
-      sl_strlcpy(format_1, _(format0_1), sizeof(format_1));
-      format_init = 1;
-    }
-
-  if (logline && sh_string_len(logline) > 0)
-    {
-      size_t lengths[3];
-      unsigned int  fields = 3;
-      char ** array;
-      char * p = strchr(sh_string_str(logline), ',');
-
-      *p = '\0'; ++p;
-      array = split_array_ws(p, &fields, lengths);
-
-      if (fields == 3)
-	{
-	  struct tm btime;
-	  char * ptr;
-
-	  memset(&btime, 0, sizeof(struct tm));
-	  btime.tm_isdst = -1;
-
-	  ptr = strptime(sh_string_str(logline), format_1, &btime);
-
-	  if (ptr && *ptr == '\0') /* no error, whole string consumed */
-	    {
-	      record = SH_ALLOC(sizeof(struct sh_logrecord));
-
-	      record->timestamp = conv_timestamp(&btime, &old_tm, &old_time);
-
-	      p = sh_string_str(logline); ++p;
-	  
-	      record->timestr   = sh_string_new_from_lchar(p, strlen(p));
-	      
-	      record->message   = sh_string_new_from_lchar(array[2], lengths[2]);
-	  
-	      record->pid       = 0;
-	      record->host      = sh_string_new_from_lchar(sh.host.name, 
-							   strlen(sh.host.name));
-	    }
-	}
-      SH_FREE(array);
-    }
-  return record;
-}
-
-#endif
Index: trunk/src/sh_log_parse_syslog.c
===================================================================
--- trunk/src/sh_log_parse_syslog.c	(revision 591)
+++ 	(revision )
@@ -1,190 +1,0 @@
-/**************************************
- **
- ** PARSER RULES
- **
- ** (a) must set record->host 
- **     (eventually to dummy value)
- **
- ** (b) must set record->prefix
- **     (eventually to dummy value) 
- **
- **
- **************************************/
-
-/* for strptime */
-#define _XOPEN_SOURCE
-
-#include "config_xor.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#if defined(HOST_IS_SOLARIS)
-/* For 'struct timeval' in <sys/time.h> */
-#define __EXTENSIONS__
-#endif
-
-#include <sys/types.h>
-#include <time.h>
-
-#ifdef USE_LOGFILE_MONITOR
-
-#include "samhain.h"
-#include "sh_pthread.h"
-#include "sh_log_check.h"
-#include "sh_utils.h"
-#include "sh_string.h"
-
-#undef  FIL__
-#define FIL__  _("sh_log_parse_syslog.c")
-
-static int hidepid = 0;
-extern int flag_err_debug;
-
-int sh_get_hidepid()
-{
-  return hidepid;
-}
-
-int sh_set_hidepid(const char *s)
-{
-  return sh_util_flagval(s, &hidepid);
-}
-
-struct sh_logrecord * sh_parse_syslog (sh_string * logline, void * fileinfo)
-{
-  static const char *    format0_1 = N_("%b %d %T");
-  static const char *    format0_2 = N_("%Y-%m-%dT%T");
-
-  static char   format_1[16]; 
-  static char   format_2[16];
-  static int    format_init = 0;
-
-  static struct tm old_tm;
-  static time_t    old_time;
-
-  const unsigned int Tpos = 10;
-  volatile unsigned int tlen = 16;
-
-  (void) fileinfo;
-
-  if (!format_init)
-    {
-      sl_strlcpy(format_1, _(format0_1), sizeof(format_1));
-      sl_strlcpy(format_2, _(format0_2), sizeof(format_2));
-      format_init = 1;
-    }
-
-
-  if (flag_err_debug == S_TRUE && sh_string_len(logline) > 0)
-    {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		      logline->str,
-		      _("sh_parse_syslog"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-    }
-
-  if (logline && sh_string_len(logline) > tlen)
-    {
-      struct tm btime;
-      char * ptr;
-      int    flag;
-      size_t lengths[3];
-
-      memset(&btime, 0, sizeof(struct tm));
-      btime.tm_isdst = -1;
-
-      /* This is RFC 3164. 
-       */
-      if (logline->str[Tpos] != 'T')
-	{
-	  logline->str[tlen-1] = '\0';
-	  ptr = /*@i@*/strptime(logline->str, format_1, &btime);
-	}
-
-      /* RFC 3339 describes an alternative timestamp format.
-       * Unfortunately, POSIX strptime() does not support reading 
-       * the TZ offset.
-       */
-      else
-	{
-	  ptr = strptime(logline->str, format_2, &btime);
-	  if (ptr)
-	    { 
-	      tlen = 20;
-	      if (*ptr && *ptr != ' ') {
-		do { ++ptr; ++tlen; } while (*ptr && *ptr != ' ');
-		if (*ptr == ' ') *ptr = '\0'; 
-	      }
-	    }
-	}
-
-      if (ptr && *ptr == '\0') /* no error, whole string consumed */
-	{
-	  unsigned int  fields = 3; /* host program(\[pid\])+: message */
-	  char ** array  = split_array_ws(&(logline->str[tlen]), &fields, lengths);
-
-	  if (fields == 3)
-	    {
-	      struct sh_logrecord * record = SH_ALLOC(sizeof(struct sh_logrecord));
-
-	      record->timestamp = conv_timestamp(&btime, &old_tm, &old_time);
-	      record->timestr   = sh_string_new_from_lchar(logline->str, (tlen-1)); 
-
-	      /* host
-	       */
-	      record->host = sh_string_new_from_lchar(array[0], lengths[0]);
-
-	      /* program and pid
-	       */
-	      if (NULL != (ptr = strchr(array[1], '[')))
-		{
-		  *ptr = '\0';
-		  ++ptr;
-		  record->pid = (pid_t) atoi(ptr);
-
-		  if (hidepid == 0 || !*ptr) {
-		    --ptr;
-		    *ptr = '[';
-		  } else {
-		    *ptr = '\0'; /* overwrite first digit */
-		    --ptr;
-		    *ptr = ':';  /* overwrite ex-':' */
-		    lengths[1] = strlen(array[1]);
-		  }
-		}
-	      else
-		{
-		  flag = 0;
-		  ptr = array[1];
-		  if (ptr[lengths[1]] == ':') {
-		    ptr[lengths[1]] = '\0';
-		    flag = 1;
-		  }
-		  record->pid = PID_INVALID;
-		  if (flag == 1) {
-		    ptr[lengths[1]] = ':';
-		  }
-		}
-
-	      /* message
-	       */
-	      record->message = sh_string_new_from_lchar3(array[1], lengths[1],
-							  " ", 1,
-							  array[2], lengths[2]);
-
-	      SH_FREE(array);
-	      return record;
-	    }
-	  SH_FREE(array);
-	}
-    }
-  /* corrupted logline
-   */
-  return NULL;
-}
-
-/* USE_LOGFILE_MONITOR */
-#endif
Index: trunk/src/sh_log_repeat.c
===================================================================
--- trunk/src/sh_log_repeat.c	(revision 591)
+++ 	(revision )
@@ -1,572 +1,0 @@
-#include "config_xor.h"
-
-#ifdef USE_LOGFILE_MONITOR
-
-#include <string.h>
-#include <ctype.h>
-
-#undef  FIL__
-#define FIL__  _("sh_log_repeat.c")
-
-#include "samhain.h"
-#include "sh_pthread.h"
-#include "sh_utils.h"
-#include "sh_string.h"
-#include "sh_log_check.h"
-#include "sh_log_evalrule.h"
-
-#define SH_NHIST   12
-#define SH_NFINT    5
-#define SH_NFIELDS  5*sizeof(SINT32) /* 16 */
-#define SH_NBLOCK  63
-
-typedef enum
-{
-  SH_GFLAG_EMAIL = 1 << 0,
-  SH_GFLAG_PATH  = 1 << 1,
-  SH_GFLAG_IP    = 1 << 2,
-  SH_GFLAG_FQDN  = 1 << 3,
-  SH_GFLAG_NUM   = 1 << 4,
-  SH_GFLAG_ELSE  = 1 << 5,
-
-  SH_GFLAG_XNUM  = 1 << 6,
-  SH_GFLAG_CHAR  = 1 << 7,
-  SH_GFLAG_USC   = 1 << 8
-} SH_GFlags;
-
-
-/* 64 bytes 
- */
-struct gestalt {
-  unsigned char hist[SH_NHIST];      /* time histogram 12 minutes   */
-  union {
-    unsigned char flags[SH_NFIELDS]; /* flags indicating field type */
-    SINT32        flint[SH_NFINT];
-  } f;
-  UINT16      sum[SH_NFIELDS];     /* checksum of field           */
-  UINT16      ltime;               /* last time, in minutes       */
-  UINT16      total;               /* seen how often?             */
-};
-
-static unsigned int     nrec = 0;    /* size of array               */
-static unsigned int     urec = 0;    /* in use thereof              */
-static struct gestalt * arec = NULL; /* array                       */
-
-static int      repeat_count = 24;   /* triggers report             */
-static int     clean_counter = 0;    /* cleanup after N inserts     */
-static int        free_slots = 0;    /* free slots available        */
-
-#define SH_CLEANUP 256
-
-void * sh_dummy_g_array     = NULL;
-
-static struct gestalt * add_entry (unsigned char * flags, UINT16 * sum, 
-				   time_t ltime)
-{
-  struct gestalt * array = NULL;
-
-  sh_dummy_g_array = (void*) &array;
-  
- start:
-  if (urec < nrec)
-    {
-      if (free_slots)
-	{
-	  unsigned int i;
-	  for (i = 0; i < urec; ++i)
-	    {
-	      if (arec[i].total == 0)
-		{
-		  array = &arec[i];
-		  --free_slots;
-		  break;
-		}
-	    }
-	}
-
-      if (!array)
-	{
-	  array = &arec[urec];
-	  ++urec;
-	}
-
-      memcpy(array->sum,       sum, sizeof(UINT16)      * SH_NFIELDS);
-      memcpy(array->f.flags, flags, sizeof(unsigned char) * SH_NFIELDS);
-      memset(array->hist,        0, sizeof(unsigned char) * SH_NHIST);
-
-      array->ltime               = (UINT16)(ltime % 60);
-      array->hist[SH_NHIST-1]    = 1;
-      array->total               = 1;
-
-      ++clean_counter;
-      return array;
-    }
-
-  array =    SH_ALLOC(sizeof(struct gestalt) * (nrec + SH_NBLOCK + 1));
-  memset(array,    0, sizeof(struct gestalt) * (nrec + SH_NBLOCK + 1));
-  memcpy(array, arec, sizeof(struct gestalt) * (nrec));
-
-  nrec += (SH_NBLOCK + 1);
-  goto start;
-}
-
-static UINT16 shift_history(unsigned char * hist, unsigned int shift, 
-			      UINT16 total)
-{
-  unsigned int i, j = 0;
-
-  if (shift >= SH_NHIST)
-    {
-      memset(hist,      0, sizeof(unsigned char) * SH_NHIST);
-      return 0;
-    }
-
-  for (i = shift; i < SH_NHIST; ++i)
-    {
-      if (j < shift)
-	total  -= hist[j];
-      hist[j] = hist[i];
-      ++j;
-    }
-  for (i = (SH_NHIST-shift); i < SH_NHIST; ++i)
-    {
-      hist[i] = 0;
-    }
-  return total;
-}
- 
-static void update_entry (struct gestalt * array, time_t ltime)
-{
-  UINT16 ntime = (UINT16)(ltime % 60);
-
-  if (array->ltime == ntime)
-    {
-      if (array->hist[SH_NHIST-1] < 255) 
-	{
-	  ++(array->hist[SH_NHIST-1]);
-	  ++(array->total);
-	}
-    }
-  else if (array->ltime < ntime)
-    {
-      unsigned int shift = ntime - array->ltime;
-      array->total = shift_history(array->hist, shift, array->total);
-      array->hist[SH_NHIST-1] = 1;
-      array->ltime = ntime;
-      ++(array->total);
-    }
-}
-
-static struct gestalt * update_or_add (unsigned char * flags, UINT16 * sum, 
-				       time_t ltime)
-{
-  SINT32 flint[SH_NFINT];
- start:
-
-  if (arec)
-    {
-      unsigned int i;
-      struct gestalt * array = arec;
-
-      sh_dummy_g_array = (void*) &array;
-      
-      memcpy(flint, flags, SH_NFIELDS);
-
-      for (i = 0; i < urec; ++i)
-	{
-	  /* Check whether field types match. Integer
-	   * comparison is much faster than memcmp() [tested].
-	   */
-	  if (flint[0] == array->f.flint[0] &&
-	      flint[1] == array->f.flint[1] &&
-	      flint[2] == array->f.flint[2] &&
-	      flint[3] == array->f.flint[3] &&
-	      flint[4] == array->f.flint[4])
-	    {
-	      unsigned int j; 
-	      int c1 = 0, c2 = 0;
-	      UINT16 * asum = array->sum;
-
-	      for (j = 0; j < SH_NFIELDS; ++j)
-		{
-		  if (flags[j] == SH_GFLAG_ELSE)
-		    {
-		      ++c1;
-		      if (asum[j] == sum[j]) 
-			++c2;
-		    }
-		}
-
-	      if (c1 == c2)
-		{
-		  /* Found a matching entry, update time histogram
-		   */
-		  update_entry (array, ltime);
-		  return array;
-		}
-	    }
-	  ++array;
-	}
-
-      /* No match found, create a new entry
-       */
-      array = add_entry (flags, sum, ltime);
-      return array;
-    }
-  
-  arec = SH_ALLOC(sizeof(struct gestalt) * SH_NBLOCK);
-  nrec = SH_NBLOCK;
-  urec = 0;
-
-  goto start;
-}
-
-/* --------------------------------------------------------------------
- *
- * crc16 checksum from the linux kernel.
- * This source code is licensed under the GNU General Public License,
- * Version 2. 
- */
-
-/** CRC table for the CRC-16. The poly is 0x8005 (x^16 + x^15 + x^2 + 1) */
-UINT16 const crc16_table[256] = {
-  0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
-  0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
-  0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
-  0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
-  0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
-  0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
-  0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
-  0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
-  0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
-  0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
-  0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
-  0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
-  0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
-  0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
-  0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
-  0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
-  0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
-  0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
-  0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
-  0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
-  0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
-  0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
-  0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
-  0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
-  0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
-  0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
-  0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
-  0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
-  0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
-  0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
-  0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
-  0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
-};
-
-static inline UINT16 crc16_byte(UINT16 crc, const unsigned char data)
-{
-  return (crc >> 8) ^ crc16_table[(crc ^ data) & 0xff];
-}
-
-/**
- * crc16 - compute the CRC-16 for the data buffer
- * @crc:        previous CRC value
- * @buffer:     data pointer
- * @len:        number of bytes in the buffer
- *
- * Returns the updated CRC value.
- */
-static inline UINT16 crc16(UINT16 crc, const unsigned char * buffer, 
-			     size_t len)
-{
-  while (len--)
-    crc = crc16_byte(crc, *buffer++);
-  return crc;
-}
-
-
-/* end crc16 code
- *
- * -------------------------------------------------------------------- */
-
-static void  classify(char ** splits, size_t * lengths, unsigned int nfields, 
-		      unsigned char * flags, UINT16 * sums)
-{
-  unsigned int i;
-  unsigned int flag;
-
-  /* flags we don't want to see in XYZ 
-   */
-  static int m_ip    = SH_GFLAG_PATH|SH_GFLAG_EMAIL|SH_GFLAG_USC|SH_GFLAG_ELSE|SH_GFLAG_CHAR|SH_GFLAG_XNUM;
-  static int m_num   = SH_GFLAG_PATH|SH_GFLAG_EMAIL|SH_GFLAG_USC|SH_GFLAG_ELSE|SH_GFLAG_CHAR;
-  static int m_fqdn  = SH_GFLAG_PATH|SH_GFLAG_EMAIL|SH_GFLAG_USC|SH_GFLAG_ELSE;
-  static int m_email = SH_GFLAG_PATH;
-
-  nfields = (nfields > SH_NFIELDS) ? SH_NFIELDS : nfields;
-
-  for (i = 0; i < nfields; ++i) 
-    {
-      char *p = splits[i];
-      unsigned int np   = 0;
-      unsigned int fqdn = 0;
-
-      flag = 0;
-
-      while (*p)
-	{
-	  if (isxdigit((unsigned int)*p))
-	    {
-	      if (isdigit((unsigned int)*p))
-		{
-		  flag |= SH_GFLAG_NUM;
-		}
-	      else
-		{
-		  flag |= SH_GFLAG_XNUM;
-		}
-	    }
-	  else if (*p == '.')
-	    {
-	      flag |= SH_GFLAG_IP;
-	      ++np;
-	    }
-	  else if (*p == '/')
-	    {
-	      flag |= SH_GFLAG_PATH;
-	    }
-	  else if (*p == '@')
-	    {
-	      flag |= SH_GFLAG_EMAIL;
-	    }
-	  else if (*p == '-')
-	    {
-	      flag |= SH_GFLAG_FQDN;
-	    }
-	  else if (*p == '_')
-	    {
-	      flag |= SH_GFLAG_USC;
-	    }
-	  else if (isalpha((unsigned int)*p))
-	    {
-	      if (flag & SH_GFLAG_IP)
-		++fqdn;
-	      flag |= SH_GFLAG_CHAR;
-	    }
-	  else if (!isascii((unsigned int)*p))
-	    {
-	      flags[i] = SH_GFLAG_ELSE;
-	      break;
-	    }
-	  else
-	    {
-	      flag |= SH_GFLAG_ELSE;
-	    }
-	  ++p;
-	}
-
-      if (flags[i] == 0)
-	{
-	  if (0 == (flag & m_ip)         && 
-	      0 != (flag & SH_GFLAG_IP)  && 
-	      0 != (flag & SH_GFLAG_NUM) && 
-	      np > 2)
-	    {
-	      flags[i] = SH_GFLAG_IP;
-	    }
-	  else if (0 == (flag & m_num) && 
-		   (0 != (flag & SH_GFLAG_NUM) || 0 != (flag & SH_GFLAG_XNUM)))
-	    {
-	      flags[i] = SH_GFLAG_NUM;
-	    }
-	  else if (0 == (flag & m_fqdn)        && 
-		   0 != (flag & SH_GFLAG_IP)   && 
-		   0 != (flag & SH_GFLAG_CHAR) && 
-		   fqdn)
-	    {
-	      flags[i] = SH_GFLAG_FQDN;
-	    }
-	  else if ('/' == splits[i][0])
-	    {
-	      flags[i] = SH_GFLAG_PATH;
-	    }
-	  else if (0 == (flag & m_email)        && 
-		   0 != (flag & SH_GFLAG_EMAIL) && 
-		   0 != (flag & SH_GFLAG_CHAR)) 
-	    {
-	      flags[i] = SH_GFLAG_EMAIL;
-	    }
-	  else 
-	    {
-	      flags[i] = SH_GFLAG_ELSE;
-	    }
-	}
-
-      /* CRC-16 checksum
-       */ 
-      sums[i] = crc16(0, (unsigned char *) splits[i], lengths[i]);
-    }
-
-  return;
-}
-
-static void cleanup_array (time_t ltime)
-{
-  UINT16 ntime = (UINT16)(ltime % 60);
-
-  if (ntime > 12) ntime -= 12;
-
-  if (arec && urec > 0)
-    {
-      struct gestalt * array;
-      unsigned int i, last, urec_orig = urec;
-
-      last = urec-1;
-      array = &arec[0];
-  
-      for (i = 0; i < urec_orig; ++i)
-	{
-	  if (array->ltime < ntime)
-	    {
-	      memset(array,    0, sizeof(struct gestalt));
-	      if (i != last)
-		++free_slots;
-	      else
-		--urec;
-	    }
-	}
-      ++array;
-    }
-  clean_counter = 0;
-  return;
-}
-
-/* ----------------------------------------------------------------------
- *
- *   Public functions
- */
-
-int sh_repeat_set_trigger (const char * str)
-{
-  unsigned long  value;
-  char * foo;
-
-  value = (size_t) strtoul(str, &foo, 0);
-
-  if (*foo == '\0' && value < 65535) {
-    repeat_count = value;
-    return 0;
-  }
-  return -1;
-}
-
-static char * sh_repeat_queue = NULL;
-
-int sh_repeat_set_queue (const char * str)
-{
-  if (!str)
-    return -1;
-  if (sh_repeat_queue)
-    SH_FREE(sh_repeat_queue);
-  sh_repeat_queue = sh_util_strdup(str);
-  return 0;
-}
-
-static int sh_repeat_cron = S_FALSE;
-
-int sh_repeat_set_cron (const char * str)
-{
-  return sh_util_flagval(str, &sh_repeat_cron);
-}
-
-int sh_repeat_message_check (const sh_string * host, 
-			     const sh_string * msg, 
-			     time_t ltime)
-{
-  struct gestalt * array;
-
-  UINT16         sums[SH_NFIELDS] = { 0 };
-  unsigned char flags[SH_NFIELDS] = { 0 };
-
-  /* split message into SH_NFIELDS+1, discard last  */
-
-  unsigned int nfields = SH_NFIELDS+1;
-  size_t       lengths[SH_NFIELDS+1];
-  char *       new;
-  char **      splits;
-
-  if (repeat_count == 0)
-    return 0;
-
-  if (sh_repeat_cron == S_FALSE)
-    {
-      char * s = sh_string_str(msg);
-
-      if (0 == strcmp(s, _("cron")) || 0 == strcmp(s, _("CRON")))
-	return 0;
-    }
-
-  new = sh_util_strdup_l(sh_string_str(msg), sh_string_len(msg));
-
-  splits = split_array_token (new, &nfields, lengths, 
-			      " :,()='[]<>\t\n");
-
-  /* classify fields                                */
-
-  classify (splits, lengths, nfields, flags, sums); 
-
-  /* compare                                        */
-
-  array = update_or_add (flags, sums, ltime);
-
-  /* report                                         */
-
-  if (array->total > repeat_count)
-    {
-      volatile int repeat = array->total;
-      char * tmpmsg;
-      char * tmphost;
-      sh_string * alias;
-
-      /* issue report             */
-
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      tmphost = sh_util_safe_name (sh_string_str(host));
-      tmpmsg  = sh_util_safe_name_keepspace (sh_string_str(msg));
-      sh_error_handle (sh_log_lookup_severity(sh_repeat_queue), 
-		       FIL__, __LINE__, 0, MSG_LOGMON_BURST, 
-		       repeat, tmpmsg, tmphost);
-      alias = sh_log_lookup_alias(sh_repeat_queue);
-      if (alias)
-	{
-	  sh_error_mail (sh_string_str(alias), 
-			 sh_log_lookup_severity(sh_repeat_queue), 
-			 FIL__, __LINE__, 0, MSG_LOGMON_BURST, 
-			 repeat, tmpmsg, tmphost);
-	}
-      SH_FREE(tmpmsg);
-      SH_FREE(tmphost);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-
-      /* mark slot as free        */
-
-      memset(array,    0, sizeof(struct gestalt));
-      if (array != &arec[urec-1])
-	++free_slots;
-      else
-	urec -= 1;
-    }
-
-  SH_FREE(new);
-
-  /* run cleanup routine                            */
-
-  if (clean_counter >= SH_CLEANUP)
-    {
-      cleanup_array(ltime);
-    }
-
-  return 0;
-}
-
-#endif
Index: trunk/src/sh_login_track.c
===================================================================
--- trunk/src/sh_login_track.c	(revision 591)
+++ 	(revision )
@@ -1,1417 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 2010 Rainer Wichmann                                      */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-#include "config_xor.h"
-
-#undef  FIL__
-#define FIL__  _("sh_login_track.c")
-
-#if defined(SH_USE_UTMP) && (defined(SH_WITH_CLIENT) || defined (SH_STANDALONE)) 
-
-#include <string.h>
-
-#include "samhain.h"
-#include "sh_pthread.h"
-#include "sh_utils.h"
-#include "sh_unix.h"
-#include "sh_string.h"
-#include "sh_tools.h"
-#include "sh_ipvx.h"
-#include "sh_error_min.h"
-
-#ifdef HAVE_UTMPX_H
-
-#include <utmpx.h>
-#define SH_UTMP_S utmpx
-#undef  ut_name
-#define ut_name ut_user
-#ifdef HAVE_UTXTIME
-#undef  ut_time
-#define ut_time        ut_xtime
-#else
-#undef  ut_time
-#define ut_time        ut_tv.tv_sec
-#endif
-
-#else
-
-#include <utmp.h>
-#define SH_UTMP_S utmp
-
-#endif
-
-
-#if HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
-#include <time.h>
-
-#define SH_LTRACK_VERSION 1
-
-#define SH_LTRACK_USIZE  32
-#define SH_LTRACK_HSIZE 256
-/* One hour    (15 deg)  */
-#define SH_LTRACK_HTRES  24
-/* Ten minutes (2.5 deg) */
-#define SH_LTRACK_GTRES 144
-
-/* Avoid compiling against lmath by including result tables for sin, cos
- */
-const double sintab_htres[SH_LTRACK_HTRES] = {
- 0.13052619222005157340,  0.38268343236508978178,  0.60876142900872065589,  0.79335334029123516508,  0.92387953251128673848,  0.99144486137381038215, 
- 0.99144486137381038215,  0.92387953251128673848,  0.79335334029123516508,  0.60876142900872087793,  0.38268343236508989280,  0.13052619222005157340, 
--0.13052619222005132360, -0.38268343236508967076, -0.60876142900872065589, -0.79335334029123494304, -0.92387953251128651644, -0.99144486137381038215, 
--0.99144486137381049318, -0.92387953251128662746, -0.79335334029123516508, -0.60876142900872087793, -0.38268343236509039240, -0.13052619222005168442, 
-};
-const double costab_htres[SH_LTRACK_HTRES] = {
- 0.99144486137381038215,  0.92387953251128673848,  0.79335334029123516508,  0.60876142900872065589,  0.38268343236508983729,  0.13052619222005171218, 
--0.13052619222005160116, -0.38268343236508972627, -0.60876142900872065589, -0.79335334029123505406, -0.92387953251128673848, -0.99144486137381038215, 
--0.99144486137381049318, -0.92387953251128684951, -0.79335334029123516508, -0.60876142900872087793, -0.38268343236509033689, -0.13052619222005162891, 
- 0.13052619222005126809,  0.38268343236509000382,  0.60876142900872054486,  0.79335334029123494304,  0.92387953251128651644,  0.99144486137381038215, 
-};
-const double sintab_gtres[SH_LTRACK_GTRES] = {
- 0.02181488503456112046,  0.06540312923014306168,  0.10886687485196457070,  0.15212338618991669281,  0.19509032201612824808,  0.23768589232617309825, 
- 0.27982901403099208482,  0.32143946530316158672,  0.36243803828370163567,  0.40274668985873718352,  0.44228869021900124592,  0.48098876891938763256, 
- 0.51877325816052144436,  0.55557023301960217765,  0.59130964836358235193,  0.62592347218405908205,  0.65934581510006884386,  0.69151305578226940352, 
- 0.72236396205975550444,  0.75183980747897738439,  0.77988448309288171956,  0.80644460426748254545,  0.83146961230254523567,  0.85491187067294649449, 
- 0.87672675570750768781,  0.89687274153268836674,  0.91531147911944710227,  0.93200786928279844012,  0.94693012949510557696,  0.96004985438592871372, 
- 0.97134206981326143282,  0.98078528040323043058,  0.98836151046776066220,  0.99405633822231964647,  0.99785892323860347908,  0.99976202707990913243, 
- 0.99976202707990913243,  0.99785892323860347908,  0.99405633822231964647,  0.98836151046776066220,  0.98078528040323043058,  0.97134206981326143282, 
- 0.96004985438592871372,  0.94693012949510568799,  0.93200786928279855115,  0.91531147911944721329,  0.89687274153268836674,  0.87672675570750779883, 
- 0.85491187067294671653,  0.83146961230254545772,  0.80644460426748254545,  0.77988448309288183058,  0.75183980747897738439,  0.72236396205975561546, 
- 0.69151305578226951454,  0.65934581510006895488,  0.62592347218405919307,  0.59130964836358257397,  0.55557023301960217765,  0.51877325816052133334, 
- 0.48098876891938763256,  0.44228869021900130143,  0.40274668985873729454,  0.36243803828370174669,  0.32143946530316175325,  0.27982901403099230686, 
- 0.23768589232617337581,  0.19509032201612860891,  0.15212338618991663730,  0.10886687485196457070,  0.06540312923014311719,  0.02181488503456121761, 
--0.02181488503456097475, -0.06540312923014286739, -0.10886687485196432090, -0.15212338618991641526, -0.19509032201612835911, -0.23768589232617312601, 
--0.27982901403099202930, -0.32143946530316153121, -0.36243803828370152464, -0.40274668985873707250, -0.44228869021900107938, -0.48098876891938741052, 
--0.51877325816052122232, -0.55557023301960195560, -0.59130964836358235193, -0.62592347218405908205, -0.65934581510006884386, -0.69151305578226929249, 
--0.72236396205975550444, -0.75183980747897727337, -0.77988448309288194160, -0.80644460426748265647, -0.83146961230254523567, -0.85491187067294660551, 
--0.87672675570750768781, -0.89687274153268825572, -0.91531147911944710227, -0.93200786928279844012, -0.94693012949510557696, -0.96004985438592860270, 
--0.97134206981326132180, -0.98078528040323031956, -0.98836151046776066220, -0.99405633822231953545, -0.99785892323860347908, -0.99976202707990913243, 
--0.99976202707990913243, -0.99785892323860347908, -0.99405633822231964647, -0.98836151046776066220, -0.98078528040323043058, -0.97134206981326143282, 
--0.96004985438592871372, -0.94693012949510568799, -0.93200786928279855115, -0.91531147911944721329, -0.89687274153268847776, -0.87672675570750790985, 
--0.85491187067294682755, -0.83146961230254545772, -0.80644460426748287851, -0.77988448309288216365, -0.75183980747897782848, -0.72236396205975605955, 
--0.69151305578226918147, -0.65934581510006873284, -0.62592347218405897102, -0.59130964836358235193, -0.55557023301960217765, -0.51877325816052144436, 
--0.48098876891938774358, -0.44228869021900141245, -0.40274668985873740557, -0.36243803828370185771, -0.32143946530316186427, -0.27982901403099241788, 
--0.23768589232617348683, -0.19509032201612871993, -0.15212338618991719241, -0.10886687485196513969, -0.06540312923014367230, -0.02181488503456178660
-};
-const double costab_gtres[SH_LTRACK_GTRES] = {
- 0.99976202707990913243,  0.99785892323860347908,  0.99405633822231964647,  0.98836151046776066220,  0.98078528040323043058,  0.97134206981326143282, 
- 0.96004985438592871372,  0.94693012949510568799,  0.93200786928279855115,  0.91531147911944721329,  0.89687274153268836674,  0.87672675570750768781, 
- 0.85491187067294660551,  0.83146961230254523567,  0.80644460426748265647,  0.77988448309288183058,  0.75183980747897738439,  0.72236396205975561546, 
- 0.69151305578226940352,  0.65934581510006884386,  0.62592347218405908205,  0.59130964836358235193,  0.55557023301960228867,  0.51877325816052155538, 
- 0.48098876891938774358,  0.44228869021900124592,  0.40274668985873723903,  0.36243803828370169118,  0.32143946530316169774,  0.27982901403099202930, 
- 0.23768589232617309825,  0.19509032201612833135,  0.15212338618991680383,  0.10886687485196473724,  0.06540312923014304780,  0.02181488503456115863, 
--0.02181488503456103373, -0.06540312923014292290, -0.10886687485196461234, -0.15212338618991669281, -0.19509032201612819257, -0.23768589232617298723, 
--0.27982901403099191828, -0.32143946530316158672, -0.36243803828370158016, -0.40274668985873712801, -0.44228869021900113490, -0.48098876891938746603, 
--0.51877325816052122232, -0.55557023301960195560, -0.59130964836358246295, -0.62592347218405908205, -0.65934581510006884386, -0.69151305578226929249, 
--0.72236396205975550444, -0.75183980747897727337, -0.77988448309288160853, -0.80644460426748243442, -0.83146961230254534669, -0.85491187067294660551, 
--0.87672675570750768781, -0.89687274153268825572, -0.91531147911944710227, -0.93200786928279844012, -0.94693012949510557696, -0.96004985438592860270, 
--0.97134206981326132180, -0.98078528040323043058, -0.98836151046776066220, -0.99405633822231964647, -0.99785892323860347908, -0.99976202707990913243, 
--0.99976202707990913243, -0.99785892323860347908, -0.99405633822231964647, -0.98836151046776077322, -0.98078528040323043058, -0.97134206981326143282, 
--0.96004985438592871372, -0.94693012949510568799, -0.93200786928279855115, -0.91531147911944721329, -0.89687274153268836674, -0.87672675570750779883, 
--0.85491187067294671653, -0.83146961230254545772, -0.80644460426748254545, -0.77988448309288183058, -0.75183980747897749541, -0.72236396205975561546, 
--0.69151305578226951454, -0.65934581510006906591, -0.62592347218405897102, -0.59130964836358224090, -0.55557023301960217765, -0.51877325816052144436, 
--0.48098876891938768807, -0.44228869021900135694, -0.40274668985873735005, -0.36243803828370180220, -0.32143946530316180876, -0.27982901403099236237, 
--0.23768589232617343132, -0.19509032201612866442, -0.15212338618991713690, -0.10886687485196507030, -0.06540312923014361679, -0.02181488503456172415, 
- 0.02181488503456135639,  0.06540312923014325597,  0.10886687485196470948,  0.15212338618991677608,  0.19509032201612830359,  0.23768589232617307050, 
- 0.27982901403099197379,  0.32143946530316147570,  0.36243803828370146913,  0.40274668985873701699,  0.44228869021900102387,  0.48098876891938735501, 
- 0.51877325816052111129,  0.55557023301960184458,  0.59130964836358201886,  0.62592347218405863796,  0.65934581510006839977,  0.69151305578226895943, 
- 0.72236396205975572649,  0.75183980747897749541,  0.77988448309288183058,  0.80644460426748254545,  0.83146961230254523567,  0.85491187067294660551, 
- 0.87672675570750768781,  0.89687274153268825572,  0.91531147911944710227,  0.93200786928279844012,  0.94693012949510557696,  0.96004985438592860270, 
- 0.97134206981326132180,  0.98078528040323031956,  0.98836151046776066220,  0.99405633822231953545,  0.99785892323860347908,  0.99976202707990913243
-};
-
-struct sh_track_entry_data {
-  UINT64      last_login;
-  UINT32      array[SH_LTRACK_HTRES]; /* 1 h resolution */
-  char        hostname[SH_LTRACK_HSIZE];
-};
-
-struct sh_track_entry {
-  struct sh_track_entry_data data;
-  struct sh_track_entry * next;
-};
-
-struct sh_track_head {
-  UINT32 version;
-  UINT32 n_entries;
-  UINT64 last_login;
-  char   hostname[SH_LTRACK_HSIZE];
-  UINT32 array[SH_LTRACK_GTRES]; /* 10 min resolution */
-};
-
-struct sh_track {
-  struct sh_track_head head;
-  struct sh_track_entry * list;
-};
-
-
-/* Returns zero/nonzero
- */
-static int get_bool(char *bitarray, unsigned int index)
-{
-  int bool;
-
-  bitarray += index / 8; /* skip to char */
-  bool = (*bitarray & (1 << (index % 8)));
-
-  return bool;
-}
-
-static void set_bool(char *bitarray, unsigned int index, int bool)
-{
-  bitarray += index / 8; /* skip to char */
-  if (bool)
-    *bitarray |= 1 << (index % 8);
-  else    
-    *bitarray &= ~(1 << (index % 8));
-  return;
-}
-
-
-static char * build_path (const char * user)
-{
-  char * ui;
-
-  if (0 != sh_util_base64_enc_alloc (&ui, user, sl_strlen(user)))
-    {
-      char * path = sh_util_strconcat(DEFAULT_DATAROOT, "/", ui, NULL);
-
-      SH_FREE(ui);
-      return path;
-    }
-  return NULL;
-}
-
-static void destroy_loaded(struct sh_track * urecord)
-{
-  if (urecord)
-    {
-      struct sh_track_entry * entry = urecord->list;
-      struct sh_track_entry * entry_old;
-
-      while(entry)
-	{
-	  entry_old = entry;
-	  entry = entry->next;
-	  SH_FREE(entry_old);
-	}
-      SH_FREE(urecord);
-    }
-  return;
-}
-
-static struct sh_track * load_data_int (char * path)
-{
-  struct sh_track_head * uhead;
-  struct sh_track * urecord;
-
-  urecord = SH_ALLOC(sizeof(struct sh_track));
-  memset(urecord, 0, sizeof(struct sh_track));
-
-  uhead = &(urecord->head);
-  uhead->version = SH_LTRACK_VERSION;
-
-  if (path)
-    {
-      FILE * fp = fopen(path, "rb");
-      
-      if (fp)
-	{
-	  size_t n;
-	  
-	  n = fread(uhead, sizeof(struct sh_track_head), 1, fp);
-	  
-	  if (n == 1)
-	    {
-	      struct sh_track_entry_data entry_data;
-	      struct sh_track_entry * entry;
-	      
-	      while (1 == fread(&entry_data, sizeof(entry_data), 1, fp))
-		{
-		  entry = SH_ALLOC(sizeof(struct sh_track_entry));
-		  memcpy(&(entry->data), &entry_data, sizeof(entry_data));
-		  entry->next   = urecord->list;
-		  urecord->list = entry;
-		}
-	    }
-	  fclose(fp);
-	}
-    }
-
-  return urecord;
-}
-
-static struct sh_track * load_data (const char * user)
-{
-  char * path = build_path (user);
-  struct sh_track * res = load_data_int (path);
-
-  if (path)
-    SH_FREE(path);
-  return res;
-}
-
-static void save_data_int (struct sh_track * urecord, char * path)
-{
-  mode_t mask;
-  FILE * fp;
-  
-  mask = umask(S_IWGRP | S_IWOTH);
-  fp = fopen(path, "wb");
-  (void) umask(mask);
-  
-  if (fp)
-    {
-      size_t n;
-      
-      n = fwrite(&(urecord->head), sizeof(struct sh_track_head), 1, fp);
-      
-      if (n == 1)
-	{
-	  struct sh_track_entry * entry = urecord->list;
-	  
-	  while (entry && (n > 0))
-	    {
-	      n = fwrite(&(entry->data), sizeof(struct sh_track_entry_data), 
-			 1, fp);
-	      entry = entry->next;
-	    }
-	}
-      fclose(fp);
-    }
-  return;
-}
-
-static void save_data (struct sh_track * urecord, const char * user)
-{
-  char * path = build_path (user);
-
-  if (path)
-    {
-      save_data_int (urecord, path);
-      SH_FREE(path);
-    }
-  return;
-}
-
-/**************
- *
- * Configurable
- *
- **************/
-
-enum significance { SIG00, SIG01, SIG05 };
-enum checklevel   { CHECK_NONE, CHECK_HOST, CHECK_DOMAIN };
-enum days         { WORKDAYS = 0, SATURDAY, SUNDAY };
-#define LTRACK_NDAYS 3
-
-static int sig_level    = SIG00;
-static int check_level  = CHECK_NONE;
-static int check_date   = S_FALSE;
-
-/* We use a bit array of SH_LTRACK_GTRES bits for allowed times 
- * (10 min resolution)
- */
-#define BITARRSIZ(a) ((a + 7) / 8)
-
-static int global_init  = S_FALSE;
-static char global_dates[LTRACK_NDAYS][BITARRSIZ(SH_LTRACK_GTRES)];
-
-struct sh_track_dates {
-  char user[SH_LTRACK_USIZE];
-  char dates[LTRACK_NDAYS][BITARRSIZ(SH_LTRACK_GTRES)];
-  struct sh_track_dates * next;
-};
-struct sh_track_dates * user_dates = NULL;
-
-static int set_dates (char bitarray[][BITARRSIZ(SH_LTRACK_GTRES)], 
-		      unsigned int size, const char * defstr);
-
-void sh_login_reset (void)
-{
-  int i, j;
-  struct sh_track_dates *u_old, *u;
-
-  u          = user_dates;
-  user_dates = NULL;
-
-  while(u)
-    {
-      u_old = u;
-      u     = u->next;
-      SH_FREE(u_old);
-    }
-
-  for (j = 0; j < LTRACK_NDAYS; ++j)
-    {
-      for (i = 0; i < SH_LTRACK_GTRES; ++i)
-	{ 
-	  set_bool(global_dates[j], i, 0);
-	}
-    }
-  global_init = S_FALSE;
-
-  sig_level    = SIG00;
-  check_level  = CHECK_NONE;
-  check_date   = S_FALSE;
-
-  return;
-}
-
-int sh_login_set_def_allow(const char * c)
-{
-  int res = set_dates(global_dates, SH_LTRACK_GTRES, c);
-
-  if (res == 0)
-    {
-      check_date   = S_TRUE;
-      global_init  = S_TRUE;
-    }
-  return res;
-}
-
-static struct sh_track_dates * find_user(const char * user)
-{
-  struct sh_track_dates * u = user_dates;
-
-  while(u)
-    {
-      if (0 == sl_strcmp(user, u->user))
-	{
-	  return u;
-	}
-      u = u->next;
-    }
-  return NULL;
-}
-
-int sh_login_set_user_allow(const char * c)
-{
-  unsigned int i = 0;
-  const char *p = c;
-  char user[SH_LTRACK_USIZE];
-  
-  struct sh_track_dates * u;
-
-  while (p && *p && *p != ':' && *p != ' ' && *p != '\t')
-    {
-      user[i] = *p; ++p; ++i;
-
-      if (i == SH_LTRACK_USIZE)
-	return -1;
-    }
-
-  while (p && *p && (*p == ' ' || *p == '\t')) ++p;
-
-  if (p && *p && (i < SH_LTRACK_USIZE) && (*p == ':'))
-    {
-      user[i] = '\0';
-
-      ++p; while (*p && (*p == ' ' || *p == '\t')) ++p;
-
-      if (*p)
-	{
-	  int res;
-	  int flag = 0;
-
-	  u = find_user(user);
-
-	  if (!u)
-	    {
-	      u = SH_ALLOC(sizeof(struct sh_track_dates));
-	      memset(u, 0, sizeof(struct sh_track_dates));
-	      sl_strlcpy(u->user, user, SH_LTRACK_USIZE);
-	      flag = 1;
-	    }
-
-	  res = set_dates(u->dates, SH_LTRACK_GTRES, p);
-	  if (res != 0)
-	    {
-	      if (flag == 1)
-		SH_FREE(u);
-	      return -1;
-	    }
-
-	  if (flag == 1)
-	    {
-	      u->next    = user_dates;
-	      user_dates = u;
-	    }
-
-	  check_date = S_TRUE;
-	  return 0;
-	}
-    }
-  return -1;
-}
-
-int sh_login_set_siglevel(const char * c)
-{
-  int ret = sh_util_flagval(c, &sig_level);
-
-  if (ret == 0)
-    {
-      sig_level = (sig_level == S_FALSE) ? SIG00 : SIG01;
-      return 0;
-    }
-  else
-    {
-      if (0 == strcmp(c, _("paranoid")))
-	{
-	  sig_level = SIG05;
-	  return 0;
-	}
-    }
-  sig_level = SIG00;
-  return -1;
-}
-
-int sh_login_set_checklevel(const char * c)
-{
-  int ret = sh_util_flagval(c, &check_level);
-
-  if (ret == 0)
-    {
-      check_level = (check_level == S_FALSE) ? CHECK_NONE : CHECK_HOST;
-      return 0;
-    }
-  else
-    {
-      if (0 == strcmp(c, _("domain")))
-	{
-	  check_level = CHECK_DOMAIN;
-	  return 0;
-	}
-    }
-  check_level = CHECK_NONE;
-  return -1;
-}
-
-static int eval_range(char * bitarray, unsigned int size, char * def)
-{
-  unsigned int h1, m1, h2, m2;
-
-  int res = sscanf(def, "%d:%d - %d:%d", &h1, &m1, &h2, &m2);
-
-  if (res == 4)
-    {
-      unsigned int t1 = 3600*h1 + 60*m1;
-      unsigned int t2 = 3600*h2 + 60*m2;
-      int hres        = (60*60*24)/size;
-      unsigned int i;
-
-      if (t1 > t2 || t1 > 86340 || t2 > 86340)
-	return -1;
-
-      t1  = t1 / hres;
-      t2  = t2 / hres;
-      t1  = (t1 < size) ? t1 : (size-1);
-      t2  = (t2 < size) ? t2 : (size-1);
-
-      for (i = t1; i <= t2; ++i)
-	{
-	  set_bool(bitarray, i, 1);
-	}
-      return 0;
-    }
-  return -1;
-}
-
-static int set_ranges(char * bitarray, unsigned int size, 
-		      char ** splits, unsigned int nfields)
-{
-  unsigned int i;
-  int retval = 0;
-
-  for (i = 0; i < nfields; ++i)
-    {
-      char * range = &(splits[i][0]);
-
-      if (0 != eval_range(bitarray, size, range))
-	retval = -1;
-    }
-  return retval;
-}
-
-/* 'always', 'never', workdays(list of ranges), (sun|satur)day(list of ranges)
- */
-static int set_dates (char bitarray[][BITARRSIZ(SH_LTRACK_GTRES)], 
-		      unsigned int size, 
-		      const char * defstr)
-{
-  unsigned int i, j;
-  int retval = -1;
-
-  if (0 == strcmp(_("always"), defstr))
-    {
-      for (j = 0; j < LTRACK_NDAYS; ++j)
-	for (i = 0; i < size; ++i)
-	  set_bool(bitarray[j], i, 1);
-      retval = 0;
-    }
-  else if (0 == strcmp(_("never"), defstr))
-    {
-      for (j = 0; j < LTRACK_NDAYS; ++j)
-	for (i = 0; i < size; ++i)
-	  set_bool(bitarray[j], i, 0);
-      retval = 0;
-    }
-  else
-    {
-      unsigned int nfields = 24; /* list of ranges */
-      size_t       lengths[24];
-      char *       new    = NULL;
-      char **      splits = NULL;
-
-      if      (0 == strncmp(_("workdays"), defstr, 7))
-	{
-	  new    = sh_util_strdup(defstr);
-	  splits = split_array_braced(new, _("workdays"), 
-				      &nfields, lengths);
-	  j = WORKDAYS;
-	}
-      else if (0 == strncmp(_("saturday"), defstr, 8))
-	{
-	  new    = sh_util_strdup(defstr);
-	  splits = split_array_braced(new, _("saturday"), 
-				      &nfields, lengths);
-	  j = SATURDAY;
-	}
-      else if (0 == strncmp(_("sunday"), defstr, 6))
-	{
-	  new    = sh_util_strdup(defstr);
-	  splits = split_array_braced(new, _("sunday"), 
-				      &nfields, lengths);
-	  j = SUNDAY;
-	}
-      else
-	{
-	  return -1;
-	}
-
-      if (new && splits && nfields > 0)
-	{
-	  retval = set_ranges(bitarray[j], size, splits, nfields);
-	}
-
-      if (new) SH_FREE(new);
-    }
-  return retval;
-}
-
-
-
-/**************
- *
- * Report
- *
- **************/
-
-void report_generic(char * file, int line, 
-		    const char * user, time_t time, const char * host, int what)
-{
-  char   ttt[TIM_MAX];
-
-  SH_MUTEX_LOCK(mutex_thread_nolog);
-  (void) sh_unix_time (time, ttt, TIM_MAX);
-  sh_error_handle ((-1), file, line, 0, what,
-		   user, host, ttt);
-  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-  return;
-}
-
-void report_bad_date(char * file, int line, 
-		     const char *user, time_t time, const char * host)
-{
-  report_generic(file, line, user, time, host, MSG_UT_BAD);
-}
-
-void report_first(char * file, int line, 
-		  const char *user, time_t time, const char * host)
-{
-  report_generic(file, line, user, time, host, MSG_UT_FIRST);
-}
-
-void report_outlier(char * file, int line, 
-		    const char *user, time_t time, const char * host)
-{
-  report_generic(file, line, user, time, host, MSG_UT_OUTLIER);
-}
-
-/**************
- *
- * Dates
- *
- **************/
-
-static int check_login_date(const char * user, unsigned int index, int wday)
-{
-  unsigned int i, j;
-  struct sh_track_dates * allowed = NULL;
-  int day;
-
-  /* Use an intermediate array 'char* b[m]' to cast 'char a[m][n]' to 'char** c' */
-  char * aux[LTRACK_NDAYS];
-  char **good = (char **) aux;
-
-  for (i = 0; i < LTRACK_NDAYS; ++i)
-    {
-      aux[i] = (char *) &global_dates[i][0];
-      /* + i * BITARRSIZ(SH_LTRACK_GTRES); */
-    }
-
-  if (wday > 0 && wday < 6)
-    day = WORKDAYS;
-  else if (wday == 6)
-    day = SATURDAY;
-  else
-    day = SUNDAY;
-
-  if (check_date != S_FALSE)
-    {
-      if (S_FALSE == global_init)
-	{
-	  for (j = 0; j < LTRACK_NDAYS; ++j)
-	    {
-	      for (i = 0; i < SH_LTRACK_GTRES; ++i) 
-		set_bool(global_dates[j], i, 1);
-	    }
-	  global_init = S_TRUE;
-	}
-
-      if (user) {
-	allowed = find_user(user);
-      }
-
-      if (allowed)
-	{
-	  for (i = 0; i < LTRACK_NDAYS; ++i)
-	    {
-	      aux[i] = (char *)&(allowed->dates)[i][0]; 
-	      /* + i*BITARRSIZ(SH_LTRACK_GTRES); */
-	    }
-	}
-      
-      if (0 == get_bool(good[day], index))
-	{
-	  return -1;
-	}
-    }
-  return 0;
-} 
-
-/**************
- *
- * Statistics
- *
- **************/
-
-/* Compute sqrt(s) using the babylonian algorithm
- * (to avoid linking with -lm).
- */ 
-static double sh_sqrt(double s)
-{
-  double eps = 1.0e-6;
-  double x0  = 1.0;
-  double xs  = s;
-
-  double diff = xs - x0;
-  diff = (diff > 0.0) ? diff : -diff;
-
-  while (diff > eps)
-    {
-      xs = x0;
-      x0 = 0.5 * (x0 + (s/x0));
-      diff = xs - x0;
-      diff = (diff > 0.0) ? diff : -diff;
-    }
-  return x0;
-}
-
-static double M_crit(int n, int flag)
-{
-#define SH_MCSIZE 10
-  const double M_05[SH_MCSIZE] = { 0.975, 0.918, 0.855, 0.794, 0.739, 0.690, 0.647, 0.577, 0.497, 0.406 };
-  const double M_01[SH_MCSIZE] = { 0.995, 0.970, 0.934, 0.891, 0.845, 0.799, 0.760, 0.688, 0.603, 0.498 };
-  const int    M_nn[SH_MCSIZE] = {     4,     5,     6,     7,     8,     9,    10,    12,    15,    20 };
-
-  if (n > M_nn[SH_MCSIZE-1])
-    {
-      return ((flag == SIG05) ? M_05[SH_MCSIZE-1] : M_01[SH_MCSIZE-1]);
-    }
-  else
-    {
-      unsigned int i;
-
-      for (i = 1; i < SH_MCSIZE; ++i)
-	{
-	  if (n < M_nn[i])
-	    {
-	      return ((flag == SIG05) ? M_05[i-1] : M_01[i-1]);
-	    }
-	}
-    }
-
-  return ((flag == SIG05) ? M_05[SH_MCSIZE-1] : M_01[SH_MCSIZE-1]);
-}
-
-static int check_statistics (unsigned int index, UINT32 * array, unsigned int size,
-			     const double * costab, const double * sintab)
-{
-  double C = 0.0;
-  double S = 0.0;
-  double R, Rk, M;
-
-  unsigned int i, n = 0;
-
-  if (sig_level != SIG00)
-    {
-      for (i = 0; i < size; ++i)
-	{
-	  n += array[i];
-	  C += (array[i] * costab[i]);
-	  S += (array[i] * sintab[i]);
-	}
-      
-      if (n > 2) /* current is at least 4th datapoint */
-	{
-	  R = sh_sqrt(S*S + C*C);
-	  
-	  C += array[index] * costab[index];
-	  S += array[index] * sintab[index];
-	  Rk = sh_sqrt(S*S + C*C);
-	  ++n;
-	  
-	  M  = (Rk - R + 1.0)/((double)n - R);
-	  
-	  if (M > M_crit(n, sig_level))
-	    {
-	      return -1;
-	    }
-	}
-    }
-  return 0;
-}
-
-static char * stripped_hostname (const char * host)
-{
-  char *p, *q;
-  
-  if (sh_ipvx_is_numeric(host))
-    {
-      p = sh_util_strdup(host);
-      q = strrchr(p, '.');
-      if (q) 
-	{
-	  *q = '\0';
-	  q = strrchr(p, '.');
-	  if (q)
-	    {
-	      *q = '\0';
-	    }
-	}
-    }
-  else
-    {
-      char * tmp = sh_util_strdup(host);
-      q = strchr(tmp, '.'); 
-      if (q && *q)
-	{
-	  ++q;
-	  p = sh_util_strdup(q);
-	  SH_FREE(tmp);
-	}
-      else
-	{
-	  p = tmp;
-	}
-    }
-  return p;
-}
-
-static unsigned int time_to_index(struct tm * tp, int nbin)
-{
-  int hres  = (60*60*24)/nbin;
-  int index = tp->tm_hour * 3600 + tp->tm_min * 60 + tp->tm_sec;
-  index  = index / hres;
-  index  = (index < nbin) ? index : (nbin-1);
-
-  return index;
-}
-
-static struct sh_track_entry * check_host(struct sh_track_entry * list, 
-					  const char * user, time_t time, const char * host,
-					  struct tm * tp)
-{
-  unsigned int    index = time_to_index(tp, SH_LTRACK_HTRES);
-  struct sh_track_entry * entry = list;
-
-  char * p = NULL;
-  const char * q;
-
-  if (check_level == CHECK_DOMAIN)
-    {
-      p = stripped_hostname(host);
-      q = p;
-    }
-  else
-    {
-      q = host;
-    }
-
-  while (entry)
-    {
-      if (0 == strncmp(q, (entry->data).hostname, SH_LTRACK_HSIZE))
-	break;
-      entry = entry->next;
-    }
-
-  if (entry)
-    {
-      int isAlert;
-
-      (entry->data).last_login    = time;
-
-      /* Check host statistics here 
-       */
-      isAlert = check_statistics (index, (entry->data).array, SH_LTRACK_HTRES, 
-				  costab_htres, sintab_htres); 
-
-      if (isAlert != 0) 
-	{
-	  report_outlier(FIL__, __LINE__, user, time, host);
-	}
-
-      /* Update array afterwards
-       */
-      (entry->data).array[index] += 1;
-    }
-  else
-    {
-      entry = SH_ALLOC(sizeof(struct sh_track_entry));
-      memset(entry, 0, sizeof(struct sh_track_entry));
-      (entry->data).last_login    = time;
-      (entry->data).array[index]  = 1;
-      sl_strlcpy((entry->data).hostname, q, SH_LTRACK_HSIZE);
-
-      /* Report first login from this host 
-       */
-      if (check_level != CHECK_NONE) 
-	{ 
-	  report_first (FIL__, __LINE__, user, time, host);
-	}
-      
-      if (p)
-	SH_FREE(p);
-      return entry;
-    }
-
-  if (p)
-    SH_FREE(p);
-  return NULL;
-}
-
-/********************************************************
- *
- * Public Function
- *
- ********************************************************/
-
-void sh_ltrack_check(struct SH_UTMP_S * ut)
-{
-  int gres;
-  const char * user;
-  time_t time;
-#if defined(HAVE_UTHOST)
-  const char * host;
-#else
-  const char * host;
-#endif
-  struct sh_track * urecord;
-  time_t last_login;
-
-  /* Just return if we are not supposed to do anything
-   */
-  if (sig_level == SIG00 && check_level == CHECK_NONE && check_date == S_FALSE)
-    return;
-
-
-#if defined(HAVE_UTHOST)
-  host = ut->ut_host;
-#else
-  host = sh_util_strdup(_("unknown"));
-#endif
-  time = ut->ut_time;
-  user = ut->ut_name;
-
-  gres  = (60*60*24)/SH_LTRACK_GTRES;
-
-  urecord    = load_data(user);
-  last_login = (urecord->head).last_login;
-
-  if (   last_login < time &&
-	 ( (time - last_login) >= gres || 
-	   0 != strcmp(host, (urecord->head).hostname)
-	   )
-	 )
-    {
-      struct tm ts;
-      unsigned int  index;
-      int isAlert;
-      struct sh_track_entry * entry;
-
-      (urecord->head).last_login = time;
-      sl_strlcpy((urecord->head).hostname, host, SH_LTRACK_HSIZE);
-      (urecord->head).n_entries += 1;
-
-      memcpy(&ts, localtime(&time), sizeof(struct tm));
-      index = time_to_index(&ts, SH_LTRACK_GTRES);
-      
-      /* Check global statistics here 
-       */
-      isAlert = check_statistics (index, (urecord->head).array, 
-				  SH_LTRACK_GTRES, 
-				  costab_gtres, sintab_gtres);
-      
-      if (isAlert != 0) 
-	{
-	  report_outlier(FIL__, __LINE__, user, time, host);
-	}
-      
-
-      if (check_date != S_FALSE)
-	{
-	  int isBad = check_login_date(user, index, ts.tm_wday);
-
-	  if (isBad != 0)
-	    {
-	      report_bad_date(FIL__, __LINE__, user, time, host);
-	    }
-	}
-
-      /* Update array afterwards 
-       */
-      (urecord->head).array[index] += 1;
-
-      entry = check_host(urecord->list, user, time, host, &ts);
-      if (entry)
-	{
-	  entry->next   = urecord->list;
-	  urecord->list = entry;
-	}
-
-      save_data(urecord, user);
-    } 
-
-  destroy_loaded(urecord);
-
-#if !defined(HAVE_UTHOST)
-  SH_FREE(host);
-#endif
-  return;
-}
-
-#ifdef SH_CUTEST
-#include <stdlib.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#include "CuTest.h"
-
-void Test_login (CuTest *tc) {
-  char bitarr[10] = { 0,0,0,0,0,0,0,0,0,128 };
-  unsigned int i;
-  int j, k;
-  char buf[1024];
-  char *p, *q;
-  size_t l1, l2;
-
-  /* Check bitarray */
-
-  for (i = 0; i < 72; ++i)
-    {
-      set_bool(bitarr, i, 1);
-    }
-  for (i = 72; i < 80; ++i)
-    {
-      set_bool(bitarr, i, 0);
-    }
-  for (i = 0; i < 80; ++i)
-    {
-      j = get_bool(bitarr, i);
-      if (i < 72)
-	CuAssertTrue(tc, j > 0);
-      else
-	CuAssertIntEquals(tc, 0, j);
-    }
-
-  /* check build_path */
-
-  j = sl_strlcpy(buf, DEFAULT_DATAROOT, sizeof(buf));
-  CuAssertIntEquals(tc, 0, j);
-
-  p = build_path("rainer");
-  q = sh_util_dirname(p);
-  j = strncmp(buf, q, strlen(buf));
-  l1 = strlen(buf); l2 = strlen(q);
-  CuAssertTrue(tc, l2 >= l1);
-  CuAssertIntEquals(tc, 0, j);
-
-  q = sh_util_basename(p);
-  CuAssertStrEquals(tc, q, "cmFpbmVy");
-
-  { /* Check load/save of user data */
-    struct sh_track urecord, *precord;
-    struct sh_track_entry uentry0, *pentry;
-    struct sh_track_entry uentry1;
-
-    urecord.head.version   = 40;
-    urecord.head.n_entries = 41;
-    urecord.head.last_login = 42;
-    for (i = 0; i < SH_LTRACK_GTRES; ++i)
-      urecord.head.array[i] = 0;
-    urecord.head.array[30] = 30;
-
-    urecord.list = &uentry0;
-    uentry0.next = &uentry1;
-    uentry1.next = NULL;
-
-    uentry0.data.last_login = 52;
-    strcpy(uentry0.data.hostname, "host0");
-    for (i = 0; i < SH_LTRACK_HTRES; ++i)
-      uentry0.data.array[i] = 0;
-    uentry0.data.array[5] = 50;
-
-    uentry1.data.last_login = 62;
-    strcpy(uentry1.data.hostname, "host1");
-    for (i = 0; i < SH_LTRACK_HTRES; ++i)
-      uentry1.data.array[i] = 0;
-    uentry1.data.array[6] = 60;
-
-    snprintf(buf, sizeof(buf), "cutest_%06d", (int) getpid());
-
-    save_data_int(&urecord, buf);
-
-    precord = load_data_int(buf);
-
-    CuAssertIntEquals(tc, urecord.head.version, (precord->head).version);
-    CuAssertIntEquals(tc, urecord.head.n_entries, (precord->head).n_entries);
-    CuAssertIntEquals(tc, urecord.head.last_login, (precord->head).last_login);
-    for (i = 0; i < SH_LTRACK_GTRES; ++i)
-      CuAssertIntEquals(tc, urecord.head.array[i], (precord->head).array[i]);
-
-    CuAssertPtrNotNull(tc, precord->list);
-    pentry = precord->list;
-    CuAssertIntEquals(tc, uentry1.data.last_login, (pentry->data).last_login);
-    CuAssertStrEquals(tc, uentry1.data.hostname, (pentry->data).hostname);
-    for (i = 0; i < SH_LTRACK_HTRES; ++i)
-      CuAssertIntEquals(tc, uentry1.data.array[i], (pentry->data).array[i]);
-
-    CuAssertPtrNotNull(tc, pentry->next);
-    pentry = pentry->next;
-    CuAssertIntEquals(tc, uentry0.data.last_login, (pentry->data).last_login);
-    CuAssertStrEquals(tc, uentry0.data.hostname, (pentry->data).hostname);
-    for (i = 0; i < SH_LTRACK_HTRES; ++i)
-      CuAssertIntEquals(tc, uentry0.data.array[i], (pentry->data).array[i]);
-
-    CuAssertPtrEquals(tc, pentry->next, NULL);
-    destroy_loaded(precord);
-    unlink(buf);
-
-    precord = load_data_int("supacalifragilistic");
-    CuAssertPtrNotNull(tc, precord);
-    CuAssertPtrEquals(tc, precord->list, NULL);
-    CuAssertIntEquals(tc, SH_LTRACK_VERSION, (precord->head).version);
-    CuAssertIntEquals(tc, 0, (precord->head).n_entries);
-    CuAssertIntEquals(tc, 0, (precord->head).last_login);
-    for (i = 0; i < SH_LTRACK_GTRES; ++i)
-      CuAssertIntEquals(tc, 0, (precord->head).array[i]);
-    destroy_loaded(precord);
-
-    precord = load_data_int(NULL);
-    CuAssertPtrNotNull(tc, precord);
-    CuAssertPtrEquals(tc, precord->list, NULL);
-    CuAssertIntEquals(tc, SH_LTRACK_VERSION, (precord->head).version);
-    CuAssertIntEquals(tc, 0, (precord->head).n_entries);
-    CuAssertIntEquals(tc, 0, (precord->head).last_login);
-    for (i = 0; i < SH_LTRACK_GTRES; ++i)
-      CuAssertIntEquals(tc, 0, (precord->head).array[i]);
-    destroy_loaded(precord);
-  }
-
-  /* check configuration */
-
-  j = sh_login_set_siglevel("duh");
-  CuAssertIntEquals(tc, -1, j);
-  CuAssertIntEquals(tc, SIG00, sig_level);
-
-  j = sh_login_set_siglevel("yes");
-  CuAssertIntEquals(tc, 0, j);
-  CuAssertIntEquals(tc, SIG01, sig_level);
-  j = sh_login_set_siglevel("no");
-  CuAssertIntEquals(tc, 0, j);
-  CuAssertIntEquals(tc, SIG00, sig_level);
-  j = sh_login_set_siglevel("paranoid");
-  CuAssertIntEquals(tc, 0, j);
-  CuAssertIntEquals(tc, SIG05, sig_level);
-
-  j = sh_login_set_checklevel("duh");
-  CuAssertIntEquals(tc, -1, j);
-  CuAssertIntEquals(tc, CHECK_NONE, check_level);
-
-  j = sh_login_set_checklevel("yes");
-  CuAssertIntEquals(tc, 0, j);
-  CuAssertIntEquals(tc, CHECK_HOST, check_level);
-  j = sh_login_set_checklevel("no");
-  CuAssertIntEquals(tc, 0, j);
-  CuAssertIntEquals(tc, CHECK_NONE, check_level);
-  j = sh_login_set_checklevel("domain");
-  CuAssertIntEquals(tc, 0, j);
-  CuAssertIntEquals(tc, CHECK_DOMAIN, check_level);
-
-  j = sh_login_set_def_allow("always");
-  CuAssertIntEquals(tc, 0, j);
-  for (j = 0; j < LTRACK_NDAYS; ++j)
-    {
-      for (i = 0; i < SH_LTRACK_GTRES; ++i)
-	{
-	  k = get_bool(global_dates[j], i);
-	  CuAssertTrue(tc, k > 0);
-	}
-    }
-
-  j = sh_login_set_def_allow("never");
-  CuAssertIntEquals(tc, 0, j);
-  for (j = 0; j < LTRACK_NDAYS; ++j)
-    {
-      for (i = 0; i < SH_LTRACK_GTRES; ++i)
-	{
-	  k = get_bool(global_dates[j], i);
-	  CuAssertIntEquals(tc, 0, k);
-	}
-    }
-
-  j = sh_login_set_def_allow("workdays( 0:12-1:30, 07:30-18:29,23:30-23:59)");
-  CuAssertIntEquals(tc, 0, j);
-  for (j = 0; j < LTRACK_NDAYS; ++j)
-    {
-      for (i = 0; i < SH_LTRACK_GTRES; ++i)
-	{
-	  k = get_bool(global_dates[j], i);
-	  // fprintf(stderr, "%d: %d: %d\n", j, i, k);
-	  if (j == WORKDAYS)
-	    {
-	      if ( (i>=1 && i<=9) || (i>=45 && i <=110) || (i>=141 && i<=143))
-		CuAssertTrue(tc, k > 0);
-	      else
-		CuAssertIntEquals(tc, 0, k);
-	    }
-	  else
-	    {
-	      CuAssertIntEquals(tc, 0, k);
-	    }
-	}
-    }
-
-  j = sh_login_set_user_allow("rainer :workdays( 0:12-1:30, 07:30-18:29,23:30-23:59)");
-  CuAssertIntEquals(tc, 0, j);
-  j = sh_login_set_user_allow("rainer :saturday( 0:0-23:59)");
-  CuAssertIntEquals(tc, 0, j);
-  j = sh_login_set_user_allow("rain : workdays(0:12-1:30, 07:30-18:29,23:30-23:59)");
-  CuAssertIntEquals(tc, 0, j);
-  j = sh_login_set_user_allow("cat: workdays( 0:12-1:30, 07:30-18:29,23:30-23:59 )");
-  CuAssertIntEquals(tc, 0, j);
-  j = sh_login_set_user_allow("cat: sunday(0:00-23:59)");
-  CuAssertIntEquals(tc, 0, j);
-
-  {
-    int count = 0;
-    struct sh_track_dates * u = user_dates;
-    
-    CuAssertPtrNotNull(tc, u);
-
-    do {
-
-      if (count == 0) {
-	CuAssertStrEquals(tc, u->user, "cat");
-	CuAssertPtrNotNull(tc, u->next);
-      }
-      else if (count == 1) {
-	CuAssertStrEquals(tc, u->user, "rain");
-	CuAssertPtrNotNull(tc, u->next);
-      }
-      else if (count == 2) {
-	CuAssertStrEquals(tc, u->user, "rainer");
-	CuAssertPtrEquals(tc, u->next, NULL);
-      }
-
-      for (j = 0; j < LTRACK_NDAYS; ++j)
-	{
-	  for (i = 0; i < SH_LTRACK_GTRES; ++i)
-	    {
-	      k = get_bool(u->dates[j], i);
-	      // fprintf(stderr, "%d: %d: %d\n", j, i, k);
-	      if (j == WORKDAYS)
-		{
-		  if ( (i>=1 && i<=9) || (i>=45 && i <=110) || 
-		       (i>=141 && i<=143) )
-		    {
-		      CuAssertTrue(tc, k > 0);
-		    }
-		  else
-		    {
-		      CuAssertIntEquals(tc, 0, k);
-		    }
-		}
-	      else
-		{
-		  if ((count == 0 && j == SUNDAY) || 
-		      (count == 2 && j == SATURDAY))
-		    CuAssertTrue(tc, k > 0);
-		  else
-		    CuAssertIntEquals(tc, 0, k);
-		}
-	    }
-	}
-
-      if (u->next == NULL)
-	break;
-
-      u = u->next; ++count;
-
-    } while (1 == 1);
-  }
-
-  sh_login_reset();
-  CuAssertIntEquals(tc, SIG00, sig_level);
-  CuAssertIntEquals(tc, CHECK_NONE, check_level);
-
-  /* check dates */
-
-  j = sh_login_set_def_allow("workdays( 0:12-1:30, 07:30-18:29,23:30-23:59)");
-  CuAssertIntEquals(tc, 0, j);
-
-  j = check_login_date("rainer", 0, 2);
-  CuAssertIntEquals(tc, -1, j);
-  j = check_login_date("rainer", 1, 2);
-  CuAssertIntEquals(tc,  0, j);
-  j = check_login_date("rainer",50, 3);
-  CuAssertIntEquals(tc,  0, j);
-  j = check_login_date("rainer",142, 5);
-  CuAssertIntEquals(tc,  0, j);
-  j = check_login_date("rainer", 1, 0);
-  CuAssertIntEquals(tc, -1, j);
-  j = check_login_date("rainer", 1, 6);
-  CuAssertIntEquals(tc, -1, j);
-  j = sh_login_set_user_allow("rainer :saturday( 0:0-23:59)");
-  CuAssertIntEquals(tc, 0, j);
-  j = check_login_date("rainer", 1, 6);
-  CuAssertIntEquals(tc,  0, j);
-  j = sh_login_set_user_allow("mouse :sunday( 0:0-23:59)");
-  CuAssertIntEquals(tc, 0, j);
-  j = sh_login_set_user_allow("cat :saturday(0:0-23:59)");
-  CuAssertIntEquals(tc, 0, j);
-  j = check_login_date("rainer", 1, 6);
-  CuAssertIntEquals(tc,  0, j);
-  j = check_login_date("mouse", 1, 6);
-  CuAssertIntEquals(tc, -1, j);
-  j = check_login_date("mouse", 1, 0);
-  CuAssertIntEquals(tc,  0, j);
-  j = check_login_date("cat", 1, 6);
-  CuAssertIntEquals(tc,  0, j);
-  j = check_login_date("dog", 1, 6);
-  CuAssertIntEquals(tc, -1, j);
-
-  sh_login_reset();
-
-  /* statistics, critical values */
-  {
-    double f;
-
-    f = M_crit(1, SIG05);
-    CuAssertTrue(tc, f > 0.974 && f < 0.976);
-    f = M_crit(13, SIG05);
-    CuAssertTrue(tc, f > 0.576 && f < 0.578);
-    f = M_crit(22, SIG05);
-    CuAssertTrue(tc, f > 0.405 && f < 0.407);
-    f = M_crit(10, SIG05);
-    CuAssertTrue(tc, f > 0.646 && f < 0.648);
-    f = M_crit(10, SIG01);
-    CuAssertTrue(tc, f > 0.759 && f < 0.761);
-  }
-
-  /* stripped hostname */
-  p = stripped_hostname("127.20.120.100");
-  CuAssertStrEquals(tc, "127.20", p);
-
-  p = stripped_hostname("foo.www.example.com");
-  CuAssertStrEquals(tc, p, "www.example.com");
-
-  p = stripped_hostname("www.example.com");
-  CuAssertStrEquals(tc, p, "example.com");
-
-  p = stripped_hostname("localhost");
-  CuAssertStrEquals(tc, p, "localhost");
-
-  {
-    struct tm tt;
-
-    tt.tm_hour =  0;
-    tt.tm_min  = 30;
-    tt.tm_sec  =  0;
-
-    for (i = 0; i < 24; ++i)
-      {
-	tt.tm_hour =  i;
-	j = time_to_index(&tt, SH_LTRACK_HTRES);
-	CuAssertIntEquals(tc, j, i);
-      }
-
-    tt.tm_min  = 10;
-
-    for (i = 0; i < 24; ++i)
-      {
-	tt.tm_hour =  i;
-	j = time_to_index(&tt, SH_LTRACK_GTRES);
-	CuAssertIntEquals(tc, 1+i*6, j);
-      }
-  } 
-}
-/* #ifdef SH_CUTEST */
-#endif
-
-#else
-
-#ifdef SH_CUTEST
-#include <stdlib.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#include "CuTest.h"
-
-void Test_login (CuTest *tc) {
-  (void) tc;
-}
-
-/* #ifdef SH_CUTEST */
-#endif
-
-#endif
Index: trunk/src/sh_mail.c
===================================================================
--- trunk/src/sh_mail.c	(revision 591)
+++ trunk/src/sh_mail.c	(revision 1)
@@ -34,8 +34,15 @@
 #if defined(SH_WITH_MAIL)
 
+#if TIME_WITH_SYS_TIME
+#include <sys/time.h>
+#include <time.h>
+#else
 #if HAVE_SYS_TIME_H
 #include <sys/time.h>
+#else
+#include <time.h>
 #endif
-#include <time.h>
+#endif
+
 
 #ifdef HAVE_MEMORY_H
@@ -51,9 +58,4 @@
 #include "sh_fifo.h"
 #include "sh_tools.h"
-#include "sh_pthread.h"
-#include "sh_filter.h"
-#include "sh_mail_int.h"
-#include "sh_nmail.h"
-#include "sh_ipvx.h"
 
 #undef  FIL__
@@ -62,6 +64,19 @@
 #undef  BAD
 
-static int failedMail = S_FALSE;
-
+static int failedMail = SL_FALSE;
+
+/* MX Resolver Struct
+ */
+typedef struct mx_ {
+  int    pref;
+  char * address;
+} mx;
+
+typedef struct dnsrep_ {
+  int    count;
+  mx   * reply;
+} dnsrep;
+
+static int free_mx (dnsrep * answers);
 static dnsrep * return_mx (char *domain);
 
@@ -78,5 +93,5 @@
 static mail_trail_type * mail_trail = NULL;
 
-int sh_mail_sigverify (const char * s)
+int sh_mail_sigverify (char * s)
 {
   SL_TICKET  fd;
@@ -109,5 +124,5 @@
       _exit (EXIT_FAILURE);
     }
-  if ( SL_ISERROR(fd = sl_open_read (FIL__, __LINE__, s, SL_NOPRIV)))
+  if ( SL_ISERROR(fd = sl_open_read (s, SL_NOPRIV)))
     {
       fprintf(stderr, _("Could not open file %s\n"), s);
@@ -115,6 +130,6 @@
     }
 
-  buf     = SH_ALLOC( (size_t)(SH_MSG_BUF+SH_BUFSIZE+1));
-  bufc    = SH_ALLOC( (size_t)(SH_MSG_BUF+SH_MAXBUF+1));
+  buf     = SH_ALLOC( (size_t)(SH_BUFSIZE+1));
+  bufc    = SH_ALLOC( (size_t)(SH_MAXBUF+1));
 
   while (1 == 1)
@@ -128,5 +143,5 @@
 			     sizeof("-----BEGIN MESSAGE-----")-1)) 
 	{
-	  (void) sh_unix_getline (fd, buf, SH_MSG_BUF+SH_BUFSIZE);
+	  (void) sh_unix_getline (fd, buf, SH_BUFSIZE);
 	  if (buf[0] == '\0')
 	    {
@@ -147,5 +162,5 @@
       while (1 == 1)
 	{
-	  (void) sh_unix_getline (fd, buf, SH_MSG_BUF+SH_BUFSIZE);
+	  (void) sh_unix_getline (fd, buf, SH_BUFSIZE);
 	  if (0 == sl_strncmp(buf, _("-----BEGIN SIGNATURE-----"),
 			      sizeof("-----BEGIN SIGNATURE-----")-1))
@@ -153,13 +168,13 @@
 	  if (buf[0] == '\0') 
 	    _exit (EXIT_FAILURE);
-	  (void) sh_util_compress(bufc, buf, SH_MSG_BUF+SH_MAXBUF-KEY_LEN);
+	  (void) sh_util_compress(bufc, buf, SH_MAXBUF-KEY_LEN);
 	}
       
       /* get signature and number 
        */
-      (void) sh_unix_getline (fd, key, (int)sizeof(key));
+      (void) sh_unix_getline (fd, key, (int)(  sizeof(key)-1));
       key[KEY_LEN] = '\0';
 
-      (void) sh_unix_getline (fd, number, (int)sizeof(number));
+      (void) sh_unix_getline (fd, number, (int)(sizeof(number)-1));
       number[(2*SH_MINIBUF) - 2]   = '\0';
       numsig = atol (number);
@@ -181,5 +196,5 @@
 	  if (numsig > 0)
 	    {
-	      fprintf (stderr, "%s",_("ERROR (no key -- cannot check)\n"));
+	      fprintf (stderr, _("ERROR (no key -- cannot check)\n"));
 	      continue;
 	    }
@@ -195,5 +210,5 @@
       else if (numsig == 0)
 	{
-	  fprintf (stderr, "%s",_("ERROR (repeated audit trail)\n"));
+	  fprintf (stderr, _("ERROR (repeated audit trail)\n"));
 	  continue;
 	}
@@ -204,10 +219,8 @@
 	  sh_util_encode(key, bufc, 1, 'A');
 	  (void) sl_strlcpy (mail_trail_ptr->trail_key, key, KEY_LEN+1);
-	  fprintf (stderr, "%s",_("(unchecked)\n"));
+	  fprintf (stderr, _("(unchecked)\n"));
 	}
       else
 	{
-	  char sigbuf[KEYBUF_SIZE];
-
 	  /* iterate key
 	   */
@@ -215,23 +228,20 @@
 	  for (i = 0; i < numsig; ++i) 
 	    {
-	      char hashbuf[KEYBUF_SIZE];
 	      (void) sl_strlcpy (key2, 
-				 sh_tiger_hash (key2, TIGER_DATA, KEY_LEN,
-						hashbuf, sizeof(hashbuf)), 
+				 sh_tiger_hash (key2, TIGER_DATA, KEY_LEN), 
 				 KEY_LEN+1);
 	    }
 	  
-	  theSig = sh_util_siggen (key2, bufc, sl_strlen(bufc), 
-				   sigbuf, sizeof(sigbuf));
-
+
+	  theSig = sh_util_siggen (key2, bufc, sl_strlen(bufc));
 	  if (sl_strncmp (key, 
 			  theSig,
 			  KEY_LEN) != 0) 
 	    {
-	      fprintf (stderr, "%s",_("(FAILED)\n"));
+	      fprintf (stderr, _("(FAILED)\n"));
 	    } 
 	  else 
 	    { 
-	      fprintf (stderr, "%s",_("(passed)\n"));
+	      fprintf (stderr, _("(passed)\n"));
 	    }
 
@@ -243,5 +253,285 @@
 }
 
-int sh_mail_setNum (const char * str)
+#define SH_FILT_NUM 32
+#define SH_FILT_OR  0
+#define SH_FILT_AND 1
+#define SH_FILT_NOT 2
+#define SH_FILT_INIT { 0, { NULL }, 0, { NULL }, 0, { NULL }}
+
+typedef struct _sh_filter_type
+{
+  int      for_c;
+  char   * for_v[SH_FILT_NUM];
+  int      fand_c;
+  char   * fand_v[SH_FILT_NUM];
+  int      fnot_c;
+  char   * fnot_v[SH_FILT_NUM];
+
+} sh_filter_type;
+
+static
+int sh_filter_filteradd (char * argstring, sh_filter_type * filter, int ftype)
+{
+  int     i = 0;
+  int     flag = 0;
+  size_t  s;
+
+  char  * dup;
+  char  * p;
+  char  * end;
+  int   * ntok;
+  char ** stok;
+
+  SL_ENTER(_("sh_filter_filteradd"));
+
+  if (NULL == argstring)
+    {
+      SL_RETURN((-1), _("sh_filter_filteradd")); 
+    }
+
+  if (ftype == SH_FILT_OR) {
+    ntok = &(filter->for_c);
+    stok = filter->for_v;
+  }
+  else if (ftype == SH_FILT_AND) {
+    ntok = &(filter->fand_c);
+    stok = filter->fand_v;
+  }
+  else if (ftype == SH_FILT_NOT) {
+    ntok = &(filter->fnot_c);
+    stok = filter->fnot_v;
+  }
+  else {
+    SL_RETURN((-1), _("sh_filter_filteradd")); 
+  }
+
+  *ntok = 0;
+
+  dup = sh_util_strdup(argstring);
+  p   = dup;
+
+  do
+    {
+      while (*p == ',' || *p == ' ' || *p == '\t')
+	++p;
+      if (*p == '\0')
+	break;
+
+      end = p; ++end;
+      if (*end == '\0')
+	break;
+
+      if (*p == '\'')
+	{
+	  ++p; end = p; ++end;
+	  if (*p == '\0' || *end == '\0')
+	    break;
+	  while (*end != '\0' && *end != '\'')
+	    ++end;
+	}
+      else if (*p == '"')
+	{
+	  ++p; end = p; ++end;
+	  if (*p == '\0' || *end == '\0')
+	    break;
+	  while (*end != '\0' && *end != '"')
+	    ++end;
+	}
+      else
+	{
+	  while (*end != '\0' && *end != ',' && *end != ' ' && *end != '\t')
+	    ++end;
+	}
+      if (*end == '\0')
+	flag = 1;
+      else
+	*end = '\0';
+
+      s = strlen(p) + 1;
+      if (stok[i] != NULL)
+	SH_FREE(stok[i]);
+      stok[i] = SH_ALLOC(s);
+      (void) sl_strlcpy(stok[i], p, s);
+
+      p = end; ++p;
+
+      ++i;
+      if (i == SH_FILT_NUM)
+	break;
+    }
+  while (p != NULL && *p != '\0' && flag == 0);
+
+  *ntok = i;
+  SH_FREE(dup);
+
+  SL_RETURN (0, _("sh_filter_filteradd"));
+}
+
+/*
+ * -- check filters
+ */
+static 
+int sh_filter_filter (char * message, sh_filter_type * filter)
+{
+  int i;
+  int j = 0;
+
+  SL_ENTER(_("sh_mail_filter"));
+
+  /* Presence of any of these keywords prevents execution.
+   */
+  if (filter->fnot_c > 0)
+    {
+      for (i = 0; i < filter->fnot_c; ++i)
+	{
+	  if (NULL != sl_strstr(message, filter->fnot_v[i]))
+	    {
+	      SL_RETURN ((-1), _("sh_filter_filter"));
+	    }
+	}
+    }
+
+  /* Presence of all of these keywords is required for execution.
+   */
+  if (filter->fand_c > 0)
+    {
+      j = 0;
+
+      for (i = 0; i < filter->fand_c; ++i)
+	if (NULL != sl_strstr(message, filter->fand_v[i]))
+	  ++j;
+
+      if (j != filter->fand_c)
+	{
+	  SL_RETURN ((-1), _("sh_filter_filter"));
+	}
+    }
+
+  /* Presence of at least one of these keywords is required for execution.
+   */
+  if (filter->for_c > 0)
+    {
+      for (i = 0; i < filter->for_c; ++i)
+	{
+	  if (NULL != sl_strstr(message, filter->for_v[i]))
+	    {
+	      goto isok;
+	    }
+	}
+      SL_RETURN ((-1), _("sh_filter_filter"));
+    }
+
+ isok:
+  SL_RETURN ((0), _("sh_filter_filter"));
+}
+
+
+static sh_filter_type mail_filter = SH_FILT_INIT;
+
+/*
+ * -- add keywords to the OR filter
+ */
+int sh_mail_add_or (char * str)
+{
+  return (sh_filter_filteradd (str, &(mail_filter), SH_FILT_OR));
+}
+
+/*
+ * -- add keywords to the AND filter
+ */
+int sh_mail_add_and (char * str)
+{
+  return (sh_filter_filteradd (str, &(mail_filter), SH_FILT_AND));
+}
+
+/*
+ * -- add keywords to the NOT filter
+ */
+int sh_mail_add_not (char * str)
+{
+  return (sh_filter_filteradd (str, &(mail_filter), SH_FILT_NOT));
+}
+
+
+static char * address_list[8] = { 
+  NULL, NULL, NULL, NULL, 
+  NULL, NULL, NULL, NULL 
+};
+
+static   int   address_num = 0;
+static   int   address_num_compiled = 0;
+static   int   setaddress_compiled = S_FALSE;
+
+void reset_count_dev_mail(void)
+{
+  /* if not, then we still have the compiled-in address (if any), so we
+   * don' touch them
+   */
+  if (address_num_compiled == -99)
+    address_num = 0;
+  return;
+}
+
+int sh_mail_setaddress (char * address)
+{
+  char     *     p;
+
+  SL_ENTER(_("sh_mail_setaddress"));
+  
+  if (0 == strcmp(address, _("NULL")))
+    SL_RETURN ( (0), _("sh_mail_setaddress"));
+    
+  if (address != NULL && address_num < (2 * SH_PATHBUF / 64 )) 
+    {
+      if (address_num < (SH_PATHBUF / 64 ))
+	p = &sh.srvmail.name[address_num*64];
+      else
+	p = &sh.srvmail.alt[address_num*64];
+
+      (void) sl_strlcpy (p, address, 64);
+      
+      if ((p == NULL) || ( sl_strlen(address) != sl_strlen(p)))
+	{
+	  memset(p, (int)'\0', 64);
+	  SL_RETURN ( (-1), _("sh_mail_setaddress"));
+	}
+      address_list[address_num] = p;
+#if 0
+      if (!sl_is_suid())
+	{
+	  TPT(( 0, FIL__, __LINE__, _("msg=<address_list[%d] = %s>\n"), 
+		address_num, address_list[address_num]));
+	}
+#endif
+      if (setaddress_compiled == S_TRUE)
+	{
+	  ++address_num;
+	  ++address_num_compiled;
+	}
+      else
+	{
+	  if (address_num == address_num_compiled)
+	    {
+	      address_num = 0;
+	      address_num_compiled = -99;
+	    }
+	  ++address_num;
+	}
+      SL_RETURN ( (0), _("sh_mail_setaddress"));
+    }
+  SL_RETURN ( (-1), _("sh_mail_setaddress"));
+}
+
+int sh_mail_setaddress_int (char * address)
+{
+  int i;
+  SL_ENTER(_("sh_mail_setaddress_int"));
+  setaddress_compiled = S_TRUE;
+  i = sh_mail_setaddress(address);
+  setaddress_compiled = S_FALSE;
+  SL_RETURN(i, _("sh_mail_setaddress_int"));
+}
+
+int sh_mail_setNum (char * str)
 {
   int i = atoi (str);
@@ -257,11 +547,11 @@
 
 
-int sh_mail_all_in_one = S_FALSE;
-
-int sh_mail_setFlag (const char * str)
+static int all_in_one = S_FALSE;
+
+int sh_mail_setFlag (char * str)
 {
   int i;
   SL_ENTER(_("sh_mail_setFlag"));
-  i = sh_util_flagval(str, &sh_mail_all_in_one);
+  i = sh_util_flagval(str, &all_in_one);
   SL_RETURN(i, _("sh_mail_setFlag"));
 }
@@ -269,5 +559,5 @@
 static char * mail_subject = NULL;
 
-int set_mail_subject (const char * str)
+int set_mail_subject (char * str)
 {
   SL_ENTER(_("set_mail_subject"));
@@ -288,7 +578,6 @@
 }
 
-SH_MUTEX_INIT(mutex_fifo_mail, PTHREAD_MUTEX_INITIALIZER);
-
-SH_FIFO * fifo_mail = NULL;
+
+static SH_FIFO * fifo_mail = NULL;
 
 static
@@ -303,5 +592,4 @@
     SL_RET0(_("sh_mail_emptystack"));
 
-  SH_MUTEX_LOCK(mutex_fifo_mail);
   while (NULL != (msg = pop_list(fifo_mail)))
     {
@@ -310,5 +598,4 @@
       SH_FREE(msg);
     }
-  SH_MUTEX_UNLOCK(mutex_fifo_mail);
 
   SL_RET0(_("sh_mail_emptystack"));
@@ -317,25 +604,26 @@
 /* insert "\r\n" after each 998 char
  */
-static char * split_string(const char * str);
-
-/* fixes warning: variable âpâ might be clobbered by âlongjmpâ or âvforkâ*/
-static char ** p_dummy;
-
-int sh_mail_pushstack (int severity, const char * msg, const char * alias)
+static char * split_string(char * str);
+
+int sh_mail_pushstack (/*@null@*/char * msg)
 {
   char * p;
-  volatile int    retval = 0;
+  int    retval = 0;
   int    status;
 
   SL_ENTER(_("sh_mail_pushstack"));
 
-  if (msg == NULL || failedMail == S_TRUE /* || sh.srvmail.name[0] == '\0' */) 
+  if (msg == NULL || failedMail == SL_TRUE || sh.srvmail.name[0] == '\0') 
     SL_RETURN((0), (_("sh_mail_pushstack")));
 
+  if (0 != sh_filter_filter(msg, &mail_filter))
+    SL_RETURN((0), (_("sh_mail_pushstack")));
+
+#if 0
+  if (msg != NULL && sl_strlen(msg) > 998)  /* RFC 2822 */
+    msg[998] = '\0';
+#endif
+
   p = split_string(msg);
-  /* fixes "variable âpâ might be clobbered by âlongjmpâ or âvforkâ" */
-  p_dummy = &p;
-
-  SH_MUTEX_LOCK(mutex_fifo_mail);
 
   if (fifo_mail == NULL)
@@ -344,7 +632,6 @@
       fifo_init(fifo_mail);
     }
-  status = push_list (fifo_mail, p, severity, alias);
-  SH_MUTEX_UNLOCK(mutex_fifo_mail);
-
+
+  status = push_list (fifo_mail, p);
   if (status >= 0)
     ++sh.mailNum.alarm_last;
@@ -354,6 +641,6 @@
   if (sh.mailNum.alarm_last >= sh.mailNum.alarm_interval)
     {
-      BREAKEXIT(sh_nmail_flush);
-      retval = sh_nmail_flush ();
+      BREAKEXIT(sh_mail_msg);
+      retval = sh_mail_msg (NULL);
     }
 
@@ -366,9 +653,9 @@
 /* The mailer.
  */
-static int sh_mail_end_conn (FILE * connfile, int fd);
-static FILE * sh_mail_start_conn (struct alias * address, int * fd, int * anum);
+static int sh_mail_end_conn (FILE * connfile);
+static FILE * sh_mail_start_conn (int aFlag);
 
 static
-void sh_mail_get_subject(const char * message,
+void sh_mail_get_subject(char * message,
 			 char * mheader, size_t len)
 {
@@ -384,5 +671,4 @@
   char * mptr;
   char   sev[8];
-  char * msg;
 
   SL_ENTER(_("sh_mail_get_subject"));
@@ -401,7 +687,5 @@
   /* fast forward to the important part
    */
-  msg  = sh_util_strdup(message);
-
-  mptr = sl_strstr(msg, _("msg="));
+  mptr = sl_strstr(message, _("msg="));
   if (mptr)
     {
@@ -410,7 +694,7 @@
     }
   else
-    rep_serv_tab[2].data_str   = msg;
-
-  mptr = sl_strstr(msg, _("sev="));
+    rep_serv_tab[2].data_str   = message;
+
+  mptr = sl_strstr(message, _("sev="));
   if (mptr)
     {
@@ -424,5 +708,5 @@
   else
     {
-      mptr = msg;
+      mptr = message;
       sev[0] = *mptr; ++mptr;
       sev[1] = *mptr; ++mptr;
@@ -447,120 +731,103 @@
   (void) sl_strlcat(mheader, p, len);
   SH_FREE(p);
-  SH_FREE(msg);
   SL_RET0(_("sh_mail_get_subject"));
 }
 
-sh_string * sh_mail_signature_block (sh_string  * sigMsg, char * recipient,
-				     char * bufcompress)
-{
-  time_t         id_audit;
-  char         * theSig;
-  char ibuf[80];
-  unsigned int count;
-
-  /* ------ signature block ------------------------------------ */
-  
-  sigMsg = sh_string_add_from_char(sigMsg, 
-				   _("-----BEGIN SIGNATURE-----\r\n"));
-  
-  count  = sh_nmail_get_mailkey (recipient, skey->mailkey_new, KEY_LEN+1,
-				 &id_audit);
-  
-  if (count != 0)
-    {
-      char sigbuf[KEYBUF_SIZE];
-      
-      /* Sign the message with the signature key.
-       */
-      theSig = sh_util_siggen (skey->mailkey_new, 
-			       bufcompress, sl_strlen(bufcompress),
-			       sigbuf, sizeof(sigbuf));
-      sigMsg = sh_string_add_from_char(sigMsg, theSig);
-    }
-  else
-    {
-       /* reveal first signature key
-       */
-      /* flawfinder: ignore */
-      (void) sl_strlcpy(skey->crypt, skey->mailkey_new, KEY_LEN+1); 
-      
-      BREAKEXIT(sh_util_encode);
-      /* flawfinder: ignore */
-      sh_util_encode(skey->crypt, bufcompress, 0, 'A');
-      
-      /* flawfinder: ignore */
-      sigMsg     = sh_string_add_from_char(sigMsg, skey->crypt);
-      
-      /* flawfinder: ignore */
-      memset (skey->crypt, 0, KEY_LEN);
-    }
-
-    sigMsg     = sh_string_add_from_char(sigMsg, "\r\n");
-
-    sl_snprintf(ibuf, sizeof(ibuf), _("%06u %010lu::%s\r\n"),
-		count, (unsigned long) id_audit, sh.host.name);
-
-    sigMsg     = sh_string_add_from_char(sigMsg, ibuf);
-    sigMsg     = sh_string_add_from_char(sigMsg, _("-----END MESSAGE-----"));
-
-    return sigMsg;
-}
-
-int sh_mail_msg (const char * message)
+
+#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
+#include <sys/mman.h>
+#endif
+
+static char * sh_mail_realloc (char * inbuf, size_t * insize, size_t increase)
+{
+  size_t newsize = (*insize) + increase + 1;
+  char * outbuf;
+
+  SL_ENTER(_("sh_mail_realloc"));
+
+  outbuf = SH_ALLOC(newsize);
+  MLOCK(outbuf, newsize);
+  (void) sl_strlcpy(outbuf, inbuf, newsize);
+
+  memset (inbuf, 0, (*insize));
+  MUNLOCK(inbuf, (*insize));
+  SH_FREE(inbuf);
+
+  *insize = newsize;
+
+  SL_RETURN( (outbuf), _("sh_mail_realloc"));
+}
+
+int sh_mail_msg (/*@null@*/char * message)
 {
     char         subject[32+32+SH_MINIBUF+2+3+SH_PATHBUF];
     char         mheader[32+32+SH_MINIBUF+2+3];
 
-    sh_string  * mailMsg;
-    sh_string  * compMsg;
-    int          status = 0;
-    volatile int errcount;
+    char       * mailMsg;
+    char       * popMsg;
+    int          status = 0, errcount;
     size_t       wrlen;
-    volatile int retval = -1;  
+    int          i;
+    int          num_popped = 0;
+    int          retval = -1;  
 
     char       * bufcompress;
-    size_t       compressed;
-
     static int   failcount = 0;
+    static int   isfirst   = 1;
+    static int   mailcount = 0;
     FILE       * connfile  = NULL;
 
+    struct  sigaction  old_act;
+    struct  sigaction  new_act;
+
+    static  time_t id_audit  = 0;
     static  time_t fail_time = 0;
     static  time_t success_time = 0;
 
-    int       ma_socket = -1;
-
-    int            address_num = 0;
-    sh_string    * theMsg = NULL;
+    static  int ma_block = 0;
+
+    SH_FIFO * fifo_temp = NULL;
+
+    char    * theSig;
+    char    * theMsg = NULL;
 
     /* #define SH_MAILBUF (256)    */
-#define SH_MAILBUF 4096 
-
-    char      timebuf[81];
+#define SH_MAILBUF (8*4096) 
+
+    size_t    msgbufsize = SH_MAILBUF;
+    size_t    combufsize = SH_MAILBUF;
 
     SL_ENTER(_("sh_mail_msg"));
 
-    /* 
-     * Return if we cannot mail.
+    if (ma_block == 1)
+      SL_RETURN( (0), _("sh_mail_msg"));
+
+    /* Return if we cannot mail.
      */
-    if (failedMail == S_TRUE) 
+    if (failedMail == SL_TRUE) 
       SL_RETURN((-1), _("sh_mail_msg"));
 
-    /*
-     * Final failure, can't mail for SH_MAX_FAIL hours.
-     */
+    if (failedMail == SL_FALSE && address_list[0] == NULL)
+      {
+	TPT((0, FIL__, __LINE__, 
+	     _("msg=<Mail error: no recipient address.>\n")));
+	failedMail = SL_TRUE;
+	SL_RETURN((-1), _("sh_mail_msg"));
+      }
+
     if ( (success_time > 0) && (fail_time > 0) &&
 	 (time(NULL) - success_time) > 3600*SH_MAX_FAIL)
       {
+	ma_block = 1;
 	sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_SRV_FAIL,
-			 _("mail"), 
-			 sh_string_str(all_recipients->recipient));
+			 _("mail"), address_list[0]);
+	ma_block = 0;
 	sh_mail_emptystack();
 	sh.mailNum.alarm_last = 0;
-	failedMail = S_TRUE;
+	failedMail = SL_TRUE;
 	SL_RETURN((-1), _("sh_mail_msg"));
       }
 
-    /*
-     * Try at most every three seconds to mail if there was a failure.
+    /* Try at most each hour.
      */
     if ((fail_time > 0) && (time(NULL) - fail_time) < 3/*600*/)
@@ -568,10 +835,10 @@
 	if (failcount > 3)
 	  {
-	    /* -- Save for later. Changed: done by caller. -- 
-	     *	    sh_nmail_pushstack (severity, message, alias);
+	    /* -- Save for later. -- 
 	     */
+	    sh_mail_pushstack (message);
 	    ++failcount;
 	    
-	    SL_RETURN((-2), _("sh_mail_msg"));
+	    SL_RETURN((-1), _("sh_mail_msg"));
 	  }
 	else
@@ -586,15 +853,19 @@
     fail_time = 0;
 
+    /* -- Polling, empty queue. --
+     */
+    if (message == NULL && sh.mailNum.alarm_last == 0)
+      SL_RETURN((-1), _("sh_mail_msg"));
+
+    /* -- Filtered. --
+     */
+    if (message != NULL && 0 != sh_filter_filter(message, &mail_filter))
+      SL_RETURN((-1), (_("sh_mail_msg")));
+
+
 
     /* ---------  Build complete message. ------------------------ */
 
-    /* Don't flush the queue here, because tag_list doesn't know
-     * how to filter messages. */
-
-    theMsg = sh_string_new_from_lchar(message, sl_strlen(message));
-    if (!theMsg)
-      {
-	SL_RETURN((-1), _("sh_mail_msg"));
-      }
+    theMsg = split_string(message);
 
     /* ---------- Header  ---------------------------------------- */
@@ -603,7 +874,5 @@
       {
 	(void) sl_strlcpy(mheader, _("Subject: "),       sizeof(mheader)-5);
-	(void) sl_strlcat(mheader, 
-			  sh_unix_time (0, timebuf, sizeof(timebuf)),
-			  sizeof(mheader)-5);
+	(void) sl_strlcat(mheader, sh_unix_time (0),     sizeof(mheader)-5);
 	(void) sl_strlcat(mheader, " ",                  sizeof(mheader)-5);
 	(void) sl_strlcat(mheader, sh.host.name,         sizeof(mheader)-5);
@@ -611,4 +880,12 @@
     else
       {
+	
+	if (message == NULL)
+	  {
+	    theMsg  = pop_list(fifo_mail);
+	    message = theMsg;
+	    if (message)
+	      --sh.mailNum.alarm_last;
+	  }
 	
 	if (message)
@@ -619,7 +896,5 @@
 	  {
 	    (void) sl_strlcpy(mheader, _("Subject: "),     sizeof(mheader)-5);
-	    (void) sl_strlcat(mheader, 
-			      sh_unix_time (0, timebuf, sizeof(timebuf)),
-			      sizeof(mheader)-5);
+	    (void) sl_strlcat(mheader, sh_unix_time (0),   sizeof(mheader)-5);
 	    (void) sl_strlcat(mheader, " ",                sizeof(mheader)-5);
 	    (void) sl_strlcat(mheader, sh.host.name,       sizeof(mheader)-5);
@@ -633,6 +908,5 @@
     /* ---------- Message  --------------------------------------- */
 
-    (void) sl_strlcpy(subject, sh_unix_time (0, timebuf, sizeof(timebuf)),
-		      sizeof(subject));
+    (void) sl_strlcpy(subject, sh_unix_time (0),          sizeof(subject));
     (void) sl_strlcat(subject, " ",                       sizeof(subject));
     (void) sl_strlcat(subject, sh.host.name,              sizeof(subject));
@@ -640,139 +914,195 @@
 
 
-    mailMsg     = sh_string_new (SH_MAILBUF);
-    compMsg     = sh_string_new (SH_MAILBUF);
-
-    mailMsg     = sh_string_add_from_char(mailMsg, mheader);
-    mailMsg     = sh_string_add_from_char(mailMsg, 
-					  _("-----BEGIN MESSAGE-----\r\n"));
-
-    mailMsg     = sh_string_add_from_char(mailMsg, subject);
-    mailMsg     = sh_string_add          (mailMsg, theMsg);
-    mailMsg     = sh_string_add_from_char(mailMsg, "\r\n");
-
-    /* ---------- Compressed Message  ---------------------------- */
-
-    compMsg     = sh_string_add_from_char(compMsg, subject);
-    compMsg     = sh_string_add          (compMsg, theMsg);
-    compMsg     = sh_string_add_from_char(compMsg, "\r\n");
-
-    bufcompress = SH_ALLOC(sh_string_len(compMsg) + KEY_LEN + 1);
+    mailMsg     = (char *) SH_ALLOC (msgbufsize);
+    bufcompress = (char *) SH_ALLOC (combufsize);
+
+    MLOCK(mailMsg     , msgbufsize);
+    MLOCK(bufcompress , combufsize);
+
+    (void) sl_strlcpy(mailMsg, mheader, msgbufsize);
     bufcompress[0] = '\0';
 
-    compressed = sh_util_compress (bufcompress, 
-				   sh_string_str(compMsg), 
-				   sh_string_len(compMsg) + 1);
+    (void) sl_strlcat(mailMsg, _("-----BEGIN MESSAGE-----\r\n"), msgbufsize);
+    (void) sl_strlcat(mailMsg, subject, msgbufsize);
+    (void) sh_util_compress (bufcompress, subject, 
+			     (combufsize - KEY_LEN - 1));
+    if (message != NULL)
+      {
+	if ((sl_strlen(theMsg) + sl_strlen(mailMsg) + 1) > 
+	    (msgbufsize-(4*KEY_LEN)))
+	  {
+	    mailMsg     = sh_mail_realloc(mailMsg,     
+					  &msgbufsize, sl_strlen(theMsg)+2);
+	    bufcompress = sh_mail_realloc(bufcompress, 
+					  &combufsize, sl_strlen(theMsg));
+	  }
+	(void) sl_strlcat(mailMsg,  theMsg, msgbufsize-(4*KEY_LEN));
+	(void) sl_strlcat(mailMsg,  "\r\n", msgbufsize-(4*KEY_LEN));
+
+	(void) sh_util_compress (bufcompress,  theMsg, combufsize-KEY_LEN-1);
+      }
+
+    if (sh.mailNum.alarm_last > 0)
+      {
+	fifo_temp = SH_ALLOC (sizeof(SH_FIFO));
+	fifo_init (fifo_temp);
+
+	while ( NULL != (popMsg = pop_list(fifo_mail)) )
+	  {
+	    (void) push_list (fifo_temp, popMsg);
+
+	    if ((sl_strlen(popMsg) + sl_strlen(mailMsg) + 1) > 
+		(msgbufsize-(4*KEY_LEN)))
+	      {
+		mailMsg     = sh_mail_realloc(mailMsg,     
+					      &msgbufsize, 
+					      sl_strlen(popMsg)+2);
+		bufcompress = sh_mail_realloc(bufcompress, 
+					      &combufsize, 
+					      sl_strlen(popMsg));
+	      }
+
+	    (void) sl_strlcat(mailMsg, popMsg, msgbufsize-(4*KEY_LEN));
+	    (void) sl_strlcat(mailMsg, "\r\n", msgbufsize-(4*KEY_LEN));
+	    (void) sh_util_compress(bufcompress, popMsg, combufsize-KEY_LEN-1);
+	    SH_FREE(popMsg);
+	    --sh.mailNum.alarm_last;
+	    ++num_popped;
+	  }
+      }
+
+    /* ------ signature block ------------------------------------ */
+    
+    (void) sl_strlcat(mailMsg, _("-----BEGIN SIGNATURE-----\r\n"), msgbufsize);
+
+    /* Generate new signature key.
+     */
+    if (isfirst == 1)
+      {
+	BREAKEXIT(sh_util_keyinit);
+	(void) sh_util_keyinit (skey->mailkey_old, KEY_LEN+1);
+      }
+
+    /* iterate the key
+     */
+    (void) sl_strlcpy(skey->mailkey_new,
+		      sh_tiger_hash (skey->mailkey_old, TIGER_DATA, KEY_LEN),
+		      KEY_LEN+1);
+
+    if (isfirst == 0)
+      {
+        /* Sign the message with the signature key.
+         */
+	theSig = sh_util_siggen (skey->mailkey_new, 
+				 bufcompress, sl_strlen(bufcompress));
+	(void) sl_strlcat (mailMsg, 
+			   theSig,
+			   msgbufsize);
+      }
+    else
+      {
+        id_audit = time (NULL);
+
+        /* reveal first signature key
+         */
+        (void) sl_strlcpy(skey->crypt, skey->mailkey_new, KEY_LEN+1);
+
+	BREAKEXIT(sh_util_encode);
+        sh_util_encode(skey->crypt, bufcompress, 0, 'A');
+
+        (void) sl_strlcat (mailMsg, skey->crypt, msgbufsize);
+        memset (skey->crypt, 0, KEY_LEN);
+        isfirst = 0;
+      }
+    (void) sl_strlcat (mailMsg, "\r\n", msgbufsize);
+
+    /* X(n) -> X(n-1)
+     */
+    (void) sl_strlcpy (skey->mailkey_old, skey->mailkey_new, KEY_LEN+1);
+
+    /*@-bufferoverflowhigh@*/
+    sprintf(subject, _("%06d %010ld::%s\r\n"),         /* known to fit  */
+            mailcount, (long) id_audit, sh.host.name);
+    /*@+bufferoverflowhigh@*/
+
+    (void) sl_strlcat (mailMsg, subject, msgbufsize);
+    ++mailcount;
+
+    (void) sl_strlcat (mailMsg, _("-----END MESSAGE-----"), msgbufsize);
+
+
 
     /* ---------- Connect ---------------------------------------- */
 
+
+
+    /* -- Catch (ignore) 'broken pipe'.
+     */
+    new_act.sa_handler = SIG_IGN;
+    sigemptyset( &new_act.sa_mask );         /* set an empty mask       */
+    new_act.sa_flags = 0;                    /* init sa_flags           */
+
+    (void) sigaction (SIGPIPE, &new_act, &old_act);
+
+    i        = 0;
     errcount = 0;
 
-    if (sh_mail_all_in_one == S_FALSE)
+    if (all_in_one == S_FALSE)
       {
-	struct alias * address_list;
-
-	address_list = all_recipients;
-
-	while (address_list)
+	while (address_list[i] != NULL && i < address_num)
 	  {
-	    if (address_list->send_mail == 1)
+	    connfile = sh_mail_start_conn (i);
+	    
+	    if (NULL != connfile)
 	      {
-		connfile = sh_mail_start_conn (address_list, 
-					       &ma_socket, &address_num);
-	    
-		if (NULL != connfile)
-		  {
-		    wrlen = fwrite (sh_string_str(mailMsg), 1, 
-				    sh_string_len(mailMsg), connfile);
-		    wrlen -= sh_string_len(mailMsg);
-
-		    if (wrlen == 0)
-		      {
-			sh_string  * sigMsg  = sh_string_new (0);
-
-			sigMsg = sh_mail_signature_block (sigMsg, 
-							  sh_string_str(address_list->recipient),
-							  bufcompress);
-
-			wrlen = fwrite (sh_string_str(sigMsg), 1, 
-					sh_string_len(sigMsg), connfile);
-			wrlen -= sh_string_len(sigMsg);
-
-			sh_string_destroy(&sigMsg);
-		      }
-
-		    if (wrlen == 0) 
-		      status = sh_mail_end_conn (connfile, ma_socket);
-		    else
-		      status = -1;
-		  }
-		if (NULL == connfile ||  status != 0)
-		  {
-		    sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_SRV_FAIL,
-				     _("mail"), 
-				     sh_string_str(address_list->recipient));
-		    ++errcount;
-		    ++sh.statistics.mail_failed;
-		  }
+		wrlen = fwrite (mailMsg, 1, sl_strlen(mailMsg), 
+				 connfile);
+		wrlen -= sl_strlen(mailMsg);
+		if (wrlen == 0) 
+		  status = sh_mail_end_conn (connfile);
 		else
-		  {
-		    ++sh.statistics.mail_success;
-		  }
-		
-		if (connfile != NULL)
-		  {
-		    (void) sl_fclose (FIL__, __LINE__, connfile);
-		    connfile = NULL;
-		  }
+		  status = -1;
 	      }
-	    address_list = address_list->all_next;
+	    if (NULL == connfile ||  status != 0)
+	      {
+		ma_block = 1;
+		sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_SRV_FAIL,
+				 _("mail"), address_list[i]);
+		ma_block = 0;
+		++errcount;
+		++sh.statistics.mail_failed;
+	      }
+	    else
+	      {
+		++sh.statistics.mail_success;
+	      }
+
+	    if (connfile != NULL)
+	      {
+		(void) fclose (connfile);
+		connfile = NULL;
+	      }
+	    ++i;
 	  }
       }
     else
       {
-	connfile = sh_mail_start_conn (NULL, &ma_socket, &address_num);
-
+	connfile = sh_mail_start_conn ( -9 );
+	
 	if (NULL != connfile)
 	  {
-	    wrlen = fwrite (sh_string_str(mailMsg), 1, 
-			    sh_string_len(mailMsg), connfile);
-	    wrlen -= sh_string_len(mailMsg);
-
+	    wrlen = fwrite (mailMsg, 1, sl_strlen(mailMsg), connfile);
+	    wrlen -= sl_strlen(mailMsg);
 	    if (wrlen == 0)
-	      {
-		sh_string  * sigMsg  = sh_string_new (0);
-		
-		sigMsg  = sh_mail_signature_block (sigMsg, 
-						   NULL,
-						   bufcompress);
-		
-		wrlen = fwrite (sh_string_str(sigMsg), 1, 
-				sh_string_len(sigMsg), connfile);
-		wrlen -= sh_string_len(sigMsg);
-		
-		sh_string_destroy(&sigMsg);
-	      }
-
-	    if (wrlen == 0)
-	      status = sh_mail_end_conn (connfile, ma_socket);
+	      status = sh_mail_end_conn (connfile);
 	    else
 	      status = -1;
 	  }
-
-	if (NULL == connfile || status != 0)
+	if (NULL == connfile ||  status != 0)
 	  {
-	    struct alias* ma_address = all_recipients;
-
-	    while (ma_address)
-	      {
-		if (ma_address->send_mail == 1)
-		  break;
-		ma_address = ma_address->all_next;
-	      }
-
-	    if (ma_address)
-	      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_SRV_FAIL,
-			       _("mail"), 
-			       sh_string_str(ma_address->recipient));
+	    ma_block = 1;
+	    sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_SRV_FAIL,
+			     _("mail"), address_list[0]);
+	    ma_block = 0;
 	    errcount = address_num;
 	    ++sh.statistics.mail_failed;
@@ -785,31 +1115,65 @@
 	if (connfile != NULL)
 	  {
-	    (void) sl_fclose (FIL__, __LINE__, connfile);
+	    (void) fclose (connfile);
 	    connfile = NULL;
 	  }
       }
+
     
-    memset (bufcompress, 0, compressed);
+    memset (bufcompress, 0, combufsize);
+    MUNLOCK(bufcompress , combufsize);
     SH_FREE(bufcompress);
 
-    memset (sh_string_str(mailMsg), 0, sh_string_len(mailMsg));
-    memset (sh_string_str(compMsg), 0, sh_string_len(compMsg));
-    memset (sh_string_str(theMsg),  0, sh_string_len(theMsg));
-
-    sh_string_destroy(&mailMsg);
-    sh_string_destroy(&compMsg);
-    sh_string_destroy(&theMsg);
+    memset (mailMsg, 0, msgbufsize);
+    MUNLOCK(mailMsg , msgbufsize);
+    SH_FREE(mailMsg);
 
     /* --- Stay responsible for delivery in case of failure --- */
 
-    if (errcount == address_num)
+    if (errcount == address_num && fifo_temp != NULL)
       {
-	rollback_list(fifo_mail);
-	retval = -3;
+        while ( (NULL != (popMsg = pop_list(fifo_temp))) )
+          {
+            if (push_list (fifo_mail, popMsg) >= 0)
+	      ++sh.mailNum.alarm_last;
+            SH_FREE(popMsg);
+          }
+	if (message != NULL)
+	  {
+	    if (fifo_mail == NULL)
+	      {
+		fifo_mail = SH_ALLOC(sizeof(SH_FIFO));
+		fifo_init(fifo_mail);
+	      }
+	    retval = push_list (fifo_mail,  theMsg);
+	    if (retval >= 0) 
+	      ++sh.mailNum.alarm_last;
+	    if (retval == SH_FIFO_MAX)
+	      retval = -2;
+	    else
+	      retval = -1;
+	  }
       }
-    else
+    else if (fifo_temp != NULL)
       {
-	mark_list(fifo_mail);
+        while ( (NULL != (popMsg = pop_list(fifo_temp))) )
+          {
+            SH_FREE(popMsg);
+          }
       }
+    if (fifo_temp != NULL)
+      SH_FREE(fifo_temp);
+
+    /*
+    if (connfile != NULL) 
+      fclose (connfile);
+    */
+
+    if (theMsg != NULL)
+      SH_FREE(theMsg);
+
+    /* --- Reset signal. ---
+     */
+    (void) sigaction (SIGPIPE, &old_act, NULL);
 
     if (errcount == address_num)
@@ -818,5 +1182,4 @@
 	SL_RETURN((retval), _("sh_mail_msg"));
       }
-
     success_time = time(NULL);
     failcount = 0;
@@ -855,10 +1218,12 @@
 #endif
 
-static int sh_mail_wait(int code, int ma_socket);
+static int sh_mail_wait(int code, FILE * m_socket);
 
 static char * relay_host = NULL;
 
-int sh_mail_set_relay (const char * str_s)
-{
+int sh_mail_set_relay (char * str_s)
+{
+  size_t i = 0;
+
   SL_ENTER(_("sh_mail_set_relay"));
 
@@ -867,16 +1232,18 @@
 
   if (relay_host != NULL)
-    {
-      SH_FREE (relay_host);
+    SH_FREE (relay_host);
+
+  if (0 == sl_strncmp(str_s, _("NULL"), 4))
+    {
       relay_host = NULL;
-    }
-
-  if (0 == sl_strncmp(str_s, _("NULL"), 4))
-    {
       SL_RETURN( 0, _("sh_mail_set_relay"));
     }
 
-  relay_host = sh_util_strdup(str_s);
-
+  i = sl_strlen(str_s) + 1;
+  relay_host = SH_ALLOC (i);
+  if (relay_host != NULL)
+    (void) sl_strlcpy(relay_host, str_s, i);
+  else
+    fprintf(stderr, _("ERROR:  sh_mail_set_relay: Out of memory"));
   SL_RETURN( 0, _("sh_mail_set_relay"));
 }
@@ -884,5 +1251,5 @@
 static char * mail_sender = NULL;
 
-int sh_mail_set_sender (const char *str)
+int sh_mail_set_sender (char *str)
 {
   if (mail_sender != NULL) 
@@ -902,42 +1269,16 @@
 }
 
-static int sh_mail_port = IPPORT_SMTP;
-
-int sh_mail_set_port (const char * str)
-{
-  int i = atoi (str);
-  
-  SL_ENTER(_("sh_mail_set_port"));
-  
-  if (i >= 0 && i < 65535)
-    { 
-      sh_mail_port = i;
-    }
-  else
-    {
-      sh_mail_port = IPPORT_SMTP;
-      SL_RETURN ((-1), _("sh_mail_set_port"));
-    }
-  
-  SL_RETURN( (0), _("sh_mail_set_port"));
-}
 
 /*************************
  *
  * start connection
- * for details on SMTP, see RFC 821
- *
- * If ma_address == NULL, will send to all marked with
- * send_mail=1 in recipient list, else to ma_address.   
+ * for details on SMTP, see RFC 821 
  */
 
 static time_t time_wait = 300;
-static void report_smtp (char * reply);
-
-static FILE * sh_mail_start_conn (struct alias * ma_address, 
-				  int * ma_socket, int * anum)
+
+static FILE * sh_mail_start_conn (int aFlag)
 {
   char       * address;
-  int          aFlag = 0;
 
   int          ecount;
@@ -952,7 +1293,4 @@
   FILE       * connFile = NULL;
   struct tm  * my_tm;
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
-  struct tm    time_tm;
-#endif
   time_t       my_time;
   char         my_tbuf[128];
@@ -965,32 +1303,21 @@
   SL_ENTER(_("sh_mail_start_conn"));
 
-  *ma_socket = -1;
-  time_wait  = 300;
-
-  if (ma_address == NULL)
-    {
-      aFlag = 1;
-      ma_address = all_recipients;
-
-      while (ma_address)
-	{
-	  if (ma_address->send_mail == 1)
-	    break;
-	  ma_address = ma_address->all_next;
-	}
-    }
-
-  if (!ma_address)
+  time_wait = 300;
+
+  if (aFlag >= 0)
+    address = address_list[aFlag];
+  else
+    address = address_list[0];
+
+  TPT(( 0, FIL__, __LINE__, _("msg=<aFlag %d address %s>\n"), 
+	aFlag, address)); 
+
+  /* -------   split adress ------------------  */
+
+  if (0 == strcmp(address, _("NULL")))
     {
       SL_RETURN( NULL, _("sh_mail_start_conn"));
     } 
-
-  address = sh_string_str(ma_address->recipient);
-
-  TPT(( 0, FIL__, __LINE__, _("msg=<address %s>\n"), 
-	address)); 
-
-  /* -------   split adress ------------------  */
-
+    
   if (strchr (address, '@') == NULL) {
     (void) sl_strlcpy(ma_user,    address,     256);
@@ -1002,5 +1329,5 @@
       ++i;
     }
-    
+
     /* adress[i] = '@' 
      */
@@ -1024,16 +1351,10 @@
       TPT((0, FIL__, __LINE__, _("msg=<user %s machine %s>\n"), 
 	   ma_user, ma_machine)); 
-      fd = connect_port (ma_machine, sh_mail_port, 
+      fd = connect_port (ma_machine, IPPORT_SMTP, 
 			 error_call, &error_num, error_msg, 256);
     }
   else
     {
-      answers = ma_address->mx_list;
-      if (!answers)
-	{
-	  answers = return_mx (ma_machine);
-	  ma_address->mx_list = answers;
-	}
-
+      answers = return_mx (ma_machine);
       if (answers)
 	{
@@ -1047,9 +1368,10 @@
 		   _("msg=<user %s mx %s pref %d>\n"), 
 		   ma_user, ma_machine, result[i].pref));
-	      fd = connect_port (ma_machine, sh_mail_port, 
+	      fd = connect_port (ma_machine, IPPORT_SMTP, 
 				 error_call, &error_num, error_msg, 256);
 	      if (fd >= 0)
 		break;
 	    }
+	  (void) free_mx(answers);
 	}
       else
@@ -1078,5 +1400,5 @@
     {
       TPT(( 0, FIL__, __LINE__, _("msg=<fdopen() failed>\n")));
-      (void) sl_close_fd(FIL__, __LINE__, fd);
+      (void) close(fd);
       SL_RETURN( NULL, _("sh_mail_start_conn"));
     }
@@ -1085,5 +1407,5 @@
   /* say HELO to the other socket
    */
-  if (0 == sh_mail_wait (220, fd)) 
+  if (0 == sh_mail_wait (220, connFile)) 
     {
       sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET, 
@@ -1092,5 +1414,5 @@
 		      _("mail"), sh.host.name);
       TPT(( 0, FIL__, __LINE__, _("msg=<Timeout>\n")));
-      (void) sl_fclose(FIL__, __LINE__, connFile);
+      (void) fclose(connFile);
       SL_RETURN( NULL, _("sh_mail_start_conn"));
     }
@@ -1098,17 +1420,15 @@
   (void) fflush(connFile);
 
-  if (0 != sh_ipvx_is_numeric(sh.host.name))
-    {
-      sl_snprintf(error_msg, sizeof(error_msg), "HELO [%s]", 
-		  sh.host.name);
+  if (0 != is_numeric(sh.host.name))
+    {
+      TPT(( 0, FIL__, __LINE__, _("msg=<HELO [%s]>%c%c"), 
+	    sh.host.name, 13, 10));
     }
   else
     {
-      sl_snprintf(error_msg, sizeof(error_msg), "HELO %s", 
-		  sh.host.name);
-    }
-  report_smtp(error_msg);
-
-  if (0 != sh_ipvx_is_numeric(sh.host.name))
+      TPT(( 0, FIL__, __LINE__, _("msg=<HELO %s>%c%c"), 
+	    sh.host.name, 13, 10));
+    }
+  if (0 != is_numeric(sh.host.name))
     fprintf(connFile, _("HELO [%s]%c%c"), sh.host.name, 13, 10);
   else
@@ -1117,5 +1437,5 @@
   (void) fflush(connFile);
 
-  if (0 == sh_mail_wait(250, fd)) 
+  if (0 == sh_mail_wait(250, connFile)) 
     {
       sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET, 
@@ -1124,5 +1444,5 @@
 
       TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
-      (void) sl_fclose(FIL__, __LINE__, connFile);
+      (void) fclose(connFile);
       SL_RETURN( NULL, _("sh_mail_start_conn"));
     }
@@ -1135,5 +1455,5 @@
     {
       (void) sl_strlcat (this_address, "@", 256);
-      if (0 != sh_ipvx_is_numeric(sh.host.name))
+      if (0 != is_numeric(sh.host.name))
 	(void) sl_strlcat (this_address, _("example.com"), 256);
       else
@@ -1141,7 +1461,6 @@
     }
 
-  sl_snprintf(error_msg, sizeof(error_msg), "MAIL FROM:<%s>", 
-	      this_address);
-  report_smtp(error_msg);
+  TPT(( 0, FIL__, __LINE__,  _("msg=<MAIL FROM:<%s>>%c%c"), 
+	this_address, 13, 10));
 
   (void) fflush(connFile);
@@ -1151,5 +1470,5 @@
   (void) fflush(connFile);
 
-  if (0 == sh_mail_wait(250, fd)) 
+  if (0 == sh_mail_wait(250, connFile)) 
     {
       sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET, 
@@ -1157,5 +1476,5 @@
 		      _("mail"), this_address);
       TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
-      (void) sl_fclose(FIL__, __LINE__, connFile);
+      (void) fclose(connFile);
       SL_RETURN( NULL, _("sh_mail_start_conn"));
     }
@@ -1163,9 +1482,8 @@
   /* tell them who to send mail to
    */
-  if (aFlag == 0)
-    {
-      sl_snprintf(error_msg, sizeof(error_msg), "RCPT TO:<%s>", 
-		  address);
-      report_smtp(error_msg);
+  if (aFlag >= 0)
+    {
+      TPT(( 0, FIL__, __LINE__,  _("msg=<RCPT TO:<%s>>%c%c"), 
+	    address, 13, 10)); 
 
       (void) fflush(connFile);
@@ -1173,5 +1491,5 @@
       (void) fflush(connFile);
 
-      if (0 == sh_mail_wait(250, fd)) 
+      if (0 == sh_mail_wait(250, connFile)) 
 	{
 	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET, 
@@ -1179,52 +1497,35 @@
 			  _("mail"), address);
 	  TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
-	  (void) sl_fclose(FIL__, __LINE__, connFile);
+	  (void) fclose(connFile);
 	  SL_RETURN( NULL, _("sh_mail_start_conn"));
 	}
-      *anum = 1;
     }
   else
     {
-      int address_num = 0;
-      ecount      = 0;
-
-      ma_address = all_recipients;
-
-      while (ma_address)
-	{
-	  if (ma_address->send_mail != 1)
-	    {
-	      ma_address = ma_address->next;
-	      continue;
-	    }
-
-	  ++address_num;
-
-	  sl_snprintf(error_msg, sizeof(error_msg), "RCPT TO:<%s>", 
-		      sh_string_str(ma_address->recipient));
-	  report_smtp(error_msg);
+      ecount = 0;
+      for (i = 0; i < address_num; ++i)
+	{
+	  if (address_list[i] == NULL)  /* paranoia */
+	    break;
+	  TPT(( 0, FIL__, __LINE__,  _("msg=<RCPT TO:<%s>>%c%c"), 
+		address_list[i], 13, 10)); 
 	  
 	  (void) fflush(connFile);
-	  fprintf(connFile, _("RCPT TO:<%s>%c%c"), 
-		  sh_string_str(ma_address->recipient), 13, 10); 
+	  fprintf(connFile, _("RCPT TO:<%s>%c%c"), address_list[i], 13, 10); 
 	  (void) fflush(connFile);
 	  
-	  if (0 == sh_mail_wait(250, fd)) 
+	  if (0 == sh_mail_wait(250, connFile)) 
 	    {
 	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET, 
 			      _("RCPT TO failed"), _("sh_mail_start_conn"), 
-			      _("mail"), sh_string_str(ma_address->recipient));
+			      _("mail"), address_list[i]);
 
 	      TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
 	      ++ecount;
 	    }
-	  ma_address = ma_address->next;
-	}
-
-      *anum += address_num;
-
+	}
       if (ecount == address_num)
 	{
-	  (void) sl_fclose(FIL__, __LINE__, connFile);
+	  (void) fclose(connFile);
 	  SL_RETURN( NULL, _("sh_mail_start_conn"));
 	}
@@ -1233,5 +1534,5 @@
   /* Send the message 
    */
-  report_smtp(_("DATA"));
+  TPT(( 0, FIL__, __LINE__,  _("msg=<DATA>%c%c"), 13, 10)); 
 
   (void) fflush(connFile);
@@ -1239,5 +1540,5 @@
   (void) fflush(connFile);
 
-  if (0 == sh_mail_wait(354, fd)) 
+  if (0 == sh_mail_wait(354, connFile)) 
     {
       sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET, 
@@ -1245,5 +1546,5 @@
 		      _("mail"), address);
       TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
-      (void) sl_fclose(FIL__, __LINE__, connFile);
+      (void) fclose(connFile);
       SL_RETURN( NULL, _("sh_mail_start_conn"));
     }
@@ -1251,27 +1552,9 @@
 
   my_time = time(NULL);
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
-  my_tm = localtime_r(&my_time, &time_tm);
-#else
-  my_tm = localtime(&my_time);
-#endif
-  
-  if (!my_tm) 
-    {
-      sl_strlcpy(my_tbuf, _("Thu, 01 Jan 1970 00:00:00 +00:00"), sizeof(my_tbuf));
-    }
-  else
-    {
-#if defined(HAVE_STRFTIME_Z)
-      (void) strftime(my_tbuf, 127, _("%a, %d %b %Y %H:%M:%S %z"), my_tm);
-#else
-      (void) strftime(my_tbuf, 127, _("%a, %d %b %Y %H:%M:%S %Z"), my_tm);
-#endif
-    }
+  my_tm   = localtime(&my_time);
+  (void)    strftime(my_tbuf, 127, _("%a, %d %b %Y %H:%M:%S %Z"), my_tm);
 
   TPT(( 0, FIL__, __LINE__,  _("msg=<From: <%s>%c%cTo: <%s>%c%cDate: %s>%c%c"),
 	this_address, 13, 10, address, 13, 10, my_tbuf, 13, 10));
-
-  report_smtp(_("sending data.."));
 
   (void) fflush(connFile);
@@ -1284,5 +1567,4 @@
 	  my_tbuf, 13, 10);
 
-  *ma_socket = fd;
   SL_RETURN( connFile, _("sh_mail_start_conn"));
 }
@@ -1294,11 +1576,9 @@
  */
 
-static int sh_mail_end_conn (FILE * connFile, int fd)
+static int sh_mail_end_conn (FILE * connFile)
 {
   SL_ENTER(_("sh_mail_end_conn"));
 
   time_wait = 300;
-
-  report_smtp(_("."));
 
   (void) fflush(connFile);
@@ -1306,5 +1586,7 @@
   (void) fflush(connFile);
 
-  if (0 != sh_mail_wait(250, fd))
+  TPT(( 0, FIL__, __LINE__, _("msg=<message end written>\n")));
+
+  if (0 != sh_mail_wait(250, connFile))
     {  
       (void) fflush(connFile);
@@ -1333,32 +1615,21 @@
  *
  */
-extern int flag_err_debug;
-
-static void report_smtp (char * reply)
-{
-  char * tmp;
-
-  if (flag_err_debug == S_TRUE)
-    {
-      tmp = sh_util_safe_name_keepspace(reply);
-
-      sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		       tmp,
-		       _("report_smtp") );
-      SH_FREE(tmp);
-    }
-  return;
-}
-
-
-static int sh_mail_wait(int code, int ma_socket)
+
+static jmp_buf wait_timeout;
+
+static void sh_mail_alarmhandle (int mysignal)
+{
+  /*@-noeffect@*/
+  (void) mysignal; /* avoid compiler warning */
+  /*@+noeffect@*/
+
+  longjmp(wait_timeout, 1);
+}
+
+static int sh_mail_wait(int code, FILE * ma_socket)
 {
   int rcode, g;
 
-  char c;
-
-  char errmsg[194];
-  char reply[128];
-  unsigned int  ireply = 0;
+  char errmsg[128];
 
   enum { 
@@ -1371,36 +1642,53 @@
   time_t waited_time;
 
+  struct   sigaction          old_act;
+  volatile unsigned int       old_alarm = 0;
+
+  struct  sigaction  new_act;
+  sigset_t           unblock;
+
+  (void) sigemptyset(&unblock);
+  (void) sigaddset  (&unblock, SIGALRM);
+
+  new_act.sa_handler = sh_mail_alarmhandle;
+  (void) sigemptyset( &new_act.sa_mask );       /* set an empty mask       */
+  new_act.sa_flags = 0;                         /* init sa_flags           */
+
   SL_ENTER(_("mail_wait"));
   
+  /* alarm was triggered
+   */
+  if (setjmp(wait_timeout) != 0)
+    {
+      (void) alarm(0);
+      (void) sigaction (SIGALRM, &old_act, NULL);
+      (void) alarm(old_alarm);
+      (void) sigprocmask(SIG_UNBLOCK, &unblock, NULL);
+      TPT((0, FIL__, __LINE__, _("msg=<mail_wait: timeout>\n"))); 
+      SL_RETURN( 0, _("mail_wait"));
+    }
+
   waited_time = time(NULL);
 
   /* timeout after 5 minutes
    */
-
-  rcode    = 0;
-  state    = WAIT_CODE_START;
-  reply[0] = '\0';
-
-  while (sl_read_timeout_fd (ma_socket, &c, 1, time_wait, S_FALSE) > 0) {
-
-    if (ireply < (sizeof(reply) - 1))
+  old_alarm = alarm(0);
+  (void) sigaction (SIGALRM, &new_act, &old_act);
+  (void) alarm((unsigned int) time_wait);
+
+  rcode = 0;
+  state = WAIT_CODE_START;
+
+  while (0 == feof(ma_socket) && 0 == ferror(ma_socket)) {
+
+    if ( (g=fgetc(ma_socket)) == EOF)
       {
-	if (c != '\n' && c != '\r')
-	  {
-	    reply[ireply] = c;
-	    ++ireply;
-	    reply[ireply] = '\0';
-	  }
-      }
-
-    g = (int) c;
-
-    /*
-    if (g == EOF)
-      {
+	(void) alarm(0);
+	(void) sigaction (SIGALRM, &old_act, NULL); 
+	(void) alarm(old_alarm);
+	(void) sigprocmask(SIG_UNBLOCK, &unblock, NULL);
 	TPT((0, FIL__, __LINE__, _("msg=<mail_wait: EOF>\n"))); 
 	SL_RETURN( 0, _("mail_wait")); 
       }
-    */
 
     switch(state) {
@@ -1411,9 +1699,6 @@
       if (0 != isspace(g))
 	break;             /* Skip white space                    */
-      if (0 == isdigit(g)) 
-	{
-	  report_smtp(reply);
-	  SL_RETURN( 0, _("mail_wait")); /* No leading number     */
-	}
+      if (0 == isdigit(g))
+	return 0;          /* No leading number                   */
       rcode = g-(int)'0';  /* convert to number                   */
       state = WAIT_CODE;
@@ -1439,4 +1724,8 @@
 	break;
       /*@-charintliteral@*/
+      (void) alarm(0);
+      (void) sigaction (SIGALRM, &old_act, NULL); 
+      (void) alarm(old_alarm);
+      (void) sigprocmask(SIG_UNBLOCK, &unblock, NULL);
 
       TPT((0, FIL__, __LINE__, 
@@ -1446,16 +1735,11 @@
       if (g != 1)
 	{
-	  char * tmp = sh_util_safe_name_keepspace(reply);
-          sl_snprintf(errmsg, sizeof(errmsg),
-		      _("Bad response (%s), expected %d"), tmp, code);
-	  SH_FREE(tmp);
-
+	  /*@-bufferoverflowhigh@*/
+          sprintf(errmsg,                              /* known to fit  */
+		  _("Bad response (%d), expected %d"), rcode, code);
+	  /*@+bufferoverflowhigh@*/
 	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_NET, 
 			  errmsg, _("sh_mail_wait"), 
 			  _("mail"), _("SMTP server"));
-	}
-      else
-	{
-	  report_smtp(reply);
 	}
       waited_time = time(NULL) - waited_time;
@@ -1477,7 +1761,10 @@
       
     default:
+      (void) alarm(0);
+      (void) sigaction (SIGALRM, &old_act, NULL); 
+      (void) alarm(old_alarm);
+      (void) sigprocmask(SIG_UNBLOCK, &unblock, NULL);
 
       TPT((0, FIL__, __LINE__, _("msg=<mail_wait: bad>\n"))); 
-      report_smtp(reply);
       SL_RETURN( 0, _("mail_wait")); 
       
@@ -1485,8 +1772,12 @@
   }
 
+  (void) alarm(0);                            /* Disable alarm       */
+  (void) sigaction (SIGALRM, &old_act, NULL); 
+  (void) alarm(old_alarm);
+  (void) sigprocmask(SIG_UNBLOCK, &unblock, NULL);
+
   TPT((0, FIL__, __LINE__, _("msg=<mail_wait: failed>\n"))); 
 
   /* Failed, EOF or error on socket */
-  report_smtp(reply);
   SL_RETURN( 0, _("mail_wait")); 
 }
@@ -1497,5 +1788,5 @@
 #define SPLIT_AT 998
 
-static char * split_string(const char * str)
+static char * split_string(char * str)
 {
   size_t size;
@@ -1504,26 +1795,17 @@
 
   char * p, * p0;
-  const char * q;
+  char * q;
 
   if (!str)
     return NULL;
 
-  size   = strlen(str) + 1;
+  size   = strlen(str);
+  /* fprintf(stderr, "orig = %d\n", size); */
   blocks = 1 + (size / SPLIT_AT);
   
-  if (sl_ok_muls(2, blocks) && sl_ok_adds(size, (2*blocks)))
-    {
-      size   = size + (2*blocks);
-    }
-  else
-    {
-      /* integer overflow, do not split */
-      p = sh_util_strdup(str);
-      return p;
-    }
-
+  size   = size + (2*blocks) + 1;
   p = SH_ALLOC(size);
   memset(p, 0, size);
-
+  /* fprintf(stderr, "alloc = %d\n", size); */
   p0 = p;
 
@@ -1601,5 +1883,4 @@
   int  ret, length, status;
   mx * result;
-  size_t len;
 
   typedef union
@@ -1609,10 +1890,9 @@
   } querybuf;
 
-  querybuf * reply;
+  querybuf reply;
   char expanded[1024];
   unsigned char * comp_dn, * eom;
   HEADER * header;
-  int      type, rdlength, pref;
-  unsigned int count, theindex;
+  int      count, index, type, rdlength, pref;
   dnsrep * retval;
 
@@ -1622,14 +1902,9 @@
     SL_RETURN (NULL, _("get_mx"));
 
-  reply = SH_ALLOC(sizeof(querybuf));
-
   errno = 0;
   length = res_query (hostname, C_IN, T_MX, 
-		      (unsigned char *) reply, 4095);
-
+		      (unsigned char *) &reply, 4095);
   if (length < 1)
     {
-      char errbuf[SH_ERRBUF_SIZE];
-
       /* error handling
        */
@@ -1644,28 +1919,27 @@
 	  sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, status, MSG_E_SUBGEN,
 			   (errno == ECONNREFUSED) ? 
-			   sh_error_message (status, errbuf, sizeof(errbuf)) : 
-			   sh_tools_errmessage(status, errbuf, sizeof(errbuf)),
+			   sh_error_message (status) : 
+			   sh_tools_errmessage(status),
 			   _("res_query"));
 #else
 	  if (errno == ECONNREFUSED)
-	    fprintf(stderr, " ERROR: %s: \n", strerror(errno)); /* TESTONLY */
+	    fprintf(stderr, " ERROR: %s: \n", strerror(errno));
 	  else
-	    fprintf(stderr, "HERROR: %s\n", hstrerror(h_errno));/* TESTONLY */
+	    fprintf(stderr, "HERROR: %s\n", hstrerror(h_errno));
 #endif
 	}
-      SH_FREE(reply);
       SL_RETURN (NULL, _("get_mx"));
     }
 
-
-  header  = (HEADER *) reply;
+  ret = 0;
+  header  = (HEADER *) &reply;
 
   /* start of data section
    */
-  comp_dn = (unsigned char *) reply + HFIXEDSZ;
+  comp_dn = (unsigned char *) &reply + HFIXEDSZ;
 
   /* end-of-message
    */
-  eom     = (unsigned char *) reply + length;
+  eom     = (unsigned char *) &reply + length;
 
   /* HEADER NAME  -- must be skipped or decompressed
@@ -1683,48 +1957,28 @@
    */
   count = ntohs (header->qdcount); 
-  for (theindex = 0; theindex < count; ++theindex)
+  for (index = 0; index < count; ++index)
     {
       ret = dn_skipname (comp_dn, eom);
       comp_dn += ret + QFIXEDSZ;
       if (ret < 1 || comp_dn >= eom)
-	{
-	  SH_FREE(reply);
-	  SL_RETURN (NULL, _("get_mx"));
-	}
-    }
-
+	SL_RETURN (NULL, _("get_mx"));
+    }
   count         = ntohs (header->ancount);
   if (count < 1)
-    {
-      SH_FREE(reply);
-      SL_RETURN (NULL, _("get_mx"));
-    }
+    SL_RETURN (NULL, _("get_mx"));
 
   retval        = SH_ALLOC (sizeof (dnsrep));
   if (!retval)
-    {
-      SH_FREE(reply);
-      SL_RETURN (NULL, _("get_mx"));
-    }
-
+    SL_RETURN (NULL, _("get_mx"));
   retval->count = count;
 
   /* allocate space for the results */
 
-  if (!sl_ok_muls(count, sizeof (mx)))
-    {
-      SH_FREE(reply);
+  result        = SH_ALLOC (count * sizeof (mx));
+  if (!result)
+    {
       SH_FREE   (retval);
       SL_RETURN (NULL, _("get_mx"));
     }
-
-  result        = SH_ALLOC (count * sizeof (mx));
-  
-  if (!result)
-    {
-      SH_FREE(reply);
-      SH_FREE   (retval);
-      SL_RETURN (NULL, _("get_mx"));
-    }
   retval->reply = result;
 
@@ -1733,10 +1987,9 @@
       /* HEADER NAME 
        */
-      ret = dn_expand ((unsigned char *) reply, eom, comp_dn, 
+      ret = dn_expand ((unsigned char *) &reply, eom, comp_dn, 
 		       (char *) expanded, 1023);
       comp_dn += ret;
       if (ret < 1 || comp_dn >= eom)
 	{
-	  SH_FREE(reply);
 	  SH_FREE (result);
 	  SH_FREE (retval);
@@ -1750,5 +2003,4 @@
       if (type != T_MX || comp_dn >= eom)
 	{
-	  SH_FREE(reply);
 	  SH_FREE (result);
 	  SH_FREE (retval);
@@ -1756,17 +2008,14 @@
 	}
 
-
       /* CLASS (re-use 'type' var)
        */
-      /* type = get_short (comp_dn); *//* don't care */
+      type = get_short (comp_dn);
       comp_dn += 2;
       if (comp_dn >= eom)
 	{
-	  SH_FREE(reply);
 	  SH_FREE (result);
 	  SH_FREE (retval);
 	  SL_RETURN (NULL, _("get_mx"));
 	}
-
 
       /* TTL
@@ -1775,5 +2024,4 @@
       if (comp_dn >= eom)
 	{
-	  SH_FREE(reply);
 	  SH_FREE (result);
 	  SH_FREE (retval);
@@ -1787,5 +2035,4 @@
       if (rdlength < 1 || comp_dn >= eom)
 	{
-	  SH_FREE(reply);
 	  SH_FREE (result);
 	  SH_FREE (retval);
@@ -1799,5 +2046,4 @@
       if (comp_dn >= eom)
 	{
-	  SH_FREE(reply);
 	  SH_FREE (result);
 	  SH_FREE (retval);
@@ -1805,10 +2051,9 @@
 	}
 
-      ret = dn_expand ((unsigned char *) reply, eom, comp_dn, 
+      ret = dn_expand ((unsigned char *) &reply, eom, comp_dn, 
 		       (char *) expanded, 1023);
       comp_dn += ret;
       if (ret < 1)
 	{
-	  SH_FREE(reply);
 	  SH_FREE (result);
 	  SH_FREE (retval);
@@ -1820,11 +2065,9 @@
        */
       result[count].pref = pref;
-      len = strlen (expanded) + 1;
-      result[count].address = SH_ALLOC (len);
-      sl_strlcpy (result[count].address, expanded, len);
+      result[count].address = SH_ALLOC (strlen (expanded) + 1);
+      strcpy (result[count].address, expanded);        /* known to fit  */
     }
   while (ret > 0 && comp_dn < eom && count);
 
-  SH_FREE(reply);
   SL_RETURN (retval, _("get_mx"));
 }
@@ -1854,9 +2097,9 @@
 static dnsrep * return_mx (char *domain)
 {
+  struct hostent *host = NULL;
   dnsrep * answers = NULL;
   mx     * result;
   dnsrep * retval;
-  char   * address = NULL;
-  char     errmsg[128];
+  char     errmsg[128]; 
 
   SL_ENTER(_("return_mx"));
@@ -1875,6 +2118,4 @@
   else
     {
-      char numeric[SH_IP_BUF];
-
       if (domain != NULL)
 	{
@@ -1887,5 +2128,4 @@
 			   _("get_mx"));
 #else
-	  /* flawfinder: ignore *//* test code only */
 	  strcpy  (errmsg,                               /* known to fit  */
 		   _("No MX record for domain "));
@@ -1896,21 +2136,7 @@
 #endif
 	}
-
-      retval = NULL;
-
       if (domain != NULL)
-	address = sh_ipvx_canonical(domain, numeric, sizeof(numeric));
-
-      if (address)
-	{
-	  result       = SH_ALLOC (sizeof (mx));
-	  retval       = SH_ALLOC (sizeof (dnsrep));
-	  retval->reply = result;
-	  retval->count = 1;
-	  result->pref  = 0;
-
-	  result->address = address;
-	}
-      else
+	host = /*@-unrecog@*/sh_gethostbyname (domain)/*@+unrecog@*/;
+      if (!host)
 	{
 #ifdef FIL__
@@ -1923,10 +2149,18 @@
 	  SL_RETURN (NULL, _("return_mx"));
 	}
-
+      result       = SH_ALLOC (sizeof (mx));
+      retval       = SH_ALLOC (sizeof (dnsrep));
+      retval->reply = result;
+      retval->count = 1;
+      result->pref  = 0;
+      /*@-type@*/
+      result->address = SH_ALLOC (strlen (host->h_name) + 1);
+      strcpy (result->address, host->h_name);          /* known to fit  */
+      /*@+type@*/
       SL_RETURN (retval, _("return_mx"));
     }
 }
 
-int free_mx (dnsrep * answers)
+static int free_mx (dnsrep * answers)
 {
   mx     * result;
Index: trunk/src/sh_mem.c
===================================================================
--- trunk/src/sh_mem.c	(revision 591)
+++ trunk/src/sh_mem.c	(revision 1)
@@ -20,7 +20,4 @@
 #include "config_xor.h"
 
-#if defined(HAVE_PTHREAD_MUTEX_RECURSIVE)
-#define _XOPEN_SOURCE 500
-#endif
 
 #include <stdio.h>
@@ -35,16 +32,14 @@
 #endif
 
-#define SH_REAL_SET
-
 #include "samhain.h"
 #include "sh_error.h"
 #include "sh_utils.h"
 #include "sh_mem.h"
-#include "sh_pthread.h"
-
-extern int safe_logger (int thesignal, int method, char * details);
+
+extern int safe_logger (int signal, int method, pid_t thepid);
 
 #undef  FIL__
 #define FIL__  _("sh_mem.c")
+static int eblock = 0;
 
 #ifdef MEM_DEBUG
@@ -73,41 +68,51 @@
 unsigned long Mem_Current = 0, Mem_Max = 0;
 
-#ifdef HAVE_PTHREAD
-SH_MUTEX_RECURSIVE(mutex_mem);
-#endif
-
-/* define MEM_LOG to enable this */
-/* #define MEM_LOG 1 */
+#if 0
+#define MEM_DETAILS 
+#endif
+
+#ifdef MEM_DETAILS
+int           max_upto_032 = 0;
+int           max_upto_064 = 0;
+int           max_upto_128 = 0;
+int           max_upto_256 = 0;
+int           max_upto_512 = 0;
+int           max_upto_1024 = 0;
+int           max_upto_4096 = 0;
+int           max_upto_inf = 0;
+
+int           now_upto_032 = 0;
+int           now_upto_064 = 0;
+int           now_upto_128 = 0;
+int           now_upto_256 = 0;
+int           now_upto_512 = 0;
+int           now_upto_1024 = 0;
+int           now_upto_4096 = 0;
+int           now_upto_inf = 0;
+
+int           tot_upto_032 = 0;
+int           tot_upto_064 = 0;
+int           tot_upto_128 = 0;
+int           tot_upto_256 = 0;
+int           tot_upto_512 = 0;
+int           tot_upto_1024 = 0;
+int           tot_upto_4096 = 0;
+int           tot_upto_inf = 0;
+#endif
+
 #ifdef MEM_LOG
 void sh_mem_dump ()
 {
   memlist_t   * this = memlist;
-  FILE * fd;
-  static unsigned int nr = 0;
-  char filename[256];
-
-  snprintf(filename, sizeof(filename), "sh_mem_dump.%04u.%lu", nr, (unsigned long) sh.pid);
-  
-  SH_MUTEX_RECURSIVE_INIT(mutex_mem);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_mem);
-
-  fd = fopen(filename, "w");
-  if (!fd)
-    {
-      perror(filename);
-      _exit(EXIT_FAILURE);
-    }
+
+
+  FILE * fd = fopen(MEM_LOG, "w");
 
   while (this != NULL)
     {
-      fprintf (fd, "## %20s %5d %ld\n",  this->file, this->line, this->size);
-      fprintf (fd, "%10p %8ld\n", (void *)this->address, this->size);
+      fprintf (fd, "%20s %5d %ld\n",  this->file, this->line, this->size);
       this = this->next;
     }
-  sl_fclose(FIL__, __LINE__, fd);
-
-  SH_MUTEX_RECURSIVE_UNLOCK(mutex_mem);
-  ++nr;
-  /* _exit(EXIT_SUCCESS); */
+  fclose(fd);
   return;
 }
@@ -119,14 +124,10 @@
 #endif
 
-memlist_t ** sh_dummy_114_merrlist;
-
 void sh_mem_stat ()
 {
   memlist_t   * this;
-  memlist_t   * merrlist = NULL;
+
 
   SL_ENTER(_("sh_mem_stat"));
-
-  sh_dummy_114_merrlist = (memlist_t **) &merrlist;
 
   if (Alloc_Count == Free_Count) 
@@ -142,6 +143,17 @@
 		   Mem_Max, Mem_Current);
 
-  SH_MUTEX_RECURSIVE_INIT(mutex_mem);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_mem);
+#ifdef MEM_DETAILS
+  fprintf(stderr, "\n");
+  fprintf(stderr, "__SIZE_____TOTAL___MAXIMUM___\n");
+  fprintf(stderr, "    32    %6d    %6d\n", tot_upto_032, max_upto_032);
+  fprintf(stderr, "    64    %6d    %6d\n", tot_upto_064, max_upto_064);
+  fprintf(stderr, "   128    %6d    %6d\n", tot_upto_128, max_upto_128);
+  fprintf(stderr, "   256    %6d    %6d\n", tot_upto_256, max_upto_256);
+  fprintf(stderr, "   512    %6d    %6d\n", tot_upto_512, max_upto_512);
+  fprintf(stderr, "  1024    %6d    %6d\n", tot_upto_1024, max_upto_1024);
+  fprintf(stderr, "  4096    %6d    %6d\n", tot_upto_4096, max_upto_4096);
+  fprintf(stderr, "   inf    %6d    %6d\n", tot_upto_inf, max_upto_inf);
+  fprintf(stderr, "\n");
+#endif
 
   this = memlist;
@@ -149,46 +161,22 @@
   while (this != NULL) 
     {
-      memlist_t   * merr = calloc(1,sizeof(memlist_t));
-
-      memcpy(merr, this, sizeof(memlist_t));
-      merr->next = merrlist;
-      merrlist   = merr;
-
+      sh_error_handle (SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_NOTFREE,
+		       this->size, this->file, this->line);
       this = this->next;
     }
 
-  SH_MUTEX_RECURSIVE_UNLOCK(mutex_mem);
-
-  while (merrlist != NULL) 
-    {
-      memlist_t   * tmp = merrlist;
-      merrlist = merrlist->next;
-      
-      sh_error_handle (SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_NOTFREE,
-		       tmp->size, tmp->file, tmp->line);
-      free(tmp);
-    }
-
   SL_RET0(_("sh_mem_stat"));
 }
 
-memlist_t ** sh_dummy_168_merrlist;
-
 void sh_mem_check ()
 {
   memlist_t * this;
-  memlist_t * merrlist = NULL;
-  memlist_t * merr;
+  long        nerr = 0;
 
   SL_ENTER(_("sh_mem_check"));
-
-  sh_dummy_168_merrlist = (memlist_t **) &merrlist;
 
   sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MSTAMP,
 		   Mem_Max, Mem_Current);
 
-  SH_MUTEX_RECURSIVE_INIT(mutex_mem);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_mem);
-
   this = memlist;
 
@@ -197,11 +185,6 @@
       if ( this->address == NULL )
 	{
-	  merr = calloc (1,sizeof(memlist_t));
-
-	  memcpy(merr, this, sizeof(memlist_t));
-	  merr->size = 2;
-
-	  merr->next = merrlist;
-	  merrlist   = merr;
+	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MNULL);
+	  ++nerr;
 	}
       else
@@ -209,21 +192,13 @@
 	  if ( this->address[this->size]        != CHECKBYTE )
 	    {
-	      merr = calloc(1, sizeof(memlist_t));
-	      
-	      memcpy(merr, this, sizeof(memlist_t));
-	      merr->size = 1;
-	      
-	      merr->next = merrlist;
-	      merrlist   = merr;
+	      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MOVER,
+			       this->file, this->line, FIL__, __LINE__);
+	      ++nerr;
 	    }
 	  if ( this->real_address[SH_MEMMULT-1] != CHECKBYTE )
 	    {
-	      merr = calloc(1, sizeof(memlist_t));
-	      
-	      memcpy(merr, this, sizeof(memlist_t));
-	      merr->size = 0;
-	      
-	      merr->next = merrlist;
-	      merrlist   = merr;
+	      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MUNDER,
+			       this->file, this->line, FIL__, __LINE__);
+	      ++nerr;
 	    }
 	}
@@ -231,25 +206,5 @@
     }
 
-
-  SH_MUTEX_RECURSIVE_UNLOCK(mutex_mem);
-
-  while (merrlist != NULL) 
-    {
-      memlist_t   * tmp = merrlist;
-      merrlist = merrlist->next;
-      
-      if (tmp->size == 2)
-	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MNULL,
-			   tmp->file, tmp->line, FIL__, __LINE__);
-      if (tmp->size == 1)
-	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MOVER,
-			   tmp->file, tmp->line, FIL__, __LINE__);
-      else
-	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MUNDER,
-			   tmp->file, tmp->line, FIL__, __LINE__);
-      free(tmp);
-      SH_ABORT;
-      _exit (EXIT_FAILURE);
-    }
+  /* if (nerr > 0) abort(); */
 
   SL_RET0(_("sh_mem_check"));
@@ -264,16 +219,21 @@
   SL_ENTER(_("sh_mem_malloc"));
 
-  SH_MUTEX_RECURSIVE_INIT(mutex_mem);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_mem);
-
-  the_realAddress = calloc(1,size + 2 * SH_MEMMULT);
+  the_realAddress = malloc(size + 2 * SH_MEMMULT);
   
   if ( the_realAddress  == NULL ) 
     {
-      (void) safe_logger (0, 0, NULL);
-
+      if (eblock == 0)
+	{
+	  eblock = 1;
+	  (void) safe_logger (0, 0, getpid());
+	  /*
+	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MMEM,
+			   file, line);
+	  */
+	  eblock = 0;
+	}
       /* use _exit() rather than exit() - we malloc() in atexit() functions 
        */
-      _exit (EXIT_FAILURE);
+      _exit (42);
     }
   
@@ -292,68 +252,110 @@
     Max_Alloc_Count = Now_Alloc_Count;
 
+#ifdef MEM_DETAILS
+  if (size <= 32)
+    { 
+      ++now_upto_032;
+      ++tot_upto_032;
+      if (now_upto_032 > max_upto_032) max_upto_032 = now_upto_032;
+    }
+  else if  (size <= 64)
+    { 
+      ++now_upto_064;
+      ++tot_upto_064;
+      if (now_upto_064 > max_upto_064) max_upto_064 = now_upto_064;
+    }
+  else if  (size <= 128)
+    { 
+      ++now_upto_128;
+      ++tot_upto_128;
+      if (now_upto_128 > max_upto_128) max_upto_128 = now_upto_128;
+    }
+  else if  (size <= 256)
+    { 
+      ++now_upto_256;
+      ++tot_upto_256;
+      if (now_upto_256 > max_upto_256) max_upto_256 = now_upto_256;
+    }
+  else if  (size <= 512)
+    { 
+      ++now_upto_512;
+      ++tot_upto_512;
+      if (now_upto_512 > max_upto_512) max_upto_512 = now_upto_512;
+    }
+  else if  (size <= 1024)
+    { 
+      ++now_upto_1024;
+      ++tot_upto_1024;
+      if (now_upto_1024 > max_upto_1024) max_upto_1024 = now_upto_1024;
+    }
+  else if  (size <= 4096)
+    { 
+      ++now_upto_4096;
+      ++tot_upto_4096;
+      if (now_upto_4096 > max_upto_4096) max_upto_4096 = now_upto_4096;
+    }
+  else 
+    { 
+      ++now_upto_inf;
+      ++tot_upto_inf;
+      if (now_upto_inf > max_upto_inf) max_upto_inf = now_upto_inf;
+
+      fprintf(stderr, "\n___BIGSIZE___");
+      fprintf(stderr, "   %6d  -> %16s  %10d \n", size, file, line);
+      fprintf(stderr, "\n");
+
+    }
+#endif
+
   Mem_Current += size;
   Mem_Max = ( (Mem_Current > Mem_Max) ? Mem_Current : Mem_Max);
 
-  this = calloc(1,sizeof(memlist_t));
+  this = (memlist_t *) malloc (sizeof(memlist_t));
 
   if ( this == NULL) 
     {
-      (void) safe_logger(0, 0, NULL);
-
-      _exit(EXIT_FAILURE);
-    }
-  else
-    {
-      /* make list entry */
-
-      this->real_address = the_realAddress;
-      this->address      = theAddress;
-      this->size         = size;
-      this->line         = line;
-      sl_strlcpy(this->file, file, 20);
-
-      this->next = memlist;
-      memlist = this;
-    }
-
-  SH_MUTEX_RECURSIVE_UNLOCK(mutex_mem);
+      if (eblock == 0)
+	{
+	  eblock = 1;
+	  (void) safe_logger(0, 0, getpid());
+	  /*
+	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MMEM,
+			   file, line);
+	  */
+	  eblock = 0;
+	}
+      SL_RETURN( the_realAddress, _("sh_mem_malloc"));
+    }
+
+  /* make list entry */
+
+  this->real_address = the_realAddress;
+  this->address      = theAddress;
+  this->size         = size;
+  this->line         = line;
+  sl_strlcpy(this->file, file, 20);
+
+  this->next = memlist;
+  memlist = this;
+
   SL_RETURN( theAddress, _("sh_mem_malloc"));
 }
 
-void ** sh_mem_dummy_a;
-memlist_t ** sh_mem_merr_3;
-
-void sh_mem_free (void * aa, char * file, int line)
-{
-  memlist_t * this;
-  memlist_t * before;
-  memlist_t * merr;
-  memlist_t * merrlist = NULL;
-  unsigned long        size   = 0;
-  void      * a;
-  volatile int         flag = 0;
+
+void sh_mem_free (void * a, char * file, int line)
+{
+  memlist_t * this   = memlist;
+  memlist_t * before = memlist;
+  unsigned long size = 0;
 
   SL_ENTER(_("sh_mem_free"));
 
-  a      = aa;
-  sh_mem_dummy_a = &a;
-  sh_mem_merr_3  = (memlist_t **) &merrlist;
-
-
   if ( a == NULL ) 
     {
       sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MNULL,
-		       file, line, FIL__, __LINE__);
-      SH_ABORT;
-      _exit (EXIT_FAILURE);
+		       file, line);
       SL_RET0(_("sh_mem_free"));
     }
     
-  SH_MUTEX_RECURSIVE_INIT(mutex_mem);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_mem);
-
-  this   = memlist;
-  before = memlist;
-  
   /* -- Find record. -- 
    */
@@ -368,6 +370,7 @@
   if (this == NULL) 
     {
-      flag = 1;
-      goto out;
+      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MREC,
+		       file, line);
+      SL_RET0(_("sh_mem_free"));
     } 
   else 
@@ -377,23 +380,10 @@
       if ( this->address[this->size]        != CHECKBYTE )
 	{
-	  merr = calloc(1, sizeof(memlist_t));
-
-	  memcpy(merr, this, sizeof(memlist_t));
-	  merr->size = 1;
-
-	  merr->next = merrlist;
-	  merrlist = merr;
-	}
-
+	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MOVER,
+			   this->file, this->line, file, line);
+	}
       if ( this->real_address[SH_MEMMULT-1] != CHECKBYTE )
-	{
-	  merr = calloc(1,sizeof(memlist_t));
-
-	  memcpy(merr, this, sizeof(memlist_t));
-	  merr->size = 0;
-
-	  merr->next = merrlist;
-	  merrlist = merr;
-	}
+	sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MUNDER,
+			 this->file, this->line, file, line);
 
       size = this->size;
@@ -408,33 +398,27 @@
   if (this)
     free(this);
-
   ++Free_Count;
   --Now_Alloc_Count;
 
+#ifdef MEM_DETAILS
+  if (size <= 32)
+    --now_upto_032;
+  else if  (size <= 64)
+    --now_upto_064;
+  else if  (size <= 128)
+    --now_upto_128;
+  else if  (size <= 256)
+    --now_upto_256;
+  else if  (size <= 512)
+    --now_upto_512;
+  else if  (size <= 1024)
+    --now_upto_1024;
+  else if  (size <= 4096)
+    --now_upto_4096;
+  else 
+    --now_upto_inf;
+#endif
+
   Mem_Current -= size;
- out:
-  ; /* label at end of compound statement */
-  SH_MUTEX_RECURSIVE_UNLOCK(mutex_mem);
-
-  while (merrlist != NULL) 
-    {
-      memlist_t   * tmp = merrlist;
-      merrlist = merrlist->next;
-      
-      if (tmp->size == 1)
-	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MOVER,
-			   tmp->file, tmp->line, file, line);
-      else
-	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MUNDER,
-			   tmp->file, tmp->line, file, line);
-      free(tmp);
-      SH_ABORT;
-      _exit (EXIT_FAILURE);
-    }
-
-  if (flag != 0)
-    sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MREC,
-		     file, line);
-
   SL_RET0(_("sh_mem_free"));
 }
@@ -453,5 +437,4 @@
     {
       sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MNULL);
-      SH_ABORT;
     }
   SL_RET0(_("sh_mem_free"));
@@ -464,19 +447,24 @@
   SL_ENTER(_("sh_mem_malloc"));
 
-  theAddress = calloc(1,size);
-
-  if ( theAddress != NULL ) 
-    {
-      SL_RETURN( theAddress, _("sh_mem_malloc"));
-    }
-  else
-    {
-      (void) safe_logger(0, 0, NULL);
-
+  theAddress = malloc(size);
+
+  if ( theAddress == NULL ) 
+    {
+      if (eblock == 0)
+	{
+	  eblock = 1;
+	  (void) safe_logger(0, 0, getpid());
+	  /*
+	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MMEM);
+	  */
+	  eblock = 0;
+	}
       /* use _exit() rather than exit() - we malloc() in atexit()  
        */
-      SH_ABORT;
-      _exit (EXIT_FAILURE);
-    }
-}
-#endif
+      _exit (42);
+    }
+  /* memset (theAddress, 0, 1); *//* needs testing */
+
+  SL_RETURN( theAddress, _("sh_mem_malloc"));
+}
+#endif
Index: trunk/src/sh_modules.c
===================================================================
--- trunk/src/sh_modules.c	(revision 591)
+++ trunk/src/sh_modules.c	(revision 1)
@@ -7,15 +7,10 @@
 
 #include "sh_modules.h"
-#include "sh_pthread.h"
 
 #include "sh_utmp.h"
 #include "sh_mounts.h"
 #include "sh_userfiles.h"
+#include "sh_kern.h"
 #include "sh_suidchk.h"
-#include "sh_processcheck.h"
-#include "sh_portcheck.h"
-#include "sh_logmon.h"
-#include "sh_registry.h"
-#include "sh_fInotify.h"
 
 sh_mtype modList[] = {
@@ -23,15 +18,13 @@
   {
     N_("UTMP"),
-    -1,
-    SH_MODFL_NOTIMER,
+    0,
     sh_utmp_init,
     sh_utmp_timer,
     sh_utmp_check,
     sh_utmp_end,
-    sh_utmp_reconf,
+    sh_utmp_null,
 
     N_("[Utmp]"),
     sh_utmp_table,
-    PTHREAD_MUTEX_INITIALIZER,
   },
 #endif
@@ -40,5 +33,4 @@
   {
     N_("MOUNTS"),
-    -1,
     0,
     sh_mounts_init,
@@ -50,5 +42,4 @@
     N_("[Mounts]"),
     sh_mounts_table,
-    PTHREAD_MUTEX_INITIALIZER,
   },
 #endif
@@ -57,5 +48,4 @@
   {
     N_("USERFILES"),
-    -1,
     0,
     sh_userfiles_init,
@@ -67,12 +57,24 @@
     N_("[UserFiles]"),
     sh_userfiles_table,
-    PTHREAD_MUTEX_INITIALIZER,
   },
 #endif
 
+#ifdef SH_USE_KERN
+  {
+    N_("KERNEL"),
+    0,
+    sh_kern_init,
+    sh_kern_timer,
+    sh_kern_check,
+    sh_kern_end,
+    sh_kern_null,
+
+    N_("[Kernel]"),
+    sh_kern_table,
+  },
+#endif
 #ifdef SH_USE_SUIDCHK
   {
     N_("SUIDCHECK"),
-    -1,
     0,
     sh_suidchk_init,
@@ -80,100 +82,12 @@
     sh_suidchk_check,
     sh_suidchk_end,
-    sh_suidchk_reconf,
+    sh_suidchk_free_schedule,
 
     N_("[SuidCheck]"),
     sh_suidchk_table,
-    PTHREAD_MUTEX_INITIALIZER,
   },
 #endif
-
-#ifdef SH_USE_PROCESSCHECK
-  {
-    N_("PROCESSCHECK"),
-    -1,
-    0,
-    sh_prochk_init,
-    sh_prochk_timer,
-    sh_prochk_check,
-    sh_prochk_cleanup,
-    sh_prochk_reconf,
-
-    N_("[ProcessCheck]"),
-    sh_prochk_table,
-    PTHREAD_MUTEX_INITIALIZER,
-  },
-#endif
-
-#ifdef SH_USE_PORTCHECK
-  {
-    N_("PORTCHECK"),
-    -1,
-    0,
-    sh_portchk_init,
-    sh_portchk_timer,
-    sh_portchk_check,
-    sh_portchk_cleanup,
-    sh_portchk_reconf,
-
-    N_("[PortCheck]"),
-    sh_portchk_table,
-    PTHREAD_MUTEX_INITIALIZER,
-  },
-#endif
-
-#ifdef USE_LOGFILE_MONITOR
-  {
-    N_("LOGMON"),
-    -1,
-    0,
-    sh_log_check_init,
-    sh_log_check_timer,
-    sh_log_check_check,
-    sh_log_check_cleanup,
-    sh_log_check_reconf,
-
-    N_("[Logmon]"),
-    sh_log_check_table,
-    PTHREAD_MUTEX_INITIALIZER,
-  },
-#endif
-
-#ifdef USE_REGISTRY_CHECK
-  {
-    N_("REGISTRY"),
-    -1,
-    0,
-    sh_reg_check_init,
-    sh_reg_check_timer,
-    sh_reg_check_run,
-    sh_reg_check_cleanup,
-    sh_reg_check_reconf,
-
-    N_("[Registry]"),
-    sh_reg_check_table,
-    PTHREAD_MUTEX_INITIALIZER,
-  },
-#endif
-
-#if defined(HAVE_SYS_INOTIFY_H)
-  {
-    N_("INOTIFY"),
-    -1,
-    SH_MODFL_NEEDPAUSED,
-    sh_fInotify_init,
-    sh_fInotify_timer,
-    sh_fInotify_run,
-    sh_fInotify_cleanup,
-    sh_fInotify_reconf,
-
-    N_("[Inotify]"),
-    sh_fInotify_table,
-    PTHREAD_MUTEX_INITIALIZER,
-  },
-#endif
-
   {
     NULL,
-    -1,
     0,
 
@@ -186,5 +100,4 @@
     NULL,
     NULL,
-    PTHREAD_MUTEX_INITIALIZER,
   },
 };
Index: trunk/src/sh_mounts.c
===================================================================
--- trunk/src/sh_mounts.c	(revision 591)
+++ trunk/src/sh_mounts.c	(revision 1)
@@ -51,9 +51,9 @@
 
 /* Prototypes for configuration functions */
-int sh_mounts_config_activate (const char * opt);
-int sh_mounts_config_timer    (const char * opt);
-int sh_mounts_config_mount    (const char * opt);
-int sh_mounts_config_sevmnt   (const char * opt);
-int sh_mounts_config_sevopt   (const char * opt);
+int sh_mounts_config_activate (char * opt);
+int sh_mounts_config_timer    (char * opt);
+int sh_mounts_config_mount    (char * opt);
+int sh_mounts_config_sevmnt   (char * opt);
+int sh_mounts_config_sevopt   (char * opt);
 
 /* Prototype for the function to read info on mounted filesystems */
@@ -157,6 +157,6 @@
 static int    ShMountsActive    = S_FALSE;
 static time_t ShMountsInterval  = 86400;
-static int    ShMountsSevMnt    = SH_ERR_ERR;
-static int    ShMountsSevOpt    = SH_ERR_ERR;
+static int    ShMountsSevMnt    = 7;
+static int    ShMountsSevOpt    = 7;
 
 static struct sh_mounts_mnt *mountlist = NULL;
@@ -165,7 +165,6 @@
  * This is called once at the start of each samhain run.
  * Non-configuration setup code should be placed here. */
-int sh_mounts_init (struct mod_type * arg)
-{
-  (void) arg;
+int sh_mounts_init ()
+{
   SL_ENTER(_("sh_mounts_init"));
 
@@ -180,4 +179,6 @@
   }
 
+  lastcheck = time(NULL);
+
   SL_RETURN(0, _("sh_mounts_init"));
 }
@@ -195,5 +196,5 @@
     SL_RETURN(-1, _("sh_mounts_timer"));
   }
- 
+  
   SL_RETURN(0, _("sh_mounts_timer"));
 }
@@ -261,6 +262,6 @@
 
 /* Module reconfiguration
- * Run on receipt of a HUP.
- */
+ * Run on receipt of a HUP. Right now this is identical to _end(), but it may
+ * not always be. */
 int sh_mounts_reconf()
 {
@@ -268,12 +269,4 @@
   sh_mounts_mnt_free(mountlist);
   mountlist = NULL;
-
-  /* re-set defaults
-   */
-  ShMountsActive    = S_FALSE;
-  ShMountsInterval  = 86400;
-  ShMountsSevMnt    = 7;
-  ShMountsSevOpt    = 7;
-
   SL_RETURN( (0), _("sh_mounts_null"));
 }
@@ -283,9 +276,9 @@
 
 /* Configure to check a particular mount */
-int sh_mounts_config_mount (const char * opt_in)
+int sh_mounts_config_mount (char * opt)
 {
   struct sh_mounts_mnt *m;
   struct sh_mounts_opt *o;
-  char *sp, *temp, *opt;
+  char *sp, *temp;
 
   SL_ENTER(_("sh_mounts_config_mount"));
@@ -293,9 +286,9 @@
   /* It's probably best to make a copy of opt before messing about with it
    * via string functions. Good practice and all that. */
-  temp = sh_util_strdup(opt_in);
+  temp = sh_util_strdup(opt);
 
   /* Since we're going to "consume" this new buffer, it'll be good to have a
    * reference to it's allocated memory so we can free it later. Let's use
-   * temp for that, and "opt" for consumption */
+   * temp for that, and the now-unused "opt" for consumption */
   opt = temp;
   
@@ -334,5 +327,5 @@
 
 /* Simply sets our boolean as to whether this module is active */
-int sh_mounts_config_activate (const char * opt)
+int sh_mounts_config_activate (char * opt)
 {
   int i;
@@ -343,5 +336,5 @@
 
 /* Sets up our timer */
-int sh_mounts_config_timer (const char * opt)
+int sh_mounts_config_timer (char * opt)
 {
   long val;
@@ -364,5 +357,5 @@
 
 /* Configure severity for "mount missing" messages */
-int sh_mounts_config_sevmnt  (const char * opt)
+int sh_mounts_config_sevmnt  (char * opt)
 {
   int retval = 0;
@@ -377,5 +370,5 @@
 }
 
-int sh_mounts_config_sevopt  (const char * opt)
+int sh_mounts_config_sevopt  (char * opt)
 {
   int retval = 0;
@@ -398,5 +391,5 @@
 
 /* FreeBSD includes */
-#if defined(HOST_IS_FREEBSD) || defined(HOST_IS_OPENBSD) || defined(HOST_IS_DARWIN)
+#ifdef HOST_IS_FREEBSD
 #include <sys/param.h>
 #include <sys/ucred.h>
@@ -457,5 +450,6 @@
   
   while ((c = getc (fd)) == '*') {
-    while (((c = getc (fd)) != '\n') && (c != EOF)) {} /* do nothing */
+    while (((c = getc (fd)) != '\n') && (c != EOF)) {
+    }
   }
 }
@@ -572,8 +566,8 @@
 
 /* end AIX helper routines */
-#endif 
-#endif
-
-#if defined(HOST_IS_FREEBSD) || defined(HOST_IS_OPENBSD) || defined(HOST_IS_DARWIN)
+#endif
+#endif
+
+#if defined(HOST_IS_FREEBSD)
 
 /* FreeBSD returns flags instead of strings as mount options, so we'll convert
@@ -585,31 +579,12 @@
 
 	struct {char *opt; int flag;} table[] = {
-#ifdef MNT_RDONLY
 		{"ro",		MNT_RDONLY},
-#endif
-#ifdef MNT_NOEXEC
 		{"noexec",	MNT_NOEXEC},
-#endif
-#ifdef MNT_NOSUID
 		{"nosuid",	MNT_NOSUID},
-#endif
-#ifdef MNT_NODEV
 		{"nodev",	MNT_NODEV},
-#endif
-#ifdef MNT_SYNCHRONOUS
 		{"sync",	MNT_SYNCHRONOUS},
-#endif
-#ifdef MNT_ASYNC
 		{"async",	MNT_ASYNC},
-#endif
-#ifdef MNT_LOCAL
 		{"local",	MNT_LOCAL},
-#endif
-#ifdef MNT_QUOTA
 		{"quota",	MNT_QUOTA},
-#endif
-#ifdef MNT_NOATIME
-		{"noatime",	MNT_NOATIME},
-#endif
 		{"bound",	-1}
 	};
@@ -682,6 +657,6 @@
 	list = m;
 
-/* The Open/FreeBSD way */
-#if defined(HOST_IS_FREEBSD) || defined(HOST_IS_OPENBSD) || defined(HOST_IS_DARWIN)
+/* The FreeBSD way */
+#ifdef HOST_IS_FREEBSD
 {
 	struct statfs *fsp;
@@ -749,5 +724,5 @@
 	}
 
-	sl_fclose(FIL__, __LINE__, tab);
+	fclose(tab);
 }
 #endif
@@ -806,5 +781,5 @@
         }
 
-        (void) sl_fclose(FIL__, __LINE__, tab);
+        (void) fclose(tab);
 	aix_fs_get (NULL, NULL); /* reset */
 }
Index: trunk/src/sh_nmail.c
===================================================================
--- trunk/src/sh_nmail.c	(revision 591)
+++ 	(revision )
@@ -1,1032 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 2008 Rainer Wichmann                                      */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-#include "config_xor.h"
-
-#if defined(HAVE_PTHREAD_MUTEX_RECURSIVE)
-#define _XOPEN_SOURCE 500
-#endif
-
-#if defined(GCC_VERSION_MAJOR) && !defined(__clang__)
-#if (GCC_VERSION_MAJOR > 4) || ((GCC_VERSION_MAJOR == 4) && (GCC_VERSION_MINOR > 8))
-#pragma GCC diagnostic ignored "-Wclobbered"
-#endif
-#endif
-
-#include <string.h>
-#include <time.h>
-
-#if defined(SH_WITH_MAIL)
-
-#undef  FIL__
-#define FIL__  _("sh_nmail.c")
-
-#include "samhain.h"
-#include "sh_pthread.h"
-#include "sh_mem.h"
-#include "sh_mail.h"
-#include "sh_tiger.h"
-#include "sh_string.h"
-#include "sh_utils.h"
-#include "sh_fifo.h"
-#include "sh_filter.h"
-#include "sh_mail_int.h"
-
-SH_MUTEX_INIT(mutex_listall, PTHREAD_MUTEX_INITIALIZER);
-SH_MUTEX_INIT(mutex_flush_l, PTHREAD_MUTEX_INITIALIZER);
-
-/* Pointer to last address */
-
-static struct alias * last = NULL;
-
-/* List of mail recipients */
-
-static struct alias * recipient_list = NULL;
-
-static struct alias * compiled_recipient_list = NULL;
-static sh_filter_type compiled_mail_filter = SH_FILT_INIT;
-
-/* List of mail aliases */
-
-static struct alias * alias_list = NULL;
-
-/* List of all recipients */
-
-struct alias * all_recipients = NULL;
-
-/* Check if addr is in list. If list is all_recipients,
- * must iterate over ->all_next instead of ->next
- */
-static int check_double (const char * str, struct alias * list, int isAll)
-{
-  if (str && list)
-    {
-      struct alias * item = list;
-
-      while (item)
-	{
-	  if (0 == strcmp(sh_string_str(item->recipient), str))
-	    return -1;
-	  if (isAll)
-	    item = item->all_next;
-	  else
-	    item = item->next;
-	}
-    }
-  return 0;
-}
-
-/* Add recipient to 'list' AND to all_recipients. If
- * it already is in all_recipients, mark it as an alias
- * (isAlias = 1).
- */
-struct alias * add_recipient_intern(const char * str, 
-				    struct alias * list)
-{
-  if (str)
-    {
-      struct alias * new  = SH_ALLOC(sizeof(struct alias));
-      new->next           = list;
-      new->mx_list        = NULL;
-      new->mail_filter    = NULL;
-      new->recipient_list = NULL;
-      new->severity       = (-1);
-      new->send_mail      = 0;
-      new->isAlias        = 0;
-      new->recipient      = sh_string_new_from_lchar(str, strlen(str));
-      list                = new;
-
-      SH_MUTEX_LOCK_UNSAFE(mutex_listall);
-      if (0 != check_double(str, all_recipients, S_TRUE))
-	{
-	  new->isAlias    = 1;
-	}
-      new->all_next       = all_recipients;
-      all_recipients      = new;
-      SH_MUTEX_UNLOCK_UNSAFE(mutex_listall);
-    }
-  return list;
-}
-
-int sh_nmail_close_recipient(const char * str)
-{
-  (void) str;
-
-  if (last)
-    {
-      last = NULL;
-      return 0;
-    }
-  return -1;
-}
-
-/* Add a single recipient. Must not be in in
- * recipient_list already, and not in all_recipients.
- */
-int sh_nmail_add_recipient(const char * str)
-{
-  /* return error if duplicate, or 
-   * already defined within an alias list.
-   */
-  if (0 == check_double(str,  recipient_list, S_FALSE) &&
-      0 == check_double(str,  all_recipients, S_TRUE))
-    {
-      recipient_list = add_recipient_intern(str, recipient_list);
-      last           = recipient_list;
-      return 0;
-    }
-  return -1;
-}
-
-/* Add a compiled-in address. These share the compiled_mail_filter
- */
-int sh_nmail_add_compiled_recipient(const char * str)
-{
-  if (0 == check_double(str,  compiled_recipient_list, S_FALSE))
-    {
-      compiled_recipient_list = 
-	add_recipient_intern(str, compiled_recipient_list);
-      if (compiled_recipient_list)
-	compiled_recipient_list->mail_filter = &compiled_mail_filter;
-      last           = compiled_recipient_list;
-      return 0;
-    }
-  return -1;
-}
-
-/* Add an alias; format is name ":" comma-delimited_list_of_recipients
- */
-int sh_nmail_add_alias(const char * str)
-{
-#define SH_ALIASES_RECP_NUM 256
-  size_t lengths[SH_ALIASES_RECP_NUM];
-  unsigned int    nfields = SH_ALIASES_RECP_NUM;
-  char * new = sh_util_strdup(str);
-  char * p   = strchr(new, ':');
-  char * q;
-
-  if (p && strlen(p) > 1)
-    {
-      unsigned int     i;
-      char ** array;
-
-      *p = '\0'; q = p; ++p;
-      if (strlen(new) > 0)
-	{
-	  /* strip trailing space
-	   */
-	  --q; while ((q != new) && *q == ' ') { *q = '\0'; --q; }
-	}
-      else
-	{
-	  goto err;
-	}
-
-      if (0 == check_double(new, alias_list, S_FALSE))
-	{
-	  array = split_array_list(p, &nfields, lengths);
-
-	  if (array && nfields > 0)
-	    {
-	      struct alias * newalias = NULL;
-
-	      /* Enforce that all list members are defined already
-	       */
-	      int                nflag = 0;
-
-	      for (i = 0; i < nfields; ++i) {
-		if (0 == check_double(array[i],  all_recipients, S_TRUE))
-		  nflag = 1; /* not in all_recipients --> bad */
-	      }
-
-	      if (nflag == 0)
-		{
-		  newalias                 = SH_ALLOC(sizeof(struct alias));
-		  newalias->recipient_list = NULL;
-		  newalias->mail_filter    = NULL;
-		  newalias->mx_list        = NULL;
-		  newalias->severity       = (-1);
-		  
-		  /* This is the alias */
-		  newalias->recipient = sh_string_new_from_lchar(new, strlen(new));
-		  
-		  for (i = 0; i < nfields; ++i)
-		    {
-		      if (lengths[i] > 0 && 
-			  0 == check_double(array[i], newalias->recipient_list, S_FALSE))
-			{
-			  newalias->recipient_list = 
-			    add_recipient_intern(array[i], newalias->recipient_list);
-			}
-		    }
-		}
-
-	      SH_FREE(array);
-
-	      if (newalias == NULL || newalias->recipient_list == NULL)
-		{
-		  if (newalias)
-		    SH_FREE(newalias);
-		  goto err;
-		}
-      
-	      newalias->next = alias_list;
-	      alias_list     = newalias;
-	      last           = alias_list;
-
-	      SH_FREE(new);
-	      return 0;
-	    }
-	}
-    }
- err:
-  SH_FREE(new);
-  return -1;
-}
-
-
-/* <<<<<<<<<<<<<<< Recipient List >>>>>>>>>>>>>>>>>>>>>> */
-
-static struct alias * find_list (const char * alias, int * single)
-{
-  struct alias * list   = NULL;
-
-  *single = 0;
-
-  if (!alias)
-    {
-      list = all_recipients;
-    }
-  else
-    {
-      struct alias * test = alias_list;
-      
-      while (test)
-	{
-	  if (0 == strcmp(alias, sh_string_str(test->recipient)))
-	    {
-	      list = test->recipient_list;
-	      break;
-	    }
-	  test = test->next;
-	}
-      
-      if (!list)
-	{
-	  test = recipient_list;
-	  while (test)
-	    {
-	      if (0 == strcmp(alias, sh_string_str(test->recipient)))
-		{
-		  list   = test;
-		  *single = 1;
-		  break;
-		}
-	      test = test->next;
-	    }
-	}
-      
-      if (!list)
-	{
-	  test = compiled_recipient_list;
-	  while (test)
-	    {
-	      if (0 == strcmp(alias, sh_string_str(test->recipient)))
-		{
-		  list   = test;
-		  *single = 1;
-		  break;
-		}
-	      test = test->next;
-	    }
-	}
-    }
-  return list;
-}
-
-/* Returns zero (no) or one (yes). Used to tag messages that are
- * valid for a given recipient (or mailing list alias).
- */
-int sh_nmail_valid_message_for_alias (int level, 
-				      const char * message, 
-				      const char * alias, 
-				      const void * rcv_info)
-{
-  const struct alias * rcv = (const struct alias *) rcv_info;
-
-  if (!alias || 0 == strcmp(alias, sh_string_str(rcv->recipient)))
-    {
-      if ((level & rcv->severity) == 0)
-	{
-	  return 0;
-	}
-
-      if (rcv->mail_filter)
-	{
-	  if (0 != sh_filter_filter(message, rcv->mail_filter))
-	    {
-	      return 0;
-	    }
-	}
-    }
-
-  return 1;
-}
-
-/* Returns number of recipients */
-
-static
-int sh_nmail_compute_recipients (int level, const char * message, 
-				 const char * alias, int flagit)
-{
-  struct alias * list   = NULL;
-  int            single = 0;
-  int            retval = 0;
-
-  if (flagit)
-    {
-      list = all_recipients;
-      while (list)
-	{
-	  list->send_mail = 0;
-	  list = list->all_next;
-	}
-      list = NULL;
-    }
-
-  if (message)
-    {
-      int flag = 0;
-
-      list = find_list (alias, &single);
-      if (list == all_recipients)
-	flag = 1;
-
-      while (list)
-	{
-	  /* Check severity 
-	   */
-	  if ((list->severity & level) == 0)
-	    {
-	      if (single) break;
-	      if (flag)
-		list = list->all_next;
-	      else
-		list = list->next;
-	      continue;
-	    }
-
-	  /* Check filter
-	   */
-	  if (list->mail_filter &&
-	      0 != sh_filter_filter(message, list->mail_filter))
-	    {
-	      if (single) break;
-	      if (flag)
-		list = list->all_next;
-	      else
-		list = list->next;
-	      continue;
-	    }
-	  
-	  /* Mark the entry
-	   */
-	  if (flag)
-	    {
-	      /* Don't mark aliases
-	       */
-	      if (flagit && list->isAlias == 0)
-		{
-		  list->send_mail = 1;
-		}
-	      list = list->all_next;
-	    }
-	  else
-	    {
-	      if (flagit)
-		  list->send_mail = 1;
-	      list = list->next;
-	    }
-	  ++retval;
-	}
-    }
-  return retval;
-}
-
-/* Is not called from same(recursively) or different thread
- */
-static
-int sh_nmail_flag_recipients (int level, const char * message, 
-			      const char * alias)
-{
-  int retval = 0;
-
-  if (message)
-    {
-      SH_MUTEX_LOCK_UNSAFE(mutex_listall);
-      retval = sh_nmail_compute_recipients (level, message, alias, 1);
-      SH_MUTEX_UNLOCK_UNSAFE(mutex_listall);
-    }
-  return retval;
-}
-
-/* Can be called from same thread with mutex_listall held via sh_nmail_flush()
- */
-static
-int sh_nmail_test_recipients (int level, const char * message, 
-			      const char * alias)
-{
-  int retval = 0;
-
-  if (message)
-    {
-      if (0 == SH_MUTEX_TRYLOCK_UNSAFE(mutex_flush_l))
-	{
-	  SH_MUTEX_LOCK_UNSAFE(mutex_listall);
-	  retval = sh_nmail_compute_recipients (level, message, alias, 0);
-	  SH_MUTEX_UNLOCK_UNSAFE(mutex_listall);
-	  SH_MUTEX_UNLOCK_UNSAFE(mutex_flush_l);
-	}
-    }
-  return retval;
-}
-
-/* <<<<<<<<<<<<<<<<<<<  Mail the message  >>>>>>>>>>>>>>>>>>>>>> */
-
-SH_MUTEX_RECURSIVE(mutex_nmail_msg);
-SH_MUTEX_STATIC(nmail_lock, PTHREAD_MUTEX_INITIALIZER);
-
-/*
- * First test list of recipients, then call sh_mail_pushstack().
- */
-int sh_nmail_pushstack (int level, const char * message, 
-			const char * alias)
-{
-  int retval = 0;
-
-  if (0 != sh_nmail_test_recipients (level, message, alias))
-    {
-      retval = sh_mail_pushstack(level, message, alias);
-    }
-  return retval;
-}
-
-static int nmail_count = 0;
-
-/*
- * First mark list of recipients, then call sh_mail_msg().
- */
-int sh_nmail_msg (int level, const char * message, 
-		  const char * alias)
-{
-  volatile int retval = 0;
-
-  /* Need to:
-   *   -- wait if different thread, and
-   *   -- fail if same thread. */
-  SH_MUTEX_RECURSIVE_INIT(mutex_nmail_msg);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_nmail_msg);
-
-  /* Only same thread beyond this point. We fail
-   * if count > 0 already. */
-  if (0 == SH_MUTEX_TRYLOCK_UNSAFE(nmail_lock))
-    {
-      ++nmail_count;
-      if (nmail_count != 1)
-	{
-	  --nmail_count;
-	  SH_MUTEX_UNLOCK_UNSAFE(nmail_lock);
-	  goto cleanup;
-	}
-      SH_MUTEX_UNLOCK_UNSAFE(nmail_lock);
-
-      if (0 != sh_nmail_flag_recipients (level, message, alias))
-	{
-	  /* Need to keep info for sh_nmail_pushstack() 
-	   */
-	  SH_MUTEX_LOCK(mutex_listall);
-	  retval = sh_mail_msg(message);
-	  SH_MUTEX_UNLOCK(mutex_listall);
-
-	  if (retval != 0)
-	    {
-	      sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 
-			       retval, MSG_E_SUBGEN,
-			       _("could not mail immediately"),
-			       _("sh_nmail_msg") );
-	      sh_mail_pushstack(level, message, alias);
-	    }
-	}
-      SH_MUTEX_LOCK_UNSAFE(nmail_lock);
-      --nmail_count;
-      SH_MUTEX_UNLOCK_UNSAFE(nmail_lock);
-    }
- cleanup:
-  ; /* label at end of compound statement */
-  SH_MUTEX_RECURSIVE_UNLOCK(mutex_nmail_msg);
-  return retval;
-}
-
-static int sh_nmail_flush_int (void);
-
-int sh_nmail_flush ()
-{
-  int                retval = 0;
-
-  if (0 == SH_MUTEX_TRYLOCK_UNSAFE(nmail_lock))
-    {
-      ++nmail_count;
-      if (nmail_count != 1)
-	{
-	  --nmail_count;
-	  SH_MUTEX_UNLOCK_UNSAFE(nmail_lock);
-	  return retval;
-	}
-      SH_MUTEX_UNLOCK_UNSAFE(nmail_lock);
-
-      retval = sh_nmail_flush_int ();
-
-      SH_MUTEX_LOCK_UNSAFE(nmail_lock);
-      --nmail_count;
-      SH_MUTEX_UNLOCK_UNSAFE(nmail_lock);
-    }
-  return retval;
-}
-
-/* warning: variable âlistâ might be clobbered by âlongjmpâ or âvforkâ*/
-static struct alias ** list_dummy;
-
-/*
- * Loop over all recipients in stack. 
- * For each distinct one, mark all messages for sending. 
- * Then call sh_mail_msg().
- */
-
-static int sh_nmail_flush_int ()
-{
-  int                retval = 0;
-  sh_string *        msg    = NULL;
-  sh_string *        smsg   = NULL;
-  struct alias     * list;
-  struct alias     * dlist;
-
-  /* warning: variable âlistâ might be clobbered by âlongjmpâ or âvforkâ*/
-  list_dummy = &list;
-
-  SH_MUTEX_LOCK(mutex_listall);
-
-  /* Reset recipient list
-   */
-  list = all_recipients;
-  while (list)
-    {
-      list->send_mail = 0;
-      list = list->all_next;
-    }
-
-  /* Check (i) compiled recipients, (b) aliases, (c) single recipients.
-   * For each, tag all messages, then call sh_mail_msg with
-   *  appropriate address list.
-   */
-
-  reset_list(fifo_mail);
-
-  /* Compiled recipients. These share threshold and filter,
-   * hence only the first recipient needs to be tested.
-   */
-  list  = compiled_recipient_list;
-
-  if (list)
-    {
-      msg   = tag_list(fifo_mail, sh_string_str(list->recipient),
-		       sh_nmail_valid_message_for_alias, list, S_TRUE);
-    }
-
-  if (msg)
-    {
-      while (list)
-	{
-	  list->send_mail = 1;
-	  list = list->next;
-	}
-      
-      list = compiled_recipient_list;
-      
-      SH_MUTEX_LOCK(mutex_flush_l);
-      (void) sh_mail_msg(sh_string_str(msg));
-      SH_MUTEX_UNLOCK(mutex_flush_l);
-
-      sh_string_destroy(&msg);
-      
-      list = compiled_recipient_list;
-      while (list)
-	{
-	  list->send_mail = 0;
-	  list = list->next;
-	}
-    }
-
-  /* Aliases
-   */
-  list  = alias_list;
-
-  while (list) {
-
-    /* Work through the recipient list. As smsg stores last msg,
-     * we send a batch whenever msg != smsg, and continue from
-     * that point in the recipient list.
-     */
-    struct alias     * lnew;
-
-    while (list)
-      {
-	msg   = tag_list(fifo_mail, sh_string_str(list->recipient),
-			 sh_nmail_valid_message_for_alias, list, S_FALSE);
-
-	if (msg)
-	  {
-	    if (!smsg) /* init */
-	      {
-		smsg      = sh_string_copy(msg);
-	      }
-	    else
-	      {
-		if (0 != strcmp(sh_string_str(smsg), sh_string_str(msg)))
-		  {
-		    /*
-		     * Don't set list = list->next here, since we want
-		     * to continue with this recipient in the next batch.
-		     */
-		    sh_string_destroy(&msg);
-		    break;
-		  }
-	      }
-	    lnew = list->recipient_list;
-	    while (lnew)
-	      {
-		lnew->send_mail = 1;
-		lnew= lnew->next;
-	      }
-	    sh_string_destroy(&msg);
-	  }
-	list      = list->next;
-      }
-
-    /* Continue here if smsg != msg */
-
-    if (smsg)
-      {
-	SH_MUTEX_LOCK(mutex_flush_l);
-	(void) sh_mail_msg(sh_string_str(smsg));
-	SH_MUTEX_UNLOCK(mutex_flush_l);
-	sh_string_destroy(&smsg);
-      }
-
-    /* Reset old list of recipients (up to current point in list)
-     * and then continue with list from current point on.
-     */
-    dlist  = alias_list;
-    while (dlist)
-      {
-	lnew = dlist->recipient_list;
-	while (lnew)
-	  {
-	    lnew->send_mail = 0;
-	    lnew = lnew->next;
-	  }
-	dlist = dlist->next;
-      }
-  }
-
-
-  /* Single recipients
-   */
-  list  = recipient_list;
-
-  while (list) {
-
-    /* Work through the recipient list. As smsg stores last msg,
-     * we send a batch whenever msg != smsg, and continue from
-     * that point in the recipient list.
-     */
-
-    while (list)
-      {
-	msg   = tag_list(fifo_mail, sh_string_str(list->recipient),
-			 sh_nmail_valid_message_for_alias, list, S_TRUE);
-
-	if (msg)
-	  {
-	    if (!smsg) /* init */
-	      {
-		smsg = sh_string_copy(msg);
-	      }
-	    else
-	      {
-		if (0 != strcmp(sh_string_str(smsg), sh_string_str(msg)))
-		  {
-		    /*
-		     * Don't set list = list->next here, since we want
-		     * to continue with this recipient in the next batch.
-		     */
-		    sh_string_destroy(&msg);
-		    break;
-		  }
-	      }
-	    list->send_mail = 1;
-	    sh_string_destroy(&msg);
-	  }
-	list = list->next;
-      }
-
-    /* Continue here if smsg != msg */
-
-    if (smsg)
-      {
-	SH_MUTEX_LOCK(mutex_flush_l);
-	(void) sh_mail_msg(sh_string_str(smsg));
-	SH_MUTEX_UNLOCK(mutex_flush_l);
-	sh_string_destroy(&smsg);
-      }
-
-    /* Reset old list of recipients (up to current point in list)
-     * and then continue with list from current point on.
-     */
-    dlist  = recipient_list;
-    while (dlist)
-      {
-	dlist->send_mail = 0;
-	dlist = dlist->next;
-      }
-  }
-
-  /* Remove all mails for which no recipient failed
-   */
-
-  sh.mailNum.alarm_last -= commit_list(fifo_mail);
-  SH_MUTEX_UNLOCK(mutex_listall);
-
-  return retval;
-}
-
-
-
-/* <<<<<<<<<<<<<<<<<<<  Severity  >>>>>>>>>>>>>>>>>>>>>> */
-
-/* 
- * -- set severity threshold for recipient or alias
- */
-int sh_nmail_set_severity (const char * str)
-{
-  if (last == recipient_list || last == alias_list)
-    {
-      if (0 == sh_error_set_level(str, &(last->severity)))
-	{
-	  /* All recipients in alias share the severity
-	   */
-	  if (last == alias_list)
-	    {
-	      struct alias * ptr = last->recipient_list;
-
-	      while (ptr)
-		{
-		  ptr->severity = last->severity;
-		  ptr = ptr->next;
-		}
-	    }
-	  return 0;
-	}
-    }
-  return (-1);
-}
-
-/* <<<<<<<<<<<<<<<<<<<  Filters >>>>>>>>>>>>>>>>>>>>>> */
-
-
-int sh_nmail_add_generic (const char * str, int flag)
-{
-  if (last)
-    {
-      if (NULL == last->mail_filter)
-	last->mail_filter = sh_filter_alloc();
-
-      /* All recipients in alias share the mail filter
-       */
-      if (last == alias_list)
-	{
-	  struct alias * ptr = last->recipient_list;
-	  
-	  while (ptr)
-	    {
-	      ptr->mail_filter = last->mail_filter;
-	      ptr = ptr->next;
-	    }
-	}
-
-      return (sh_filter_add (str, last->mail_filter, flag));
-    }
-  return (-1);
-}
-
-/*
- * -- add keywords to the OR filter
- */
-int sh_nmail_add_or (const char * str)
-{
-  return sh_nmail_add_generic(str, SH_FILT_OR);
-}
-
-/*
- * -- add keywords to the AND filter
- */
-int sh_nmail_add_and (const char * str)
-{
-  return sh_nmail_add_generic(str, SH_FILT_AND);
-}
-
-/*
- * -- add keywords to the NOT filter
- */
-int sh_nmail_add_not (const char * str)
-{
-  return sh_nmail_add_generic(str, SH_FILT_NOT);
-}
-
-
-/* <<<<<<<<<<<<<<<<<<<  Mailkey per Alias >>>>>>>>>>>>>>>>>>>>>>>>> */
-
-#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
-#include <sys/mman.h>
-#endif
-
-#include "zAVLTree.h"
-
-zAVLTree        * mailkeys = NULL;
-
-struct alias_mailkey {
-  char * alias;
-  unsigned int mailcount;
-  time_t       id_audit;
-  char   mailkey_old[KEY_LEN+1];
-  char   mailkey_new[KEY_LEN+1];
-};
-
-static zAVLKey sh_nmail_getkey(void const *item)
-{
-  const struct alias_mailkey * t = (const struct alias_mailkey *) item;
-  return (zAVLKey) t->alias;
-}
-
-/* Return mailkey for alias. If there's no key yet, create it and
- * store it in the AVL tree.
- * This is called from sh_mail_msg, 
- *    which is called from sh_nmail_msg,
- *        which is protected by a mutex.
- */
-int sh_nmail_get_mailkey (const char * alias, char * buf, size_t bufsiz,
-			  time_t * id_audit)
-{
-  char hashbuf[KEYBUF_SIZE];
-
- start:
-
-  if (mailkeys)
-    {
-      struct alias_mailkey * t;
-
-      if (!alias)
-	t = (struct alias_mailkey *) zAVLSearch (mailkeys, _("(null)"));
-      else
-	t = (struct alias_mailkey *) zAVLSearch (mailkeys, alias);
-
-      if (t)
-	{
-	  /* iterate the key
-	   */
-	  (void) sl_strlcpy(t->mailkey_new,
-			    sh_tiger_hash (t->mailkey_old, TIGER_DATA, KEY_LEN,
-					   hashbuf, sizeof(hashbuf)),
-			    KEY_LEN+1);
-	  (void) sl_strlcpy(buf, t->mailkey_new, bufsiz);
-	  ++(t->mailcount);
-	}
-      else
-	{
-	  t = SH_ALLOC(sizeof(struct alias_mailkey));
-
-	  MLOCK(t, sizeof(struct alias_mailkey));
-
-	  if (!alias)
-	    t->alias = sh_util_strdup(_("(null)"));
-	  else
-	    t->alias = sh_util_strdup(alias);
-
-	  t->mailcount = 0;
-	  t->id_audit  = time(NULL);
-
-	  BREAKEXIT(sh_util_keyinit);
-	  (void) sh_util_keyinit (t->mailkey_old, KEY_LEN+1);
-
-	  /* iterate the key
-	   */
-	  (void) sl_strlcpy(t->mailkey_new,
-			    sh_tiger_hash (t->mailkey_old, TIGER_DATA, KEY_LEN,
-					   hashbuf, sizeof(hashbuf)),
-			    KEY_LEN+1);
-	  (void) sl_strlcpy(buf, t->mailkey_new, bufsiz);
-	  (void) zAVLInsert(mailkeys, t);
-	}
-
-      /* X(n) -> X(n-1)
-       */
-      (void) sl_strlcpy (t->mailkey_old, t->mailkey_new, KEY_LEN+1);
-
-      *id_audit = t->id_audit;
-
-      return (t->mailcount);
-    }
-
-  mailkeys = zAVLAllocTree (sh_nmail_getkey, zAVL_KEY_STRING);
-  goto start;
-}
-
-/* <<<<<<<<<<<<<<<<<<<  Free for Reconfigure >>>>>>>>>>>>>>>>>>>>>> */
-
-
-static void free_recipient_list(struct alias * list)
-{
-  struct alias * new;
-  sh_filter_type * p = NULL;
-
-  while (list)
-    {
-      new  = list;
-      list = new->next;
-      if (new->mx_list)
-	free_mx(new->mx_list);
-      if (new->mail_filter)
-	{
-	  sh_filter_free(new->mail_filter);
-	  if (!p || p != new->mail_filter)
-	    {
-	      p = new->mail_filter;
-	      SH_FREE(new->mail_filter);
-	    }
-	}
-      sh_string_destroy(&(new->recipient));
-      SH_FREE(new);
-    }
-}
-
-/* Free everything to prepare for reconfigure
- */
-void sh_nmail_free()
-{
-  SH_MUTEX_LOCK_UNSAFE(mutex_listall);
-  all_recipients = NULL;
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_listall);
-
-  free_recipient_list(recipient_list);
-  recipient_list = NULL;
-
-  sh_filter_free(&compiled_mail_filter);
-
-  while (alias_list) 
-    {
-      struct alias * item = alias_list;
-
-      alias_list = item->next;
-
-      sh_string_destroy(&(item->recipient));
-      free_recipient_list(item->recipient_list);
-      if (item->mail_filter)
-	{
-	  sh_filter_free(item->mail_filter);
-	  /* SH_FREE(item->mail_filter); */
-	}
-      SH_FREE(item);
-    }
-  alias_list = NULL;
-
-  last = compiled_recipient_list;
-  return;
-}
-
-/* defined(SH_WITH_MAIL) */
-#endif
Index: trunk/src/sh_port2proc.c
===================================================================
--- trunk/src/sh_port2proc.c	(revision 591)
+++ 	(revision )
@@ -1,1080 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 2008 Rainer Wichmann                                      */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-#include "config_xor.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <unistd.h>
-
-#ifdef HAVE_DIRENT_H
-#include <dirent.h>
-#define NAMLEN(dirent) sl_strlen((dirent)->d_name)
-#else
-#define dirent direct
-#define NAMLEN(dirent) (dirent)->d_namlen
-#ifdef HAVE_SYS_NDIR_H
-#include <sys/ndir.h>
-#endif
-#ifdef HAVE_SYS_DIR_H
-#include <sys/dir.h>
-#endif
-#ifdef HAVE_NDIR_H
-#include <ndir.h>
-#endif
-#endif
-#define NEED_ADD_DIRENT
-
-#if defined(SH_USE_PORTCHECK) && (defined (SH_WITH_CLIENT) || defined (SH_STANDALONE))
-
-/* #define DEBUG_P2P 1 */
-
-#include "samhain.h"
-#include "sh_utils.h"
-
-/****************************************************************************
- *
- *  >>> COMMON CODE <<<
- *
- ****************************************************************************/
-#if defined(__linux__) || defined(__FreeBSD__)
- 
-#include "sh_error_min.h"
-#include "sh_pthread.h"
-#include "sh_ipvx.h"
-
-#define FIL__  _("sh_port2proc.c")
-
-struct sock_store {
-  unsigned long sock;
-  size_t        pid;
-  char *        path;
-  char *        user;
-  struct sock_store * next;
-};
-
-/* /proc: 
- *        linux:     /proc/pid/exe
- *        freebsd:   /proc/pid/file
- *        solaris10: /proc/pid/path/a.out
- */
-static void get_user_and_path (struct sock_store * add)
-{
-  extern char *  sh_unix_getUIDname (int level, uid_t uid, char * out, size_t len);
-
-  char        path[128];
-  char *      buf;
-  struct stat sbuf;
-  int         len;
-  char *      tmp;
-
-  sl_snprintf (path, sizeof(path), "/proc/%ld/exe", (unsigned long) add->pid);
-
-  if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
-    {
-      goto linkread;
-    }
-
-  sl_snprintf (path, sizeof(path), "/proc/%ld/file", (unsigned long) add->pid);
-
-  if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
-    {
-      goto linkread;
-    }
-
-  sl_snprintf (path, sizeof(path), "/proc/%ld/path/a.out", (unsigned long) add->pid);
-
-  if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
-    {
-      goto linkread;
-    }
-
-  return;
-
- linkread:
-
-  buf = SH_ALLOC(PATH_MAX);
-  len = readlink(path, buf, PATH_MAX);   /* flawfinder: ignore */
-  len = (len >= PATH_MAX) ? (PATH_MAX-1) : len;
-
-  if (len > 0)
-    { 
-      buf[len] = '\0';
-      add->path = buf;
-    }
-  else
-    {
-      SH_FREE(buf);
-    }
-
-  add->user = SH_ALLOC(USER_MAX);
-  tmp  = sh_unix_getUIDname (SH_ERR_ALL, sbuf.st_uid, add->user, USER_MAX);
-
-  if (!tmp)
-    sl_snprintf (add->user, USER_MAX, "%ld", (unsigned long) sbuf.st_uid);
-
-  return;
-}
-
-#endif
-
-/****************************************************************************
- *
- *  >>> LINUX CODE <<<
- *
- ****************************************************************************/
-
-#if defined(__linux__)
-
-static  size_t  sh_minpid = 0x0001;
-static  size_t  sh_maxpid = 0x8000;
-
-#ifndef HAVE_LSTAT
-#define lstat(x,y) stat(x,y)
-#endif /* HAVE_LSTAT */
-
-#if defined(S_IFLNK) && !defined(S_ISLNK)
-# define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
-#else
-# if !defined(S_ISLNK)
-#  define S_ISLNK(mode) (0)
-# endif
-#endif
-
-#if defined(__linux__)
-#define PROC_PID_MAX _("/proc/sys/kernel/pid_max")
-
-static int proc_max_pid (size_t * procpid)
-{
-  char * ret;
-  unsigned long  pid;
-  FILE * fd;
-  char   str[128];
-  char * ptr;
-
-  SL_ENTER(_("proc_max_pid"));
-    
-  if (0 == access(PROC_PID_MAX, R_OK)) /* flawfinder: ignore */
-    {
-      if (NULL != (fd = fopen(PROC_PID_MAX, "r")))
-        {
-          str[0] = '\0';
-          ret = fgets(str, 128, fd);
-          if (ret && *str != '\0')
-            {
-              pid = strtoul(str, &ptr, 0);
-              if (*ptr == '\0' || *ptr == '\n')
-                {
-                  sl_fclose(FIL__, __LINE__, fd);
-                  *procpid = (size_t) pid;
-                  SL_RETURN(0, _("proc_max_pid"));
-                }
-            }
-          sl_fclose(FIL__, __LINE__, fd);
-        }
-    }
-  SL_RETURN((-1), _("proc_max_pid"));
-}
-#else
-static int proc_max_pid(size_t * procpid)
-{
-  *procpid = sh_maxpid;
-  return 0;
-}
-#endif
-
-static struct sock_store * socklist = NULL;
-
-static void del_sock_all()
-{
-  struct sock_store * del = socklist;
-
-  while (del)
-    {
-      socklist = del->next;
-      if (del->path)
-	SH_FREE(del->path);
-      if (del->user)
-	SH_FREE(del->user);
-      SH_FREE(del);
-      del = socklist;
-    }
-  socklist = NULL;
-  return;
-}
-
-static void add_sock(unsigned long sock, size_t pid)
-{
-  struct sock_store * add = SH_ALLOC(sizeof(struct sock_store));
-
-  add->sock = sock;
-  add->pid  = pid;
-  add->path = NULL;
-  add->user = NULL;
-  SH_MUTEX_LOCK(mutex_thread_nolog);
-  get_user_and_path(add);
-  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-  add->next = socklist;
-  socklist  = add;
-  return;
-}
-
-static void check_and_add_sock(char * fbuf, size_t pid)
-{
-  if (0 == strncmp(_("socket:["), fbuf, 8))
-    {
-      char * end;
-      unsigned long sock;
-      size_t len = strlen(fbuf);
-      if (fbuf[len-1] == ']')
-	fbuf[len-1] = '\0';
-      sock = strtoul(&fbuf[8], &end, 0);
-      if (*end == '\0' && fbuf[8] != '\0')
-	{
-	  add_sock(sock, pid);
-	}
-    }
-}
-
-static void fetch_socks(size_t pid)
-{
-  char path[128];
-  DIR * dir;
-  sl_snprintf(path, sizeof(path), _("/proc/%lu/fd"), (unsigned long) pid);
-
-  dir = opendir(path);
-  if (dir)
-    {
-      struct dirent *entry;
-      while (NULL != (entry = readdir(dir)))
-	{
-	  char fpath[384];
-	  char fbuf[64];
-	  int  ret;
-	  /* /proc/PID/fd/N-> socket:[15713] */
-	  sl_snprintf(fpath, sizeof(fpath), _("%s/%s"), path, entry->d_name);
-	  ret = readlink(fpath, fbuf, sizeof(fbuf)-1);   /* flawfinder: ignore */
-	  if (ret > 0)
-	    {
-	      fbuf[ret] = '\0';
-	      check_and_add_sock(fbuf, pid);
-	    }
-	}
-      closedir(dir);
-    }
-}
-
-int sh_port2proc_prepare()
-{
-  size_t i;
-  
-  if (0 != proc_max_pid(&sh_maxpid))
-    {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-                      _("Failed to detect max_pid"), 
-                      _("sh_port2proc"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      SL_RETURN ((-1), _("sh_port2proc"));
-    }
-
-  /* Delete old socket list and re-create it
-   */
-  del_sock_all();
-
-  for (i = sh_minpid; i < sh_maxpid; ++i)
-    {
-      fetch_socks(i);
-    }
-
-  return 0;
-}
-
-void sh_port2proc_finish()
-{
-  /* Delete old socket list
-   */
-  del_sock_all();
-  return;
-}
-
-
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-
-/* returns the command and fills the 'user' array 
- */
-static char * port2proc_query(char * file, int proto, int domain,
-			      struct sh_sockaddr * saddr, int sport, 
-			      unsigned long * pid, char * user, size_t userlen)
-{
-  FILE * fd;
-
-  fd = fopen(file, "r");
-
-  *pid = 0;
-
-#ifdef DEBUG_P2P
-  {
-    char errmsg[256];
-    char siface[SH_IP_BUF];
-    sh_ipvx_ntoa(siface, sizeof(siface), saddr);
-    sl_snprintf(errmsg, sizeof(errmsg), 
-		"query, file=%s, proto=%d, port=%d, iface=%s\n",
-		file, proto, sport, siface);
-    fprintf(stderr, "%s", errmsg);
-  }
-#endif
-
-  if (fd)
-    {
-      unsigned int n, i, port, niface, inode, istatus;
-      char line[512];
-      char ip_port[128];
-      char iface[SH_IP_BUF];
-
-      while (NULL != fgets(line, sizeof(line), fd))
-	{
-	
-#ifdef DEBUG_P2P
-	  {
-	    fprintf(stderr, "%s", line);
-	  }
-#endif
-
-	  if (4 == sscanf(line, 
-			  "%u: %127s %*X:%*X %X %*X:%*X %*X:%*X %*X %*d %*d %u %*s",
-			  &n, ip_port, &istatus, &inode))
-	    {
-	      struct sockaddr_in  addr4;
-	      struct sockaddr_in6 addr6;
-	      struct sh_sockaddr  ss;
-	      
-	      char * p;
-
-	      ip_port[127] = '\0';
-
-	      p = strchr(ip_port, ':');
-
-	      if (p)
-		{
-		  *p = '\0'; ++p;
-		  port = (unsigned int) strtoul(p, NULL, 16);
-		  sl_strlcpy(iface, ip_port, sizeof(iface));
-		}
-	      else
-		{
-		  continue;
-		}
-
-	      niface = 0;
-
-	      switch (domain) 
-		{
-		case AF_INET:
-		  addr4.sin_addr.s_addr = (int) strtol(iface, NULL, 16);
-		  niface = (unsigned int) addr4.sin_addr.s_addr;
-		  sh_ipvx_save(&ss, AF_INET, (struct sockaddr *)&addr4);
-		  break;
-
-		case AF_INET6:
-		  sscanf(iface, 
-			 "%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx", 
-			 &addr6.sin6_addr.s6_addr[3], &addr6.sin6_addr.s6_addr[2], &addr6.sin6_addr.s6_addr[1], &addr6.sin6_addr.s6_addr[0], 
-			 &addr6.sin6_addr.s6_addr[7], &addr6.sin6_addr.s6_addr[6], &addr6.sin6_addr.s6_addr[5], &addr6.sin6_addr.s6_addr[4], 
-			 &addr6.sin6_addr.s6_addr[11], &addr6.sin6_addr.s6_addr[10], &addr6.sin6_addr.s6_addr[9], &addr6.sin6_addr.s6_addr[8], 
-			 &addr6.sin6_addr.s6_addr[15], &addr6.sin6_addr.s6_addr[14], &addr6.sin6_addr.s6_addr[13], &addr6.sin6_addr.s6_addr[12]);
-		  
-		  for (i = 0; i < 16; ++i)
-		    {
-		      if (0 != (unsigned int) addr6.sin6_addr.s6_addr[i]) 
-			++niface;
-		    }
-		  sh_ipvx_save(&ss, AF_INET6, (struct sockaddr *)&addr6);
-		  break;
-		}
-
-#ifdef DEBUG_P2P
-	      {
-		char a[SH_IP_BUF];
-		char b[SH_IP_BUF];
-
-		sh_ipvx_ntoa(a, sizeof(a), &ss);
-		sh_ipvx_ntoa(b, sizeof(b), saddr);
-
-		fprintf(stderr, " -> inode %u, iface/port %s,%u, status %u, searching %s,%u, %u\n", 
-			inode, a, port, istatus, b, sport, 
-			proto == IPPROTO_TCP ? 0x0a : 0x07);
-	      }
-#endif
-
-	      if (proto == IPPROTO_TCP && istatus != 0x0a)
-		continue;
-	      if (proto == IPPROTO_UDP && istatus == 0x01)
-		continue;
-
-#ifdef DEBUG_P2P
-	      {
-		fprintf(stderr, "check iface %u..\n", iface);
-	      }
-#endif
-
-	      if ((proto == IPPROTO_UDP || niface == 0 || 0 == sh_ipvx_cmp(&ss, saddr)) && 
-		  port == (unsigned int)sport)
-		{
-		  struct sock_store * new = socklist;
-
-#ifdef DEBUG_P2P
-		  {
-		    fprintf(stderr, "found it\n");
-		  }
-#endif
-
-		  while (new)
-		    {
-#ifdef DEBUG_P2P
-		      {
-			fprintf(stderr, "searching inode %u: %lu\n", 
-				inode, new->sock);
-		      }
-#endif
-		      if (inode == new->sock)
-			{
-#ifdef DEBUG_P2P
-			  {
-			    fprintf(stderr, "found it: path=(%s), user=(%s)\n",
-				    new->path == NULL ? "NULL" : new->path,
-				    new->user == NULL ? "NULL" : new->user);
-			  }
-#endif
-			  sl_fclose(FIL__, __LINE__, fd);
-			  *pid = (unsigned long) new->pid;
-			  if (new->path)
-			    {
-			      if (new->user)
-				sl_strlcpy(user, new->user, userlen);
-			      else
-				sl_strlcpy(user, "-", userlen);
-			      return sh_util_strdup(new->path);
-			    }
-			  goto err_out;
-			}
-		      new = new->next;
-		    }
-		}
-	    }
-	}
-      sl_fclose(FIL__, __LINE__, fd);
-    }
- err_out:
-  sl_strlcpy(user, "-", userlen);
-  return sh_util_strdup("-");
-}
-
-/* returns the command and fills the 'user' array 
- */
-char * sh_port2proc_query(int proto, struct sh_sockaddr * saddr, int sport, 
-			  unsigned long * pid, char * user, size_t userlen)
-{
-  char file[32];
-  char * ret;
- 
-  if (proto == IPPROTO_TCP)
-    {
-      sl_strlcpy(file, _("/proc/net/tcp"), sizeof(file));
-      ret = port2proc_query(file, proto, AF_INET, saddr, sport, pid, user, userlen);
-
-      if (ret[0] == '-' && ret[1] == '\0')
-	{
-	  SH_FREE(ret);
-	  sl_strlcpy(file, _("/proc/net/tcp6"), sizeof(file));
-	  ret = port2proc_query(file, proto, AF_INET6, saddr, sport, pid, user, userlen);
-	}
-      return ret;
-    }
-  else
-    {
-      char * ret;
-      sl_strlcpy(file, _("/proc/net/udp"), sizeof(file));
-      ret = port2proc_query(file, proto, AF_INET, saddr, sport, pid, user, userlen);
-
-      if (ret[0] == '-' && ret[1] == '\0')
-	{
-	  SH_FREE(ret);
-	  sl_strlcpy(file, _("/proc/net/udp6"), sizeof(file));
-	  ret = port2proc_query(file, proto, AF_INET6, saddr, sport, pid, user, userlen);
-	}
-      return ret;
-    }
-}
-
-
-/****************************************************************************
- *
- *  >>> FREEBSD CODE <<<
- *
- ****************************************************************************/
-
-#elif defined(__FreeBSD__)
-
-/* Uses code from sockstat.c. Error and memory handling modified.
- * Only required functions from sockstat.c are included.
- */
-
-/*-
- * Copyright (c) 2002 Dag-Erling Co<EF>dan Sm<F8>rgrav
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer
- *    in this position and unchanged.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. The name of the author may not be used to endorse or promote products
- *    derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/usr.bin/sockstat/sockstat.c,v 1.13.2.1.4.1 2008/10/02 02:57:24 kensmith Exp $");
-
-#include <sys/param.h>
-#include <sys/socket.h>
-#include <sys/socketvar.h>
-#include <sys/sysctl.h>
-#include <sys/file.h>
-#include <sys/user.h>
-
-#include <sys/un.h>
-#include <sys/unpcb.h>
-
-#include <net/route.h>
-
-#include <netinet/in.h>
-#include <netinet/in_pcb.h>
-#include <netinet/tcp.h>
-#include <netinet/tcp_seq.h>
-#include <netinet/tcp_var.h>
-#include <arpa/inet.h>
-
-#include <ctype.h>
-#include <errno.h>
-#include <netdb.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-#include <unistd.h>
-
-static int       opt_4 = 1;         /* Show IPv4 sockets */
-static int       opt_6 = 1;         /* Show IPv6 sockets */
-static int       opt_c = 0;         /* Show connected sockets */
-static int       opt_l = 1;         /* Show listening sockets */
-static int       opt_v = 0;         /* Verbose mode */
-
-struct sock {
-        void *socket;
-        void *pcb;
-        int vflag;
-        int family;
-        int proto;
-
-        struct sockaddr_storage laddr;
-        struct sockaddr_storage faddr;
-        struct sock *next;
-};
-
-#define HASHSIZE 1009
-static struct sock *sockhash[HASHSIZE];
-
-static struct xfile *xfiles;
-static int nxfiles;
-
-
-static void * xrealloc(void * buf, size_t len0, size_t len)
-{
-  if (len > 0)
-    {
-      void * xbuf = SH_ALLOC(len);
-      if (buf)
-	{
-	  if (len0 <= len)
-	    memcpy(xbuf, buf, len0);
-	  else
-	    memset(xbuf, 0, len);
-	  SH_FREE(buf);
-	}
-      return xbuf;
-    }
-  SH_FREE(buf);
-  return NULL;
-}
-
-/* Sets address and port in struct sockaddr_storage *sa
- */
-static void
-sockaddr(struct sockaddr_storage *sa, int af, void *addr, int port)
-{
-        struct sockaddr_in *sin4;
-        struct sockaddr_in6 *sin6;
-
-        bzero(sa, sizeof *sa);
-        switch (af) {
-        case AF_INET:
-                sin4 = (struct sockaddr_in *)sa;
-                sin4->sin_len = sizeof *sin4;
-                sin4->sin_family = af;
-                sin4->sin_port = port;
-                sin4->sin_addr = *(struct in_addr *)addr;
-                break;
-        case AF_INET6:
-                sin6 = (struct sockaddr_in6 *)sa;
-                sin6->sin6_len = sizeof *sin6;
-                sin6->sin6_family = af;
-                sin6->sin6_port = port;
-                sin6->sin6_addr = *(struct in6_addr *)addr;
-                break;
-        default:
-                return;
-        }
-}
-
-/* Get socket information from the kernel.
- */
-static void
-gather_inet(int proto)
-{
-        struct xinpgen *xig, *exig;
-        struct xinpcb *xip;
-        struct xtcpcb *xtp;
-        struct inpcb *inp;
-        struct xsocket *so;
-        struct sock *sock;
-        char varname[32];
-        size_t len, bufsize, bufsize0;
-        void *buf;
-        int hash, retry, vflag;
-
-        vflag = 0;
-        if (opt_4)
-                vflag |= INP_IPV4;
-        if (opt_6)
-                vflag |= INP_IPV6;
-
-        switch (proto) {
-        case IPPROTO_TCP:
-	  sl_strlcpy(varname, _("net.inet.tcp.pcblist"), sizeof(varname));
-                break;
-        case IPPROTO_UDP:
-                sl_strlcpy(varname, _("net.inet.udp.pcblist"), sizeof(varname));
-                break;
-        case IPPROTO_DIVERT:
-                sl_strlcpy(varname, _("net.inet.divert.pcblist"), sizeof(varname));
-                break;
-        default:
-                return;
-        }
-
-        buf = NULL;
-        bufsize  = 8192;
-	bufsize0 = bufsize;
-        retry = 5;
-        do {
-                for (;;) {
-		        buf = xrealloc(buf, bufsize0, bufsize);
-			bufsize0 = bufsize;
-                        len = bufsize;
-                        if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
-                                break;
-		        if (errno == ENOENT)
-                                goto out;
-                        if (errno != ENOMEM)
-			  {
-			    SH_MUTEX_LOCK(mutex_thread_nolog);
-			    sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 
-					    0, MSG_E_SUBGEN, 
-					    _("sysctlbyname()"), 
-					    _("gather_inet"));
-			    SH_MUTEX_UNLOCK(mutex_thread_nolog);
-			    SH_FREE(buf);
-			    return;
-			  }
-                        bufsize *= 2;
-                }
-                xig = (struct xinpgen *)buf;
-                exig = (struct xinpgen *)(void *)
-                    ((char *)buf + len - sizeof *exig);
-                if (xig->xig_len != sizeof *xig ||
-                    exig->xig_len != sizeof *exig)
-		  {
-		    SH_MUTEX_LOCK(mutex_thread_nolog);
-		    sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-				    _("struct xinpgen size mismatch"), 
-				    _("gather_inet"));
-		    SH_MUTEX_UNLOCK(mutex_thread_nolog);
-		    goto out;
-		  }
-
-        } while (xig->xig_gen != exig->xig_gen && retry--);
-
-        if (xig->xig_gen != exig->xig_gen && opt_v)
-		  {
-		    SH_MUTEX_LOCK(mutex_thread_nolog);
-		    sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-				    _("data may be inconsistent"), 
-				    _("gather_inet"));
-		    SH_MUTEX_UNLOCK(mutex_thread_nolog);
-		  }
-
-        for (;;) {
-                xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
-                if (xig >= exig)
-                        break;
-                switch (proto) {
-                case IPPROTO_TCP:
-                        xtp = (struct xtcpcb *)xig;
-                        if (xtp->xt_len != sizeof *xtp) {
-				SH_MUTEX_LOCK(mutex_thread_nolog);
-				sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-						_("struct xtcpcb size mismatch"), 
-						_("gather_inet"));
-				SH_MUTEX_UNLOCK(mutex_thread_nolog);
-                                goto out;
-                        }
-                        inp = &xtp->xt_inp;
-                        so = &xtp->xt_socket;
-                        break;
-                case IPPROTO_UDP:
-                case IPPROTO_DIVERT:
-                        xip = (struct xinpcb *)xig;
-                        if (xip->xi_len != sizeof *xip) {
-				SH_MUTEX_LOCK(mutex_thread_nolog);
-				sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-						_("struct xinpcb size mismatch"), 
-						_("gather_inet"));
-				SH_MUTEX_UNLOCK(mutex_thread_nolog);
-                                goto out;
-                        }
-                        inp = &xip->xi_inp;
-                        so = &xip->xi_socket;
-                        break;
-                default:
-                        return;
-                }
-                if ((inp->inp_vflag & vflag) == 0)
-                        continue;
-                if (inp->inp_vflag & INP_IPV4) {
-                        if ((inp->inp_fport == 0 && !opt_l) ||
-                            (inp->inp_fport != 0 && !opt_c))
-                                continue;
-                } else if (inp->inp_vflag & INP_IPV6) {
-#ifndef in6p_fport
-#define in6p_fport inp_fport
-#endif
-                        if ((inp->in6p_fport == 0 && !opt_l) ||
-                            (inp->in6p_fport != 0 && !opt_c))
-                                continue;
-                } else {
-		        if (opt_v) {
-			        char errmsg[64];
-                                sl_snprintf(errmsg, sizeof(errmsg), 
-					    _("invalid vflag 0x%x"), inp->inp_vflag);
-				SH_MUTEX_LOCK(mutex_thread_nolog);
-				sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-						errmsg, 
-						_("gather_inet"));
-				SH_MUTEX_UNLOCK(mutex_thread_nolog);
-				continue;
-			}
-                }
-
-                sock = SH_ALLOC(sizeof *sock);
-		memset(sock, 0, sizeof (*sock));
-
-#ifndef in6p_lport
-#define in6p_lport inp_lport
-#endif
-                sock->socket = so->xso_so;
-                sock->proto = proto;
-                if (inp->inp_vflag & INP_IPV4) {
-                        sock->family = AF_INET;
-                        sockaddr(&sock->laddr, sock->family,
-                            &inp->inp_laddr, inp->inp_lport);
-                        sockaddr(&sock->faddr, sock->family,
-                            &inp->inp_faddr, inp->inp_fport);
-                } else if (inp->inp_vflag & INP_IPV6) {
-                        sock->family = AF_INET6;
-                        sockaddr(&sock->laddr, sock->family,
-                            &inp->in6p_laddr, inp->in6p_lport);
-                        sockaddr(&sock->faddr, sock->family,
-                            &inp->in6p_faddr, inp->in6p_fport);
-                }
-                sock->vflag = inp->inp_vflag;
-
-                hash = (int)((uintptr_t)sock->socket % HASHSIZE);
-                sock->next = sockhash[hash];
-                sockhash[hash] = sock;
-        }
-out:
-	if (buf)
-	  SH_FREE(buf);
-}
-
-static void
-getfiles(void)
-{
-        size_t len;
-        size_t len0;
-
-        xfiles = SH_ALLOC(len = sizeof *xfiles);
-	len0   = len;
-
-        while (sysctlbyname(_("kern.file"), xfiles, &len, 0, 0) == -1) {
-                if (errno != ENOMEM)
-		  {
-		    volatile int status = errno;
-		    SH_MUTEX_LOCK(mutex_thread_nolog);
-		    sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
-				    _("sysctlbyname()"),
-				    _("getfiles"));
-		    SH_MUTEX_UNLOCK(mutex_thread_nolog);
-		  }
-                len *= 2;
-                xfiles = xrealloc(xfiles, len0, len);
-		len0   = len;
-        }
-        if (len > 0 && xfiles->xf_size != sizeof *xfiles)
-                if (errno != ENOMEM)
-		  {
-		    volatile int status = errno;
-		    SH_MUTEX_LOCK(mutex_thread_nolog);
-		    sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
-				    _("struct xfile size mismatch"),
-				    _("getfiles"));
-		    SH_MUTEX_UNLOCK(mutex_thread_nolog);
-		  }
-        nxfiles = len / sizeof *xfiles;
-}
-
-static const char *
-getprocname(pid_t pid)
-{
-        static struct kinfo_proc proc;
-        size_t len;
-        int mib[4];
-
-        mib[0] = CTL_KERN;
-        mib[1] = KERN_PROC;
-        mib[2] = KERN_PROC_PID;
-        mib[3] = (int)pid;
-        len = sizeof proc;
-        if (sysctl(mib, 4, &proc, &len, NULL, 0) == -1) {
-                /* Do not warn if the process exits before we get its name. */
-                if (errno != ESRCH)
-		  {
-		    volatile int status = errno;
-		    SH_MUTEX_LOCK(mutex_thread_nolog);
-		    sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, status, MSG_E_SUBGEN,
-				    _("sysctl()"),
-				    _("getfiles"));
-		    SH_MUTEX_UNLOCK(mutex_thread_nolog);
-		  }
-                return ("-");
-        }
-        return (proc.ki_ocomm);
-}
-
-char * sh_port2proc_query(int proto, struct sh_sockaddr * saddr, int sport,
-			  unsigned long * pid, char * user, size_t userlen)
-{
-  int n, hash;
-  struct xfile *xf;
-  struct in_addr  * haddr  = NULL;
-  struct in6_addr * haddr6 = NULL;
-  struct sock * s;
-  struct in6_addr   anyaddr = IN6ADDR_ANY_INIT; 
-
-  *pid = 0;
-  
-  for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
-    
-    if (xf->xf_data == NULL)
-      continue;
-    
-    /* Find the socket in sockhash[] that corresponds to it
-     */
-    hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
-    for (s = sockhash[hash]; s != NULL; s = s->next)
-      if ((void *)s->socket == xf->xf_data)
-	break;
-    
-    if (!s)
-      continue;
-
-    if (s->proto != proto)
-      continue;
-
-    if (s->family != AF_INET && s->family != AF_INET6)
-      continue;
-
-    if (s->family == AF_INET &&
-	(sport != ntohs(((struct sockaddr_in *)(&s->laddr))->sin_port)))
-      continue;
-
-    if (s->family == AF_INET6 &&
-	(sport != ntohs(((struct sockaddr_in6 *)(&s->laddr))->sin6_port)))
-      continue;
-
-    if (s->family == AF_INET)
-      haddr  = &((struct sockaddr_in  *)(&s->laddr))->sin_addr;
-    if (s->family == AF_INET6)
-      haddr6 = &((struct sockaddr_in6 *)(&s->laddr))->sin6_addr;
-    
-
-    if ( (s->family == AF_INET && 
-	  (haddr->s_addr == (saddr->sin).sin_addr.s_addr || 
-	   sh_ipvx_isany(saddr) || 
-	   inet_lnaof(*haddr) == INADDR_ANY))
-	 ||
-	 (s->family == AF_INET6 &&
-	  (0 == memcmp(haddr6->s6_addr, &((saddr->sin6).sin6_addr.s6_addr), 16) ||
-	   0 == memcmp(haddr6->s6_addr, &(anyaddr.s6_addr), 16) ||
-	   sh_ipvx_isany(saddr) ))
-	 )
-      {
-	struct sock_store try;
-	
-	*pid = xf->xf_pid;
-
-	try.pid  = xf->xf_pid;
-	try.path = NULL;
-	try.user = NULL;
-	get_user_and_path (&try); /* Try to get info from /proc */
-
-	if (try.path == NULL)
-	  {
-	    extern char * sh_unix_getUIDname (int level, uid_t uid, char * out, size_t len);
-	    char * tmp  = sh_unix_getUIDname (SH_ERR_ALL, xf->xf_uid, user, userlen);
-	    if (!tmp)
-	      sl_snprintf (user, userlen, "%ld", (unsigned long) xf->xf_uid);
-	    return sh_util_strdup(getprocname(xf->xf_pid));
-	  }
-	else
-	  {
-	    sl_strlcpy(user, try.user, userlen);
-	    SH_FREE(try.user);
-	    return try.path;
-	  }
-      }
-  }
-  sl_strlcpy(user, "-", userlen);
-  return sh_util_strdup("-");
-}
-
-static void sockdel(struct sock * sock)
-{
-  if (sock)
-    {
-      if (sock->next)
-	sockdel(sock->next);
-      SH_FREE(sock);
-    }
-  return;
-}
-
-int sh_port2proc_prepare()
-{
-  int i;
-
-  if (xfiles)
-    {
-      SH_FREE(xfiles);
-      xfiles = NULL;
-    }
-
-  for (i = 0; i < HASHSIZE; ++i)
-    {
-      sockdel(sockhash[i]);
-      sockhash[i] = NULL;
-    }
-
-  /* Inet connections
-   */
-  gather_inet(IPPROTO_TCP);
-  gather_inet(IPPROTO_UDP);
-  gather_inet(IPPROTO_DIVERT);
-
-  getfiles();
-
-  return 0;
-}
-
-void sh_port2proc_finish()
-{
-  return;
-}
-
-#else /* !defined(__linux__) && !defined(__FreeBSD__) */
-
-#include "samhain.h"
-#include "sh_utils.h"
-#include "sh_ipvx.h"
-
-char * sh_port2proc_query(int proto, struct sh_sockaddr * saddr, int sport,
-			  unsigned long * pid, char * user, size_t userlen)
-{
-  (void) proto;
-  (void) saddr;
-  (void) sport;
-
-  *pid = 0;
-
-  sl_strlcpy(user, "-", userlen);
-  return sh_util_strdup("-");
-}
-
-int sh_port2proc_prepare()
-{
-  return 0;
-}
-
-void sh_port2proc_finish()
-{
-  return;
-}
-#endif
-
-#endif /* defined(SH_USE_PORTCHECK) */
Index: trunk/src/sh_portcheck.c
===================================================================
--- trunk/src/sh_portcheck.c	(revision 591)
+++ 	(revision )
@@ -1,2703 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 2006 Rainer Wichmann                                      */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-/***************************************************************************
- *
- * This file provides a module for samhain to check for open ports
- * on the local machine.
- *
- */
-
-
-/* #define TEST_ONLY */
-#ifndef TEST_ONLY
-#include "config_xor.h"
-#endif
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#ifdef HAVE_IFADDRS_H
-#include <ifaddrs.h>
-#include <netdb.h>
-#endif
-#include <errno.h>
-#include <unistd.h>
-#include <fcntl.h>
-
-#define PORTCHK_VERSION "1.1"
-
-#if defined(TEST_ONLY) || (defined(SH_USE_PORTCHECK) && (defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)))
-
-
-#define PORTMAP
-#ifdef  HAVE_RPC_RPC_H
-#include <rpc/rpc.h>
-#ifdef  HAVE_RPC_RPCENT_H
-#include <rpc/rpcent.h>
-#endif
-#include <rpc/pmap_clnt.h>
-#include <rpc/pmap_prot.h>
-#endif
-#include <netdb.h>
-
-/*
- * struct pmaplist {
- *      struct pmap     pml_map;
- *      struct pmaplist *pml_next;
- * };
- */
-
-/* struct pmap {
- *      long unsigned pm_prog;
- *      long unsigned pm_vers;
- *      long unsigned pm_prot;
- *      long unsigned pm_port;
- * };
- */
-
-/* TIME_WAIT ? 60-240 seconds */
-
-#if !defined(TEST_ONLY)
-
-#define FIL__ _("sh_portcheck.c")
-#include "samhain.h"
-#include "sh_error.h"
-#include "sh_mem.h"
-#include "sh_calls.h"
-#include "sh_utils.h"
-#include "sh_modules.h"
-#define SH_NEED_GETHOSTBYXXX
-#include "sh_static.h"
-#include "sh_pthread.h"
-#include "sh_ipvx.h"
-
-/* the size of an interface string 
- */
-#define SH_INTERFACE_SIZE SH_IP_BUF
-
-#define SH_PORT_NOT 0
-#define SH_PORT_REQ 1
-#define SH_PORT_OPT 2
-#define SH_PORT_IGN 3
-#define SH_PORT_BLACKLIST 4
-
-static char * sh_port_type2str (int type)
-{
-  if (type == 0) return _("not");
-  if (type == 1) return _("req");
-  if (type == 2) return _("opt");
-  if (type == 3) return _("ign");
-  if (type == 4) return _("blc");
-  return _("???");
-}
-
-#define SH_PORT_MISS 0
-#define SH_PORT_ISOK 1
-#define SH_PORT_UNKN 2
-
-#define SH_PORT_NOREPT 0
-#define SH_PORT_REPORT 1
-
-#define SH_PROTO_TCP 0
-#define SH_PROTO_UDP 1
-#define SH_PROTO_STR(a) (((a) == IPPROTO_TCP) ? _("tcp") : _("udp"))
-
-struct sh_portentry {
-  int  port;
-  char interface[SH_INTERFACE_SIZE];
-  char * service;
-  char * error;
-  int  flag;    /* required or not */
-  int  status;  /* missing or not  */
-  struct sh_portentry * next;
-};
-
-static struct sh_portentry * portlist_tcp = NULL;
-static struct sh_portentry * portlist_udp = NULL;
-
-
-#define SH_PORTCHK_INTERVAL 300
-
-static int sh_portchk_check_udp  = 1;
-static int sh_portchk_active     = 1;
-static int sh_portchk_same_ports = 1;
-static int sh_portchk_transients = 1;
-static int sh_portchk_interval   = SH_PORTCHK_INTERVAL;
-
-static int sh_portchk_minport = -1;
-static int sh_portchk_maxport = -1;
-
-struct sh_port {
-  int                  port;
-  struct sh_sockaddr * paddr;
-  struct sh_port     * next;
-};
-
-static struct sh_port * blacklist_tcp = NULL;
-static struct sh_port * blacklist_udp = NULL;
-
-static struct sh_port * transient_tcp = NULL;
-static struct sh_port * transient_udp = NULL;
-
-SH_MUTEX_STATIC(mutex_port_check, PTHREAD_MUTEX_INITIALIZER);
-
-static int sh_portchk_severity  = SH_ERR_SEVERE;
-
-extern char * sh_port2proc_query(int proto, struct sh_sockaddr * saddr, int sport,
-				 unsigned long * pid, char * user, size_t userlen);
-extern int sh_port2proc_prepare();
-extern void sh_port2proc_finish();
-
-#endif
-
-/* Exported interface to add ignoreable ports as 'iface:portlist'
- */
-static int sh_portchk_add_ignore (const char * str);
-
-/* Exported interface to add required ports as 'iface:portlist'
- */
-static int sh_portchk_add_required (const char * str);
-
-/* Exported interface to add optional ports as 'iface:portlist'
- */
-static int sh_portchk_add_optional (const char * str);
-
-/* Exported interface to add blacklisted ports as 'iface:portlist'
- */
-static int sh_portchk_add_blacklist (const char * str);
-
-/* Exported interface to add an ethernet interface
- */
-static int sh_portchk_add_interface (const char * str);
-
-#if defined(HAVE_IFADDRS_H)
-/* Exported interface to add an ethernet device
- */
-static int sh_portchk_add_device (const char * str);
-#endif
-
-/* verify whether port/interface is blacklisted (do not check)
- */
-static int sh_portchk_is_blacklisted(int port, struct sh_sockaddr * haddr, int proto);
-
-/* verify whether port/interface is transient (used as source port hence no check required)
- */
-static int sh_portchk_is_transient(int port, struct sh_sockaddr * haddr, int proto);
-static int sh_portchk_transient(int port, struct sh_sockaddr * haddr, int proto);
-
-#ifndef TEST_ONLY
-
-static int sh_portchk_set_interval (const char * c)
-{
-  int retval = 0;
-  long val;
-
-  SL_ENTER(_("sh_portchk_set_interval"));
-  val = strtol (c, (char **)NULL, 10);
-  if (val <= 0)
-    {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
-		       _("port check interval"), c);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      retval = -1;
-    }
-  else
-    {
-      sh_portchk_interval = (time_t) val;
-    }
-  SL_RETURN(retval, _("sh_portchk_set_interval"));
-}
-
-static int sh_portchk_set_port_minmax (const char * c, int * setthis)
-{
-  int retval = 0;
-  long val;
-
-  SL_ENTER(_("sh_portchk_set_port_minmax"));
-  val = strtol (c, (char **)NULL, 10);
-  if (val < 0 || val > 65535)
-    {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
-		       _("port check port minmax"), c);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      retval = -1;
-    }
-  else
-    {
-      *setthis = (int) val;
-    }
-  SL_RETURN(retval, _("sh_portchk_set_port_minmax"));
-}
-
-
-static int sh_portchk_set_minport     (const char * str)
-{
-  return sh_portchk_set_port_minmax (str, &sh_portchk_minport);
-}
-
-static int sh_portchk_set_maxport     (const char * str)
-{
-  return sh_portchk_set_port_minmax (str, &sh_portchk_maxport);
-}
-
-static int sh_portchk_set_active     (const char * str)
-{
-  return sh_util_flagval(str, &sh_portchk_active);
-}
-
-static int sh_portchk_set_udp        (const char * str)
-{
-  return sh_util_flagval(str, &sh_portchk_check_udp);
-}
-#if defined(SH_ALLOW_RESTORE)
-static int sh_portchk_set_transients (const char * str)
-{
-  return sh_util_flagval(str, &sh_portchk_transients);
-}
-
-static int sh_portchk_set_same_ports (const char * str)
-{
-  return sh_util_flagval(str, &sh_portchk_same_ports);
-}
-#endif
-static int sh_portchk_set_severity   (const char * str)
-{
-  char tmp[32];
-  tmp[0] = '='; tmp[1] = '\0';
-  sl_strlcat (tmp, str, 32);
-  return sh_error_set_level (tmp, &sh_portchk_severity);
-}
-
-sh_rconf sh_portchk_table[] = {
-    {
-        N_("severityportcheck"),
-        sh_portchk_set_severity,
-    },
-    {
-        N_("portcheckrequired"),
-        sh_portchk_add_required,
-    },
-    {
-        N_("portcheckoptional"),
-        sh_portchk_add_optional,
-    },
-    {
-        N_("portcheckignore"),
-        sh_portchk_add_ignore,
-    },
-    {
-        N_("portcheckskip"),
-        sh_portchk_add_blacklist,
-    },
-    {
-        N_("portcheckactive"),
-        sh_portchk_set_active,
-    },
-#if defined(HAVE_IFADDRS_H)
-    {
-        N_("portcheckdevice"),
-        sh_portchk_add_device,
-    },
-#endif
-    {
-        N_("portcheckinterface"),
-        sh_portchk_add_interface,
-    },
-    {
-        N_("portcheckinterval"),
-        sh_portchk_set_interval,
-    },
-    {
-        N_("portcheckminport"),
-        sh_portchk_set_minport,
-    },
-    {
-        N_("portcheckmaxport"),
-        sh_portchk_set_maxport,
-    },
-    {
-        N_("portcheckudp"),
-        sh_portchk_set_udp,
-    },
-#if defined(SH_ALLOW_RESTORE)
-    {
-        N_("portchecktransients"),
-        sh_portchk_set_transients,
-    },
-    {
-        N_("portchecksameports"),
-        sh_portchk_set_same_ports,
-    },
-#endif
-    {
-        NULL,
-        NULL
-    }
-};
-
-#endif
-
-/* Interface to initialize port check
- */
-int sh_portchk_init (struct mod_type * arg);
-
-/* Interface to reset port check
- */
-int sh_portchk_reset (void);
-
-/* Interface to run port check
- */
-int sh_portchk_check (void);
-
-
-static char * check_services (int port, int proto);
-
-#ifdef TEST_ONLY
-
-static int portchk_debug = 0; 
-#define SH_ALLOC       malloc
-#define SH_FREE        free
-#define sh_util_strdup strdup
-#define sl_strlcpy     strncpy
-#define _(a)           a
-
-#else
-
-static int portchk_debug = 0;
-
-#endif
-
-#ifdef  HAVE_RPC_RPC_H
-static char * sh_getrpcbynumber (int number, char * buf, size_t len)
-{
-  FILE * fp;
-
-  if (NULL != (fp = fopen(_("/etc/rpc"), "r")))
-    {
-      sh_string * s = sh_string_new(0);
-      while (0 < sh_string_read(s, fp, 1024))
-	{
-	  char * p = sh_string_str(s);
-	  while (*p && (*p == ' ' || *p == '\t')) ++p; /* skip whitespace */
-	  if (*p == '\0' || *p == '#') 
-	    continue; /* skip comment */
-	  else
-	    {
-	      size_t lengths[3];
- 	      unsigned int  fields = 3;
- 	      char * q             = sh_string_str(s);
-	      char ** splits       = split_array_ws(q, &fields, lengths);
-
-	      if (fields >= 2)
-		{
-		  int n = atoi(splits[1]);
-		  if (n == number)
-		    {
-		      sl_strlcpy(buf, splits[0], len);
-		      SH_FREE(splits);
-		      sh_string_destroy(&s);
-		      sl_fclose(FIL__, __LINE__, fp);
-		      return buf;
-		    }
-		}
-	      SH_FREE(splits);
-	    }
-	}
-      sh_string_destroy(&s);
-      sl_fclose(FIL__, __LINE__, fp);
-    }
-  /* cppcheck-suppress resourceLeak */
-  return NULL;
-}
-#endif
-
-static char * sh_getservbyport (int port, const char * proto_in, char * buf, size_t len)
-{
-  FILE * fp;
-  char   proto[8];
-
-  sl_strlcpy(proto, proto_in, sizeof(proto));
-
-  if (NULL != (fp = fopen(_("/etc/services"), "r")))
-    {
-      sh_string * s = sh_string_new(0);
-      while (0 < sh_string_read(s, fp, 1024))
-	{
-	  char * p = sh_string_str(s);
-	  while (*p && (*p == ' ' || *p == '\t')) ++p; /* skip whitespace */
-	  if (*p == '\0' || *p == '#')
-	    continue; /* skip comment */
-	  else
-	    {
-	      size_t lengths[3];
- 	      unsigned int  fields = 3;
- 	      char * q             = sh_string_str(s);
-	      char ** splits       = split_array_ws(q, &fields, lengths);
-
-	      if (fields >= 2)
-		{
-		  char * end;
-		  long n = strtol(splits[1], &end, 10);
-		  if (n == port && end && (*end == '/' || *end == ','))
-		    {
-		      ++end;
-		      if (0 == strcmp(end, proto))
-			{
-			  sl_strlcpy(buf, splits[0], len);
-			  SH_FREE(splits);
-			  sh_string_destroy(&s);
-			  sl_fclose(FIL__, __LINE__, fp);
-			  return buf;
-			}
-		    }
-		}
-	      SH_FREE(splits);
-	    }
-	}
-      sh_string_destroy(&s);
-      sl_fclose(FIL__, __LINE__, fp);
-    }
-  /* cppcheck-suppress resourceLeak */
-  return NULL;
-}
-
-static void sh_portchk_add_to_list (int proto, 
-				    int port, struct sh_sockaddr * paddr, 
-				    char * service,
-				    int flag, int status)
-{
-  struct sh_portentry * new = SH_ALLOC (sizeof(struct sh_portentry));
-
-  new->port = port;
-  sh_ipvx_ntoa(new->interface, SH_INTERFACE_SIZE, paddr);
-  new->status = status;
-  new->flag   = flag;
-
-  new->error  = NULL;
-
-  if (portchk_debug)
-    fprintf(stderr, _("add to list: port %d/%s %d %d (%s) %s\n"),
-	    port, SH_PROTO_STR(proto), flag, status, service ? service : _("undef"),
-	    new->interface);
-
-  if (service)
-    new->service = sh_util_strdup (service);
-  else
-    new->service = NULL;
-  if (proto == IPPROTO_TCP)
-    {
-      new->next = portlist_tcp;
-      portlist_tcp = new;
-    }
-  else
-    {
-      new->next = portlist_udp;
-      portlist_udp = new;
-    }
-  return;
-}
-
-/* Reset the list by setting all entries to UNKN.
- * In the next cycle we will check, and set found ports to ISOK.
- * Thereafter, we check for entries that are still UNKN.
- */
-static void sh_portchk_reset_lists (void)
-{
-  struct sh_portentry * portlist;
-
-  portlist = portlist_tcp;
-  while (portlist)
-    {
-      if (portlist->status != SH_PORT_MISS)
-	portlist->status = SH_PORT_UNKN;
-      portlist = portlist->next;
-    }
-  portlist = portlist_udp;
-  while (portlist)
-    {
-      if (portlist->status != SH_PORT_MISS)
-	portlist->status = SH_PORT_UNKN;
-      portlist = portlist->next;
-    }
-  return;
-}
-
-static struct sh_portentry * sh_portchk_kill_list (struct sh_portentry * head)
-{
-  if (head)
-    {
-      if (head->next)
-	sh_portchk_kill_list (head->next);
-
-      if (head->service)
-	SH_FREE(head->service);
-      SH_FREE(head);
-    }
-  return NULL;
-}
-  
-static struct sh_port * sh_portchk_kill_blacklist (struct sh_port * head)
-{
-  if (head)
-    {
-      if (head->next)
-	sh_portchk_kill_blacklist (head->next);
-
-      SH_FREE(head->paddr);
-      SH_FREE(head);
-    }
-  return NULL;
-}
-  
-static struct sh_port * sh_portchk_kill_transient (struct sh_port * head)
-{
-  if (head)
-    {
-      if (head->next)
-	sh_portchk_kill_transient (head->next);
-
-      SH_FREE(head->paddr);
-      SH_FREE(head);
-    }
-  return NULL;
-}
-  
-/* These variables are not used anywhere. They only exist
- * to assign &pre, &ptr to them, which keeps gcc from
- * putting it into a register, and avoids the 'clobbered
- * by longjmp' warning. And no, 'volatile' proved insufficient.
- */
-void * sh_dummy_531_pre = NULL;
-void * sh_dummy_532_ptr = NULL;
-
-/* check the list of open ports for any that are marked as UNKN
- */
-static void sh_portchk_check_list (struct sh_portentry ** head, 
-				   int proto, int report)
-{
-  struct sh_portentry * ptr = *head;
-  struct sh_portentry * pre = *head;
-  char errbuf[512];
-
-  /* Take the address to keep gcc from putting them into registers. 
-   * Avoids the 'clobbered by longjmp' warning. 
-   */
-  sh_dummy_531_pre = (void*) &pre;
-  sh_dummy_532_ptr = (void*) &ptr;
- 
-  while (ptr)
-    {
-      if (portchk_debug && report)
-	fprintf(stderr, _("check list: port %d/%s %d %d\n"),
-		ptr->port, SH_PROTO_STR(proto), ptr->flag, ptr->status);
-
-      if (ptr->status == SH_PORT_UNKN)
-	{
-	  /* Don't report missing ports that are marked as optional
-	   */
-	  if (ptr->flag != SH_PORT_OPT && ptr->flag != SH_PORT_IGN)
-	    {
-	      snprintf (errbuf, sizeof(errbuf), _("port: %s:%d/%s (%s)"), 
-			ptr->interface, ptr->port, SH_PROTO_STR(proto), 
-			ptr->service ? ptr->service : check_services(ptr->port, proto));
-#ifdef TEST_ONLY
-	      if (report == SH_PORT_REPORT)
-		fprintf(stderr, _("%s\n"), errbuf);
-#else
-	      if (report == SH_PORT_REPORT)
-		{
-		  SH_MUTEX_LOCK(mutex_thread_nolog);
-		  sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0, 
-				  MSG_PORT_MISS, errbuf);
-		  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-		}
-#endif
-	    }
-
-	  ptr->status = SH_PORT_MISS;
-
-	  if ((ptr->flag != SH_PORT_REQ) && (ptr->flag != SH_PORT_OPT) && (ptr->flag != SH_PORT_IGN))
-	    {
-	      if (portchk_debug && report)
-		fprintf(stderr, _("removing: port %d/%s %d %d\n"),
-			ptr->port, SH_PROTO_STR(proto), ptr->flag, ptr->status);
-	      
-	      if (ptr == *head)
-		{
-		  *head = ptr->next;
-		  if (ptr->service)
-		    SH_FREE(ptr->service);
-		  SH_FREE(ptr);
-		  ptr = *head;
-		  pre = *head;
-		  continue;
-		}
-	      else if (ptr->next == NULL)
-		{
-		  pre->next = NULL;
-		  if (ptr->service)
-		    SH_FREE(ptr->service);
-		  SH_FREE(ptr);
-		  return;
-		}
-	      else
-		{
-		  pre->next = ptr->next;
-		  if (ptr->service)
-		    SH_FREE(ptr->service);
-		  SH_FREE(ptr);
-		  ptr = pre->next;
-		  continue;
-		}
-	    }
-	}
-      pre = ptr;
-      ptr = ptr->next;
-    }
-
-  sh_dummy_532_ptr = NULL;
-  sh_dummy_531_pre = NULL;
-
-  return;
-}
-
-
-static struct sh_portentry * sh_portchk_get_from_list (int proto, int port, 
-						       struct sh_sockaddr * paddr, char * service)
-{
-  struct sh_portentry * portlist;
-  char str_addr[SH_IP_BUF];
-
-
-  if (proto == IPPROTO_TCP)
-    portlist = portlist_tcp;
-  else
-    portlist = portlist_udp;
-
-  sh_ipvx_ntoa(str_addr, sizeof(str_addr), paddr);
-
-  if (service)
-    {
-      while (portlist) 
-	{
-	  if (portlist->service && 
-	      0 == strcmp(service, portlist->service) &&
-	      ( 0 == strcmp(portlist->interface, str_addr) ||
-	        sh_ipvx_isany(paddr) ))
-	    return portlist;
-	  portlist = portlist->next;
-	}
-    }
-  else
-    {
-      while (portlist) 
-	{
-	  if (port == portlist->port &&
-	      (0 == strcmp(portlist->interface, str_addr)  ||
-	       sh_ipvx_isany(paddr) ))
-	    return portlist;
-	  portlist = portlist->next;
-	}
-    }
-  return NULL;
-}
-      
-
-static void sh_portchk_cmp_to_list (int proto, int port, 
-				    struct sh_sockaddr * paddr, char * service)
-{
-  struct sh_portentry * portent;
-  char errbuf[512];
-
-  
-  portent = sh_portchk_get_from_list (proto, port, paddr, service);
-
-  if (service)
-    {
-      if (!portent)
-	{
-	  char * path;
-	  unsigned long qpid;
-	  char   user[USER_MAX];
-	  char   saddr[SH_IP_BUF];
-
-	  sh_ipvx_ntoa(saddr, sizeof(saddr), paddr);
-
-	  snprintf (errbuf, sizeof(errbuf), _("port: %s:%d/%s (%s)"), 
-		    saddr, port, SH_PROTO_STR(proto), service);
-
-	  if (portchk_debug)
-	    fprintf(stderr, _("cmp_to_list: open port: %s:%d/%s (%s)\n"), 
-		    saddr, port, SH_PROTO_STR(proto), service);
-
-#ifndef TEST_ONLY
-	  path = sh_port2proc_query(proto, paddr, port, &qpid, user, sizeof(user));
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0, 
-			  MSG_PORT_NEW, errbuf, path, qpid, user);
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  SH_FREE(path);
-#endif
-	  /* 
-	   * was not there, thus it is not in 'required' or 'optional' list
-	   */
-	  sh_portchk_add_to_list (proto, port, paddr, service, SH_PORT_NOT, SH_PORT_ISOK);
-	}
-      else if (portent->status == SH_PORT_MISS && portent->flag != SH_PORT_IGN)
-	{
-	  char * path;
-	  unsigned long qpid;
-	  char   user[USER_MAX];
-	  char   saddr[SH_IP_BUF];
-
-	  sh_ipvx_ntoa(saddr, sizeof(saddr), paddr);
-
-	  snprintf (errbuf, sizeof(errbuf), _("port: %s:%d/%s (%s), was %d/%s"), 
-		    saddr, port, SH_PROTO_STR(proto), service, portent->port, SH_PROTO_STR(proto));
-#ifdef TEST_ONLY
-	  fprintf(stderr, _("service: %s\n"), errbuf);
-#else
-	  path = sh_port2proc_query(proto, paddr, port, &qpid, user, sizeof(user));
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0, 
-			  MSG_PORT_RESTART, errbuf, path, qpid, user);
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  SH_FREE(path);
-#endif
-
-	  portent->status = SH_PORT_ISOK;
-	}
-      else if (port != portent->port && (-1) != portent->port)
-	{
-	  char * path;
-	  unsigned long qpid;
-	  char   user[USER_MAX];
-	  char   saddr[SH_IP_BUF];
-
-	  sh_ipvx_ntoa(saddr, sizeof(saddr), paddr);
-
-	  snprintf (errbuf, sizeof(errbuf), _("port: %s:%d/%s (%s), was %d/%s"), 
-		    saddr, port, SH_PROTO_STR(proto), service, portent->port, SH_PROTO_STR(proto));
-#ifdef TEST_ONLY
-	  fprintf(stderr, _("service: %s\n"), errbuf);
-#else
-	  path = sh_port2proc_query(proto, paddr, port, &qpid, user, sizeof(user));
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0, 
-			  MSG_PORT_NEWPORT, errbuf, path, qpid, user);
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  SH_FREE(path);
-#endif
-	  portent->port   = port;
-	  portent->status = SH_PORT_ISOK;
-	}
-      else
-	{
-	  portent->status = SH_PORT_ISOK;
-	}
-    }
-  else
-    {
-      if (!portent)
-	{
-	  char * path;
-	  unsigned long qpid;
-	  char   user[USER_MAX];
-	  char   saddr[SH_IP_BUF];
-
-	  if (portchk_debug)
-	    fprintf(stderr, _("call to  sh_ipvx_ntoa (port %d)\n"), port);
- 
-	  sh_ipvx_ntoa(saddr, sizeof(saddr), paddr);
-
-	  snprintf (errbuf, sizeof(errbuf), _("port: %s:%d/%s (%s)"), 
-		    saddr, port, SH_PROTO_STR(proto), check_services(port, proto));
-
-	  if (portchk_debug)
-	    fprintf(stderr, _("cmp_to_list: open port: %s:%d/%s (%s) check_services\n"), 
-		    saddr, port, SH_PROTO_STR(proto), check_services(port, proto));
-
-#ifndef TEST_ONLY
-	  path = sh_port2proc_query(proto, paddr, port, &qpid, user, sizeof(user));
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0, 
-			  MSG_PORT_NEW, errbuf, path, qpid, user);
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  SH_FREE(path);
-#endif
-
-	  /* was not there, thus it is not in 'required' or 'optional' list
-	   */
-	  sh_portchk_add_to_list (proto, port, paddr, service, SH_PORT_NOT, SH_PORT_ISOK);
-	}
-      else if (portent->status == SH_PORT_MISS && portent->flag != SH_PORT_IGN)
-	{
-	  char * path;
-	  unsigned long qpid;
-	  char   user[USER_MAX];
-	  char   saddr[SH_IP_BUF];
-
-	  sh_ipvx_ntoa(saddr, sizeof(saddr), paddr);
-
-	  snprintf (errbuf, sizeof(errbuf), _("port: %s:%d/%s (%s)"), 
-		    saddr, port, SH_PROTO_STR(proto), check_services(port, proto));
-#ifdef TEST_ONLY
-	  fprintf(stderr, _("port   : %s\n"), errbuf);
-#else
-	  path = sh_port2proc_query(proto, paddr, port, &qpid, user, sizeof(user));
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle(sh_portchk_severity, FIL__, __LINE__, 0, 
-			  MSG_PORT_RESTART, errbuf, path, qpid, user);
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  SH_FREE(path);
-#endif
-
-	  portent->status = SH_PORT_ISOK;
-	}
-      else
-	{
-	  portent->status = SH_PORT_ISOK;
-	}
-    }
-
-  return;
-}
-
-			       
-/* Returns a static buffer containing the name of the service
- * running on port <port> (from /etc/services)
- * Returns NULL on failure
- */
-static char * check_services (int port, int proto)
-{
-  static char buf[256];
-  char * service = sh_getservbyport(port, SH_PROTO_STR(proto), buf, sizeof(buf));
-
-  if (!service)
-    {
-      snprintf (buf, sizeof(buf), "%s",_("unknown"));
-    }
-  return buf;
-}
-
-/* Returns a static buffer containing the name of the service
- * running on port <port> at <address> (from portmap daemon)
- * Returns NULL on failure
- */
-static char * check_rpc_list (int port, struct sockaddr_in * address, 
-			      unsigned long prot)
-{
-#if defined(HAVE_RPC_RPC_H) && defined(HAVE_PMAP_GETMAPS)
-  struct pmaplist * head;
-  char *r;
-  static char buf[256];
-
-  head = pmap_getmaps(address);
-
-  if (head) 
-    {
-      do /* while (head != NULL) */
-	{
-	  if ((head->pml_map.pm_prot == prot) && 
-	      (port == (int)head->pml_map.pm_port)) 
-	    {
-	      r = sh_getrpcbynumber((int)head->pml_map.pm_prog, 
-				    buf, sizeof(buf));
-	      if (r)
-		{
-		  return buf;
-		}
-	      else
-		{
-		  snprintf (buf, sizeof(buf), "RPC_%lu",
-			    (unsigned long)head->pml_map.pm_prog);
-		  return buf;
-		}
-	    }
-	  head = head->pml_next;
-	}
-      while (head != NULL);
-    }
-#else
-  (void) port;
-  (void) address;
-  (void) prot;
-#endif
-  return NULL;
-}
-
-void * sh_dummy_950_p = NULL;
-void * sh_dummy_951_p = NULL;
-
-static int check_port_udp_internal (int fd, int port, struct sh_sockaddr * paddr)
-{
-  int                retval;
-  char             * p = NULL;
-  char               buf[8] = { 0 };
-#ifndef TEST_ONLY
-  char               errmsg[256];
-  volatile int       nerr;
-#endif
-  char errbuf[SH_ERRBUF_SIZE];
-  char ipbuf[SH_IP_BUF];
-
-  struct sh_sockaddr	saddr;
-  socklen_t		slen  = 0;
-  volatile int		sport = 0;
-
-  sh_dummy_950_p = (void*) &p;
-  
-  sh_ipvx_set_port(paddr, port);
-
-  do {
-    retval = connect(fd, sh_ipvx_sockaddr_cast(paddr), SH_SSP_LEN(paddr));
-  } while (retval < 0 && (errno == EINTR || errno == EINPROGRESS));
-
-  if (retval == -1)
-    {
-#ifdef TEST_ONLY
-      if (portchk_debug)
-	perror(_("connect"));
-#else
-      sh_ipvx_ntoa(ipbuf, sizeof(ipbuf), paddr);
-
-      nerr = errno;
-      sl_snprintf(errmsg, sizeof(errmsg), _("check port: %5d/udp on %15s: %s"),
-		  port, ipbuf, sh_error_message(errno, errbuf, sizeof(errbuf)));
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle((-1), FIL__, __LINE__, nerr, MSG_E_SUBGEN, 
-		      errmsg, _("connect"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-#endif
-    }
-  else
-    {
-      /* Register the used source port as transient. This will avoid
-       * the issue of lingering source ports being reported as a spurious
-       * service. The lingering source port is effectvely a race condition.
-       *
-       * Also use this code to obtain the source port. Sometimes Samhain
-       * reports on a port where it connects back to itself. In that case
-       * source and destination port are the same.
-       *
-       * AGH, 23 Apr 2017 (www.2024sight.com).
-       */
-
-#if defined(USE_IPVX)
-      if (paddr->ss_family == AF_INET)
-        {
-          saddr.ss_family = AF_INET;
-          slen = sizeof( struct sockaddr_in );
-          retval = getsockname(fd, (struct sockaddr *)&(saddr.sin), &slen);
-        }
-      else
-        {
-          saddr.ss_family = AF_INET6;
-          slen = sizeof( struct sockaddr_in6 );
-          retval = getsockname(fd, (struct sockaddr *)&(saddr.sin6), &slen);
-        }
-#else
-      saddr.ss_family = AF_INET;
-      slen = sizeof( struct sockaddr_in );
-      retval = getsockname(fd, (struct sockaddr *)&(saddr.sin), &slen);
-#endif
-
-      if ( retval == 0 )
-        {
-          sport = sh_ipvx_get_port(&saddr);
-          sh_portchk_transient(sport, &saddr, IPPROTO_UDP);
-        }
-      else
-        {
-#ifdef TEST_ONLY
-          if (portchk_debug)
-            perror(_("getsockname"));
-#else
-            sh_ipvx_ntoa(ipbuf, sizeof(ipbuf), paddr);
-            nerr = errno;
-            sl_snprintf(errmsg, sizeof(errmsg), _("source port transient for %15s:%d/udp: %s"),
-		            ipbuf, port, sh_error_message(errno, errbuf, sizeof(errbuf)));
-            SH_MUTEX_LOCK(mutex_thread_nolog);
-            sh_error_handle((-1), FIL__, __LINE__, nerr, MSG_E_SUBGEN, errmsg, _("getsockname"));
-            SH_MUTEX_UNLOCK(mutex_thread_nolog);
-#endif
-        }
-
-      if (( sport != port ) || ( sh_portchk_same_ports == S_FALSE ))
-        {
-          do {
-	    retval = send (fd, buf, 0, 0);
-          } while (retval < 0 && errno == EINTR);
-
-          if (retval == -1 && errno == ECONNREFUSED)
-	    {
-	      sh_ipvx_ntoa(ipbuf, sizeof(ipbuf), paddr);
-	      if (portchk_debug)
-	        fprintf(stderr, _("check port_udp: %5d/udp on %15s established/time_wait\n"),
-		        port, ipbuf);
-	    }
-          else 
-	    {
-	      /* Only the second send() may catch the error 
-	       */
-	      do {
-	        retval = send (fd, buf, 0, 0);
-	      } while (retval < 0 && errno == EINTR);
-
-	      if (retval == -1 && errno == ECONNREFUSED)
-	        {
-	          sh_ipvx_ntoa(ipbuf, sizeof(ipbuf), paddr);
-	          if (portchk_debug)
-		    fprintf(stderr, _("check port: %5d/udp on %15s established/time_wait\n"),
-			    port, ipbuf);
-	        }
-	      else if (retval != -1)
-	        {
-	          /* Try to get service name from portmap
-	           */
-	          if (paddr->ss_family == AF_INET)
-		    {
-		      p = check_rpc_list (port, 
-				          (struct sockaddr_in *) sh_ipvx_sockaddr_cast(paddr), 
-				          IPPROTO_UDP);
-		    }
-
-	          sh_portchk_cmp_to_list (IPPROTO_UDP, port, paddr, p ? p : NULL);
-	      
-	          /* If not an RPC service, try to get name from /etc/services
-	           */
-	          if (!p)
-		    p = check_services(port, IPPROTO_UDP);
-	      
-	          if (portchk_debug)
-		    {
-		      sh_ipvx_ntoa(ipbuf, sizeof(ipbuf), paddr);
-		      fprintf(stderr, _("check port_udp: %5d/udp on %15s open %s\n"), 
-			      port, ipbuf, p);
-		    }
-	      
-	        }
-	      else
-	        {
-	          if (portchk_debug)
-		    {
-		      sh_ipvx_ntoa(ipbuf, sizeof(ipbuf), paddr);
-		      fprintf(stderr, _("check port_udp: %5d/udp on %15s ERRNO %d\n"), 
-			      port, ipbuf, errno);
-		    }
-	        }
-	    }
-        }
-      else
-        {
-	  if (portchk_debug)
-            {
-              sh_ipvx_ntoa(ipbuf, sizeof(ipbuf), paddr);
-	      fprintf(stderr, _("check port_udp: %5d/udp on %15s same source and destination port\n"), 
-		      port, ipbuf);
-            }
-        }
-    }
-  sl_close_fd (FIL__, __LINE__, fd);
-  sh_dummy_950_p = NULL; 
-  return 0;
-}
-
-static int check_port_tcp_internal (int fd, int port, struct sh_sockaddr * paddr)
-{
-  volatile int       retval;
-  int                flags;
-  char             * p = NULL;
-#ifndef TEST_ONLY
-  char               errmsg[256];
-  volatile int       nerr;
-#endif
-  char errbuf[SH_ERRBUF_SIZE];
-  char ipbuf[SH_IP_BUF];
-
-  struct sh_sockaddr	saddr;
-  socklen_t		slen  = 0;
-  volatile int		sport = 0;
-
-  sh_dummy_951_p = (void*) &p;
-
-  sh_ipvx_set_port(paddr, port);
-
-  do {
-    retval = connect(fd, sh_ipvx_sockaddr_cast(paddr), SH_SSP_LEN(paddr));
-  } while (retval < 0 && (errno == EINTR || errno == EINPROGRESS));
-
-  if (retval == -1 && errno == ECONNREFUSED)
-    {
-      if (portchk_debug)
-	{
-	  sh_ipvx_ntoa(ipbuf, sizeof(ipbuf), paddr);
-	  fprintf(stderr, _("check port_tcp: %5d on %15s closed\n"),
-		  port, ipbuf);
-	}
-    }
-  else if (retval == -1)
-    {
-#ifdef TEST_ONLY
-      if (portchk_debug)
-	perror(_("connect"));
-#else
-      sh_ipvx_ntoa(ipbuf, sizeof(ipbuf), paddr);
-      nerr = errno;
-      sl_snprintf(errmsg, sizeof(errmsg), _("check port: %5d/tcp on %15s: %s"),
-		  port, ipbuf, sh_error_message(errno, errbuf, sizeof(errbuf)));
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle((-1), FIL__, __LINE__, nerr, MSG_E_SUBGEN, 
-		      errmsg, _("connect"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-#endif
-    }
-  else
-    {
-      /* Register the used source port as transient. This will avoid
-       * the issue of lingering source ports being reported as a spurious
-       * service. The lingering source port is effectively a race condition.
-       *
-       * Also use this code to obtain the source port. Sometimes Samhain
-       * reports on a port where it connects back to itself. In that case
-       * source and destination port are the same.
-       *
-       * AGH, 23 Apr 2017 (www.2024sight.com).
-       */
-
-#if defined(USE_IPVX)
-      if (paddr->ss_family == AF_INET)
-        {
-          saddr.ss_family = AF_INET;
-          slen = sizeof( struct sockaddr_in );
-          retval = getsockname(fd, (struct sockaddr *)&(saddr.sin), &slen);
-        }
-      else
-        {
-          saddr.ss_family = AF_INET6;
-          slen = sizeof( struct sockaddr_in6 );
-          retval = getsockname(fd, (struct sockaddr *)&(saddr.sin6), &slen);
-        }
-#else
-      saddr.ss_family = AF_INET;
-      slen = sizeof( struct sockaddr_in );
-      retval = getsockname(fd, (struct sockaddr *)&(saddr.sin), &slen);
-#endif
-
-      if ( retval == 0 )
-        {
-          sport = sh_ipvx_get_port(&saddr);
-          sh_portchk_transient(sport, &saddr, IPPROTO_TCP);
-        }
-      else
-        {
-#ifdef TEST_ONLY
-          if (portchk_debug)
-	    perror(_("getsockname"));
-#else
-            sh_ipvx_ntoa(ipbuf, sizeof(ipbuf), paddr);
-            nerr = errno;
-            sl_snprintf(errmsg, sizeof(errmsg), _("source port transient for %15s:%d/tcp: %s"),
-		        ipbuf, port, sh_error_message(errno, errbuf, sizeof(errbuf)));
-            SH_MUTEX_LOCK(mutex_thread_nolog);
-            sh_error_handle((-1), FIL__, __LINE__, nerr, MSG_E_SUBGEN, 
-		            errmsg, _("getsockname"));
-            SH_MUTEX_UNLOCK(mutex_thread_nolog);
-#endif
-        }
-
-      if (( sport != port ) || ( sh_portchk_same_ports == S_FALSE ))
-        {
-          /* Try to get service name from portmap
-           */
-          if (paddr->ss_family == AF_INET)
-	    {
-	      p = check_rpc_list (port, 
-			          (struct sockaddr_in *) sh_ipvx_sockaddr_cast(paddr), 
-			          IPPROTO_TCP);
-	    }
-
-          sh_portchk_cmp_to_list (IPPROTO_TCP, port, paddr, p ? p : NULL);
-
-          /* If not an RPC service, try to get name from /etc/services
-           */
-          if (!p)
-	    p = check_services(port, IPPROTO_TCP);
-
-          if (portchk_debug)
-	    {
-	      sh_ipvx_ntoa(ipbuf, sizeof(ipbuf), paddr);
-	      fprintf(stderr, _("check port_tcp: %5d on %15s open %s\n"), 
-		      port, ipbuf, p);
-	    }
-        }
-      else
-        {
-	  if (portchk_debug)
-            {
-              sh_ipvx_ntoa(ipbuf, sizeof(ipbuf), paddr);
-	      fprintf(stderr, _("check port_udp: %5d/tcp on %15s same source and destination port\n"), 
-		      port, ipbuf);
-            }
-        }
-
-#if !defined(O_NONBLOCK)
-#if defined(O_NDELAY)
-#define O_NONBLOCK  O_NDELAY
-#else
-#define O_NONBLOCK  0
-#endif
-#endif
-
-      /* prepare to close connection gracefully
-       */
-      if      (port == 22)  /* ssh */
-	{
-	  flags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
-	  retry_fcntl(FIL__, __LINE__, fd, F_SETFL, flags | O_NONBLOCK);
-	  retval = write (fd, _("SSH-2.0-Foobar"), 14);
-	  if (retval > 0) retval = write (fd, "\r\n", 2);
-	}
-      else if (port == 25)  /* smtp */
-	{
-	  flags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
-	  retry_fcntl(FIL__, __LINE__, fd, F_SETFL, flags | O_NONBLOCK);
-	  retval = write (fd, _("QUIT"), 4);
-	  if (retval > 0) retval = write (fd, "\r\n", 2);
-	}
-      else if (port == 79)  /* finger */
-	{
-	  flags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
-	  retry_fcntl(FIL__, __LINE__, fd, F_SETFL, flags | O_NONBLOCK);
-	  retval = write (fd, "\r\n", 2);
-	}
-      else if (port == 110) /* pop3 */
-	{
-	  flags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
-	  retry_fcntl(FIL__, __LINE__, fd, F_SETFL, flags | O_NONBLOCK);
-	  retval = write (fd, _("QUIT"), 4);
-	  if (retval > 0) retval = write (fd, "\r\n", 2);
-	}
-      else if (port == 143) /* imap */
-	{
-	  flags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
-	  retry_fcntl(FIL__, __LINE__, fd, F_SETFL, flags | O_NONBLOCK);
-	  retval = write (fd, _("A01 LOGOUT"), 10);
-	  if (retval > 0) retval = write (fd, "\r\n", 2);
-	}
-
-      if (portchk_debug && retval < 0)
-	fprintf(stderr, _("check port: error writing to port %5d\n"), 
-		port);
-     }
-  sl_close_fd (FIL__, __LINE__, fd);
-  sh_dummy_951_p = NULL;
-  return 0;
-}
-
-/* typedef uint32_t in_addr_t;
- * struct in_addr
- * {
- * in_addr_t s_addr;
- * };
- */
-
-#define SH_IFACE_MAX 64
-#define SH_IFACE_ADDR 0
-#define SH_IFACE_DEV  1
-
-struct portchk_interfaces {
-  struct sh_sockaddr iface;
-  int            type;
-};
-
-static struct portchk_interfaces iface_list[SH_IFACE_MAX];
-static int iface_list_used   = 0;
-static int iface_initialized = 0;
-
-#ifdef TEST_ONLY
-static char * portchk_hostname = NULL;
-#else
-static char * portchk_hostname = sh.host.name;
-#endif
-
-static int sh_portchk_init_internal (void)
-{
-  volatile int     i, j; /* might be clobbered by âlongjmpâ or âvforkâ*/
-  char errbuf[256];
-#if defined(USE_IPVX)
-  struct addrinfo hints;
-  struct addrinfo *res;
-#else
-  struct hostent * hent;
-#endif
-  char ipbuf[SH_IP_BUF];
-
-  if (portchk_debug)
-    fprintf(stderr, _("checking ports on: %s\n"), portchk_hostname ? portchk_hostname : _("NULL"));
-
-  if (!portchk_hostname)
-    return -1;
-
-  if (sh_portchk_active == S_FALSE)
-    return -1;
-
-  SH_MUTEX_LOCK(mutex_port_check);
-  if (iface_initialized == 0)
-    {
-      iface_list_used   = 0;
-      iface_initialized = 1;
-    }
-
-#if !defined(USE_IPVX)
-  SH_MUTEX_LOCK(mutex_resolv);
-  hent = sh_gethostbyname(portchk_hostname);
-  i = 0;
-  while (hent && hent->h_addr_list[i] && (iface_list_used < SH_IFACE_MAX))
-    {
-      struct sockaddr_in sin;
-      struct sh_sockaddr iface_tmp;
-
-      memcpy(&(sin.sin_addr.s_addr), hent->h_addr_list[i], sizeof(in_addr_t));
-      sh_ipvx_save(&iface_tmp, AF_INET, (struct sockaddr *)&sin);
-
-      for (j = 0; j < iface_list_used; ++j)
-	{
-	  if (0 == sh_ipvx_cmp(&iface_tmp, &(iface_list[j].iface)))
-	    {
-	      goto next_iface;
-	    }
-	}
-
-      sh_ipvx_save(&(iface_list[iface_list_used].iface), 
-		   AF_INET, (struct sockaddr *)&sin);
-      iface_list[iface_list_used].type = SH_IFACE_ADDR;
-      
-      if (portchk_debug)
-	{
-	  char buf[256];
-	  sh_ipvx_ntoa(buf, sizeof(buf), &(iface_list[iface_list_used].iface));
-	  fprintf(stderr, _("added interface[%d]: %s\n"), i, buf); 
-	}
-      ++iface_list_used;
-
-    next_iface:
-      ++i;
-    }
-  SH_MUTEX_UNLOCK(mutex_resolv);
-#else
-  memset(&hints, 0, sizeof(hints));
-  hints.ai_family = PF_UNSPEC;
-  hints.ai_flags  = AI_ADDRCONFIG;
-
-  if (0 == getaddrinfo(portchk_hostname, NULL, &hints, &res))
-    {
-      struct addrinfo *p = res;
-      struct sh_sockaddr iface_tmp;
-
-      while ((p != NULL) && (iface_list_used < SH_IFACE_MAX))
-	{
-	  sh_ipvx_save(&iface_tmp, p->ai_family, p->ai_addr);
-
-          for (j = 0; j < iface_list_used; ++j)
-	    {
-	      if (portchk_debug)
-		{
-		  char buf1[256], buf2[256];
-		  sh_ipvx_ntoa(buf1, sizeof(buf1), &(iface_list[j].iface));
-		  sh_ipvx_ntoa(buf2, sizeof(buf2), &iface_tmp);
-		  fprintf(stderr, _("check interface[%d]: %s vs %s\n"), j, buf1, buf2); 
-		}
-	      if (0 == sh_ipvx_cmp(&iface_tmp, &(iface_list[j].iface)))
-		{
-		  if (portchk_debug) 
-		    fprintf(stderr, _("skipping interface[%d]\n"), j);
-		  goto next_iface;
-		}
-	    }
-	  sh_ipvx_save(&(iface_list[iface_list_used].iface), 
-		       p->ai_family, p->ai_addr);
-	  iface_list[iface_list_used].type = SH_IFACE_ADDR;
-	  if (portchk_debug)
-	    {
-	      char buf[256];
-	      sh_ipvx_ntoa(buf, sizeof(buf), &(iface_list[iface_list_used].iface));
-	      fprintf(stderr, _("added interface[%d]: %s\n"), iface_list_used, buf); 
-	    }
-
-	  ++iface_list_used;
-
-	next_iface:
-	  p = p->ai_next;
-	}
-      freeaddrinfo(res);
-    } 
-#endif
-
-  for (i = 0; i < iface_list_used; ++i)
-    {
-      sh_ipvx_ntoa(ipbuf, sizeof(ipbuf), &(iface_list[i].iface));
-      sl_snprintf(errbuf, sizeof(errbuf), _("added interface: %s"), ipbuf);
-
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-		      errbuf, _("sh_portchk_init"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-    }
-  SH_MUTEX_UNLOCK(mutex_port_check);
-
-  return 0;
-}
-
-int sh_portchk_init (struct mod_type * arg)
-{
-#ifndef HAVE_PTHREAD
-  (void) arg;
-#endif
-
-  if (sh_portchk_active == S_FALSE)
-    return SH_MOD_FAILED;
-  if (!portchk_hostname)
-    return SH_MOD_FAILED;
-
-#ifdef HAVE_PTHREAD
-  if (arg != NULL && arg->initval < 0 &&
-      (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
-    {
-      if (0 == sh_pthread_create(sh_threaded_module_run, (void *)arg))
-	return SH_MOD_THREAD;
-      else
-	return SH_MOD_FAILED;
-    }
-  else if (arg != NULL && arg->initval == SH_MOD_THREAD &&
-	   (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
-    {
-      (void) sh_portchk_init_internal();
-      return SH_MOD_THREAD;
-    }
-#endif
-  return sh_portchk_init_internal();
-}
-
-static void dev_list_kill();
-
-#if !defined(TEST_ONLY)
-int sh_portchk_reconf (void)
-{
-  SH_MUTEX_LOCK(mutex_port_check);
-  iface_initialized    = 0;
-  sh_portchk_active    = 1;
-  sh_portchk_check_udp = 1;
-  sh_portchk_interval  = SH_PORTCHK_INTERVAL;
-
-  sh_portchk_minport = -1;
-  sh_portchk_maxport = -1;
-
-  dev_list_kill();
-  
-  portlist_udp = sh_portchk_kill_list (portlist_udp);
-  portlist_tcp = sh_portchk_kill_list (portlist_tcp);
-
-  blacklist_udp = sh_portchk_kill_blacklist (blacklist_udp);
-  blacklist_tcp = sh_portchk_kill_blacklist (blacklist_tcp);
-  sh_port2proc_finish();
-
-  SH_MUTEX_UNLOCK(mutex_port_check);
-  return 0;
-}
-
-int sh_portchk_cleanup (void)
-{
-  return sh_portchk_reconf ();
-}
-
-int sh_portchk_timer (time_t tcurrent) 
-{
-  static time_t lastcheck = 0;
-
-  SL_ENTER(_("sh_portchk_timer"));
-  if ((time_t) (tcurrent - lastcheck) >= sh_portchk_interval)
-    {
-      lastcheck  = tcurrent;
-      SL_RETURN((-1), _("sh_portchk_timer"));
-    }
-  SL_RETURN(0, _("sh_portchk_timer"));
-}
-#endif
-
-static int check_port_generic (int port, int domain, int type, int protocol)
-{
-  volatile int     i    =  0;
-  int              sock = -1;
-  int              flag =  1; /* non-zero to enable an option */
-  struct sh_sockaddr   paddr;
-  char errbuf[SH_ERRBUF_SIZE];
-
-  /* Check all interfaces for this host
-   */
-  while (i < iface_list_used)
-    {
-      memcpy(&paddr, &(iface_list[i].iface), sizeof(paddr));
-
-      if (paddr.ss_family != domain)
-	{
-	  ++i;
-	  continue;
-	}
-
-      if (0 != sh_portchk_is_blacklisted(port, &paddr, protocol))
-	{
-	  ++i; 
-	  continue;
-	}
-
-      if (0 != sh_portchk_is_transient(port, &paddr, protocol))
-	{
-	  ++i; 
-	  continue;
-	}
-
-      if ((sock = socket(paddr.ss_family, type, protocol)) < 0 )
-	{
-	  ++i;
-#ifdef TEST_ONLY
-	  if (portchk_debug)
-	    perror(_("socket"));
-#else
-
-#ifndef EPROTONOSUPPORT
-#define EPROTONOSUPPORT 0
-#endif
-#ifndef EAFNOSUPPORT
-#define EAFNOSUPPORT    0
-#endif
-	  if (errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT)
-	    {
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN, 
-			      sh_error_message(errno, errbuf, sizeof(errbuf)), _("socket"));
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	    }
-#endif
-	  continue;
-	}
-      if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
-		      (void *) &flag, sizeof(flag)) < 0 )
-	{
-	  ++i;
-#ifdef TEST_ONLY
-	  if (portchk_debug)
-	    perror(_("setsockopt"));
-#else
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN, 
-			  sh_error_message(errno, errbuf, sizeof(errbuf)),_("setsockopt"));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-#endif
-	  continue;
-	}
-
-
-      if (protocol == IPPROTO_TCP)
-	check_port_tcp_internal(sock, port, &paddr);
-      else
-	check_port_udp_internal(sock, port, &paddr);
-
-      ++i;
-    }
-
-  return 0;
-}
-
-
-
-static int check_port_udp (int port, int domain)
-{
-  return check_port_generic(port, domain, SOCK_DGRAM, IPPROTO_UDP);
-}
-
-static int check_port_tcp (int port, int domain)
-{
-  return check_port_generic(port, domain, SOCK_STREAM, IPPROTO_TCP);
-}
-
-
-static int sh_portchk_scan_ports_generic (int min_port, int max_port_arg, 
-					  int domain, int type, int protocol)
-{
-  /*
-  int min_port = 1024;
-  int max_port = 65535;
-  */
-
-  volatile int port; /*  might be clobbered by âlongjmpâ or âvforkâ*/
-  volatile int max_port = max_port_arg;
-  int retval;
-  int sock   = -1;
-#if 0
-  int flag   = 1; /* non-zero to enable an option */
-#endif
-  struct sockaddr_in  addr4;
-  struct sockaddr_in6 addr6;
-
-  int addrlen4      = sizeof(addr4);
-  int addrlen6      = sizeof(addr6);
-
-  struct in6_addr anyaddr = IN6ADDR_ANY_INIT; 
-
-  char errbuf[SH_ERRBUF_SIZE];
-
-  if (min_port == -1)
-     min_port = 0;
-  if (max_port == -1)
-    max_port = 65535;
-
-  if (portchk_debug)
-    fprintf(stderr, _("scan_ports_generic %d-%d %s %s\n"), 
-	    min_port, max_port, (domain == AF_INET6) ? _("AF_INET6") : _("AF_INET"),
-	    (protocol == IPPROTO_TCP) ? _("tcp") : _("udp"));
-	    
-  for (port = min_port; port <= max_port; ++port) 
-    {
-      if ((sock = socket(domain, type, protocol)) < 0 )
-	{
-#ifdef TEST_ONLY
-	  if (portchk_debug)
-	    perror(_("socket"));
-#else
-#ifndef EPROTONOSUPPORT
-#define EPROTONOSUPPORT 0
-#endif
-#ifndef EAFNOSUPPORT
-#define EAFNOSUPPORT    0
-#endif
-	  if (errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT)
-	    {
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN, 
-			      sh_error_message(errno, errbuf, sizeof(errbuf)), _("socket"));
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	    }
-#endif
-	  continue;
-	}
-
-#if 0
-      if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
-		      (void *) &flag, sizeof(flag)) < 0 )
-	{
-#ifdef TEST_ONLY
-	  if (portchk_debug)
-	    perror(_("setsockopt"));
-#else
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN, 
-			  sh_error_message(errno, errbuf, sizeof(errbuf)),_("setsockopt"));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-#endif
-	  continue;
-	}
-#endif
-
-      if (domain == AF_INET)
-	{
-	  addr4.sin_family      = AF_INET;
-	  addr4.sin_port        = htons(port);
-	  addr4.sin_addr.s_addr = INADDR_ANY;
-	  retval = bind (sock, (struct sockaddr *) &addr4, addrlen4);
-	}
-      else
-	{
-	  addr6.sin6_family       = AF_INET6;
-	  addr6.sin6_port         = htons(port);
-	  memcpy(&(addr6.sin6_addr.s6_addr), &anyaddr, sizeof(anyaddr));
-	  retval = bind (sock, (struct sockaddr *) &addr6, addrlen6);
-	}
-
-      if (retval == 0)
-	{
-	  /* we can bind the port, thus it is unused
-	   */
-	  sl_close_fd (FIL__, __LINE__, sock);
-	}
-      else
-	{
-	  if (errno == EINVAL || errno == EADDRINUSE)
-	    {
-	      /* try to connect to the port
-	       */
-	      if (protocol == IPPROTO_TCP)
-		check_port_tcp(port, domain);
-	      else
-		check_port_udp(port, domain);
-	    }
-	  else
-	    {
-#ifdef TEST_ONLY
-	      if (portchk_debug)
-		perror(_("bind"));
-#else
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN, 
-			      sh_error_message(errno, errbuf, sizeof(errbuf)), _("bind"));
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-#endif
-	    }
-	  sl_close_fd (FIL__, __LINE__, sock);
-	}
-    }
-
-  if (protocol == IPPROTO_TCP)
-    transient_tcp=sh_portchk_kill_transient(transient_tcp);
-  else
-    transient_udp=sh_portchk_kill_transient(transient_udp);
-  
-  return 0;
-}
-
-static int sh_portchk_scan_ports_tcp (int min_port, int max_port)
-{
-#if defined(USE_IPVX)
-  sh_portchk_scan_ports_generic (min_port, max_port, AF_INET6, 
-				 SOCK_STREAM, IPPROTO_TCP);
-#endif
-  return sh_portchk_scan_ports_generic (min_port, max_port, AF_INET, 
-					SOCK_STREAM, IPPROTO_TCP);
-}
- 
-static int sh_portchk_scan_ports_udp (int min_port, int max_port)
-{
-#if defined(USE_IPVX)
-  sh_portchk_scan_ports_generic (min_port, max_port, AF_INET6, 
-				 SOCK_DGRAM, IPPROTO_UDP);
-#endif
-  return sh_portchk_scan_ports_generic (min_port, max_port, AF_INET,
-					SOCK_DGRAM, IPPROTO_UDP);
-}
-
-/* Subroutine to add an interface
- */
-void * sh_dummy_1564_str    = NULL; /* fix clobbered by.. warning */
-
-static int sh_portchk_add_interface_int (const char * str, int type)
-{
-  struct sh_sockaddr saddr;
-  char errbuf[256];
-  char buf[64];
-
-  sh_dummy_1564_str    = (void*) &str;
-
-  if (iface_initialized == 0)
-    {
-      iface_list_used   = 0;
-      iface_initialized = 1;
-    }
-
-  do {
-
-    while (*str == ',' || *str == ' ' || *str == '\t') ++str;
-
-    if (*str)
-      {
-	char ipbuf[SH_IP_BUF];
-	unsigned int i = 0;
-	while (*str && i < (sizeof(buf)-1) && 
-	       *str != ',' && *str != ' ' && *str != '\t')
-	  {
-	    buf[i] = *str; ++str; ++i;
-	  }
-	buf[i] = '\0';
-
-	if (0 == sh_ipvx_aton(buf, &saddr))
-	  return -1;
-
-	if (iface_list_used == SH_IFACE_MAX)
-	  return -1;
-
-	sh_ipvx_ntoa(ipbuf, sizeof(ipbuf), &saddr);
-	sl_snprintf(errbuf, sizeof(errbuf), _("interface: %s"), ipbuf);
-	SH_MUTEX_LOCK(mutex_thread_nolog);
-	sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-			errbuf, _("sh_portchk_add_interface"));
-	SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	
-	memcpy (&(iface_list[iface_list_used].iface), &(saddr), sizeof(saddr));
-	iface_list[iface_list_used].type = type;
-	++iface_list_used;
-      }
-  } while (*str);
-
-  sh_dummy_1564_str = NULL;
-  return 0;
-}
-
-static int sh_portchk_add_interface (const char * str)
-{
-  return sh_portchk_add_interface_int (str, SH_IFACE_ADDR);
-}
-
-#if defined(HAVE_IFADDRS_H)
-/* 
- * subroutines to add a device
- */
-void * sh_dummy_1651_ifa    = NULL; /* fix clobbered by.. warning */
-
-static int portchk_add_device_int (const char * buf)
-{
-  struct ifaddrs *ifaddr, *ifa;
-  int family;
-#ifndef NI_MAXHOST
-#define NI_MAXHOST 1025
-#endif
-  char host[NI_MAXHOST];
-
-  sh_dummy_1651_ifa = (void*) &ifa;
-  
-  if (getifaddrs(&ifaddr) == -1)
-    {
-      volatile int nerr = errno;
-      char errbuf[SH_ERRBUF_SIZE];
-      sh_error_message(errno, errbuf, sizeof(errbuf));
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle((-1), FIL__, __LINE__, nerr, MSG_E_SUBGEN, 
-		      errbuf, _("getifaddrs"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      return -1;
-    }
-
-  for ( ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
-    {
-      if (ifa->ifa_addr == NULL)
-	continue;
-      
-      if (strcmp(ifa->ifa_name, buf) == 0)
-	{
-	  volatile int s = 0;
-          family = ifa->ifa_addr->sa_family;
-	  
-          if (family == AF_INET)
-	    {
-	      s = getnameinfo( ifa->ifa_addr, sizeof(struct sockaddr_in),
-			       host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST );
-	      
-	      if (s == 0)
-		{
-		  if (sh_portchk_add_interface_int(host, SH_IFACE_DEV) < 0)
-		    {
-		      freeifaddrs(ifaddr);
-		      return -1;
-		    }
-		}
-	    }
-	  
-#if defined(USE_IPVX)
-          if (family == AF_INET6)
-	    {
-	      s = getnameinfo( ifa->ifa_addr, sizeof(struct sockaddr_in6),
-			       host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST );
-	      
-	      if (s == 0)
-		{
-		  if (sh_portchk_add_interface_int(host, SH_IFACE_DEV) < 0)
-		    {
-		      freeifaddrs(ifaddr);
-		      return -1;
-		    }
-		}
-	    }
-#endif
-	  
-	  if (s != 0)
-	    {
-	      char errbuf[SH_ERRBUF_SIZE];
-	      sl_strlcpy(errbuf, buf,             sizeof(errbuf));
-	      sl_strlcat(errbuf, ": ",            sizeof(errbuf));
-	      sl_strlcat(errbuf, gai_strerror(s), sizeof(errbuf));
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle((-1), FIL__, __LINE__, s, MSG_E_SUBGEN, 
-			      errbuf, _("getnameinfo"));
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	    }
-	  
-        }
-    }
-
-  freeifaddrs(ifaddr);
-  return 0;
-}
-
-struct added_dev {
-  char dev[64];
-  struct added_dev * next;
-};
-
-static struct added_dev * dev_list = NULL;
-
-static void dev_list_add (char * buf)
-{
-  struct added_dev * new = SH_ALLOC(sizeof(struct added_dev));
-  sl_strlcpy(new->dev, buf, 64);
-  new->next = dev_list;
-  dev_list  = new;
-  return;
-}
-
-static void dev_list_kill ()
-{
-  struct added_dev * old;
-  struct added_dev * new = dev_list;
-  dev_list = NULL;
-
-  while (new)
-    {
-      old = new;
-      new = new->next;
-      SH_FREE(old);
-    }
-  return;
-}
- 
-static int sh_portchk_add_device (const char * str)
-{
-  char buf[64];
-                
-  do {
-
-    while (*str == ',' || *str == ' ' || *str == '\t') ++str;
-
-    if (*str)
-      {
-	unsigned int i = 0;
-	while (*str && i < (sizeof(buf)-1) &&
-	       *str != ',' && *str != ' ' && *str != '\t') {
-	  buf[i] = *str; ++str; ++i;
-	}
-	buf[i] = '\0';
-	
-	if (portchk_add_device_int (buf) < 0)
-	  return -1;
-	
-	dev_list_add(buf);
-      }
-  } while (*str);
-
-  return 0;
-}
-
-static int iface_comp (const void *a, const void *b)
-{
-  const struct portchk_interfaces * aa = (const struct portchk_interfaces *) a;
-  const struct portchk_interfaces * bb = (const struct portchk_interfaces *) b;
-  return (aa->type - bb->type);
-}
-
-static void iface_qsort()
-{
-  qsort(&iface_list[0], iface_list_used, sizeof(struct portchk_interfaces),
-	iface_comp);
-  return;
-}
-
-static void recheck_devices()
-{
-  if (dev_list)
-    {
-      struct added_dev * dev = dev_list;
-      int i, j;
-
-      if (portchk_debug)
-	{
-	  for (j = 0; j < iface_list_used; ++j)
-	    {
-	      char buf[SH_IP_BUF];
-	      struct portchk_interfaces * aa = &(iface_list[j]);
-	      sh_ipvx_ntoa(buf, sizeof(buf), &(aa->iface));
-	      fprintf(stderr, _("presort: iface[%d] type(%d) %s\n"), j, iface_list[j].type, buf);
-	    }
-	}
-
-      iface_qsort();
-      
-      if (portchk_debug)
-	{
-	  for (j = 0; j < iface_list_used; ++j)
-	    {
-	      char buf[SH_IP_BUF];
-	      struct portchk_interfaces * aa = &(iface_list[j]);
-	      sh_ipvx_ntoa(buf, sizeof(buf), &(aa->iface));
-	      fprintf(stderr, _("postsor: iface[%d] type(%d) %s\n"), j, iface_list[j].type, buf);
-	    }
-	}
-
-      i = 0;
-      for (j = 0; j < iface_list_used; ++j)
-	if (iface_list[j].type == SH_IFACE_DEV) ++i;
-      iface_list_used -= i;
-
-      if (portchk_debug)
-	{
-	  for (j = 0; j < iface_list_used; ++j)
-	    {
-	      char buf[SH_IP_BUF];
-	      struct portchk_interfaces * aa = &(iface_list[j]);
-	      sh_ipvx_ntoa(buf, sizeof(buf), &(aa->iface));
-	      fprintf(stderr, _("postdel: iface[%d] type(%d) %s\n"), j, iface_list[j].type, buf);
-	    }
-	}
-
-      while (dev)
-	{
-	  portchk_add_device_int (dev->dev);
-	  dev = dev->next;
-	}
-    }
-  return;
-}
-#endif
-
-/* verify whether port/interface is blacklisted (do not check)
- */
-static int sh_portchk_is_blacklisted(int port, struct sh_sockaddr * saddr, 
-				     int proto)
-{
-  struct sh_port * head;
-
-  if (proto == IPPROTO_TCP)
-    head = blacklist_tcp;
-  else
-    head = blacklist_udp;
-
-  while (head)
-    {
-      if (head->port == port && ( sh_ipvx_isany(head->paddr) ||  0 == sh_ipvx_cmp(head->paddr, saddr) ))
-	return 1;
-      head = head->next;
-    }
-  return 0;
-}
-
-
-static int sh_portchk_blacklist(int port, struct sh_sockaddr * saddr, int proto)
-{
-  struct sh_port * black;
-  struct sh_port * head;
-
-  if (proto == IPPROTO_TCP)
-    head = blacklist_tcp;
-  else
-    head = blacklist_udp;
-
-  black = head;
-
-  while (black)
-    {
-      if (black->port == port && 
-	  0 == sh_ipvx_cmp(head->paddr, saddr))
-	return -1;
-      black = black->next;
-    }
-
-  black = SH_ALLOC (sizeof(struct sh_port));
-  black->paddr = SH_ALLOC (sizeof(struct sh_sockaddr));
-  black->port  = port;
-  memcpy(black->paddr, saddr, sizeof(struct sh_sockaddr));
-  black->next  = head;
-
-  if (proto == IPPROTO_TCP)
-    blacklist_tcp = black;
-  else
-    blacklist_udp = black;
-
-  if (portchk_debug)
-    {
-      int checkit = sh_portchk_is_blacklisted(port, saddr, proto);
-      fprintf(stderr, _("port blacklisted: %d %s\n"), port, 
-	      (checkit == 1) ? _("ok") : _("fail"));
-    }
-  return 0;
-}
-
-
-/* verify whether port/interface is transient (used as source port 
- *hence no check required)
- */
-static int sh_portchk_is_transient(int port, struct sh_sockaddr * saddr, 
-				     int proto)
-{
-  struct sh_port * head;
-  
-  if (proto == IPPROTO_TCP)
-    head = transient_tcp;
-  else
-    head = transient_udp;
-  
-  while (head)
-    {
-      if (head->port == port && ( sh_ipvx_isany(head->paddr) || 0 == sh_ipvx_cmp(head->paddr, saddr) ))
-	return 1;
-      head = head->next;
-    }
-  return 0;
-}
-
-
-static int sh_portchk_transient(int port, struct sh_sockaddr * saddr, int proto)
-{
-  struct sh_port * transient;
-  struct sh_port * head;
-
-  if (sh_portchk_transients == S_FALSE)
-    return 0;
-
-  if (proto == IPPROTO_TCP)
-    head = transient_tcp;
-  else
-    head = transient_udp;
-
-  transient = head;
-
-  while (transient)
-    {
-      if (transient->port == port && 
-	  0 == sh_ipvx_cmp(head->paddr, saddr))
-	return -1;
-      transient = transient->next;
-    }
-
-  transient = SH_ALLOC (sizeof(struct sh_port));
-  transient->paddr = SH_ALLOC (sizeof(struct sh_sockaddr));
-  transient->port  = port;
-  memcpy(transient->paddr, saddr, sizeof(struct sh_sockaddr));
-  transient->next  = head;
-
-  if (proto == IPPROTO_TCP)
-    transient_tcp = transient;
-  else
-    transient_udp = transient;
-
-  if (portchk_debug)
-    {
-      int checkit = sh_portchk_is_transient(port, saddr, proto);
-      fprintf(stderr, _("port transient: %d %s\n"), port, 
-	      (checkit == 1) ? _("ok") : _("fail"));
-    }
-  return 0;
-}
-
-
-/* Subroutine to add a required or optional port/service
- */
-static int sh_portchk_add_required_port_generic (char * service, 
-						 char * interface, int type)
-{
-  char buf[256] = { '\0' };
-  int proto;
-  char * p;
-  char * endptr;
-  unsigned long int  port;
-  struct sh_sockaddr   saddr;
-  struct sh_portentry * portent;
-
-  if (0 == sh_ipvx_aton(interface, &saddr))
-    return -1;
-
-  sl_strlcpy (buf, service, sizeof(buf));
-
-  p = strchr(buf, '/');
-  if (!p)
-    return -1;
-  if (0 == strcasecmp(p, _("/tcp")))
-    proto = IPPROTO_TCP;
-  else if  (0 == strcasecmp(p, _("/udp")))
-    proto = IPPROTO_UDP;
-  else
-    return -1;
-
-  *p = '\0';
-  port = strtoul(buf, &endptr, 0);
-
-  if (portchk_debug)
-    {
-      char buf[SH_IP_BUF];
-      sh_ipvx_ntoa(buf, sizeof(buf), &saddr);
-      fprintf(stderr, _("add_port_generic: %s (%s) %d %s (%s)\n"),
-	      interface, buf, (int) port, (proto == IPPROTO_TCP) ? _("tcp") : _("udp"),
-	      sh_port_type2str(type));
-    }
-
-  /* Blacklisted ports
-   */
-  if (*endptr == '\0' && port <= 65535 && type == SH_PORT_BLACKLIST)
-    return (sh_portchk_blacklist(port, &saddr, proto));
-
-  if (*endptr != '\0')
-    {  
-      portent = sh_portchk_get_from_list (proto, -1, &saddr, buf);
-      if (!portent)
-	{
-	  if (portchk_debug)
-	    fprintf(stderr, _("add_required_port %d\n"), (int) port);
-	  sh_portchk_add_to_list (proto,   -1, &saddr,  buf, type, SH_PORT_UNKN);
-	}
-      else
-	{
-#ifdef TEST_ONLY
-	  fprintf(stderr, "** WARNING: duplicate port definition %s/%s\n", buf, SH_PROTO_STR(proto));
-#else
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-			  _("duplicate port definition"), _("sh_portchk_add_required_port_generic"));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-#endif
-	  return -1;
-	}
-    }
-  else if (port <= 65535)
-    {
-      portent = sh_portchk_get_from_list (proto, port, &saddr, NULL);
-      if (!portent)
-	{
-	  if (portchk_debug)
-	    fprintf(stderr, _("add_required_port: open port: %d/%s\n"), 
-		    (int) port, SH_PROTO_STR(proto));
-	  sh_portchk_add_to_list (proto, port, &saddr, NULL, type, SH_PORT_UNKN);
-	}
-      else
-	{
-#ifdef TEST_ONLY
-	  fprintf(stderr, "** WARNING: duplicate port definition %lu/%s\n", port, SH_PROTO_STR(proto));
-#else
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-			  _("duplicate port definition"), _("sh_portchk_add_required_port_generic"));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-#endif
-	  return -1;
-	}
-    }
-  else
-    return -1;
-
-  return 0;
-}
-
-/* Internal interface to add required or optional ports as 'iface:portlist'
- */
-static int sh_portchk_add_required_generic (const char * str, int type)
-{
-  size_t ll = 0;
-  int    status;
-
-  char * interface = NULL;
-  char * list;
-  char * p;
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
-  char * saveptr;
-#endif
-
-  if (!str)
-    return -1;
-
-  if (strchr(str, ':'))
-    {
-      char *last = strrchr(str, ':');
-      if (last != NULL)
-	{
-	  size_t tolast = (last - str);
-	  interface = SH_ALLOC(tolast+1);
-	  sl_strlcpy(interface, str, tolast+1);
-	  interface[tolast] = '\0';
-
-	  ll = tolast;
-
-	  while (str[ll] == ':' || str[ll] == ' ' || str[ll] == '\t')
-	    ++ll;
-	}
-    }
-  else
-    {
-      interface = SH_ALLOC(8);
-      sl_strlcpy(interface, _("0.0.0.0"), 8);
-      interface[7] = '\0';
-      while (str[ll] == ' ' || str[ll] == '\t')
-	++ll;      
-    }
-
-  if (!interface)
-    return -1;
-
-  if (str[ll] == '\0')
-    {
-      SH_FREE(interface);
-      return -1;
-    }
-
-  if (portchk_debug)
-    fprintf(stderr, "add ports for interface: %s (%s)\n", interface, sh_port_type2str(type));
-
-  list = sh_util_strdup(&str[ll]);
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
-  p    = strtok_r (list, " ,\t", &saveptr);
-#else
-  p    = strtok (list, " ,\t");
-#endif
-  if (!p)
-    {
-      SH_FREE(interface);
-      SH_FREE(list);
-      return -1;
-    }
-  while (p)
-    {
-      status = sh_portchk_add_required_port_generic (p, interface, type);
-
-      if (-1 == status)
-	{
-	  SH_FREE(interface);
-	  SH_FREE(list);
-	  return -1;
-	}
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
-      p    = strtok_r (NULL, " ,\t", &saveptr);
-#else
-      p    = strtok (NULL, " ,\t");
-#endif
-    }
-  SH_FREE(interface);
-  SH_FREE(list);
-  return 0;
-}
-
-/* User interface to add required ports as 'iface:portlist'
- */
-static int sh_portchk_add_required (const char * str)
-{
-  return sh_portchk_add_required_generic (str, SH_PORT_REQ); 
-}
-
-/* User interface to add optional ports as 'iface:portlist'
- */
-static int sh_portchk_add_optional (const char * str)
-{
-  return sh_portchk_add_required_generic (str, SH_PORT_OPT); 
-}
-
-/* User interface to add ignoreable ports as 'iface:portlist'
- */
-static int sh_portchk_add_ignore (const char * str)
-{
-  return sh_portchk_add_required_generic (str, SH_PORT_IGN); 
-}
-
-/* User interface to add ports that should not be checked as 'iface:portlist'
- */
-static int sh_portchk_add_blacklist (const char * str)
-{
-  return sh_portchk_add_required_generic (str, SH_PORT_BLACKLIST); 
-}
-
-/* Interface to run port check
- */
-int sh_portchk_check ()
-{
-  volatile int min_port;
-  static int noprivports = 0;
-
-  SH_MUTEX_LOCK(mutex_port_check);
-
-  min_port = (sh_portchk_minport == -1) ? 0 : sh_portchk_minport;
-
-  if (sh_portchk_active != S_FALSE)
-    {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-		      _("Checking for open ports"),
-		      _("sh_portchk_check"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-
-      sh_portchk_reset_lists();
-
-#if defined(HAVE_IFADDRS_H)
-      recheck_devices();
-#endif
-
-      if ((0 != geteuid()) && (min_port < 1024))
-	{
-	  min_port = 1024;
-	  if (noprivports == 0)
-	    {
-#ifdef TEST_ONLY
-	      fprintf(stderr, "** WARNING not scanning ports < 1024\n");
-#else
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-			      _("not scanning ports below 1024"), 
-			      _("sh_portchk_check"));
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-#endif
-	      noprivports = 1;
-	    }
-	}
-
-      sh_port2proc_prepare();
-
-      if (sh_portchk_check_udp == 1)
-	sh_portchk_scan_ports_udp(min_port, sh_portchk_maxport);
-      sh_portchk_scan_ports_tcp(min_port, sh_portchk_maxport);
-
-
-      sh_portchk_check_list (&portlist_tcp, IPPROTO_TCP, SH_PORT_REPORT);
-      if (sh_portchk_check_udp == 1)
-	sh_portchk_check_list (&portlist_udp, IPPROTO_UDP, SH_PORT_REPORT);
-
-    }
-  SH_MUTEX_UNLOCK(mutex_port_check);
-  return 0;
-}
-#endif
-
-#ifdef SH_CUTEST
-#include "CuTest.h"
-
-void Test_portcheck_lists (CuTest *tc)
-{
-#if defined(SH_USE_PORTCHECK) && (defined(SH_WITH_CLIENT) || defined(SH_STANDALONE))
-  struct sh_sockaddr    haddr_local;
-  struct sh_portentry * portent;
-  char   buf[256];
-  char * p;
-
-#ifdef HAVE_RPC_RPC_H
-  p = sh_getrpcbynumber(0, buf, sizeof(buf));
-  CuAssertTrue(tc, p == NULL);
-
-  p = sh_getrpcbynumber(100000, buf, sizeof(buf));
-  CuAssertPtrNotNull(tc, p);
-  CuAssertTrue(tc, (0 == strcmp(p, "portmapper") || 0 == strcmp(p, "rpcbind")));
-  CuAssertTrue(tc, (0 == strcmp(buf, "portmapper") || 0 == strcmp(p, "rpcbind")));
-
-  p = sh_getrpcbynumber(100007, buf, sizeof(buf));
-  CuAssertPtrNotNull(tc, p);
-  CuAssertTrue(tc, 0 == strcmp(p, "ypbind"));
-  CuAssertTrue(tc, 0 == strcmp(buf, "ypbind"));
-#endif
-
-  p = sh_getservbyport(0, SH_PROTO_STR(IPPROTO_UDP), buf, sizeof(buf));
-  CuAssertTrue(tc, p == NULL);
-
-#if !defined(HOST_IS_CYGWIN)
-  p = sh_getservbyport(22, SH_PROTO_STR(IPPROTO_TCP), buf, sizeof(buf));
-  CuAssertPtrNotNull(tc, p);
-  CuAssertTrue(tc, 0 == strcmp(p, "ssh"));
-  CuAssertTrue(tc, 0 == strcmp(buf, "ssh"));
-#endif
-
-  p = sh_getservbyport(13, SH_PROTO_STR(IPPROTO_UDP), buf, sizeof(buf));
-  CuAssertPtrNotNull(tc, p);
-  CuAssertTrue(tc, 0 == strcmp(p, "daytime"));
-  CuAssertTrue(tc, 0 == strcmp(buf, "daytime"));
-
-  CuAssertTrue(tc, 0 != sh_ipvx_aton("127.0.0.1", &haddr_local));
-
-  sh_portchk_add_to_list (IPPROTO_TCP,  8000, &haddr_local, NULL, SH_PORT_NOT, SH_PORT_UNKN);
-
-  portent = sh_portchk_get_from_list(IPPROTO_TCP,  8000, &haddr_local, NULL);
-  CuAssertPtrNotNull(tc, portent);
-
-  CuAssertTrue(tc, portent->port == 8000);
-  CuAssertTrue(tc, 0 == strcmp("127.0.0.1", portent->interface));
-  CuAssertTrue(tc, portent->status == SH_PORT_UNKN);
-  CuAssertTrue(tc, portent->flag == SH_PORT_NOT);
-
-  sh_portchk_check_list (&portlist_tcp, IPPROTO_TCP, SH_PORT_NOREPT);
-
-  CuAssertTrue(tc, NULL == portlist_tcp);
-
-  sh_portchk_add_to_list (IPPROTO_TCP,  8000, &haddr_local, NULL, SH_PORT_REQ, SH_PORT_UNKN);
-  sh_portchk_add_to_list (IPPROTO_TCP,  8001, &haddr_local, NULL, SH_PORT_NOT, SH_PORT_UNKN);
-  sh_portchk_add_to_list (IPPROTO_TCP,  8002, &haddr_local, NULL, SH_PORT_REQ, SH_PORT_UNKN);
-  sh_portchk_add_to_list (IPPROTO_TCP,  8003, &haddr_local, NULL, SH_PORT_NOT, SH_PORT_UNKN);
-  sh_portchk_add_to_list (IPPROTO_TCP,  8004, &haddr_local, NULL, SH_PORT_IGN, SH_PORT_UNKN);
-  sh_portchk_add_to_list (IPPROTO_TCP,    -1, &haddr_local, "foo1", SH_PORT_NOT, SH_PORT_UNKN);
-  sh_portchk_add_to_list (IPPROTO_TCP,    -1, &haddr_local, "foo2", SH_PORT_REQ, SH_PORT_UNKN);
-  sh_portchk_add_to_list (IPPROTO_TCP,    -1, &haddr_local, "foo3", SH_PORT_NOT, SH_PORT_UNKN);
-  sh_portchk_add_to_list (IPPROTO_TCP,    -1, &haddr_local, "foo4", SH_PORT_REQ, SH_PORT_UNKN);
-  sh_portchk_add_to_list (IPPROTO_TCP,    -1, &haddr_local, "foo5", SH_PORT_IGN, SH_PORT_UNKN);
-
-  sh_portchk_check_list (&portlist_tcp, IPPROTO_TCP, SH_PORT_NOREPT);
-
-  CuAssertPtrNotNull(tc, portlist_tcp);
-
-  portent = sh_portchk_get_from_list(IPPROTO_TCP,  8000, &haddr_local, NULL);
-  CuAssertPtrNotNull(tc, portent);
-
-  portent = sh_portchk_get_from_list(IPPROTO_TCP,  8001, &haddr_local, NULL);
-  CuAssertTrue(tc, NULL == portent);
-
-  portent = sh_portchk_get_from_list(IPPROTO_TCP,  8002, &haddr_local, NULL);
-  CuAssertPtrNotNull(tc, portent);
-
-  portent = sh_portchk_get_from_list(IPPROTO_TCP,  8003, &haddr_local, NULL);
-  CuAssertTrue(tc, NULL == portent);
-
-  portent = sh_portchk_get_from_list(IPPROTO_TCP,  8004, &haddr_local, NULL);
-  CuAssertPtrNotNull(tc, portent);
-
-  portent = sh_portchk_get_from_list(IPPROTO_TCP,  8000, &haddr_local, "foo1");
-  CuAssertTrue(tc, NULL == portent);
-
-  portent = sh_portchk_get_from_list(IPPROTO_TCP,  8000, &haddr_local, "foo2");
-  CuAssertPtrNotNull(tc, portent);
-  CuAssertTrue(tc, 0 == strcmp(portent->service, "foo2"));
-
-  portent = sh_portchk_get_from_list(IPPROTO_TCP,  8000, &haddr_local, "foo3");
-  CuAssertTrue(tc, NULL == portent);
-
-  portent = sh_portchk_get_from_list(IPPROTO_TCP,  8000, &haddr_local, "foo4");
-  CuAssertPtrNotNull(tc, portent);
-  CuAssertTrue(tc, 0 == strcmp(portent->service, "foo4"));
-
-  portent = sh_portchk_get_from_list(IPPROTO_TCP,  8000, &haddr_local, "foo5");
-  CuAssertPtrNotNull(tc, portent);
-  CuAssertTrue(tc, 0 == strcmp(portent->service, "foo5"));
-
-  CuAssertTrue(tc, 0 == sh_portchk_blacklist(666, &haddr_local, IPPROTO_TCP));
-  CuAssertTrue(tc, 0 != sh_portchk_blacklist(666, &haddr_local, IPPROTO_TCP));
-  CuAssertTrue(tc, 0 == sh_portchk_blacklist(667, &haddr_local, IPPROTO_TCP));
-  CuAssertTrue(tc, 0 == sh_portchk_blacklist(668, &haddr_local, IPPROTO_TCP));
-  CuAssertTrue(tc, 0 == sh_portchk_blacklist(666, &haddr_local, IPPROTO_UDP));
-  CuAssertTrue(tc, 0 != sh_portchk_blacklist(666, &haddr_local, IPPROTO_UDP));
-  CuAssertTrue(tc, 0 == sh_portchk_blacklist(667, &haddr_local, IPPROTO_UDP));
-  CuAssertTrue(tc, 0 == sh_portchk_blacklist(668, &haddr_local, IPPROTO_UDP));
-
-  CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(668, &haddr_local, IPPROTO_UDP));
-  CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(667, &haddr_local, IPPROTO_UDP));
-  CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(666, &haddr_local, IPPROTO_UDP));
-  CuAssertTrue(tc, 0 == sh_portchk_is_blacklisted(665, &haddr_local, IPPROTO_UDP));
-
-  CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(668, &haddr_local, IPPROTO_TCP));
-  CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(667, &haddr_local, IPPROTO_TCP));
-  CuAssertTrue(tc, 0 != sh_portchk_is_blacklisted(666, &haddr_local, IPPROTO_TCP));
-  CuAssertTrue(tc, 0 == sh_portchk_is_blacklisted(665, &haddr_local, IPPROTO_TCP));
-#else
-  (void) tc; /* fix compiler warning */
-#endif
-  return;
-}
-#endif
-
-#ifdef TEST_ONLY
-
-void usage (char * pname)
-{
-  printf ("%s [-r|--required interface:portlist][-o|--optional interface:portlist][--no-udp][-d|--debug] hostname\n\n", pname);
-  printf ("   Check local host for open ports; Version %s\n\n", PORTCHK_VERSION);
-  printf ("   Interface: Numeric address for an interface, e.g. 127.0.0.1\n");
-  printf ("   Portlist:  List of ports or services, e.g. 22/tcp,nfs/udp,nlockmgr/udp\n");
-  printf ("     required -> must be open\n");
-  printf ("     optional ->  may be open or closed\n");
-  printf ("   RPC services must be specified with service **name**, others with **port number**\n\n");
-  printf ("   Example:\n");
-  printf ("      %s --required 192.168.1.2:22/tcp,nfs/udp,nlockmgr/udp\n\n", pname);
-  return;
-}
-
-int main(int argc, char *argv[])
-{
-  char * pname = argv[0];
-
-
-  /* 
-  test_lists();
-
-  portlist_tcp = sh_portchk_kill_list (portlist_tcp);
-  portlist_udp = sh_portchk_kill_list (portlist_udp);
-  */
-
-  /* sh_portchk_add_required ("127.0.0.1 : nlockmgr/tcp, 5308/tcp, nfs/tcp"); */
-
-  while (argc > 1 && argv[1][0] == '-')
-    {
-      if (0 == strcmp(argv[1], "--help") || 0 == strcmp(argv[1], "-h"))
-	{
-	  usage(pname);
-	  exit (0);
-	}
-      else if (0 == strcmp(argv[1], "--required") || 0 == strcmp(argv[1], "-r"))
-	{
-	  if (argc < 3)
-	    {
-	      usage(pname);
-	      exit (1);
-	    }
-	  sh_portchk_add_required (argv[2]);
-	  --argc; ++argv;
-	}
-      else if (0 == strcmp(argv[1], "--optional") || 0 == strcmp(argv[1], "-o"))
-	{
-	  if (argc < 3)
-	    {
-	      usage(pname);
-	      exit (1);
-	    }
-	  sh_portchk_add_optional (argv[2]);
-	  --argc; ++argv;
-	}
-      else if (0 == strcmp(argv[1], "--no-udp"))
-	{
-	  sh_portchk_check_udp = 0;
-	}
-      else if (0 == strcmp(argv[1], "--debug") || 0 == strcmp(argv[1], "-d"))
-	{
-	  portchk_debug = 1;
-	}
-      else
-	{
-	  usage(pname);
-	  exit (1);
-	}
-      --argc; ++argv;
-    }
-
-  if (argc < 2)
-    {
-      usage(pname);
-      exit (1);
-    }
-
-  portchk_hostname = argv[1];
-      
-  if (0 != sh_portchk_init ())
-    {
-      usage(pname);
-      exit (1);
-    }
-
-  sh_portchk_check();
-
-  return 0;
-}
-#endif
Index: trunk/src/sh_prelink.c
===================================================================
--- trunk/src/sh_prelink.c	(revision 591)
+++ trunk/src/sh_prelink.c	(revision 1)
@@ -38,6 +38,7 @@
 static char * prelink_hash = NULL;
 
-int sh_prelink_set_path (const char * str)
-{
+int sh_prelink_set_path (char * str)
+{
+  size_t len;
   SL_ENTER(_("sh_prelink_set_path"));
   if (prelink_path != NULL)
@@ -48,14 +49,11 @@
       SL_RETURN((-1), _("sh_prelink_set_path")); 
     }
-#ifdef SH_EVAL_SHELL
-  prelink_path = sh_util_strdup(str);
+  len = sl_strlen (str);
+  prelink_path = SH_ALLOC(len+1);
+  (void) sl_strlcpy(prelink_path, str, len+1);
   SL_RETURN(0, _("sh_prelink_set_path")); 
-#else
-  prelink_path = NULL;
-  SL_RETURN((-1), _("sh_prelink_set_path"));
-#endif
-}
-
-int sh_prelink_set_hash (const char * str)
+}
+
+int sh_prelink_set_hash (char * str)
 {
   size_t len;
@@ -74,37 +72,23 @@
 }
 
-int sh_prelink_iself (SL_TICKET fd, off_t size, int alert_timeout, char * path)
-{
-  long   status;
-  char   magic[4];
-  char * tmp;
-
-  /* 42 bytes is about the minimum an ELF binary might have
-   * (with plenty of hacks to reduce the size, such as interleaving
-   * the code with the header...)
-   */
-  if (size < 42)
-    return S_FALSE;
-
-  status = sl_read_timeout (fd, magic, 4, alert_timeout, S_FALSE);
-  (void) sl_rewind(fd);
-  if (status == 4)
-    {
-      /*@-usedef@*/
-      if (magic[0] == (char) 0x7f &&
-	  magic[1] == 'E'  &&
-	  magic[2] == 'L'  &&
-	  magic[3] == 'F')
-	return S_TRUE;
-      /*@+usedef@*/
-    }
-  else
-    {
-      tmp = sh_util_safe_name (path);
-      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_E_SUBGPATH, 
-		      _("Error reading file"), _("sh_prelink_iself"), tmp);
-      SH_FREE(path);
-    }
-  return S_FALSE;
+int sh_prelink_iself (SL_TICKET fd, off_t size, int alert_timeout)
+{
+   long status;
+   char magic[4];
+   if (size < 42)
+	return S_FALSE;
+   status = sl_read_timeout (fd, magic, 4, alert_timeout);
+   (void) sl_rewind(fd);
+   if (status == 4)
+     {
+       /*@-usedef@*/
+       if (magic[0] == (char) 0x7f &&
+	   magic[1] == 'E'  &&
+	   magic[2] == 'L'  &&
+	   magic[3] == 'F')
+	 return S_TRUE;
+       /*@+usedef@*/
+     }
+   return S_FALSE;
 }
 
@@ -115,5 +99,4 @@
   SL_TICKET ticket;
   char * tmp;
-  char hashbuf[KEYBUF_SIZE];
 
   if (task->com_ti != (-1))
@@ -123,21 +106,17 @@
       task->com_ti = -1;
     }
-  ticket = sl_open_read(FIL__, __LINE__, task->command, 
+  ticket = sl_open_read(task->command, 
 			task->privileged == 0 ? SL_NOPRIV : SL_YESPRIV);
   if (SL_ISERROR(ticket))
     {
-      char errbuf[SH_ERRBUF_SIZE];
-      char errbuf2[SH_ERRBUF_SIZE];
-      sh_error_message(errno, errbuf2, sizeof(errbuf2));
-      sl_strlcpy(errbuf, sl_error_string(ticket), sizeof(errbuf));
       tmp = sh_util_safe_name (task->command);
-      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, ticket, MSG_E_READ, errbuf, errbuf2, tmp);
+      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, ticket, MSG_E_READ, tmp);
       SH_FREE(tmp);
       return;
     }
-
+  tiger_fd = ticket;
   if (*(task->checksum) == '\0' ||
       0 == sl_strcmp(task->checksum, 
-		     sh_tiger_hash (task->command, ticket, TIGER_NOLIM, hashbuf, sizeof(hashbuf))))
+		     sh_tiger_hash (task->command, TIGER_FD, 0)))
     {
       task->com_fd = get_the_fd(ticket);
@@ -156,5 +135,5 @@
 /* returns static storage
  */
-int sh_prelink_run (char * path, char * file_hash, int alert_timeout, unsigned long mask)
+int sh_prelink_run (char * path, char * file_hash, int alert_timeout)
 {
   static int      init = S_FALSE;
@@ -164,4 +143,6 @@
   int    status = 0;
   char * p;
+  struct  sigaction  new_act;
+  struct  sigaction  old_act;
 
   SL_ENTER(_("sh_prelink_run"));
@@ -185,8 +166,6 @@
   if (init == S_FALSE)
     {
-      char dir[SH_PATHBUF];
-
       sh_ext_tas_init(&task);
-      p = sh_unix_getUIDdir (SH_ERR_ERR, task.run_user_uid, dir, sizeof(dir));
+      p = sh_unix_getUIDdir (SH_ERR_ERR, task.run_user_uid);
       if (p)
 	{
@@ -255,17 +234,18 @@
     }
 
+  /* ignore SIGPIPE (instead get EPIPE if connection is closed)
+   */
+  new_act.sa_handler = SIG_IGN;
+  (void) retry_sigaction (FIL__, __LINE__, SIGPIPE, &new_act, &old_act);
+
   /* read from pipe
    */
-  sl_read_timeout_prep (task.pipeTI);
-
-  {
-    char hashbuf[KEYBUF_SIZE];
-    UINT64 length_nolim = TIGER_NOLIM;
-    if (sh.flag.opts == S_TRUE) sh_tiger_set_hashtype_mask(mask);
-    sl_strlcpy(file_hash,
-	       sh_tiger_generic_hash (path, task.pipeTI, &length_nolim, alert_timeout,
-				      hashbuf, sizeof(hashbuf)),
-	       KEY_LEN+1);
-  }
+  tiger_fd = task.pipeTI;
+  strcpy(file_hash,                        /* known to fit */
+	 sh_tiger_generic_hash (path, TIGER_FD, 0, alert_timeout));
+
+  /* restore old signal handler
+   */
+  (void) retry_sigaction (FIL__, __LINE__, SIGPIPE, &old_act, NULL);
 
   /* close pipe and return exit status
Index: trunk/src/sh_prelude.c
===================================================================
--- trunk/src/sh_prelude.c	(revision 591)
+++ trunk/src/sh_prelude.c	(revision 1)
@@ -27,31 +27,34 @@
  *       include libprelude 0.9 code from Yoann Vandoorselaere
  */
-
-
-/*
- * for strptime()
- */
-#define _GNU_SOURCE 1 
-
+ 
 #include "config_xor.h"
+
+#define _XOPEN_SOURCE 500 /* glibc2 needs this */
 
 #include <stdio.h>
 #include <string.h>
-#include <ctype.h>
 #include <sys/types.h>
 
-#if HAVE_SYS_TIME_H
-#include <sys/time.h>
+#if TIME_WITH_SYS_TIME
+
+# include <sys/time.h>
+# include <time.h>
+
+#else
+
+# if HAVE_SYS_TIME_H
+#  include <sys/time.h>
+# else
+#  include <time.h>
+# endif
+
 #endif
-#include <time.h>
 
 #include <unistd.h>
 #include <syslog.h>
 #include <pwd.h>
-
-int     sh_argc_store;
-char ** sh_argv_store;
-
-#if defined(HAVE_LIBPRELUDE)
+#include <grp.h>
+
+#if defined(HAVE_LIBPRELUDE) && defined(HAVE_LIBPRELUDE_9)
 
 
@@ -70,11 +73,9 @@
  * includes for samhain-specific functions (sl_strstr, sh_error_handle)
  */
-#include "samhain.h"
+#include "slib.h"
 #include "sh_cat.h"
 #include "sh_error_min.h"
 #include "sh_prelude.h"
-#define SH_NEED_PWD_GRP 1
-#include "sh_static.h"
-char * sh_util_strdup (const char * str) SH_GNUC_MALLOC;
+
 /*
  * When SH_USE_XML is set, value are formated using name="value".
@@ -92,5 +93,7 @@
 #define IDMEF_ANALYZER_CLASS _("Integrity Checker")
 #define IDMEF_ANALYZER_VERSION VERSION
-#define IDMEF_ANALYZER_MANUFACTURER _("http://www.la-samhna.de/samhain/")
+#define IDMEF_ANALYZER_MANUFACTURER _("Samhain by Rainer Wichmann")
+
+#define CLASSIFICATION_URL _("http://www.la-samhna.de/samhain/")
 
 
@@ -127,20 +130,8 @@
 }
 
-static int set_prelude_severity_int (const char * str, int prelude_sev)
-{
-        char * p;
-	char * dup = strdup (str);
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
-	char * saveptr;
-#endif
-
-	if (!dup) 
-	        return -1;
-
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
-	p = strtok_r (dup, ", \t", &saveptr);
-#else
-	p = strtok (dup, ", \t");
-#endif
+static int set_prelude_severity_int (char * str, int prelude_sev)
+{
+        char * p = strtok (str, ", \t");
+
         if (p) {
                 do {
@@ -161,32 +152,25 @@
                         else if (0 == strcmp (p, _("info")))
 	                        clear_and_set (prelude_sev, SH_ERR_INFO);
-                        else {
-			        free (dup);
+                        else
 	                        return -1;
-			}
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
-                        p = strtok_r (NULL, ", \t", &saveptr);
-#else
                         p = strtok (NULL, ", \t");
-#endif
                 } while (p);
         }
-	free(dup);
         return 0;
 }
 
-int sh_prelude_map_info (const char * str)
+int sh_prelude_map_info (char * str)
 {
         return (set_prelude_severity_int(str,(int)IDMEF_IMPACT_SEVERITY_INFO));
 }
-int sh_prelude_map_low (const char * str)
+int sh_prelude_map_low (char * str)
 {
         return (set_prelude_severity_int(str,(int)IDMEF_IMPACT_SEVERITY_LOW));
 }
-int sh_prelude_map_medium (const char * str)
+int sh_prelude_map_medium (char * str)
 {
         return (set_prelude_severity_int(str,(int)IDMEF_IMPACT_SEVERITY_MEDIUM));
 }
-int sh_prelude_map_high (const char * str)
+int sh_prelude_map_high (char * str)
 {
         return (set_prelude_severity_int(str,(int)IDMEF_IMPACT_SEVERITY_HIGH));
@@ -210,9 +194,5 @@
 {
         char *ret = NULL;
-#if defined(SH_WITH_SERVER)
-        int    delim_start_count = 0;
-        int    found = 0;
-#endif                
-
+        
         ptr = strchr(ptr, delim_start);
         if ( ! ptr )
@@ -220,19 +200,6 @@
 
         ret = ++ptr;
-#if defined(SH_WITH_SERVER)
-        while ((*ptr != '\0') && (!found)){
-		if (*ptr == delim_end) {
-		        if (delim_start_count == 0)
-			        found = 1;
-			delim_start_count--;
-		}
-		else if (*ptr == delim_start) 
-		        delim_start_count++;
-		ptr++;
-        }
-        ptr = (found) ? ptr-1 : NULL ;
-#else
+
         ptr = strchr(ptr, delim_end);
-#endif
         if ( ! ptr )
                 return NULL;
@@ -330,5 +297,4 @@
 }
 
-/* flawfinder: ignore *//* is part of name, not access() */
 static void get_access_info(idmef_file_access_t *access, char * mode, int pos, int mpos)
 {
@@ -339,6 +305,5 @@
 	do {
 	        if ( mode[pos] == 'r' ) {
-			/* flawfinder: ignore *//* is part of name, not access() */
-	                ret = idmef_file_access_new_permission(access, &str, IDMEF_LIST_APPEND);
+	                ret = idmef_file_access_new_permission(access, &str, -1);
 	                if ( ret < 0 )
 	                        return;
@@ -347,6 +312,5 @@
 	        }
 	        else if ( mode[pos] == 'w' ) {
-			/* flawfinder: ignore *//* is part of name, not access() */
-	                ret = idmef_file_access_new_permission(access, &str, IDMEF_LIST_APPEND);
+	                ret = idmef_file_access_new_permission(access, &str, -1);
 	                if ( ret < 0 )
 	                        return;
@@ -355,6 +319,5 @@
 	        }
 	        else if ( mode[pos] == 'x' || mode[pos] == 's' || mode[pos] == 't') {
-			/* flawfinder: ignore *//* is part of name, not access() */
-	                ret = idmef_file_access_new_permission(access, &str, IDMEF_LIST_APPEND);
+	                ret = idmef_file_access_new_permission(access, &str, -1);
 	                if ( ret < 0 )
 	                        return;
@@ -374,6 +337,5 @@
 
 	if ( got == 0 ) {
-	        /* flawfinder: ignore *//* is part of name, not access() */
-	        ret = idmef_file_access_new_permission(access, &str, IDMEF_LIST_APPEND);
+	        ret = idmef_file_access_new_permission(access, &str, -1);
 	        if ( ret < 0 )
 	                return;
@@ -395,10 +357,10 @@
         prelude_string_t *str;
         idmef_checksum_t *checksum;
-        idmef_file_access_t *access; /* flawfinder: ignore */
+        idmef_file_access_t *access;
         idmef_user_id_t *userid;
         const char *suffix = (category == IDMEF_FILE_CATEGORY_CURRENT) ? "_new" : "_old";
 	char *mode = NULL;
                 
-        ret = idmef_target_new_file(target, &file, IDMEF_LIST_APPEND);
+        ret = idmef_target_new_file(target, &file, -1);
         if ( ret < 0  )
                 return;
@@ -412,8 +374,6 @@
                  */
                 ret = idmef_file_new_path(file, &str);
-                if ( ret < 0 ) {
-		        free(ptr);
-                        return;
-		}
+                if ( ret < 0 )
+                        return;
                 prelude_string_set_nodup(str, ptr);
 
@@ -421,7 +381,8 @@
                 if ( ptr ) {
                         ret = idmef_file_new_name(file, &str);
-                        if ( ret == 0 ) {
-			        prelude_string_set_dup(str, ptr + 1);
-			}
+                        if ( ret < 0 )
+                                return;
+                        
+                        prelude_string_set_dup(str, ptr + 1);
                 }
         }
@@ -436,7 +397,8 @@
         if ( ptr ) {
                 ret = idmef_time_from_samhain(&time, ptr);
-                if ( ret == 0 ) {
-                        idmef_file_set_modify_time(file, time);
-		}
+                if ( ret < 0 )
+                        return;
+                
+                idmef_file_set_modify_time(file, time);
                 free(ptr);
         }
@@ -445,7 +407,8 @@
         if ( ptr ) {
                 ret = idmef_time_from_samhain(&time, ptr);
-                if ( ret == 0 ) {
-                        idmef_file_set_create_time(file, time);
-		}
+                if ( ret < 0 )
+                        return;
+                
+                idmef_file_set_create_time(file, time);
                 free(ptr);
         }
@@ -454,17 +417,8 @@
         if ( ptr ) {
                 ret = idmef_file_new_inode(file, &inode);
-                if ( ret == 0 ) {
-			char * dev = get_value(msg, _("dev"), suffix);
-			if (dev) {
-			        char * q = strchr(dev, ',');
-				if (*q) {
-				         *q = '\0'; ++q;
-					 idmef_inode_set_major_device(inode, strtoul(dev, NULL, 0));
-					 idmef_inode_set_minor_device(inode, strtoul(  q, NULL, 0));
-				}
-				free(dev);
-			}
-			idmef_inode_set_number(inode, strtoul(ptr, NULL, 10));
-		}
+                if ( ret < 0 )
+                        return;
+                
+                idmef_inode_set_number(inode, strtoul(ptr, NULL, 10));
                 free(ptr);
         }
@@ -472,148 +426,109 @@
         ptr = get_value(msg, _("chksum"), suffix);
         if ( ptr ) {
-                ret = idmef_file_new_checksum(file, &checksum, IDMEF_LIST_APPEND);
-                if ( ret < 0 ) {
-			free(ptr);
-			goto get_mode;
-		}
+                ret = idmef_file_new_checksum(file, &checksum, 0);
+                if ( ret < 0 )
+                        return;
 
 		hashtype = sh_tiger_get_hashtype();
 
 		if (hashtype == 0)
-			idmef_checksum_set_algorithm(checksum, IDMEF_CHECKSUM_ALGORITHM_TIGER);
-
-		else if (hashtype == 1)
-			idmef_checksum_set_algorithm(checksum, IDMEF_CHECKSUM_ALGORITHM_SHA1);
-		
-		else if (hashtype == 2)
-			idmef_checksum_set_algorithm(checksum, IDMEF_CHECKSUM_ALGORITHM_MD5);
-		
-		else
-			idmef_checksum_set_algorithm(checksum, IDMEF_CHECKSUM_ALGORITHM_TIGER);
-
-
-		ret = idmef_checksum_new_value(checksum, &str);
-		if ( ret < 0 ) {
-			free(ptr);
-			goto get_mode;
-		}
+                        idmef_checksum_set_algorithm(checksum, IDMEF_CHECKSUM_ALGORITHM_TIGER);
+
+                else if (hashtype == 1)
+                        idmef_checksum_set_algorithm(checksum, IDMEF_CHECKSUM_ALGORITHM_SHA1);
+
+                else if (hashtype == 2)
+                        idmef_checksum_set_algorithm(checksum, IDMEF_CHECKSUM_ALGORITHM_MD5);
+
+                else
+                        idmef_checksum_set_algorithm(checksum, IDMEF_CHECKSUM_ALGORITHM_TIGER);
+
+
+                ret = idmef_checksum_new_value(checksum, &str);
+                if ( ret < 0 )
+                        return;
 
 		/* will be freed on destroy()
 		 */
-		prelude_string_set_nodup(str, ptr);
-	}
-
- get_mode:
+                prelude_string_set_nodup(str, ptr);
+        }
 
 	mode = get_value(msg, _("mode"), suffix);
         if ( mode ) {
-	        /* flawfinder: ignore *//* is part of name, not access() */
-                ret = idmef_file_new_file_access(file, &access, IDMEF_LIST_APPEND);
-                if ( ret < 0 )
-                        goto get_owner;
-
-	        /* flawfinder: ignore *//* is part of name, not access() */
+                ret = idmef_file_new_file_access(file, &access, -1);
+                if ( ret < 0 )
+                        return;
+
                 ret = idmef_file_access_new_user_id(access, &userid);
                 if ( ret < 0 )
-                        goto get_owner;
+                        return;
                 idmef_user_id_set_type(userid, IDMEF_USER_ID_TYPE_OTHER_PRIVS);
 
-	        /* flawfinder: ignore *//* is part of name, not access() */
 		get_access_info ( access, mode, 7, 9 );
         }
-
- get_owner:
  
         ptr = get_value(msg, _("owner"), suffix);
         if ( ptr ) {
-	        char * uid;
-                
-	        /* flawfinder: ignore *//* is part of name, not access() */
-                ret = idmef_file_new_file_access(file, &access, IDMEF_LIST_APPEND);
-                if ( ret < 0 ) {
-                        free(ptr);
-                        goto get_group;
-		}
-
-	        /* flawfinder: ignore *//* is part of name, not access() */
+                struct passwd *pw;
+                
+                ret = idmef_file_new_file_access(file, &access, 0);
+                if ( ret < 0 )
+                        return;
+
                 ret = idmef_file_access_new_user_id(access, &userid);
-                if ( ret < 0 ) {
-                        free(ptr);
-                        goto get_group;
-		}
+                if ( ret < 0 )
+                        return;
                 idmef_user_id_set_type(userid, IDMEF_USER_ID_TYPE_USER_PRIVS);
                 
                 ret = idmef_user_id_new_name(userid, &str);
-                if ( ret < 0 ) {
-                        free(ptr);
-                        goto get_group;
-                }
+                if ( ret < 0 )
+                        return;
+                
                 prelude_string_set_nodup(str, ptr);
                 
-                uid = get_value(msg, _("iowner"), suffix);
-                if ( ! uid )
-                        goto get_group;
-                
-                idmef_user_id_set_number(userid, strtoul(uid, NULL, 0));
+                pw = getpwnam(ptr);
+                if ( ! pw )
+                        return;
+                
+                idmef_user_id_set_number(userid, pw->pw_uid);
 
 		if ( mode ) {
-		        /* flawfinder: ignore *//* is part of name, not access() */
 		        get_access_info ( access, mode, 1, 3 );
 		}
-
-		free(uid);
-		/* Don't free(ptr) because of prelude_string_set_nodup(str, ptr) */
-        }
-
- get_group:
+        }
 
         ptr = get_value(msg, _("group"), suffix);
         if ( ptr ) {
-                char *gid;
-                
-	        /* flawfinder: ignore *//* is part of name, not access() */
-                ret = idmef_file_new_file_access(file, &access, IDMEF_LIST_APPEND);
-                if ( ret < 0 ) {
-                        free(ptr);
-                        goto mode_free;
+                struct group *gr;
+                
+                ret = idmef_file_new_file_access(file, &access, -1);
+                if ( ret < 0 )
+                        return;
+
+                ret = idmef_file_access_new_user_id(access, &userid);
+                if ( ret < 0 )
+                        return;
+                idmef_user_id_set_type(userid, IDMEF_USER_ID_TYPE_GROUP_PRIVS);
+                
+                ret = idmef_user_id_new_name(userid, &str);
+                if ( ret < 0 )
+                        return;
+                
+                prelude_string_set_nodup(str, ptr);
+
+                gr = getgrnam(ptr);
+                if ( ! gr )
+                        return;
+
+                idmef_user_id_set_number(userid, gr->gr_gid);
+
+		if ( mode ) {
+		        get_access_info ( access, mode, 4, 6 );
 		}
-
-                ret = idmef_file_access_new_user_id(access, &userid);/* flawfinder: ignore *//* is part of name, not access() */
-                if ( ret < 0 ) {
-                        free(ptr);
-                        goto mode_free;
-		}
-
-                idmef_user_id_set_type(userid, IDMEF_USER_ID_TYPE_GROUP_PRIVS);
-                
-                ret = idmef_user_id_new_name(userid, &str);
-                if ( ret < 0 ) {
-                        free(ptr);
-                        goto mode_free;
-		}
-                
-                prelude_string_set_nodup(str, ptr);
-
-                gid = get_value(msg, _("igroup"), suffix);
-                if ( ! gid )
-                        goto mode_free;
-
-                idmef_user_id_set_number(userid, strtoul(gid, NULL, 0));
-
-		if ( mode ) {
-		        get_access_info ( access, mode, 4, 6 ); /* flawfinder: ignore */
-		}
-
-		free(gid);
-		/* Don't free(ptr) because of prelude_string_set_nodup(str, ptr) */
-        }
-
- mode_free:
+        }
 
 	if ( mode ) {
 	        free ( mode );
 	}
-
-	return;
 }
 
@@ -661,11 +576,4 @@
 #endif
 
-#ifdef SH_USE_PORTCHECK
-		{ MSG_PORT_MISS, N_("Service closed"), IDMEF_IMPACT_TYPE_OTHER },
-		{ MSG_PORT_NEW, N_("Service opened"), IDMEF_IMPACT_TYPE_OTHER },
-		{ MSG_PORT_RESTART, N_("Service restarted"), IDMEF_IMPACT_TYPE_OTHER },
-		{ MSG_PORT_NEWPORT, N_("Service restarted"), IDMEF_IMPACT_TYPE_OTHER },
-#endif
-
 #ifdef SH_USE_SUIDCHK
                 { MSG_SUID_POLICY, N_("SUID/SGID File Detected"), IDMEF_IMPACT_TYPE_FILE },
@@ -693,13 +601,5 @@
                 return -1;
         }
-
-#if defined(SH_WITH_SERVER)
-        /* when using yule, theres a msg=<... msg=<...> >*/
-	while ( (msg = get_value(ptr, _("msg"), NULL)) ) {
-	        free(ptr);
-		ptr = msg;
-	}
-#endif        
-
+        
         ret = prelude_string_cat(out, ptr);
         free(ptr);
@@ -709,276 +609,4 @@
 
 
-#ifdef SH_USE_PORTCHECK
-static int get_service_info(char *msg, idmef_alert_t *alert)
-{
-        int ret;
-	long port;
-	char *ptr, *new, *tmp, *ip, *srv, *protocol, *end;
-        prelude_string_t *str;
-        idmef_address_t *address;
-        idmef_node_t *node;
-	idmef_user_t *user;
-	idmef_process_t *process;
-        idmef_service_t *service;
-        idmef_source_t *source = idmef_alert_get_next_source(alert, NULL);
-        struct passwd *pw;
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-	struct passwd pwd;
-	char * buffer;
-#endif
-
-        new = sh_util_strdup(msg);
- 
-        ptr = strstr(new, _("port: "));
-        if ( ! ptr ) {
-                sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
-                                _("malformed Samhain port check message"), _("get_service_info"));
-		SH_FREE( new );
-                return -1;
-        }
-
-        ptr += 6; /* skip 'port: ', position on first byte of interface */
-        tmp = strchr(ptr, ':');
-        if ( ! tmp ) {
-                sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
-                                _("malformed Samhain port check message (no port)"), _("get_service_info"));
-		SH_FREE( new );
-                return -1;
-        }
-	*tmp = '\0';
-
-	/* Get interface 
-	 */
-	ip = strdup(ptr);
-        if ( ip ) {
-                if ( ! source ) {
-                        ret = idmef_alert_new_source(alert, &source, IDMEF_LIST_APPEND);
-                        if ( ret < 0 ) {
-                                free(ip);
-				SH_FREE( new );
-                                return ret;
-                        }
-                }
-
-                ret = idmef_source_new_node(source, &node);
-                if ( ret < 0 ) {
-                        free(ip);
-			SH_FREE( new );
-                        return ret;
-                }
-                
-                ret = idmef_node_new_address(node, &address, IDMEF_LIST_APPEND);
-                if ( ret < 0 ) {
-                        free(ip);
-			SH_FREE( new );
-                        return ret;
-                }
-                
-                ret = idmef_address_new_address(address, &str);
-                if ( ret < 0 ) {
-                        free(ip);
-			SH_FREE( new );
-                        return ret;
-                }
-                
-                prelude_string_set_nodup(str, ip);
-        }
-
-	ptr = tmp;
-	++ptr;
-        tmp = strchr(ptr, '/');
-        if ( ! tmp ) {
-                sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
-                                _("malformed Samhain port check message (no protocol)"), _("get_service_info"));
-		SH_FREE( new );
-                return -1;
-        }
-	*tmp = '\0';
-
-	/* Get port number
-	 */
-	port = strtol(ptr, &end, 0);
-        if ( *ptr && *end == '\0' && port >= 0 && port < 65536) {
-
-                if ( ! source ) {
-                        ret = idmef_alert_new_source(alert, &source, IDMEF_LIST_APPEND);
-                        if ( ret < 0 ) {
-				SH_FREE( new );
-                                return ret;
-                        }
-                }
-
-                ret = idmef_source_new_service(source, &service);
-                if ( ret < 0 ) {
-			SH_FREE( new );
-                        return ret;
-                }
-
-		idmef_service_set_port(service, port);
-
-                ret = idmef_service_new_protocol(service, &str);
-                if ( ret < 0 ) {
-			SH_FREE( new );
-                        return ret;
-                }
-                
-		++tmp; 
-		if (*tmp) { 
-		        char * tmpw = tmp;
-			char tmpw_store;
-			while (*tmpw && !isblank((int) *tmpw)) ++tmpw;
-			tmpw_store = *tmpw; *tmpw = '\0';
-		        protocol = strdup(tmp);
-			*tmpw = tmpw_store;
-			prelude_string_set_nodup(str, protocol);
-		}
-
-	}
-
-	ptr = tmp;
-	++ptr;
-        ptr = strchr(ptr, '(');
-        if ( ! ptr ) {
-                sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
-                                _("malformed Samhain port check message (no service)"), _("get_service_info"));
-		SH_FREE( new );
-                return -1;
-        }
-	++ptr;
-        tmp = strchr(ptr, ')');
-        if ( ! tmp ) {
-                sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
-                                _("malformed Samhain port check message (service not closed)"), _("get_service_info"));
-		SH_FREE( new );
-                return -1;
-        }
-	*tmp = '\0';
-
-	/* Get service
-	 */
-	srv = strdup(ptr);
-        if ( srv ) {
-                if ( ! source ) {
-                        ret = idmef_alert_new_source(alert, &source, IDMEF_LIST_APPEND);
-                        if ( ret < 0 ) {
-                                free(srv);
-				SH_FREE( new );
-                                return ret;
-                        }
-                }
-
-		if ( ! service ) {
-                        ret = idmef_source_new_service(source, &service);
-			if ( ret < 0 ) {
-                                free(srv);
-				SH_FREE( new );
-				return ret;
-			}
-                }
-
-                ret = idmef_service_new_name(service, &str);
-                if ( ret < 0 ) {
-                        free(srv);
-			SH_FREE( new );
-                        return ret;
-                }
-                
-                prelude_string_set_nodup(str, srv);
-        }
-
-	SH_FREE( new );
-
-        ptr = get_value(msg, _("userid"), NULL);
-
-        if ( ptr ) {
-
-	        idmef_user_id_t * user_id;
-
-	        ret = idmef_source_new_user(source, &user);
-                if ( ret < 0 ) {
-                        free(ptr);
-                        return ret;
-                }
-
-                idmef_user_set_category(user, IDMEF_USER_CATEGORY_APPLICATION);
-                
-                ret = idmef_user_new_user_id(user, &user_id, IDMEF_LIST_APPEND);
-                if ( ret < 0 ) {
-                        free(ptr);
-                        return ret;
-                }
-                
-                idmef_user_id_set_type(user_id, IDMEF_USER_ID_TYPE_CURRENT_USER);
-
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-		buffer = SH_ALLOC(SH_PWBUF_SIZE);
-		sh_getpwnam_r(ptr, &pwd, buffer, SH_PWBUF_SIZE, &pw);
-#else
-		pw = sh_getpwnam(ptr);
-#endif
-                if ( pw )
-                        idmef_user_id_set_number(user_id, pw->pw_uid);
-
-                ret = idmef_user_id_new_name(user_id, &str);
-                if ( ret < 0 ) {
-                        free(ptr);
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-			SH_FREE(buffer);
-#endif
-                        return ret;
-                }
-                prelude_string_set_nodup(str, ptr);
-
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-		SH_FREE(buffer);
-#endif
-	}
-
-
-        ptr = get_value(msg, _("path"), NULL);
-        tmp = get_value(msg, _("pid"), NULL);
-
-        if ( ptr ) {
-
-                /*
-                 * In term of IDMEF, this is the full path,
-                 * including the name.
-                 */
-                ret = idmef_source_new_process(source, &process);
-                if ( ret < 0 ) {
-		        free(ptr);
-                        return ret;
-		}
-
-		ret = idmef_process_new_path(process, &str);
-                if ( ret < 0 ) {
-		        free(ptr);
-                        return ret;
-		}
-                prelude_string_set_nodup(str, ptr);
-
-                
-                if ( NULL != strrchr(ptr, '/') ) {
-                        ret = idmef_process_new_name(process, &str);
-                        if ( ret == 0 ) {
-			        ptr = strrchr(ptr, '/');
-			        prelude_string_set_dup(str, ptr + 1);
-			}
-                } else {
-		        ret = idmef_process_new_name(process, &str);
-                        if ( ret == 0 ) {
-			        prelude_string_set_dup(str, ptr);
-			}
-		}
-
-		idmef_process_set_pid(process, strtoul(tmp, NULL, 0));
-        }
-
-	if (tmp)
-	  free(tmp);
-
-	return 0;
-}
-#endif
 
 static int get_login_info(char *msg, idmef_alert_t *alert)
@@ -989,8 +617,4 @@
         idmef_node_t *node;
         struct passwd *pw;
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-	struct passwd pwd;
-	char * buffer;
-#endif
         prelude_string_t *str;
         idmef_user_id_t *user_id;
@@ -1002,5 +626,5 @@
         if ( ptr ) {
                 if ( ! source ) {
-                        ret = idmef_alert_new_source(alert, &source, IDMEF_LIST_APPEND);
+                        ret = idmef_alert_new_source(alert, &source, -1);
                         if ( ret < 0 ) {
                                 free(ptr);
@@ -1015,5 +639,5 @@
                 }
                 
-                ret = idmef_node_new_address(node, &address, IDMEF_LIST_APPEND);
+                ret = idmef_node_new_address(node, &address, -1);
                 if ( ret < 0 ) {
                         free(ptr);
@@ -1036,5 +660,5 @@
                 else {
                         if ( ! source ) {
-                                ret = idmef_alert_new_source(alert, &source, IDMEF_LIST_APPEND);
+                                ret = idmef_alert_new_source(alert, &source, -1);
                                 if ( ret < 0 ) {
                                         free(ptr);
@@ -1069,5 +693,5 @@
                 idmef_user_set_category(user, IDMEF_USER_CATEGORY_OS_DEVICE);
                 
-                ret = idmef_user_new_user_id(user, &user_id, IDMEF_LIST_APPEND);
+                ret = idmef_user_new_user_id(user, &user_id, -1);
                 if ( ret < 0 ) {
                         free(ptr);
@@ -1077,10 +701,5 @@
                 idmef_user_id_set_type(user_id, IDMEF_USER_ID_TYPE_TARGET_USER);
                 
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-		buffer = SH_ALLOC(SH_PWBUF_SIZE);
-		sh_getpwnam_r(ptr, &pwd, buffer, SH_PWBUF_SIZE, &pw);
-#else
-		pw = sh_getpwnam(ptr);
-#endif
+                pw = getpwnam(ptr);
                 if ( pw )
                         idmef_user_id_set_number(user_id, pw->pw_uid);
@@ -1089,7 +708,4 @@
                 if ( ret < 0 ) {
                         free(ptr);
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-			SH_FREE(buffer);
-#endif
                         return ret;
                 }
@@ -1101,7 +717,4 @@
                         if ( ret < 0 ) {
                                 free(ptr);
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-				SH_FREE(buffer);
-#endif
                                 return ret;
                         }
@@ -1109,7 +722,4 @@
                         prelude_string_set_nodup(str, ptr);
                 }
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-		SH_FREE(buffer);
-#endif
         }
 
@@ -1130,37 +740,7 @@
 }
 
-#if defined(SH_WITH_SERVER)
-static int node_set_address(idmef_node_t *node, const char *addr)
-{
-        int ret;
-	prelude_string_t *prelude_str;
-	idmef_address_t *idmef_addr;
-
-	ret = prelude_string_new(&prelude_str);
-	if ( ret < 0 ) 
-                goto err;
-      
-	ret = prelude_string_set_ref(prelude_str, addr);
-	if ( ret < 0 ) 
-                goto err;
-
-	ret = idmef_address_new(&idmef_addr);
-	if ( ret < 0 ) 
-                goto err;
-      
-	idmef_address_set_category(idmef_addr, IDMEF_ADDRESS_CATEGORY_IPV4_ADDR);
-	idmef_address_set_address(idmef_addr, prelude_str);
-	idmef_node_set_address(node, idmef_addr, 0);
-
-	return 0;
- err:
-        return -1;
-}
-#endif
-                                          
 
 static int samhain_alert_prelude(int priority, int sh_class, 
-				 char *message, unsigned long msgid, 
-				 char * inet_peer_ip)
+				 char *message, unsigned long msgid)
 {
         int ret;
@@ -1175,11 +755,6 @@
         idmef_confidence_t *confidence;
         prelude_string_t *str;
-#if defined(SH_WITH_SERVER)
-        idmef_node_t *node;
-#else
-	(void) inet_peer_ip;
-#endif
-                
-        if ( !client || sh_class == STAMP)
+                
+        if ( sh_class == STAMP)
                 return 0;
         
@@ -1192,5 +767,5 @@
                 goto err;
 
-        idmef_alert_set_analyzer(alert, idmef_analyzer_ref(prelude_client_get_analyzer(client)), IDMEF_LIST_PREPEND);
+        idmef_alert_set_analyzer(alert, idmef_analyzer_ref(prelude_client_get_analyzer(client)), 0);
         
         ret = idmef_time_new_from_gettimeofday(&time);
@@ -1208,34 +783,18 @@
                 goto err;
         
-        ret = idmef_alert_new_target(alert, &target, IDMEF_LIST_APPEND);
+        ret = idmef_alert_new_target(alert, &target, -1);
         if ( ret < 0 )
                 goto err;
 
         idmef_target_set_decoy(target, IDMEF_TARGET_DECOY_NO);
-
-#if defined(SH_WITH_SERVER)
-        if ( inet_peer_ip != NULL){
-                ret = idmef_target_new_node(target, &node);
-		if ( ret < 0 )
-                          goto err;
-        
-		ret = node_set_address(node, inet_peer_ip); 
-		if ( ret < 0 )
-                          goto err;
-                          
-		idmef_target_set_node(target, idmef_node_ref(node));
-        }
-        else
-#endif        
+        
         if ( idmef_analyzer_get_node(prelude_client_get_analyzer(client)) ) {
                 idmef_node_ref(idmef_analyzer_get_node(prelude_client_get_analyzer(client)));
                 idmef_target_set_node(target, idmef_analyzer_get_node(prelude_client_get_analyzer(client)));
         }
-
+        
         if ( strstr(message, _("path=")) ) {
-#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
                 if ( msgid != MSG_FI_ADD && msgid != MSG_FI_ADD2 )
                         get_file_infos(target, message, IDMEF_FILE_CATEGORY_ORIGINAL);
-#endif
                 
                 get_file_infos(target, message, IDMEF_FILE_CATEGORY_CURRENT);
@@ -1258,12 +817,4 @@
                 goto err;
         
-#ifdef SH_USE_PORTCHECK
-	if (msgid == MSG_PORT_MISS || msgid == MSG_PORT_NEW || msgid == MSG_PORT_RESTART || msgid == MSG_PORT_NEWPORT) {
-	        ret = get_service_info(message, alert);
-		if ( ret < 0 )
-		        goto err;
-        }
-#endif
-   
         map_policy_to_class(message, msgid, impact, str);
 
@@ -1288,5 +839,5 @@
         idmef_confidence_set_rating(confidence, IDMEF_CONFIDENCE_RATING_HIGH);
         
-        ret = idmef_alert_new_additional_data(alert, &data, IDMEF_LIST_APPEND);
+        ret = idmef_alert_new_additional_data(alert, &data, -1);
         if ( ret < 0 )
                 goto err;
@@ -1313,5 +864,5 @@
 
 
-int sh_prelude_alert(int priority, int sh_class, char *message, long msgflags, unsigned long msgid, char *inet_peer_ip)
+int sh_prelude_alert(int priority, int sh_class, char *message, long msgflags, unsigned long msgid)
 {
         int ret;
@@ -1319,8 +870,8 @@
 	(void) msgflags; /* fix compiler warning */
 
-        if ( initialized < 1 )
+        if ( initialized < 0 )
                 return -1;
         
-        ret = samhain_alert_prelude(priority, sh_class, message, msgid, inet_peer_ip);
+        ret = samhain_alert_prelude(priority, sh_class, message, msgid);
         if ( ret < 0 ) {
                 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
@@ -1334,5 +885,5 @@
 
 
-int sh_prelude_set_profile(const char *arg)
+int sh_prelude_set_profile(char *arg)
 {
         if ( profile ) {
@@ -1350,8 +901,5 @@
 }
 
-/* Allow initialization of prelude; to be called
- * after forking the daemon. Delays heartbeat
- * start after config read until it is safe.
- */
+
 void sh_prelude_reset(void)
 {
@@ -1369,14 +917,5 @@
         if (initialized < 1)
                 return;
-
-	if (sh.flag.isdaemon == S_TRUE)
-	        prelude_client_destroy(client, PRELUDE_CLIENT_EXIT_STATUS_FAILURE);
-	else
-	        prelude_client_destroy(client, PRELUDE_CLIENT_EXIT_STATUS_SUCCESS);
-
-	client = NULL;
-
-	prelude_deinit();
-
+        prelude_client_destroy(client, PRELUDE_CLIENT_EXIT_STATUS_SUCCESS);
         initialized = 0;
         return;
@@ -1391,8 +930,4 @@
         idmef_analyzer_t *analyzer;
         prelude_client_flags_t flags;
-#ifdef SH_NOFAILOVER
-	prelude_connection_pool_t *pool;
-	prelude_connection_pool_flags_t conn_flags;
-#endif
 
 	if (ready_for_init == 0)
@@ -1403,5 +938,5 @@
 
 	prelude_thread_init(NULL);
-        prelude_init(&sh_argc_store, sh_argv_store);
+        prelude_init(NULL, NULL);
 
         ret = prelude_client_new(&client, profile ? profile : _("samhain"));
@@ -1430,12 +965,4 @@
         prelude_string_set_dup(str, IDMEF_ANALYZER_VERSION);
         
-#ifdef SH_NOFAILOVER
-	pool = prelude_client_get_connection_pool(client);
-	conn_flags = prelude_connection_pool_get_flags(pool);
-
-	conn_flags &= ~PRELUDE_CONNECTION_POOL_FLAGS_FAILOVER;
-	prelude_connection_pool_set_flags(pool, conn_flags);
-#endif
-
         ret = prelude_client_start(client);
         if ( ret < 0 ) {
Index: trunk/src/sh_prelude_old.c
===================================================================
--- trunk/src/sh_prelude_old.c	(revision 1)
+++ trunk/src/sh_prelude_old.c	(revision 1)
@@ -0,0 +1,825 @@
+/*
+ *
+ * Copyright (C) 2004, 2005 Rainer Wichmann, Patrice Bourgin, 
+ *                    Yoann Vandoorselaere
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING.  If not, write to
+ * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+/*
+ *
+ * 03/12/2004 : R.W.: 
+ *       fix more memory leaks in get_file_infos()
+ *       workaround (re-init) for file descriptor closing problem with GPG
+ *
+ * R.W.: fix missing treatment of alternative XML-style messages
+ *       fix get_value (if terminating '>' or '"' is not found, may 
+ *           overwrite the NULL terminator of the string ...)
+ *       fix memory leaks in get_file_infos(), retrieve_time()
+ *
+ * 13/03/2004 : This file is modified by Patrice Bourgin 
+ *              <pbourgin@xpconseil.com> 
+ *
+ * R.W.: Some problems with the patch by Patrice Bourgin fixed 
+ *       (e.g. memory leak)
+ *
+ * Modifications (13/03/2004) by Patrice bourgin (pbourgin@xpconseil.com) : 
+ * Comment : thanks for memory leak fix :p
+ * 1 : remove translation of HTML tag
+ * 2 : send detailled information about files to prelude (with two 
+ *     functions : get_value and get_file_infos)
+ *     these two functions were written by Yoann, but for future 
+ *     version of prelude, I adapt them to work
+ *     with version 0.8.10 of libprelude
+ * 3 : send a heartbeat just after initialization, to alert prelude that 
+ *     samhain is started
+ * 4 : these modifications was tested successfully, and all informations are 
+ *     correctly transmitted to prelude and displayed with piwi
+ *
+ * Modifications (7/03/2004) by Patrice bourgin (pbourgin@xpconseil.com) : 
+ * 1 : translation of HTML tag <> to tag () in alert to permit 
+ *     displaying alerts on piwi
+ * 2 : add the address in the source and in the target for displaying on piwi
+ * 3 : add information about the classification, because there was only 
+ *     one classification and it was not enough
+ * 4 : add impact field to classify alert in prelude, becuse impact is 
+ *     needed to treat information
+ * 5 : correct some errors with transmission to prelude  with libprelude
+ */
+ 
+#include "config_xor.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+
+#if TIME_WITH_SYS_TIME
+#include <sys/time.h>
+#include <time.h>
+#else
+#if HAVE_SYS_TIME_H
+#include <sys/time.h>
+#else
+#include <time.h>
+#endif
+#endif
+#include <unistd.h>
+#include <syslog.h>
+
+#if defined(HAVE_LIBPRELUDE) && !defined(HAVE_LIBPRELUDE_9)
+
+/*
+ * _() macros are samhain specific; they are used to replace string
+ * constants at runtime. This is part of the samhain stealth mode
+ * (fill string constants with encoded strings, decode at runtime).
+ */
+#define FIL__  _("sh_prelude.c")
+
+#if defined(__GNUC__)
+extern char *strptime (const char * s,
+                       const char * fmt, struct tm * tp);
+#endif
+
+#include <netdb.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <sys/utsname.h>
+#ifdef HAVE_LIBGEN_H
+#include <libgen.h> 
+#endif
+
+#include <libprelude/list.h>
+#include <libprelude/idmef-tree.h>
+#include <libprelude/idmef-tree-func.h>
+#include <libprelude/prelude-io.h>
+#include <libprelude/prelude-message.h>
+#include <libprelude/prelude-message-buffered.h>
+#include <libprelude/idmef-msg-send.h>
+#include <libprelude/idmef-message-id.h>
+#include <libprelude/prelude-message-id.h>
+#include <libprelude/sensor.h>
+
+#ifndef HAVE_BASENAME
+#define basename(a) ((NULL == strrchr(a, '/')) ? (a) : (strrchr(a, '/')))
+#endif
+
+/* 
+ * includes for samhain-specific functions (sl_strstr, sh_error_handle)
+ */
+#include "slib.h"
+#include "sh_mem.h"
+#include "sh_cat.h"
+#include "sh_error_min.h"
+#include "sh_prelude.h"
+#define SH_NEED_GETHOSTBYXXX
+#include "sh_static.h"
+
+static char               programname[64];
+static idmef_heartbeat_t  heartbeat;
+static prelude_msgbuf_t * hb_msgbuf  = NULL; 
+static struct utsname   * uname_data = NULL;
+
+static char               hostname[256];
+			  
+static char               model[64];
+static char               class[64];
+static char               version[8];
+static char               manufacturer[64];
+			  
+static char               name[16];
+static char               url[32];
+			  
+static char               meaning[64];
+static char               description[128];
+
+static char             * path_basename = NULL;
+static char             * path_fullname = NULL;
+
+/* safe string duplication function
+ */
+static char * xstrdup (const char * str)
+{
+  size_t len;
+  char * ret;
+
+  if (!str)
+    return NULL;
+  len = sl_strlen(str);
+  ret = SH_ALLOC(len+1);
+  sl_strlcpy (ret, str, len+1);
+  return (ret);
+}
+
+/* Get the value for a key. The key is built from toktmp + toksuffix.
+ * msg is modified temporarily, so it should not be declared 'const'.
+ */
+static char *get_value (char *msg, const char *toktmp, 
+			const char *toksuffix)
+{
+  char * ret = NULL, *ptr, tok[128];
+
+  snprintf(tok, sizeof(tok), "%s%s", toktmp, (toksuffix) ? toksuffix : "");
+
+  ptr = strstr(msg, tok);
+  if ( ! ptr )
+    return NULL;
+
+#ifdef SH_USE_XML
+  while (*ptr && *ptr != '"') ptr++;
+  if (*ptr) { 
+    ret = ptr + 1; ptr = ret; 
+  }
+  while (*ptr && *ptr != '"') ptr++;
+  if (*ptr)
+    {
+      *ptr = '\0';
+      if (ret) ret = xstrdup(ret);
+      *ptr = '"';
+    }
+  else
+    {
+      if (ret) ret = xstrdup(ret);
+    }
+#else
+  while (*ptr && *ptr != '<') ptr++;
+  if (*ptr) ret = ptr + 1;
+  while (*ptr && *ptr != '>') ptr++;
+  if (*ptr)
+    {
+      *ptr = '\0';
+      if (ret) ret = xstrdup(ret);
+      *ptr = '>';
+    }
+  else
+    {
+      if (ret) ret = xstrdup(ret);
+    }
+#endif
+
+  return ret;
+}
+
+int sh_prelude_init ()
+{
+  int ret = -1;
+
+  if (uname_data != NULL) {
+    SH_FREE(uname_data);
+    uname_data = NULL;
+  }
+  uname_data = SH_ALLOC(sizeof(struct utsname));    /* only once */
+  if (!uname_data) {
+    return -1; 
+  }
+  ret = uname(uname_data);
+  if (ret < 0) {
+    uname_data = NULL;
+    return -1;
+  }
+
+  /* ------- LibPrelude Init ------- 
+   */
+  strncpy(programname, _("Samhain"), 64);
+  programname[63] = '\0';
+
+  if ( prelude_sensor_init(programname, NULL, 0, NULL) < 0)
+    {
+      sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
+		      _("Failed to initialize Prelude"), 
+		      _("sh_prelude_init"));
+      return -1;
+    }
+
+  strncpy(model, _("Samhain"), 64);
+  model[63] = '\0';
+  strncpy(class, _("Samhain Host Intrusion Detection System"), 64);
+  class[63] = '\0';
+  strncpy(version, VERSION, 8); 
+  version[7] = '\0';
+  strncpy(manufacturer, _("Samhain by Rainer Wichmann"), 64);
+  manufacturer[63] = '\0';
+
+  /*
+  if (sh.host.name[0] != '\0') {
+    strncpy(hostname, sh.host.name, 256); hostname[255] = '\0';
+  } else {
+    gethostname (hostname, 256); hostname[255] = '\0';
+  }
+  */
+
+  /* According to the manpage, if gethostname returns a truncated hostname,
+   * it may or may not be NULL terminated. So we terminate explicitely.
+   */
+  gethostname (hostname, 256); hostname[255] = '\0';
+
+  strncpy (name, _("Samhain HIDS"), 16);
+  name[15] = '\0';
+  strncpy (url, _("http://www.la-samhna.de/samhain/"), 32);
+  url[31] = '\0';
+
+  strncpy (meaning, _("Message generated by Samhain"), 64);
+  meaning[63] = '\0';
+
+  /* analyzer information */
+  idmef_string_set (&heartbeat.analyzer.model,   model);
+  idmef_string_set (&heartbeat.analyzer.class,   class);
+  idmef_string_set (&heartbeat.analyzer.version, version);
+  
+  /* analyzer address */
+  idmef_analyzer_node_new(&heartbeat.analyzer);
+  idmef_string_set (&heartbeat.analyzer.node->name, hostname);
+  
+  /* analyzer type */
+  idmef_string_set(&heartbeat.analyzer.ostype,    uname_data->sysname);
+  idmef_string_set(&heartbeat.analyzer.osversion, uname_data->release);
+
+  
+  INIT_LIST_HEAD(&heartbeat.additional_data_list);
+  
+  if (hb_msgbuf != NULL)
+    {
+      prelude_msgbuf_close (hb_msgbuf);
+      hb_msgbuf = NULL;
+    }
+  hb_msgbuf = prelude_msgbuf_new(0);
+  if (!hb_msgbuf) {
+    return -1;
+  }
+
+  /* prelude_heartbeat_register_cb(&SendHeartbeat, NULL); */
+  return 1;
+}
+
+/* Retrieve the content of "msg=" for adding informations on 
+ * impact tag with prelude 
+ */
+char *RetrieveImpact(const char *msg)
+{
+  char *tmp1;
+  char *tmp2;
+  char *tmp0;
+  char *ret = NULL;
+  
+  tmp2 = xstrdup(msg);
+  /* 
+   * don't use strtok - strtok (str, delim) is 'one of the chars 
+   * in delim', not the full 'delim' string
+   */
+  if (tmp2)
+    tmp1 = sl_strstr (tmp2, _("msg="));
+  else
+    return NULL;
+
+  if (tmp1)
+    {
+      tmp1 += 5;
+#ifdef SH_USE_XML
+      tmp0 = strchr(tmp1, '"');
+#else
+      tmp0 = strchr(tmp1, '>');
+#endif
+      if (tmp0)
+	*tmp0 = '\0';
+      ret = xstrdup(tmp1);
+    }
+  SH_FREE(tmp2); /* fix memory leak */
+
+  return ret;
+}
+
+/* Transform the string time from the event into time 
+ */
+time_t retrieve_time(char *stime)
+{
+#ifdef HAVE_STRPTIME
+  struct tm tmptime;
+  time_t rettime = -1;
+  char *tmp0, *tmp1, *tmp2;
+
+  /* fix more memory leaks
+   */
+  if ( stime )
+    {
+      tmp0 = xstrdup(stime);
+      tmp1 = tmp0;
+      tmp2 = tmp1 + 1;
+        	
+      while (*tmp1 && *tmp1 != ']') tmp1++;
+      if (*tmp1)
+	{
+	  *tmp1 = '\0';
+	  tmp2 = xstrdup(tmp2);  
+	  SH_FREE (tmp0);
+	}
+      else
+	{
+	  tmp2 = xstrdup(tmp2);
+	  SH_FREE (tmp0);
+	}
+      
+      memset (&tmptime, '\0', sizeof(struct tm));
+      strptime(tmp2,"%Y-%m-%dT%H:%M:%S", &tmptime);
+      rettime = mktime(&tmptime);
+
+      SH_FREE(tmp2);
+
+      if ( rettime != -1 )
+	return rettime;
+      else
+	return 0;
+    } 
+#endif
+  return 0;
+}
+
+/* msg is modified temporarily in get_value(), 
+ * so it should not be declared 'const'.
+ */
+void get_file_infos(idmef_target_t *target, char *msg, 
+		    idmef_file_category_t category)
+{
+  char *ptr;
+  idmef_file_t *file;
+  idmef_time_t *temps;
+  idmef_inode_t *inode;
+  const char *suffix = (category == current) ? "_new" : "_old";
+  
+  file = idmef_target_file_new(target);
+  if ( ! file )
+    return;
+  
+  file->category = category;
+  /* 
+   * Fix memory leak - the pointer to  get_value(msg, "path", NULL) is lost
+   * libprelude does not strdup, only sets a pointer, so need a global pointer
+   * to keep track of this :(
+   */
+  if (category == original)
+    path_fullname = get_value(msg, "path", NULL);
+  idmef_string_set (&file->path, path_fullname);
+
+  if (category == original)
+    path_basename = get_value(msg, "path", NULL);
+  if (path_basename) {
+    idmef_string_set (&file->name, basename(path_basename));
+  }
+  
+  ptr = get_value(msg, "size", suffix);
+  if ( ptr ) {
+    file->data_size = strtoul(ptr, NULL, 10);
+    SH_FREE(ptr);
+  }
+  
+  ptr = get_value(msg, "mtime", suffix);
+  if ( ptr ) {
+    temps = idmef_file_modify_time_new(file);
+    temps->sec = retrieve_time(ptr);
+    SH_FREE(ptr);
+  }
+  
+  ptr = get_value(msg, "ctime", suffix);
+  if ( ptr ) {
+    temps = idmef_file_create_time_new(file);
+    temps->sec = retrieve_time(ptr);
+    SH_FREE(ptr);
+  }
+  
+  ptr = get_value(msg, "atime", suffix);
+  if ( ptr ) {
+    temps = idmef_file_access_time_new(file);
+    temps->sec = retrieve_time(ptr);
+    SH_FREE(ptr);
+  }
+  
+  ptr = get_value(msg, "inode", suffix);
+  if ( ptr ) {
+    inode = idmef_file_inode_new(file);
+    inode->number = strtoul(ptr, NULL, 10);
+    SH_FREE(ptr);
+  }
+}
+
+void sh_prelude_reset()
+{
+  (void) sh_prelude_alert (0, 0, NULL, 0, 0);
+  return;
+}
+
+int sh_prelude_alert (int priority, int sh_class, char * message,
+		      long msgflags, unsigned long msgid)
+{
+  static int                initialized = 0;
+  struct timeval            tv;
+
+  idmef_alert_t           * alert;
+  idmef_message_t         * idmef;
+  prelude_msgbuf_t        * msgbuf;
+  idmef_classification_t  * classification;        
+  idmef_target_t          * target;
+  idmef_address_t 	  * taddr;
+  idmef_source_t          * source;
+  idmef_assessment_t      * assessment;
+  idmef_additional_data_t * data;
+  /* To store impact */
+  char			  * impactmsg = NULL; 
+  struct hostent 	  * myhost;
+  char 			  * src_ip = NULL;
+
+  static int                some_error = 0;
+
+  (void) msgflags;
+  (void) msgid;
+
+  /* Workaround for the file closing bug
+   */
+  if (message == NULL && priority == 0 && sh_class == 0)
+    {
+      initialized = 0;
+      return 0;
+    }
+
+  if (initialized == 0)
+    {
+      /* initialize
+       */
+      initialized = sh_prelude_init();
+
+     /* send a heartbeat after initialization to say to prelude "I'm alive" */
+     
+      gettimeofday(&tv, NULL);
+      heartbeat.create_time.sec  = tv.tv_sec;
+      heartbeat.create_time.usec = tv.tv_usec;
+  
+      /*
+       * we could use additional data to send stats.
+       */
+      prelude_msgbuf_set_header(hb_msgbuf, PRELUDE_MSG_IDMEF, 0);
+      idmef_send_heartbeat(hb_msgbuf, &heartbeat);
+      prelude_msgbuf_mark_end(hb_msgbuf);
+    }
+  if (initialized == -1)
+    {
+      /* init failed
+       */
+      if (some_error == 0)
+	{
+	  sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
+			  _("Problem with prelude-ids support: init failed"), 
+			  _("sh_prelude_alert"));
+	}
+      some_error = 1;
+      return -1; 
+    }
+
+  if (sh_class == STAMP)
+    {
+      gettimeofday(&tv, NULL);
+      heartbeat.create_time.sec  = tv.tv_sec;
+      heartbeat.create_time.usec = tv.tv_usec;
+  
+      /*
+       * we could use additional data to send stats.
+       */
+      prelude_msgbuf_set_header(hb_msgbuf, PRELUDE_MSG_IDMEF, 0);
+      idmef_send_heartbeat(hb_msgbuf, &heartbeat);
+      prelude_msgbuf_mark_end(hb_msgbuf);
+      return 0;
+    }
+
+  /* This function serves to initialize a message structure.
+   * The returned idmef_message_t structure is a static variable
+   * declared in idmef_message_new().
+   */
+  idmef = idmef_message_new();
+  if ( ! idmef )
+    goto err;
+        
+  /* 'alert' is a static variable that gets initialized and
+   * associated with the idmef_message_t idmef_alert_t member. 
+   * -> no new memory allocated; not signal-safe or thread-safe.
+   */
+  idmef_alert_new(idmef);
+  alert = idmef->message.alert;
+      
+  /* Set the 'detect time'. idmef_alert_new() will already set the
+   * 'create time', whatever the difference is supposed to be.
+   */
+  gettimeofday(&tv, NULL);
+  idmef_alert_detect_time_new(alert);
+  alert->detect_time->sec  = tv.tv_sec;
+  alert->detect_time->usec = tv.tv_usec;
+       
+  /* ------- Analyzer. -------
+   * 
+   * This apparently is supposed to provide some information
+   * about the sensor to the server (what sensor process ? where ?).
+   *
+   * idmef_string_set (x, y) is a macro that will make x
+   * a pointer to y. Therefore the caller must guarantee that y will
+   * never be overwritten until the alert is sent.
+   * With the samhain _() macros, this means we must copy to another
+   * storage region.
+   *
+   * N.B.: with constant strings, you can use idmef_string_set_constant() 
+   * instead.
+   */
+  idmef_string_set (&alert->analyzer.model,        model);
+  idmef_string_set (&alert->analyzer.class,        class);
+  idmef_string_set (&alert->analyzer.version,      version);
+  idmef_string_set (&alert->analyzer.manufacturer, manufacturer);
+
+  /* Here we add some information on the host OS.
+   */
+  idmef_string_set (&alert->analyzer.ostype,    uname_data->sysname);
+  idmef_string_set (&alert->analyzer.osversion, uname_data->release);
+
+  /* ------- Analyzer / Process ------- 
+   *
+   * Fill in minimal info about the process. Apparently one could also
+   * supply things like path, argv, env (?).
+   */
+  idmef_analyzer_process_new (&alert->analyzer);
+  alert->analyzer.process->pid = getpid();
+
+  /* ------- Analyzer / Node ------- 
+   *
+   * Provide the name of this node, i.e. host.
+   */
+  idmef_analyzer_node_new (&alert->analyzer);
+  idmef_string_set (&alert->analyzer.node->name, hostname);
+
+
+
+  /* ------- Classification -------
+   *
+   * Apparently 'classification' provides details about the sensor
+   * program.
+   *
+   * For reasons unbeknown to me (did not care to investigate),
+   * this function does allocate memory, instead of using a static variable. 
+   *
+   */
+  classification = idmef_alert_classification_new(alert);
+  if ( ! classification )
+    goto err;
+
+
+  impactmsg = RetrieveImpact(message);
+  if (impactmsg)
+    idmef_string_set (&classification->name, impactmsg);
+  idmef_string_set (&classification->url,  url);
+
+  classification->origin = vendor_specific;
+         
+  /* Get information about ip address */
+  
+  myhost = sh_gethostbyname(hostname);
+  src_ip = xstrdup(inet_ntoa(*((struct in_addr *)myhost->h_addr_list[0])));
+              
+
+  /* ------- Target -------
+   *
+   * Purpose ? To provide informations about destination of alert
+   *
+   * Allocates memory.
+   *
+   */
+  target = idmef_alert_target_new(alert);
+  if ( ! target )
+    goto err;
+  idmef_target_node_new(target);
+  idmef_string_set(&target->node->name, hostname);
+
+  if ( strstr(message, "path=") ) {
+    get_file_infos(target, message, original);
+    get_file_infos(target, message, current);
+  }
+
+  if (src_ip)
+    {
+      taddr = idmef_node_address_new(target->node);
+      if (!taddr) 
+	goto err;
+      
+      taddr->category = ipv4_addr;
+      idmef_string_set(&taddr->address, src_ip);
+    }
+
+  /* ------- Source -------
+   *
+   * Purpose ? To provide informations about source of alert
+   *
+   * Allocates memory.
+   *
+   */
+  source = idmef_alert_source_new(alert);
+  if ( ! source )
+    goto err;
+
+  /* ------- Impact ------- 
+   */
+  idmef_alert_assessment_new(alert);
+  assessment = alert->assessment;
+  idmef_assessment_impact_new(assessment);
+
+  if ((priority == SH_ERR_SEVERE) || (priority == SH_ERR_FATAL))
+    {
+      assessment->impact->severity   = impact_high;
+    }
+  else if ((priority == SH_ERR_ALL) || (priority == SH_ERR_INFO) ||
+      (priority == SH_ERR_NOTICE))
+    {
+      assessment->impact->severity   = impact_low;
+    }
+  else
+    {
+      assessment->impact->severity   = impact_medium;
+    }
+
+  if (NULL != sl_strstr(message, _("POLICY")))
+    {
+      if (NULL != sl_strstr(message, _("POLICY KERNEL")))
+	{
+	  assessment->impact->severity   = impact_high;
+	  assessment->impact->completion = succeeded;
+	  assessment->impact->type       = other;
+	  strncpy(description, 
+		  _("Kernel modification detected by Samhain."),
+		  128);
+	  description[127] = '\0';
+	}
+      else
+	{
+	  assessment->impact->severity   = impact_high;
+	  assessment->impact->completion = succeeded;
+	  assessment->impact->type       = file;
+	  strncpy(description, 
+		  _("File system modification detected by Samhain."),
+		  128);
+	  description[127] = '\0';
+	}
+    }
+  else
+    {
+      if ( ((NULL != sl_strstr(message, _("Login"))) ||
+	    (NULL != sl_strstr(message, _("Multiple login"))) ||
+	    (NULL != sl_strstr(message, _("Logout")))) &&
+	   (NULL == sl_strstr(message, _("Checking"))))
+	{
+	  assessment->impact->completion = succeeded;
+	  assessment->impact->type       = user;
+	  strncpy(description, 
+		  _("Login/logout detected by Samhain."),
+		  128);
+	  description[127] = '\0';
+	}
+      else
+	{
+	  /* assessment->impact->severity   = impact_low; */
+	  assessment->impact->completion = succeeded;
+	  assessment->impact->type       = other;
+	  strncpy(description, 
+		  _("Message by Samhain."),
+		  128);
+	  description[127] = '\0';
+	}
+    }
+  idmef_string_set (&assessment->impact->description, description); 
+  idmef_assessment_confidence_new(assessment);
+  assessment->confidence->rating = high;
+
+  /* ------- Additional Data ------- 
+   * 
+   * Here we supply the log message.
+   *
+   */
+  data = idmef_alert_additional_data_new(alert);
+  if ( ! data )
+    goto err;
+
+  data->type = string;
+  idmef_string_set (&data->meaning, meaning);
+  if (message)
+    idmef_additional_data_set_data (data, string, message, 
+				    strlen(message) + 1);
+  
+  /* ------- Send ------- 
+   *
+   * Finally, the preparated message is sent.
+   */      
+  msgbuf = prelude_msgbuf_new(0);
+  if ( ! msgbuf )
+    goto err;
+
+  /* Always return 0 (in libprelude 0.8.10); i.e. no useful
+   * exit status
+   */
+  idmef_msg_send(msgbuf, idmef, PRELUDE_MSG_PRIORITY_HIGH);
+
+  /* Cleanup
+   */
+  idmef_message_free(idmef);
+  prelude_msgbuf_close(msgbuf);
+  if (path_basename)
+    {
+      SH_FREE(path_basename);
+      path_basename = NULL;
+    }
+  if (path_fullname)
+    {
+      SH_FREE(path_fullname);
+      path_fullname = NULL;
+    }
+  if (impactmsg)
+    SH_FREE(impactmsg);
+  if (src_ip)
+    SH_FREE(src_ip);
+
+  some_error = 0;
+
+  return 0;
+        
+ err:
+  /* Cleanup
+   */
+  idmef_message_free(idmef);
+  if (0 == some_error)
+    {
+      sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
+		      _("Problem with IDMEF for prelude-ids support: alert lost"), 
+		      _("sh_prelude_alert"));
+    }
+  if (path_basename)
+    {
+      SH_FREE(path_basename);
+      path_basename = NULL;
+    }
+  if (path_fullname)
+    {
+      SH_FREE(path_fullname);
+      path_fullname = NULL;
+    }
+  if (impactmsg)
+    SH_FREE(impactmsg);
+  if (src_ip)
+    SH_FREE(src_ip);
+  some_error = 1;
+  return -1;
+
+}
+
+/* HAVE_LIBPRELUDE */
+#endif
Index: trunk/src/sh_processcheck.c
===================================================================
--- trunk/src/sh_processcheck.c	(revision 591)
+++ 	(revision )
@@ -1,1558 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 2006 Rainer Wichmann                                      */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-/***************************************************************************
- *
- * This file provides a module for samhain to check for hidden/faked/missing
- * processes on the host.
- *
- */
-
-#include "config_xor.h"
-
-/* changed from 500 to 600 b/o FreeBSD (see sys/cdefs.h) 
- * which needs _POSIX_C_SOURCE >= 200112 for lstat()
- */
-#if defined(__sun) || defined(__sun__) || defined(sun)
-#define _XOPEN_SOURCE 500
-#else
-#define _XOPEN_SOURCE 600
-#endif
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <ctype.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <dirent.h>
-#include <signal.h>
-#include <unistd.h>
-
-#ifdef _POSIX_PRIORITY_SCHEDULING
-#include <sched.h>
-#endif
-
-#ifdef HAVE_GETPRIORITY
-#include <errno.h>
-#include <sys/resource.h>
-#endif
-
-#ifdef HAVE_SYS_STATVFS_H
-#include <sys/statvfs.h>
-#endif
-
-
-#ifdef HAVE_REGEX_H
-#include <regex.h>
-#endif
-
-#include "samhain.h"
-#include "sh_modules.h"
-#include "sh_processcheck.h"
-#include "sh_utils.h"
-#include "sh_error.h"
-#include "sh_extern.h"
-#include "sh_calls.h"
-#include "sh_pthread.h"
-
-#ifdef SH_USE_PROCESSCHECK
-
-#define FIL__  _("sh_processcheck.c")
-
-#ifdef __linux__
-#define PS_THREADS
-#endif
-
-/* We won't want to build this into yule 
- */
-#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
-
-SH_MUTEX_STATIC(mutex_proc_check, PTHREAD_MUTEX_INITIALIZER);
-
-/* sh_prochk_maxpid is one more than the largest pid
- */
-static  size_t  sh_prochk_minpid = 0x0001;
-static  size_t  sh_prochk_maxpid = 0x8000;
-static  size_t  sh_prochk_size   = 0;
-
-static  int     ShProchkActive  = S_TRUE;
-static  short * sh_prochk_res   = NULL; 
-
-static  char  * sh_prochk_pspath = NULL;
-static  char  * sh_prochk_psarg  = NULL;
-
-#define SH_PROCHK_INTERVAL 300
-static time_t   sh_prochk_interval = SH_PROCHK_INTERVAL;
-static int      sh_prochk_severity = SH_ERR_SEVERE;
-static int      sh_prochk_openvz   = S_FALSE;
-
-static int sh_prochk_set_maxpid  (const char * str);
-static int sh_prochk_set_minpid  (const char * str);
-static int sh_prochk_set_active  (const char *str);
-static int sh_prochk_add_process (const char *str);
-static int sh_prochk_set_pspath  (const char *str);
-static int sh_prochk_set_psarg   (const char *str);
-static int sh_prochk_set_interval(const char *str);
-static int sh_prochk_set_severity(const char *str);
-static int sh_prochk_set_openvz  (const char *str);
-
-sh_rconf sh_prochk_table[] = {
-    {
-        N_("severityprocesscheck"),
-        sh_prochk_set_severity,
-    },
-    {
-        N_("processcheckexists"),
-        sh_prochk_add_process,
-    },
-    {
-        N_("processcheckactive"),
-        sh_prochk_set_active,
-    },
-    {
-        N_("processcheckminpid"),
-        sh_prochk_set_minpid,
-    },
-    {
-        N_("processcheckmaxpid"),
-        sh_prochk_set_maxpid,
-    },
-    {
-        N_("processcheckpspath"),
-        sh_prochk_set_pspath,
-    },
-    {
-        N_("processcheckpsarg"),
-        sh_prochk_set_psarg,
-    },
-    {
-        N_("processcheckinterval"),
-        sh_prochk_set_interval,
-    },
-    {
-        N_("processcheckisopenvz"),
-        sh_prochk_set_openvz,
-    },
-    {
-        NULL,
-        NULL
-    }
-};
-
-#define    SH_PROC_MISSING 1
-#define    SH_PROC_FAKED   2
-#define    SH_PROC_HIDDEN  4
-#define    SH_PROC_EXISTS  8
-
-#ifndef HAVE_LSTAT
-#define lstat(x,y) stat(x,y)
-#endif /* HAVE_LSTAT */
-
-#if defined(S_IFLNK) && !defined(S_ISLNK)
-#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
-#else
-#if !defined(S_ISLNK)
-#define S_ISLNK(mode) (0)
-#endif
-#endif
-
-static const short SH_PR_PS       = 0x0001;
-
-static const short SH_PR_GETSID   = 0x0002;
-static const short SH_PR_KILL     = 0x0004;
-static const short SH_PR_GETPGID  = 0x0008;
-
-static const short SH_PR_LSTAT    = 0x0010;
-static const short SH_PR_OPENDIR  = 0x0020;
-static const short SH_PR_CHDIR    = 0x0040;
-static const short SH_PR_SCHED    = 0x0080;
-
-static const short SH_PR_PRIORITY = 0x0100;
-static const short SH_PR_STATVSF  = 0x0200;
-
-static const short SH_PR_PS2      = 0x1000;
-static const short SH_PR_PS_ANY   = 0x2000;
-static const short SH_PR_ALL      = 0x4000;
-static const short SH_PR_ANY      = 0x8000;
-
-/* /proc: 
- *        linux:     /proc/pid/exe
- *        freebsd:   /proc/pid/file
- *        solaris10: /proc/pid/path/a.out
- */
-static char * get_user_and_path (pid_t pid, char * user, size_t usrlen)
-{
-  extern char *  sh_unix_getUIDname (int level, uid_t uid, char * out, size_t len);
-
-  char        path[128];
-  char *      buf;
-  struct stat sbuf;
-  int         len;
-  char *      tmp;
-
-  sl_snprintf (path, sizeof(path), _("/proc/%ld/exe"), (unsigned long) pid);
-
-  if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
-    {
-      goto linkread;
-    }
-
-  sl_snprintf (path, sizeof(path), _("/proc/%ld/file"), (unsigned long) pid);
-
-  if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
-    {
-      goto linkread;
-    }
-
-  sl_snprintf (path, sizeof(path), _("/proc/%ld/path/a.out"), (unsigned long) pid);
-
-  if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))
-    {
-      goto linkread;
-    }
-
-  return NULL;
-
- linkread:
-
-  buf = SH_ALLOC(PATH_MAX);
-  len = readlink(path, buf, PATH_MAX);   /* flawfinder: ignore */
-  len = (len >= PATH_MAX) ? (PATH_MAX-1) : len;
-
-  if (len > 0)
-    { 
-      buf[len] = '\0';
-    }
-  else
-    {
-      SH_FREE(buf);
-      return NULL;
-    }
-
-  tmp = sh_unix_getUIDname (SH_ERR_ALL, sbuf.st_uid, user, usrlen);
-
-  if (!tmp)
-    sl_snprintf (user, usrlen, "%ld", (unsigned long) sbuf.st_uid);
-
-  return buf;
-}
-
-
-struct watchlist {
-  char        * str;
-  unsigned long pid;
-#ifdef HAVE_REGEX_H
-  regex_t       preg;
-#endif
-  int           seen;
-
-  struct watchlist *next;
-};
-
-static struct watchlist * process_check = NULL;
-
-static struct watchlist * list_missing  = NULL;
-static struct watchlist * list_fake     = NULL;
-static struct watchlist * list_hidden   = NULL;
-
-/* recursively remove all list entries
- */
-static void kill_list (struct watchlist * head)
-{
-  if (head->next)
-    kill_list (head->next);
-
-  if (head->str)
-    SH_FREE(head->str);
-  SH_FREE(head);
-
-  return;
-}
-
-  
-/* check the list for old entries; clean out old entries; reset others
- * Return number of non-obsolete entries
- */
-static size_t clean_list (struct watchlist ** head_ptr)
-{
-  size_t count = 0;
-  struct watchlist * ptr = *head_ptr;
-  struct watchlist * pre = *head_ptr;
-
-  while (ptr)
-    {
-      if (ptr->seen == S_FALSE) /* obsolete entry */
-	{
-	  if (ptr == pre)       /* at head        */
-	    {
-	      ptr       = pre->next;
-	      *head_ptr = pre->next;
-	      if (pre->str) 
-		SH_FREE(pre->str);
-	      SH_FREE(pre);
-	      pre       = ptr;
-	    }
-	  else
-	    {
-	      pre->next = ptr->next;
-	      if (ptr->str) 
-		SH_FREE(ptr->str);
-	      SH_FREE(ptr);
-	      ptr       = pre->next;
-	    }
-	}
-      else
-	{
-	  ++count;
-	  ptr->seen = S_FALSE; /* reset status */
-	  pre = ptr;
-	  ptr = ptr->next;
-	}
-    }
-  return count;
-}
-
-/* check if process is in list; if not, add it and return false
- */
-static int  is_in_list (struct watchlist ** head_ptr, 
-			char * str, unsigned long pid)
-{
-  struct watchlist * ptr = *head_ptr;
-
-  if (str)
-    {
-      while (ptr)
-	{
-	  if (ptr->str && (0 == strcmp(str, ptr->str)))
-	    {
-	      ptr->seen = S_TRUE;
-	      return S_TRUE;
-	    }
-	  ptr = ptr->next;
-	}
-    }
-  else
-    {
-      while (ptr)
-	{
-	  if (ptr->pid == pid)
-	    {
-	      ptr->seen = S_TRUE;
-	      return S_TRUE;
-	    }
-	  ptr = ptr->next;
-	}
-    }
-
-  ptr = SH_ALLOC(sizeof(struct watchlist));
-
-  if (str)
-    {
-      ptr->str = sh_util_strdup(str);
-    }
-  else
-    {
-      ptr->str = NULL;
-      ptr->pid = pid;
-    }
-  ptr->next = *head_ptr;
-  ptr->seen = S_TRUE;
-  *head_ptr = ptr;
-
-  return S_FALSE;
-}
-
-static int is_in_watchlist (const char *str, unsigned long num)
-{
-  struct watchlist * list = process_check;
-
-  while (list) 
-    {
-#ifdef HAVE_REGEX_H
-      if (0 == regexec(&(list->preg), str, 0, NULL, 0))
-	{
-	  list->seen = S_TRUE;
-	  list->pid  = num;
-	  return S_TRUE;
-	}
-#else
-      if (strstr(str, list->str)) 
-	{
-	  list->seen = S_TRUE;
-	  list->pid  = num;
-	  return S_TRUE;
-	}
-#endif
-      list = list->next;
-    }
-  return S_FALSE;
-} 
-
-/* These variables are not used anywhere. They only exist
- * to assign &userold, &user to them, which keeps gcc from
- * putting them into a register, and avoids the 'clobbered
- * by longjmp' warning. And no, 'volatile' proved insufficient.
- */
-void * sh_dummy_413_watchlist = NULL;
-
-static void check_watchlist (short * res)
-{
-  struct watchlist * list = process_check;
-  char * tmp;
-  size_t indx;
-
-  /* Take the address to keep gcc from putting them into registers. 
-   * Avoids the 'clobbered by longjmp' warning. 
-   */
-  sh_dummy_413_watchlist = (void*) &list;
-
-  while (list) 
-    {
-      if (list->seen == S_FALSE)
-	{
-	  /* avoid repetition of messages
-	   */
-	  if (S_FALSE == is_in_list(&list_missing, list->str, 0))
-	    {
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      tmp = sh_util_safe_name (list->str);
-	      sh_error_handle(sh_prochk_severity, FIL__, __LINE__, 0, 
-			      MSG_PCK_MISS,
-			      tmp);
-	      SH_FREE(tmp);
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	    }
-	}
-      else
-	{
-	  indx = list->pid - sh_prochk_minpid;
-
-	  if (list->pid < sh_prochk_maxpid && list->pid >= sh_prochk_minpid && 
-	      ((res[indx] & SH_PR_ANY) == 0) && /* not found         */
-	      ((res[indx] & SH_PR_PS)  != 0) && /* seen in first ps  */ 
-	      ((res[indx] & SH_PR_PS2) != 0))   /* seen in second ps */
-	    {
-	      /* fake process, thus considered missing
-	       */
-	      if (S_FALSE == is_in_list(&list_missing, list->str, 0))
-		{
-		  SH_MUTEX_LOCK(mutex_thread_nolog);
-		  tmp = sh_util_safe_name (list->str);
-		  sh_error_handle(sh_prochk_severity, FIL__, __LINE__, 0, 
-				  MSG_PCK_MISS, 
-				  tmp);
-		  SH_FREE(tmp);
-		  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-		}
-	    }
-	  list->seen = S_FALSE;
-	}
-      list = list->next;
-    }
-
-  sh_dummy_413_watchlist = NULL;
-  return;
-}
-
-/* Add 'str' to the list of watched processes for which
- * existence should be checked.
- */
-int sh_prochk_add_process (const char *str) 
-{
-  struct watchlist *new;
-  int               status;
-  char              errbuf[256];
-    
-  SL_ENTER(_("sh_prochk_add_process"));
-
-  if( str == NULL )
-    SL_RETURN(-1, _("sh_prochk_add_process") );
-
-  new       = SH_ALLOC(sizeof(struct watchlist));
-  new->next = process_check;
-  new->str  = sh_util_strdup(str);
-#ifdef HAVE_REGEX_H
-  status = regcomp(&(new->preg), str, REG_NOSUB|REG_EXTENDED);
-  if (status != 0)
-    {
-      regerror(status, &(new->preg), errbuf, sizeof(errbuf));
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN, 
-		      errbuf, _("sh_processes_add_process"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      SH_FREE(new->str);
-      SH_FREE(new);
-      SL_RETURN(-1, _("sh_prochk_add_process") );
-    }
-#endif
-  new->pid  = 0;
-  new->seen = S_FALSE;
-
-  process_check = new;
-  SL_RETURN(0, _("sh_prochk_add_process") );
-}
-
-/* severity
- */
-int sh_prochk_set_severity  (const char * c)
-{
-  char tmp[32];
-  tmp[0] = '='; tmp[1] = '\0';
-  sl_strlcat (tmp, c, 32);
-  return sh_error_set_level (tmp, &sh_prochk_severity);
-}
-
-
-
-/* Path to ps
- */
-int sh_prochk_set_pspath(const char *str) 
-{
-  SL_ENTER(_("sh_prochk_set_pspath"));
-
-  if (!str || ('/' != str[0]))
-    SL_RETURN((-1), _("sh_prochk_set_pspath"));
-  if (sh_prochk_pspath)
-    SH_FREE(sh_prochk_pspath);
-#ifdef SH_EVAL_SHELL
-  sh_prochk_pspath = sh_util_strdup (str);
-  SL_RETURN((0), _("sh_prochk_set_pspath"));
-#else
-  sh_prochk_pspath = NULL;
-  SL_RETURN((-1), _("sh_prochk_set_pspath"));
-#endif
-}
-
-/* argument for ps
- */
-int sh_prochk_set_psarg(const char *str) 
-{
-  SL_ENTER(_("sh_prochk_set_psarg"));
-
-  if (sh_prochk_psarg)
-    SH_FREE(sh_prochk_psarg);
-#ifdef SH_EVAL_SHELL
-  sh_prochk_psarg = sh_util_strdup (str);
-  SL_RETURN((0), _("sh_prochk_set_psarg"));
-#else
-  (void) str;
-  sh_prochk_psarg = NULL;
-  SL_RETURN((-1), _("sh_prochk_set_psarg"));
-#endif
-}
-
-
-/* Decide if we're active.
- */
-int sh_prochk_set_active(const char *str) 
-{
-  int value;
-    
-  SL_ENTER(_("sh_prochk_set_active"));
-
-  value = sh_util_flagval(str, &ShProchkActive);
-
-  SL_RETURN((value), _("sh_prochk_set_active"));
-}
-
-/* Are we on openvz.
- */
-static int openvz_hidden = 0;
-
-int sh_prochk_set_openvz(const char *str) 
-{
-  int value;
-    
-  SL_ENTER(_("sh_prochk_set_openvz"));
-
-  value = sh_util_flagval(str, &sh_prochk_openvz);
-
-  if (sh_prochk_openvz != S_FALSE) {
-    openvz_hidden = 1;
-  }
-
-  SL_RETURN((value), _("sh_prochk_set_openvz"));
-}
-
-/* Minimum PID
- */
-int sh_prochk_set_minpid(const char * str)
-{
-  size_t  value;
-  char * foo;
-  int    retval = 0;
-
-  SL_ENTER(_("sh_prochk_set_minpid"));
-
-  value = (size_t) strtoul(str, &foo, 0);
-  if (*foo != '\0')
-    retval = -1;
-  else
-    sh_prochk_minpid = value;
-
-  SL_RETURN((retval), _("sh_prochk_set_minpid"));
-}
-
-/* Maximum PID
- */
-static int userdef_maxpid = 0;
-
-int sh_prochk_set_maxpid(const char * str)
-{
-  size_t  value;
-  char * foo;
-  int    retval = -1;
-
-  SL_ENTER(_("sh_prochk_set_maxpid"));
-
-  value = (size_t) strtoul(str, &foo, 0);
-
-  if (*foo == '\0' && S_TRUE == sl_ok_adds(value, 1)) {
-    sh_prochk_maxpid = value + 1;
-    userdef_maxpid   = 1;
-    retval = 0;
-  }
-
-  SL_RETURN((retval), _("sh_prochk_set_maxpid"));
-}
-
-int sh_prochk_set_interval (const char * c)
-{
-  int retval = 0;
-  long val;
-
-  SL_ENTER(_("sh_prochk_set_interval"));
-  val = strtol (c, (char **)NULL, 10);
-  if (val <= 0)
-    {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
-		       _("process check interval"), c);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      retval = -1;
-    }
-  else
-    {
-      sh_prochk_interval = (time_t) val;
-    }
-  SL_RETURN(retval, _("sh_prochk_set_interval"));
-}
-
-
-
-/* Recurse to the end of the list and then free the data as we return
- * back up towards the start, making sure to free any strdupped strings
- */
-static void sh_prochk_free_list(struct watchlist *head) 
-{
-  if ( head != NULL ) 
-    {
-      sh_prochk_free_list(head->next);
-      if (head->str)
-	SH_FREE(head->str);
-#ifdef HAVE_REGEX_H
-      regfree(&(head->preg));
-#endif
-      SH_FREE(head);
-    }
-  return;
-}
-
-#if defined(__linux__)
-#define PROC_PID_MAX _("/proc/sys/kernel/pid_max")
-
-static int proc_max_pid (size_t * procpid)
-{
-  char * ret;
-  unsigned long  pid;
-  FILE * fd;
-  char   str[128];
-  char * ptr;
-
-  SL_ENTER(_("proc_max_pid"));
-
-  if (userdef_maxpid != 0)
-    SL_RETURN((-1), _("proc_max_pid"));
-    
-  if (0 == access(PROC_PID_MAX, R_OK)) /* flawfinder: ignore */
-    {
-      if (NULL != (fd = fopen(PROC_PID_MAX, "r")))
-	{
-	  str[0] = '\0';
-	  ret = fgets(str, 128, fd);
-	  if (ret && *str != '\0')
-	    {
-	      pid = strtoul(str, &ptr, 0);
-	      if (*ptr == '\0' || *ptr == '\n')
-		{
-		  sl_fclose(FIL__, __LINE__, fd);
-		  *procpid = (size_t) pid;
-		  SL_RETURN(0, _("proc_max_pid"));
-		}
-	    }
-	  sl_fclose(FIL__, __LINE__, fd);
-	}
-    }
-  SL_RETURN((-1), _("proc_max_pid"));
-}
-#else
-static int proc_max_pid(size_t * dummy)
-{
-  (void) dummy;
-  return -1;
-}
-#endif
-
-static void sh_processes_tlist (char * list, size_t len, short res)
-{
-  if (res & SH_PR_PS)       sl_strlcat(list, _(" ps(initial)"), len);
-  if (res & SH_PR_CHDIR)    sl_strlcat(list, _(" chdir"), len);
-  if (res & SH_PR_OPENDIR)  sl_strlcat(list, _(" opendir"), len);
-  if (res & SH_PR_LSTAT)    sl_strlcat(list, _(" lstat"), len);
-  if (res & SH_PR_PRIORITY) sl_strlcat(list, _(" getpriority"), len);
-  if (res & SH_PR_SCHED)    sl_strlcat(list, _(" sched_getparam"), len);
-  if (res & SH_PR_GETSID)   sl_strlcat(list, _(" getsid"), len);
-  if (res & SH_PR_GETPGID)  sl_strlcat(list, _(" getpgid"), len);
-  if (res & SH_PR_KILL)     sl_strlcat(list, _(" kill"), len);
-  if (res & SH_PR_STATVSF)  sl_strlcat(list, _(" statvfs"), len);
-  if (res & SH_PR_PS2)      sl_strlcat(list, _(" ps(final)"), len);
-  return;
-}
-
-
-static short sh_processes_check (pid_t pid, short res)
-{
-  int  have_checks = 0;
-  int  need_checks = 0;
-#ifdef HAVE_PROCFS
-  char path[128];
-  struct stat buf;
-  DIR * dir;
-  int  retval;
-#if defined(HAVE_STATVFS) && !defined(__FreeBSD__)
-  struct statvfs vfsbuf;
-#endif
-#endif
-
-#if !defined(sun) && !defined(__sun) && !defined(__sun__) && !defined(__OpenBSD__) && !defined(__APPLE__)
-#ifdef _POSIX_PRIORITY_SCHEDULING
-  struct sched_param p;
-#endif
-#endif
-
-  if (0 == kill(pid, 0))
-    { 
-      res |= SH_PR_KILL;    res |= SH_PR_ANY; ++have_checks;
-      ++need_checks;
-    }
-  else if (errno != EPERM)
-    {
-      ++need_checks;
-    }
-
-
-#ifdef HAVE_GETPGID
-  if ((pid_t)-1 != getpgid(pid))
-    { 
-      res |= SH_PR_GETPGID; res |= SH_PR_ANY; ++have_checks;
-    }
-  ++need_checks;
-#endif
-
-#ifdef HAVE_GETSID
-  if ((pid_t)-1 != getsid(pid))
-    { 
-      res |= SH_PR_GETSID;  res |= SH_PR_ANY; ++have_checks;
-    }
-  ++need_checks;
-#endif
-
-  /* sched_getparam() is broken on solaris 10, may segfault in librt
-   * also not on MacOS
-   */
-#if !defined(sun) && !defined(__sun) && !defined(__sun__) && !defined(__OpenBSD__) && !defined(__APPLE__)
-#ifdef _POSIX_PRIORITY_SCHEDULING
-  if (0 == sched_getparam (pid, &p))
-    { 
-      res |= SH_PR_SCHED;   res |= SH_PR_ANY; ++have_checks;
-    }
-  ++need_checks;
-#endif
-#endif
-
-#ifdef HAVE_GETPRIORITY
-  errno = 0;
-  if (((-1) == getpriority (PRIO_PROCESS, (int) pid)) && (errno == ESRCH));
-  else
-    { 
-      res |= SH_PR_PRIORITY; res |= SH_PR_ANY; ++have_checks;
-    }
-  ++need_checks;
-#endif
-
-#ifdef HAVE_PROCFS
-  sl_snprintf (path, sizeof(path), "/proc/%ld", (unsigned long) pid);
-
-  do {
-    retval = lstat (path, &buf);
-  } while (retval < 0 && errno == EINTR);
-
-  if (0 == retval)
-    { 
-      res |= SH_PR_LSTAT;   res |= SH_PR_ANY; ++have_checks;
-    }
-  ++need_checks;
-
-  if (NULL != (dir = opendir(path)))
-    {
-      res |= SH_PR_OPENDIR; res |= SH_PR_ANY; ++have_checks;
-      closedir(dir);
-    }
-  ++need_checks;
-
-#if defined(HAVE_STATVFS) && !defined(__FreeBSD__)
-  do {
-    retval = statvfs (path, &vfsbuf);
-  } while (retval < 0 && errno == EINTR);
-
-  if (0 == retval)
-    { 
-      res |= SH_PR_STATVSF;   res |= SH_PR_ANY; ++have_checks;
-    }
-  ++need_checks;
-#endif
-
-#if !defined(SH_PROFILE)
-  if (0 == chdir(path))
-    {
-      res |= SH_PR_CHDIR;   res |= SH_PR_ANY; ++have_checks;
-      do {
-	retval = chdir ("/");
-      } while (retval < 0 && errno == EINTR);
-    }
-  ++need_checks;
-#endif
-#endif
-
-  if (have_checks == need_checks)
-    {
-      res |= SH_PR_ALL;
-    }
-  return res;
-}
-
-extern int flag_err_debug;
-
-static int sh_processes_readps (FILE * in, short * res, 
-				char * str, size_t len, 
-				short flag, pid_t pid)
-{
-  int  cc; 
-  volatile unsigned int  lnum   = 0;
-  volatile unsigned long num    = 0;
-  char c;
-  unsigned int  pos = 0;
-#define SH_TWAIT_MAX 60
-  volatile unsigned int  twait = 0;
-  char tstr[256];
-  enum { SKIP_TO_WS, SKIP_WS, SKIP_TO_WS2, SKIP_WS2, GET_NUM, SKIP_END, GET_NUM2 } line;
-
-  SL_ENTER(_("sh_processes_readps"));
-
-  if (!in) {
-    SL_RETURN((-1), _("sh_processes_readps"));
-  }
-
-  tstr[(sizeof(tstr)-1)] = '\0';
-  tstr[0]                = '\0';
-  line = SKIP_END;		/* Skip 1st line */
-
-  do
-    {
-      cc = fgetc(in);
-
-      if (EOF == cc) 
-	{
-	  if (feof(in))
-	    {
-	      break;
-	    }
-	  else if ((errno == EAGAIN) && (twait < SH_TWAIT_MAX))
-	    {
-	      clearerr(in);
-	      retry_msleep(1, 0);
-	      ++twait;
-	      continue;
-	    }
-#ifdef HOST_IS_OPENBSD
-	  else if (errno == ENODEV)
-	    {
-	      clearerr(in);
-	      continue;
-	    }
-#endif
-	  else
-	    {
-	      char errbuf[SH_ERRBUF_SIZE];
-
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, errno, MSG_E_SUBGEN,
-			      sh_error_message(errno, errbuf, sizeof(errbuf)),
-			      _("sh_processes_readps"));
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	      break;
-	    }
-	}
-
-      c = (char) cc;
-
-      if (pos < (sizeof(tstr)-1))
-	{ 
-	  tstr[pos] = c; ++pos; 
-	}
-
-      switch(line)
-	{
-	case SKIP_END:
-	  if (c == '\n')
-	    { 
-	      tstr[pos-1] = '\0';
-	      if (flag_err_debug == S_TRUE)
-		{
-		  SH_MUTEX_LOCK(mutex_thread_nolog);
-		  sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, num, 
-				  MSG_E_SUBGEN,
-				  tstr,
-				  _("sh_processes_readps"));
-		  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-		}
-	      /* fprintf(stderr, "<%ld> %s\n", num, tstr); */
-	      line = SKIP_WS; pos = 0;
-	      if (str != NULL && num == (unsigned long) pid)
-		sl_strlcpy(str, tstr, len);
-	      if (lnum != 0)
-		is_in_watchlist (tstr, num);
-	      ++lnum;
-	    }
-	  break;
-	case SKIP_TO_WS:
-	  if (!isspace(cc))
-	    break;
-	  line = SKIP_WS;
-	  /* fallthrough */
-	case SKIP_WS:
-	  if (isspace(cc))
-	    break;
-	  num  = 0;
-	  line = GET_NUM;
-	  /* fallthrough */
-	case GET_NUM:
-	  if (isdigit(cc))
-	    {
-	      num = num * 10 + (c - '0');
-	      break;
-	    }
-	  else if (isspace(cc))
-	    {
-#ifdef PS_THREADS
-	      num  = 0;
-	      line = SKIP_WS2;
-#else
-	      if (num < sh_prochk_maxpid && num >= sh_prochk_minpid)
-		{
-		  res[num - sh_prochk_minpid] |= flag;
-		}
-	      line = SKIP_END;
-#endif
-	      break;
-	    }
-	  else
-	    {
-	      line = SKIP_TO_WS;
-	      break;
-	    }
-	case SKIP_TO_WS2:
-	  if (!isspace(cc))
-	    break;
-	  line = SKIP_WS2;
-	  /* fallthrough */
-	case SKIP_WS2:
-	  if (isspace(cc))
-	    break;
-	  num  = 0;
-	  line = GET_NUM2;
-	  /* fallthrough */
-	case GET_NUM2:
-	  if (isdigit(cc))
-	    {
-	      num = num * 10 + (c - '0');
-	      break;
-	    }
-	  else if (isspace(cc))
-	    {
-	      if (num < sh_prochk_maxpid && num >= sh_prochk_minpid)
-		{
-		  res[num - sh_prochk_minpid] |= flag;
-		}
-	      line = SKIP_END;
-	      break;
-	    }
-	  else
-	    {
-	      line = SKIP_TO_WS2;
-	      break;
-	    }
-	default:
-	  SL_RETURN ((-1), _("sh_processes_readps"));
-	}
-    } while (1);
-
-  if (ferror(in))
-    {
-      SL_RETURN ((-1), _("sh_processes_readps"));
-    }
-
-  SL_RETURN ((0), _("sh_processes_readps"));
-}
-
-static int sh_processes_runps (short * res, char * str, size_t len, 
-			       short flag, pid_t pid)
-{
-  sh_tas_t task;
-
-  int    status = 0;
-  char * p;
-  int retval = 0;
-  char  dir[SH_PATHBUF];
-
-  SL_ENTER(_("sh_processes_runps"));
-
-  sh_ext_tas_init(&task);
-  p = sh_unix_getUIDdir (SH_ERR_ERR, task.run_user_uid, dir, sizeof(dir));
-  if (p)
-    {
-      (void) sh_ext_tas_add_envv (&task, _("HOME"), p);
-    }
-  (void) sh_ext_tas_add_envv (&task, _("SHELL"), 
-			      _("/bin/sh")); 
-  (void) sh_ext_tas_add_envv (&task, _("PATH"),  
-			      _("/sbin:/usr/sbin:/bin:/usr/bin")); 
-  if (sh.timezone != NULL)
-    {
-      (void) sh_ext_tas_add_envv(&task,  "TZ", sh.timezone);
-    }
-
-  if (!sh_prochk_pspath)
-    sh_ext_tas_command(&task,  PSPATH);
-  else
-    sh_ext_tas_command(&task,  sh_prochk_pspath);
-
-  (void) sh_ext_tas_add_argv(&task,  _("ps"));
-
-  if (!sh_prochk_psarg)
-    {
-#ifdef PS_THREADS
-      (void) sh_ext_tas_add_argv(&task,  _("-eT"));
-#else
-      (void) sh_ext_tas_add_argv(&task,  PSARG);
-#endif
-    }
-  else
-    {
-      (void) sh_ext_tas_add_argv(&task,  sh_prochk_psarg);
-    }
-
-  task.rw = 'r';
-  task.fork_twice = S_FALSE;
-
-  status = sh_ext_popen(&task);
-  if (status != 0)
-    {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, status, MSG_E_SUBGEN, 
-		      _("Could not open pipe"), _("sh_processes_runps"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      SL_RETURN ((-1), _("sh_processes_runps"));
-    }
-
-  /* read from the open pipe
-   */
-  if (task.pipe != NULL)
-    {
-      retval = sh_processes_readps (task.pipe, res, str, len, flag, pid);
-    }
-
-  /* close pipe and return exit status
-   */
-  (void) sh_ext_pclose(&task);
-  sh_ext_tas_free (&task);
-  SL_RETURN ((retval), _("sh_processes_runps"));
-}
-
-/* Check whether there is a visible process
- * with PID = i + 1024
- */
-static size_t p_store = 0;
-
-static int openvz_ok(short * res, size_t i)
-{
-
-  if (sh_prochk_openvz == S_FALSE) {
-    return 0;
-  }
-
-  i += 1024;
-
-  if (i >= sh_prochk_size) {
-    return 0;
-  }
-
-  if ( ((res[i] & SH_PR_PS) || (res[i] & SH_PR_PS2)) && (res[i] & SH_PR_ANY))
-    {
-      /* This is a system process corresponding to a 'virtual'
-       * process that has a PID offset by 1024
-       */
-      return 1;
-    }
-
-  if (openvz_hidden > 0)
-    {
-      p_store = i;
-      --openvz_hidden;
-      return 1;
-    }
-  else if (i == p_store)
-    {
-      return 1;
-    }
-
-  return 0;
-}
-
-static int sh_process_check_int (short * res)
-{
-  volatile size_t i;
-  size_t j;
-  char  tests[512];
-  volatile int   retval;
-
-  pid_t this_pid;
-
-  SL_ENTER(_("sh_process_check_int"));
-
-  this_pid = getpid();
-
-  if (!res)
-    {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-		      _("Internal error: NULL argument, switching off"), 
-		      _("sh_process_check_int"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      SL_RETURN ((-1), _("sh_process_check_int"));
-    }
-
-  retval = sh_processes_runps (res, NULL, 0, SH_PR_PS, 0);
-
-  for (i = sh_prochk_minpid; i != sh_prochk_maxpid; ++i)
-    {
-      j      = i - sh_prochk_minpid; 
-      res[j] = sh_processes_check ((pid_t) i, res[j]);
-    }
-
-  retval += sh_processes_runps (res, NULL, 0, SH_PR_PS2, 0);
-
-  if (retval != 0)
-    {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-		      _("Failed to run ps, switching off"), 
-		      _("sh_process_check_int"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      SL_RETURN ((-1), _("sh_process_check_int"));
-    }
-
-  /* Evaluate results
-   */
-  for (i = sh_prochk_minpid; i != sh_prochk_maxpid; ++i)
-    {
-      /* don't check the current process
-       */
-      if (i == (size_t) this_pid)
-	continue;
-
-      j      = i - sh_prochk_minpid;
-
-      if (((res[j] & SH_PR_PS) != 0) || ((res[j] & SH_PR_PS2) != 0))
-	{
-	  res[j] |= SH_PR_PS_ANY;
-	}
-      else
-	{
-	  res[j] &= ~SH_PR_PS_ANY;
-	}
-
-      tests[0] = '\0';
-
-      if ((res[j] & SH_PR_ANY) || (res[j] & SH_PR_PS_ANY))
-	{
-	  /* list all tests where the pid was found
-	   */
-	  sh_processes_tlist (tests, sizeof(tests), res[j]);
-
-	  /* 
-	   * case 1: in ps and found 
-	   */
-	  if ((res[j] & SH_PR_PS_ANY) && (res[j] & SH_PR_ANY))
-	    {
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_PCK_OK, 
-			      (unsigned long) i, tests);
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	    }
-
-	  /* 
-	   * case 2: not in ps and found
-	   */
-	  else if ((res[j] & SH_PR_PS_ANY) == 0) 
-	    {
-	      res[j] = sh_processes_check ((pid_t) i, 0);
-	      /*
-	       * if still there, it is real and hidden
-	       */
-	      if ((res[j] & SH_PR_ANY) && !openvz_ok(res, j))
-		{
-		  if (S_FALSE == is_in_list(&list_hidden, NULL, i))
-		    {
-		      char   user[16];
-		      char * aout;
-		      char * safe;
-
-		      SH_MUTEX_LOCK(mutex_thread_nolog);
-		      aout = get_user_and_path ((pid_t) i, user, sizeof(user));
-		      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-
-		      if (aout)
-			{
-			  safe = sh_util_safe_name (aout);
-			  SH_MUTEX_LOCK(mutex_thread_nolog);
-			  sh_error_handle(sh_prochk_severity, FIL__, __LINE__, 0, 
-					  MSG_PCK_P_HIDDEN,
-					  (unsigned long) i, tests, safe, user);
-			  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-			  SH_FREE(safe);
-			  SH_FREE(aout);
-			}
-		      else
-			{
-			  SH_MUTEX_LOCK(mutex_thread_nolog);
-			  sh_error_handle(sh_prochk_severity, FIL__, __LINE__, 0, 
-					  MSG_PCK_HIDDEN,
-					  (unsigned long) i, tests);
-			  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-			}
-		    }
-		}
-	    }
-
-	  /*
-	   * case 3: in ps, but not found
-	   */
-	  else
-	    {
-	      if (((res[j] & SH_PR_PS) != 0) && ((res[j] & SH_PR_PS2) != 0))
-		{
-		  if (S_FALSE == is_in_list(&list_fake, NULL, i))
-		    {
-		      SH_MUTEX_LOCK(mutex_thread_nolog);
-		      sh_error_handle(sh_prochk_severity, FIL__, __LINE__, 0, 
-				      MSG_PCK_FAKE, 
-				      (unsigned long) i, tests);
-		      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-		    }
-		}
-	    }
-	}
-    } /* loop end */
-
-  check_watchlist (res);
-
-  SL_RETURN (0, _("sh_process_check_int"));
-}
-
-/* Initialise. 
- */
-static int sh_prochk_init_internal(void) 
-{
-  SL_ENTER(_("sh_prochk_init"));
-
-  (void) proc_max_pid (&sh_prochk_maxpid);
-
-  if (sh_prochk_minpid > sh_prochk_maxpid)
-    ShProchkActive = S_FALSE;
-
-  /* We need to free anything allocated by the configuration functions if
-   * we find that the module is to be left inactive - otherwise _reconf()
-   * won't quite work. 
-   */
-  if( ShProchkActive == S_FALSE ) 
-    {
-      sh_prochk_free_list(process_check);
-      process_check = NULL;
-      SL_RETURN(-1, _("sh_prochk_init"));
-    }
-
-  sh_prochk_size = sh_prochk_maxpid - sh_prochk_minpid;
-
-  if (sh_prochk_res == NULL)
-    {
-      sh_prochk_res  = SH_ALLOC(sizeof(short) * sh_prochk_size);
-    }
-  memset (sh_prochk_res, 0, sizeof(short) * sh_prochk_size);
-  
-  SL_RETURN(0, _("sh_prochk_init"));
-}
-
-int sh_prochk_init (struct mod_type * arg)
-{
-#ifndef HAVE_PTHREAD
-  (void) arg;
-#endif
-
-  if (ShProchkActive == S_FALSE)
-    return SH_MOD_FAILED;
-#ifdef HAVE_PTHREAD
-  if (arg != NULL && arg->initval < 0 &&
-      (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
-    {
-      if (0 == sh_pthread_create(sh_threaded_module_run, (void *)arg))
-	return SH_MOD_THREAD;
-      else
-	return SH_MOD_FAILED;
-    }
-  else if (arg != NULL && arg->initval == SH_MOD_THREAD &&
-	   (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
-    {
-      sh_prochk_init_internal();
-      return SH_MOD_THREAD;
-    }
-#endif
-  return sh_prochk_init_internal();
-}
-
-int sh_prochk_timer(time_t tcurrent) 
-{
-  static time_t lastcheck = 0;
-
-  SL_ENTER(_("sh_prochk_timer"));
-  if ((time_t) (tcurrent - lastcheck) >= sh_prochk_interval)
-    {
-      lastcheck  = tcurrent;
-      SL_RETURN((-1), _("sh_prochk_timer"));
-    }
-  SL_RETURN(0, _("sh_prochk_timer"));
-}
-
-int sh_prochk_check(void) 
-{
-  int status;
-
-  SL_ENTER(_("sh_prochk_check"));
-
-  SH_MUTEX_LOCK(mutex_proc_check);
-
-  status = 0;
-
-  if( ShProchkActive != S_FALSE )
-    {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_PCK_CHECK, 
-		      (unsigned long) sh_prochk_minpid, 
-		      (unsigned long) (sh_prochk_maxpid-1));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-
-      if (sh_prochk_res) {
-	memset (sh_prochk_res, 0, sizeof(short) * sh_prochk_size);
-      }
-      status = sh_process_check_int(sh_prochk_res);
-
-      if (status != 0)
-	ShProchkActive = S_FALSE;
-
-      /* clean out old entries which are not marked 
-       * as missing/hidden/fake anymore
-       */
-      clean_list (&list_missing);
-      clean_list (&list_hidden);
-      clean_list (&list_fake);
-    }
-
-  SH_MUTEX_UNLOCK(mutex_proc_check);
-
-  SL_RETURN(status, _("sh_prochk_check"));
-}
-
-/* Free our lists and the associated memory 
- */
-int sh_prochk_cleanup(void) 
-{
-  SL_ENTER(_("sh_prochk_cleanup"));
-
-  sh_prochk_reconf();
-
-  if (list_missing) {
-    kill_list(list_missing);
-    list_missing = NULL;
-  }
-  if (list_hidden) {
-    kill_list(list_hidden);
-    list_hidden  = NULL;
-  }
-  if (list_fake) {
-    kill_list(list_fake);
-    list_fake    = NULL;
-  }
-  
-  SL_RETURN(0, _("sh_prochk_cleanup"));
-}
-
-/* Free our lists and the associated memory 
- */
-int sh_prochk_reconf(void) 
-{
-  SL_ENTER(_("sh_prochk_reconf"));
-
-  SH_MUTEX_LOCK(mutex_proc_check);
-  userdef_maxpid     = 0;
-  sh_prochk_maxpid   = 0x8000;
-  sh_prochk_minpid   = 0x0001;
-  sh_prochk_interval = SH_PROCHK_INTERVAL;
-  sh_prochk_openvz   = S_FALSE;
-  p_store            = 0;
-  openvz_hidden      = 0;
-
-  sh_prochk_free_list(process_check);
-  process_check = NULL;
-  if (sh_prochk_res != NULL)
-    SH_FREE(sh_prochk_res);
-  sh_prochk_res = NULL;
-
-  if (sh_prochk_psarg)
-    SH_FREE(sh_prochk_psarg);
-  sh_prochk_psarg = NULL;
-  if (sh_prochk_pspath)
-    SH_FREE(sh_prochk_pspath);
-  sh_prochk_pspath = NULL;
-  SH_MUTEX_UNLOCK(mutex_proc_check);
-
-  SL_RETURN(0, _("sh_prochk_reconf"));
-}
-
-/* #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) */
-#endif
-
-/* #ifdef SH_USE_PROCESSCHECK */
-#endif
-
-
-#ifdef SH_CUTEST
-#include "CuTest.h"
-
-void Test_processcheck_watchlist_ok (CuTest *tc) {
-#if defined(SH_USE_PROCESSCHECK) && (defined(SH_WITH_CLIENT) || defined(SH_STANDALONE))
-  CuAssertTrue(tc, 0 == sh_prochk_add_process("init"));
-  CuAssertTrue(tc, 
-	       S_TRUE  == is_in_watchlist("    1 ?        00:00:00 init", 0));
-  CuAssertTrue(tc, 
-	       S_FALSE == is_in_watchlist("    1 ?        00:00:00 flix", 0));
-  CuAssertTrue(tc, 
-	       S_TRUE  == is_in_watchlist("25218 ?        SNs    0:01 /usr/sbin/init -k start -DSSL", 0));
-  CuAssertTrue(tc, 
-	       S_FALSE  == is_in_watchlist("25218 ?        SNs    0:01 /usr/sbin/apache2 -k start -DSSL", 0));
-
-
-  sh_prochk_free_list(process_check);
-  process_check = NULL;
-  CuAssertTrue(tc, S_FALSE == is_in_watchlist("init", 0));
-
-  CuAssertTrue(tc, 0 == sh_prochk_add_process("init"));
-  CuAssertTrue(tc, 0 == sh_prochk_add_process("ssh"));
-  CuAssertTrue(tc, 0 == sh_prochk_add_process("syslog"));
-  CuAssertTrue(tc, S_TRUE  == is_in_watchlist("init", 0));
-  CuAssertTrue(tc, S_TRUE  == is_in_watchlist("ssh", 0));
-  CuAssertTrue(tc, S_TRUE  == is_in_watchlist("syslog", 0));
-
-  sh_prochk_free_list(process_check);
-  process_check = NULL;
-  CuAssertTrue(tc, S_FALSE == is_in_watchlist("init", 0));
-  CuAssertTrue(tc, S_FALSE == is_in_watchlist("ssh", 0));
-  CuAssertTrue(tc, S_FALSE == is_in_watchlist("syslog", 0));
-#else
-  (void) tc; /* fix compiler warning */
-#endif
-  return;
-}
-
-void Test_processcheck_listhandle_ok (CuTest *tc) {
-#if defined(SH_USE_PROCESSCHECK) && (defined(SH_WITH_CLIENT) || defined(SH_STANDALONE))
-  CuAssertTrue(tc, S_FALSE == is_in_list(&list_missing, "init", 0));
-  CuAssertTrue(tc, S_TRUE  == is_in_list(&list_missing, "init", 0));
-  CuAssertTrue(tc, S_FALSE == is_in_list(&list_missing, "foobar", 0));
-  CuAssertTrue(tc, S_TRUE  == is_in_list(&list_missing, "foobar", 0));
-
-  if (list_missing)
-    kill_list(list_missing);
-  list_missing = NULL;
-
-  CuAssertTrue(tc, S_FALSE == is_in_list(&list_missing, "init", 0));
-  CuAssertTrue(tc, S_TRUE  == is_in_list(&list_missing, "init", 0));
-  CuAssertTrue(tc, S_FALSE == is_in_list(&list_missing, "foobar", 0));
-  CuAssertTrue(tc, S_TRUE  == is_in_list(&list_missing, "foobar", 0));
-
-  if (list_missing)
-    kill_list(list_missing);
-  list_missing = NULL;
-
-  CuAssertTrue(tc, S_FALSE == is_in_list(&list_missing, "init", 0));
-  CuAssertTrue(tc, S_TRUE  == is_in_list(&list_missing, "init", 0));
-  CuAssertTrue(tc, S_FALSE == is_in_list(&list_missing, "foobar", 0));
-  CuAssertTrue(tc, S_TRUE  == is_in_list(&list_missing, "foobar", 0));
-
-  CuAssertTrue(tc, 2  == clean_list(&list_missing));
-  CuAssertPtrNotNull(tc, list_missing);
-
-  CuAssertTrue(tc, S_TRUE  == is_in_list(&list_missing, "init", 0));
-  CuAssertTrue(tc, S_TRUE  == is_in_list(&list_missing, "foobar", 0));
-
-  CuAssertTrue(tc, 2  == clean_list(&list_missing));
-  CuAssertPtrNotNull(tc, list_missing);
-
-  CuAssertTrue(tc, 0  == clean_list(&list_missing));
-  CuAssertTrue(tc, NULL == list_missing);
-#else
-  (void) tc; /* fix compiler warning */
-#endif
-  return;
-}
-
-
-/* #ifdef SH_CUTEST */
-#endif
-
Index: trunk/src/sh_pthread.c
===================================================================
--- trunk/src/sh_pthread.c	(revision 591)
+++ 	(revision )
@@ -1,323 +1,0 @@
-#include "config_xor.h"
-
-#include "sh_pthread.h"
-
-#ifdef HAVE_PTHREAD
-
-#include <signal.h>
-#include "sh_calls.h"
-#include "sh_modules.h"
-extern volatile  int      sh_thread_pause_flag;
-
-SH_MUTEX_INIT(mutex_skey,         PTHREAD_MUTEX_INITIALIZER);
-SH_MUTEX_INIT(mutex_resolv,       PTHREAD_MUTEX_INITIALIZER);
-SH_MUTEX_INIT(mutex_pwent,        PTHREAD_MUTEX_INITIALIZER);
-SH_MUTEX_INIT(mutex_readdir,      PTHREAD_MUTEX_INITIALIZER);
-SH_MUTEX_INIT(mutex_thread_nolog, PTHREAD_MUTEX_INITIALIZER);
-
-int sh_pthread_setsigmask(int how, const void *set, void *oldset)
-{
-  return pthread_sigmask(how, (const sigset_t *)set, (sigset_t *)oldset);
-}
-
-void sh_pthread_mutex_unlock (void *arg)
-{
-  (void) pthread_mutex_unlock ((pthread_mutex_t *)arg);
-  return;
-}
-
-int sh_pthread_init_threadspecific(void)
-{
-  int rc = 0;
-#ifdef SH_STEALTH
-  do {
-    extern int sh_g_thread(void);
-
-    rc = sh_g_thread();
-  } while (0);
-#endif
-
-  return rc;
-}
-
-
-/* 
- *  ----  Utilities for modules  ----
- */
-
-/* MODULES: init()
- *
- * #ifdef HAVE_PTHREAD
- *  if (arg != NULL)
- *    {
- *      if (0 == sh_pthread_create(sh_threaded_module_run, (void *)arg))
- *	  return SH_MOD_THREAD;
- *      else
- *	  return SH_MOD_FAILED;
- *    }
- * #else
- *  return sh_utmp_init_internal();
- * #endif
- *
- *
- *          sh_threaded_module_run(module_struct) 
- *             -- calls internal init, 
- *             -- polls timer, 
- *             -- runs module check,
- *             -- runs sh_pthread_testcancel()
- *             -- returns (return == exit)
- */
-
-#define SH_NUM_THREADS 16
-static pthread_t threads[SH_NUM_THREADS];
-static int       ithread[SH_NUM_THREADS];
-static pthread_mutex_t  create_mutex = PTHREAD_MUTEX_INITIALIZER;
-
-int sh_pthread_create(void *(*start_routine)(void*), void *arg)
-{
-  int rc, nthread = 1;
-  sigset_t signal_set;
-  int retval = 0;
-
-  pthread_mutex_lock(&create_mutex);
-
-  /* block all signals 
-   */
-  sigfillset( &signal_set );
-#if defined(SCREW_IT_UP)
-  /*
-   * raise(SIGTRAP) sends to same thread, like 
-   * pthread_kill(pthread_self(), sig); so we need to unblock the
-   * signal. 
-   */
-  sigdelset( &signal_set, SIGTRAP );
-#endif 
-  pthread_sigmask( SIG_BLOCK, &signal_set, NULL );
-
-  /* find a free slot in threads[]
-   */
-  while (nthread < SH_NUM_THREADS) 
-    {
-      if (ithread[nthread] == 0)
-	break;
-      ++nthread;
-      if (nthread == SH_NUM_THREADS)
-	{
-	  retval = -1;
-	  goto err_out;
-	}
-    } 
-
-  rc = pthread_create(&threads[nthread], NULL, start_routine, arg);
-  if (rc != 0)
-    {
-      retval = -1;
-      goto err_out;
-    }
-
-  ithread[nthread] = 1;
-
- err_out:
-  pthread_sigmask( SIG_UNBLOCK, &signal_set, NULL );
-  pthread_mutex_unlock(&create_mutex);
-  return retval;
-}
-
-int sh_pthread_cancel_all()
-{
-  int i;
-  int ret = 0;
-
-  SH_MUTEX_LOCK(create_mutex);
-
-  for (i = 1; i < SH_NUM_THREADS; ++i)
-    {
-      if (ithread[i] != 0)
-	if (0 != pthread_cancel(threads[i]))
-	  ithread[i] = 0;
-    }
-
-  for (i = 1; i < SH_NUM_THREADS; ++i)
-    {
-      if (ithread[i] != 0)
-	pthread_join(threads[i], NULL);
-      ithread[i] = 0;
-    }
-
-  SH_MUTEX_UNLOCK(create_mutex);
-  return ret;
-}
-
-/* ---- Utility functions for modules ----
- */
-
-#undef  S_TRUE
-#define S_TRUE    1
-#undef  S_FALSE
-#define S_FALSE   0
-
-void sh_threaded_module_cleanup(void *arg)
-{
-  sh_mtype * this_module = (sh_mtype *) arg;
-  this_module->mod_cleanup();
-  this_module->initval = -1;
-  return;
-}
-
-void * sh_threaded_module_run(void *arg)
-{
-  sh_mtype * this_module = (sh_mtype *) arg;
-
-  /* First we lock the module. This ensures that it cannot be
-   * run twice.
-   */
-  pthread_cleanup_push(sh_pthread_mutex_unlock, (void*) &(this_module->mod_mutex));
-  pthread_mutex_lock(&(this_module->mod_mutex));
-
-  if (0 == sh_pthread_init_threadspecific())
-    {
-
-      if (0 == this_module->mod_init(NULL))
-	{
-	  pthread_cleanup_push(sh_threaded_module_cleanup, arg);
-
-	  while (1)
-	    {
-	      if (sh_thread_pause_flag != S_TRUE)
-		{
-		  this_module->flags &= ~SH_MODFL_ISPAUSED;
-		  
-		  if (0 != this_module->mod_timer(time(NULL)))
-		    {
-		      /* If module has been de-activated on reconfigure,
-		       * mod_check() must return non-zero.
-		       * The mod_cleanup() routine must then enable the 
-		       * module to be re-activated eventually.
-		       */
-		      if (0 != this_module->mod_check())
-			break;
-		      pthread_testcancel();
-		    }
-		}
-	      else
-		{
-		  this_module->flags |= SH_MODFL_ISPAUSED;
-		}
-	      if (0 == (SH_MODFL_NOTIMER & this_module->flags) ||
-		  sh_thread_pause_flag == S_TRUE)
-		retry_msleep(1,0);
-	    }
-
-	  pthread_cleanup_pop(1); /* notreached,but required */
-	}
-    }
-
-  pthread_cleanup_pop(1);
-
-  return NULL;
-}
-
-
-/*
- *  ----  Implementation of recursive mutexes from libxml2  ----
- */
-#if !defined(HAVE_PTHREAD_MUTEX_RECURSIVE)
-/**
- * libxml2 threads.c: set of generic threading related routines 
- *
- * Gary Pennington <Gary.Pennington@uk.sun.com>
- * daniel@veillard.com
- 
- * Except where otherwise noted in the source code (e.g. the files hash.c,
- * list.c and the trio files, which are covered by a similar licence but
- * with different Copyright notices) all the files are:
- *
- *    Copyright (C) 1998-2003 Daniel Veillard.  All Rights Reserved.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is fur-
- * nished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIT-
- * NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
- * DANIEL VEILLARD BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CON-
- * NECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- *
- * Except as contained in this notice, the name of Daniel Veillard shall not
- * be used in advertising or otherwise to promote the sale, use or other deal-
- * ings in this Software without prior written authorization from him.
- */
-
-/* Modified NewRMutex -> InitRMutex. We use a static structure, rather than 
- * allocating one. Also dropped code for non-POSIX OSes.
- */
-void sh_InitRMutex(struct sh_RMutex * tok)
-{
-  pthread_mutex_init(&tok->lock, NULL);
-  tok->held = 0;
-  tok->waiters = 0;
-  pthread_cond_init(&tok->cv, NULL);
-
-  return;
-}
-
-void sh_RMutexLock(struct sh_RMutex * tok)
-{
-  if (tok == NULL)
-    return;
-
-  pthread_mutex_lock(&tok->lock);
-  if (tok->held) {
-    if (pthread_equal(tok->tid, pthread_self())) {
-      tok->held++;
-      pthread_mutex_unlock(&tok->lock);
-      return;
-    } else {
-      tok->waiters++;
-      while (tok->held)
-	pthread_cond_wait(&tok->cv, &tok->lock);
-      tok->waiters--;
-    }
-  }
-  tok->tid = pthread_self();
-  tok->held = 1;
-  pthread_mutex_unlock(&tok->lock);
-}
-
-void sh_RMutexUnlock(void * arg)
-{
-  struct sh_RMutex * tok = (struct sh_RMutex *) arg;
-
-  if (tok == NULL)
-    return;
-    
-  pthread_mutex_lock(&tok->lock);
-  tok->held--;
-  if (tok->held == 0) {
-    if (tok->waiters)
-      pthread_cond_signal(&tok->cv);
-    tok->tid = 0;
-  }
-  pthread_mutex_unlock(&tok->lock);
-}
-#endif
-
-#else
-
-#include <signal.h>
-
-int sh_pthread_setsigmask(int how, const void *set, void *oldset)
-{
-  return sigprocmask(how, (const sigset_t *)set, (sigset_t *)oldset);
-}
-
-
-#endif
Index: trunk/src/sh_readconf.c
===================================================================
--- trunk/src/sh_readconf.c	(revision 591)
+++ trunk/src/sh_readconf.c	(revision 1)
@@ -28,29 +28,29 @@
 
 #include "samhain.h"
+#include "sh_error.h"
+#include "sh_database.h"
+#include "sh_unix.h"
+#include "sh_utils.h"
+#include "sh_files.h"
+#include "sh_mail.h"
 #include "sh_calls.h"
-#include "sh_error.h"
+#include "sh_tiger.h"
+#include "sh_forward.h"
+#include "sh_modules.h"
+#include "sh_gpg.h"
+#include "sh_hash.h"
+#include "sh_ignore.h"
+#include "sh_prelink.h"
 #include "sh_extern.h"
-#include "sh_unix.h"
-#include "sh_files.h"
-#include "sh_xfer.h"
-#include "sh_sig.h"
-#include "sh_hash.h"
-#include "sh_dbIO.h"
-#include "sh_ignore.h"
+
+#ifdef WITH_DATABASE
 #include "sh_database.h"
-#include "sh_mail.h"
-#include "sh_modules.h"
-#include "sh_nmail.h"
-#include "sh_prelink.h"
-#ifdef HAVE_LIBPRELUDE
+#endif
+
+#ifdef HAVE_LIBPRELUDE_9
 #include "sh_prelude.h"
 #endif
-#include "sh_tiger.h"
-#include "sh_tools.h"
-#include "sh_utils.h"
-#include "sh_restrict.h"
-#include "sh_socket.h"
-
-extern int set_reverse_lookup (const char * c);
+
+extern int set_reverse_lookup (char * c);
 
 #undef  FIL__
@@ -69,7 +69,4 @@
   SH_SECTION_USER0,
   SH_SECTION_USER1,
-  SH_SECTION_USER2,
-  SH_SECTION_USER3,
-  SH_SECTION_USER4,
   SH_SECTION_PRELINK,
 #if defined (SH_WITH_MAIL) 
@@ -96,5 +93,5 @@
 
 typedef struct str_ListSections {
-  const char * name;
+  char * name;
   int    type;
 } sh_str_ListSections;
@@ -111,7 +108,4 @@
   { N_("[User0]"),            SH_SECTION_USER0},
   { N_("[User1]"),            SH_SECTION_USER1},
-  { N_("[User2]"),            SH_SECTION_USER2},
-  { N_("[User3]"),            SH_SECTION_USER3},
-  { N_("[User4]"),            SH_SECTION_USER4},
   { N_("[Prelink]"),          SH_SECTION_PRELINK},
 #ifdef WITH_EXTERNAL
@@ -135,198 +129,4 @@
 };
 
-static char * sh_readconf_expand_value (const char * str)
-{
-#ifdef SH_EVAL_SHELL
-  char * tmp = sh_util_strdup(str);
-  char * out;
-  char * tmp_orig = tmp;
-
-  while (tmp && isspace((int)*tmp)) ++tmp;
-  
-  if (tmp && tmp[0] == '$' && tmp[1] == '(')
-    {
-      size_t len = strlen(tmp);
-      while (isspace((int) tmp[len-1])) { tmp[len-1] = '\0'; --len; }
-      if (tmp[len-1] == ')')
-	{
-	  tmp[len-1] = '\0';
-	  out = sh_ext_popen_str(&tmp[2]);
-	  SH_FREE(tmp_orig);
-	  return out;
-	}
-    }
-  SH_FREE(tmp_orig);
-#endif
-  return sh_util_strdup(str);
-}
-
-enum {
-  SH_RC_ANY        = 0, 
-  SH_RC_HOST       = 1, 
-  SH_RC_SYSTEM     = 2,
-  SH_RC_FILE       = 3,
-  SH_RC_IFACE      = 4,
-#ifdef SH_EVAL_SHELL
-  SH_RC_CMD        = 5
-#endif
-};
-
-
-static int sh_readconf_cond_match(char * str, int line)
-{
-  int    match  = 0;
-  int    negate = 1;
-  int    cond_type = SH_RC_ANY;
-  char   myident[3*SH_MINIBUF+3];
-  struct stat buf;
-
-  char * p = str;
-
-  if (*p == '!') { negate = 0; ++p; }
-  if (*p == '$') { 
-    cond_type = SH_RC_SYSTEM; ++p; /* [!]$system */ 
-  }
-  else { /* *p == '@' */
-
-    ++p; while (isspace((int)*p)) ++p;
-
-    if (0 != strncasecmp(p, _("if "),   3)) {
-      cond_type = SH_RC_HOST; /* [!]$host */
-    }
-
-    else {
-
-      p += 3; while (isspace((int)*p)) ++p; /* skip the 'if\s+' */
-
-      if (0 == strncasecmp(p, _("not "), 4))
-	{
-	  p += 4; while (isspace((int)*p)) ++p;
-	  negate = 0;
-	  match  = 1;
-	}
-      else if (0 == strncmp(p, _("!"), 1))
-	{
-	  ++p; while (isspace((int)*p)) ++p;
-	  negate = 0;
-	  match  = 1;
-	}
-  
-      if (0 == strncasecmp(p, _("file_exists "), 12))
-	{
-	  p += 12; cond_type = SH_RC_FILE;
-	}
-      else if (0 == strncasecmp(p, _("interface_exists "), 17))
-	{
-	  p += 17; cond_type = SH_RC_IFACE;
-	}
-      else if (0 == strncasecmp(p, _("hostname_matches "), 17))
-	{
-	  p += 17; cond_type = SH_RC_HOST;
-	}
-      else if (0 == strncasecmp(p, _("system_matches "), 15))
-	{
-	  p += 15; cond_type = SH_RC_SYSTEM;
-	}
-#ifdef SH_EVAL_SHELL
-      else if (0 == strncasecmp(p, _("command_succeeds "), 17))
-	{
-	  p += 17; cond_type = SH_RC_CMD;
-	}
-#endif
-      else
-	{
-	  char errbuf[SH_ERRBUF_SIZE];
-	  sl_snprintf(errbuf, sizeof(errbuf), 
-		      _("Unsupported test at line %d of configuration file"),
-		      line);
-	  sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-			  errbuf,
-			  _("sh_readconf_cond_match"));
-	  return 0;
-	}
-    }
-  }
-
-  while (isspace((int)*p)) ++p;
-
-  switch (cond_type)
-    {
-    case SH_RC_HOST:
-      if  (sl_strncmp (p,  sh.host.name, strlen(sh.host.name)) == 0
-#ifdef HAVE_REGEX_H
-	   || sh_util_regcmp (p, sh.host.name) == 0
-#endif
-	   )
-	match = negate;
-      break;
-    case SH_RC_SYSTEM:
-      /*
-       * The system type, release, and machine.
-       */
-      sl_snprintf(myident, sizeof(myident), _("%s:%s:%s"),  
-		  sh.host.system, /* flawfinder: ignore */ 
-		  sh.host.release, sh.host.machine);
-      
-      if  (sl_strncmp (p,  myident, strlen(myident)) == 0
-#ifdef HAVE_REGEX_H
-	   || sh_util_regcmp (p, myident) == 0
-#endif
-	   )
-	match = negate;
-      break;
-    case SH_RC_FILE:
-      if (0 == retry_lstat(FIL__, __LINE__, p, &buf))
-	match = negate;
-      break;
-    case SH_RC_IFACE:
-      if (sh_tools_iface_is_present(p))
-	match = negate;
-      break;
-#ifdef SH_EVAL_SHELL
-    case SH_RC_CMD:
-      if (0 == sh_unix_run_command(p))
-	match = negate;
-      break;
-#endif
-    default:
-      /* do nothing */;
-    }
-  return match;
-}
-
-static int sh_readconf_is_end (char * str)
-{
-  int retval = 0;
-
-  if (str[0] == '@' || str[0] == '$')
-    {
-      char * p = str;
-      ++p; while (isspace((int)*p)) ++p;
-      if ( 
-	  (0 == strncasecmp (p, _("end"), 3) && (p[3] == '\0' || isspace((int)p[3]))) ||
-	  (0 == strncasecmp (p, _("fi"),  2) && (p[2] == '\0' || isspace((int)p[2])))
-	   )
-	{
-	  return 1;
-	}
-    }
-  return retval;
-}
-   
-static int sh_readconf_is_else (char * str)
-{
-  int retval = 0;
-
-  if (str[0] == '@')
-    {
-      char * p = str;
-      ++p; while (isspace((int)*p)) ++p;
-      if ( 0 == strncasecmp (p, _("else"), 4) && (p[4] == '\0' || isspace((int)p[4])) )
-	{
-	  return 1;
-	}
-    }
-  return retval;
-}
    
 static int sh_readconfig_line (char * line);
@@ -351,23 +151,31 @@
 #if defined(SH_STEALTH) && !defined(SH_STEALTH_MICRO)
   SL_TICKET    fdTmp = -1;
-#endif
-#if defined(WITH_SIG) 
-  SL_TICKET    fdSIG = -1;
+  SL_TICKET open_tmp (void);
 #endif
   char * tmp;
-
-#define SH_LINE_IN 16384
-  char * line_in;
+  char * lptr;
+
+  char   line_in[512+2];
   char * line;
+  int    line_int;
+
+  char   myident[3*SH_MINIBUF+3];
 
   /* This is for nested conditionals.
    */
-  int    cond_depth  = 0;
-  int    cond_excl   = 0;
-  
+  int    some_other_host[16]   = { 0 };
+  int    some_other_system[16] = { 0 };
+  int    seen_host   = 0;
+  int    seen_system = 0;
+  int    host_int    = 0;
+  int    sys_int     = 0;
+
+  int    invert = 0;
+  int    length = sl_strlen(sh.host.name);
+
   int    local_file = 1;
   char   local_flag = 'R';
 
-#if defined(WITH_SIG)
+#if defined(WITH_GPG) || defined(WITH_PGP)
   int    signed_content = S_FALSE;
   int    true_content   = S_FALSE;
@@ -377,5 +185,4 @@
 #endif
   uid_t  euid;
-  char hashbuf[KEYBUF_SIZE];
 
   SL_ENTER(_("sh_readconf_read"));
@@ -388,18 +195,12 @@
       sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_D_START);
 
-      fd = sh_xfer_request_file(_("CONF"));
+      fd = sh_forward_req_file(_("CONF"));
 
       if (!SL_ISERROR(fd))
-	{
-	  local_file = 0;
-	}
+	local_file = 0;
       else if (sh.flag.checkSum != SH_CHECK_INIT)
-	{
-	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FBAD);
-	  aud_exit (FIL__, __LINE__, EXIT_FAILURE);
-	}
+	aud_exit (FIL__, __LINE__, EXIT_FAILURE);
       else
 	{
-	  sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_TCP_FBAD);
 	  sh_error_handle ((-1), FIL__, __LINE__, fd, MSG_D_FAIL);
 	  local_file = 1;
@@ -427,6 +228,5 @@
 	  aud_exit (FIL__, __LINE__, EXIT_FAILURE);
 	}
-      if (SL_ISERROR(fd = sl_open_read(FIL__, __LINE__, 
-				       file_path('C',local_flag),SL_YESPRIV)))
+      if (SL_ISERROR(fd = sl_open_read(file_path('C',local_flag),SL_YESPRIV)))
 	{
 	  sl_get_euid(&euid);
@@ -445,11 +245,9 @@
   /* Compute the checksum of the open file.
    */
+  tiger_fd = fd;
   sl_strlcpy(sh.conf.hash, 
-	     sh_tiger_hash(file_path('C',local_flag), fd, TIGER_NOLIM, 
-			   hashbuf, sizeof(hashbuf)),
+	     sh_tiger_hash(file_path('C',local_flag),TIGER_FD, 0),
 	     KEY_LEN+1);
   sl_rewind (fd);
-
-  line_in = SH_ALLOC(SH_LINE_IN);
 
 #if defined(SH_STEALTH) && !defined(SH_STEALTH_MICRO)
@@ -457,8 +255,5 @@
      */
   fdTmp = open_tmp(); 
-
-  sh_unix_getline_stealth (0, NULL, 0); /* initialize */
-
-  while ( sh_unix_getline_stealth (fd, line_in, SH_LINE_IN-2) > 0) {
+  while ( sh_unix_getline_stealth (fd, line_in, 512) > 0) {
     hidden_count++;
     if (line_in[0] == '\n')
@@ -470,6 +265,6 @@
 	sl_write_line(fdTmp, line_in, sl_strlen(line_in));
       }
-#if defined(WITH_SIG)
-    if (S_TRUE == sh_sig_data_end(line_in))
+#if defined(WITH_GPG) || defined(WITH_PGP)
+    if (0 == sl_strncmp(line_in, _("-----END PGP SIGNATURE-----"), 25))
       break;
 #else
@@ -477,5 +272,5 @@
       break;
 #endif
-    if (hidden_count > 1048576)  /* arbitrary safeguard, 1024*1024 */
+    if (hidden_count > 4096)  /* arbitrary safeguard */
       break;
   }
@@ -485,23 +280,8 @@
 #endif
 
-#if defined(WITH_SIG)
-
-  /* extract the data and copy to temporary file
+  /* The system type, release, and machine.
    */
-  fdSIG = sh_sig_extract_signed(fd);
-
-  sl_close(fd);
-  fd = fdSIG;
-
-  /* Validate signature of open file.
-   */
-  if (0 != sh_sig_check_signature (fd, SIG_CONF))
-    {
-      SH_FREE(line_in);
-      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1, sh.prg_name);
-      aud_exit (FIL__, __LINE__, EXIT_FAILURE);
-    }
-  sl_rewind (fd);
-#endif
+  sprintf(myident, _("%s:%s:%s"),                  /* known to fit  */
+	  sh.host.system, sh.host.release, sh.host.machine);
 
 
@@ -510,5 +290,5 @@
   conf_line = 0;
 
-  while ( sh_unix_getline (fd, line_in, SH_LINE_IN-2) > 0) {
+  while ( sh_unix_getline (fd, line_in, 512) > 0) {
 
     ++conf_line;
@@ -520,17 +300,24 @@
     /* Sun May 27 18:40:05 CEST 2001
      */
-#if defined(WITH_SIG)
+#if defined(WITH_GPG) || defined(WITH_PGP)
     if (signed_content == S_FALSE)
       { 
-	if (S_TRUE == sh_sig_msg_start(line))
+	if (0 == sl_strcmp(line, _("-----BEGIN PGP SIGNED MESSAGE-----")))
 	  signed_content = S_TRUE;
 	else 
 	  continue;
       }
-    else /* if (signed_content == S_TRUE) */
+    else if (true_content == S_FALSE)
+      {
+	if (line[0] == '\n')
+	  true_content = S_TRUE;
+	else
+	  continue;
+      }
+    else if (signed_content == S_TRUE)
       { 
-	if (S_TRUE == sh_sig_msg_end(line))
+	if (0 == sl_strcmp(line, _("-----BEGIN PGP SIGNATURE-----")))
 	  break;
-	else if (S_TRUE == sh_sig_msg_start(line))
+	else if (0 == sl_strcmp(line, _("-----BEGIN PGP SIGNED MESSAGE-----")))
 	  {
 	    sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
@@ -541,15 +328,6 @@
 	    sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
 			     sh.prg_name);
-	    SH_FREE(line_in);
 	    aud_exit (FIL__, __LINE__,EXIT_FAILURE);
 	  }
-      }
-
-    if (true_content == S_FALSE) /* continue if in header */
-      {
-	if (S_TRUE == sh_sig_msg_startdata(line))
-	  true_content = S_TRUE;
-	else
-	  continue;
       }
 #endif
@@ -557,6 +335,11 @@
     /* Skip leading white space.
      */
-    while (isspace((int)*line)) ++line;
-
+    while (*line)
+      {
+	line_int = *line;
+	if (!isspace(line_int))
+	  break;
+	++line;
+      }
 
     /* Skip header etc. 
@@ -572,71 +355,17 @@
 
 
-    /* ---  an @host/@if/$system directive -------------- */
-
-    if (line[0] == '@' || (line[0] == '!' && line[1] == '@') || 
-	line[0] == '$' || (line[0] == '!' && line[1] == '$'))
-      {
-	if (sh_readconf_is_end(line))
-	  {
-	    if (0 == cond_depth) {
-	      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EINVALD,
-			       _("config file"), 
-			       (long) conf_line);
-	    }
-	    else {
-	      if (cond_excl == cond_depth)
-		cond_excl = 0;
-	      --cond_depth;
-	    }
-	  }
-	else if (sh_readconf_is_else(line))
-	  {
-	    if (0 == cond_depth) {
-	      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EINVALD,
-			       _("config file"), 
-			       (long) conf_line);
-	    }
-	    else if (cond_excl == cond_depth) {
-	      cond_excl = 0;
-	    }
-	    else if (cond_excl == 0) {
-	      cond_excl = cond_depth;
-	    }
-	  }
-	else
-	  {
-	    if (sh_readconf_cond_match(line, conf_line)) {
-	      ++cond_depth;
-	    }
-	    else {
-	      ++cond_depth;
-	      if (cond_excl == 0)
-		cond_excl = cond_depth;
-	    }
-	  }
-	continue;
-      }
-
-    /* fprintf(stderr, "%d %s\n", cond_excl, line); */
-
-    /****************************************************
-     *
-     * Only carry on if this section is intended for us
-     *
-     ****************************************************/
-    
-    if (cond_excl != 0) {
-      continue;
-    }
-
     /* -------  starts a section  ------------  */
     
-    else if (line[0] == '[')
+    if (line[0] == '['                       && 
+	some_other_host[seen_host] == 0      &&
+	some_other_system[seen_system] == 0) 
       { 
 	read_mode = SH_SECTION_NONE;
 
-	if (0 == sl_strncasecmp (line,  _("[EOF]"), 5)) {
-	  goto nopel;
-	}
+	if (sl_strncmp (line,  _("[EOF]"), 
+			  5) == 0)
+	  {
+	    goto nopel;
+	  }
 
 	i = 0;
@@ -644,6 +373,6 @@
 	while (tab_ListSections[i].name != 0)
 	  {
-	    if (sl_strncasecmp (line, _(tab_ListSections[i].name), 
-				sl_strlen(tab_ListSections[i].name)) == 0)
+	    if (sl_strncmp (line, _(tab_ListSections[i].name), 
+			    sl_strlen(tab_ListSections[i].name)) == 0)
 	      { 
 		read_mode = tab_ListSections[i].type;
@@ -658,6 +387,6 @@
 	    for (modnum = 0; modList[modnum].name != NULL; ++modnum) 
 	      {
-		if (0 == sl_strncasecmp (line, _(modList[modnum].conf_section),
-					 sl_strlen(modList[modnum].conf_section)) )
+		if (0 == sl_strncmp (line,  _(modList[modnum].conf_section),
+				     sl_strlen(modList[modnum].conf_section)) )
 		  read_mode = SH_SECTION_OTHER;
 	      }
@@ -669,4 +398,70 @@
 			     (long) conf_line);
 	  }
+      } 
+
+    /* ---  an @host directive -------------- */
+
+    else if (line[0] == '@' || (line[0] == '!' && line[1] == '@')) 
+      {
+	if (line[0] == '!')
+	  {
+	    lptr   = &line[2];
+	    invert = 1;
+	  }
+	else
+	  {
+	    lptr   = &line[1];
+	    invert = 0;
+	  }
+
+	if (sl_strncmp (lptr, _("end"), 3) == 0)
+	  {
+	    if (0 == seen_host)
+	      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EINVALD,
+			       _("config file"), 
+			       (long) conf_line);
+
+	    else if (host_int == 0)
+	      {
+		/* end  of an @host directive 
+		 */
+		some_other_host[seen_host] = 0;
+		--seen_host;
+		seen_host = (seen_host < 0) ? 0 : seen_host;
+	      }
+
+	    else 
+	      {
+		--host_int;
+		host_int = (host_int < 0) ? 0 : host_int;
+	      }
+	  }
+	else if (some_other_host[seen_host] == 0      &&
+		 some_other_system[seen_system] == 0  && 
+		 seen_host < 15)
+	  {
+	    if  (sl_strncmp (lptr,  sh.host.name, length) == 0
+#ifdef HAVE_REGEX_H
+		 || sh_util_regcmp (lptr, sh.host.name) == 0
+#endif
+		 )
+	      {
+		/* if match and '@',  set some_other_host = 0;
+		 * if match and '!@', set some_other_host = 1;
+		 */
+		++seen_host;
+		some_other_host[seen_host] = invert; 
+	      }
+	    else
+	      {
+		/* if no match and '@',  set some_other_host = 1;
+		 * if no match and '!@', set some_other_host = 0;
+		 */
+		++seen_host;
+		some_other_host[seen_host] = (invert == 0) ? 1 : 0;
+	      }
+	  }
+	else
+	  ++host_int;
       } 
 
@@ -676,7 +471,7 @@
       {
 #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
-	if (line[0] == '!' && 0 == sl_strcasecmp(&(line[2]), _("SCHEDULE_TWO")))
+	if (line[0] == '!' && 0 == sl_strcmp(&(line[2]), _("SCHEDULE_TWO")))
 	  set_dirList(1);
-	else if (0 == sl_strcasecmp(&(line[1]), _("SCHEDULE_TWO")))
+	else if (0 == sl_strcmp(&(line[1]), _("SCHEDULE_TWO")))
 	  set_dirList(2);
 #else
@@ -684,9 +479,70 @@
 #endif
       }
+	  
+    /* ---  an $system directive -------------- */
+
+    else if (line[0] == '$' || (line[0] == '!' && line[1] == '$')) 
+      {
+	if (line[0] == '!')
+	  {
+	    lptr   = &line[2];
+	    invert = 1;
+	  }
+	else
+	  {
+	    lptr   = &line[1];
+	    invert = 0;
+	  }
+
+	if (sl_strncmp (lptr, _("end"), 3) == 0)
+	  {
+	    if (0 == seen_system)
+	      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EINVALD,
+			       _("config file"),
+			       (long) conf_line);
+
+	    else if (sys_int == 0)
+	      {
+		/* end  of an $system directive 
+		 */
+		some_other_system[seen_system] = 0;
+		--seen_system;
+		seen_system = (seen_system < 0) ? 0 : seen_system;
+	      }
+	    else 
+	      {
+		--sys_int;
+		sys_int = (sys_int < 0) ? 0 : sys_int;
+	      }
+	  }
+	else if (some_other_host[seen_host] == 0      &&
+		 some_other_system[seen_system] == 0  && 
+		 seen_system < 15)
+	  {
+	    if  (sl_strncmp (lptr,  myident, sl_strlen(myident)) == 0
+#ifdef HAVE_REGEX_H
+		 || sh_util_regcmp (lptr, myident) == 0
+#endif
+		 )
+	      {
+		++seen_system;
+		some_other_system[seen_system] = invert;
+	      }
+	    else
+	      {
+		++seen_system;
+		some_other_system[seen_system] = (invert == 0) ? 1 : 0;
+	      }
+	  }
+	else
+	  ++sys_int;
+      }
 
     /* ------  no new section -------------- */
 
 
-    else if (read_mode != SH_SECTION_NONE)
+    else if (some_other_host[seen_host] == 0          && 
+	     some_other_system[seen_system] == 0      && 
+	     read_mode != SH_SECTION_NONE) 
       { 
 	if (0 != sh_readconfig_line (line))
@@ -696,13 +552,23 @@
 	  }
       }
-  } /* while getline() */
+
+  }
 
  nopel:
 	   
-  if (0 != cond_depth)
+  if (0 != seen_host || 0 != seen_system)
     sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EINVALDD,
 		     _("config file"), 
 		     (long) conf_line);
 
+#if defined(WITH_GPG) || defined(WITH_PGP)
+  /* Validate signature of open file.
+   */
+  sl_rewind (fd);
+  sh_error_only_stderr (S_FALSE);
+  if (0 != sh_gpg_check_sign (fd, 0, 1))
+    aud_exit (FIL__, __LINE__, EXIT_FAILURE);
+#endif
+
   sl_close (fd);
 
@@ -711,9 +577,8 @@
   read_mode = SH_SECTION_NONE; /* reset b/o sighup reload */
 
-  SH_FREE(line_in);
   SL_RETURN( 0, _("sh_readconf_read"));
 }
 
-int sh_readconf_set_path (char * which, const char * what)
+int sh_readconf_set_path (char * which, char * what)
 {
   int len;
@@ -756,15 +621,15 @@
 }
 
-int sh_readconf_set_database_path (const char * what)
+int sh_readconf_set_database_path (char * what)
 {
   return (sh_readconf_set_path(sh.data.path, what));
 }
 
-int sh_readconf_set_logfile_path (const char * what)
+int sh_readconf_set_logfile_path (char * what)
 {
   return (sh_readconf_set_path(sh.srvlog.name, what));
 }
 
-int sh_readconf_set_lockfile_path (const char * what)
+int sh_readconf_set_lockfile_path (char * what)
 {
   return( sh_readconf_set_path(sh.srvlog.alt, what));
@@ -780,5 +645,5 @@
  
     
-int sh_readconf_setTime (const char * str, ShTimerItem what)
+int sh_readconf_setTime (char * str, ShTimerItem what)
 {
   unsigned long i = atoi (str);
@@ -809,15 +674,15 @@
 }
 
-int sh_readconf_setMailtime (const char * c)
+int sh_readconf_setMailtime (char * c)
 {
   return sh_readconf_setTime (c, SET_MAILTIME);
 }
 
-int sh_readconf_setFiletime (const char * c)
+int sh_readconf_setFiletime (char * c)
 {
   return sh_readconf_setTime (c, SET_FILETIME);
 }
 
-int sh_readconf_set_nice (const char * c)
+int sh_readconf_set_nice (char * c)
 {
   long val;
@@ -838,19 +703,6 @@
 }
 
-
-#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) 
-static int sh_readconf_set_delay (const char * c)
-{
-  unsigned long i = atol (c);
-
-  if (i  > INT_MAX)
-    return -i;
-  sh.delayload = (int) i;
-  return 0;
-}
-#endif
-
 #ifdef FANCY_LIBCAP
-int sh_readconf_setCaps(const char * c)
+int sh_readconf_setCaps(char * c)
 {
   int i;
@@ -863,19 +715,18 @@
 
 typedef struct _cfg_options {
-  const char * optname;
+  char * optname;
   ShSectionType   section;
   ShSectionType   alt_section;
-  int (*func)(const char * opt);
+  int (*func)(char * opt);
 } cfg_options;
 
 #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
-extern int sh_set_schedule_one(const char * str);
-extern int sh_set_schedule_two(const char * str);
-extern int sh_set_silent_full (const char * str);
+extern int sh_set_schedule_one(char * str);
+extern int sh_set_schedule_two(char * str);
 #endif
 #if defined (SH_WITH_SERVER)
-extern int sh_socket_use (const char * c);
-extern int sh_socket_uid (const char * c);
-extern int sh_socket_password (const char * c);
+extern int sh_socket_use (char * c);
+extern int sh_socket_uid (char * c);
+extern int sh_socket_password (char * c);
 #endif
 
@@ -884,6 +735,4 @@
   { N_("opencommand"),     SH_SECTION_EXTERNAL, SH_SECTION_NONE,  
     sh_ext_setcommand },
-  { N_("closecommand"),    SH_SECTION_EXTERNAL, SH_SECTION_NONE,  
-    sh_ext_close_command },
   { N_("setcommandline"),  SH_SECTION_EXTERNAL, SH_SECTION_NONE,  
     sh_ext_add_argv },
@@ -934,5 +783,4 @@
     set_enter_wrapper },
 #endif
-
 
 #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
@@ -970,16 +818,4 @@
   { N_("file"),           SH_SECTION_USER1,      SH_SECTION_NONE, 
     sh_files_pushfile_user1 },
-  { N_("dir"),            SH_SECTION_USER2,      SH_SECTION_NONE, 
-    sh_files_pushdir_user2 },
-  { N_("file"),           SH_SECTION_USER2,      SH_SECTION_NONE, 
-    sh_files_pushfile_user2 },
-  { N_("dir"),            SH_SECTION_USER3,      SH_SECTION_NONE, 
-    sh_files_pushdir_user3 },
-  { N_("file"),           SH_SECTION_USER3,      SH_SECTION_NONE, 
-    sh_files_pushfile_user3 },
-  { N_("dir"),            SH_SECTION_USER4,      SH_SECTION_NONE, 
-    sh_files_pushdir_user4 },
-  { N_("file"),           SH_SECTION_USER4,      SH_SECTION_NONE, 
-    sh_files_pushfile_user4 },
   { N_("dir"),            SH_SECTION_PRELINK,    SH_SECTION_NONE, 
     sh_files_pushdir_prelink },
@@ -987,16 +823,8 @@
     sh_files_pushfile_prelink },
 
-  { N_("ignoreadded"),          SH_SECTION_MISC,   SH_SECTION_NONE, 
+  { N_("ignoreadded"), SH_SECTION_MISC,   SH_SECTION_NONE, 
     sh_ignore_add_new },
-  { N_("ignoremissing"),        SH_SECTION_MISC,   SH_SECTION_NONE, 
+  { N_("ignoremissing"), SH_SECTION_MISC,   SH_SECTION_NONE, 
     sh_ignore_add_del },
-  { N_("ignoremodified"),       SH_SECTION_MISC,   SH_SECTION_NONE, 
-    sh_ignore_add_mod },
-
-  { N_("skipchecksum"),         SH_SECTION_MISC,   SH_SECTION_NONE, 
-    sh_restrict_define },
-  { N_("filetype"),             SH_SECTION_MISC,   SH_SECTION_NONE, 
-    sh_restrict_add_ftype },
-
 
   { N_("filecheckscheduleone"), SH_SECTION_MISC,   SH_SECTION_NONE, 
@@ -1005,65 +833,34 @@
     sh_set_schedule_two },
 
-  { N_("usehardlinkcheck"),     SH_SECTION_MISC,   SH_SECTION_NONE, 
+  { N_("usehardlinkcheck"),   SH_SECTION_MISC,   SH_SECTION_NONE, 
     sh_files_check_hardlinks },
-  { N_("usersrccheck"),         SH_SECTION_MISC,   SH_SECTION_NONE, 
-    sh_files_use_rsrc },
-  { N_("hardlinkoffset"),       SH_SECTION_MISC,   SH_SECTION_NONE,
+  { N_("hardlinkoffset"),     SH_SECTION_MISC,   SH_SECTION_NONE,
     sh_files_hle_reg },
-#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
-  { N_("useattributescheck"),      SH_SECTION_MISC,   SH_SECTION_NONE, 
-    sh_unix_setcheckattributes },
-#endif
-#if defined(USE_XATTR)
-  { N_("useselinuxcheck"),      SH_SECTION_MISC,   SH_SECTION_NONE, 
-    sh_unix_setcheckselinux },
-#endif
-#if defined(USE_ACL)
-  { N_("useaclcheck"),          SH_SECTION_MISC,   SH_SECTION_NONE, 
-    sh_unix_setcheckacl },
-#endif
-#if !defined(SH_COMPILE_STATIC) && defined(__linux__) && defined(HAVE_AUPARSE_H) && defined(HAVE_AUPARSE_LIB)
-  { N_("setauditdflags"),       SH_SECTION_MISC,   SH_SECTION_NONE, 
-    sh_audit_set_flags },
-#endif
-  { N_("loosedircheck"),        SH_SECTION_MISC,   SH_SECTION_NONE,
-    sh_hash_loosedircheck },
-  { N_("addokchars"),           SH_SECTION_MISC,   SH_SECTION_NONE,
+  { N_("addokchars"),         SH_SECTION_MISC,   SH_SECTION_NONE,
     sh_util_obscure_ok },
-  { N_("filenamesareutf8"),     SH_SECTION_MISC,   SH_SECTION_NONE,
-    sh_util_obscure_utf8 },
-  { N_("setrecursionlevel"),    SH_SECTION_MISC,   SH_SECTION_NONE, 
+  { N_("setrecursionlevel"),  SH_SECTION_MISC,   SH_SECTION_NONE, 
     sh_files_setrecursion },
-  { N_("checksumtest"),         SH_SECTION_MISC,   SH_SECTION_NONE, 
+  { N_("checksumtest"),       SH_SECTION_MISC,   SH_SECTION_NONE, 
     sh_util_setchecksum },
-  { N_("reportonlyonce"),       SH_SECTION_MISC,   SH_SECTION_NONE, 
+  { N_("reportonlyonce"),     SH_SECTION_MISC,   SH_SECTION_NONE, 
     sh_files_reportonce },
-  { N_("reportfulldetail"),     SH_SECTION_MISC,   SH_SECTION_NONE, 
+  { N_("reportfulldetail"),   SH_SECTION_MISC,   SH_SECTION_NONE, 
     sh_files_fulldetail },
-  { N_("uselocaltime"),         SH_SECTION_MISC,   SH_SECTION_NONE, 
+  { N_("uselocaltime"),       SH_SECTION_MISC,   SH_SECTION_NONE, 
     sh_unix_uselocaltime },
 
-  { N_("setnicelevel"),         SH_SECTION_MISC,   SH_SECTION_NONE, 
+  { N_("setnicelevel"),   SH_SECTION_MISC,   SH_SECTION_NONE, 
     sh_readconf_set_nice },
 
-  { N_("startuploaddelay"),     SH_SECTION_MISC,   SH_SECTION_NONE, 
-    sh_readconf_set_delay },
-
 #if defined(FANCY_LIBCAP)
-  { N_("usecaps"),              SH_SECTION_MISC,   SH_SECTION_NONE, 
+  { N_("usecaps"),        SH_SECTION_MISC,   SH_SECTION_NONE, 
     sh_readconf_setCaps },
 #endif
 
-  { N_("reportcheckflags"),     SH_SECTION_MISC,   SH_SECTION_NONE, 
-    set_report_checkflags },
-
-  { N_("setdropcache"),         SH_SECTION_MISC,   SH_SECTION_NONE, 
-    sl_set_drop_cache },
-
-  { N_("setiolimit"),           SH_SECTION_MISC,   SH_SECTION_NONE, 
+  { N_("setiolimit"),   SH_SECTION_MISC,   SH_SECTION_NONE, 
     sh_unix_set_io_limit },
 
   { N_("versionstring"),        SH_SECTION_MISC,   SH_SECTION_NONE,
-    sh_dbIO_version_string },
+    sh_hash_version_string },
 
   { N_("digestalgo"),           SH_SECTION_MISC,   SH_SECTION_NONE, 
@@ -1093,13 +890,4 @@
   { N_("redefuser1"),           SH_SECTION_MISC,   SH_SECTION_NONE, 
     sh_files_redef_user1 },
-
-  { N_("redefuser2"),           SH_SECTION_MISC,   SH_SECTION_NONE, 
-    sh_files_redef_user2 },
-
-  { N_("redefuser3"),           SH_SECTION_MISC,   SH_SECTION_NONE, 
-    sh_files_redef_user3 },
-
-  { N_("redefuser4"),           SH_SECTION_MISC,   SH_SECTION_NONE, 
-    sh_files_redef_user4 },
 
   { N_("redefprelink"),         SH_SECTION_MISC,   SH_SECTION_NONE, 
@@ -1111,8 +899,4 @@
   { N_("setprelinkchecksum"),   SH_SECTION_MISC,   SH_SECTION_NONE, 
     sh_prelink_set_hash },
-
-  { N_("setfullsilent"),        SH_SECTION_MISC,   SH_SECTION_NONE, 
-    sh_set_silent_full },
-
   /* client or standalone
    */
@@ -1131,5 +915,5 @@
     sh_socket_password },
   { N_("setstripdomain"),      SH_SECTION_SRV,  SH_SECTION_MISC, 
-    sh_xfer_set_strip },
+    sh_forward_set_strip },
   { N_("useseparatelogs"),     SH_SECTION_SRV,  SH_SECTION_MISC, 
     set_flag_sep_log },
@@ -1137,43 +921,28 @@
     sh_unix_set_chroot },
   { N_("setclienttimelimit"),  SH_SECTION_SRV,  SH_SECTION_MISC, 
-    sh_xfer_set_time_limit },
-  { N_("setconnectiontimeout"),SH_SECTION_SRV,  SH_SECTION_MISC, 
-    sh_xfer_set_timeout },
+    sh_forward_set_time_limit },
   { N_("useclientseverity"),   SH_SECTION_SRV,  SH_SECTION_MISC, 
-  sh_xfer_use_clt_sev },
+  sh_forward_use_clt_sev },
   { N_("useclientclass"),      SH_SECTION_SRV,  SH_SECTION_MISC, 
-  sh_xfer_use_clt_class },
+  sh_forward_use_clt_class },
   { N_("severitylookup"),      SH_SECTION_SRV,  SH_SECTION_MISC, 
-  sh_xfer_lookup_level },
+  sh_forward_lookup_level },
   { N_("setclientfromaccept"), SH_SECTION_SRV,  SH_SECTION_MISC, 
     set_socket_peer },
   { N_("setserverport"),       SH_SECTION_SRV,  SH_SECTION_MISC, 
-    sh_xfer_set_port },
+    sh_forward_set_port },
   { N_("setserverinterface"),  SH_SECTION_SRV,  SH_SECTION_MISC, 
-    sh_xfer_set_interface },
-  { N_("alias"),               SH_SECTION_CLIENTS,           SH_SECTION_NONE, 
-    sh_xfer_register_alias },
+    sh_forward_set_interface },
   { N_("client"),              SH_SECTION_CLIENTS,           SH_SECTION_NONE, 
-    sh_xfer_register_client },
-#endif
-
-#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
+    sh_forward_register_client },
+#endif
+
+#ifdef SH_WITH_CLIENT
   { N_("exportseverity"),      SH_SECTION_LOG,  SH_SECTION_NONE, 
     sh_error_setexport },
   { N_("exportclass"),         SH_SECTION_LOG,  SH_SECTION_NONE, 
     sh_error_export_mask },
-#if defined(SH_WITH_SERVER)
-  { N_("setlogserver"),        SH_SECTION_SRV,  SH_SECTION_MISC, 
-    sh_xfer_set_logserver },
-#else
   { N_("setlogserver"),        SH_SECTION_CLT,  SH_SECTION_MISC, 
-    sh_xfer_set_logserver },
-  { N_("setthrottle"),         SH_SECTION_CLT,  SH_SECTION_MISC, 
-    sh_xfer_set_throttle_delay},
-  { N_("setdeltaretrycount"),   SH_SECTION_CLT,  SH_SECTION_MISC, 
-    set_delta_retry_count},
-  { N_("setdeltaretryinterval"),SH_SECTION_CLT,  SH_SECTION_MISC, 
-    set_delta_retry_interval},
-#endif
+    sh_forward_setlogserver },
 #endif
   { N_("setfilechecktime"),  SH_SECTION_MISC,   SH_SECTION_NONE, 
@@ -1191,8 +960,8 @@
   { N_("setmailnum"),        SH_SECTION_MAIL,  SH_SECTION_MISC, 
     sh_mail_setNum },
+  { N_("setmailaddress"),    SH_SECTION_MAIL,  SH_SECTION_MISC, 
+    sh_mail_setaddress },
   { N_("setmailrelay"),      SH_SECTION_MAIL,  SH_SECTION_MISC, 
     sh_mail_set_relay },
-  { N_("setmailport"),       SH_SECTION_MAIL,  SH_SECTION_MISC,
-    sh_mail_set_port },
   { N_("mailsingle"),        SH_SECTION_MAIL,  SH_SECTION_MISC, 
     sh_mail_setFlag },
@@ -1201,18 +970,10 @@
   { N_("setmailsender"),     SH_SECTION_MAIL,  SH_SECTION_MISC, 
     sh_mail_set_sender },
-  { N_("setmailalias"),       SH_SECTION_MAIL,  SH_SECTION_MISC, 
-    sh_nmail_add_alias },
-  { N_("setmailaddress"),    SH_SECTION_MAIL,  SH_SECTION_MISC, 
-    sh_nmail_add_recipient },
-  { N_("closeaddress"),      SH_SECTION_MAIL,  SH_SECTION_MISC, 
-    sh_nmail_close_recipient },
-  { N_("setaddrseverity"),   SH_SECTION_MAIL,  SH_SECTION_MISC,
-    sh_nmail_set_severity },
   { N_("setmailfilternot"),  SH_SECTION_MAIL,  SH_SECTION_MISC,
-    sh_nmail_add_not },
+    sh_mail_add_not },
   { N_("setmailfilterand"),  SH_SECTION_MAIL,  SH_SECTION_MISC,
-    sh_nmail_add_and },
+    sh_mail_add_and },
   { N_("setmailfilteror"),   SH_SECTION_MAIL,  SH_SECTION_MISC,
-    sh_nmail_add_or },
+    sh_mail_add_or },
 #endif
   { N_("setbindaddress"),    SH_SECTION_MISC,  SH_SECTION_NONE,
@@ -1246,4 +1007,5 @@
   { N_("preludeclass"),      SH_SECTION_LOG,   SH_SECTION_NONE, 
     sh_error_prelude_mask },
+#ifdef HAVE_LIBPRELUDE_9
   { N_("preludeprofile"),    SH_SECTION_MISC,  SH_SECTION_NONE,
     sh_prelude_set_profile },
@@ -1257,4 +1019,5 @@
     sh_prelude_map_high },
 #endif
+#endif
 
   { N_("logcalls"),          SH_SECTION_LOG,   SH_SECTION_NONE, 
@@ -1266,9 +1029,4 @@
   { N_("setconsole"),        SH_SECTION_MISC,  SH_SECTION_NONE, 
     sh_log_set_console },
-
-  { N_("setreportfile"),     SH_SECTION_MISC,  SH_SECTION_NONE, 
-    sh_efile_path },
-  { N_("setreportgroup"),    SH_SECTION_MISC,  SH_SECTION_NONE, 
-    sh_efile_group },
 
 #ifdef WITH_MESSAGE_QUEUE
@@ -1295,18 +1053,7 @@
     sh_log_set_facility },
 
-  { N_("syslogmapstampto"),    SH_SECTION_LOG,   SH_SECTION_MISC, 
-    sh_log_set_stamp_priority },
-
   { N_("mactype"),     SH_SECTION_MISC,  SH_SECTION_NONE, 
     sh_util_sigtype },
 
-  { N_("avoidblock"),     SH_SECTION_MISC,  SH_SECTION_NONE, 
-    sh_calls_set_sub },
-
-#ifdef SCREW_IT_UP
-  { N_("setsigtrapmaxduration"),        SH_SECTION_MISC,  SH_SECTION_MISC, 
-    sh_sigtrap_max_duration_set },
-#endif
-
   { NULL,    0,   0,  NULL}
 };
@@ -1316,8 +1063,6 @@
 
 static int sh_readconfig_line (char * line)
-{
-  char * key;
-  const char * value;
-  char * value_dup;
+{    
+  char * c;
   char * tmp;
   int    i;
@@ -1328,15 +1073,5 @@
 #endif
 
-  static const char  *dummy = N_("dummy");
-
-  static const char  *closing[] = {
-    N_("closecommand"),
-    N_("closeaddress"),
-    N_("logmonendgroup"),
-    N_("logmonendhost"),
-    NULL
-  };
-
-  static const char  *ident[] = {
+  static char  *ident[] = {
     N_("severityreadonly"),
     N_("severitylogfiles"),
@@ -1350,7 +1085,4 @@
     N_("severityuser0"),
     N_("severityuser1"),
-    N_("severityuser2"),
-    N_("severityuser3"),
-    N_("severityuser4"),
     N_("severityprelink"),
     NULL
@@ -1369,7 +1101,4 @@
     SH_ERR_T_USER0,       
     SH_ERR_T_USER1,       
-    SH_ERR_T_USER2,       
-    SH_ERR_T_USER3,       
-    SH_ERR_T_USER4,       
     SH_ERR_T_PRELINK,       
   };
@@ -1377,50 +1106,10 @@
   SL_ENTER(_("sh_readconf_line"));
 
-  /* convert to lowercase                              */
-
-  tmp = line;
-  while (*tmp != '=' && *tmp != '\0')
-    {
-      *tmp = tolower( (int) *tmp);
-      ++tmp;
-    }
-
-  key = line;
-
   /* interpret line                                    */
 
-  value = strchr(line, '=');
-
-  if (value == NULL || (*value) == '\0')
-    {
-      if (key != NULL)
-	{
-	  i = 0;
-	  while (closing[i] != NULL) 
-	    {
-	      if (sl_strncmp(key,_(closing[i]),sl_strlen(closing[i])-1) == 0)
-		{
-		  value = dummy;
-		  goto ok_novalue;
-		}
-	      ++i;
-	    }
-
-	  TPT(( 0, FIL__, __LINE__, _("msg=<ConfigFile: not key=value: %s>\n"),
-		line));
-	}
-      SL_RETURN(good_opt, _("sh_readconf_line"));
-    }
-  else
-    ++value;
-
-  /* skip leading whitespace
-   */
-  while ((*value) == ' ' || (*value) == '\t')
-    ++value;
-
-  if ((*value) == '\0')     /* no value                    */
-    {
-      if (key != NULL)
+  c = strchr(line, '=');
+  if (c == NULL || (*c) == '\0')
+    {
+      if (line != NULL)
 	{
 	  TPT(( 0, FIL__, __LINE__, _("msg=<ConfigFile: not key=value: %s>\n"),
@@ -1429,6 +1118,30 @@
       SL_RETURN(good_opt, _("sh_readconf_line"));
     }
-
- ok_novalue:
+  else
+    ++c;
+
+  /* skip leading whitespace
+   */
+  while ((*c) == ' ' || (*c) == '\t')
+    ++c;
+
+  if ((*c) == '\0')     /* no value                    */
+    {
+      if (line != NULL)
+	{
+	  TPT(( 0, FIL__, __LINE__, _("msg=<ConfigFile: not key=value: %s>\n"),
+		line));
+	}
+      SL_RETURN(good_opt, _("sh_readconf_line"));
+    }
+
+  /* convert to lowercase                              */
+
+  tmp = line;
+  while (*tmp != '=')
+    {
+      *tmp = tolower( (int) *tmp);
+      ++tmp;
+    }
 
   if (!sl_is_suid())
@@ -1437,18 +1150,4 @@
     }
 
-  /* Expand shell expressions. This return allocated memory which we must free.
-   * If !defined(SH_EVAL_SHELL), this will reduce to a strdup.
-   */
-  value_dup = sh_readconf_expand_value(value);
-
-  if (!value_dup || (*value_dup) == '\0')
-    {
-      TPT(( 0, FIL__, __LINE__, 
-	    _("msg=<ConfigFile: empty after shell expansion: %s>\n"),
-	    line));
-      SL_RETURN(good_opt, _("sh_readconf_line"));
-    }
-
-  value = value_dup;
 
 #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) 
@@ -1460,12 +1159,12 @@
 	       ++modkey) 
 	    {
-	      if (sl_strncmp (key,
+	      if (sl_strncmp (line,   
 			      _(modList[modnum].conf_table[modkey].the_opt),
 			      sl_strlen(modList[modnum].conf_table[modkey].the_opt) ) == 0)
 		{
 		  good_opt = 0;
-		  if (0 != modList[modnum].conf_table[modkey].func(value))
+		  if (0 != modList[modnum].conf_table[modkey].func(c))
 		    sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EINVALS,
-				     _(modList[modnum].conf_table[modkey].the_opt), value);
+				     _(modList[modnum].conf_table[modkey].the_opt), c);
 		  if (!sl_is_suid())
 		    {
@@ -1487,8 +1186,8 @@
       i = 0;
       while (ident[i] != NULL) {
-	if (sl_strncmp (key, _(ident[i]), sl_strlen(ident[i])) == 0)
+	if (sl_strncmp (line, _(ident[i]), sl_strlen(ident[i])-1) == 0)
 	  {
 	    good_opt = 0;
-	    sh_error_set_iv (identnum[i], value);
+	    sh_error_set_iv (identnum[i], c);
 	    break;
 	  }
@@ -1503,11 +1202,11 @@
 	  if ((ext_table[i].section == read_mode || 
 	       ext_table[i].alt_section == read_mode) &&
-	      sl_strncmp (key, _(ext_table[i].optname), 
+	      sl_strncmp (line, _(ext_table[i].optname), 
 			  sl_strlen(ext_table[i].optname)) == 0)
 	    {
 	      good_opt = 0;
-	      if (0 != ext_table[i].func (value))
+	      if (0 != ext_table[i].func (c))
 		sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EINVALS,
-				 _(ext_table[i].optname), value);
+				 _(ext_table[i].optname), c);
 	      break;
 	    }
@@ -1516,8 +1215,6 @@
     }
 
-  SH_FREE(value_dup);
-
   SL_RETURN(good_opt, _("sh_readconf_line"));
 }
-
+  
     
Index: trunk/src/sh_registry.c
===================================================================
--- trunk/src/sh_registry.c	(revision 591)
+++ 	(revision )
@@ -1,1019 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 2010       Rainer Wichmann                                */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-/***************************************************************************
- *
- * This file provides a module for samhain to check the MS Windows registry.
- *
- */
-
-#include "config_xor.h"
-
-#ifdef USE_REGISTRY_CHECK
-
-#include <windows.h>
-#include <stdio.h>
-#include <time.h>
-
-#define FIL__  _("sh_registry.c")
-
-/* We don't want to build this into yule 
- */
-#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
-
-#include <sys/types.h>
-#include <regex.h>
-
-#include "samhain.h"
-#include "sh_pthread.h"
-#include "sh_utils.h"
-#include "sh_unix.h"
-#include "sh_modules.h"
-#include "sh_hash.h"
-#include "sh_tiger.h"
-
-static int check_key (char * name, int isSingle);
-
-static int sh_reg_set_active  (const char *s);
-static int sh_reg_set_interval (const char * c);
-static int sh_reg_set_severity (const char *s);
-static int sh_reg_add_key (const char *s);
-static int sh_reg_add_hierarchy (const char *s);
-static int sh_reg_add_stop (const char *s);
-static int sh_reg_add_ign (const char *s);
-static int sh_reg_ign_time (const char *s);
-
-#define STOP_FALSE  0
-#define STOP_CHECK  1
-#define STOP_IGN    2
-
-sh_rconf sh_reg_check_table[] = {
-    {
-        N_("severitychange"),
-        sh_reg_set_severity,
-    },
-    {
-        N_("registrycheckactive"),
-        sh_reg_set_active,
-    },
-    {
-        N_("registrycheckinterval"),
-        sh_reg_set_interval,
-    },
-    {
-        N_("ignoretimestamponly"),
-        sh_reg_ign_time,
-    },
-    {
-        N_("singlekey"),
-        sh_reg_add_key,
-    },
-    {
-        N_("hierarchy"),
-        sh_reg_add_hierarchy,
-    },
-    {
-        N_("stopatkey"),
-        sh_reg_add_stop,
-    },
-    {
-        N_("ignorekey"),
-        sh_reg_add_ign,
-    },
-    {
-        NULL,
-        NULL
-    }
-};
-
-/* Runtime configuration */
-
-#define SH_REGISTRY_INTERVAL 300
-
-static int      ShRegCheckActive      = S_FALSE;
-static time_t   sh_reg_check_interval = SH_REGISTRY_INTERVAL;
-static int      sh_reg_check_severity = SH_ERR_SEVERE;
-static int      ShRegIgnTime          = S_FALSE;
-
-struct regkeylist {
-  char        * name;
-  int           stop;
-  int           single;
-#ifdef HAVE_REGEX_H
-  regex_t       preg;
-#endif
-
-  struct regkeylist *next;
-};
-
-static struct regkeylist * keylist = NULL;
-
-static int sh_reg_set_active(const char *s) 
-{
-  int value;
-    
-  SL_ENTER(_("sh_reg_set_active"));
-
-  value = sh_util_flagval(s, &ShRegCheckActive);
-
-  SL_RETURN((value), _("sh_reg_set_active"));
-}
-
-static int sh_reg_ign_time(const char *s) 
-{
-  int value;
-    
-  SL_ENTER(_("sh_reg_ign_time"));
-
-  value = sh_util_flagval(s, &ShRegIgnTime);
-
-  SL_RETURN((value), _("sh_reg_ign_time"));
-}
-
-static int sh_reg_set_interval (const char * c)
-{
-  int retval = 0;
-  long val;
-
-  SL_ENTER(_("sh_reg_set_interval"));
-  val = strtol (c, (char **)NULL, 10);
-  if (val <= 0)
-    {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
-		       _("registry check interval"), c);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      retval = -1;
-    }
-
-  sh_reg_check_interval = (time_t) val;
-  SL_RETURN(0, _("sh_reg_set_interval"));
-}
-
-static int sh_reg_set_severity (const char *s)
-{
-  char tmp[32];
-  tmp[0] = '='; tmp[1] = '\0';
-  sl_strlcat (tmp, s, 32);
-  return sh_error_set_level (tmp, &sh_reg_check_severity);
-}
-
-static int sh_reg_add_key_int (const char *s, int isSingle, int isStop)
-{
-  struct regkeylist * newkey;
-  size_t len = sl_strlen(s);
-
-  if (len > 0)
-    {
-      newkey = SH_ALLOC(sizeof(struct regkeylist));
-      newkey->single = isSingle;
-      newkey->stop   = isStop;
-      newkey->name = NULL;
-
-      if (STOP_FALSE == isStop)
-	{
-	  newkey->name = SH_ALLOC(len + 1);
-	  sl_strlcpy(newkey->name, s, len+1);
-	}
-      else
-	{
-#ifdef HAVE_REGEX_H
-	  int status = regcomp(&(newkey->preg), s, REG_NOSUB|REG_EXTENDED);
-	  if (status != 0)
-	    {
-	      char  errbuf[512];
-	      char  *p;
-	      regerror(status, &(newkey->preg), errbuf, sizeof(errbuf));
-
-	      sl_strlcat(errbuf, ": ", sizeof(errbuf));
-	      p = sh_util_safe_name_keepspace(s);
-	      sl_strlcat(errbuf, p, sizeof(errbuf));
-	      SH_FREE(p);
-
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN, 
-			      errbuf, _("sh_reg_add_key_int"));
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	      SH_FREE(newkey);
-	      return -1;
-	    }
-#else
-	  newkey->name = SH_ALLOC(len + 1);
-	  sl_strlcpy(newkey->name, s, len+1);
-#endif
-	}
-      newkey->next = keylist;
-      keylist      = newkey;
-      return 0;
-    }
-  return -1;
-}
-
-static int sh_reg_add_key (const char *s)
-{
-  return sh_reg_add_key_int (s, S_TRUE, STOP_FALSE);
-}
-static int sh_reg_add_hierarchy (const char *s)
-{
-  return sh_reg_add_key_int (s, S_FALSE, STOP_FALSE);
-}
-static int sh_reg_add_stop (const char *s)
-{
-  return sh_reg_add_key_int (s, S_FALSE, STOP_CHECK);
-}
-static int sh_reg_add_ign (const char *s)
-{
-  return sh_reg_add_key_int (s, S_FALSE, STOP_IGN);
-}
-
-/* Module functions      */
-
-int sh_reg_check_init(struct mod_type * arg)
-{
-#ifndef HAVE_PTHREAD
-  (void) arg;
-#endif
-
-  if (ShRegCheckActive == S_FALSE)
-    return SH_MOD_FAILED;
-#ifdef HAVE_PTHREAD
-  if (arg != NULL && arg->initval < 0 &&
-      (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
-    {
-      if (0 == sh_pthread_create(sh_threaded_module_run, (void *)arg))
-	return SH_MOD_THREAD;
-      else
-	return SH_MOD_FAILED;
-    }
-  else if (arg != NULL && arg->initval == SH_MOD_THREAD &&
-	   (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
-    {
-      return SH_MOD_THREAD;
-    }
-#endif
-  return 0;
-}
-
-int sh_reg_check_timer(time_t tcurrent)
-{
-  static time_t lastcheck = 0;
-
-  SL_ENTER(_("sh_reg_check_timer"));
-  if ((time_t) (tcurrent - lastcheck) >= sh_reg_check_interval)
-    {
-      lastcheck  = tcurrent;
-      SL_RETURN((-1), _("sh_reg_check_timer"));
-    }
-  SL_RETURN(0, _("sh_reg_check_timer"));
-}
-
-#define SH_REGFORM_NEW 1
-#define SH_REGFORM_OLD 2
-
-static char * format_changes(int flag, char * buf, size_t len,
-			     time_t time_old, unsigned long size_old, 
-			     unsigned long keys_old, unsigned long values_old,
-			     char * hash_old,
-			     time_t time_new, unsigned long size_new, 
-			     unsigned long keys_new, unsigned long values_new,
-			     char * hash_new)
-{
-  char timestr1[32];
-  char timestr2[32];
-  char timestr3[32];
-
-  char buf_old[512] = "";
-  char buf_new[512] = "";
-
-  if ((0 != (flag & SH_REGFORM_NEW)) && (NULL != hash_new))
-    {
-      (void) sh_unix_gmttime (time_new,   timestr1,  sizeof(timestr1));
-      (void) sh_unix_gmttime (keys_new,   timestr2,  sizeof(timestr2));
-      (void) sh_unix_gmttime (values_new, timestr3,  sizeof(timestr3));
-
-#ifdef SH_USE_XML
-      sl_snprintf(buf_new, sizeof(buf_new), 
-		  "size_new=\"%lu\" mtime_new=\"%s\" ctime_new=\"%s\" atime_new=\"%s\" chksum_new=\"%s\"",
-		  size_new, timestr1, timestr2, timestr3, hash_new);
-#else
-      sl_snprintf(buf_new, sizeof(buf_new), 
-		  "size_new=<%lu>, mtime_new=<%s>, ctime_new=<%s>, atime_new=<%s>, chksum_new=<%s>",
-		  size_new, timestr1, timestr2, timestr3, hash_new);
-#endif
-    }
-
-  if ((0 != (flag & SH_REGFORM_OLD)) && (NULL != hash_old))
-    {
-      (void) sh_unix_gmttime (time_old,   timestr1,  sizeof(timestr1));
-      (void) sh_unix_gmttime (keys_old,   timestr2,  sizeof(timestr2));
-      (void) sh_unix_gmttime (values_old, timestr3,  sizeof(timestr3));
-
-#ifdef SH_USE_XML
-      sl_snprintf(buf_old, sizeof(buf_old), 
-		  " size_old=\"%lu\" mtime_old=\"%s\" ctime_old=\"%s\" atime_old=\"%s\" chksum_old=\"%s\"",
-		  size_old, timestr1, timestr2, timestr3, hash_old);
-#else
-      sl_snprintf(buf_old, sizeof(buf_old), 
-		  " size_old=<%lu>, mtime_old=<%s>, ctime_old=<%s>, atime_old=<%s>, chksum_old=<%s>",
-		  size_old, timestr1, timestr2, timestr3, hash_old);
-#endif
-    }
-
-  sl_strlcpy(buf, buf_new, len);
-  sl_strlcat(buf, buf_old, len);
-
-  return buf;
-}
-
-static void report_missing_entry(const char * path)
-{
-  char  * infobuf  = SH_ALLOC(1024);
-  char  * errbuf   = SH_ALLOC(1024);
-  char  * tmp      = sh_util_safe_name (path);
-  char timestr[32];
-  struct store2db save;
-
-  memset(&save, 0, sizeof(struct store2db));
-  sh_hash_db2pop (path, &save);
-    
-  (void) sh_unix_gmttime (save.val1, timestr,  sizeof(timestr));
-  
-  sl_snprintf(infobuf, 1024, _("mtime=%s size=%lu subkeys=%lu values=%lu"),
-	      timestr, 
-	      (unsigned long) save.val0, 
-	      (unsigned long) save.val2, 
-	      (unsigned long) save.val3);
-
-  (void) format_changes (SH_REGFORM_OLD, errbuf, 1024, 
-			 save.val1, save.val0, save.val2, save.val3, save.checksum,
-			 0, 0, 0, 0, NULL);
-  
-  SH_MUTEX_LOCK(mutex_thread_nolog);
-  sh_error_handle(sh_reg_check_severity, FIL__, __LINE__, 0, MSG_REG_MISS, 
-		  infobuf, tmp, errbuf);
-  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-  
-  SH_FREE(tmp);
-  SH_FREE(errbuf);
-  SH_FREE(infobuf);
-  return;
-}
-
-int sh_reg_check_run(void)
-{
-  struct regkeylist *this = keylist;
-
-  if (this)
-    {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-		      _("Checking the registry"),
-		      _("sh_reg_check_run"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-
-      while (this)
-	{
-	  if (STOP_FALSE == this->stop)
-	    {
-	      /* 
-	       *  -- Check key -- 
-	       */
-	      check_key (this->name, this->single);
-	    }
-	  this = this->next;
-	}
-    }
-  sh_hash_unvisited_custom ('H', report_missing_entry);
-
-  return 0;
-}
-
-int sh_reg_check_reconf(void)
-{
-  struct regkeylist *this;
-
-  while (keylist)
-    {
-      this    = keylist;
-      keylist = keylist->next;
-
-      if (this->name)
-	SH_FREE(this->name);
-#ifdef HAVE_REGEX_H
-      if (STOP_FALSE != this->stop)
-	regfree(&(this->preg));
-#endif
-      SH_FREE(this);
-    }
-
-  sh_reg_check_interval = SH_REGISTRY_INTERVAL;
-
-  return 0;
-}
-
-int sh_reg_check_cleanup(void)
-{
-  sh_reg_check_reconf();
-  return 0;
-}
-
-/* >>>>>>>>>>>> Main check function <<<<<<<<<<<< */
-
-
-#include <windows.h>
-
-#define MAX_KEY_LENGTH (2*256)
-#define MAX_VALUE_NAME (2*16384)
-
-CHAR  achValue[MAX_VALUE_NAME];
-
-unsigned long nKeys = 0;
-unsigned long nVals = 0;
-
-static int CheckThisSubkey (HKEY key, char * subkey, char * path, 
-			    int isSingle, int view);
-
-static time_t convertTime(FILETIME * ft)
-{
-  time_t result;
-
-  /* Shift high part up by 2^32
-   */
-  UINT64 date = ((UINT64)ft->dwHighDateTime) << 32; 
-
-  /* Add low part 
-   */
-  date |= (UINT64)ft->dwLowDateTime;
-
-  /* Subtract difference between Jan 1, 1601 and Jan 1, 1970
-   */
-  date -= ((UINT64)116444736) * ((UINT64)100) * ((UINT64)10000000);
-
-  /* Divide by number of 100-nanosecond intervals per second
-   */
-  date /= ((UINT64)10000000);
-
-  /* Convert to a time_t 
-   */
-  result = (time_t) date;
-
-  return result;
-}
-
-#if !defined(KEY_WOW64_64KEY)
-#define KEY_WOW64_64KEY 0x0100;
-#endif
-#if !defined(KEY_WOW64_32KEY)
-#define KEY_WOW64_32KEY 0x0200;
-#endif
-
-
-#define SH_KEY_NULL _("000000000000000000000000000000000000000000000000")
-
-int QueryKey(HKEY hKey, char * path, size_t pathlen, int isSingle) 
-{ 
-  CHAR     achKey[MAX_KEY_LENGTH];   /* buffer for subkey name */
-  DWORD    cbName;                   /* size of name string */
-  /* CHAR     achClass[MAX_PATH] = "";  *//* buffer for class name */
-  /* DWORD    cchClassName = MAX_PATH/2;*//* size of class string */
-  DWORD    cSubKeys=0;               /* number of subkeys */
-  DWORD    cbMaxSubKey;              /* longest subkey size */
-  DWORD    cchMaxClass;              /* longest class string */
-  DWORD    cValues;              /* number of values for key */
-  DWORD    cchMaxValue;          /* longest value name */
-  DWORD    cbMaxValueData;       /* longest value data */
-  DWORD    cbSecurityDescriptor; /* size of security descriptor */
-  FILETIME ftLastWriteTime;      /* last write time */
-  DWORD    lpType;               /* type of data stored in value */
-  BYTE     lpData[256];          /* buffer for data in value */
-  DWORD    lpcbData;             /* size of lpData buffer */
-  DWORD    i, retCode; 
-  DWORD    cchValue = MAX_VALUE_NAME/2;
-
-  char hashbuf[KEYBUF_SIZE];
-  unsigned long totalSize = 0;
-  time_t fTime = 0;
-
-  char * tPath = NULL;
-  int    doUpdate = S_FALSE;
-
-  retCode = RegQueryInfoKey(
-			    hKey,                    /* key handle */
-			    NULL /* achClass */,     /* buffer for class name */
-			    NULL /* &cchClassName */,/* size of class string */
-			    NULL,                    /* reserved */
-			    &cSubKeys,               /* number of subkeys */
-			    &cbMaxSubKey,            /* longest subkey size */
-			    &cchMaxClass,            /* longest class string */
-			    &cValues,                /* number of values for this key */
-			    &cchMaxValue,            /* longest value name */
-			    &cbMaxValueData,         /* longest value data */
-			    &cbSecurityDescriptor,   /* security descriptor */
-			    &ftLastWriteTime);       /* last write time */
-  
-  if (retCode != ERROR_SUCCESS)
-    {
-      return -1;
-    }
-
-  ++nKeys;
-
-  fTime = convertTime (&ftLastWriteTime);
-
-  /* Enumerate the subkeys, until RegEnumKeyEx fails. */
-  
-  if (cSubKeys)
-    {
-      /*
-       * printf( "\nNumber of subkeys: %lu\n", (unsigned long) cSubKeys);
-       */
-
-      for (i=0; i<cSubKeys; i++) 
-	{ 
-	  cbName = MAX_KEY_LENGTH/2;
-	  retCode = RegEnumKeyEx(hKey, i,
-				 achKey, 
-				 &cbName, 
-				 NULL, 
-				 NULL, 
-				 NULL, 
-				 &ftLastWriteTime);
- 
-	  if (retCode == ERROR_SUCCESS && S_TRUE != isSingle) 
-	    {
-	      /*
-	       * _tprintf(TEXT("(%lu) %s\\%s\n"), (unsigned long) i+1, 
-	       * path, achKey);
-	       */
-	      CheckThisSubkey (hKey, achKey, path, isSingle, 0); 
-	    }
-	}
-    } 
-  
-  /* Enumerate the key values. */
-
-  if (cValues) 
-    {
-      char hashtmp[3][KEYBUF_SIZE];
-
-      memset(hashbuf, '0', sizeof(hashbuf));
-
-      /* Loop over values and build checksum */
-
-      for (i=0, retCode=ERROR_SUCCESS; i<cValues; i++) 
-	{ 
-	  LPBYTE lpDataAlloc = NULL;
-
-	  cchValue = MAX_VALUE_NAME/2; 
-	  achValue[0] = '\0';
-	  lpcbData = sizeof(lpData);
-	  retCode = RegEnumValue(hKey, i, 
-				 achValue, 
-				 &cchValue, 
-				 NULL, 
-				 &lpType,
-				 lpData,
-				 &lpcbData);
-	  
-	  if (retCode == ERROR_MORE_DATA)
-	    {
-	      lpDataAlloc = SH_ALLOC(lpcbData);
-
-	      retCode = RegEnumValue(hKey, i, 
-				     achValue, 
-				     &cchValue, 
-				     NULL, 
-				     &lpType,
-				     lpDataAlloc,
-				     &lpcbData);
-	    }
-
-	  if (retCode == ERROR_SUCCESS)
-	    {
-	      totalSize += lpcbData;
-
-	      /* checksum(valuename) */
-	      sh_tiger_hash (achValue, TIGER_DATA, cchValue, 
-			     hashtmp[0], KEYBUF_SIZE);
-
-	      /* checksum(valuedata) */
-	      if (NULL == lpDataAlloc)
-		{
-		  sh_tiger_hash ((char*) lpData,      TIGER_DATA, lpcbData, 
-				 hashtmp[1], KEYBUF_SIZE);
-		}
-	      else
-		{
-		  sh_tiger_hash ((char*) lpDataAlloc, TIGER_DATA, lpcbData, 
-				 hashtmp[1], KEYBUF_SIZE);
-		}
-
-	      /* old_checksum */
-	      memcpy(hashtmp[2], hashbuf, KEYBUF_SIZE);
-
-	      /* hash(hash(valuename)+hash(valuedata)+old_hash) */
-	      sh_tiger_hash ((char*) hashtmp, TIGER_DATA, 
-			     sizeof(hashtmp), hashbuf, sizeof(hashbuf));
-
-	      ++nVals;
-	    }
-
-	  if (lpDataAlloc)
-	    {
-	      SH_FREE(lpDataAlloc);
-	    }
-	}
-    }
-  else
-    {
-      /* no values */
-      sl_strlcpy(hashbuf, SH_KEY_NULL, sizeof(hashbuf));
-    }
-
-  /* Here we have:
-   *  hashbuf       [checksum over values], 
-   *  fTime         [last write time], 
-   *  totalSize     [size of all value data],
-   *  cSubKeys      [number of subkeys],
-   *  cValues       [number of values],
-   *  path, pathlen [which may be up to 131072 (256*512) bytes] 
-   */
-
-  if (pathlen > (PATH_MAX-1))
-    {
-      char hashbuf2[KEYBUF_SIZE];
-      char * p = strchr(path, '\\');
-
-      if (p)
-	{
-	  char *q = p;
-
-	  ++p;
-	  
-	  tPath = SH_ALLOC(256 + KEYBUF_SIZE);
-	  *q = '\0';
-	  sl_strlcpy(tPath, path, 256); /* truncates */
-	  *q = '\\';
-	  sl_strlcat(tPath, "\\", 257);
-	  (void) sh_tiger_hash(p, TIGER_DATA, sl_strlen(p), 
-			       hashbuf2, sizeof(hashbuf2));
-	  sl_strlcat(tPath, hashbuf2, 256 + KEYBUF_SIZE);
-	}
-    }
- 
-  if (sh.flag.checkSum == SH_CHECK_CHECK || sh.flag.update == S_TRUE)
-    {
-      struct store2db save;
-
-      memset(&save, 0, sizeof(struct store2db));
-
-      if (tPath)
-	{
-	  sh_hash_db2pop (tPath, &save);
-	}
-      else
-	{
-	  sh_hash_db2pop (path, &save);
-	}
-
-      if (save.size == -1)
-	{
-	  /* Not in database */
-
-	  char  * infobuf  = SH_ALLOC(1024);
-	  char  * errbuf   = SH_ALLOC(1024);
-	  char  * tmp      = sh_util_safe_name ((tPath == NULL) ? path : tPath);
-	  char timestr[32];
-      
-	  (void) sh_unix_gmttime (fTime, timestr,  sizeof(timestr));
-
-	  sl_snprintf(infobuf, 1024, 
-		      _("mtime=%s size=%lu subkeys=%lu values=%lu"), 
-		      timestr, 
-		      (unsigned long) totalSize, 
-		      (unsigned long) cSubKeys, 
-		      (unsigned long) cValues);
-
-	  (void) format_changes (SH_REGFORM_NEW, errbuf, 1024, 
-				 0, 0, 0, 0, NULL,
-				 fTime, totalSize, cSubKeys, cValues, hashbuf);
-      
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle(sh_reg_check_severity, FIL__, __LINE__, 
-			  0, MSG_REG_NEW, 
-			  infobuf, tmp, errbuf);
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  
-	  SH_FREE(tmp);
-	  SH_FREE(errbuf);
-	  SH_FREE(infobuf);
-
-	  doUpdate = S_TRUE;
-	}
-      else if (save.val0 != totalSize ||  
-	       save.val2 != cSubKeys ||
-	       save.val3 != cValues ||
-	       0 != strcmp(save.checksum, hashbuf) || 
-	       ( (((time_t) save.val1) != fTime) && (ShRegIgnTime == S_FALSE)) )
-	{
-	  /* Change detected */
-	  char  * infobuf  = SH_ALLOC(1024);
-	  char  * errbuf   = SH_ALLOC(1024);
-	  char  * tmp      = sh_util_safe_name ((tPath == NULL) ? path : tPath);
- 	  char timestr_new[32];
-      
-	  (void) sh_unix_gmttime (fTime,     timestr_new,  sizeof(timestr_new));
-
-	  sl_snprintf(infobuf, 1024, 
-		      _("mtime=%s size %lu->%lu subkeys %lu->%lu values %lu->%lu checksum %s"), 
-		      timestr_new, 
-		      (unsigned long) save.val0, (unsigned long) totalSize, 
-		      (unsigned long) save.val2, (unsigned long) cSubKeys, 
-		      (unsigned long) save.val3, (unsigned long) cValues, 
-		      (0 == strcmp(save.checksum, hashbuf)) ? _("good") : _("bad"));
-
-	  (void) format_changes (SH_REGFORM_OLD|SH_REGFORM_NEW, errbuf, 1024, 
-				 save.val1, save.val0, 
-				 save.val2, save.val3, save.checksum,
-				 fTime, totalSize, 
-				 cSubKeys, cValues, hashbuf);
-      
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle(sh_reg_check_severity, FIL__, __LINE__, 
-			  0, MSG_REG_CHANGE, 
-			  infobuf, tmp, errbuf);
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  
-	  SH_FREE(tmp);
-	  SH_FREE(errbuf);
-	  SH_FREE(infobuf);
-
-	  doUpdate = S_TRUE;
-	}
-
-    }
- 
-  if ( sh.flag.checkSum == SH_CHECK_INIT || doUpdate == S_TRUE /* change detected */ )
-    {
-      struct store2db save;
-
-      memset(&save, 0, sizeof(struct store2db));
-      
-      save.val0 = totalSize;
-      save.val1 = fTime;
-      save.val2 = cSubKeys;
-      save.val3 = cValues;
-      sl_strlcpy(save.checksum, hashbuf, KEY_LEN+1);
-
-      if (tPath)
-	{
-	  sh_hash_push2db (tPath, &save);
-	}
-      else
-	{
-	  sh_hash_push2db (path, &save);
-	}
-    }
-
-  /* Without this, freshly updated entries would get deleted
-   * as 'not seen'.
-   */
-  if (sh.flag.checkSum != SH_CHECK_INIT)
-    {
-      if (tPath)
-	sh_hash_set_visited (tPath);
-      else
-	sh_hash_set_visited (path);
-    }
-
-  if (tPath)
-    {
-      SH_FREE(tPath);
-    }
-
-  return 0;
-}
-
-static int check_for_stop (char * name)
-{
-  struct regkeylist *this = keylist;
-
-  while (this)
-    {
-      if (STOP_FALSE != this->stop)
-	{
-#ifdef HAVE_REGEX_H
-	  if (0 == regexec(&(this->preg), name, 0, NULL, 0))
-	    return this->stop;
-#else
-	  if (0 == strcmp(this->name, name))
-	    return this->stop;
-#endif
-	}
-      this = this->next;
-    }
-  return STOP_FALSE;
-}
-
-
-int CheckThisSubkey (HKEY key, char * subkey, char * path, int isSingle,
-		     int view)
-{
-  HKEY hTestKey;
-  LONG qError;
-  char * newpath;
-  size_t len;
-  int    retval = -1;
-
-  if (!subkey)
-    return 0;
-  
-  len = strlen(path) + 1 + strlen(subkey) + 1;
-  newpath = SH_ALLOC(len);
-  snprintf(newpath, len, "%s\\%s", path, subkey);
-  
-  /* Check for stop condition, if not single key. 
-   * Set flag to isSingle = S_TRUE if we should stop here. 
-   */
-  if (S_TRUE != isSingle)
-    {
-      int isStop = check_for_stop(newpath);
-
-      if (STOP_CHECK == isStop)
-	{
-	  isSingle = S_TRUE;
-	}
-      else if (STOP_IGN == isStop)
-	{
-	  SH_FREE(newpath);
-	  return 0;
-	}
-    }
-
-  len = strlen(path) + 1 + strlen(subkey) + 1;
-  newpath = SH_ALLOC(len);
-  snprintf(newpath, len, "%s\\%s", path, subkey);
-  
-  qError = RegOpenKeyEx( key,
-			 subkey,
-			 0,
-			 (KEY_READ | view),
-			 &hTestKey);
-
-
-  if (qError == ERROR_SUCCESS)
-    {
-      QueryKey(hTestKey, newpath, len-1, isSingle);
-      RegCloseKey(hTestKey);
-      retval = 0;
-    }
-  else
-    {
-      /* Error message */
-      LPVOID lpMsgBuf;
-  
-      char  * tmp     = sh_util_safe_name (newpath);
-      size_t  tlen    = sl_strlen(tmp);
-
-      if (S_TRUE == sl_ok_adds(64, tlen))
-	{
-	  char * errbuf;
-	  size_t elen;
-
-	  tlen += 64;
-
-	  elen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
-			       FORMAT_MESSAGE_FROM_SYSTEM |
-			       FORMAT_MESSAGE_IGNORE_INSERTS,
-			       NULL,
-			       qError,
-			       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
-			       /* cppcheck-suppress uninitvar */
-			       (LPTSTR) &lpMsgBuf,
-			       0, NULL );
-
-	  if (elen > 0 && S_TRUE == sl_ok_adds(elen, tlen))
-	    {
-	      tlen += elen;
-
-	      errbuf = SH_ALLOC(elen + tlen);
-	      sl_snprintf(errbuf, 64+tlen, _("Failed to open key %s: %s"), 
-			  tmp, lpMsgBuf);
-	      LocalFree(lpMsgBuf);
-
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-			      errbuf, _("CheckThisSubkey"));
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	      
-	      SH_FREE(errbuf);
-	    }
-	}
-      sh_reg_add_ign (newpath);
-      SH_FREE(tmp);
-    }
-  
-  SH_FREE(newpath);
-  return retval;
-}
-
-
-int check_key (char * key, int isSingle)
-{
-  HKEY topKey;
-  char * subkey = NULL;
-  char path[20] = "";
-  int pos = 0;
-  
-  if      (0 == strncmp(key, _("HKEY_CLASSES_ROOT"), 17))
-    {
-      topKey = HKEY_CLASSES_ROOT;
-      pos = 17;
-      strncpy(path, _("HKEY_CLASSES_ROOT"), sizeof(path));
-    }
-  else if (0 == strncmp(key, _("HKEY_CURRENT_USER"), 17))
-    {
-      topKey = HKEY_CURRENT_USER;
-      pos = 17;
-      strncpy(path, _("HKEY_CURRENT_USER"), sizeof(path));
-    }
-  else if (0 == strncmp(key, _("HKEY_LOCAL_MACHINE"), 18))
-    {
-      topKey = HKEY_LOCAL_MACHINE;
-      pos = 18;
-      strncpy(path, _("HKEY_LOCAL_MACHINE"), sizeof(path));
-    }
-  else if (0 == strncmp(key, _("HKEY_USERS"), 10))
-    {
-      topKey = HKEY_USERS;
-      pos = 10;
-      strncpy(path, _("HKEY_USERS"), sizeof(path));
-    }
-
-
-  if (pos > 0)
-    {
-      if (key[pos] == '\\')
-	{
-	  ++pos;
-	  subkey = &key[pos];
-	}
-    }
-  else
-    {
-      /* We bail out here if the topKey is undefined */
-      char * tmp = sh_util_safe_name_keepspace(key);
-      size_t tlen = sl_strlen(tmp);
-
-      if (S_TRUE == sl_ok_adds(64, tlen))
-	{
-	  char * errbuf = SH_ALLOC(64 + tlen);
-	  
-	  sl_snprintf(errbuf, 64+tlen, _("Invalid key %s"), tmp);
-	  
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-			  errbuf, _("check_key"));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  
-	  SH_FREE(errbuf);
-	}
-      SH_FREE(tmp);
-      return -1;
-    }
-
-  /************************  
-  if (ShCheckBothViews)
-    {
-      CheckThisSubkey (topKey, subkey, path, isSingle, KEY_WOW64_32KEY);
-      return CheckThisSubkey (topKey, subkey, path, isSingle, KEY_WOW64_64KEY);
-    }
-  *************************/
-
-  /* Returns 0 if !subkey */
-  /* cppcheck-suppress legacyUninitvar */
-  return CheckThisSubkey (topKey, subkey, path, isSingle, 0);
-}
-
-/* #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) */
-#endif
-
-/* #ifdef USE_REGISTRY_CHECK */
-#endif
-
Index: trunk/src/sh_restrict.c
===================================================================
--- trunk/src/sh_restrict.c	(revision 591)
+++ 	(revision )
@@ -1,686 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 2011 Rainer Wichmann                                      */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-#include "config_xor.h"
-
-#ifndef NULL
-#if !defined(__cplusplus)
-#define NULL ((void*)0)
-#else
-#define NULL (0)
-#endif
-#endif
-
-#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
-
-#ifdef HAVE_REGEX_H
-#include <sys/types.h>
-#include <regex.h>
-#endif
-#include <stdlib.h>
-#include <string.h>
-#include <ctype.h>
-
-#include "samhain.h"
-#include "sh_mem.h"
-#include "sh_error_min.h"
-#include "sh_string.h"
-#include "sh_utils.h"
-#include "sh_restrict.h"
-
-#define FIL__ _("sh_restrict.c")
-
-#define SH_COND_NOT    (1 << 0)
-#define SH_COND_PREFIX (1 << 1)
-#define SH_COND_REGEX  (1 << 2)
-#define SH_COND_SIZE   (1 << 3)
-#define SH_COND_PERM   (1 << 4)
-#define SH_COND_FTYPE  (1 << 5)
-#define SH_COND_PINCL  (1 << 6)
-
-#define SH_COND_MAX 6
-
-struct sh_restrict_cond {
-
-  unsigned char cond_type[SH_COND_MAX];
-
-#ifdef HAVE_REGEX_H
-  regex_t *     cond_preg[SH_COND_MAX];
-#endif
-
-  char  *       cond_str[SH_COND_MAX];
-
-  UINT64        cond_int[SH_COND_MAX];
-
-  struct sh_restrict_cond * next;
-};
-
-static struct sh_restrict_cond * sh_restrict_list = NULL;
-
-extern int matches_filetype(SL_TICKET ft, char * test_type);
-
-#ifdef HAVE_REGEX_H
-static int matches_regex (const char * path, const regex_t * regex)
-{
-  SL_ENTER(_("matches_regex"));
-
-  if (0 == regexec(regex, path, 0, NULL, 0))
-    {
-      SL_RETURN(1, _("matches_regex"));
-    }
-  SL_RETURN(0, _("matches_regex"));
-}
-#else
-static int matches_string (const char * path, const char * string)
-{
-  SL_ENTER(_("matches_string"));
-  
-  if (NULL == strstr(path, string))
-    {
-      SL_RETURN(0, _("matches_string"));
-    }
-  SL_RETURN(1, _("matches_string"));
-}
-#endif
-
-static int matches_prefix (const char * path, const char * prefix)
-{
-  size_t path_len;
-  size_t pref_len;
-
-  SL_ENTER(_("matches_prefix"));
-
-  if (path && prefix)
-    {
-      path_len = sl_strlen(path);
-      pref_len = sl_strlen(prefix);
-      
-      if (path_len >= pref_len)
-	{
-	  if (0 == strncmp(path, prefix, pref_len))
-	    {
-	      SL_RETURN(1, _("matches_prefix"));
-	    }
-	}
-    }
-  SL_RETURN(0, _("matches_prefix"));
-}
-
-static int exceeds_size (UINT64 size, UINT64 maxsize)
-{
-  SL_ENTER(_("exceeds_size"));
-
-  if (size > maxsize)
-    {
-      SL_RETURN(1, _("exceeds_size"));
-    }
-  SL_RETURN(0, _("exceeds_size"));
-}
-
-static int matches_perm (UINT64 perm, UINT64 needed_perm)
-{
-  SL_ENTER(_("matches_perm"));
-
-  if (needed_perm == (perm & 07777))
-    {
-      SL_RETURN(1, _("matches_perm"));
-    }
-  SL_RETURN(0, _("matches_perm"));
-}
-
-static int includes_perm (UINT64 perm, UINT64 needed_perm)
-{
-  UINT64 tmp = perm & 07777;
-
-  SL_ENTER(_("includes_perm"));
-
-  if (needed_perm == (tmp & needed_perm))
-    {
-      SL_RETURN(1, _("includes_perm"));
-    }
-  SL_RETURN(0, _("includes_perm"));
-}
-
-static int sh_restrict_test(const char * path, 
-			    UINT64 size, UINT64 perm, SL_TICKET fh,
-			    struct sh_restrict_cond * current)
-{
-  int i;
-  unsigned char flag;
-  int res = 0;
-
-  (void) fh;
-
-  SL_ENTER(_("sh_restrict_test"));
-
-  for (i = 0; i < SH_COND_MAX; ++i)
-    {
-      flag = current->cond_type[i];
-
-      if (flag != 0)
-	{
-	  if      ((flag & (SH_COND_PREFIX)) != 0) {
-	    res = matches_prefix(path, current->cond_str[i]);
-	  }
-	  else if ((flag & (SH_COND_REGEX)) != 0) {
-#ifdef HAVE_REGEX_H
-	    res = matches_regex(path, current->cond_preg[i]);
-#else
-	    res = matches_string(path, current->cond_str[i]);
-#endif
-	  }
-	  else if ((flag & (SH_COND_SIZE)) != 0) {
-	    res = exceeds_size(size, current->cond_int[i]);
-	  }
-	  else if ((flag & (SH_COND_PERM)) != 0) {
-	    res = matches_perm(perm, current->cond_int[i]);
-	  }
-	  else if ((flag & (SH_COND_PINCL)) != 0) {
-	    res = includes_perm(perm, current->cond_int[i]);
-	  }
-	  else if ((flag & (SH_COND_FTYPE)) != 0) {
-	    res = matches_filetype(fh, current->cond_str[i]);
-	  }
-
-	  /* Does condition hold?
-	   */
-	  if ((flag & (SH_COND_NOT)) != 0) {
-	    /* 
-	     * Condition negated, ok if false (res == 0)
-	     */
-	    if (0 != res) {
-	      SL_RETURN(0, _("sh_restrict_this"));
-	    }
-	  }
-	  else {
-	    /* Condition ok if true (res != 0) */
-	    if (0 == res) {
-	      SL_RETURN(0, _("sh_restrict_this"));
-	    }
-	  }
-	}
-      else
-	{
-	  break;
-	}
-    }
-
-  /* All conditions true, restricted 
-   */
-  SL_RETURN(1, _("sh_restrict_this"));
-}
-
-/* >>>>>>>>>> Evaluate the list <<<<<<<<<< */
-
-int sh_restrict_this(const char * path, UINT64 size, UINT64 perm, SL_TICKET fh)
-{
-  struct sh_restrict_cond * current = sh_restrict_list;
-
-  SL_ENTER(_("sh_restrict_this"));
-
-  if (!current) 
-    {
-      SL_RETURN(0, _("sh_restrict_this"));
-    }
-
-  while (current) 
-    {
-      if (0 != sh_restrict_test(path, size, perm, fh, current))
-	{
-	  /* The current conditions are true, restricted
-	   */
-	  SL_RETURN(1, _("sh_restrict_this"));
-	}
-      current = current->next;
-    }
-
-  SL_RETURN(0, _("sh_restrict_this"));
-}
-
-
-/* >>>>>>>>>> Purge the list <<<<<<<<<< */
-
-static void sh_restrict_delete (struct sh_restrict_cond * current)
-{
-  int i;
-
-  if (current->next)
-    {
-      sh_restrict_delete(current->next);
-    }
-
-  for (i = 0; i < SH_COND_MAX; ++i)
-    {
-      if (current->cond_str[i]) {
-	SH_FREE(current->cond_str[i]);
-      }
-#ifdef HAVE_REGEX_H
-      if (current->cond_preg[i]) {
-	regfree(current->cond_preg[i]);
-	SH_FREE(current->cond_preg[i]);
-      }
-#endif
-    }
-  SH_FREE(current);
-  return;
-}
-
-void sh_restrict_purge ()
-{
-  struct sh_restrict_cond * current = sh_restrict_list;
-
-  sh_restrict_list = NULL;
-  if (current)
-    sh_restrict_delete(current);
-
-  sh_restrict_add_ftype(NULL);
-
-  return;
-}
-
-/* >>>>>>>>>> Create the list <<<<<<<<<< */
-
-static char * get_com(char * str)
-{
-  char * s;
-  char * e;
-
-  /* skip leading WS 
-   */
-  for (s = str; *s && isspace((int)*s); ++s) /* nothing */;
-
-  e = strchr(s, '(');
-  if (e && (e != s))
-    {
-      *e = '\0'; --e;
-      while ( (e != s) && isspace((int)*e) )
-	{
-	  *e = '\0'; --e;
-	}
-      if (e != s)
-	return s;
-    }
-  return NULL;
-}
-
-static char * get_arg(char * str)
-{
-  char * s;
-  char * e;
-
-  s = strchr(str, '(');
-
-  if (s)
-    {
-      ++s;
-      
-      /* skip leading WS 
-       */
-      for (; *s && isspace((int)*s); ++s) /* nothing */;
-
-      e = strrchr(s, ')');
-      if (e && (e != s))
-	{
-	  /* check that nothing follows */
-	  char * u = e;
-	  ++u; for (; *u && isspace((int)*u); ++u) /* nothing */;
-	  if (*u != '\0') return NULL;
-	  
-	  /* strip trailing space */
-	  *e = '\0'; --e;
-	  while ( (e != s) && isspace((int)*e) )
-	    {
-	      *e = '\0'; --e;
-	    }
-	  
-	  if (e != s)
-	    return s;
-	}
-    }
-  return NULL;
-}
-
-static int set_cond(struct sh_restrict_cond * current, int i, 
-		    char * com, char * arg)
-{
-  if (!com || !arg || (i >= SH_COND_MAX))
-    return -1;
-
-  if      (0 == strcmp(com, _("match_prefix")))
-    {
-      current->cond_str[i] = sh_util_strdup(arg);
-      current->cond_type[i] |= SH_COND_PREFIX;
-    }
-  else if (0 == strcmp(com, _("match_regex")))
-    {
-#ifdef HAVE_REGEX_H
-      regex_t * preg = SH_ALLOC(sizeof(regex_t)); 
-
-      if (0 != regcomp(preg, arg, REG_NOSUB|REG_EXTENDED))
-	{
-	  SH_FREE(preg);
-	  return (-1);
-	}
-      current->cond_preg[i] = preg;
-#else
-      current->cond_str[i] = sh_util_strdup(arg);
-#endif
-      current->cond_type[i] |= SH_COND_REGEX;
-    }
-  else if (0 == strcmp(com, _("size_exceeds")))
-    {
-      current->cond_int[i] = (UINT64) strtoul(arg, (char **) NULL, 0);
-      current->cond_type[i] |= SH_COND_SIZE;
-    }
-  else if (0 == strcmp(com, _("match_permission")))
-    {
-      current->cond_int[i] = (UINT64) strtoul(arg, (char **) NULL, 8);
-      current->cond_type[i] |= SH_COND_PERM;
-    }
-  else if (0 == strcmp(com, _("have_permission")))
-    {
-      current->cond_int[i] = (UINT64) strtoul(arg, (char **) NULL, 8);
-      current->cond_type[i] |= SH_COND_PINCL;
-    }
-  else if (0 == strcmp(com, _("match_filetype")))
-    {
-      current->cond_str[i] = sh_util_strdup(arg);
-      current->cond_type[i] |= SH_COND_FTYPE;
-    }
-  else
-    {
-      return (-1);
-    }
-  return 0;
-}
-
-/* Format is [!]cond1(arg), cond2(arg), ... 
- */
-int sh_restrict_define(const char * str)
-{
-  SL_ENTER(_("sh_restrict_define"));
-
-  if (str) 
-    {
-      size_t lengths[SH_COND_MAX];
-      unsigned int nfields = SH_COND_MAX;
-      char ** array;
-      sh_string * def = sh_string_new_from_lchar(str, strlen(str));
-
-      array = split_array_list(sh_string_str(def), &nfields, lengths);
-
-      if (array && nfields > 0)
-	{
-	  char * p;
-	  char * q;
-	  unsigned int i;
-	  struct sh_restrict_cond * current = 
-	    SH_ALLOC(sizeof(struct sh_restrict_cond));
-
-	  current->next = NULL;
-	  for (i = 0; i < SH_COND_MAX; ++i)
-	    {
-	      current->cond_int[i]  = 0; 
-	      current->cond_type[i] = 0;
-	      current->cond_str[i] = NULL;
-#ifdef HAVE_REGEX_H
-	      current->cond_preg[i] = NULL;
-#endif
-	    }
-      
-	  for (i = 0; i < nfields; ++i) 
-	    {
-	      if (i == SH_COND_MAX)
-		{
-		  sh_restrict_delete (current);
-		  sh_string_destroy(&def);
-		  SH_FREE(array);
-		  SL_RETURN((-1), _("sh_restrict_define"));
-		}
-	      
-	      p = array[i];
-
-	      if (*p == '!')
-		{
-		  current->cond_type[i] |= SH_COND_NOT;
-		  ++p;
-		}
-
-	      q = get_arg(p);
-	      p = get_com(p);
-
-	      if (!q || !p || (0 != set_cond(current, i, p, q)))
-		{
-		  sh_restrict_delete (current);
-		  sh_string_destroy(&def);
-		  SH_FREE(array);
-		  SL_RETURN((-1), _("sh_restrict_define"));
-		}
-	    }
-
-	  SH_FREE(array);
-
-	  current->next = sh_restrict_list;
-	  sh_restrict_list = current;
-	}
-
-      sh_string_destroy(&def);
-      SL_RETURN(0, _("sh_restrict_define"));
-    }
-
-  SL_RETURN((-1), _("sh_restrict_define"));
-}
-
-
-/* #if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE) */
-#endif
-
-#ifdef SH_CUTEST
-#include "CuTest.h"
-
-void Test_restrict (CuTest *tc) {
-
-#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
-
-  char str[256];
-  char * p;
-  char * q;
-  int  res;
-  SL_TICKET fd;
-  char buf[1024];
-
-  strcpy(str, "match(this)");
-  p = get_arg(str);
-  q = get_com(str);
-  CuAssertPtrNotNull(tc, p);
-  CuAssertPtrNotNull(tc, q);
-  CuAssertStrEquals(tc, "match", q);
-  CuAssertStrEquals(tc, "this",  p);
-  
-  strcpy(str, "  match( this)");
-  p = get_arg(str);
-  q = get_com(str);
-  CuAssertPtrNotNull(tc, p);
-  CuAssertPtrNotNull(tc, q);
-  CuAssertStrEquals(tc, "match", q);
-  CuAssertStrEquals(tc, "this",  p);
-  
-  strcpy(str, "  match ( this ) ");
-  p = get_arg(str);
-  q = get_com(str);
-  CuAssertPtrNotNull(tc, p);
-  CuAssertPtrNotNull(tc, q);
-  CuAssertStrEquals(tc, "match", q);
-  CuAssertStrEquals(tc, "this",  p);
-  
-  strcpy(str, "  match   (this   ) ");
-  p = get_arg(str);
-  q = get_com(str);
-  CuAssertPtrNotNull(tc, p);
-  CuAssertPtrNotNull(tc, q);
-  CuAssertStrEquals(tc, "match", q);
-  CuAssertStrEquals(tc, "this",  p);
-  
-  strcpy(str, "size_exceeds(800), match_prefix(/home), match_regex(.*\\.mpg) ");
-  CuAssertTrue(tc, sh_restrict_list == NULL);
-  res = sh_restrict_define(str);
-  CuAssertIntEquals(tc,0,res);
-  CuAssertPtrNotNull(tc, sh_restrict_list);
-
-  sh_restrict_purge();
-  CuAssertTrue(tc, sh_restrict_list == NULL);
-
-  strcpy(str, "size_exceeds(800), match_prefix(/home), match_regex(.*\\.mpg), match_permission(0755) ");
-  CuAssertTrue(tc, sh_restrict_list == NULL);
-  res = sh_restrict_define(str);
-  CuAssertIntEquals(tc,0,res);
-  CuAssertPtrNotNull(tc, sh_restrict_list);
-
-  strcpy(str, "size_exceeds(800), match_prefix(/foo), have_permission(0100)");
-  res = sh_restrict_define(str);
-  CuAssertIntEquals(tc,0,res);
-  CuAssertPtrNotNull(tc, sh_restrict_list);
-
-  strcpy(str, "size_exceeds(800); match_prefix(/foo), have_permission(0100)");
-  res = sh_restrict_define(str);
-  CuAssertIntEquals(tc,-1,res);
-  CuAssertPtrNotNull(tc, sh_restrict_list);
-  
-  res = sh_restrict_this("/home/foo.mpg", 1000, 0755, 0);
-  CuAssertIntEquals(tc,1,res);
-
-  res = sh_restrict_this("/foo.mpg",      1000, 0755, 0);
-  CuAssertIntEquals(tc,1,res);
-
-  /* size too small */
-  res = sh_restrict_this("/foo.mpg",       600, 0755, 0);
-  CuAssertIntEquals(tc,0,res);
-
-  /* no execute permission */
-  res = sh_restrict_this("/foo.mpg",       600, 0644, 0);
-  CuAssertIntEquals(tc,0,res);
-
-  /* regex does not match */
-   res = sh_restrict_this("/home/foo",     1000, 0755, 0);
-  CuAssertIntEquals(tc,0,res);
-
-  /* wrong permission */
-  res = sh_restrict_this("/home/foo.mpg", 1000, 0705, 0);
-  CuAssertIntEquals(tc,0,res);
-  
-  /* size too small */
-  res = sh_restrict_this("/home/foo.mpg",  600, 0755, 0);
-  CuAssertIntEquals(tc,0,res);
-  
-  /* wrong prefix */
-  res = sh_restrict_this("/hoff/foo.mpg", 1000, 0755, 0);
-  CuAssertIntEquals(tc,0,res);
-  
-  sh_restrict_purge();
-  CuAssertTrue(tc, sh_restrict_list == NULL);
-
-  fd = sl_open_fastread(FIL__, __LINE__, "/bin/sh", SL_NOPRIV);
-  CuAssertTrue(tc, fd > 0);
-
-  strcpy(str, "match_prefix(/bin), match_filetype(EXECUTABLE:UNIX:ELF)");
-  res = sh_restrict_define(str);
-  CuAssertIntEquals(tc,0,res);
-  CuAssertPtrNotNull(tc, sh_restrict_list);
-
-#if !defined(HOST_IS_CYGWIN) && !defined(HOST_IS_DARWIN)
-  res = sh_restrict_this("/bin/sh", 1000, 0755, fd);
-  CuAssertIntEquals(tc,1,res);
-#endif
-
-  sl_close(fd);
-
-  sh_restrict_purge();
-  CuAssertTrue(tc, sh_restrict_list == NULL);
-
-  strcpy(str, "match_filetype(FILE:TEXT:COPYING)");
-  res = sh_restrict_define(str);
-  CuAssertIntEquals(tc,0,res);
-  CuAssertPtrNotNull(tc, sh_restrict_list);
-
-  p = getcwd(buf, sizeof(buf));
-  CuAssertPtrNotNull(tc, p);
-
-  strcpy(str, "0:0:0:FILE:TEXT:COPYING:Copying:=0a=53=41=4d=48=41=49=4e");
-  res = sh_restrict_add_ftype(str);
-  CuAssertIntEquals(tc,0,res);
-
-  sl_strlcat(buf, "/COPYING", sizeof(buf));
-  fd = sl_open_fastread(FIL__, __LINE__, buf, SL_NOPRIV);
-  CuAssertTrue(tc, fd > 0);
-
-  res = sh_restrict_this(buf, 1000, 0755, fd);
-  CuAssertIntEquals(tc,1,res);
-
-  res = sh_restrict_add_ftype(str);
-  CuAssertIntEquals(tc,0,res);
-  res = sh_restrict_add_ftype(str);
-  CuAssertIntEquals(tc,0,res);
-  res = sh_restrict_add_ftype(str);
-  CuAssertIntEquals(tc,0,res);
-
-  res = sh_restrict_add_ftype(str);
-  CuAssertIntEquals(tc,0,res);
-  res = sh_restrict_add_ftype(str);
-  CuAssertIntEquals(tc,0,res);
-  res = sh_restrict_add_ftype(str);
-  CuAssertIntEquals(tc,0,res);
-  res = sh_restrict_add_ftype(str);
-  CuAssertIntEquals(tc,0,res);
-
-  res = sh_restrict_add_ftype(str);
-  CuAssertIntEquals(tc,0,res);
-  res = sh_restrict_add_ftype(str);
-  CuAssertIntEquals(tc,0,res);
-  res = sh_restrict_add_ftype(str);
-  CuAssertIntEquals(tc,0,res);
-  res = sh_restrict_add_ftype(str);
-  CuAssertIntEquals(tc,0,res);
-
-  res = sh_restrict_add_ftype(str);
-  CuAssertIntEquals(tc,0,res);
-  res = sh_restrict_add_ftype(str);
-  CuAssertIntEquals(tc,0,res);
-  res = sh_restrict_add_ftype(str);
-  CuAssertIntEquals(tc,0,res);
-  res = sh_restrict_add_ftype(str);
-  CuAssertIntEquals(tc,0,res);
-
-  res = sh_restrict_add_ftype(str);
-  CuAssertIntEquals(tc,-1,res);
-
-  sh_restrict_purge();
-  CuAssertTrue(tc, sh_restrict_list == NULL);
-
-  res = sh_restrict_add_ftype(str);
-  CuAssertIntEquals(tc,0,res);
-  
-#else
-  (void) tc;
-/* #if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE) */
-#endif
-
-}
-#endif
-
-
Index: trunk/src/sh_schedule.c
===================================================================
--- trunk/src/sh_schedule.c	(revision 591)
+++ trunk/src/sh_schedule.c	(revision 1)
@@ -42,12 +42,16 @@
 #endif
 
+#if TIME_WITH_SYS_TIME
+#include <sys/time.h>
+#include <time.h>
+#else
 #if HAVE_SYS_TIME_H
 #include <sys/time.h>
-#endif
+#else
 #include <time.h>
-
-#include "samhain.h"
+#endif
+#endif
+
 #include "sh_mem.h"
-#include "sh_error_min.h"
 
 /* TESTONLY */
@@ -125,7 +129,4 @@
   struct tm * tval;
   int count, i, nval;
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
-  struct tm     time_tm;
-#endif
 
   if (!isched)
@@ -133,16 +134,5 @@
 
   now  = time(NULL);
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
-  tval = localtime_r(&now, &time_tm);
-#else
   tval = localtime(&now);
-#endif
-
-  if (!tval)
-    {
-      sh_error_handle ((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
-                       _("localime() failed"), _("test_sched_int") );
-      return 0;
-    }
 
   count = 0;
@@ -215,5 +205,5 @@
 	/*@+charint@*//* Incompatible types for == (char, char): ??? */
 	for (k = 0; k < 3; ++k)
-	  if (p[k] != '\0' && tolower((int) p[k]) == MonNames[j][k]) ++l;
+	  if (p[k] != '\0' && tolower(p[k]) == MonNames[j][k]) ++l;
 	/*@-charint@*/
 	if (l == 3)
@@ -227,5 +217,5 @@
 	/*@+charint@*//* Incompatible types for == (char, char): ??? */
 	for (k = 0; k < 3; ++k)
-	  if (p[k] != '\0' && tolower((int) p[k]) == DayNames[j][k]) ++l;
+	  if (p[k] != '\0' && tolower(p[k]) == DayNames[j][k]) ++l;
 	/*@-charint@*/
 	if (l == 3)
@@ -328,26 +318,16 @@
   char * copy;
   int    i = 0;
-  size_t len;
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
-  char * saveptr;
-#endif
 
   if (!ssched || !isched)
     return -1;
 
-  len = strlen(ssched)+1;
 #ifdef TESTONLY
-  copy = calloc(1,len);                 /* testonly code */
-#else
-  copy = SH_ALLOC(len);
-#endif
-  sl_strlcpy(copy, ssched, len);
-
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
-  p = strtok_r(copy, " \t", &saveptr); /* parse crontab-style schedule */
-#else
+  copy = malloc(strlen(ssched)+1);                 /* testonly code */
+#else
+  copy = SH_ALLOC(strlen(ssched)+1);
+#endif
+  strcpy(copy, ssched);                            /* known to fit  */
+
   p = strtok(copy, " \t"); /* parse crontab-style schedule */
-#endif
-
   if (!p)
     goto err; 
@@ -357,9 +337,5 @@
   for (i = 1; i < 5; ++i)
     {
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
-      p = strtok_r(NULL, " \t", &saveptr); /* parse crontab-style schedule */
-#else
       p = strtok(NULL, " \t"); /* parse crontab-style schedule */
-#endif
       if (!p)
 	goto err; 
@@ -367,4 +343,10 @@
 	goto err;
     }
+
+  /*
+  for (i = 0; i < 5; ++i)
+    printf("%2d MIN  %3d  MAX  %3d  STEP  %3d\n", 
+	   i, isched->min[i], isched->max[i], isched->step[i]);
+  */
 
   isched->last_exec = (time_t)-1;
@@ -441,5 +423,5 @@
     {
       if (test_sched(&isched))
-	printf("EXECUTE  at: %s", ctime(&(isched.last_exec))); /* TESTONLY */
+	printf("EXECUTE  at: %s", ctime(&(isched.last_exec)));
       sleep (1); /* TESTONLY */
     }
Index: trunk/src/sh_sem.c
===================================================================
--- trunk/src/sh_sem.c	(revision 591)
+++ 	(revision )
@@ -1,325 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 1999, 2000, 2015 Rainer Wichmann                          */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-#include "config_xor.h"
-#include <stdlib.h>
-#include <stdio.h>
-
-#if defined(HAVE_SYS_SEM_H) && defined(HAVE_UNISTD_H)
-#include "samhain.h"
-#include "sh_sem.h"
-#include "sh_error_min.h"
-
-#include <signal.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/ipc.h>
-#include <sys/sem.h>
-#include <unistd.h>
-
-#undef  FIL__
-#define FIL__  _("sh_sem.c")
-
-typedef enum {
-  exit_ok   = 0,
-  exit_fail = 1,
-  exit_time = 2,
-  exit_err  = 3
-} sh_estat;
-
-
-/* FreeBSD 6.1 defines this in <sys/sem.h> too...     */
-#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
-/* union semun is defined by including <sys/sem.h>    */
-#else
-#if !defined(HAVE_UNION_SEMUN)
-/* according to X/OPEN we have to define it ourselves */
-union semun {
-  int val;
-  struct semid_ds *buf;
-  unsigned short *array;
-};
-#endif
-#endif
-
-
-#define SH_SEMVMX 32767
-
-static int get_semaphore (void) 
-{
-  key_t  key = ftok(DEFAULT_DATAROOT, '#');
-  int    semid;
-
-  if (key < 0)
-    return -1;
-  semid = semget (key, 0, IPC_PRIVATE);
-  if (semid < 0)
-    return -1;
-  return semid;
-}
-
-static void sem_purge(int sem_id)
-{
-  union semun tmp;
-
-  tmp.val = 0;
-  
-  if (sem_id != -1)
-    semctl(sem_id, 0, IPC_RMID, tmp);
-  return;
-}
-
-static void sem_purge_stale()
-{
-  int stale_ID = get_semaphore();
-  if (stale_ID != -1)
-    sem_purge(stale_ID);
-  return;
-}
-
-static int report_err(int errnum, char * file, int line, char * func)
-{
-  char errbuf[SH_ERRBUF_SIZE];
-  sh_error_message(errnum, errbuf, sizeof(errbuf));
-  sh_error_handle((-1), file, line, errnum, MSG_E_SUBGEN,
-		  errbuf, func);
-  return -1;
-}
-
-static int init_semaphore (int nsems) 
-{
-  int    i;
-  mode_t mask;
-  int    semid;
-  int    errnum;
-  union semun tmp;
-  key_t  key = ftok(DEFAULT_DATAROOT, '#');
-
-  if (key < 0)
-    return report_err(errno, FIL__, __LINE__, _("ftok"));
-
-  mask   = umask(0);
-  semid  = semget (key, nsems, IPC_CREAT | IPC_EXCL | 0660);
-  errnum = errno;
-  umask(mask);
-  
-  tmp.val = 1;
-
-  if (semid < 0)
-    return report_err(errnum, FIL__, __LINE__, _("semget"));
-  for (i=0; i<nsems; ++i)
-    if (semctl (semid, i, SETVAL, tmp) == -1)
-      return report_err(errnum, FIL__, __LINE__, _("semclt"));
-  return semid;
-}
-
-
-static int sem_set(int semid, int sem_no, int val)
-{
-  union semun tmp;
-
-  tmp.val = val;
-  
-  if (semid < 0)
-    return -1;
-  if (semctl (semid, sem_no, SETVAL, tmp) == -1)
-    return -1;
-  return 0;
-}
-
-static int sem_get(int semid, int sem_no)
-{
-  union semun tmp;
-  if (semid < 0)
-    return -1;
-  tmp.val = 0;
-  return semctl (semid, sem_no, GETVAL, tmp);
-}
-
-
-static int sem_change(int semid, int sem_no, int amount)
-{
-  struct sembuf tmp;
-  int retval;
-
-  tmp.sem_num = sem_no;
-  tmp.sem_flg = SEM_UNDO;
-  tmp.sem_op  = amount;
-
-  do { retval = semop(semid, &tmp, 1);
-  } while (retval == -1 && errno == EINTR);
-
-  return retval;
-}
-
-static int sem_try_change(int semid, int sem_no, int amount)
-{
-  struct sembuf tmp;
-  int retval;
-
-  tmp.sem_num = sem_no;
-  tmp.sem_flg = IPC_NOWAIT|SEM_UNDO;
-  tmp.sem_op  = amount;
-
-  do { retval = semop(semid, &tmp, 1);
-  } while (retval == -1 && errno == EINTR);
-
-  return retval;
-}
-
-#define SH_SEMAPHORE_EXTERN(S)  int S = get_semaphore()
-#define SH_SEMAPHORE_INIT(S, N) int S = init_semaphore(N)
-#define SH_SEMAPHORE_TRYLOCK(S) sem_try_change(S, 0, SH_SEM_LOCK)
-#define SH_SEMAPHORE_LOCK(S)    sem_change(S, 0, SH_SEM_LOCK)
-#define SH_SEMAPHORE_UNLOCK(S)  sem_change(S, 0, SH_SEM_UNLOCK)
-#define SH_SEMAPHORE_PURGE(S)   sem_purge(S)
-
-static int sem_ID = -1;
-
-void sh_sem_open()
-{
-  if (sh.flag.isdaemon != S_TRUE)
-    return;
-
-  if (sem_ID < 0)
-    {
-      sem_purge_stale();
-      sem_ID = init_semaphore(2);
-      sem_set(sem_ID, 1, (int) 0);
-    }
-
-  return;
-}
-
-void sh_sem_trylock()
-{
-  SH_SEMAPHORE_TRYLOCK(sem_ID);
-  return;
-}
-
-void sh_sem_lock()
-{
-  SH_SEMAPHORE_LOCK(sem_ID);
-  return;
-}
-
-void sh_sem_unlock (long val)
-{
-  if (val >= 0)
-    {
-      val = (val > SH_SEMVMX) ? SH_SEMVMX : val; /* signed short int maxval */
-      sem_set(sem_ID, 1, (int) val);
-    }
-  SH_SEMAPHORE_UNLOCK(sem_ID);
-  return;
-}
-
-void sh_sem_close()
-{
-  SH_SEMAPHORE_PURGE(sem_ID);
-  return;
-}
-
-static volatile int alarm_triggered = 0;
-static void alarm_handler(int sig)
-{
-  (void) sig;
-  alarm_triggered = 1;
-  return;
-}
-
-int  sh_sem_wait(const char * wait)
-{
-  int rc, flag = 0;
-  int time_wait = atoi(wait);
-
-  SH_SEMAPHORE_EXTERN(sem_id);
-
-  if (time_wait < 0) { time_wait *= (-1); time_wait -= 1; flag = 1; }
-  if (time_wait < 0 || time_wait > (24*3600))
-    {
-      fprintf(stderr, _("Invalid argument <%d>.\n"), time_wait);
-      _exit(exit_err);
-    }
-  if (sem_id == -1)
-    {
-      if (flag && errno == ENOENT) { 
-	do { retry_msleep(1, 0); rc = get_semaphore(); } while (rc == -1);
-	sem_id = rc;
-      } else {
-	if (errno == ENOENT) {
-	  fputs(_("Samhain IPC not initialized.\n"), stderr);
-	  _exit(exit_err); }
-	else if (errno == EACCES)
-	  fputs(_("No permission to access Samhain IPC.\n"), stderr);
-	_exit(exit_err);
-      }
-    }
-
-  retry_msleep(0, 50);
-
-  if (time_wait > 0)
-    {
-      signal(SIGALRM, alarm_handler);
-      alarm(time_wait);
-    }
-  rc = SH_SEMAPHORE_LOCK(sem_id);
-  if (rc == -1 && errno == EINTR)
-    {
-      if (alarm_triggered)
-        {
-	  fputs(_("Timeout on wait.\n"), stderr);
-	  _exit(exit_time);
-        }
-    }
-  else if (rc == -1)
-    {
-      if (errno == EACCES)
-	fputs(_("No permission to access Samhain IPC.\n"), stderr);
-      else
-	perror(_("semop"));
-      _exit(exit_err);
-    }
-
-  rc = sem_get(sem_id, 1);
-  if (rc == 0)    
-    _exit(exit_ok);
-  else if (rc == SH_SEMVMX)
-    fprintf(stdout, _("%d or more issues reported\n"), rc);
-  else
-    fprintf(stdout, _("%d issues reported\n"), rc);
-  _exit(exit_fail);
-}
-
-#else
-
-void sh_sem_open()    { return; }
-void sh_sem_trylock() { return; }
-void sh_sem_lock()    { return; }
-void sh_sem_unlock(long val)  { (void) val; return; }
-void sh_sem_close()   { return; }
-int  sh_sem_wait(const char * wait)
-{
-  (void) wait;
-  fputs(_("Function not implemented (OS does not support SysV semaphores).\n"),
-	stderr);
-  exit(exit_err);
-}
-
-#endif /* defined(HAVE_SYS_SEM_H) && defined(HAVE_UNISTD_H) */
Index: trunk/src/sh_sig.c
===================================================================
--- trunk/src/sh_sig.c	(revision 591)
+++ 	(revision )
@@ -1,1789 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 1999, 2000 Rainer Wichmann                                */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-#include "config_xor.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-
-
-#if defined(WITH_SIG)
-
-#include <unistd.h>
-#include <fcntl.h>
-#include <signal.h>
-#if defined(SH_WITH_SERVER)
-#include <pwd.h>
-#endif
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <errno.h>
-#include <sys/wait.h>
-
-#include <string.h>
-#ifdef HAVE_MEMORY_H
-#include <memory.h>
-#endif
-
-
-#if !defined(O_NONBLOCK)
-#if defined(O_NDELAY)
-#define O_NONBLOCK  O_NDELAY
-#else
-#define O_NONBLOCK  0
-#endif
-#endif
-
-
-#include "samhain.h"
-#include "sh_utils.h"
-#include "sh_error.h"
-#include "sh_tiger.h"
-#if defined(SH_WITH_SERVER)
-#define SH_NEED_PWD_GRP 1
-#include "sh_static.h"
-#endif
-#include "sh_sig.h"
-
-int get_the_fd(SL_TICKET file_1);
-
-#if defined(WITH_GPG)
-static struct {
-  char     conf_id[SH_MINIBUF+1];
-  char     conf_fp[SH_MINIBUF+1];
-  char     data_id[SH_MINIBUF+1];
-  char     data_fp[SH_MINIBUF+1];
-} gp;
-#endif
-
-typedef struct {
-  pid_t    pid;
-  FILE   * pipe;
-} sh_gpg_popen_t;
-
-#define SH_SIG_OK      0
-#define SH_SIG_BAD     1
-#define SH_SIG_BADSIGN 2
-
-/* replace #if 0 by #if 1 and set an appropriate path in front of '/pdbg.'
- * for debugging
- */
-#if 0
-#define PDGBFILE "/pdbg."
-#endif
-
-#if defined(PDGBFILE)
-FILE * pdbg;
-FILE * pdbgc;
-#define PDBG_OPEN    pdbg = fopen(PDGBFILE"main",  "a")  
-#define PDBG_CLOSE   sl_fclose (FIL__, __LINE__, pdbg)
-#define PDBG(arg)    fprintf(pdbg,  "PDBG: step %d\n", arg); fflush(pdbg)
-#define PDBG_D(arg)  fprintf(pdbg,  "PDBG: %d\n", arg); fflush(pdbg)
-#define PDBG_S(arg)  fprintf(pdbg,  "PDBG: %s\n", arg); fflush(pdbg)
-
-#define PDBGC_OPEN   pdbgc = fopen(PDGBFILE"child", "a")  
-#define PDBGC_CLOSE  sl_fclose (FIL__, __LINE__, pdbgc)
-#define PDBGC(arg)   fprintf(pdbgc, "PDBG: step %d\n", arg); fflush(pdbgc)
-#define PDBGC_D(arg) fprintf(pdbgc, "PDBG: %d\n", arg); fflush(pdbgc)
-#define PDBGC_S(arg) fprintf(pdbgc, "PDBG: %s\n", arg); fflush(pdbgc)
-#else
-#define PDBG_OPEN    
-#define PDBG_CLOSE   
-#define PDBG(arg)    
-#define PDBG_D(arg)  
-#define PDBG_S(arg)  
-#define PDBGC_OPEN    
-#define PDBGC_CLOSE   
-#define PDBGC(arg)    
-#define PDBGC_D(arg)  
-#define PDBGC_S(arg)  
-#endif
-
-#undef  FIL__
-#define FIL__  _("sh_sig.c")
-
-#if defined(SIG_HASH) || defined(SIG_KEY_HASH)
-
-typedef enum { SIG_HASH_REPORT, SIG_HASH_REPORTFULL, SIG_HASH_OTHER } checksum_flag;
-
-static int sh_sig_checksum (SL_TICKET checkfd, checksum_flag flag, const char * expected_in, const char * path)
-{
-  char * test_sig;
-  char * expected = NULL;
-  char * test_ptr1 = NULL;
-  char * test_ptr2 = NULL;
-  char   wstrip1[128];
-  char   wstrip2[128];
-  int    i, k;
-#include "sh_sig_chksum.h"
-
-  SL_ENTER(_("sh_sig_checksum"));
-
-  
-  if (flag == SIG_HASH_OTHER)
-    expected = sh_util_strdup(expected_in);
-  
-  if (flag == SIG_HASH_OTHER)
-    test_sig = sh_tiger_hash_gpg (path, checkfd, TIGER_NOLIM);
-  else
-    test_sig = sh_tiger_hash_gpg (DEFAULT_SIG_PATH, checkfd, TIGER_NOLIM);
-  
-  test_ptr1 = (flag == SIG_HASH_OTHER) ? strchr(expected, ':') : strchr(SIG_HASH, ':');
-  if (test_ptr1 != NULL)
-    test_ptr1 += 2;
-  else
-    test_ptr1 = (flag == SIG_HASH_OTHER) ? expected : SIG_HASH;
-
-  if (test_sig != NULL)
-    test_ptr2 = strchr(test_sig, ':');
-  if (test_ptr2 != NULL)
-    test_ptr2 += 2;
-  else
-    test_ptr2 = test_sig;
-
-  /* Tue Jun 24 23:11:54 CEST 2003 (1.7.9) -- strip whitespace
-   */
-  k = 0;
-  for (i = 0; i < 127; ++i)
-    {
-      if (test_ptr1[i] == '\0')
-	break;
-      if (test_ptr1[i] != ' ')
-	{
-	  wstrip1[k] = test_ptr1[i];
-	  ++k;
-	}
-    }
-  wstrip1[k] = '\0';
-
-  if (flag != SIG_HASH_OTHER)
-    {
-      for(i = 0; i < KEY_LEN; ++i)
-	{
-	  if (sigchk[i] != wstrip1[i]) 
-	    {
-	      sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0, MSG_E_GPG_CHK, 
-			      sigchk, wstrip1);
-	      break;
-	    }
-	}
-    }
-
-  k = 0;
-  if (test_ptr2)
-    {
-      for (i = 0; i < 127; ++i)
-	{
-	  if (test_ptr2[i] == '\0')
-	    break;
-	  if (test_ptr2[i] != ' ')
-	    {
-	      wstrip2[k] = test_ptr2[i];
-	      ++k;
-	    }
-	}
-    }
-  wstrip2[k] = '\0';
-
-  if (0 != sl_strncmp(wstrip1, wstrip2, 127))
-    {
-      TPT(((0), FIL__, __LINE__, _("msg=<sig checksum: %s>\n"), test_sig));
-      TPT(((0), FIL__, __LINE__, _("msg=<compiled in : %s>\n"), (flag == SIG_HASH_OTHER) ? expected : SIG_HASH));
-      TPT(((0), FIL__, __LINE__, _("msg=<wstrip1     : %s>\n"), wstrip1));
-      TPT(((0), FIL__, __LINE__, _("msg=<wstrip2     : %s>\n"), wstrip2));
-      if (flag == SIG_HASH_REPORTFULL)
-	sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_GPG, 
-			SIG_HASH, test_sig);
-      if (flag == SIG_HASH_OTHER)
-	dlog(1, FIL__, __LINE__, _("The compiled-in checksum of the public key %s\n(%s)\ndoes not match the actual checksum\n(%s).\nYou need to recompile with the correct checksum."), path, wstrip1, wstrip2);
-      else
-	dlog(1, FIL__, __LINE__, _("The compiled-in checksum of the signature checking binary %s\n(%s)\ndoes not match the actual checksum\n(%s).\nYou need to recompile with the correct checksum."), DEFAULT_SIG_PATH, wstrip1, wstrip2);
-      SH_FREE(test_sig);
-      if (expected)
-	SH_FREE(expected);
-      SL_RETURN((-1), _("sh_sig_checksum"));
-    }
-  SH_FREE(test_sig);
-  if (expected)
-    SH_FREE(expected);
-  SL_RETURN( (0), _("sh_sig_checksum"));
-}
-#endif
-
-struct startup_info {
-  long   line;
-  char * program;
-  long   uid;
-  char * path;
-  char * key_uid;
-  char * key_id;
-};
-
-static struct startup_info startInfo = { 0, NULL, 0, NULL, NULL, NULL };
-
-static void sh_sig_fill_startup (long line, char * program, long uid, char * path, 
-				 char * key_uid, char * key_id)
-{
-  startInfo.line    = line;
-  startInfo.program = sh_util_strdup(program);
-  startInfo.uid     = uid;
-  startInfo.path    = sh_util_strdup(path);
-  if (key_uid)
-    startInfo.key_uid = sh_util_strdup(key_uid);
-  else
-    startInfo.key_uid = sh_util_strdup(_("(not given)"));
-  if (key_id)
-    startInfo.key_id  = sh_util_strdup(key_id);
-  else
-    startInfo.key_id  = sh_util_strdup(_("(not given)"));
-  return;
-}
-
-typedef enum { SIG_DATASIG, SIG_DATAONLY } extractlevel;
-
-
-static FILE * sh_sig_popen (char *const argv[], sh_gpg_popen_t  *source, int fd);
-
-
-static FILE * sh_sig_popen (char *const arg[], sh_gpg_popen_t  *source, int fd)
-{
-  size_t len;
-  extern int flag_err_debug;
-  int pipedes[2];
-  FILE * outf = NULL;
-  char * envp[2];
-
-#if defined(HAVE_SIG_CHECKSUM)
-  SL_TICKET   checkfd = -1;
-  int         myrand;
-  int         i;
-#if defined(__linux__)
-  int         get_the_fd(SL_TICKET);
-  char        pname[128];
-  int         pfd;
-  int         val_return;
-#endif
-#endif
-
-  SL_ENTER(_("sh_sig_popen"));
-
-  /* use homedir of effective user
-   */
-  len = sl_strlen(sh.effective.home) + 6;
-  envp[0] = calloc(1, len); /* free() ok   */
-  if (envp[0] != NULL)
-	sl_snprintf (envp[0], len, _("HOME=%s"), sh.effective.home); 
-  envp[1] = NULL;
-
-  /* Create the pipe 
-   */
-  if (aud_pipe(FIL__, __LINE__, pipedes) < 0) 
-    {
-      if (envp[0] != NULL) 
-	free(envp[0]);
-      SL_RETURN( (NULL), _("sh_gpg_popen"));
-    }
-
-  fflush (NULL);
-  
-  source->pid = aud_fork(FIL__, __LINE__);
-  
-  /* Failure
-   */
-  if (source->pid == (pid_t) - 1) 
-    {
-      sl_close_fd(FIL__, __LINE__, pipedes[0]);
-      sl_close_fd(FIL__, __LINE__, pipedes[1]);
-      if (envp[0] != NULL) 
-	free(envp[0]);
-      SL_RETURN( (NULL), _("sh_sig_popen"));
-    }
-
-  if (source->pid == (pid_t) 0) 
-    {
-
-      /* child - make read side of the pipe stdout 
-       */
-      if (retry_aud_dup2(FIL__, __LINE__,
-			pipedes[STDOUT_FILENO], STDOUT_FILENO) < 0)
-	{
-	  TPT(((0), FIL__, __LINE__, _("msg=<dup2 on pipe failed>\n")));
-	  dlog(1, FIL__, __LINE__, _("Internal error: dup2 failed\n"));
-	  aud__exit(FIL__, __LINE__, EXIT_FAILURE);
-	}
-      
-      /* close the pipe descriptors 
-       */
-      sl_close_fd (FIL__, __LINE__, pipedes[STDIN_FILENO]);
-      sl_close_fd (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
-      
-      if (retry_aud_dup2(FIL__, __LINE__, fd, STDIN_FILENO) < 0)
-	{
-	  TPT(((0), FIL__, __LINE__, _("msg=<dup2 on fd failed>\n")));
-	  dlog(1, FIL__, __LINE__, _("Internal error: dup2 failed\n"));
-	  aud__exit(FIL__, __LINE__, EXIT_FAILURE);
-	}
- 
-      /* don't leak file descriptors
-       */
-      sh_unix_closeall (3, -1, S_TRUE); /* in child process */
-
-      if (flag_err_debug != S_TRUE)
-	{
-	  if (NULL == freopen(_("/dev/null"), "r+", stderr))
-	    {
-	      dlog(1, FIL__, __LINE__, _("Internal error: freopen failed\n"));
-	      aud__exit(FIL__, __LINE__, EXIT_FAILURE);
-	    }
-	}
-
-
-      /* We should become privileged if SUID,
-       * to be able to read the keyring.
-       * We have checked that gpg is OK,
-       * AND that only a trusted user could overwrite
-       * gpg.
-       */
-      memset (skey, 0, sizeof(sh_key_t));
-      aud_setuid(FIL__, __LINE__, geteuid());
-      
-      PDBGC_OPEN;
-      PDBGC_D((int)getuid());
-      PDBGC_D((int)geteuid());
-
-      {
-	int i = 0;
-	while (arg[i] != NULL)
-	  {
-	    PDBGC_S(arg[i]);
-	    ++i;
-	  }
-      }
-      PDBGC_CLOSE;
-
-      /* exec the program */
-
-#if defined(__linux__) && defined(HAVE_SIG_CHECKSUM)
-      /* 
-       * --  emulate an fexecve with checksum testing
-       */
-      checkfd = sl_open_read(FIL__, __LINE__, DEFAULT_SIG_PATH, SL_NOPRIV);
-
-      if (0 != sh_sig_checksum(checkfd, SIG_HASH_REPORT, NULL, NULL))
-	{
-	  sl_close(checkfd);
-	  aud__exit(FIL__, __LINE__, EXIT_FAILURE);
-	}
-
-      pfd = get_the_fd(checkfd);
-      do {
-	val_return = dup (pfd);
-      } while (val_return < 0 && errno == EINTR);
-      pfd = val_return;
-      sl_close(checkfd);
-      /* checkfd = -1; *//* never read */
-
-      sl_snprintf(pname, sizeof(pname), _("/proc/self/fd/%d"), pfd);
-      if (0 == access(pname, R_OK|X_OK))               /* flawfinder: ignore */
-
-	{
-	  fcntl  (pfd, F_SETFD, FD_CLOEXEC);
-	  retry_aud_execve (FIL__, __LINE__,  pname, arg, envp);
-	      
-	  dlog(1, FIL__, __LINE__, _("Unexpected error: execve %s failed\n"),
-	       pname);
-	  /* failed 
-	   */
-	  aud__exit(FIL__, __LINE__, EXIT_FAILURE);
-	}
-	  
-      /* procfs not working, go ahead 
-       */
-#endif
-
-#if defined(HAVE_SIG_CHECKSUM)
-      /* This is an incredibly ugly kludge to prevent an attacker
-       * from knowing when it is safe to slip in a fake executable
-       * between the integrity check and the execve
-       */
-      myrand = (int) taus_get ();
-
-      myrand = (myrand < 0) ? (-myrand) : myrand;
-      myrand = (myrand % 32) + 2;
-
-      for (i = 0; i < myrand; ++i)
-	{
-	  checkfd = sl_open_fastread(FIL__, __LINE__, 
-				     DEFAULT_SIG_PATH, SL_NOPRIV);
-
-	  if (0 != sh_sig_checksum(checkfd, SIG_HASH_REPORT, NULL, NULL)) {
-	    aud__exit(FIL__, __LINE__, EXIT_FAILURE);
-	  }
-	  sl_close(checkfd);
-	}
-#endif
-
-      retry_aud_execve (FIL__, __LINE__, DEFAULT_SIG_PATH, arg, envp);
-      dlog(1, FIL__, __LINE__, _("Unexpected error: execve %s failed\n"),
-	   DEFAULT_SIG_PATH);
-      
-      /* failed 
-       */
-      TPT(((0), FIL__, __LINE__, _("msg=<execve failed>\n")));
-      dlog(1, FIL__, __LINE__, _("Unexpected error: execve failed\n"));
-      aud__exit(FIL__, __LINE__, EXIT_FAILURE);
-    }
-
-  /* parent
-   */
-
-  if (envp[0] != NULL) 
-    free(envp[0]);
-
-  sl_close_fd (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
-  retry_fcntl (FIL__, __LINE__, pipedes[STDIN_FILENO], F_SETFD, FD_CLOEXEC);
-  retry_fcntl (FIL__, __LINE__, pipedes[STDIN_FILENO], F_SETFL,  O_NONBLOCK);
-
-  outf = fdopen (pipedes[STDIN_FILENO], "r");
-  
-  if (outf == NULL) 
-    {
-      aud_kill (FIL__, __LINE__, source->pid, SIGKILL);
-      sl_close_fd (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
-      waitpid (source->pid, NULL, 0);
-      source->pid = 0;
-      SL_RETURN( (NULL), _("sh_sig_popen"));
-    }
-  
-  SL_RETURN( (outf), _("sh_sig_popen"));
-}
-
-
-static int sh_sig_pclose (sh_gpg_popen_t *source)
-{
-  int status = 0;
-  
-  SL_ENTER(_("sh_sig_pclose"));
-
-  status = sl_fclose(FIL__, __LINE__, source->pipe);
-  if (status)
-    SL_RETURN( (-1), _("sh_sig_pclose"));
-  
-  if (waitpid(source->pid, NULL, 0) != source->pid)
-    status = -1;
-  
-  source->pipe = NULL;
-  source->pid = 0;
-  SL_RETURN( (status), _("sh_sig_pclose"));
-}
-
-/* This is signify specific stuff 
- */
-#if defined(WITH_SIGNIFY)
-
-#include <ctype.h>
-
-static
-int sh_signify_comp_comm(const char * line, size_t * commlen)
-{
-  /* check for a valid comment line: not exceeding 1023 chars and 
-   * starting with 'untrusted comment: ' */
-  static char   cmp[SH_MINIBUF];
-  static size_t cmp_len = 0;
-
-  size_t len = sl_strlen(line);
-  
-  if (cmp_len == 0) {
-    sl_strlcpy(cmp, _("untrusted comment: "), sizeof(cmp));
-    cmp_len = strlen(cmp);
-  }
-
-  if (line[len-1] == '\n') {
-    /* signify will replace the '\n' with '\0', so 1024 -> 1023, which fits */
-    if (len > 1024) return S_FALSE;
-    else            *commlen = len;
-  } else {
-    if (len > 1023) return S_FALSE;
-    else            *commlen = (len+1);
-  }
-
-  if (len >= cmp_len && 0 == strncmp(cmp, line, cmp_len))
-    return S_TRUE;
-  return S_FALSE;
-}
-
-static const char bto64_0[] = N_("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
-static char bto64[65] = { '\0' };
-
-static
-int sh_signify_comp_sig(const char * line, size_t commlen)
-{
-  char   cmp[128];
-  char   out[128];
-  size_t len = sl_strlen(line);
-  size_t i, j = 0;
-  int padf = 0;
-
-  if (bto64[0] == '\0')
-    memcpy(bto64, _(bto64_0), 65);
-  
-  if (line[len-1] == '\n') {
-    if ((len+commlen) > 2047) return S_FALSE;
-  } else {
-    if ((len+commlen) > 2046) return S_FALSE;
-  }
-
-  for (i = 0; i < len; ++i)
-    {
-      if (isspace(line[i])) {
-	/* signify will skip arbitrary space, using isspace() */
-	continue;
-      }
-      if (line[i] == '=') {
-	if (padf > 1) /* more than two padding '=' */
-	  return S_FALSE;
-	else
-	  ++padf;
-      } else if (!strchr(bto64, line[i]) || (line[i] == '=' && padf > 0)) {
-	return S_FALSE;
-      }
-      if (j < sizeof(cmp)) {
-	  cmp[j] = line[i]; ++j;
-      }
-    }
-
-  /* signature is 'Ed' + 8 byte random + 64 bytes = 74 bytes
-   * => 1 pad byte => 75 bytes => 100 b64 bytes */
-  if (j != 100 || padf != 1)
-    return S_FALSE;
-  
-  cmp[j] = '\0'; /* j == 100 */
-  sh_util_base64_dec((unsigned char *) out, (unsigned char *) cmp, j);
-  if(out[0] == 'E' && out[1] == 'd')
-    return S_TRUE;
-  
-  return S_FALSE;
-}
-static
-int sh_signify_msg_start(const char * line)
-{
-  static int  step = 0;
-  static size_t commlen = 0;
-
-  if (step == 0) {
-    if (S_TRUE == sh_signify_comp_comm(line, &commlen))
-      ++step;
-  }
-  else if (step == 1) {
-    if (S_TRUE == sh_signify_comp_sig(line, commlen)) {
-      ++step;
-    }
-    else {
-      step = 0; commlen = 0;
-    }
-  }
-  else if (step == 2) {
-    step = 0; commlen = 0;
-    return S_TRUE;
-  }
-  return S_FALSE;
-}
-
-static
-int sh_signify_msg_startdata(const char * line)
-{
-  (void) line;
-  return S_TRUE;
-}
- 
-static
-int sh_signify_msg_end(const char * line)
-{
-  if (line[0] != '\0')
-    return S_FALSE;
-  return S_TRUE;
-}
-
-static
-int sh_signify_data_end(const char * line)
-{
-  if (line[0] == '[' && line[1] == 'E' && line[2] == 'O' &&
-      line[3] == 'F' && line[4] == ']')
-    return S_TRUE;
-  else if (line[0] != '\0')
-    return S_FALSE;
-  return S_TRUE;
-}
-
-static
-SL_TICKET sh_signify_extract_signed(SL_TICKET fd, extractlevel extract_level)
-{
-  const  int fgets_buf_size = 16384;
-  FILE * fin_cp = NULL;
-  char * buf    = NULL;
-  int    bufc;
-  char * comment = NULL;
-  size_t commlen = 0;
-  
-  int    flag_comm = S_FALSE;
-  int    flag_sig  = S_FALSE;
-  SL_TICKET fdTmp  = (-1);
-  SL_TICKET open_tmp (void);
-
-  /* extract the data and copy to temporary file
-   */
-  fdTmp = open_tmp();
-  if (SL_ISERROR(fdTmp))
-    {
-      dlog(1, FIL__, __LINE__, _("Error opening temporary file.\n")); 
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-		      _("Error opening temporary file."), 
-		      _("sh_signify_extract_signed"));
-      return -1;
-    }
-
-  fin_cp = fdopen(dup(get_the_fd(fd)), "rb");
-  buf = SH_ALLOC(fgets_buf_size);
-
-  while (NULL != fgets(buf, fgets_buf_size, fin_cp))
-    {
-      
-      bufc = 0; 
-      while (bufc < fgets_buf_size) { 
-	if (buf[bufc] == '\n') { ++bufc; break; }
-	++bufc;
-      }
-
-      if (flag_comm == S_FALSE)
-	{
-	  if (sh_signify_comp_comm(buf, &commlen) == S_TRUE)
-	    {
-	      flag_comm = S_TRUE;
-	      if (extract_level == SIG_DATASIG)
-		{
-		  comment = sh_util_strdup(buf);
-		  commlen = bufc;
-		}
-	    }
-	  continue;
-	}
-      else if (flag_comm == S_TRUE && flag_sig == S_FALSE)
-	{
-	  if (sh_signify_comp_sig(buf, commlen) == S_TRUE)
-	    {
-	      flag_sig = S_TRUE;
-	      if (extract_level == SIG_DATASIG)
-		{
-		  sl_write(fdTmp, comment, commlen);
-		  sl_write(fdTmp, buf, bufc);
-		}
-	      if (comment != NULL)
-		SH_FREE(comment);
-	      comment = NULL;
-	    }
-	  else
-	    {
-	      if (comment != NULL)
-		SH_FREE(comment);
-	      comment = NULL; commlen = 0; flag_comm = 0;
-	    }
-	  continue;
-	}
-      
-      if (flag_sig == S_TRUE)
-	{
-	  sl_write(fdTmp, buf, bufc); 
-	}
-    }
-  if (comment != NULL)
-    SH_FREE(comment);
-  sl_fclose(FIL__, __LINE__, fin_cp);
-  sl_rewind (fdTmp);
-
-#if defined(SH_DEBUG_SIGNIFY)
-  fin_cp = fdopen(dup(get_the_fd(fdTmp)), "rb");
-  FILE * fout = fopen("xxx.out", "w+");
-  while (NULL != fgets(buf, fgets_buf_size, fin_cp))
-    {
-      fputs(buf, fout);
-    }
-  fclose(fout);
-  sl_rewind(fdTmp);
-#endif
-  
-  SH_FREE(buf);
-  return fdTmp;
-}
-
-
-static FILE * sh_signify_popen (sh_gpg_popen_t  *source, int fd, char * homedir)
-{
-  char   path[256];
-  char   cc1[32];
-  char   cc2[32];
-  char   cc3[32];
-  char   cc4[SH_PATHBUF+32];
-  char   cc5[32];
-  char   cc6[32];
-  char * argv[9];
-  FILE * retval = NULL;
-
-  struct stat lbuf;
-  int         status_stat = 0;
-
-#ifdef HAVE_SIG_KEY_HASH
-  SL_TICKET checkfd;
-#endif
-
-
-  SL_ENTER(_("sh_signify_popen"));
-
-  sl_strlcpy (path,  DEFAULT_SIG_PATH,  256);
-
-  sl_strlcpy (cc1,   _("-Vem"),         32);
-  sl_strlcpy (cc2,   _("/dev/null"),    32);
-
-  sl_strlcpy (cc3,   _("-p"),           32);
-  sl_strlcpy (cc4,   homedir,           SH_PATHBUF+32);
-  sl_strlcat (cc4,   _("/.signify/"),   SH_PATHBUF+32);
-  sl_strlcat (cc4,   SH_INSTALL_NAME,   SH_PATHBUF+32);
-  sl_strlcat (cc4,   _(".pub"),         SH_PATHBUF+32);
-
-  /* read signed message from stdin */
-  sl_strlcpy (cc5,   _("-x"),           32);
-  sl_strlcpy (cc6,   _("-"),            32);
-
-  status_stat =  retry_lstat(FIL__, __LINE__, cc4, &lbuf);
-  if (status_stat == -1)
-    {
-      dlog(1, FIL__, __LINE__, 
-	   _("Signify public key %s\ndoes not exist or is not accessible.\nPlease add the directory and put the key there\nto allow signature verification.\n"),
-	   cc4);
-      sh_error_handle((-1), FIL__, __LINE__, status_stat, MSG_EXIT_ABORT1, 
-		      sh.prg_name);
-      aud_exit (FIL__, __LINE__, EXIT_FAILURE);
-    }
-#ifdef HAVE_SIG_KEY_HASH
-  checkfd = sl_open_read(FIL__, __LINE__, cc4, SL_YESPRIV);
-
-  if (0 != sh_sig_checksum(checkfd, SIG_HASH_OTHER, SIG_KEY_HASH, cc4))
-    {
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-		      _("Checksum mismatch for signify public key"), 
-		      _("signify_popen"));
-      sl_close(checkfd);
-      sh_error_handle((-1), FIL__, __LINE__, status_stat, MSG_EXIT_ABORT1, 
-		      sh.prg_name);
-      aud_exit (FIL__, __LINE__, EXIT_FAILURE);
-    }
-  sl_close(checkfd);
-#endif
- 
-  argv[0] = path; 
-  argv[1] = cc1;
-  argv[2] = cc2;
-  argv[3] = cc3;
-  argv[4] = cc4;
-  argv[5] = cc5;
-  argv[6] = cc6;
-  argv[7] = NULL;
-
-  retval = sh_sig_popen(argv, source, fd);
-  SL_RETURN((retval), _("sh_signify_popen")); 
-}
-
-static
-int sh_signify_check_file_sign(int fd, char * homedir)
-{
-  struct stat buf;
-  char line[256];
-  sh_gpg_popen_t  source;
-  int status = 0;
-  unsigned int n_goodsig  = 0;
-  unsigned int n_lines    = 0;
-  
-#ifdef HAVE_SIG_CHECKSUM
-  SL_TICKET checkfd;
-#endif
-
-  SL_ENTER(_("sh_signify_check_file_sign"));
-
-  /* check whether signify exists and has the correct checksum
-   */
-  TPT(((0), FIL__, __LINE__, _("msg=<Check signature>\n")));
-  TPT(((0), FIL__, __LINE__, _("msg=<signify is %s>\n"), DEFAULT_SIG_PATH));
-
-  if (0 != retry_lstat(FIL__, __LINE__, DEFAULT_SIG_PATH, &buf))
-    {
-      char errbuf[SH_ERRBUF_SIZE];
-
-      status = errno;
-      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_ERR_LSTAT,
-		      sh_error_message(status, errbuf, sizeof(errbuf)), DEFAULT_SIG_PATH);
-      SL_RETURN( SH_SIG_BAD, _("sh_signify_check_file_sign"));
-    }
-
-  if (0 != tf_trust_check (DEFAULT_SIG_PATH, SL_YESPRIV))
-    SL_RETURN( SH_SIG_BAD, _("sh_signify_check_file_sign"));
-
-#ifdef HAVE_SIG_CHECKSUM
-  checkfd = sl_open_read(FIL__, __LINE__, DEFAULT_SIG_PATH, SL_YESPRIV);
-
-  if (0 != sh_sig_checksum(checkfd, SIG_HASH_REPORTFULL, NULL, NULL))
-    {
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-		      _("Checksum mismatch"), 
-		      _("signify_check_file_sign"));
-      sl_close(checkfd);
-      SL_RETURN( SH_SIG_BAD, _("sh_signify_check_file_sign"));
-    }
-  sl_close(checkfd);
-#endif
-
-  TPT(((0), FIL__, __LINE__, _("msg=<Open pipe to check signature>\n")));
-
-  fflush(NULL);
- 
-  source.pipe   = sh_signify_popen  ( &source, fd, homedir );
-
-  if (NULL == source.pipe)
-    {
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-		      _("Could not open pipe"), 
-		      _("signify_check_file_sign"));
-      SL_RETURN( SH_SIG_BAD, _("sh_signify_check_file_sign"));
-    }
-
-  TPT(((0), FIL__, __LINE__, _("msg=<Open pipe success>\n")));
-
- xagain:
-
-  errno = 0;
-
-  while (NULL != fgets(line, sizeof(line), source.pipe))
-    {
-      TPT(((0), FIL__, __LINE__, _("msg=<signify out: %s>\n"), line));
-      if (line[strlen(line)-1] == '\n')
-	line[strlen(line)-1] = ' ';
-      sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-		      line, 
-		      _("signify_check_file_sign"));
-
-      ++n_lines;
-
-      /* the '\n' has been replaced with ' ' for logging */ 
-      if (0 == sl_strcmp(_("Signature Verified "), line))
-	{
-	  ++n_goodsig;
-	} 
-    }
-
-  if (ferror(source.pipe) && errno == EAGAIN) 
-    {
-      /* sleep 10 ms to avoid starving the gpg child writing to the pipe */
-      retry_msleep(0,10); 
-      clearerr(source.pipe);
-      goto xagain;
-    }
- 
-  if (0 != sh_sig_pclose (&source))
-    {
-	sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-			_("Error on closing process pipe"), 
-			_("signify_check_file_sign"));
-	n_goodsig = 0;
-    }
-
-  TPT(((0), FIL__, __LINE__, _("msg=<Close pipe>\n")));
-
-  if (n_goodsig == 1 && n_lines == 1)
-    {
-      TPT(((0), FIL__, __LINE__, _("msg=<Signature Verified>\n")));
-      SL_RETURN( SH_SIG_OK, _("sh_signature_check_file_sign"));
-    }
-  else
-    {
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-		      _("Error verifying file signature"), 
-		      _("signify_check_file_sign"));
-    }
-  SL_RETURN( SH_SIG_BADSIGN, _("sh_signature_check_file_sign"));
-}
-
-
-int sh_signify_check_signature (SL_TICKET file, ShSigFile what)
-{
-  int status = SH_SIG_BAD;
-  int fd = 0;
-
-  static int smsg = S_FALSE;
-
-  char  * homedir = sh.effective.home;
-  char  * home_alloc = NULL;
-#if defined(SH_WITH_SERVER)
-  struct passwd * tempres;
-#if defined(USE_GETPWNAM_R)
-  struct passwd    pwd;
-  char           * buffer = SH_ALLOC(SH_PWBUF_SIZE);
-#endif
-#endif
-
-  SL_ENTER(_("sh_signify_check_sign"));
-
-  (void) what;
-  
-  fd = get_the_fd(file);
-
-  if (fd < 0)
-    {
-      TPT(((0), FIL__, __LINE__, _("msg=<GPG_CHECK: FD = %d>\n"), fd));
-      dlog(1, FIL__, __LINE__, 
-	   _("This looks like an unexpected internal error.\n"));
-#if defined(SH_WITH_SERVER) && defined(USE_GETPWNAM_R)
-      SH_FREE(buffer);
-#endif
-      SL_RETURN( (-1), _("sh_signify_check_sign"));
-    }
-  
-#if defined(SH_WITH_SERVER)
-#if defined(USE_GETPWNAM_R)
-  sh_getpwnam_r(DEFAULT_IDENT, &pwd, buffer, SH_PWBUF_SIZE, &tempres);
-#else
-  tempres = sh_getpwnam(DEFAULT_IDENT);
-#endif
-  if ((tempres != NULL) && (0 == sl_ret_euid()))
-    {
-      /* privileges not dropped yet*/
-      homedir = tempres->pw_dir;
-    }
-#endif
-
-  home_alloc = sh_util_strdup(homedir);
-  
-  TPT(((0), FIL__, __LINE__, _("msg=<SIGNIFY_CHECK: FD = %d>\n"), fd));
-  status = sh_signify_check_file_sign(fd, homedir);
-  
-  if (status != SH_SIG_OK) 
-    {
-      TPT(((0), FIL__, __LINE__, _("msg=<Status = %d>\n"), status));
-      dlog(1, FIL__, __LINE__, 
-	   _("The signature of the configuration file or the file signature database\ncould not be verified. Possible reasons are:\n - signify binary (%s) not found\n - invalid signature\n - there is no keyfile in %s/.signify/%s.pub, or\n - the file is not signed - did you move /filename.sig to /filename ?\nTo create a signed file, use (remove old signatures before):\n   signify|signify-openbsd -Se -s KEYNAME.sec -m FILE\n   mv FILE.sig FILE\n"),
-	   DEFAULT_SIG_PATH, home_alloc, SH_INSTALL_NAME);
-      SH_FREE(home_alloc);
-      SL_RETURN( (-1), _("sh_signify_check_sign"));
-    }
-
-  if (smsg == S_FALSE)
-    {
-      sh_sig_fill_startup (__LINE__,
-			   sh.prg_name, sh.real.uid,
-			   (sh.flag.hidefile == S_TRUE) ? 
-			   _("(hidden)") : file_path('C', 'R'), 
-			   NULL,  NULL);
-    }
-  smsg = S_TRUE;
-
-  SH_FREE(home_alloc);
-  SL_RETURN(0, _("sh_signify_check_sign"));
-}  
-
-/* This is GPG specific stuff 
- */
-#elif defined(WITH_GPG)
-static FILE * sh_gpg_popen (sh_gpg_popen_t  *source, int fd, char * homedir)
-{
-  char   path[256];
-  char   cc1[32];
-  char   cc2[32];
-
-  char   cc0[2] = "-";
-  char   cc3[32];
-  char   cc4[SH_PATHBUF+32];
-  char   cc5[32];
-  char * argv[9];
-  FILE * retval = NULL;
-
-
-  SL_ENTER(_("sh_gpg_popen"));
-
-  /* -- GnuPG -- */
-  sl_strlcpy (path,  DEFAULT_SIG_PATH,  256);
-  sl_strlcpy (cc1,   _("--status-fd"),  32);
-  sl_strlcpy (cc2,   _("--verify"),     32);
-  sl_strlcpy (cc3,   _("--homedir"),    32);
-  /* sl_strlcpy (cc4,   sh.effective.home, SH_PATHBUF+32); */
-  sl_strlcpy (cc4,   homedir,           SH_PATHBUF+32);
-  sl_strlcat (cc4,   _("/.gnupg"),      SH_PATHBUF+32);
-  sl_strlcpy (cc5,   _("--no-tty"),     32);
-
-#if defined(SH_WITH_SERVER)
-  if (0 == sl_ret_euid())   /* privileges not dropped yet */
-    {
-      struct stat lbuf;
-      int         status_stat = 0;
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-      struct passwd    pwd;
-      char          *  buffer = SH_ALLOC(SH_PWBUF_SIZE);
-      struct passwd *  tempres;
-      sh_getpwnam_r(DEFAULT_IDENT, &pwd, buffer, SH_PWBUF_SIZE, &tempres);
-#else
-      struct passwd * tempres = sh_getpwnam(DEFAULT_IDENT);
-#endif
-
-      if (!tempres)
-	{
-	  dlog(1, FIL__, __LINE__, 
-	       _("User %s does not exist. Please add the user to your system.\n"), 
-	       DEFAULT_IDENT);
-	  status_stat = -1;
-	}
-      if (!tempres->pw_dir || tempres->pw_dir[0] == '\0')
-	{
-	  dlog(1, FIL__, __LINE__, 
-	       _("User %s does not have a home directory.\nPlease add the home directory for this user to your system.\n"), 
-	       DEFAULT_IDENT);
-	  status_stat = -2;
-	}
-      if (status_stat == 0)
-	{
-	  sl_strlcpy (cc4, tempres->pw_dir, SH_PATHBUF+32); 
-	  sl_strlcat (cc4,   _("/.gnupg"),      SH_PATHBUF+32); 
-	  status_stat =  retry_lstat(FIL__, __LINE__, cc4, &lbuf);
-	  if (status_stat == -1)
-	    {
-	      dlog(1, FIL__, __LINE__, 
-		   _("Gnupg directory %s for user %s\ndoes not exist or is not accessible.\nPlease add the directory and put the keyring (pubring.gpg or pubring.kbx) there\nto verify the configuration file.\n"),
-		   cc4, DEFAULT_IDENT);
-	      status_stat = -3;
-	    }
-	}
-      if (status_stat == 0 && lbuf.st_uid != tempres->pw_uid)
-	{
-	  dlog(1, FIL__, __LINE__, 
-	       _("Gnupg directory %s\nis not owned by user %s.\n"), 
-	       cc4, DEFAULT_IDENT);
-	  status_stat = -4;
-	}
-      if (status_stat == 0)
-	{
-	  char   cc4_test[SH_PATHBUF+32];
-
-	  sl_strlcpy(cc4_test, cc4, SH_PATHBUF+32);
-	  sl_strlcat (cc4_test,   _("/pubring.gpg"),      SH_PATHBUF+32); 
-
-	  status_stat =  retry_lstat(FIL__, __LINE__, cc4_test, &lbuf);
-	  if (status_stat == -1)
-	    {
-	      sl_strlcpy(cc4_test, cc4, SH_PATHBUF+32);
-	      sl_strlcat (cc4_test,   _("/pubring.kbx"),      SH_PATHBUF+32);
-
-	      status_stat =  retry_lstat(FIL__, __LINE__, cc4_test, &lbuf);
-	      if (status_stat == -1)
-		{
-		  sl_strlcpy(cc4_test, cc4, SH_PATHBUF+32);
-		  sl_strlcat (cc4_test,   _("/pubring.(gpg|kbx)"),      SH_PATHBUF+32);
-		  
-		  dlog(1, FIL__, __LINE__, 
-		       _("Gnupg public keyring %s for user %s\ndoes not exist or is not accessible.\nPlease add the directory and put the keyring (pubring.gpg or pubring.kbx) there\nto verify the configuration file.\n"),
-		       cc4_test, DEFAULT_IDENT);
-		  status_stat = -5;
-		}
-	    }
-	}
-      if (status_stat == 0 && lbuf.st_uid != tempres->pw_uid)
-	{
-	  dlog(1, FIL__, __LINE__, 
-	       _("Gnupg public keyring %s\nis not owned by user %s.\n"), 
-	       cc4, DEFAULT_IDENT);
-	  status_stat = -6;
-	}
-      if (status_stat != 0)
-	{
-	  sh_error_handle((-1), FIL__, __LINE__, status_stat, MSG_EXIT_ABORT1, 
-			  sh.prg_name);
-	  aud_exit (FIL__, __LINE__, EXIT_FAILURE);
-	}
-      sl_strlcpy (cc4, tempres->pw_dir, SH_PATHBUF+32); 
-      sl_strlcat (cc4,   _("/.gnupg"),      SH_PATHBUF+32); 
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-      SH_FREE(buffer);
-#endif
-    }
-#endif
-
-  argv[0] = path; 
-  argv[1] = cc1;
-  argv[2] = "1";
-  argv[3] = cc2;
-  argv[4] = cc3;
-  argv[5] = cc4;
-  argv[6] = cc5;
-  argv[7] = cc0;
-  argv[8] = NULL;
-
-  retval = sh_sig_popen(argv, source, fd);
-  SL_RETURN((retval), _("sh_gpg_popen")); 
-}
-
-static
-int sh_gpg_check_file_sign(int fd, char * sign_id, char * sign_fp, 
-			   char * homedir, ShSigFile whichfile)
-{
-  struct stat buf;
-  char line[256];
-  sh_gpg_popen_t  source;
-  int have_id = BAD, have_fp = BAD, status = 0;
-  unsigned int n_newsig   = 0;
-  unsigned int n_goodsig  = 0;
-  unsigned int n_validsig = 0;
-  
-#ifdef HAVE_SIG_CHECKSUM
-  SL_TICKET checkfd;
-#endif
-
-  SL_ENTER(_("sh_gpg_check_file_sign"));
-
-  /* check whether GnuPG exists and has the correct checksum
-   */
-  TPT(((0), FIL__, __LINE__, _("msg=<Check signature>\n")));
-  TPT(((0), FIL__, __LINE__, _("msg=<gpg is %s>\n"), DEFAULT_SIG_PATH));
-
-  if (0 != retry_lstat(FIL__, __LINE__, DEFAULT_SIG_PATH, &buf))
-    {
-      char errbuf[SH_ERRBUF_SIZE];
-
-      status = errno;
-      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_ERR_LSTAT,
-		      sh_error_message(status, errbuf, sizeof(errbuf)), DEFAULT_SIG_PATH);
-      SL_RETURN( SH_SIG_BAD, _("sh_gpg_check_file_sign"));
-    }
-
-  if (0 != tf_trust_check (DEFAULT_SIG_PATH, SL_YESPRIV))
-    SL_RETURN( SH_SIG_BAD, _("sh_gpg_check_file_sign"));
-
-#ifdef HAVE_SIG_CHECKSUM
-  checkfd = sl_open_read(FIL__, __LINE__, DEFAULT_SIG_PATH, SL_YESPRIV);
-
-  if (0 != sh_sig_checksum(checkfd, SIG_HASH_REPORTFULL, NULL, NULL))
-    {
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-		      _("Checksum mismatch"), 
-		      _("gpg_check_file_sign"));
-      sl_close(checkfd);
-      SL_RETURN( SH_SIG_BAD, _("sh_gpg_check_file_sign"));
-    }
-  sl_close(checkfd);
-#endif
-
-  TPT(((0), FIL__, __LINE__, _("msg=<Open pipe to check signature>\n")));
-
-  fflush(NULL);
- 
-  source.pipe   = sh_gpg_popen  ( &source, fd, homedir );
-
-  if (NULL == source.pipe)
-    {
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-		      _("Could not open pipe"), 
-		      _("gpg_check_file_sign"));
-      SL_RETURN( SH_SIG_BAD, _("sh_gpg_check_file_sign"));
-    }
-
-  TPT(((0), FIL__, __LINE__, _("msg=<Open pipe success>\n")));
-
- xagain:
-
-  errno = 0;
-
-  while (NULL != fgets(line, sizeof(line), source.pipe))
-    {
-
-      TPT(((0), FIL__, __LINE__, _("msg=<gpg out: %s>\n"), line));
-      if (line[strlen(line)-1] == '\n')
-	line[strlen(line)-1] = ' ';
-      sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-		      line, 
-		      _("gpg_check_file_sign"));
-
-      if (sl_strlen(line) < 12) 
-	continue;
-
-      /* Sun May 27 18:40:05 CEST 2001
-       */
-      if (0 == sl_strncmp(_("BADSIG"),     &line[9], 6) ||
-	  0 == sl_strncmp(_("ERRSIG"),     &line[9], 6) ||
-	  0 == sl_strncmp(_("NO_PUBKEY"),  &line[9], 6) ||
-	  0 == sl_strncmp(_("NODATA"),     &line[9], 6) ||
-	  0 == sl_strncmp(_("ERROR"),      &line[9], 5) ||
-	  0 == sl_strncmp(_("SIGEXPIRED"), &line[9], 6))
-	{
-	  if      (0 == sl_strncmp(_("BADSIG"), &line[9], 6)) {
-	    dlog(1, FIL__, __LINE__, 
-		 _("%s file is signed, but the signature is invalid."),
-		 ((whichfile == SIG_CONF) ? _("Configuration") : _("Database")));
-	  } 
-	  else if (0 == sl_strncmp(_("NO_PUBKEY"), &line[9], 6)) {
-	    dlog(1, FIL__, __LINE__, 
-		 _("%s file is signed, but the public key to verify the signature is not in my keyring %s/.gnupg/pubring.asc."), 
-		 ((whichfile == SIG_CONF) ? _("Configuration") : _("Database")),
-		 homedir);
-	  }
-	  else if (0 == sl_strncmp(_("ERRSIG"), &line[9], 6)) {
-	    dlog(1, FIL__, __LINE__, 
-		 _("%s file is signed, but the public key to verify the signature is not in my keyring %s/.gnupg/pubring.asc."), 
-		 ((whichfile == SIG_CONF) ? _("Configuration") : _("Database")),
-		 homedir);
-	  }
-	  else if (0 == sl_strncmp(_("SIGEXPIRED"), &line[9], 6)) {
-	    dlog(1, FIL__, __LINE__, 
-		 _("%s file is signed, but the public key to verify the signature has expired."), 
-		 ((whichfile == SIG_CONF) ? _("Configuration") : _("Database")));
-	  }
-	  else if (0 == sl_strncmp(_("NODATA"), &line[9], 6)) {
-	    dlog(1, FIL__, __LINE__, 
-		 _("%s file is not signed."), 
-		 ((whichfile == SIG_CONF) ? _("Configuration") : _("Database")));
-	  }
-	  else if (0 == sl_strncmp(_("ERROR"), &line[9], 5)) {
-	    dlog(1, FIL__, __LINE__, 
-		 _("%s file is not correctly signed. An error occured while verifying the signature."), 
-		 ((whichfile == SIG_CONF) ? _("Configuration") : _("Database")));
-	  }
-
-	  have_fp = BAD; have_id = BAD;
-	  break;
-	}
-      if (0 == sl_strncmp(_("GOODSIG"), &line[9], 7))
-	{
-	  ++n_goodsig;
-	  sl_strlcpy (sign_id, &line[25], SH_MINIBUF+1);
-	  if (sign_id)
-	    sign_id[sl_strlen(sign_id)-1] = '\0';  /* remove trailing '"' */
-	  have_id = GOOD;
-	} 
-      else if (0 == sl_strncmp(_("VALIDSIG"), &line[9], 8))
-	{
-	  ++n_validsig;
-	  strncpy (sign_fp, &line[18], 40);
-	  sign_fp[40] = '\0';
-	  have_fp = GOOD;
-	}
-      else if (0 == sl_strncmp(_("NEWSIG"), &line[9], 6))
-	{
-	  ++n_newsig;
-	}
-      
-    }
-
-  if (ferror(source.pipe) && errno == EAGAIN) 
-    {
-      /* sleep 10 ms to avoid starving the gpg child writing to the pipe */
-      retry_msleep(0,10); 
-      clearerr(source.pipe);
-      goto xagain;
-    }
- 
-  if (0 != sh_sig_pclose (&source))
-    {
-	sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-			_("Error on closing process pipe"), 
-			_("gpg_check_file_sign"));
-	have_id = BAD;
-    }
-
-  TPT(((0), FIL__, __LINE__, _("msg=<Close pipe>\n")));
-
-  if (n_goodsig != n_validsig || n_newsig > 1 || n_goodsig > 1)
-    {
-	sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-			_("Too many or invalid signatures"), 
-			_("gpg_check_file_sign"));
-	have_id = BAD;
-    }
-  
-  if (have_id == GOOD)
-    {
-      TPT(((0), FIL__, __LINE__, _("msg=<Got signator ID>\n")));
-    }
-  if (have_fp == GOOD)
-    {
-      TPT(((0), FIL__, __LINE__, _("msg=<Got fingerprint>\n")));
-    }
-
-  if (have_id == GOOD && have_fp == GOOD)
-    SL_RETURN( SH_SIG_OK, _("sh_gpg_check_file_sign"));
-  else
-    {
-      if (have_id == BAD)
-	sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-			_("No good signature"), 
-			_("gpg_check_file_sign"));
-      else
-	sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-			_("No fingerprint for key"), 
-			_("gpg_check_file_sign"));
-      SL_RETURN( SH_SIG_BADSIGN, _("sh_gpg_check_file_sign"));
-    }
-}
-
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && \
-  defined(HAVE_GETPWNAM_R)
-#define USE_GETPWNAM_R 1
-#endif
-
-static
-int sh_gpg_check_signature (SL_TICKET file, ShSigFile what)
-{
-  int status = SH_SIG_BAD;
-  int fd = 0;
-
-  static int smsg = S_FALSE;
-  char  * tmp;
-
-  char  * sig_id;
-  char  * sig_fp;
-
-  char  * homedir = sh.effective.home;
-#if defined(SH_WITH_SERVER)
-  struct passwd * tempres;
-#if defined(USE_GETPWNAM_R)
-  struct passwd    pwd;
-  char           * buffer = SH_ALLOC(SH_PWBUF_SIZE);
-#endif
-#endif
-
-#ifdef USE_FINGERPRINT
-#include "sh_gpg_fp.h"
-#endif
-
-  SL_ENTER(_("sh_gpg_check_sign"));
-
-
-  if (what == SIG_CONF)
-    fd = get_the_fd(file);
-  if (what == SIG_DATA)
-    fd = get_the_fd(file);
-
-
-  if (fd < 0)
-    {
-      TPT(((0), FIL__, __LINE__, _("msg=<GPG_CHECK: FD = %d>\n"), fd));
-      dlog(1, FIL__, __LINE__, 
-	   _("This looks like an unexpected internal error.\n"));
-#if defined(SH_WITH_SERVER) && defined(USE_GETPWNAM_R)
-      SH_FREE(buffer);
-#endif
-      SL_RETURN( (-1), _("sh_gpg_check_sign"));
-    }
-  
-#if defined(SH_WITH_SERVER)
-#if defined(USE_GETPWNAM_R)
-      sh_getpwnam_r(DEFAULT_IDENT, &pwd, buffer, SH_PWBUF_SIZE, &tempres);
-#else
-      tempres = sh_getpwnam(DEFAULT_IDENT);
-#endif
-      if ((tempres != NULL) && (0 == sl_ret_euid()))
-	{
-	  /* privileges not dropped yet*/
-	  homedir = tempres->pw_dir;
-	}
-#endif
-
-  if (what == SIG_CONF)
-    {
-      TPT(((0), FIL__, __LINE__, _("msg=<GPG_CHECK: FD = %d>\n"), fd));
-      status = sh_gpg_check_file_sign(fd, gp.conf_id, gp.conf_fp, homedir, SIG_CONF);
-      TPT(((0), FIL__, __LINE__, _("msg=<CONF SIGUSR: |%s|>\n"), gp.conf_id));
-      TPT(((0), FIL__, __LINE__, _("msg=<CONF SIGFP:  |%s|>\n"), gp.conf_fp));
-      sig_id =  gp.conf_id; sig_fp = gp.conf_fp;
-    }
-
-  if (what == SIG_DATA)
-    {
-      TPT(((0), FIL__, __LINE__, _("msg=<GPG_CHECK: FD = %d>\n"), fd));
-      status = sh_gpg_check_file_sign(fd, gp.data_id, gp.data_fp, homedir, SIG_DATA);
-      TPT(((0), FIL__, __LINE__, _("msg=<DATA SIGUSR: |%s|>\n"), gp.data_id));
-      TPT(((0), FIL__, __LINE__, _("msg=<DATA SIGFP:  |%s|>\n"), gp.data_fp));
-      sig_id =  gp.data_id; sig_fp = gp.data_fp;
-    }
-  
-  if (SH_SIG_OK == status)
-    {
-#ifdef USE_FINGERPRINT
-      if ((sl_strcmp(SH_GPG_FP, sig_fp) == 0))
-	{
-	  int i;
-
-	  for(i = 0; i < (int) sl_strlen(sig_fp); ++i) {
-	      if (gpgfp[i] != sig_fp[i]) {
-		sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0, 
-				MSG_E_GPG_FP, gpgfp, sig_fp);
-		break; }
-	  }
-
-	  if (smsg == S_FALSE) {
-	    tmp  = sh_util_safe_name(sig_id);
-	    sh_sig_fill_startup (__LINE__, sh.prg_name, sh.real.uid,
-				 (sh.flag.hidefile == S_TRUE) ? 
-				 _("(hidden)") : file_path('C', 'R'), 
-				 tmp, 
-				 sig_fp);
-	    SH_FREE(tmp); }
-	  smsg = S_TRUE;
-
-#if defined(SH_WITH_SERVER) && defined(USE_GETPWNAM_R)
-	  SH_FREE(buffer);
-#endif
-	  SL_RETURN(0, _("sh_gpg_check_sign"));
-	}
-      else
-	{
-	  /* fp mismatch */
-	  dlog(1, FIL__, __LINE__, 
-	       _("The fingerprint of the signing key: %s\ndoes not match the compiled-in fingerprint: %s.\nTherefore the signature could not be verified.\n"), 
-	       sig_fp, SH_GPG_FP);
-	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-		      _("Fingerprint mismatch"), _("gpg_check_sign"));
-	  status = SH_SIG_BADSIGN;
-	}
-#else /* ifdef USE_FINGERPRINT */
-      if (smsg == S_FALSE)
-	{
-	  tmp = sh_util_safe_name(sig_id);
-	  sh_sig_fill_startup (__LINE__,
-			       sh.prg_name, sh.real.uid,
-			       (sh.flag.hidefile == S_TRUE) ? 
-			       _("(hidden)") : file_path('C', 'R'), 
-			       tmp,  sig_fp);
-	  SH_FREE(tmp);
-	}
-      smsg = S_TRUE;
-
-#if defined(SH_WITH_SERVER) && defined(USE_GETPWNAM_R)
-      SH_FREE(buffer);
-#endif
-
-      /* status == OK and no fp checking */
-      SL_RETURN(0, _("sh_gpg_check_sign"));
-#endif /* !ifdef USE_FINGERPRINT */
-    }
-
-  if (status != SH_SIG_OK) 
-    {
-      uid_t   e_uid  = sl_ret_euid();
-      char  * e_home = sh.effective.home;
-
-#if defined(SH_WITH_SERVER)
-#if defined(USE_GETPWNAM_R)
-      struct passwd    e_pwd;
-      char          *  e_buffer = SH_ALLOC(SH_PWBUF_SIZE);
-      struct passwd *  e_tempres;
-      sh_getpwnam_r(DEFAULT_IDENT, &e_pwd, e_buffer, SH_PWBUF_SIZE, &e_tempres);
-#else
-      struct passwd * e_tempres = sh_getpwnam(DEFAULT_IDENT);
-#endif
-
-      if ((e_tempres != NULL) && (0 == sl_ret_euid()))   
-	{
-	  /* privileges not dropped yet */
-	  e_uid  = e_tempres->pw_uid;
-	  e_home = e_tempres->pw_dir;
-	}
-#endif
-      dlog(1, FIL__, __LINE__, 
-	   _("The signature of the configuration file or the file signature database\ncould not be verified. Possible reasons are:\n - gpg binary (%s) not found\n - invalid signature\n - the signature key is not in the private keyring of UID %d,\n - there is no keyring in %s/.gnupg, or\n - the file is not signed - did you move /filename.asc to /filename ?\nTo create a signed file, use (remove old signatures before):\n   gpg -a --clearsign --not-dash-escaped FILE\n   mv FILE.asc FILE\n"),
-	   DEFAULT_SIG_PATH,
-	   (int) e_uid, e_home);
-
-#if defined(SH_WITH_SERVER) && defined(USE_GETPWNAM_R)
-      SH_FREE(e_buffer);
-#endif
-    }
-
-  TPT(((0), FIL__, __LINE__, _("msg=<Status = %d>\n"), status));
-
-  SL_RETURN(-1, _("sh_gpg_check_sign")); /* make compiler happy */
-}  
-
-static int sh_gpg_comp(const char * line, const char * cmp)
-{
-  int retval = S_FALSE;
-
-  if (line && line[0] == '-' && line[1] == '-')
-    {
-      char * dup = sh_util_strdup(line);
-      char * tmp = dup + sl_strlen( dup );
-      --tmp;
-      if (*tmp == '\n') { *tmp = '\0'; --tmp; }
-      while( (*tmp == '\t' || *tmp == ' ' || *tmp == '\r' ) && tmp >= dup ) *tmp-- = '\0';
-
-      if (0 == sl_strcmp(dup, cmp))
-	retval = S_TRUE;
-      SH_FREE(dup);
-    }
-  return retval;
-}
-
-static
-int sh_gpg_msg_start(const char * line)
-{
-  static char cmp[SH_MINIBUF];
-  static int  initialized = 0;
-
-  if (initialized == 0) {
-    sl_strlcpy(cmp, _("-----BEGIN PGP SIGNED MESSAGE-----"), sizeof(cmp));
-    initialized = 1;
-  }
-  return sh_gpg_comp(line, cmp);
-}
-
-static
-int sh_gpg_msg_startdata(const char * line)
-{
-  if (line[0] == '\n')
-    return S_TRUE;
-  return S_FALSE;
-}
-
-static
-int sh_gpg_msg_end(const char * line)
-{
-  static char cmp[SH_MINIBUF];
-  static int  initialized = 0;
-
-  if (initialized == 0) {
-    sl_strlcpy(cmp, _("-----BEGIN PGP SIGNATURE-----"), sizeof(cmp));
-    initialized = 1;
-  }
-  return sh_gpg_comp(line, cmp);
-}
-
-static
-int sh_gpg_sig_end(const char * line)
-{
-  static char cmp[SH_MINIBUF];
-  static int  initialized = 0;
-
-  if (initialized == 0) {
-    sl_strlcpy(cmp, _("-----END PGP SIGNATURE-----"), sizeof(cmp));
-    initialized = 1;
-  }
-  return sh_gpg_comp(line, cmp);
-}
-
-static
-SL_TICKET sh_gpg_extract_signed(SL_TICKET fd, extractlevel extract_level)
-{
-  const  int fgets_buf_size = 16384;
-  FILE * fin_cp = NULL;
-  char * buf    = NULL;
-  int    bufc;
-  int    flag_pgp    = S_FALSE;
-  int    flag_nohead = S_FALSE;
-  SL_TICKET fdTmp = (-1);
-  SL_TICKET open_tmp (void);
-
-  /* extract the data and copy to temporary file
-   */
-  fdTmp = open_tmp();
-  if (SL_ISERROR(fdTmp))
-    {
-      dlog(1, FIL__, __LINE__, _("Error opening temporary file.\n")); 
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, 
-		      _("Error opening temporary file."), 
-		      _("sh_gpg_extract_signed"));
-      return -1;
-    }
-
-  fin_cp = fdopen(dup(get_the_fd(fd)), "rb");
-  buf = SH_ALLOC(fgets_buf_size);
-
-  while (NULL != fgets(buf, fgets_buf_size, fin_cp))
-    {
-      bufc = 0; 
-      while (bufc < fgets_buf_size) { 
-	if (buf[bufc] == '\n') { ++bufc; break; }
-	++bufc;
-      }
-
-      if (flag_pgp == S_FALSE && sh_gpg_msg_start(buf) == S_TRUE)
-	{
-	  flag_pgp = S_TRUE;
-	  if (extract_level == SIG_DATASIG)
-	    sl_write(fdTmp, buf, bufc);
-	  continue;
-	}
-      
-      if (flag_pgp == S_TRUE && flag_nohead == S_FALSE)
-	{
-	  /* Header finished */
-	  if (buf[0] == '\n')
-	    {
-	      flag_nohead = S_TRUE;
-	      if (extract_level == SIG_DATASIG)
-		sl_write(fdTmp, buf, 1);
-	      continue;
-	    }
-	  /* copy these headers */
-	  else if (0 == sl_strncmp(buf, _("Hash:"), 5) ||
-		   0 == sl_strncmp(buf, _("NotDashEscaped:"), 15))
-	    {
-	      if (extract_level == SIG_DATASIG)
-		sl_write(fdTmp, buf, bufc);
-	      continue;
-	    }
-	  /* ignore other headers */
-	  else
-	    continue;
-	}
-    
-      if (flag_pgp == S_TRUE && buf[0] == '\n')
-	{
-	  sl_write(fdTmp, buf, 1);
-	}
-      else if (flag_pgp == S_TRUE)
-	{
-	  if (extract_level == SIG_DATASIG) {
-	    sl_write(fdTmp, buf, bufc); 
-	  }
-	  else {
-	    if (sh_gpg_msg_end(buf) == S_TRUE)
-	      break;
-	    else
-	      sl_write(fdTmp, buf, bufc);
-	  }
-	}
-
-      /* This is after the copy has been done. */
-      if (flag_pgp == S_TRUE && sh_gpg_sig_end(buf) == S_TRUE)
-	break;
-    }
-  SH_FREE(buf);
-  sl_fclose(FIL__, __LINE__, fin_cp);
-  sl_rewind (fdTmp);
-
-  return fdTmp;
-}
-#endif
-
-/*********************************************************************
- *
- * Exported functions
- *
- *********************************************************************/
-
-int sh_sig_check_signature (SL_TICKET file, ShSigFile what)
-{
-#if defined(WITH_GPG)
-  return sh_gpg_check_signature (file, what);
-#elif defined(WITH_SIGNIFY)
-  return sh_signify_check_signature (file, what);
-#else
-  return -1;
-#endif
-}
-
-SL_TICKET sh_sig_extract_signed(SL_TICKET fd)
-{
-#if defined(WITH_GPG)
-  return sh_gpg_extract_signed(fd, SIG_DATASIG);
-#elif defined(WITH_SIGNIFY)
-  return sh_signify_extract_signed(fd, SIG_DATASIG);
-#else
-  return -1;
-#endif
-}
-
-SL_TICKET sh_sig_extract_signed_data(SL_TICKET fd)
-{
-#if defined(WITH_GPG)
-  return sh_gpg_extract_signed(fd, SIG_DATAONLY);
-#elif defined(WITH_SIGNIFY)
-  return sh_signify_extract_signed(fd, SIG_DATAONLY);
-#else
-  return -1;
-#endif
-}
-
-int sh_sig_msg_start(const char * line)
-{
-#if defined(WITH_GPG)
-  return sh_gpg_msg_start(line);
-#elif defined(WITH_SIGNIFY)
-  return sh_signify_msg_start(line);
-#else
-  return -1;
-#endif
-}
-
-int sh_sig_msg_startdata(const char * line)
-{
-#if defined(WITH_GPG)
-  return sh_gpg_msg_startdata(line);
-#elif defined(WITH_SIGNIFY)
-  return sh_signify_msg_startdata(line);
-#else
-  return -1;
-#endif
-}
-
-int sh_sig_msg_end(const char * line)
-{
-#if defined(WITH_GPG)
-  return sh_gpg_msg_end(line);
-#elif defined(WITH_SIGNIFY)
-  return sh_signify_msg_end(line);
-#else
-  return -1;
-#endif
-}
-
-int sh_sig_data_end(const char * line)
-{
-#if defined(WITH_GPG)
-  return sh_gpg_sig_end(line);
-#elif defined(WITH_SIGNIFY)
-  return sh_signify_data_end(line);
-#else
-  return -1;
-#endif
-}
-
-void sh_sig_log_startup (void)
-{
-  if (startInfo.program != NULL)
-    {
-      sh_error_handle ((-1), FIL__, startInfo.line, 0, MSG_START_GH,
-		       startInfo.program, startInfo.uid,
-		       startInfo.path,
-		       startInfo.key_uid, startInfo.key_id);
-    }
-  return;
-}
-
-/* #ifdef WITH_SIG */
-#endif
-
-
-
-
-
-
-
-
Index: trunk/src/sh_socket.c
===================================================================
--- trunk/src/sh_socket.c	(revision 591)
+++ trunk/src/sh_socket.c	(revision 1)
@@ -24,17 +24,8 @@
  */
 
-#if defined(SH_WITH_SERVER) && defined(__linux__)
-#define _GNU_SOURCE
-#endif
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <stddef.h>
 #include <string.h>
-
-#if HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
-#include <time.h>
 
 #include "samhain.h"
@@ -43,7 +34,4 @@
 #include "sh_unix.h"
 #include "sh_calls.h"
-#include "sh_guid.h"
-#include "sh_fifo.h"
-#include "sh_utils.h"
 
 #undef  FIL__
@@ -54,145 +42,4 @@
 #include <signal.h>
 
-typedef struct delta_tofetch {
-  char            uuid[SH_UUID_BUF];
-  time_t          last_time;
-  unsigned int    count;
-} SH_DELTA_DB;
-
-static SH_DELTA_DB * parse_entry(SH_DELTA_DB * db, const char * str)
-{
-  long last_time;
-  unsigned int count;
-  char buf[SH_UUID_BUF];
-  int res = sscanf(str, _("%u:%ld:%36s"), &count, &last_time, buf);
-  if (res == 3)
-    {
-      db->count = count;
-      db->last_time  = (time_t) last_time;
-      sl_strlcpy(db->uuid, buf, SH_UUID_BUF);
-      return db;
-    }
-  return NULL;
-}
-
-static char * unparse_entry(const SH_DELTA_DB * db, char * str, size_t len)
-{
-  int nbytes = sl_snprintf(str, len, _("%u:%ld:%s"), 
-			   db->count, (long) db->last_time, db->uuid);
-  if (nbytes < 0 || nbytes >= (int) len)
-    return NULL;
-  return str;
-}
-
-static SH_FIFO xfifo = SH_FIFO_INITIALIZER;
-
-int sh_socket_store_uuid(const char * cmd)
-{
-  char * p = sh_util_strdup(cmd);
-  char * q = strchr(cmd, ':');
-  char   entry[SH_BUFSIZE];
-  SH_DELTA_DB db;
-
-  if (!q) { SH_FREE(p); return -1; }
-
-  ++q;
-
-  if (0 != sh_uuid_check(q)) { SH_FREE(p); return -1; }
-
-  db.count = 0;
-  db.last_time = (time_t) 0;
-  sl_strlcpy(db.uuid, q, SH_UUID_BUF);
-  SH_FREE(p);
-
-  if (NULL != unparse_entry(&db, entry, sizeof(entry)))
-    {
-      sh_fifo_push(&xfifo, entry);
-      return 0;
-    }
-  return -1;
-}
-
-static unsigned int try_interval = 60;
-static unsigned int try_max = 2;
-
-int set_delta_retry_interval(const char * str)
-{
-  long val = strtol (str, (char **)NULL, 10);
-
-  if (val < 0 || val > INT_MAX)
-    return -1;
-  try_interval = (unsigned int) val;
-  return 0;
-}
-int set_delta_retry_count(const char * str)
-{
-  long val = strtol (str, (char **)NULL, 10);
-
-  if (val < 0 || val > INT_MAX)
-    return -1;
-  try_max = (unsigned int) val;
-  return 0;
-}
-
-char * sh_socket_get_uuid(int * errflag, unsigned int * count, time_t * last)
-{
-  char * entry = sh_fifo_pop(&xfifo);
-  char * uuid = NULL;
-
-  if (entry)
-    {
-      SH_DELTA_DB db;
-      time_t      now;
-      
-      if (NULL == parse_entry(&db, entry))
-	{
-	  SH_FREE(entry);
-	  sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-			  _("Bad entry in fifo"), 
-			  _("sh_socket_get_uuid"));
-	  *errflag = -1;
-	  return NULL;
-	}
-
-      now = time(NULL);
-
-      if ( (db.count > 0) && ((unsigned long)(now - db.last_time) < try_interval) )
-	{
-	  sh_fifo_push_tail(&xfifo, entry);
-	  SH_FREE(entry);
-	  *errflag = -1;
-	  return NULL;
-	}
-
-      SH_FREE(entry);
-      uuid   = sh_util_strdup(db.uuid);
-      *count = db.count;
-      *last  = db.last_time;
-    }
-
-  *errflag = 0;
-  return uuid;
-}
-
-int sh_socket_return_uuid (const char * uuid, unsigned int count, time_t last)
-{
-  (void) last;
-
-  if (count < try_max)
-    {
-      char   entry[SH_BUFSIZE];
-      SH_DELTA_DB db;
-      time_t now = time(NULL);
-
-      db.count     = count + 1;
-      db.last_time = now;
-      sl_strlcpy(db.uuid, uuid, SH_UUID_BUF);
-
-      if (NULL != unparse_entry(&db, entry, sizeof(entry)))
-	return sh_fifo_push_tail(&xfifo, entry); /* >0 for success */
-    }
-  return -1;
-}
-
 void sh_socket_server_cmd(const char * srvcmd)
 {
@@ -203,6 +50,6 @@
       SL_RET0(_("sh_socket_server_cmd"));
     }
-
-  if (0 == strncmp(srvcmd, _("STOP"), 4))
+  if ((srvcmd[0] == 'S') || (srvcmd[1] == 'T') || 
+      (srvcmd[2] == 'O') || (srvcmd[3] == 'P'))
     {
       TPT((0, FIL__, __LINE__, _("msg=<stop command from server>\n")));
@@ -210,10 +57,10 @@
       raise(SIGQUIT);
 #else
-      sig_terminate       = 1;
-      ++sig_raised;
+      TPT((0, FIL__, __LINE__, _("msg=<sigquit not defined>\n")));
 #endif
     } 
-
-  else if (0 == strncmp(srvcmd, _("RELOAD"), 6))
+  else if ((srvcmd[0] == 'R') || (srvcmd[1] == 'E') || 
+	   (srvcmd[2] == 'L') || (srvcmd[3] == 'O') ||
+	   (srvcmd[4] == 'A') || (srvcmd[5] == 'D'))
     {
       TPT((0, FIL__, __LINE__, _("msg=<reload command from server>\n")));
@@ -221,44 +68,10 @@
       raise(SIGHUP);
 #else
-      sig_config_read_again = 1;
-      ++sig_raised;
-#endif
-    }
-
-  else if (0 == strncmp(srvcmd, _("DELTA:"), 6))
-    {
-      TPT((0, FIL__, __LINE__, _("msg=<delta load command from server>\n")));
-
-      if (sh_socket_store_uuid(srvcmd) == 0)
-	{
-	  ++sh_load_delta_flag;
-	  ++sig_raised;
-	}
-    }
-
-  else if (0 == strncmp(srvcmd, _("SCAN"), 4))
-    {
-      TPT((0, FIL__, __LINE__, _("msg=<scan command from server>\n")));
-      if (sh.flag.isdaemon == S_TRUE) 
-	{ 
-#ifdef SIGTTOU
-	  raise(SIGTTOU);
-#else
-	  sig_force_check = 1;
-	  ++sig_raised;
-#endif
-	} 
-      else 
-	{
-	  sig_force_check = 1;
-	  ++sig_raised;
-	}
-    }
-
-  /* Unknown command 
-   */
+      TPT((0, FIL__, __LINE__, _("msg=<sighup not defined>\n")));
+#endif
+    }
   else
     {
-      sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN,
+      sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
 		      srvcmd, 
 		      _("sh_socket_server_cmd"));
@@ -266,5 +79,7 @@
   SL_RET0(_("sh_socket_server_cmd"));
 }
-#endif  /* #if defined (SH_WITH_CLIENT) */
+/* #if defined (SH_WITH_CLIENT)
+ */
+#endif
 
 #if defined(SH_WITH_SERVER)
@@ -296,5 +111,6 @@
 int    pf_unix_fd  = -1;
 static char * sh_sockname = NULL;
-static char   sh_sockpass_real[SOCKPASS_MAX+1];
+/* static char   sh_sockpass[29]; */
+static char   sh_sockpass_real[15];
 
 struct socket_cmd {
@@ -325,8 +141,5 @@
 #include "sh_utils.h"
 
-/* The reload list stores information about
- * reloads confirmed by clients (startup and/or
- * runtime cinfiguration reloaded).
- */
+
 struct reload_cmd {
   char          clt[SH_MAXMSGLEN];
@@ -344,7 +157,4 @@
       if (0 == sl_strcmp(new->clt, clt))
 	{
-#ifdef SH_DEBUG_SOCKET
-	  fprintf(stderr, "add2reload: time reset for %s\n", clt);
-#endif
 	  sl_strlcpy (new->clt, clt, SH_MAXMSGLEN);
 	  new->cti = time(NULL);
@@ -355,7 +165,4 @@
 
   new = SH_ALLOC(sizeof(struct reload_cmd));
-#ifdef SH_DEBUG_SOCKET
-  fprintf(stderr, "add2reload: time set for %s\n", clt);
-#endif
   sl_strlcpy (new->clt, clt, SH_MAXMSGLEN);
   new->cti = time(NULL);
@@ -372,5 +179,5 @@
 static void sh_socket_add2list (struct socket_cmd * in);
 
-static void sh_socket_probe4reload (void)
+static void sh_socket_probe4reload ()
 {
   struct reload_cmd  * new;
@@ -390,6 +197,4 @@
       if (item->status_now != CLT_INACTIVE)
 	{
-	  int flag = 0;
-
 	  file = get_client_conf_file (item->hostname, &dummy);
 
@@ -401,6 +206,4 @@
 		  if (0 == sl_strcmp(new->clt, item->hostname))
 		    {
-		      flag = 1; /* Client is in list already */
-
 		      if (buf.st_mtime > new->cti)
 			{
@@ -414,39 +217,14 @@
 		  new = new->next;
 		}
-
-	      if (flag == 0)
-		{
-		  /* client is active, but start message has been missed; reload 
-		   */
-		  sl_strlcpy(cmd.cmd, _("RELOAD"),    SH_MAXMSGLEN);
-		  sl_strlcpy(cmd.clt, item->hostname, SH_MAXMSGLEN);
-		  sh_socket_add2list (&cmd);
-
-		  /* Add the client to the reload list and set
-		   * time to 0, since we don't know the startup time.
-		   */
-		  sh_socket_add2reload (item->hostname);
-		  new = reloadlist;
-		  while (new)
-		    {
-		      if (0 == sl_strcmp(new->clt, item->hostname))
-			{
-			  new->cti = 0;
-			  break;
-			}
-		      new = new->next;
-		    }
-		}
-	    } /* if stat(file).. */
-	} /* if !CLT_INACTIVE */
-    } /* loop over clients */
+	    }
+	}
+    }
   return;
 }
 
-char * sh_get_sockpass (void)
-{
-  size_t j = 0;
-
-  while (skey->sh_sockpass[2*j] != '\0' && j < sizeof(sh_sockpass_real))
+char * sh_get_sockpass ()
+{
+  int j = 0;
+  while (skey->sh_sockpass[2*j] != '\0')
     {
       sh_sockpass_real[j] = skey->sh_sockpass[2*j];
@@ -454,9 +232,8 @@
     }
   sh_sockpass_real[j] = '\0';
-
   return sh_sockpass_real;
 }
 
-void sh_set_sockpass (void)
+void sh_set_sockpass ()
 {
   int j;
@@ -467,5 +244,5 @@
 }
 
-int sh_socket_use (const char * c)
+int sh_socket_use (char * c)
 {
   return sh_util_flagval(c, &sh_socket_flaguse);
@@ -509,7 +286,8 @@
 
 #define NEED_PASSWORD_AUTH
-#endif
-
-int sh_socket_uid (const char * c)
+
+#endif
+
+int sh_socket_uid (char * c)
 {
   uid_t val = (uid_t) strtol (c, (char **)NULL, 10);
@@ -523,5 +301,5 @@
 }
 
-int sh_socket_password (const char * c)
+int sh_socket_password (char * c)
 {
 #if defined(NEED_PASSWORD_AUTH)
@@ -531,8 +309,8 @@
 
   i = sl_strlen(c);
-  if (i > SOCKPASS_MAX) {
+  if (i > 14) {
     return -1;
   }
-  for (j = 0; j < (2*SOCKPASS_MAX+1); ++j)
+  for (j = 0; j < 29; ++j)
     {
       skey->sh_sockpass[j] = '\0';
@@ -563,5 +341,4 @@
 #endif
   struct stat buf;
-  char errbuf[SH_ERRBUF_SIZE];
   
   SL_ENTER(_("sh_socket_open_int"));
@@ -575,5 +352,5 @@
     {
       size = sl_strlen(DEFAULT_PIDDIR) + 1 + sl_strlen(SH_INSTALL_NAME) + 6;
-      sh_sockname = SH_ALLOC(size); /* compile-time constant */
+      sh_sockname = SH_ALLOC(size);
       sl_strlcpy(sh_sockname, DEFAULT_PIDDIR, size);
       sl_strlcat(sh_sockname, "/", size);
@@ -582,15 +359,9 @@
     }
 
-  if (0 != sh_unix_check_piddir (sh_sockname))
-    {
-      SH_FREE(sh_sockname);
-      SL_RETURN((-1),_("sh_socket_open_int"));
-    }
-
   pf_unix_fd = socket (PF_UNIX, SOCK_STREAM, 0);
   if ((pf_unix_fd) < 0)
     {
       sh_error_handle ((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
-		       sh_error_message (errno, errbuf, sizeof(errbuf)), 
+		       sh_error_message (errno), 
 		       _("sh_socket_open_int: socket"));
       SL_RETURN( (-1), _("sh_socket_open_int"));
@@ -599,5 +370,5 @@
   if (sizeof(name.sun_path) < (1 + sl_strlen(sh_sockname)))
     {
-      sl_close_fd(FIL__, __LINE__, pf_unix_fd); pf_unix_fd = -1;
+      close(pf_unix_fd); pf_unix_fd = -1;
       sh_error_handle ((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
 		       _("PID dir path too long"), 
@@ -607,5 +378,5 @@
 
   name.sun_family = AF_FILE;
-  sl_strlcpy (name.sun_path, sh_sockname, sizeof(name.sun_path));
+  strcpy (name.sun_path, sh_sockname);
 
   size = (offsetof (struct sockaddr_un, sun_path)
@@ -621,5 +392,5 @@
       if (sh_socket_remove() < 0) 
 	{
-	  sl_close_fd(FIL__, __LINE__, pf_unix_fd); pf_unix_fd = -1;
+	  close(pf_unix_fd); pf_unix_fd = -1;
 	  sh_error_handle ((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
 			   _("Unlink of socket failed, maybe path not trusted"), 
@@ -631,7 +402,7 @@
   if (bind ((pf_unix_fd), (struct sockaddr *) &name, size) < 0)
     {
-      sl_close_fd(FIL__, __LINE__, pf_unix_fd); pf_unix_fd = -1;
+      close(pf_unix_fd); pf_unix_fd = -1;
       sh_error_handle ((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
-		       sh_error_message (errno, errbuf, sizeof(errbuf)), 
+		       sh_error_message (errno), 
 		       _("sh_socket_open_int: bind"));
       SL_RETURN( (-1), _("sh_socket_open_int"));
@@ -642,7 +413,7 @@
 		      &optval, sizeof(optval)))
     {
-      sl_close_fd(FIL__, __LINE__, pf_unix_fd); pf_unix_fd = -1;
+      close(pf_unix_fd); pf_unix_fd = -1;
       sh_error_handle ((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
-		       sh_error_message (errno, errbuf, sizeof(errbuf)), 
+		       sh_error_message (errno), 
 		       _("sh_socket_open_int: setsockopt"));
       SL_RETURN( (-1), _("sh_socket_open_int"));
@@ -653,7 +424,7 @@
   if (flags < 0)
     {
-      sl_close_fd(FIL__, __LINE__, pf_unix_fd); pf_unix_fd = -1;
+      close(pf_unix_fd); pf_unix_fd = -1;
       sh_error_handle ((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
-		       sh_error_message (errno, errbuf, sizeof(errbuf)), 
+		       sh_error_message (errno), 
 		       _("sh_socket_open_int: fcntl1"));
       SL_RETURN( (-1), _("sh_socket_open_int"));
@@ -663,7 +434,7 @@
   if (flags < 0)
     {
-      sl_close_fd(FIL__, __LINE__, pf_unix_fd); pf_unix_fd = -1;
+      close(pf_unix_fd); pf_unix_fd = -1;
       sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
-		      sh_error_message (errno, errbuf, sizeof(errbuf)), 
+		      sh_error_message (errno), 
 		      _("sh_socket_open_int: fcntl2"));
       SL_RETURN( (-1), _("sh_socket_open_int"));
@@ -672,7 +443,7 @@
   if (0 != listen(pf_unix_fd, 5))
     {
-      sl_close_fd(FIL__, __LINE__, pf_unix_fd); pf_unix_fd = -1;
+      close(pf_unix_fd); pf_unix_fd = -1;
       sh_error_handle ((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
-		       sh_error_message (errno, errbuf, sizeof(errbuf)), 
+		       sh_error_message (errno), 
 		       _("sh_socket_open_int: listen"));
       SL_RETURN( (-1), _("sh_socket_open_int"));
@@ -680,5 +451,18 @@
   SL_RETURN( (0), _("sh_socket_open_int"));
 }
-
+/* #if !defined(HAVE_CMSGCRED) || !defined(SO_PEERCRED) */
+/* #endif */
+
+/*
+#if !defined(HAVE_GETPEEREID) && !defined(SO_PEERCRED) && !defined(HAVE_STRUCT_CMSGCRED) && !defined(HAVE_STRUCT_FCRED) && !(defined(HAVE_STRUCT_SOCKCRED) && defined(LOCAL_CREDS))
+static 
+int sh_socket_read (struct socket_cmd * srvcmd)
+{
+  srvcmd->cmd[0] = '\0';
+  srvcmd->clt[0] = '\0';
+  return 0;
+}
+#else
+*/
 
 /*
@@ -709,20 +493,124 @@
  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  */
-
-static int receive_message(int talkfd, struct msghdr * msg, size_t message_size)
-{
-  unsigned int retry = 0;
+static 
+int sh_socket_read (struct socket_cmd * srvcmd)
+{
+  struct socket_cmd * list_cmd;
+  char message[SH_MAXMSG];
+  struct sockaddr_un name;
+  int size;
   int nbytes;
-  char * message = msg->msg_iov->iov_base;
-  char errbuf[SH_ERRBUF_SIZE];
+  int talkfd;
+  int retry = 0;
+
+  char * cmd = NULL;
+  char * clt = NULL;
+
+  int  client_uid = -1;
+
+
+  struct msghdr msg;
+  struct iovec iov;
+
+#if defined(NEED_PASSWORD_AUTH)
+  char * eopw = NULL;
+  char * goodpassword = NULL;
+#endif
+
+#if defined(HAVE_GETPEEREID)
+  uid_t peer_uid;
+  gid_t peer_gid;
+#elif defined(SO_PEERCRED) 
+  struct ucred cr;
+  int          cl = sizeof(cr);
+
+#elif defined(HAVE_STRUCT_CMSGCRED) || defined(HAVE_STRUCT_FCRED) || (defined(HAVE_STRUCT_SOCKCRED) && defined(LOCAL_CREDS))
+
+#ifdef HAVE_STRUCT_CMSGCRED
+  typedef struct cmsgcred Cred;
+#define CRED_UID cmcred_uid 
+
+#elif HAVE_STRUCT_FCRED
+  typedef struct fcred Cred;
+#define CRED_UID fc_uid 
+
+#elif HAVE_STRUCT_SOCKCRED
+  typedef struct sockcred Cred;
+#define CRED_UID sc_uid 
+
+#endif
+  Cred       *cred;
+
+  /* Compute size without padding */
+  char   cmsgmem[ALIGN(sizeof(struct cmsghdr)) + ALIGN(sizeof(Cred))];   
+  /* for NetBSD */
+
+  /* Point to start of first structure */
+  struct cmsghdr *cmsg = (struct cmsghdr *) cmsgmem;
+#endif
+
+  if (pf_unix_fd  < 0)
+    {
+      return 0;
+    }
+
+  iov.iov_base = (char *) &message;
+  iov.iov_len  = SH_MAXMSG;
+
+  memset (&msg, 0, sizeof (msg));
+  msg.msg_iov = &iov;
+  msg.msg_iovlen = 1;
+
+#if !defined(SO_PEERCRED) && !defined(HAVE_GETPEEREID)
+#if defined(HAVE_STRUCT_CMSGCRED) || defined(HAVE_STRUCT_FCRED) || (defined(HAVE_STRUCT_SOCKCRED) && defined(LOCAL_CREDS))
+  msg.msg_control = (char *) cmsg;
+  msg.msg_controllen = sizeof (cmsgmem);
+  memset (cmsg, 0, sizeof (cmsgmem));
+#endif
+#endif
+
+  /* the socket is non-blocking 
+   * 'name' is the address of the sender socket
+   */
+  size = sizeof (name);
+  talkfd = retry_accept(FIL__, __LINE__, 
+			pf_unix_fd, (struct sockaddr *) & name, &size);
+  if ((talkfd < 0) && (errno == EAGAIN))
+    {
+      return 0;
+    }
+  else if (talkfd < 0)
+    {
+      sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
+		      sh_error_message (errno), 
+		      _("sh_socket_read: accept"));
+      return -1;
+    }
+
+
+#if defined(LOCAL_CREDS) && !defined(SO_PEERCRED) && !defined(HAVE_GETPEEREID)
+  /* Set the socket to receive credentials on the next message 
+   */
+  {
+    int on = 1;
+    if (setsockopt (talkfd, 0, LOCAL_CREDS, &on, sizeof (on)) < 0)
+      {
+	sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
+			sh_error_message (errno), 
+			_("sh_socket_read: setsockopt"));
+	close(talkfd);
+	return -1;
+      }
+  }
+#endif
 
   do {
-    nbytes = recvmsg (talkfd, msg, 0);
+    nbytes = recvmsg (talkfd, &msg, 0);
     if ((nbytes < 0) && (errno != EAGAIN))
       {
 	sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
-			sh_error_message (errno, errbuf, sizeof(errbuf)),
+			sh_error_message (errno),
 			_("sh_socket_read: recvmsg"));
-	sl_close_fd(FIL__, __LINE__, talkfd);	
+	close(talkfd);	
 	return -1;
       }
@@ -730,11 +618,15 @@
       {
 	++retry;
-	retry_msleep(0, 10);
+	retry_msleep(0, 1);
       }
   } while ((nbytes < 0) && (retry < 3));
 
-  /* msg.msg_iov.iov_base, filled by recvmsg
-   */
-  message[message_size-1] = '\0';
+#ifdef SH_DEBUG_SOCKET
+  fprintf(stderr, "%d bytes received\n", nbytes);
+#endif
+
+  /* nbytes = recv (talkfd, message, SH_MAXMSG, 0); */
+
+  message[SH_MAXMSG-1] = '\0';
 
   if (nbytes < 0)
@@ -743,81 +635,56 @@
 	{
 	  /* no data */
-	  sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, errno, MSG_E_SUBGEN,
-			  sh_error_message (errno, errbuf, sizeof(errbuf)), 
-			  _("sh_socket_read: recvfrom"));
-	  sl_close_fd(FIL__, __LINE__, talkfd);
+	  close(talkfd);
 	  return 0;
 	}
       sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
-		      sh_error_message (errno, errbuf, sizeof(errbuf)), 
+		      sh_error_message (errno), 
 		      _("sh_socket_read: recvfrom"));
-      sl_close_fd(FIL__, __LINE__, talkfd);
+      close(talkfd);
       return -1;
     }
-  return 0;
-}
 
 #if defined(HAVE_GETPEEREID)
-
-static int get_peer_uid(int talkfd)
-{
-  uid_t peer_uid;
-  gid_t peer_gid;
-  char errbuf[SH_ERRBUF_SIZE];
-
   if (0 != getpeereid(talkfd, &peer_uid, &peer_gid))
     {
       sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
-		      sh_error_message (errno, errbuf, sizeof(errbuf)), 
+		      sh_error_message (errno), 
 		      _("sh_socket_read: getpeereid"));
-      sl_close_fd(FIL__, __LINE__, talkfd);
+      close(talkfd);
       return -1;
     }
-  return peer_uid;
-}
-
-#elif defined(SO_PEERCRED) 
-
-static int get_peer_uid(int talkfd)
-{
-  char errbuf[SH_ERRBUF_SIZE];
-  struct ucred cr;
-#ifdef HAVE_SOCKLEN_T
-  socklen_t cl = sizeof(cr);
-#else
-  int       cl = sizeof(cr);
-#endif 
-
+  client_uid = peer_uid;
+  cmd = message;
+#elif defined(SO_PEERCRED)
   if (0 != getsockopt(talkfd, SOL_SOCKET, SO_PEERCRED, &cr, &cl))
     {
       sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
-		      sh_error_message (errno, errbuf, sizeof(errbuf)), 
+		      sh_error_message (errno), 
 		      _("sh_socket_read: getsockopt"));
-      sl_close_fd(FIL__, __LINE__, talkfd);
+      close(talkfd);
       return -1;
     }
-  return cr.uid;
-}
-
-#endif
-
-#if defined(NEED_PASSWORD_AUTH)
-char * check_password(char * message, size_t msglen, int * client_uid, int talkfd)
-{
-  char * cmd = NULL;
-  char * eopw = NULL;
-  char * goodpassword = NULL;
-
+  client_uid = cr.uid;
+  cmd = message;
+#elif defined(HAVE_STRUCT_CMSGCRED) || defined(HAVE_STRUCT_FCRED) || (defined(HAVE_STRUCT_SOCKCRED) && defined(LOCAL_CREDS))
+  if (cmsg->cmsg_len < sizeof (cmsgmem) || cmsg->cmsg_type != SCM_CREDS)
+    {
+      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
+		      _("Message from recvmsg() was not SCM_CREDS"), 
+		      _("sh_socket_read"));
+      close(talkfd);
+      return -1;
+    }
+  cred = (Cred *) CMSG_DATA (cmsg);
+  client_uid = cred->CRED_UID;
+  cmd = message;
+#elif defined(NEED_PASSWORD_AUTH)
   goodpassword = sh_get_sockpass();
   eopw = strchr(message, '@');
   if (eopw) 
     *eopw = '\0';
-  /*
-   * message is null-terminated and >> goodpassword
-   */
-  if (0 == strcmp(goodpassword, message) &&
-      strlen(goodpassword) < (msglen/2))
-    {
-      *client_uid = sh_socket_flaguid;
+  if (0 == strcmp(goodpassword, message))
+    {
+      client_uid = sh_socket_flaguid;
       cmd = &message[strlen(goodpassword)+1];
       sh_set_sockpass();
@@ -829,79 +696,18 @@
 		      _("sh_socket_read"));
       sh_set_sockpass();
-      sl_close_fd(FIL__, __LINE__, talkfd);
-      return NULL;
-    }
-  return cmd;
-}
-#endif
-
-static int list_all (int talkfd, char * cmd);
-static int process_message(int talkfd, char * cmd, struct socket_cmd * srvcmd);
-
-static 
-int sh_socket_read (struct socket_cmd * srvcmd)
-{
-  char message[SH_MAXMSG];
-  struct sockaddr_un name;
-  ACCEPT_TYPE_ARG3 size = sizeof(name);
-  int talkfd;
-  char * cmd = NULL;
-  int  client_uid = -1;
-  char errbuf[SH_ERRBUF_SIZE];
-  struct msghdr msg;
-  struct iovec iov;
-  int status;
-
-  if (pf_unix_fd  < 0)
-    return 0;
-
-  iov.iov_base = (char *) &message;
-  iov.iov_len  = sizeof(message);
-
-  memset (&msg, 0, sizeof (msg));
-  msg.msg_iov = &iov;
-  msg.msg_iovlen = 1;
-
-  /* the socket is non-blocking 
-   * 'name' is the address of the sender socket
-   */
-  do {
-    talkfd = accept(pf_unix_fd, (struct sockaddr *) &name, &size);
-  } while (talkfd < 0 && errno == EINTR);
-
-  if ((talkfd < 0) && (errno == EAGAIN))
-    {
-      return 0;
-    }
-  else if (talkfd < 0)
-    {
-      sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
-		      sh_error_message (errno, errbuf, sizeof(errbuf)), 
-		      _("sh_socket_read: accept"));
+      close(talkfd);
       return -1;
     }
-
-  if (receive_message(talkfd, &msg, sizeof(message)) < 0)
-    return -1;
-
-  /* Authenticate request by peer uid or password.
-   */
-#if defined(HAVE_GETPEEREID)
-  client_uid = get_peer_uid(talkfd);
-  cmd = message;
-
-#elif defined(SO_PEERCRED)
-  client_uid = get_peer_uid(talkfd);
-  cmd = message;
-
-#elif defined(NEED_PASSWORD_AUTH)
-  cmd = check_password(message, sizeof(message), &client_uid, talkfd);
-
 #else
   sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
 		  _("Socket credentials not supported on this OS"), 
 		  _("sh_socket_read"));
-  sl_close_fd(FIL__, __LINE__, talkfd);
+  close(talkfd);
   return -1;
+#endif
+
+#ifdef SH_DEBUG_SOCKET
+  fprintf(stderr, "Peer uid=%d, required=%d\n",
+	  client_uid, sh_socket_flaguid);
 #endif
 
@@ -911,85 +717,19 @@
 		      _("client does not have required uid"), 
 		      _("sh_socket_read: getsockopt"));
-      sl_close_fd(FIL__, __LINE__, talkfd);
+      close(talkfd);
       return -1;
     }
 
-  status = process_message(talkfd, cmd, srvcmd);
-
-  sl_close_fd(FIL__, __LINE__, talkfd);
-  return status;
-}
-
-static int check_valid_command(const char * str)
-{
-  unsigned int i = 0;
-  char * commands[] = { N_("DELTA"),  N_("RELOAD"),  N_("STOP"), N_("SCAN"),
-			N_("CANCEL"), N_("LISTALL"), N_("LIST"), N_("PROBE"), NULL };
-
-  while (commands[i])
-    {
-      if (0 == strcmp(_(commands[i]), str))
-	{
-	  return 0;
-	}
-      ++i;
-    }
-  return -1;
-}
-
-static int send_reply (int fd, char * msg)
-{
-  int nbytes = send (fd, msg, strlen(msg) + 1, 0);
-
-  if (nbytes < 0)
-    {
-      char errbuf[SH_ERRBUF_SIZE];
-      sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
-		      sh_error_message (errno, errbuf, sizeof(errbuf)), 
-		      _("send_reply"));
-      return -1;
-    }
-
-  return nbytes;
-}
-
-static int process_message(int talkfd, char * cmd, struct socket_cmd * srvcmd)
-{
-  int nbytes;
-  char error_type[SH_ERRBUF_SIZE] = { '\0' };
-  char * clt  = (cmd) ? strchr(cmd, ':') : NULL;
-
-  if (clt && 0 == strncmp(cmd, _("DELTA:"), 6))
-    {
-      /* DELTA:uuid:hostname 
-       */
-      char * uuid = clt;
-      
-      *uuid = '\0'; ++uuid;
-      clt = strchr(uuid, ':');
-      if (clt) { *clt = '\0'; ++clt; }
-      
-      if (sh_uuid_check(uuid) < 0)
-	{
-	  sl_strlcpy(error_type, _("!E:uuid-format:"), sizeof(error_type));
-	  sl_strlcat(error_type, uuid, sizeof(error_type));
-	  clt = NULL;
-	}
-      
-      --uuid; *uuid = ':';
-    }
-  else if (clt && *clt == ':')
-    { 
-      *clt = '\0'; ++clt; 
-      if (check_valid_command(cmd) < 0)
-	{
-	  sl_strlcpy(error_type, _("!E:cmd-invalid:"), sizeof(error_type));
-	  sl_strlcat(error_type, cmd, sizeof(error_type));
-	  clt = NULL;
-	}
-    }
-    
+
+  /* Give a diagnostic message. 
+   */
+#ifdef SH_DEBUG_SOCKET
+  fprintf (stderr, "Server: got message: %s\n", cmd);
+#endif
+
+  clt = strchr(cmd, ':');
   if (clt != NULL) 
     {
+      *clt = '\0'; ++clt;
       if (sl_strlen(cmd) >= SH_MAXMSGLEN)
 	{
@@ -997,6 +737,5 @@
 			  _("Bad message format: command too long"), 
 			  _("sh_socket_read"));
-	  sl_strlcpy(error_type, _("!E:cmd-toolong"), sizeof(error_type));
-	  send_reply(talkfd, error_type);
+	  close(talkfd);
 	  return -1;
 	}
@@ -1006,18 +745,19 @@
 			  _("Bad message format: hostname too long"), 
 			  _("sh_socket_read"));
-	  sl_strlcpy(error_type, _("!E:hostname-toolong"), sizeof(error_type));
-	  send_reply(talkfd, error_type);
+	  close(talkfd);
 	  return -1;
 	}
-
-      if (0 == strncmp(cmd, _("LIST"), 4))
-	return list_all(talkfd, cmd);
-      else if (0 == strncmp(cmd, _("PROBE"), 4))
+      if (cmd[0] == 'L' && cmd[1] == 'I' &&
+	  cmd[2] == 'S' && cmd[3] == 'T')
+	{
+	  goto list_all;
+	}
+      else if (cmd[0] == 'P' && cmd[1] == 'R' &&
+	  cmd[2] == 'O' && cmd[3] == 'B' && cmd[4] == 'E')
 	{
 	  sh_socket_probe4reload();
-	  sl_strlcpy(cmd, _("LIST"), 5);
-	  return list_all(talkfd, cmd);
-	}
-
+	  cmd[0] = 'L'; cmd[1] = 'I'; cmd[2] = 'S'; cmd[3] = 'T';cmd[4] = '\0';
+	  goto list_all;
+	}
       sl_strlcpy (srvcmd->cmd, cmd, SH_MAXMSGLEN);
       sl_strlcpy (srvcmd->clt, clt, SH_MAXMSGLEN);
@@ -1029,38 +769,61 @@
 		      _("Bad message format"), 
 		      _("sh_socket_read"));
-      if (error_type[0] == '\0')
-	sl_strlcpy(error_type, _("!E:message-format"), sizeof(error_type));
-      send_reply(talkfd, error_type);
+      close(talkfd);
       return -1;
     }
 
   /* Bounce the message back to the sender. 
+   * 'name' is the receiver address; it has been been filled
+   *        with the sender address in the recvfrom call 
    */
-  nbytes = send_reply(talkfd, cmd);
-
+#ifdef SH_DEBUG_SOCKET
+  fprintf (stderr, "Server: send message: %s to %s\n", 
+	   cmd, name.sun_path);
+#endif
+  /*
+  nbytes = sendto (pf_unix_fd, message, nbytes, 0,
+                       (struct sockaddr *) & name, size);
+  */
+  nbytes = send (talkfd, cmd, strlen(cmd) + 1, 0);
+  close(talkfd);
+  if (nbytes < 0)
+    {
+      sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
+		      sh_error_message (errno), 
+		      _("sh_socket_read: send"));
+      return -1;
+    }
+#ifdef SH_DEBUG_SOCKET
+  fprintf (stderr, "Server: message is out\n");
+#endif
   return nbytes;
-}
-
-static int list_all (int talkfd, char * cmd)
-{
-  int nbytes;
-  struct socket_cmd * list_cmd;
-  char message[SH_MAXMSG];
-  char errbuf[SH_ERRBUF_SIZE];
-
-  if (0 == strncmp(cmd, _("LISTALL"), 7))
+
+ list_all:
+#ifdef SH_DEBUG_SOCKET
+  fprintf (stderr, "Server: list all\n");
+#endif
+  if (cmd[4] == 'A' && cmd[5] == 'L' && cmd[6] == 'L')
     {
       list_cmd = runlist;
       while (list_cmd)
 	{
-	  sl_snprintf(message, sizeof(message), _("SENT  %42s  %32s  %s"),
+	  sl_snprintf(message, SH_MAXMSG, _("SENT  %8s  %32s  %s"),
 		      list_cmd->cmd, list_cmd->clt, list_cmd->cti);
-
+	  /*
+	  sl_strlcpy(message,     _("DONE"), SH_MAXMSG);
+	  sl_strlcat(message,          "  ", SH_MAXMSG);
+	  sl_strlcat(message, list_cmd->cmd, SH_MAXMSG);
+	  sl_strlcat(message,          "  ", SH_MAXMSG);
+	  sl_strlcat(message, list_cmd->clt, SH_MAXMSG);
+	  sl_strlcat(message,          "  ", SH_MAXMSG);
+	  sl_strlcat(message, list_cmd->cti, SH_MAXMSG);
+	  */
 	  nbytes = send (talkfd, message, sl_strlen(message) + 1, 0);
 	  if (nbytes < 0)
 	    {
 	      sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
-			      sh_error_message (errno, errbuf, sizeof(errbuf)), 
+			      sh_error_message (errno), 
 			      _("sh_socket_read: sendto"));
+	      close(talkfd);
 	      return -1;
 	    }
@@ -1072,13 +835,26 @@
   while (list_cmd)
     {
-      sl_snprintf(message, sizeof(message), _(">>>>  %42s  %32s  %s"),
+      sl_snprintf(message, SH_MAXMSG, _(">>>>  %8s  %32s  %s"),
 		  list_cmd->cmd, list_cmd->clt, list_cmd->cti);
-
+      /*
+      sl_strlcpy(message,     _(">>>>"), SH_MAXMSG);
+      sl_strlcat(message,          "  ", SH_MAXMSG);
+      sl_strlcat(message, list_cmd->cmd, SH_MAXMSG);
+      sl_strlcat(message,          "  ", SH_MAXMSG);
+      sl_strlcat(message, list_cmd->clt, SH_MAXMSG);
+      sl_strlcat(message,          "  ", SH_MAXMSG);
+      sl_strlcat(message, list_cmd->cti, SH_MAXMSG);
+      */
+      /*
+      nbytes = sendto (pf_unix_fd, message, sl_strlen(message) + 1, 0,
+                       (struct sockaddr *) & name, size);
+      */
       nbytes = send (talkfd, message, sl_strlen(message) + 1, 0);
       if (nbytes < 0)
 	{
 	  sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
-			  sh_error_message (errno, errbuf, sizeof(errbuf)), 
+			  sh_error_message (errno), 
 			  _("sh_socket_read: sendto"));
+	  close(talkfd);
 	  return -1;
 	}
@@ -1086,21 +862,41 @@
     }
 
-  send (talkfd, _("END"), 4, 0);
+  /*
+  nbytes = sendto (pf_unix_fd, _("END"), 4, 0,
+		   (struct sockaddr *) & name, size);
+  */
+  nbytes = send (talkfd, _("END"), 4, 0);
+  close(talkfd);
   return 0;
 }
+/* #if !defined(HAVE_CMSGCRED) || !defined(SO_PEERCRED) */
+/* #endif */
 
 static void sh_socket_add2list (struct socket_cmd * in)
 {
-  struct socket_cmd  * new  = cmdlist;
-  struct socket_cmd  * last = cmdlist;
+  struct socket_cmd  * new;
+
+  new = SH_ALLOC(sizeof(struct socket_cmd));
+  strcpy (new->cmd, in->cmd);
+  strcpy (new->clt, in->clt);
+  strcpy (new->cti, sh_unix_time(0));
+  new->next = cmdlist;
+  cmdlist   = new;
+
+  return;
+}
+
+static void sh_socket_add2run (struct socket_cmd * in)
+{
+  struct socket_cmd  * new = runlist;
+  char * client_name       = in->clt;
 
   while (new)
     {
-      /* Only skip identical commands.
-       */
-      if (0 == sl_strcmp(new->clt,  in->clt) &&
-	  0 == sl_strcmp(new->cmd,  in->cmd))
-	{
-	  (void) sh_unix_time(0, new->cti, sizeof(new->cti));
+      if (0 == sl_strcmp(new->clt, client_name))
+	{
+	  strcpy (new->cmd, in->cmd);
+	  strcpy (new->clt, in->clt);
+	  strcpy (new->cti, sh_unix_time(0));
 	  return;
 	}
@@ -1109,65 +905,16 @@
 
   new = SH_ALLOC(sizeof(struct socket_cmd));
-  sl_strlcpy (new->cmd,  in->cmd,  sizeof(new->cmd));
-  sl_strlcpy (new->clt,  in->clt,  sizeof(new->clt));
-  (void) sh_unix_time(0, new->cti, sizeof(new->cti));
-  new->next  = NULL;
-
-  if (last)
-    {
-      while (last->next) { last = last->next; }
-      last->next = new;
-    }
-  else
-    {
-      cmdlist = new;
-    }
+  strcpy (new->cmd, in->cmd);
+  strcpy (new->clt, in->clt);
+  strcpy (new->cti, sh_unix_time(0));
+  new->next = runlist;
+  runlist   = new;
+
   return;
 }
 
-static void sh_socket_add2run (struct socket_cmd * in)
-{
-  struct socket_cmd  * new  = runlist;
-  struct socket_cmd  * last = runlist;
-
-  while (new)
-    {
-      /* Only skip identical commands. First 5 will
-       * make all 'DELTA' identical.
-       */
-      if (0 == sl_strcmp(new->clt,  in->clt) &&
-	  0 == sl_strncmp(new->cmd,  in->cmd, 5))
-	{
-	  sl_strlcpy (new->cmd,  in->cmd,  sizeof(new->cmd));
-	  (void) sh_unix_time(0, new->cti, sizeof(new->cti));
-	  return;
-	}
-      new = new->next;
-    }
-
-  new = SH_ALLOC(sizeof(struct socket_cmd));
-  sl_strlcpy (new->cmd,  in->cmd,  sizeof(new->cmd));
-  sl_strlcpy (new->clt,  in->clt,  sizeof(new->clt));
-#ifdef SH_DEBUG_SOCKET
-  fprintf(stderr, "add2run: time set for %s\n", new->clt);
-#endif
-  (void) sh_unix_time(0, new->cti, sizeof(new->cti));
-  new->next  = NULL;
-
-  if (last)
-    {
-      while (last->next) { last = last->next; }
-      last->next = new;
-    }
-  else
-    {
-      runlist = new;
-    }
-  return;
-}
-
-
-
-static void sh_socket_rm2list (const char * client_name, int remove_all)
+
+
+static void sh_socket_rm2list (const char * client_name)
 {
   struct socket_cmd * old = cmdlist;
@@ -1180,5 +927,4 @@
 	  if ((new == cmdlist) && (new->next == NULL))
 	    {
-	      /* There is only one entry */
 	      cmdlist = NULL;
 	      SH_FREE(new);
@@ -1187,12 +933,7 @@
 	  else if (new == cmdlist)
 	    {
-	      /* first entry: new = old = cmdlist */
 	      cmdlist = new->next;
 	      SH_FREE(new);
-	      if (remove_all == S_FALSE)
-		return;
-	      old = cmdlist;
-	      new = cmdlist;
-	      continue;
+	      return;
 	    }
 	  else
@@ -1200,7 +941,5 @@
 	      old->next = new->next;
 	      SH_FREE(new);
-	      if (remove_all == S_FALSE)
-		return;
-	      new = old;
+	      return;
 	    }
 	}
@@ -1221,14 +960,21 @@
 
   if (pf_unix_fd  < 0)
-    return 0;
-
-  sl_strlcpy(cancel_cmd, _("CANCEL"), sizeof(cancel_cmd)); 
+    {
+      return 0;
+    }
+
+  sl_strlcpy(cancel_cmd, _("CANCEL"), SH_MAXMSGLEN); 
 
   while (sh_socket_read (&cmd) > 0)
     {
       if (0 == sl_strcmp(cmd.cmd, cancel_cmd))
-	sh_socket_rm2list  (cmd.clt, S_TRUE);
+	{
+	  sh_socket_rm2list  (cmd.clt);
+	}
       else
-	sh_socket_add2list (&cmd);
+	{
+	  sh_socket_rm2list  (cmd.clt);
+	  sh_socket_add2list (&cmd);
+	}
     }
   return 0;
@@ -1240,4 +986,5 @@
 char * sh_socket_check(const char * client_name)
 {
+  struct socket_cmd * old = cmdlist;
   struct socket_cmd * new = cmdlist;
   static char         out[SH_MAXMSGLEN];
@@ -1247,149 +994,17 @@
       if (0 == sl_strcmp(new->clt, client_name))
 	{
-	  sl_strlcpy(out,  new->cmd,  sizeof(out));
+	  sl_strlcpy(out, new->cmd, SH_MAXMSGLEN);
 	  sh_socket_add2run (new);
-	  sh_socket_rm2list (client_name, S_FALSE);
+	  sh_socket_rm2list  (client_name);
 	  return out;
 	}
+      old = new;
       new = new->next;
     }
   return NULL;
 }
+
 /* #if defined (SH_WITH_SERVER)
  */
 #endif
 
-
-#ifdef SH_CUTEST
-#include "CuTest.h"
-
-void Test_cmdlist (CuTest *tc) {
-
-#if defined (SH_WITH_SERVER)
-  struct socket_cmd cmd;
-  char * p;
-
-  sl_strlcpy(cmd.clt, "one", sizeof(cmd.clt));
-  sl_strlcpy(cmd.cmd, "RELOAD", sizeof(cmd.cmd));
-
-  sh_socket_add2list (&cmd);
-  p = sh_socket_check("one");
-  CuAssertPtrNotNull(tc, p);
-  CuAssertStrEquals(tc, "RELOAD", p);
-
-  p = sh_socket_check("one");
-  CuAssertPtrEquals(tc, NULL, p);
-
-  sh_socket_add2list (&cmd);
-  sl_strlcpy(cmd.cmd, "STOP",   sizeof(cmd.cmd));
-  sh_socket_add2list (&cmd);
-
-  sl_strlcpy(cmd.clt, "two", sizeof(cmd.clt));
-  sl_strlcpy(cmd.cmd, "STOP", sizeof(cmd.cmd));
-  sh_socket_add2list (&cmd);
-  sl_strlcpy(cmd.clt, "three", sizeof(cmd.clt));
-  sl_strlcpy(cmd.cmd, "STOP", sizeof(cmd.cmd));
-  sh_socket_add2list (&cmd);
-
-  sl_strlcpy(cmd.clt, "one", sizeof(cmd.clt));
-  sl_strlcpy(cmd.cmd, "DELTA",   sizeof(cmd.cmd));
-  sh_socket_add2list (&cmd);
-
-  p = sh_socket_check("one");
-  CuAssertPtrNotNull(tc, p);
-  CuAssertStrEquals(tc, "RELOAD", p);
-
-  sl_strlcpy(cmd.clt, "two", sizeof(cmd.clt));
-  sl_strlcpy(cmd.cmd, "RELOAD", sizeof(cmd.cmd));
-  sh_socket_add2list (&cmd);
-  sl_strlcpy(cmd.clt, "three", sizeof(cmd.clt));
-  sl_strlcpy(cmd.cmd, "RELOAD", sizeof(cmd.cmd));
-  sh_socket_add2list (&cmd);
-
-  p = sh_socket_check("one");
-  CuAssertPtrNotNull(tc, p);
-  CuAssertStrEquals(tc, "STOP", p);
-  p = sh_socket_check("one");
-  CuAssertPtrNotNull(tc, p);
-  CuAssertStrEquals(tc, "DELTA", p);
-  p = sh_socket_check("one");
-  CuAssertPtrEquals(tc, NULL, p);
-
-  /* Test removal in correct order */
-  sl_strlcpy(cmd.clt, "one", sizeof(cmd.clt));
-  sl_strlcpy(cmd.cmd, "RELOAD", sizeof(cmd.cmd));
-  sh_socket_add2list (&cmd);
-  sl_strlcpy(cmd.cmd, "STOP",   sizeof(cmd.cmd));
-  sh_socket_add2list (&cmd);
-  sl_strlcpy(cmd.cmd, "DELTA",  sizeof(cmd.cmd));
-  sh_socket_add2list (&cmd);
-  sl_strlcpy(cmd.cmd, "FOOBAR", sizeof(cmd.cmd));
-  sh_socket_add2list (&cmd);
-  
-  sh_socket_rm2list ("one", S_FALSE);
-
-  p = sh_socket_check("one");
-  CuAssertPtrNotNull(tc, p);
-  CuAssertStrEquals(tc, "STOP", p);
-
-  sh_socket_rm2list ("one", S_FALSE);
-  
-  p = sh_socket_check("one");
-  CuAssertPtrNotNull(tc, p);
-  CuAssertStrEquals(tc, "FOOBAR", p);
-
-  p = sh_socket_check("one");
-  CuAssertPtrEquals(tc, NULL, p);
-
-  sl_strlcpy(cmd.clt, "one", sizeof(cmd.clt));
-  sl_strlcpy(cmd.cmd, "RELOAD", sizeof(cmd.cmd));
-  sh_socket_add2list (&cmd);
-  sl_strlcpy(cmd.cmd, "STOP",   sizeof(cmd.cmd));
-  sh_socket_add2list (&cmd);
-
-  sl_strlcpy(cmd.clt, "two", sizeof(cmd.clt));
-  sl_strlcpy(cmd.cmd, "RELOAD", sizeof(cmd.cmd));
-  sh_socket_add2list (&cmd);
-  sl_strlcpy(cmd.clt, "three", sizeof(cmd.clt));
-  sl_strlcpy(cmd.cmd, "RELOAD", sizeof(cmd.cmd));
-  sh_socket_add2list (&cmd);
-
-  sl_strlcpy(cmd.clt, "one", sizeof(cmd.clt));
-  sl_strlcpy(cmd.cmd, "DELTA",  sizeof(cmd.cmd));
-  sh_socket_add2list (&cmd);
-  sl_strlcpy(cmd.cmd, "FOOBAR", sizeof(cmd.cmd));
-  sh_socket_add2list (&cmd);
-
-  sh_socket_rm2list ("one", S_TRUE);
-  p = sh_socket_check("one");
-  CuAssertPtrEquals(tc, NULL, p);
-
-  p = sh_socket_check("two");
-  CuAssertPtrNotNull(tc, p);
-  CuAssertStrEquals(tc, "STOP", p);
-  p = sh_socket_check("two");
-  CuAssertPtrNotNull(tc, p);
-  CuAssertStrEquals(tc, "RELOAD", p);
-  p = sh_socket_check("two");
-  CuAssertPtrEquals(tc, NULL, p);
-
-  p = sh_socket_check("three");
-  CuAssertPtrNotNull(tc, p);
-  CuAssertStrEquals(tc, "STOP", p);
-  p = sh_socket_check("three");
-  CuAssertPtrNotNull(tc, p);
-  CuAssertStrEquals(tc, "RELOAD", p);
-  p = sh_socket_check("three");
-  CuAssertPtrEquals(tc, NULL, p);
-
-  p = sh_socket_check("four");
-  CuAssertPtrEquals(tc, NULL, p);
-#else
-  (void) tc;
-#endif
-}
-
-#endif  /* #ifdef SH_CUTEST */
-
-
-
Index: trunk/src/sh_srp.c
===================================================================
--- trunk/src/sh_srp.c	(revision 591)
+++ trunk/src/sh_srp.c	(revision 1)
@@ -29,17 +29,4 @@
 #ifdef USE_SRP_PROTOCOL
 
-/* dont modify this, unless you know what you do
- */
-#define SRP_GENERATOR_1024      "2"
-#define SRP_MODULUS_1024_1        \
-_("f488fd584e49dbcd20b49de49107366b336c380d451d0f7c88b31c7c5b2d8ef6") 
-#define SRP_MODULUS_1024_2        \
-_("f3c923c043f0a55b188d8ebb558cb85d38d334fd7c175743a31d186cde33212c") 
-#define SRP_MODULUS_1024_3        \
-_("b52aff3ce1b1294018118d7c84a70a72d686c40319c807297aca950cd9969fab")
-#define SRP_MODULUS_1024_4        \
-_("d00a509b0246d3083d66a45d419f9c7cbd894b221926baaba25ec355e92f78c7")
-
-
 #if (defined (SH_WITH_CLIENT) || defined (SH_WITH_SERVER))
 
@@ -61,5 +48,5 @@
 #define bignum MP_INT
 
-static
+inline
 int big_create (bignum * a)
 {
@@ -68,5 +55,5 @@
 }
 
-static
+inline
 int big_zerop (bignum * a)
 {
@@ -82,5 +69,5 @@
 }
 
-static
+inline
 int big_trunc (bignum * a, bignum * b, bignum * q, bignum *r)
 {
@@ -89,5 +76,5 @@
 }
 
-static
+inline
 int big_exptmod (bignum * a, bignum * b, bignum * c, bignum *d)
 {
@@ -99,5 +86,5 @@
 int    siz_str_internal = 0;
 
-static
+inline
 char * big_string (bignum * a, int base)
 {
@@ -109,5 +96,5 @@
   if (get_str_internal == NULL)
     {
-      get_str_internal = calloc(1,512);   /* only once */
+      get_str_internal = malloc(512);   /* only once */
       if (get_str_internal)
 	{
@@ -127,17 +114,5 @@
       size = strlen(str) + 1;
       if (size > siz_str_internal)
-	{
-	  char * ptr = realloc (get_str_internal, size);
-	  if (ptr)
-	    get_str_internal = ptr;
-	  else
-	    {
-	      /* "If realloc() fails, the original block is left untouched;
-	       * it is not freed or moved." */
-	      /* cppcheck-suppress doubleFree */
-	      free(get_str_internal);
-	      get_str_internal = NULL;
-	    }
-	}
+	get_str_internal = realloc (get_str_internal, size);
       if (get_str_internal == NULL)
 	{
@@ -146,5 +121,5 @@
 	}
       siz_str_internal = size;
-      sl_strlcpy (get_str_internal, str, siz_str_internal);
+      strcpy (get_str_internal, str);                   /* known to fit  */
       for (i = 0; i < (size-1); ++i)
 	if (get_str_internal[i] >= 'a' && get_str_internal[i] <= 'f' )
@@ -155,5 +130,5 @@
 }
 
-static 
+inline 
 int big_add(bignum * a, bignum * b, bignum * c)
 {
@@ -162,5 +137,5 @@
 }
 
-static 
+inline 
 int big_sub(bignum * a, bignum * b, bignum * c)
 {
@@ -169,5 +144,5 @@
 }
 
-static 
+inline 
 int big_mul(bignum * a, bignum * b, bignum * c)
 {
@@ -176,5 +151,5 @@
 }
 
-static 
+inline 
 int big_greaterp(bignum * a, bignum * b)
 {
@@ -182,5 +157,5 @@
 }
 
-static 
+inline 
 int big_set_big(bignum * a, bignum * b)
 {
@@ -190,5 +165,5 @@
 
 
-static 
+inline 
 int big_set_string(const char * str, int base, bignum * a)
 {
@@ -222,8 +197,7 @@
 
   char           *combi;
-  size_t          len, l2;
+  size_t          len;
   register int i;
   unsigned char * dez = NULL;
-  char hashbuf[KEYBUF_SIZE];
 
   SL_ENTER(_("sh_srp_x"));
@@ -244,13 +218,8 @@
 
   (void) sl_strlcpy (skey->vernam,
-		     sh_tiger_hash(skey->vernam, TIGER_DATA, PW_LEN, 
-				   hashbuf, sizeof(hashbuf)), 
-		     KEY_LEN);
+		     sh_tiger_hash(skey->vernam, TIGER_DATA, PW_LEN), KEY_LEN);
   skey->vernam[KEY_LEN] = '\0';
 
-  len = sl_strlen(salt) + 1;
-  l2  = sl_strlen(skey->vernam);
-  if (sl_ok_adds(len, l2))
-    len += l2;
+  len = sl_strlen(salt) + sl_strlen(skey->vernam) + 1;
 
   /* H(s,P)
@@ -261,6 +230,5 @@
   (void) sl_strlcpy (sh_srp.x, 
 		     sh_tiger_hash(combi, TIGER_DATA, 
-				   (unsigned long) sl_strlen(combi),
-				   hashbuf, sizeof(hashbuf)),
+				   (unsigned long) sl_strlen(combi)),
 		     KEY_LEN+1);
   SH_FREE (combi);
@@ -269,8 +237,9 @@
 }
 
-char * sh_srp_M (char * x1, char * x2, char * x3, char * hash, size_t size)
+char * sh_srp_M (char * x1, char * x2, char * x3)
 {
   char           *combi;
-  size_t          len, l2, l3;
+  size_t          len;
+  static char     hash[KEY_LEN+1];
   
   SL_ENTER(_("sh_srp_M"));
@@ -279,12 +248,5 @@
 	     _("x1 != NULL && x2 != NULL && x3 !=NULL"), NULL);
 
-  len = sl_strlen(x1) + 1;
-  l2  = sl_strlen(x2); 
-  l3  = sl_strlen(x3);
-
-  if (sl_ok_adds(len, l2))
-    len += l2;
-  if (sl_ok_adds(len, l3))
-    len += l3;
+  len = sl_strlen(x1) + sl_strlen(x2) + sl_strlen(x3) + 1;
   
   /* H(x1,x2,x3)
@@ -294,6 +256,7 @@
   (void) sl_strlcat (combi, x2, len);
   (void) sl_strlcat (combi, x3, len);
-  (void) sh_tiger_hash(combi, TIGER_DATA, (unsigned long) (len-1),
-		       hash, size);
+  (void) sl_strlcpy (hash, 
+		     sh_tiger_hash(combi, TIGER_DATA, (unsigned long) (len-1)),
+		     KEY_LEN+1);
   SH_FREE (combi);
   
@@ -362,15 +325,14 @@
   int    res;
   char   hash[KEY_LEN+1];
-  char hashbuf[KEYBUF_SIZE];
 
   SL_ENTER(_("sh_srp_make_a"));
 
   for (i = 0; i < 6; ++i)
-    randl[i] = (UINT32) taus_get ();
-
+    randl[i] = (UINT32) taus_get (&(skey->rng0[0]), 
+				  &(skey->rng1[0]),
+				  &(skey->rng2[0]));
   (void) sl_strlcpy (hash, 
 		     sh_tiger_hash((char *)&randl[0], TIGER_DATA, 
-				   (unsigned long) 6*sizeof(UINT32),
-				   hashbuf, sizeof(hashbuf)), 
+				   (unsigned long) 6*sizeof(UINT32)), 
 		     KEY_LEN+1);
 
@@ -413,5 +375,4 @@
   if (res != BIG_OK)             val = (-1);
   else if (0 != big_zerop(&AB) ) val = (-1); /* 0 != (sign == 0) */
-  else if (0 != big_zerop(&r) )  val = (-1); /* 0 != (sign == 0) */
   else                           val =    0;
 
@@ -423,5 +384,5 @@
 }
 
-#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
+#ifdef SH_WITH_CLIENT
   
 
@@ -431,4 +392,5 @@
   char    *str;
   char    *combi;
+  size_t   len;
   bigerr_t res;
 
@@ -446,5 +408,9 @@
   
   if (str != NULL)
-    combi = sh_util_strdup(str);
+    {
+      len = sl_strlen(str) + 1;
+      combi = SH_ALLOC(len);
+      (void) sl_strlcpy (combi, str, len);
+    }
   else
     combi = NULL;
@@ -464,4 +430,5 @@
   char    *str;
   char    *combi;
+  long     len;
   bigerr_t res;
 
@@ -502,5 +469,10 @@
   
   if (str != NULL)
-    combi = sh_util_strdup(str);
+    {
+      len = sl_strlen(str) + 1;
+      combi = SH_ALLOC(len);
+      sl_strlcpy (combi, str, len);
+      /* fprintf(stderr, "OK2a %ld %s\n", len, combi); */
+    }
   else
     combi = NULL;
@@ -517,5 +489,5 @@
   
   
-#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
+#ifdef SH_WITH_CLIENT
   
 char * sh_srp_S_c (char * u_str, char * B_str)
@@ -524,4 +496,5 @@
   char    *str;
   char    *combi;
+  size_t   len;
   bigerr_t res;
 
@@ -598,5 +571,9 @@
 
   if (str != NULL)
-    combi = sh_util_strdup(str);
+    {
+      len = sl_strlen(str) + 1;
+      combi = SH_ALLOC(len);
+      (void) sl_strlcpy (combi, str, len);
+    }
   else
     combi = NULL;
@@ -624,4 +601,5 @@
   char    *str;
   char    *combi;
+  size_t   len;
   bigerr_t res;
 
@@ -673,5 +651,9 @@
   
   if (str != NULL)
-    combi = sh_util_strdup(str);
+    {
+      len = sl_strlen(str) + 1;
+      combi = SH_ALLOC(len);
+      (void) sl_strlcpy (combi, str, len);
+    }
   else
     combi = NULL;
@@ -697,4 +679,5 @@
   char    *combi;
   char    *str;
+  size_t   len;
   bigerr_t res;
   
@@ -717,5 +700,9 @@
   
   if (str != NULL)
-    combi = sh_util_strdup(str);
+    {
+      len = sl_strlen(str) + 1;
+      combi = SH_ALLOC(len);
+      (void) sl_strlcpy (combi, str, len);
+    }
   else
     combi = NULL;
@@ -737,95 +724,4 @@
 
 
-#ifdef SH_CUTEST
-#include "CuTest.h"
-
-void Test_srp (CuTest *tc)
-{
-#if defined(USE_SRP_PROTOCOL) && (defined (SH_WITH_CLIENT) || defined (SH_WITH_SERVER))
-
-  int result;
-  char     modulus[80*4];
-  bignum   a, b, c;
-  bigerr_t res;
-  char    *str = NULL;
-
-  res = sh_srp_init();
-  CuAssertTrue(tc, res == 0);
-
-  (void) sl_strlcpy(modulus, SRP_MODULUS_1024_1, sizeof(modulus));
-  (void) sl_strlcat(modulus, SRP_MODULUS_1024_2, sizeof(modulus));
-  (void) sl_strlcat(modulus, SRP_MODULUS_1024_3, sizeof(modulus));
-  (void) sl_strlcat(modulus, SRP_MODULUS_1024_4, sizeof(modulus));
-
-  res = big_create(&a);
-  CuAssertTrue(tc, res == BIG_OK);
-
-  /* Check plain zero 
-   */
-  result = sh_srp_check_zero ("0");
-  CuAssertTrue(tc, result != 0);
-  
-  res = big_set_string ("0",  16, &a);
-  CuAssertTrue(tc, res == BIG_OK);
-
-  result = sh_srp_check_zero (big_string(&a, 16));
-  CuAssertTrue(tc, result != 0);
-
-  /* Check modulus (equals 0 % M) 
-   */
-  result = sh_srp_check_zero (modulus);
-  CuAssertTrue(tc, result != 0);
-
-  res = big_set_string (modulus,  16, &a);
-  CuAssertTrue(tc, res == BIG_OK);
-
-  result = sh_srp_check_zero (big_string(&a, 16));
-  CuAssertTrue(tc, result != 0);
-
-  /* Check non-zero 
-   */
-  modulus[0] = 'a';
-
-  result = sh_srp_check_zero (modulus);
-  CuAssertTrue(tc, result == 0);
-
-  res = big_set_string (modulus,  16, &a);
-  CuAssertTrue(tc, res == BIG_OK);
-
-  result = sh_srp_check_zero (big_string(&a, 16));
-  CuAssertTrue(tc, result == 0);
-
-  modulus[0] = 'f';
-
-  /* Check multiple of modulus 
-   */
-  res = big_set_string (modulus,  16, &a);
-  CuAssertTrue(tc, res == BIG_OK);
-
-  res = big_create(&b);
-  CuAssertTrue(tc, res == BIG_OK);
-
-  res = big_create(&c);
-  CuAssertTrue(tc, res == BIG_OK);
-
-  res = big_set_string ("deadbeef", 16, &b);
-  CuAssertTrue(tc, res == BIG_OK);
-
-  res = big_mul (&a, &b, &c);
-  CuAssertTrue(tc, res == BIG_OK);
-
-  str = strdup(big_string (&c, 16));
-  CuAssertPtrNotNull(tc, str);
-
-  result = sh_srp_check_zero (str);
-  CuAssertTrue(tc, result != 0);
-
-#else
-  (void) tc; /* fix compiler warning */
-#endif
-  return;
-}
-#endif
-
-
-
+
+
Index: trunk/src/sh_static.c
===================================================================
--- trunk/src/sh_static.c	(revision 591)
+++ trunk/src/sh_static.c	(revision 1)
@@ -52,10 +52,4 @@
 #include <grp.h>
 
-#include "sh_pthread.h"
-
-extern  int sl_close_fd (const char * file, int line, int fd);
-extern  int sl_fclose (const char * file, int line, FILE * fp);
-
-
 #ifndef _PATH_PASSWD
 #define _PATH_PASSWD "/etc/passwd"
@@ -65,9 +59,4 @@
 #endif
 
-#undef  FIL__
-#define FIL__  _("sh_static.c")
-
-extern  int sl_strlcpy(char * dst, /*@null@*/const char * src, size_t siz);
-extern  int sl_strlcat(char * dst, /*@null@*/const char * src, size_t siz);
 
 
@@ -76,6 +65,5 @@
 
 #define PWD_BUFFER_SIZE 256
-#define GRP_BUFFER_SIZE 3584
-#define GRP_BUFFER_SIZE_MALLOC 32768
+#define GRP_BUFFER_SIZE 256
 
 /**********************************************************************/
@@ -102,8 +90,8 @@
 #define DO_GETXXKEY_R_PATHNAME  _PATH_PASSWD
 
-int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
-		    GETXXKEY_R_ENTTYPE *__restrict resultbuf,
-		    char *__restrict buffer, size_t buflen,
-		    GETXXKEY_R_ENTTYPE **__restrict result)
+static int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
+			   GETXXKEY_R_ENTTYPE *__restrict resultbuf,
+			   char *__restrict buffer, size_t buflen,
+			   GETXXKEY_R_ENTTYPE **__restrict result)
 {
   FILE *stream;
@@ -131,5 +119,5 @@
       }
     } while (1);
-    sl_fclose(FIL__, __LINE__, stream);
+    fclose(stream);
   }
   
@@ -150,8 +138,8 @@
 #define DO_GETXXKEY_R_PATHNAME  _PATH_GROUP
 
-int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
-		    GETXXKEY_R_ENTTYPE *__restrict resultbuf,
-		    char *__restrict buffer, size_t buflen,
-		    GETXXKEY_R_ENTTYPE **__restrict result)
+static int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
+			   GETXXKEY_R_ENTTYPE *__restrict resultbuf,
+			   char *__restrict buffer, size_t buflen,
+			   GETXXKEY_R_ENTTYPE **__restrict result)
 {
   FILE *stream;
@@ -179,5 +167,5 @@
       }
     } while (1);
-    sl_fclose(FIL__, __LINE__, stream);
+    fclose(stream);
   }
   
@@ -198,8 +186,8 @@
 #define DO_GETXXKEY_R_PATHNAME  _PATH_PASSWD
 
-int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
-		    GETXXKEY_R_ENTTYPE *__restrict resultbuf,
-		    char *__restrict buffer, size_t buflen,
-		    GETXXKEY_R_ENTTYPE **__restrict result)
+static int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
+			   GETXXKEY_R_ENTTYPE *__restrict resultbuf,
+			   char *__restrict buffer, size_t buflen,
+			   GETXXKEY_R_ENTTYPE **__restrict result)
 {
   FILE *stream;
@@ -227,5 +215,5 @@
       }
     } while (1);
-    sl_fclose(FIL__, __LINE__, stream);
+    fclose(stream);
   }
   
@@ -246,8 +234,8 @@
 #define DO_GETXXKEY_R_PATHNAME  _PATH_GROUP
 
-int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
-		    GETXXKEY_R_ENTTYPE *__restrict resultbuf,
-		    char *__restrict buffer, size_t buflen,
-		    GETXXKEY_R_ENTTYPE **__restrict result)
+static int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
+			   GETXXKEY_R_ENTTYPE *__restrict resultbuf,
+			   char *__restrict buffer, size_t buflen,
+			   GETXXKEY_R_ENTTYPE **__restrict result)
 {
   FILE *stream;
@@ -275,5 +263,5 @@
       }
     } while (1);
-    sl_fclose(FIL__, __LINE__, stream);
+    fclose(stream);
   }
   
@@ -291,9 +279,4 @@
 }
 
-struct passwd * getpwuid(uid_t uid)
-{
-        return sh_getpwuid(uid);
-}
-
 struct group * sh_getgrgid(gid_t gid)
 {
@@ -306,8 +289,4 @@
 }
 
-struct group * getgrgid(gid_t gid)
-{
-        return sh_getgrgid(gid);
-}
 
 struct passwd * sh_getpwnam(const char *name)
@@ -331,6 +310,6 @@
 }
 
-SH_MUTEX_STATIC(pwf_lock, PTHREAD_MUTEX_INITIALIZER);
-
+#define LOCK		((void) 0)
+#define UNLOCK		((void) 0)
 
 static FILE *pwf = NULL;
@@ -338,19 +317,19 @@
 void  sh_setpwent(void)
 {
-        SH_MUTEX_LOCK(pwf_lock);
+	LOCK;
 	if (pwf) {
 		rewind(pwf);
 	}
-	SH_MUTEX_UNLOCK(pwf_lock);
+	UNLOCK;
 }
 
 void  sh_endpwent(void)
 {
-        SH_MUTEX_LOCK(pwf_lock);
+	LOCK;
 	if (pwf) {
-		sl_fclose(FIL__, __LINE__, pwf);
+		fclose(pwf);
 		pwf = NULL;
 	}
-	SH_MUTEX_UNLOCK(pwf_lock);
+	UNLOCK;
 }
 
@@ -362,5 +341,5 @@
 	int rv;
 
-        SH_MUTEX_LOCK(pwf_lock);
+	LOCK;
 
 	*result = NULL;				/* In case of error... */
@@ -380,11 +359,11 @@
 
  ERR:
-	; /* 'label at end of compound statement' */
-	SH_MUTEX_UNLOCK(pwf_lock);
+	UNLOCK;
 
 	return rv;
 }
 
-SH_MUTEX_STATIC(grf_lock, PTHREAD_MUTEX_INITIALIZER);
+#define LOCK		((void) 0)
+#define UNLOCK		((void) 0)
 
 static FILE *grf = NULL;
@@ -392,19 +371,19 @@
 void  sh_setgrent(void)
 {
-	SH_MUTEX_LOCK(grf_lock);
+	LOCK;
 	if (grf) {
 		rewind(grf);
 	}
-	SH_MUTEX_UNLOCK(grf_lock);
+	UNLOCK;
 }
 
 void  sh_endgrent(void)
 {
-	SH_MUTEX_LOCK(grf_lock);
+	LOCK;
 	if (grf) {
-		sl_fclose(FIL__, __LINE__, grf);
+		fclose(grf);
 		grf = NULL;
 	}
-	SH_MUTEX_UNLOCK(grf_lock);
+	UNLOCK;
 }
 
@@ -415,5 +394,5 @@
 	int rv;
 
-	SH_MUTEX_LOCK(grf_lock);
+	LOCK;
 
 	*result = NULL;				/* In case of error... */
@@ -433,6 +412,5 @@
 
  ERR:
-	; /* 'label at end of compound statement' */
-	SH_MUTEX_UNLOCK(grf_lock);
+	UNLOCK;
 
 	return rv;
@@ -457,5 +435,5 @@
 	struct group *result;
 
-	sh_getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
+	 sh_getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
 	return result;
 }
@@ -464,16 +442,14 @@
 {
 	FILE *grf;
-	gid_t *group_list = NULL;
-	size_t num_groups;
-	int rv;
+	gid_t *group_list;
+	int num_groups, rv;
 	char **m;
 	struct group group;
-
-	char * buff = calloc(1,GRP_BUFFER_SIZE_MALLOC);
+	char buff[PWD_BUFFER_SIZE];
 
 	rv = -1;
 
 	/* We alloc space for 8 gids at a time. */
-	if (buff && ((group_list = calloc(8,sizeof(gid_t *))) != NULL)
+	if (((group_list = (gid_t *) malloc(8*sizeof(gid_t *))) != NULL)
 		&& ((grf = fopen(_PATH_GROUP, "r")) != NULL)
 		) {
@@ -484,5 +460,5 @@
 		num_groups = 1;
 
-		while (!__pgsreader(__parsegrent, &group, buff, GRP_BUFFER_SIZE_MALLOC, grf)) {
+		while (!__pgsreader(__parsegrent, &group, buff, sizeof(buff), grf)) {
 			assert(group.gr_mem); /* Must have at least a NULL terminator. */
 			if (group.gr_gid != gid) {
@@ -490,19 +466,12 @@
 					if (!strcmp(*m, user)) {
 						if (!(num_groups & 7)) {
-						  gid_t *tmp = NULL;
-						  if (num_groups > (SIZE_MAX - 8)) {
-						    rv = -1;
-						    goto DO_CLOSE;
-						  }
-						  if ((num_groups+8) <= (SIZE_MAX / sizeof(gid_t *))) {
-						    tmp = (gid_t *)
-						      realloc(group_list,
-							      (num_groups+8) * sizeof(gid_t *));
-						  } 
-						  if (!tmp) {
-						    rv = -1;
-						    goto DO_CLOSE;
-						  }
-						  group_list = tmp;
+							gid_t *tmp = (gid_t *)
+								realloc(group_list,
+										(num_groups+8) * sizeof(gid_t *));
+							if (!tmp) {
+								rv = -1;
+								goto DO_CLOSE;
+							}
+							group_list = tmp;
 						}
 						group_list[num_groups++] = group.gr_gid;
@@ -515,12 +484,10 @@
 		rv = setgroups(num_groups, group_list);
 	DO_CLOSE:
-		sl_fclose(FIL__, __LINE__, grf);
+		fclose(grf);
 	}
 
 	/* group_list will be NULL if initial malloc failed, which may trigger
 	 * warnings from various malloc debuggers. */
-	if (group_list) free(group_list);
-	if (buff) free(buff);
-	/* cppcheck-suppress resourceLeak */
+	free(group_list);
 	return rv;
 }
@@ -700,6 +667,6 @@
 		skip = 0;
 		do {
-			if (!fgets(line_buff, buflen, f)) {
-				if (feof(f)) {
+			if (!fgets_unlocked(line_buff, buflen, f)) {
+				if (feof_unlocked(f)) {
 					rv = ENOENT;
 				}
@@ -711,10 +678,6 @@
 				line_buff[line_len] = 0;
 			} else if (line_len + 2 == buflen) { /* line too long */
-			        rv = ERANGE;
-				break;
-				/*
 				++skip;
 				continue;
-				*/
 			}
 
@@ -886,5 +849,5 @@
 #include <arpa/inet.h>
 
-/* sl_close_fd(FIL__, __LINE__, )
+/* close()
  */
 #include <unistd.h>
@@ -898,5 +861,6 @@
 #include <arpa/nameser.h>
 
-SH_MUTEX_STATIC(resolv_lock, PTHREAD_MUTEX_INITIALIZER);
+#define BIGLOCK
+#define BIGUNLOCK
 
 #define __UCLIBC_HAS_IPV6__
@@ -917,8 +881,7 @@
 
 #undef DEBUG
-/* #define DEBUG */
+/*#define DEBUG*/
 
 #ifdef DEBUG
-/* flawfinder: ignore *//* definition of debug macro */
 #define DPRINTF(X,args...) fprintf(stderr, X, ##args)
 #else
@@ -1008,31 +971,14 @@
 		return -1;
 
-	do {
-		l = data[offset];
-		if (offset < INT_MAX)
-	                offset++;
-		else
-		        return -1;
-		if (!l)
-		  break;
-		
-		DPRINTF("l[%d] = %d\n", offset, l);
+	while ((l = data[offset++])) {
 
 		if ((l & 0xc0) == (0xc0)) {
-		        if (offset < INT_MAX)
-			  offset++;
-			else
-			  return -1;
+			offset++;
 			break;
 		}
 
-		if (offset <= (INT_MAX - l))
-		  offset += l;
-		else
-		  return -1;
-
-	} while (l);
-
-	DPRINTF("orig: %d now %d\n", orig_offset, offset);
+		offset += l;
+	}
+
 	return offset - orig_offset;
 }
@@ -1045,8 +991,6 @@
 	if (i < 0)
 		return i;
-	if (i < (INT_MAX - 4))
-	  return i + 4;
-	else
-	  return -1;
+
+	return i + 4;
 }
 
@@ -1064,44 +1008,26 @@
 	if (!data)
 		return -1;
-	if ((offset < 0) || (offset > (PACKETSZ-1)))
-	        return -1;
-	while ((l=data[offset])) {
-	        if (offset < (PACKETSZ-1)) offset++;
-		else return -1;
+
+	while ((l=data[offset++])) {
 		if (measure)
-		    { if (total < INT_MAX) total++; else return -1; }
+		    total++;
 		if ((l & 0xc0) == (0xc0)) {
-		        if (measure)
-			  { if (total < INT_MAX) total++; else return -1; }
-		        /* compressed item, redirect */ 
+			if (measure)
+				total++;
+			/* compressed item, redirect */
 			offset = ((l & 0x3f) << 8) | data[offset];
-			if ((offset < 0) || (offset > (PACKETSZ-1)))
-			  return -1; 
 			measure = 0;
 			continue;
 		}
-	        
-		if (used >= (INT_MAX - l))
-		  return -1;
 
 		if ((used + l + 1) >= maxlen)
-		  return -1; 
+			return -1;
 
 		memcpy(dest + used, data + offset, l);
-		
-		if (offset <= ((PACKETSZ-1) - l))
-		  offset += l;
-		else
-		  return -1;
-
-		if (used <= (INT_MAX - l))
-		  used += l;
-		else
-		  return -1;
+		offset += l;
+		used += l;
 		if (measure)
-		  { if (total <= (INT_MAX -l)) total += l; else return -1; }
-
-		if (used >= maxlen)
-		  return -1;
+			total += l;
+
 		if (data[offset] != 0)
 			dest[used++] = '.';
@@ -1112,5 +1038,5 @@
 	/* The null byte must be counted too */
 	if (measure) {
-	    if (total < INT_MAX) total++; else return -1; 
+	    total++;
 	}
 
@@ -1124,14 +1050,11 @@
 {
 	char temp[256];
-	int i = 0;
+	int i;
 
 	i = __decode_dotted(message, offset, temp, sizeof(temp));
-	if (i < 0 || i > PACKETSZ)
-		return -1;
-
-	if (offset <= ((PACKETSZ - 10) - i))
-	  message += offset + i;
-	else
-	  return -1;
+	if (i < 0)
+		return i;
+
+	message += offset + i;
 
 	a->dotted = strdup(temp);
@@ -1150,12 +1073,5 @@
 	DPRINTF("i=%d,rdlength=%d\n", i, a->rdlength);
 
-	if (RRFIXEDSZ <= (INT_MAX - i))
-	  i += RRFIXEDSZ;
-	else
-	  return -1;
-	if (a->rdlength <= (INT_MAX - i))
-	  return i + a->rdlength;
-	else
-	  return -1;
+	return i + RRFIXEDSZ + a->rdlength;
 }
 
@@ -1204,6 +1120,4 @@
 
 	dest += i;
-	if (maxlen < i)
-	  return -1;
 	maxlen -= i;
 
@@ -1216,8 +1130,5 @@
 	dest[3] = (q->qclass & 0x00ff) >> 0;
 
-	if (i <= (INT_MAX - 4))
-	  return i + 4;
-	else
-	  return -1;
+	return i + 4;
 }
 
@@ -1238,6 +1149,6 @@
 	struct resolv_question q;
 	int retries = 0;
-	unsigned char * packet = calloc(1,PACKETSZ);
-	char *dns, *lookup = calloc(1,MAXDNAME);
+	unsigned char * packet = malloc(PACKETSZ);
+	char *dns, *lookup = malloc(MAXDNAME);
 	int variant = 0;
 	struct sockaddr_in sa;
@@ -1254,11 +1165,11 @@
 	DPRINTF("Looking up type %d answer for '%s'\n", type, name);
 
-	SH_MUTEX_LOCK_UNSAFE(resolv_lock);
+	LOCK;
 	ns %= nscount;
-	SH_MUTEX_UNLOCK_UNSAFE(resolv_lock);
+	UNLOCK;
 
 	while (retries++ < MAX_RETRIES) {
 		if (fd != -1)
-			sl_close_fd(FIL__, __LINE__, fd);
+			close(fd);
 
 		memset(packet, 0, PACKETSZ);
@@ -1267,15 +1178,15 @@
 
 		/* Mess with globals while under lock */
-		SH_MUTEX_LOCK_UNSAFE(resolv_lock);
+		LOCK;
 		++id;
 		id &= 0xffff;
 		h.id = id;
 		dns = nsip[ns];
-		SH_MUTEX_UNLOCK_UNSAFE(resolv_lock);
+		UNLOCK;
 
 		h.qdcount = 1;
 		h.rd = 1;
 
-		DPRINTF("encoding header %d\n", h.rd);
+		DPRINTF("encoding header\n", h.rd);
 
 		i = __encode_header(&h, packet, PACKETSZ);
@@ -1283,12 +1194,12 @@
 			goto fail;
 
-		sl_strlcpy(lookup,name,MAXDNAME);
-		SH_MUTEX_LOCK_UNSAFE(resolv_lock);
+		strncpy(lookup,name,MAXDNAME);
+		BIGLOCK;
 		if (variant < __searchdomains && strchr(lookup, '.') == NULL)
 		{
-		    sl_strlcat(lookup,".", MAXDNAME);
-		    sl_strlcat(lookup,__searchdomain[variant], MAXDNAME);
-		}
-		SH_MUTEX_UNLOCK_UNSAFE(resolv_lock);
+		    strncat(lookup,".", MAXDNAME);
+		    strncat(lookup,__searchdomain[variant], MAXDNAME);
+		}
+		BIGUNLOCK;
 		DPRINTF("lookup name: %s\n", lookup);
 		q.dotted = (char *)lookup;
@@ -1363,16 +1274,15 @@
 		}
 
-		/* ok because we have checked that recv at least HFIXEDSZ */
 		__decode_header(packet, &h);
 
 		DPRINTF("id = %d, qr = %d\n", h.id, h.qr);
 
-		SH_MUTEX_LOCK_UNSAFE(resolv_lock);
+		LOCK;
 		if ((h.id != id) || (!h.qr)) {
-			SH_MUTEX_UNLOCK_UNSAFE(resolv_lock);
+			UNLOCK;
 			/* unsolicited */
 			goto again;
 		}
-		SH_MUTEX_UNLOCK_UNSAFE(resolv_lock);
+		UNLOCK;
 
 
@@ -1397,6 +1307,4 @@
 				goto again;
 			pos += i;
-			if (pos >= PACKETSZ)
-			        goto again;
 		}
 		DPRINTF("Decoding answer at pos %d\n", pos);
@@ -1417,6 +1325,4 @@
 		    free(a->dotted);
 		    pos += i;
-		    if (pos >= PACKETSZ)
-		            goto again;
 		}
 
@@ -1424,5 +1330,5 @@
 		DPRINTF("Answer type = |%d|\n", a->atype);
 
-		sl_close_fd(FIL__, __LINE__, fd);
+		close(fd);
 
 		if (outpacket)
@@ -1439,7 +1345,7 @@
 		    int sdomains;
 
-		    SH_MUTEX_LOCK_UNSAFE(resolv_lock);
+		    BIGLOCK;
 		    sdomains=__searchdomains;
-		    SH_MUTEX_UNLOCK_UNSAFE(resolv_lock);
+		    BIGUNLOCK;
 		    variant = 0;
 		    if (retries >= nscount*(sdomains+1))
@@ -1451,7 +1357,7 @@
 		{
 		    int sdomains;
-		    SH_MUTEX_LOCK_UNSAFE(resolv_lock);
+		    BIGLOCK;
 		    sdomains=__searchdomains;
-		    SH_MUTEX_UNLOCK_UNSAFE(resolv_lock);
+		    BIGUNLOCK;
 
 		    if (variant < sdomains) {
@@ -1460,7 +1366,7 @@
 		    } else {
 			/* next server, first search */
-			SH_MUTEX_LOCK_UNSAFE(resolv_lock);
+			LOCK;
 			ns = (ns + 1) % nscount;
-			SH_MUTEX_UNLOCK_UNSAFE(resolv_lock);
+			UNLOCK;
 			variant = 0;
 		    }
@@ -1470,5 +1376,5 @@
 fail:
 	if (fd != -1)
-	    sl_close_fd(FIL__, __LINE__, fd);
+	    close(fd);
 	if (lookup)
 	    free(lookup);
@@ -1483,5 +1389,4 @@
 		*fp = fopen("/etc/config/hosts", "r");
 	}
-	/* cppcheck-suppress resourceLeak */
 	return;
 }
@@ -1559,7 +1464,4 @@
 
 	*h_errnop=HOST_NOT_FOUND;
-	if (fp == NULL) {
-	        return ret;
-	}
 	while (fgets(buf, buflen, fp)) {
 		if ((cp = strchr(buf, '#')))
@@ -1630,10 +1532,10 @@
         
 		if (action!=GETHOSTENT) {
-			sl_fclose(FIL__, __LINE__, fp);
+			fclose(fp);
 		}
 		return ret;
 	}
 	if (action!=GETHOSTENT) {
-		sl_fclose(FIL__, __LINE__, fp);
+		fclose(fp);
 	}
 	return ret;
@@ -1653,5 +1555,5 @@
 }
 
-static int __open_nameservers(void)
+static int __open_nameservers()
 {
 	FILE *fp;
@@ -1661,7 +1563,8 @@
 	int argc;
 
-	SH_MUTEX_LOCK(resolv_lock);
-	if (__nameservers > 0) {
-	  goto the_end;
+	BIGLOCK;
+	if (__nameservers > 0) { 
+	    BIGUNLOCK;
+	    return 0;
 	}
 
@@ -1703,13 +1606,10 @@
 			}
 		}
-		sl_fclose(FIL__, __LINE__, fp);
+		fclose(fp);
 	} else {
 	    DPRINTF("failed to open %s\n", "resolv.conf");
 	}
 	DPRINTF("nameservers = %d\n", __nameservers);
- the_end:
-	; /* 'label at end of compound statement' */
-	SH_MUTEX_UNLOCK(resolv_lock);
-	/* cppcheck-suppress resourceLeak */
+	BIGUNLOCK;
 	return 0;
 }
@@ -1730,5 +1630,4 @@
 	char ** __nameserverXX;
 
-	DPRINTF("sh_gethostbyname_r: /%s/\n", name);
 	__open_nameservers();
 
@@ -1774,5 +1673,5 @@
 	if (buflen<256)
 		return ERANGE;
-	strncpy(buf, name, buflen-1);
+	strncpy(buf, name, buflen);
 
 	/* First check if this is already an address */
@@ -1789,8 +1688,8 @@
 	for (;;) {
 
-	SH_MUTEX_LOCK_UNSAFE(resolv_lock);
+	BIGLOCK;
 	__nameserversXX=__nameservers;
 	__nameserverXX=__nameserver;
-	SH_MUTEX_UNLOCK_UNSAFE(resolv_lock);
+	BIGUNLOCK;
 		i = __dns_lookup(buf, T_A, __nameserversXX, __nameserverXX, &packet, &a);
 
@@ -1801,5 +1700,5 @@
 		}
 
-		strncpy(buf, a.dotted, buflen-1);
+		strncpy(buf, a.dotted, buflen);
 		free(a.dotted);
 
@@ -1831,5 +1730,4 @@
 			free(packet);
 			*h_errnop=HOST_NOT_FOUND;
-			DPRINTF("host_not_found\n");
 			return TRY_AGAIN;
 		}
@@ -1843,12 +1741,12 @@
 struct hostent * sh_gethostbyname(const char *name)
 {
-        static struct hostent h = { NULL, NULL, 0, 0, NULL};
+	static struct hostent h;
 	static char buf[sizeof(struct in_addr) +
 			sizeof(struct in_addr *)*2 +
 			sizeof(char *)*(ALIAS_DIM) + 256/*namebuffer*/ + 32/* margin */];
 	struct hostent *hp;
-	
-	DPRINTF("sh_gethostbyname: /%s/\n", name);
+
 	sh_gethostbyname_r(name, &h, buf, sizeof(buf), &hp, &h_errno);
+
 	return hp;
 }
@@ -1909,5 +1807,4 @@
 	char ** __nameserverXX;
 
-	DPRINTF("sh_gethostbyaddr_r called\n");
 	*result=NULL;
 	if (!addr)
@@ -1984,5 +1881,5 @@
 
 	if(type == AF_INET) {
-		const unsigned char *tmp_addr = (const unsigned char *)addr;
+		unsigned char *tmp_addr = (unsigned char *)addr;
 
 		memcpy(&in->s_addr, addr, len);
@@ -2011,8 +1908,8 @@
 	for (;;) {
 
-	SH_MUTEX_LOCK_UNSAFE(resolv_lock);
+	BIGLOCK;
 	__nameserversXX=__nameservers;
 	__nameserverXX=__nameserver;
-	SH_MUTEX_UNLOCK_UNSAFE(resolv_lock);
+	BIGUNLOCK;
 		i = __dns_lookup(buf, T_PTR, __nameserversXX, __nameserverXX, &packet, &a);
 
@@ -2022,5 +1919,5 @@
 		}
 
-		strncpy(buf, a.dotted, buflen-1);
+		strncpy(buf, a.dotted, buflen);
 		free(a.dotted);
 
@@ -2080,5 +1977,4 @@
 	struct hostent *hp;
 
-	DPRINTF("sh_gethostbyaddr called\n");
 	sh_gethostbyaddr_r(addr, len, type, &h, buf, sizeof(buf), &hp, &h_errno);
         
Index: trunk/src/sh_string.c
===================================================================
--- trunk/src/sh_string.c	(revision 591)
+++ 	(revision )
@@ -1,1057 +1,0 @@
-
-#include "config_xor.h"
-
-#include <string.h>
-#include <stdio.h>
-#include <sys/types.h>
-
-#include "sh_string.h"
-#include "sh_mem.h"
-
-#undef  FIL__
-#define FIL__  _("sh_string.c")
-
-extern int sl_ok_adds (size_t a, size_t b);
-#define S_TRUE  1
-#define S_FALSE 0
-
-#include <ctype.h>
-#include <errno.h>
-
-/* Split array at delim in at most nfields fields. 
- * Empty fields are returned as empty (zero-length) strings. 
- * Leading and trailing WS are removed from token. 
- * The number of fields is returned in 'nfields', their
- * lengths in 'lengths'.
- * A single delimiter will return two empty fields.
- */
-char ** split_array(char *line, unsigned int * nfields, 
-                    char delim, size_t * lengths)
-{
-  char *a, *e, *s;
-  unsigned int i = 0;
-  int flag = 0;
-  char **arr;
-  unsigned int maxfields = (*nfields);
-
-  arr = SH_ALLOC((maxfields+1) * sizeof (char*));
-
-  e = line;
-
-  do
-    {
-      /* skip leading WS 
-       */
-      for (s=e; *s && isspace((int)*s); ++s) /* nothing */;
-
-      if (*s) 
-        {
-          /* move a to next delim
-           */
-          for (a=s; *a && *a != delim; ++a) /* nothing */;
-          
-          /* set e to next after delim
-           */
-          if (*a == delim)
-            {
-              e    = a+1;
-              flag = 1;
-            }
-          else /* (!*a) */
-            {
-              e    = a;
-              flag = 0;
-            }
-
-          if (a != line)
-            {
-	      if (i < (maxfields -1))
-                {
-
-		  /* chop off trailing WS 
-		   */
-		  for (a--; isspace((int)*a) && a > s; a--) /* do nothing */;
-		  
-		  /* terminate string
-		   */
-		  ++a; *a = '\0';
-		}
-              else
-                {
-                  /* If nfields < actual fields, last string 
-                   * will be remainder, therefore skip to end.
-                   */
-                  if ( *a )
-                    {
-                      do {
-                        a++;
-                      } while ( *a );
-                    }
-                }
-	    }
-          else
-            {
-              *a = '\0';
-            }
-        }
-      else /* (!*s) */
-        {
-          a = s;
-	  /* (i == 0) handles the special case of splitting the empty string */
-          if (flag || i == 0) 
-            {
-              flag = 0;
-              goto setnext;
-            }
-          break;
-        }
-
-    setnext:
-      lengths[i] = (size_t) (a-s); /* a >= s always */
-      arr[i] = s;
-      ++i;
-
-    } while (i < maxfields);
-
-  *nfields = i;
-  arr[i]   = NULL;
-
-  return arr;
-}
-
-/* Split array at whitespace in at most nfields fields.
- * Multiple whitespaces are collapsed. 
- * Empty fields are returned as empty (zero-length) strings.
- * The number of fields is returned in nfields.
- * An empty string will return zero fields.
- * If nfields < actual fields, last string will be remainder.
- */
-
-#define SH_SPLIT_LIST 0
-#define SH_SPLIT_WS   1
-
-char ** split_array_ws_int (char *line, 
-			    unsigned int * nfields, size_t * lengths,
-			    const char *delim, int isList)
-{
-  char *a, *e, *s;
-  unsigned int i = 0;
-  char **arr;
-  unsigned int maxfields = (*nfields);
-
-  arr = SH_ALLOC((maxfields+1) * sizeof (char*));
-
-  e = line;
-
-  do
-    {
-      s = e;
-
-      /* skip leading WS 
-       */
-      if (isList == SH_SPLIT_WS)
-	{
-	  if ( *s && isspace((int)*s) )
-	    {
-	      do {
-		++s;
-	      } while ( *s && isspace((int)*s) );
-	    }
-	}
-      else
-	{
-          if ( *s && strchr(delim, (int)*s))
-            {
-              do {
-                ++s;
-              } while ( *s && strchr(delim, (int)*s));
-            }
-
-	}
-
-      if (*s)
-        {
-
-          /* s is at non-ws, move a to next ws
-           */
-          a = s;
-	  if (isList == SH_SPLIT_WS)
-	    {
-	      do {
-		a++;
-	      } while ( *a && (!isspace((int)*a)) );
-	    }
-	  else
-	    {
-              do {
-                a++;
-              } while ( *a && NULL == strchr(delim, (int)*a));
-	    }
-
-          /* next token, *a is either ws or '\0' 
-           */
-          e = ( (*a) ? a+1 : a);
-          
-          /* terminate and set arr[i]
-           */
-          if (i < (maxfields-1))
-	    {
-              *a = '\0';
-	    }
-	  else
-	    {
-	      /* If nfields < actual fields, last 
-	       * string will be remainder. Therefore
-	       * skip to end.
-	       */
-	      if ( *a )
-		{
-		  do {
-		    a++;
-		  } while ( *a );
-		}
-	    }
-          lengths[i] = (size_t)(a-s); /* a >= s always */
-          arr[i]     = s; 
-          ++i;
-        }
-      else /* if (!*s) */
-        {
-          break;
-        }
-
-    } while (i < maxfields);
-
-  *nfields = i;
-  arr[i]   = NULL;
-
-  return arr;
-}
-
-char ** split_array_ws (char *line, 
-			unsigned int * nfields, size_t * lengths)
-{
-  return split_array_ws_int (line, nfields, lengths, NULL, SH_SPLIT_WS);
-}
-
-char ** split_array_list (char *line, 
-			  unsigned int * nfields, size_t * lengths)
-{
-  return split_array_ws_int (line, nfields, lengths, ", \t", SH_SPLIT_LIST);
-}
-
-char ** split_array_token (char *line, 
-			   unsigned int * nfields, size_t * lengths,
-			   const char * token)
-{
-  return split_array_ws_int (line, nfields, lengths, token, SH_SPLIT_LIST);
-}
-
-/* return a split() of a list contained in 'PREFIX\s*( list ).*'
- */
-char ** split_array_braced (char *line, const char * prefix,
-			    unsigned int * nfields, size_t * lengths)
-{
-  char * s = line;
-  char * p;
-  unsigned int sind = (prefix) ? strlen(prefix) : 0;
-
-  while ( *s && isspace((int)*s) ) ++s;
-  if (prefix && 0 != strncmp(s, prefix, strlen(prefix)))
-    return NULL;
-  s = &s[sind];
-  while ( *s && isspace((int)*s) ) ++s;
-  if (!s || (*s != '('))
-    return NULL;
-  ++s;
-  p = strchr(s, ')');
-  if (!p || (*p == *s))
-    return NULL;
-  *p = '\0';
-  return split_array_list (s, nfields, lengths);
-}
-
-#define SH_STRING_PARCEL 120
-
-static
-size_t sh_string_read_int(sh_string * s, FILE * fp, size_t maxlen, char *start);
-
-size_t sh_string_read(sh_string * s, FILE * fp, size_t maxlen)
-{
-  return sh_string_read_int(s, fp, maxlen, NULL);
-}
-
-size_t sh_string_read_cont(sh_string * s, FILE * fp, size_t maxlen, char *cont)
-{
-  return sh_string_read_int(s, fp, maxlen, cont);
-}
-
-static char * sh_str_fgets (char *s, int size, FILE *fp)
-{
-  char * ret;
-  do {
-    clearerr(fp);
-    ret = fgets(s, size, fp);
-  } while (ret == NULL && ferror(fp) && errno == EAGAIN);
-
-  return ret;
-}
-
-size_t sh_string_read_int(sh_string * s, FILE * fp, size_t maxlen, char *start)
-{
-
-  /* case 0) start != NULL and first char not in 'start'
-   */
-  if (start)
-    {
-      int first;
-
-      do {
-	clearerr(fp);
-	first = fgetc(fp);
-      } while (first == EOF && ferror(fp) && errno == EAGAIN);
-
-      if (first == EOF)
-	{
-	  sh_string_truncate(s, 0);
-	  if (ferror(fp))
-	    return -1;
-	  return 0;
-	}
-
-      if (NULL == strchr(start, first))
-	{
-	  ungetc(first, fp);
-	  return 0;
-	}
-      ungetc(first, fp);
-    }
-
-  /* case 1) EOF or error 
-   */
-  if (sh_str_fgets(s->str, s->siz, fp) == NULL)
-    {
-      sh_string_truncate(s, 0);
-      if (ferror(fp))
-        return -1;
-      return 0;
-    }
-
-  /* case 2) end of line reached. strlen should always be > 0
-   *         because of the '\n', but we check.
-   */
-  s->len = strlen(s->str);
-  if (s->len > 0 && (s->str)[s->len-1] == '\n') 
-    {
-      (s->str)[s->len-1] = '\0';
-      --(s->len);
-      return (s->len + 1);
-    }
-      
-  /* case 3) incomplete string
-   */
-  for (;;) {
-    
-    if (maxlen > 0 && (s->siz+SH_STRING_PARCEL) > maxlen)
-      {
-        if (s->siz < maxlen)
-          sh_string_grow(s, (maxlen-s->siz));
-        else
-          return -2;
-      }
-    else
-      {
-        sh_string_grow(s, 0);
-      }
-    
-    if (sh_str_fgets(&(s->str[s->len]), (s->siz - s->len), fp) == NULL) 
-      {
-        if (ferror(fp))
-          {
-            sh_string_truncate(s, 0);
-            return -1;
-          }
-        return s->len;
-      }
-    
-    s->len += strlen( &(s->str[s->len]) );
-    if (s->len > 0 && s->str[s->len-1] == '\n')
-      {
-        (s->str)[s->len-1] = '\0';
-        --(s->len);
-        return (s->len + 1);
-      }
-  }
-
-  /* notreached */
-}
-
-sh_string * sh_string_cat_lchar(sh_string * s, const char * str, size_t len)
-{
-  if (sl_ok_adds(len, s->siz) == S_TRUE)
-    {
-      if ((len + 1 + s->len) > s->siz)
-	{
-	  sh_string_grow(s, ((len+1+s->len) - s->siz) );
-	}
-      memcpy(&(s->str[s->len]), str, len);
-      s->len += len;
-      s->str[s->len] = '\0';
-      return s;
-    }
-
-  return NULL;
-}
-
-sh_string * sh_string_set_from_char(sh_string * s, const char * str)
-{
-  size_t len = strlen(str);
-
-  if ((len+1) > s->siz)
-    {
-      sh_string_grow(s, ((len+1) - s->siz) );
-    }
-  memcpy(s->str, str, (len+1));
-  s->len = len;
-  return s;
-}
-
-sh_string * sh_string_add_from_char(sh_string * s, const char * str)
-{
-  size_t len   = strlen(str);
-  size_t avail = (s->siz - s->len);
-
-  if ((len+1) > avail)
-    {
-      (void) sh_string_grow(s, ((len+1) - avail) );
-    }
-  memcpy(&(s->str[s->len]), str, (len+1));
-  s->len += len;
-  return s;
-}
-
-sh_string * sh_string_new_from_lchar(const char * str, size_t len)
-{
-  sh_string * s;
-  s      = SH_ALLOC(sizeof(sh_string));
-  s->str = SH_ALLOC(len+1);
-  if (str)
-    memcpy(s->str, str, len);
-  else
-    s->str[0] = '\0';
-  s->str[len] = '\0';
-  s->siz = len+1;
-  s->len = len;
-  return s;
-}
-
-sh_string * sh_string_new_from_lchar3(const char * str1, size_t len1,
-                                      const char * str2, size_t len2,
-                                      const char * str3, size_t len3)
-{
-  sh_string * s;
-  size_t len = 0;
-
-  if (sl_ok_adds(len1, len2) == S_TRUE)
-    len    = len1 + len2;
-  else
-    return NULL;
-  if (sl_ok_adds( len, len3) == S_TRUE)
-    len    = len  + len3;
-  else
-    return NULL;
-
-  s      = SH_ALLOC(sizeof(sh_string));
-  s->str = SH_ALLOC(len+1);
-
-  memcpy(s->str, str1, len1);
-  memcpy(&s->str[len1], str2, len2);
-  memcpy(&s->str[len1+len2], str3, len3);
-
-  s->str[len] = '\0';
-  s->siz = len+1;
-  s->len = len;
-  return s;
-}
-
-sh_string * sh_string_grow(sh_string * s, size_t increase)
-{
-  char * new;
-
-  if (increase == 0)
-    increase = SH_STRING_PARCEL;
-  
-  if (s && sl_ok_adds(s->siz, increase) == S_TRUE)
-    {
-      new = SH_ALLOC(s->siz + increase);
-
-      if (s->str)
-        {
-          memcpy(new, s->str, s->len+1);
-          SH_FREE(s->str);
-        }
-      else
-        {
-          new[0] = '\0';
-        }
-      s->str  = new;
-      s->siz += increase;
-      return s;
-    }
-  return NULL;
-}
-
-sh_string * sh_string_truncate(sh_string * s, size_t len)
-{
-  if (s)
-    {
-      if (s->str && (s->len > len) )
-        {
-          s->len            = len;
-          (s->str)[len]     = '\0';
-        }
-      return s;
-    }
-  return NULL;
-}
-
-void sh_string_destroy(sh_string ** s)
-{
-  if (s)
-    {
-      if ((*s) && (*s)->str)
-        SH_FREE ((*s)->str);
-      SH_FREE(*s);
-      *s = NULL;
-    }
-  return;
-}
-
-sh_string * sh_string_new(size_t size)
-{
-  sh_string * s;
-  s      = SH_ALLOC (sizeof(sh_string));
-  if (size == 0)
-    size = SH_STRING_PARCEL;
-  s->str = SH_ALLOC (size);
-  s->str[0] = '\0';
-  s->siz = size;
-  s->len = 0;
-  return s;
-}
-
-/* Replaces fields in s with 'replacement'. Fields are given
- * in the ordered array ovector, comprising ovecnum pairs 
- * ovector[i], ovector[i+1] which list offset of first char
- * of field, offset of first char after field (this is how
- * the pcre library does it).
- */
-#define IS_PCRE2_UNSET           (~(size_t)0)
-
-sh_string * sh_string_replace(const sh_string * s, 
-                              const size_t * ovector, int ovecnum, 
-                              const char * replacement, size_t rlen)
-{
-  sh_string * r = NULL;
-  char * p;
-  long   tlen;
-  size_t len;
-  int    end    = 0;
-  int    start  = 0;
-  size_t oldlen = 0;
-  size_t newlen = 0;
-  long   diff;
-  int    i, curr, last;
-
-  for (i = 0; i < ovecnum; ++i)
-    {
-      start = ovector[2*i];       /* offset of first char of substring       */
-      if (start >= end)
-        {
-          end   = ovector[2*i+1]; /* offset of first char after substring end*/
-
-          if (end > start && (unsigned int)end <= (s->len + 1))
-            {
-              oldlen += (end - start);
-              newlen += rlen;
-            }
-          else                    /* inconsistency detected                  */
-            {
-              return NULL;
-            }
-        }
-      else                        /* overlap detected                        */
-        {
-          return NULL;
-        }
-    }
-
-  diff = newlen - oldlen;
-
-  if ((diff > 0) && ((s->len + 1 + diff) > s->siz))
-    {
-      r = sh_string_new_from_lchar(sh_string_str(s), 
-                                   sh_string_len(s));
-      r = sh_string_grow(r, diff);
-    }
-  else
-    {
-      r = sh_string_new_from_lchar(sh_string_str(s), 
-                                   sh_string_len(s));
-    }
-
-
-  curr = -1;
-
-  for (i = 0; i < ovecnum; ++i)
-    {
-      if (ovector[2*i] != IS_PCRE2_UNSET)
-        {
-          curr = 2*i;
-          break;
-        }
-    }
-  
-  if (r && ovecnum > 0 && ovector[curr] != IS_PCRE2_UNSET)
-    {
-      r->len = 0; r->str[0] = '\0'; p = r->str;
-
-      /* First part, until start of first replacement 
-       */
-      if (r->siz > ovector[curr]) {
-	memcpy(p, s->str, ovector[curr]); 
-	p += ovector[curr]; 
-	r->len += ovector[curr];
-      }
-      if (r->siz > (r->len + rlen)) {
-	memcpy(p, replacement,    rlen); 
-	p += rlen;
-	r->len += rlen;
-      }
-      *p = '\0';
-
-      last = curr + 1;
-
-      for (i = 1; i < ovecnum; ++i)
-        {
-          if (ovector[2*i] == IS_PCRE2_UNSET)
-            continue;
-
-          curr = 2*i;
-
-          /* From end of last replacement to start of this */
-          tlen = (long)(ovector[curr] - ovector[last]);
-          if (tlen >= 0)
-            {
-              len = (size_t) tlen;
-
-              if (tlen > 0 && r->siz > (r->len + len) /* && &(s->str[ovector[last]]) always true */ )
-                {
-                  memcpy(p, &(s->str[ovector[last]]), (size_t)len);
-                  p += len;
-		  r->len += len; 
-                }
-              
-              /* The replacement */
-	      if (r->siz > (r->len + rlen)) {
-		memcpy(p, replacement, rlen);       
-		p += rlen;
-		r->len += rlen;
-	      }
-              
-              /* null terminate */
-              *p = '\0';
-
-              last = curr + 1;
-            }
-	}
-
-      /* Last part, after last replacement; includes terminating null 
-       */
-      if (last > 0)
-        {
-          /* If not, nothing has been replaced, and r is still a copy of s
-           */
-          tlen = (long)((s->len + 1) - ovector[last]);
-          if (tlen > 0)
-            {
-              len = (size_t)tlen;
-	      if (r->siz >= (r->len + len) /* && &(s->str[ovector[2*i -1]]) always true */ ) {
-		memcpy(p, &(s->str[ovector[2*i -1]]), (size_t)len);
-		p += (len - 1); 
-		r->len += (len - 1);
-		*p = '\0'; 
-	      }
-            }
-        }
-
-    }
-
-  return r;
-}
-
-
-#ifdef SH_CUTEST
-#include <stdlib.h>
-#include "CuTest.h"
-
-void Test_string (CuTest *tc) {
-  int status, i, max = 120;
-  FILE * fp;
-  sh_string * s = NULL;
-  sh_string * t;
-  static char template[] = "/tmp/xtest.XXXXXX";
-  char ** array;
-  char test[128];
-  size_t lengths[16];
-  unsigned int iarr;
-  size_t ovector[16];
-  int ovecnum;
-
-  s = sh_string_new(0);
-  CuAssertPtrNotNull(tc, s);
-  sh_string_destroy(&s);
-  CuAssertTrue(tc, s == NULL);
-
-  s = sh_string_new(0);
-  CuAssertPtrNotNull(tc, s);
-
-  status = mkstemp(template);
-  CuAssertTrue(tc, status >= 0);
-
-  fp = fdopen(status, "r+");
-  CuAssertPtrNotNull(tc, fp);
-
-  for (i = 0; i <  80; ++i) { fputc ('a', fp); } fputc ('\n', fp); /* 0 */
-  for (i = 0; i < 118; ++i) { fputc ('a', fp); } fputc ('\n', fp); /* 1 */
-  for (i = 0; i < 119; ++i) { fputc ('a', fp); } fputc ('\n', fp); /* 2 */
-  for (i = 0; i < 120; ++i) { fputc ('a', fp); } fputc ('\n', fp); /* 3 */
-  for (i = 0; i < 121; ++i) { fputc ('a', fp); } fputc ('\n', fp); /* 4 */
-  for (i = 0; i < 238; ++i) { fputc ('a', fp); } fputc ('\n', fp);
-  for (i = 0; i < 239; ++i) { fputc ('a', fp); } fputc ('\n', fp);
-  for (i = 0; i < 240; ++i) { fputc ('a', fp); } fputc ('\n', fp);
-  for (i = 0; i < 241; ++i) { fputc ('a', fp); } fputc ('\n', fp);
-
-  rewind(fp);
-
-  for (i = 0; i < 9; ++i)
-    {
-      status = sh_string_read(s, fp, max);
-
-      switch (i) {
-      case 0:
-	CuAssertTrue(tc, s->len ==  80);
-	CuAssertTrue(tc, s->siz == 120);
-	CuAssertTrue(tc, status ==  81);
-	break;
-      case 1:
-	CuAssertTrue(tc, s->len == 118);
-	CuAssertTrue(tc, s->siz == 120);
-	CuAssertTrue(tc, status == 119);
-	break;
-      case 2:
-	CuAssertTrue(tc, s->len == 119);
-	CuAssertTrue(tc, s->siz == 120);
-	CuAssertTrue(tc, status ==  -2); /* no terminating '\n', truncated */
-	break;
-      case 3:
-	CuAssertTrue(tc, s->len == 120);
-	CuAssertTrue(tc, s->siz == 240);
-	CuAssertTrue(tc, status == 121);
-	break;
-      case 4:
-	CuAssertTrue(tc, s->len == 121);
-	CuAssertTrue(tc, s->siz == 240);
-	CuAssertTrue(tc, status == 122);
-	break;
-      case 5:
-	CuAssertTrue(tc, s->len == 238);
-	CuAssertTrue(tc, s->siz == 240);
-	CuAssertTrue(tc, status == 239);
-	break;
-      case 6:
-	CuAssertTrue(tc, s->len == 239);
-	CuAssertTrue(tc, s->siz == 240);
-	CuAssertTrue(tc, status ==  -2); /* no terminating '\n', truncated */
-	break;
-      default:
-	CuAssertTrue(tc, s->len == 239);
-	CuAssertTrue(tc, s->siz == 240);
-	CuAssertTrue(tc, status ==  -2);
-      }
-      if (status == -2) /* read in rest of string */
-        { max = 240; sh_string_read(s, fp, max); }
-    }
-
-  rewind(fp);
-
-  sh_string_truncate(s, 0);
-  CuAssertTrue(tc, s->len == 0);
-
-  for (i = 0; i < 9; ++i)
-    {
-      status = sh_string_read(s, fp, 240);
-      if (status == -2)
-        sh_string_read(s, fp, 240);
-      else
-        {
-          for (status = 0; status < (int)s->len; ++status)
-            {
-              if (s->str[status] != 'a')
-                {
-                  CuFail(tc, "unexpected character");
-                }
-            }
-        }
-    }
-
-  status = fclose(fp); 
-  CuAssertTrue(tc, status == 0);
-  status = remove(template);
-  CuAssertTrue(tc, status == 0);
-
-  iarr = 10; strcpy(test, "|a1|| a2| |a3 |a4|a5|");
-  array  = split_array(test, &iarr, '|', lengths);
-  CuAssertIntEquals(tc,9,(int)iarr);
-  CuAssertStrEquals(tc,"",   array[0]);
-  CuAssertStrEquals(tc,"a1", array[1]);
-  CuAssertStrEquals(tc,"",   array[2]);
-  CuAssertStrEquals(tc,"a2", array[3]);
-  CuAssertStrEquals(tc,"",   array[4]);
-  CuAssertStrEquals(tc,"a3", array[5]);
-  CuAssertStrEquals(tc,"a4", array[6]);
-  CuAssertStrEquals(tc,"a5", array[7]);
-  CuAssertStrEquals(tc,"",   array[8]);
-
-  CuAssertIntEquals(tc, 0, (int)lengths[0]);
-  CuAssertIntEquals(tc, 2, (int)lengths[1]);
-  CuAssertIntEquals(tc, 0, (int)lengths[2]);
-  CuAssertIntEquals(tc, 2, (int)lengths[3]);
-  CuAssertIntEquals(tc, 0, (int)lengths[4]);
-  CuAssertIntEquals(tc, 2, (int)lengths[5]);
-  CuAssertIntEquals(tc, 2, (int)lengths[6]);
-  CuAssertIntEquals(tc, 2, (int)lengths[7]);
-  CuAssertIntEquals(tc, 0, (int)lengths[8]);
-
-  iarr = 10; strcpy(test, "a1|| a2| |a3 |a4|a5|");
-  array  = split_array(test, &iarr, '|', lengths);
-  CuAssertIntEquals(tc,8,(int)iarr);
-  CuAssertStrEquals(tc,"a1", array[0]);
-  CuAssertStrEquals(tc,"",   array[1]);
-  CuAssertStrEquals(tc,"a2", array[2]);
-  CuAssertStrEquals(tc,"",   array[3]);
-  CuAssertStrEquals(tc,"a3", array[4]);
-  CuAssertStrEquals(tc,"a4", array[5]);
-  CuAssertStrEquals(tc,"a5", array[6]);
-  CuAssertStrEquals(tc,"",   array[7]);
-
-  CuAssertIntEquals(tc, 2, (int)lengths[0]);
-  CuAssertIntEquals(tc, 0, (int)lengths[1]);
-  CuAssertIntEquals(tc, 2, (int)lengths[2]);
-  CuAssertIntEquals(tc, 0, (int)lengths[3]);
-  CuAssertIntEquals(tc, 2, (int)lengths[4]);
-  CuAssertIntEquals(tc, 2, (int)lengths[5]);
-  CuAssertIntEquals(tc, 2, (int)lengths[6]);
-  CuAssertIntEquals(tc, 0, (int)lengths[7]);
-
-  iarr = 10; strcpy(test, "  a1|| a2  | |a3 |a4|a5");
-  array  = split_array(test, &iarr, '|', lengths);
-  CuAssertIntEquals(tc,7,(int)iarr);
-  CuAssertStrEquals(tc,"a1", array[0]);
-  CuAssertStrEquals(tc,"",   array[1]);
-  CuAssertStrEquals(tc,"a2", array[2]);
-  CuAssertStrEquals(tc,"",   array[3]);
-  CuAssertStrEquals(tc,"a3", array[4]);
-  CuAssertStrEquals(tc,"a4", array[5]);
-  CuAssertStrEquals(tc,"a5", array[6]);
-
-  CuAssertIntEquals(tc, 2, (int)lengths[0]);
-  CuAssertIntEquals(tc, 0, (int)lengths[1]);
-  CuAssertIntEquals(tc, 2, (int)lengths[2]);
-  CuAssertIntEquals(tc, 0, (int)lengths[3]);
-  CuAssertIntEquals(tc, 2, (int)lengths[4]);
-  CuAssertIntEquals(tc, 2, (int)lengths[5]);
-  CuAssertIntEquals(tc, 2, (int)lengths[6]);
-
-  iarr = 10; strcpy(test, "a1|| a2  | |a3 |a4|a5  ");
-  array  = split_array(test, &iarr, '|', lengths);
-  CuAssertIntEquals(tc,7,(int)iarr);
-  CuAssertStrEquals(tc,"a1", array[0]);
-  CuAssertStrEquals(tc,"",   array[1]);
-  CuAssertStrEquals(tc,"a2", array[2]);
-  CuAssertStrEquals(tc,"",   array[3]);
-  CuAssertStrEquals(tc,"a3", array[4]);
-  CuAssertStrEquals(tc,"a4", array[5]);
-  CuAssertStrEquals(tc,"a5", array[6]);
-
-  CuAssertIntEquals(tc, 2, (int)lengths[0]);
-  CuAssertIntEquals(tc, 0, (int)lengths[1]);
-  CuAssertIntEquals(tc, 2, (int)lengths[2]);
-  CuAssertIntEquals(tc, 0, (int)lengths[3]);
-  CuAssertIntEquals(tc, 2, (int)lengths[4]);
-  CuAssertIntEquals(tc, 2, (int)lengths[5]);
-  CuAssertIntEquals(tc, 2, (int)lengths[6]);
-
-  iarr = 10; strcpy(test, "|");
-  array  = split_array(test, &iarr, '|', lengths);
-  CuAssertIntEquals(tc,2,(int)iarr);
-  CuAssertStrEquals(tc,"",   array[0]);
-  CuAssertStrEquals(tc,"",   array[1]);
-
-  CuAssertIntEquals(tc, 0, (int)lengths[0]);
-  CuAssertIntEquals(tc, 0, (int)lengths[1]);
-
-  iarr = 10; strcpy(test, "|||");
-  array  = split_array(test, &iarr, '|', lengths);
-  CuAssertIntEquals(tc,4,(int)iarr);
-  CuAssertStrEquals(tc,"",   array[0]);
-  CuAssertStrEquals(tc,"",   array[1]);
-  CuAssertStrEquals(tc,"",   array[2]);
-  CuAssertStrEquals(tc,"",   array[3]);
-
-  CuAssertIntEquals(tc, 0, (int)lengths[0]);
-  CuAssertIntEquals(tc, 0, (int)lengths[1]);
-  CuAssertIntEquals(tc, 0, (int)lengths[2]);
-  CuAssertIntEquals(tc, 0, (int)lengths[3]);
-
-  iarr = 10; strcpy(test, " a1 ");
-  array  = split_array(test, &iarr, '|', lengths);
-  CuAssertIntEquals(tc,1,(int)iarr);
-  CuAssertStrEquals(tc,"a1",   array[0]);
-
-  CuAssertIntEquals(tc, 2, (int)lengths[0]);
-
-  iarr = 10; strcpy(test, "");
-  array  = split_array(test, &iarr, '|', lengths);
-  CuAssertIntEquals(tc,1,(int)iarr);
-  CuAssertStrEquals(tc,"",   array[0]);
-
-  CuAssertIntEquals(tc, 0, (int)lengths[0]);
-
-  /* WS separated */
-
-  iarr = 10; strcpy(test, "a1");
-  array  = split_array_ws (test, &iarr, lengths);
-  CuAssertIntEquals(tc,1,(int)iarr);
-  CuAssertStrEquals(tc,"a1",   array[0]);
-
-  CuAssertIntEquals(tc, 2, (int)lengths[0]);
-
-  iarr = 10; strcpy(test, " a1");
-  array  = split_array_ws (test, &iarr, lengths);
-  CuAssertIntEquals(tc,1,(int)iarr);
-  CuAssertStrEquals(tc,"a1",   array[0]);
-
-  CuAssertIntEquals(tc, 2, (int)lengths[0]);
-
-  iarr = 10; strcpy(test, " a1 ");
-  array  = split_array_ws (test, &iarr, lengths);
-  CuAssertIntEquals(tc,1,(int)iarr);
-  CuAssertStrEquals(tc,"a1",   array[0]);
-
-  CuAssertIntEquals(tc, 2, (int)lengths[0]);
-
-  iarr = 10; strcpy(test, "   ");
-  array  = split_array_ws (test, &iarr, lengths);
-  CuAssertIntEquals(tc,0,(int)iarr);
-  CuAssertTrue(tc, array[0] == NULL);
-
-  iarr = 10; strcpy(test, " a1 a2");
-  array  = split_array_ws (test, &iarr, lengths);
-  CuAssertIntEquals(tc,2,(int)iarr);
-  CuAssertStrEquals(tc,"a1",   array[0]);
-  CuAssertStrEquals(tc,"a2",   array[1]);
-
-  CuAssertIntEquals(tc, 2, (int)lengths[0]);
-  CuAssertIntEquals(tc, 2, (int)lengths[1]);
-
-  iarr = 10; strcpy(test, " a1  a2  ");
-  array  = split_array_ws (test, &iarr, lengths);
-  CuAssertIntEquals(tc,2,(int)iarr);
-  CuAssertStrEquals(tc,"a1",   array[0]);
-  CuAssertStrEquals(tc,"a2",   array[1]);
-
-  CuAssertIntEquals(tc, 2, (int)lengths[0]);
-  CuAssertIntEquals(tc, 2, (int)lengths[1]);
-
-  iarr = 10; strcpy(test, "");
-  array  = split_array_ws (test, &iarr, lengths);
-  CuAssertIntEquals(tc,0,(int)iarr);
-  CuAssertTrue(tc, array[0] == NULL);
-
-  iarr = 3; strcpy(test, " this is a test for remainder");
-  array  = split_array_ws (test, &iarr, lengths);
-  CuAssertIntEquals(tc,3,(int)iarr);
-  CuAssertStrEquals(tc,"this",   array[0]);
-  CuAssertStrEquals(tc,"is",     array[1]);
-  CuAssertStrEquals(tc,"a test for remainder",     array[2]);
-  for (i = 0; i < 3; ++i)
-    {
-      CuAssertIntEquals(tc, (int)strlen(array[i]), lengths[i] );
-    }
-
-  /* split_array_list */
-  iarr = 6; strcpy(test, " this(a) is_a(test);for(b),remainder(test)foo(bar)");
-  array  = split_array_list (test, &iarr, lengths);
-  CuAssertIntEquals(tc,3,(int)iarr);
-  CuAssertStrEquals(tc,"this(a)",   array[0]);
-  CuAssertStrEquals(tc,"is_a(test);for(b)",     array[1]);
-  CuAssertStrEquals(tc,"remainder(test)foo(bar)",     array[2]);
-  for (i = 0; i < 3; ++i)
-    {
-      CuAssertIntEquals(tc, (int)strlen(array[i]), lengths[i] );
-    }
-
-  
-  /* string replace */
-  s = sh_string_new_from_lchar3 ("abc ", 4, "def ", 4, "ghi ", 4);
-  ovecnum = 2;
-  ovector[0] = 0;  ovector[1] = 2;
-  ovector[2] = 4;  ovector[3] = 11;
-
-  t = sh_string_replace(s, ovector, ovecnum, 
-                        "___", 3);
-  CuAssertPtrNotNull(tc, t);
-  CuAssertStrEquals(tc, "___c ___ ",   t->str);
-  CuAssertIntEquals(tc, 9, (int)t->len);
-
-  ovector[0] = 0;  ovector[1] = 2;
-  ovector[2] = 4;  ovector[3] = 12; 
-  t = sh_string_replace(s, ovector, ovecnum, 
-                        "___", 3);
-  CuAssertPtrNotNull(tc, t);
-  CuAssertStrEquals(tc, "___c ___",   t->str);
-  CuAssertIntEquals(tc, 8, (int)t->len);
-
-  ovector[0] = 0;  ovector[1] = 0;
-  ovector[2] = 0;  ovector[3] = 0; 
-  t = sh_string_replace(s, ovector, ovecnum, 
-                        "___", 3);
-  CuAssertTrue(tc, t == NULL);
-
-  ovector[0] = 0;  ovector[1] = 3;
-  ovector[2] = 3;  ovector[3] = 6; 
-  t = sh_string_replace(s, ovector, ovecnum, 
-                        "___", 3);
-  
-  CuAssertPtrNotNull(tc, t);
-  CuAssertStrEquals(tc, "______f ghi ",   t->str);
-  CuAssertIntEquals(tc, 12, (int)t->len);
-
-  ovector[0] = 4;  ovector[1] = 5;
-  ovector[2] = 11;  ovector[3] = 12; 
-  t = sh_string_replace(s, ovector, ovecnum, 
-                        "___", 3);
-  CuAssertPtrNotNull(tc, t);
-  CuAssertStrEquals(tc, "abc ___ef ghi___",   t->str);
-  CuAssertIntEquals(tc, 16, (int)t->len);
-
-  t = sh_string_replace(s, ovector, 0, 
-                        "___", 3);
-  CuAssertPtrNotNull(tc, t);
-  CuAssertStrEquals(tc, s->str,   t->str);
-  CuAssertIntEquals(tc, (int)s->len, (int)t->len);
-
-}
-
-#endif
Index: trunk/src/sh_sub.c
===================================================================
--- trunk/src/sh_sub.c	(revision 591)
+++ 	(revision )
@@ -1,525 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 2011 Rainer Wichmann                                      */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-#include "config_xor.h"
-
-/* 0->1 for debug */ 
-#if 0
-#define SH_SUB_DBG 1
-#endif
-
-#ifndef NULL
-#if !defined(__cplusplus)
-#define NULL ((void*)0)
-#else
-#define NULL (0)
-#endif
-#endif
-
-
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
-#include <limits.h>
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/wait.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <poll.h>
-
-#include "samhain.h"
-#include "sh_pthread.h"
-
-#ifndef HAVE_LSTAT
-#define lstat stat
-#endif
-
-#define FIL__ _("sh_sub.c")
-
-static pid_t sh_child_pid = -1;
-static pid_t sh_wait_ret  =  1;
-
-static int parent2child[2];
-static int child2parent[2];
-
-SH_MUTEX_STATIC(mutex_sub,      PTHREAD_MUTEX_INITIALIZER);
-SH_MUTEX_STATIC(mutex_sub_work, PTHREAD_MUTEX_INITIALIZER);
-
-static void wait_for_command();
-static ssize_t sh_sub_read(int fd, void *buf, size_t count);
-
-void sh_kill_sub()
-{
-  SH_MUTEX_LOCK(mutex_sub);
-
-  if (sh_child_pid != -1)
-    {
-      int status;
-#ifdef WCONTINUED
-      int wflags = WNOHANG|WUNTRACED|WCONTINUED;
-#else
-      int wflags = WNOHANG|WUNTRACED;
-#endif
-
-      close (parent2child[1]);
-      close (child2parent[0]);
-
-      /* Let's be rude. */
-      kill(sh_child_pid, SIGKILL);
-
-      retry_msleep(1,0);
-
-      if (sh_wait_ret == 0)
-	sh_wait_ret = waitpid(          -1, &status, wflags);
-      else
-	sh_wait_ret = waitpid(sh_child_pid, &status, wflags);
-
-      sh_child_pid = -1;
-    }
-
-  SH_MUTEX_UNLOCK(mutex_sub);
-  return;
-}
-
-static int sh_create_sub()
-{
-  pid_t res;
-  volatile int   retval = 0;
-
-  SH_MUTEX_LOCK(mutex_sub);
-
-#if !defined(O_NONBLOCK)
-#if defined(O_NDELAY)
-#define O_NONBLOCK  O_NDELAY
-#else
-#define O_NONBLOCK  0
-#endif
-#endif
-
-  if (sh_child_pid == -1)
-    {
-      sigset_t signal_set_new;
-      sigset_t signal_set_old;
-
-      sigfillset ( &signal_set_new );
-      sigemptyset( &signal_set_old );
-
-      /* Create pipes. */
-      res = pipe (parent2child);
-      if (res == 0)
-	res = pipe (child2parent);
-      
-      if (res != 0)
-	goto out;
-
-      SH_SETSIGMASK(SIG_BLOCK, &signal_set_new, &signal_set_old);
-
-      res = fork();
-      
-      if (res == 0)
-	{
-	  /* Child process. */
-#ifdef _SC_OPEN_MAX
-	  int fdlimit = sysconf (_SC_OPEN_MAX);
-#else
-#ifdef OPEN_MAX
-	  int fdlimit = OPEN_MAX;
-#else
-	  int fdlimit = _POSIX_OPEN_MAX;
-#endif
-#endif
-	  int sflags, i, fd = 0;
-	  struct sigaction act;
-
-	  /* zero private information 
-	   */
-	  memset(skey, 0, sizeof(sh_key_t)); 
-
-	  close (parent2child[1]);
-	  close (child2parent[0]);
-	  
-	  sflags = fcntl(parent2child[0], F_GETFL, 0);
-	  fcntl(parent2child[0], F_SETFL, sflags | O_NONBLOCK);
-	  sflags = fcntl(child2parent[1], F_GETFL, 0);
-	  fcntl(child2parent[1], F_SETFL, sflags | O_NONBLOCK);
-
-	  /* close inherited file descriptors 
-	   */
-	  if (fdlimit < 0) 
-	    fdlimit = 20;  /* POSIX lower limit */
-	  while (fd < fdlimit)
-	    {
-	      if (fd != parent2child[0] && fd != child2parent[1])
-		close(fd);
-	      ++fd;
-	    }
-
-	  /*
-	  for (i = 0; i < 3; ++i)
-	    {
-	      if ( fcntl(i, F_GETFL, 0) == (-1))
-		(void) open(_("/dev/null"), O_RDWR, 0);
-	    }
-	  */
-
-	  /* reset signal handling 
-	   */
-	  act.sa_handler = SIG_DFL;
-	  act.sa_flags   = 0;
-	  sigemptyset( &act.sa_mask );
-	  
-	  for (i = 1; i < NSIG; ++i)
-	    sigaction(i, &act, NULL);
-	  SH_SETSIGMASK(SIG_UNBLOCK, &signal_set_new, NULL);
-
-	  wait_for_command();
-	  
-	  _exit(0);
-	}
-      else if (res > 0)
-	{
-	  /* Parent process. */
-	  int sflags;
-
-	  SH_SETSIGMASK(SIG_SETMASK, &signal_set_old, NULL);
-
-	  close (parent2child[0]);
-	  close (child2parent[1]);
-	  
-	  sflags = fcntl(parent2child[1], F_GETFL, 0);
-	  fcntl(parent2child[1], F_SETFL, sflags | O_NONBLOCK);
-	  sflags = fcntl(child2parent[0], F_GETFL, 0);
-	  fcntl(child2parent[0], F_SETFL, sflags | O_NONBLOCK);
-
-	  sh_child_pid = res;
-	}
-      else
-	{
-	  /* Failure. */
-
-	  SH_SETSIGMASK(SIG_SETMASK, &signal_set_old, NULL);
-
-	  close (parent2child[0]);
-	  close (parent2child[1]);
-
-	  close (child2parent[0]);
-	  close (child2parent[1]);
-	  
-	  retval = -1;
-	}
-    }
-
- out:
-  ; /* 'label at end of compound statement' */
-  SH_MUTEX_UNLOCK(mutex_sub);
-  return retval;
-}
-
-#define  SH_SUB_BUF (PIPE_BUF-1)
-struct sh_sub_in {
-  char   command;
-  char   path[SH_SUB_BUF];
-};
-
-struct sh_sub_out {
-  int retval;
-  int errnum;
-  struct stat sbuf;
-};
-
-#define SH_COM_STAT  0
-#define SH_COM_LSTAT 1
-
-static ssize_t sh_sub_write(int fd, const void *buf, size_t count)
-{
-  const char * mbuf = (const char *) buf;
-  ssize_t rcount;
-  int ttl = 5; /* 0, 1, 9, 81, 729 millisec */
-  int tti = 1; 
-
-  do {
-
-    rcount = write(fd, mbuf, count);
-    if (rcount > 0) 
-      {
-	count -= rcount; mbuf += rcount; --ttl;
-      }
-
-    if (count > 0)
-      {
-	if (ttl > 0)
-	  {
-	    retry_msleep(0, tti);
-	    tti *= 9;
-	  }
-	else
-	  {
-	    return -1;
-	  }
-      }
-  } while (count > 0 && (errno == EAGAIN || errno == EWOULDBLOCK));
-
-  if (count > 0)
-    return -1;
-  return 0;
-}
-
-static void wait_for_command()
-{
-  int               ret;
-  struct pollfd     fds;
-  struct sh_sub_in  inbuf;
-  struct sh_sub_out outbuf;
-
-  fds.fd     = parent2child[0];
-  fds.events = POLLIN;
-
-  do {
-
-    do {
-      ret = poll(&fds, 1, -1);
-    } while (ret < 0 && errno == EINTR);
-
-    if (ret > 0)
-      {
-	ret = sh_sub_read(parent2child[0], &inbuf, sizeof(inbuf));
-
-	if (ret == 0)
-	  {
-	    if (inbuf.command == SH_COM_LSTAT)
-	      {
-		do { 
-		  outbuf.retval = lstat(inbuf.path, &(outbuf.sbuf)); 
-		} while (outbuf.retval < 0 && errno == EAGAIN);
-	      }
-	    else
-	      {
-		do { 
-		  outbuf.retval = stat(inbuf.path, &(outbuf.sbuf)); 
-		} while (outbuf.retval < 0 && errno == EAGAIN);
-	      }
-
-	    outbuf.errnum = errno;
-
-	    ret = sh_sub_write(child2parent[1], &outbuf, sizeof(outbuf));
-	    if (ret < 0)
-	      {
-		return;
-	      }
-	  }
-	else /* sh_sub_read() < 0 */
-	  {
-	    return;
-	  }
-      }
-  } while (1 == 1);
-}
-
-#ifndef ETIMEDOUT
-#define ETIMEDOUT EIO
-#endif
-
-static ssize_t sh_sub_read(int fd, void *buf, size_t count)
-{
-  char * mbuf = (char *) buf;
-  ssize_t rcount;
-  int ttl = 5; /* 0, 1, 9, 81, 729 millisec */
-  int tti = 1; 
-
-  do {
-    rcount = read(fd, mbuf, count);
-
-    if (rcount > 0) 
-      {
-	count -= rcount; mbuf += rcount; --ttl;
-      }
-
-    if (count > 0)
-      {
-	if (ttl > 0)
-	  {
-	    retry_msleep(0, tti);
-	    tti *= 9;
-	  }
-	else
-	  {
-	    if (rcount >= 0) 
-	      errno = ETIMEDOUT;
-	    return -1;
-	  }
-      }
-  } while (count > 0 && 
-	   (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR));
-
-  if (count > 0)
-    return -1;
-
-  return 0;
-}
-
-#ifdef SH_SUB_DBG
-#include <stdarg.h>
-static void debug_it (const char *fmt, ...)
-{
-  char msg[256];
-  va_list ap;
-
-  int fd = open("debug.it", O_CREAT|O_WRONLY|O_APPEND, 0666);
-
-  va_start(ap, fmt);
-  vsnprintf(msg, sizeof(msg), fmt, ap);  /* flawfinder: ignore */
-  va_end(ap);
-
-  write(fd, msg, strlen(msg));
-  write(fd, "\n", 1);
-  close(fd);
-  return;
-}
-#endif
-
-static int sh_sub_stat_int(const char *path, struct stat *buf, char command)
-{
-  int retval;
-  volatile int sflag = 0;
-  struct sh_sub_in  inbuf;
-  struct sh_sub_out outbuf;
-  struct pollfd     pfds;
-
-  size_t len = strlen(path) + 1;
-
-  if (len > SH_SUB_BUF)
-    {
-      if (command == SH_COM_LSTAT)
-	{
-	  do { 
-	    retval = lstat(path, buf); 
-	  } while (retval < 0 && errno == EAGAIN);
-
-	  return retval;
-	}
-      else
-	{
-	  do { 
-	    retval = stat(path, buf); 
-	  } while (retval < 0 && errno == EAGAIN);
-
-	  return retval;
-	}
-    }
-
-  sl_strlcpy(inbuf.path, path, SH_SUB_BUF);
-  inbuf.command = command;
-
- start:
-
-#ifdef SH_SUB_DBG
-  debug_it("%d sh_child_pid %d\n", (int)getpid(), (int) sh_child_pid);
-#endif
-
-  if (sh_child_pid == -1)
-    sh_create_sub();
-
-#ifdef SH_SUB_DBG
-  debug_it("%d stat_sub %s (%d)\n", (int)getpid(), inbuf.path, (int) sh_child_pid);
-#endif
-
-  SH_MUTEX_LOCK(mutex_sub_work);
-
-  retval = sh_sub_write(parent2child[1], &inbuf, sizeof(inbuf));
-  if (retval < 0)
-    {
-      int error = errno;
-      sh_kill_sub();
-      errno = error;
-      sflag = 1;
-      goto end;
-    }
-
-#ifdef SH_SUB_DBG
-  debug_it("%d stat_sub polling..\n", (int)getpid());
-#endif
-
-  pfds.fd     = child2parent[0];
-  pfds.events = POLLIN;
-
-  do {
-    retval = poll(&pfds, 1, 300 * 1000);
-  } while (retval < 0 && errno == EINTR);
-
-  if (retval <= 0)
-    {
-      int error = errno;
-      sh_kill_sub();
-      errno = (retval == 0) ? ETIMEDOUT : error;
-      sflag = -1;
-      goto end;
-    }
-
-#ifdef SH_SUB_DBG
-  debug_it("%d stat_sub reading..\n", (int)getpid());
-#endif
-
-  retval = sh_sub_read (child2parent[0], &outbuf, sizeof(outbuf));
-  if (retval < 0)
-    {
-      int error = errno;
-      sh_kill_sub();
-      errno = error;
-      sflag = 1;
-      goto end;
-    }
-
- end:
-  ; /* 'label at end of compound statement' */
-  SH_MUTEX_UNLOCK(mutex_sub_work);
-
-  if      (sflag == 0)
-    {
-#ifdef SH_SUB_DBG
-      debug_it("%d stat_sub done..\n", (int)getpid());
-#endif
-      memcpy(buf, &(outbuf.sbuf), sizeof(struct stat));
-      errno = outbuf.errnum;
-      return outbuf.retval;
-    }
-  else if (sflag == 1)
-    {
-#ifdef SH_SUB_DBG
-      debug_it("%d stat_sub error..\n", (int)getpid());
-#endif
-      /* could not read, thus subprocess may have gone */
-      sflag = 0;
-      goto start;
-    }
-
-  return -1;
-}
-
-int sh_sub_stat (const char *path, struct stat *buf)
-{
-  return sh_sub_stat_int(path, buf, SH_COM_STAT);
-}
-
-int sh_sub_lstat(const char *path, struct stat *buf)
-{
-  return sh_sub_stat_int(path, buf, SH_COM_LSTAT);
-}
-
Index: trunk/src/sh_subuid.c
===================================================================
--- trunk/src/sh_subuid.c	(revision 591)
+++ 	(revision )
@@ -1,245 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 2018 Rainer Wichmann                                      */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-#include "config_xor.h"
-
-#undef  FIL__
-#define FIL__  _("sh_subuid.c")
-
-
-#include <sys/types.h>
-#include <time.h>
-#include <sys/stat.h>
-#include <unistd.h>
-
-#include <stdlib.h>
-#include <errno.h>
-#include <limits.h>
-
-#if defined(__linux__)
-
-#include "samhain.h"
-#include "sh_unix.h"
-
-#define SH_SUBUID_FILE _("/etc/subuid")
-#define SH_SUBGID_FILE _("/etc/subgid")
-
-struct subuid_t {
-  char name[32];
-  unsigned long first;
-  unsigned long last;
-  struct subuid_t * next;
-};
-
-static time_t last_subuid = 0;
-static time_t last_subgid = 0;
-
-struct subuid_t * list_subuid = NULL;
-struct subuid_t * list_subgid = NULL;
-
-/* Check whether we need to re-read the subuid/subgid file
- */
-static int needs_reread (char * file, time_t * last)
-{
-  int retval = S_FALSE;
-  struct stat   buf;
-  int status = retry_lstat (FIL__, __LINE__, file, &buf);
-
-  if (status == 0)
-    {
-      if ((buf.st_mtime - *last) > 1)
-	{
-	  *last = buf.st_mtime;
-	  retval = S_TRUE;
-	}
-    }
-  else if (status && errno == ENOENT)
-    {
-      /* If there was a file make sure we attempt to re-read
-       * to zero out the list. 
-       */
-      if (*last > 0) retval = S_TRUE;
-      *last = 0;
-    }
-  return retval;
-}
-
-/* free the whole list
- */
-static void free_subordinate(struct subuid_t * head)
-{
-  struct subuid_t * prev;
-  struct subuid_t * curr = head;
-  
-  while (curr)
-    {
-      prev = curr;
-      curr = curr->next;
-      SH_FREE(prev);
-    }
-  return;
-}
-
-#define NFIELDS_SUBUID 3
-
-static int get_ulong(char * str, unsigned long * result)
-{
-  char * endptr;
-
-  errno = 0;
-  *result = strtoul(str, &endptr, 0);
-  if (*str != '\0' && *endptr == '\0' && errno != ERANGE)
-    return S_TRUE;
-  return S_FALSE;
-}
-
-/* Parse a single line into name / startuid / lastuid 
- */
-static struct subuid_t * parse_subordinate(char * line)
-{
-  unsigned int      nfields = NFIELDS_SUBUID;
-  size_t            lengths[NFIELDS_SUBUID];
-  unsigned long     start, count;
-  struct subuid_t * new;
-  
-  char ** array = split_array(line, &nfields, ':', lengths);
-
-  if (nfields != NFIELDS_SUBUID)
-    { SH_FREE(array); return NULL; }
-
-  if (S_TRUE != get_ulong(array[1], &start))
-    { SH_FREE(array); return NULL; }
-  if ((S_TRUE != get_ulong(array[2], &count)) || (count == 0))
-    { SH_FREE(array); return NULL; }
-  if (lengths[0] == 0)
-    { SH_FREE(array); return NULL; }
-
-  /* we have checked that count != 0 */
-  --count;
-
-  if (start > (ULONG_MAX - count))
-    { SH_FREE(array); return NULL; }
-    
-  new = SH_ALLOC(sizeof(struct subuid_t));
-  sl_strlcpy(new->name, array[0], 32);
-  new->first = start;
-  new->last  = start + count; /* start+count-1, but we already did --count */
-  new->next  = NULL;
-
-  SH_FREE(array);
-  return new;
-}
-
-/* (re-)read the subuid/subgid file
- */
-static void reread_subordinate (char * file, struct subuid_t ** head_ref)
-{
-  SL_TICKET fd = (-1);
-  char      line[1024];
-  
-  if (*head_ref) { free_subordinate(*head_ref); *head_ref = NULL; }
-
-  fd = sl_open_read (FIL__, __LINE__, file, SL_YESPRIV);
-  if (!SL_ISERROR(fd))
-    {
-      while ( sh_unix_getline(fd, line, sizeof(line)) > 0 ) 
-        {
-	  /* for invalid lines, NULL will be returned 
-	   */
-	  struct subuid_t * new = parse_subordinate(line);
-
-	  if (new)
-	    {
-	      new->next = *head_ref;
-	      *head_ref = new;
-	    }
-        }
-      sl_close(fd);
-    }
-  return;
-}
-
-/* Return the username for a given subuid/subgid 
- */
-static char * get_name4id (unsigned long id, struct subuid_t * head)
-{
-  struct subuid_t * cur = head;
-  
-  while (cur)
-    {
-      if (id >= cur->first && id <= cur->last)
-	return cur->name;
-      cur = cur->next;
-    }
-  return NULL;
-}
-
-/***********************************************
- *
- * Public functions
- *
- ***********************************************/
-
-/* Returns username or NULL for a subuid
- */
-char * sh_get_subuid (unsigned long subuid)
-{
-  static int  init = 0;
-  static char file[256];
-
-  if (!init) { sl_strlcpy(file, SH_SUBUID_FILE, sizeof(file)); init = 1; }
-  
-  if (S_TRUE == needs_reread(file, &last_subuid))
-    reread_subordinate(file, &list_subuid); 
-  
-  return get_name4id (subuid, list_subuid);
-}
-
-/* Returns group name or NULL for subgid
- */
-char * sh_get_subgid (unsigned long subgid)
-{
-  static int  init = 0;
-  static char file[256];
-
-  if (!init) { sl_strlcpy(file, SH_SUBGID_FILE, sizeof(file)); init = 1; }
-  
-  if (S_TRUE == needs_reread(file, &last_subgid))
-    reread_subordinate(file, &list_subgid); 
-  
-  return get_name4id (subgid, list_subgid);
-}
-
-/* Not Linux, hence no sub(u|g)id
- */
-#else
-
-char * sh_get_subuid (unsigned long subuid)
-{
-  (void) subuid;
-  return NULL;
-}
-
-char * sh_get_subgid (unsigned long subgid)
-{
-  (void) subgid;
-  return NULL;
-}
-
-#endif
Index: trunk/src/sh_suidchk.c
===================================================================
--- trunk/src/sh_suidchk.c	(revision 591)
+++ trunk/src/sh_suidchk.c	(revision 1)
@@ -30,5 +30,7 @@
 #include <errno.h>
 #include <limits.h>
-
+#ifdef HAVE_LIBGEN_H
+#include <libgen.h>
+#endif
 #ifdef HAVE_SCHED_H
 #include <sched.h>
@@ -37,4 +39,8 @@
 #ifdef SH_USE_SUIDCHK
 
+#ifndef HAVE_BASENAME
+#define basename(a) ((NULL == strrchr(a, '/')) ? (a) : (strrchr(a, '/')))
+#endif
+
 #undef  FIL__
 #define FIL__  _("sh_suidchk.c")
@@ -42,8 +48,14 @@
 #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) 
 
+#if TIME_WITH_SYS_TIME
+#include <sys/time.h>
+#include <time.h>
+#else
 #if HAVE_SYS_TIME_H
 #include <sys/time.h>
-#endif
+#else
 #include <time.h>
+#endif
+#endif
 
 #ifdef HAVE_DIRENT_H
@@ -63,8 +75,6 @@
 #endif
 #endif
-#define NEED_ADD_DIRENT
 
 #include "samhain.h"
-#include "sh_pthread.h"
 #include "sh_utils.h"
 #include "sh_error.h"
@@ -72,10 +82,8 @@
 #include "sh_suidchk.h"
 #include "sh_hash.h"
-#include "sh_dbIO.h"
 #include "sh_unix.h"
 #include "sh_files.h"
 #include "sh_schedule.h"
 #include "sh_calls.h"
-#include "zAVLTree.h"
 
 
@@ -110,8 +118,4 @@
   },
   {
-    N_("suidchecknosuid"),
-    sh_suidchk_set_nosuid
-  },
-  {
     N_("suidcheckquarantinefiles"),
     sh_suidchk_set_quarantine
@@ -135,6 +139,5 @@
 static int     ShSuidchkActive   = S_TRUE;
 static time_t  ShSuidchkInterval = 7200;
-static unsigned long    ShSuidchkFps      = 0;
-static int     ShSuidchkNosuid   = S_FALSE;
+static long    ShSuidchkFps      = 0;
 static int     ShSuidchkYield    = S_FALSE;
 static int     ShSuidchkQEnable  = S_FALSE;
@@ -142,36 +145,13 @@
 static int     ShSuidchkQDelete  = S_FALSE;
 static int     ShSuidchkSeverity = SH_ERR_SEVERE;
+static char *  ShSuidchkExclude  = NULL;
+static int     ExcludeLen        = 0;
 
 static time_t  FileLimNow        = 0;
 static time_t  FileLimStart      = 0;
-static unsigned long    FileLimNum        = 0;
-static unsigned long    FileLimTotal      = 0;
+static long    FileLimNum        = 0;
+static long    FileLimTotal      = 0;
 
 static sh_schedule_t * ShSuidchkSched = NULL;
-
-
-static zAVLTree *  ShSuidchkExclude  = NULL;
-static void sh_suid_exclude_free()
-{
-  zAVL_string_reset(ShSuidchkExclude);
-  ShSuidchkExclude  = NULL;
-  return;
-}
-static int sh_suid_exclude_add(const char * str)
-{
-  size_t len;
-  int    ret;
-  char * key = sh_util_strdup(str);
-
-  len = sl_strlen (key);
-  if (len && key[len-1] == '/')
-    {
-      key[len-1] = '\0';
-    }
-  ret = zAVL_string_set(&ShSuidchkExclude, key);
-  SH_FREE(key);
-  return ret;
-}
-
 
 static char *
@@ -182,35 +162,8 @@
 #endif
 
-SH_MUTEX_STATIC(mutex_suid_check, PTHREAD_MUTEX_INITIALIZER);
-
 extern unsigned long sh_files_maskof (int class);
-
-static void set_defaults (void)
-{
-  ShSuidchkActive   = S_TRUE;
-  ShSuidchkInterval = 7200;
-  ShSuidchkFps      = 0;
-  ShSuidchkNosuid   = S_FALSE;
-  ShSuidchkYield    = S_FALSE;
-  ShSuidchkQEnable  = S_FALSE;
-  ShSuidchkQMethod  = SH_Q_CHANGEPERM;
-  ShSuidchkQDelete  = S_FALSE;
-  ShSuidchkSeverity = SH_ERR_SEVERE;
-  if (ShSuidchkExclude != NULL)
-    sh_suid_exclude_free();
-
-  FileLimNow        = 0;
-  FileLimStart      = 0;
-  FileLimNum        = 0;
-  FileLimTotal      = 0;
-
-  return;
-}
 
 /* Recursively descend into the directory to make sure that
  * there is no symlink in the path.
- *
- * Use retry_lstat_ns() here because we cannot chdir the subprocess
- * that does the lstat().
  */
 static int do_truncate_int (char * path, int depth)
@@ -220,13 +173,10 @@
   struct stat two;
   int         fd;
-  char errbuf[SH_ERRBUF_SIZE];
 
   if (depth > 99)
     {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
       sh_error_handle ((-1), FIL__, __LINE__, EINVAL,
 		       MSG_SUID_ERROR,
 		       _("do_truncate: max depth 99 exceeded"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
       return -1;
     }
@@ -234,9 +184,7 @@
   if (path[0] != '/')
     {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
       sh_error_handle ((-1), FIL__, __LINE__, EINVAL,
 		       MSG_SUID_ERROR,
 		       _("do_truncate: not an absolute path"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
       return -1;
     }
@@ -246,11 +194,9 @@
     {
       *q = '\0';
-      if (0 != retry_lstat_ns(FIL__, __LINE__, path, &one))
+      if (0 != retry_lstat(FIL__, __LINE__, path, &one))
 	{ 
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
 	  sh_error_handle ((-1), FIL__, __LINE__, errno,
 			   MSG_SUID_ERROR,
-			   sh_error_message(errno, errbuf, sizeof(errbuf)));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
+			   sh_error_message(errno));
 	  *q = '/'; 
 	  return -1; 
@@ -259,9 +205,7 @@
 	
 	{ 
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
 	  sh_error_handle ((-1), FIL__, __LINE__, EINVAL,
 			   MSG_SUID_ERROR,
 			   _("Possible race: not a directory"));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
 	  *q = '/'; 
 	  return -1; 
@@ -271,20 +215,16 @@
       if (0 != chdir(path))
 	{
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
 	  sh_error_handle ((-1), FIL__, __LINE__, errno,
 			   MSG_SUID_ERROR,
-			   sh_error_message(errno, errbuf, sizeof(errbuf)));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
+			   sh_error_message(errno));
 	  *q = '/';
 	  return -1;
 	}
       *q = '/';
-      if (0 != retry_lstat_ns(FIL__, __LINE__, ".", &two))
+      if (0 != retry_lstat(FIL__, __LINE__, ".", &two))
 	{ 
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
 	  sh_error_handle ((-1), FIL__, __LINE__, errno,
 			   MSG_SUID_ERROR,
-			   sh_error_message(errno, errbuf, sizeof(errbuf)));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
+			   sh_error_message(errno));
 	  return -1; 
 	}
@@ -293,9 +233,7 @@
 	  (!S_ISDIR(two.st_mode))/*@+usedef@*/)
 	{ 
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
 	  sh_error_handle ((-1), FIL__, __LINE__, EINVAL,
 			   MSG_SUID_ERROR,
 			   _("Possible race: lstat(dir) != lstat(.)"));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
 	  return -1;
 	}
@@ -310,11 +248,9 @@
       if (*path == '\0')
 	return -1;
-      if (0 != retry_lstat_ns(FIL__, __LINE__, path, &one))
+      if (0 != retry_lstat(FIL__, __LINE__, path, &one))
 	{
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
 	  sh_error_handle ((-1), FIL__, __LINE__, errno,
 			   MSG_SUID_ERROR,
-			   sh_error_message(errno, errbuf, sizeof(errbuf)));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
+			   sh_error_message(errno));
 	  return -1;
 	} 
@@ -322,19 +258,15 @@
       if (-1 == fd)
 	{
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
 	  sh_error_handle ((-1), FIL__, __LINE__, errno,
 			   MSG_SUID_ERROR,
-			   sh_error_message(errno, errbuf, sizeof(errbuf)));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
+			   sh_error_message(errno));
 	  return -1;
 	} 
       if (0 != retry_fstat(FIL__, __LINE__, fd, &two))
 	{ 
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
 	  sh_error_handle ((-1), FIL__, __LINE__, errno,
 			   MSG_SUID_ERROR,
-			   sh_error_message(errno, errbuf, sizeof(errbuf)));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  (void) sl_close_fd(FIL__, __LINE__, fd);
+			   sh_error_message(errno));
+	  (void) close(fd);
 	  return -1; 
 	}
@@ -342,30 +274,24 @@
 	  (one.st_ino != two.st_ino)/*@+usedef@*/)
 	{ 
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
 	  sh_error_handle ((-1), FIL__, __LINE__, EINVAL,
 			   MSG_SUID_ERROR,
 			   _("Possible race: lstat != fstat"));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  (void) sl_close_fd(FIL__, __LINE__, fd); 
+	  (void) close(fd); 
 	  return -1;
 	}
       if (!S_ISREG(two.st_mode))
 	{ 
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
 	  sh_error_handle ((-1), FIL__, __LINE__, EINVAL,
 			   MSG_SUID_ERROR,
 			   _("Possible race: not a regular file"));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  (void) sl_close_fd(FIL__, __LINE__, fd); 
+	  (void) close(fd); 
 	  return -1;
 	}
       if ((0 == (two.st_mode & S_ISUID)) && (0 == (two.st_mode & S_ISGID)))
 	{ 
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
 	  sh_error_handle ((-1), FIL__, __LINE__, EINVAL,
 			   MSG_SUID_ERROR,
 			   _("Possible race: not a suid/sgid file"));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  (void) sl_close_fd(FIL__, __LINE__, fd); 
+	  (void) close(fd); 
 	  return -1;
 	}
@@ -379,29 +305,23 @@
 	  if (-1 == /*@-unrecog@*/fchmod(fd, two.st_mode)/*@+unrecog@*/)
 	    {
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
 	      sh_error_handle ((-1), FIL__, __LINE__, errno,
 			       MSG_SUID_ERROR,
-			       sh_error_message(errno, errbuf, sizeof(errbuf)));
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	      (void) sl_close_fd(FIL__, __LINE__, fd); 
+			       sh_error_message(errno));
+	      (void) close(fd); 
 	      return -1;
 	    }
 #else
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
 	  sh_error_handle ((-1), FIL__, __LINE__, errno,
 			   MSG_SUID_ERROR,
 			   _("The fchmod() function is not available"));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  (void) sl_close_fd(FIL__, __LINE__, fd); 
+	  (void) close(fd); 
 	  return -1;
 #endif
 	  if (two.st_nlink > 1)
 	    {
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
 	      sh_error_handle ((-1), FIL__, __LINE__, 0,
 			       MSG_SUID_ERROR,
 			       _("Not truncated because hardlink count gt 1"));
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	      (void) sl_close_fd(FIL__, __LINE__, fd); 
+	      (void) close(fd); 
 	      return -1;
 	    }
@@ -410,10 +330,8 @@
 	  if (-1 == /*@-unrecog@*/ftruncate(fd, 0)/*@+unrecog@*/)
 	    {
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
 	      sh_error_handle ((-1), FIL__, __LINE__, errno,
 			       MSG_SUID_ERROR,
-			       sh_error_message(errno, errbuf, sizeof(errbuf)));
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	      (void) sl_close_fd(FIL__, __LINE__, fd); 
+			       sh_error_message(errno));
+	      (void) close(fd); 
 	      return -1;
 	    }
@@ -423,597 +341,81 @@
 	  if (-1 == retry_aud_unlink(FIL__, __LINE__, path))
 	    {
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
 	      sh_error_handle ((-1), FIL__, __LINE__, errno,
 			       MSG_SUID_ERROR,
-			       sh_error_message(errno, errbuf, sizeof(errbuf)));
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	      (void) sl_close_fd(FIL__, __LINE__, fd); 
+			       sh_error_message(errno));
+	      (void) close(fd); 
 	      return -1;
 	    }
 	}
-      (void) sl_close_fd (FIL__, __LINE__, fd);
+      (void) close (fd);
       return (0);
     }
 }
 
-static int do_truncate (const char * path_in)
-{
-  volatile int    caperr;
-  int    result;
-  char * path;
-  char errbuf[SH_ERRBUF_SIZE];
+static int do_truncate (char * path)
+{
+  int         caperr;
+  int result;
 
   if (0 != chdir("/"))
     {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
       sh_error_handle ((-1), FIL__, __LINE__, errno,
 		       MSG_SUID_ERROR,
-		       sh_error_message(errno, errbuf, sizeof(errbuf)));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
+		       sh_error_message(errno));
     }
 
   if (0 != (caperr = sl_get_cap_qdel()))
     {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
       sh_error_handle((-1), FIL__, __LINE__, caperr, MSG_E_SUBGEN,
-		      sh_error_message (caperr, errbuf, sizeof(errbuf)), 
+		      sh_error_message (caperr), 
 		      _("sl_get_cap_qdel"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-    }
-
-  path   = sh_util_strdup  (path_in);
+    }
+
   result = do_truncate_int (path, 0);
-  SH_FREE(path);
 
   if (0 != (caperr = sl_drop_cap_qdel()))
     {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
       sh_error_handle((-1), FIL__, __LINE__, caperr, MSG_E_SUBGEN,
-		      sh_error_message (caperr, errbuf, sizeof(errbuf)), 
+		      sh_error_message (caperr), 
 		      _("sl_drop_cap_qdel"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
     }
 
   if (0 != chdir("/"))
     {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
       sh_error_handle ((-1), FIL__, __LINE__, errno,
 		       MSG_SUID_ERROR,
-		       sh_error_message(errno, errbuf, sizeof(errbuf)));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
+		       sh_error_message(errno));
     }
   return result;
 }
-
-/* This variable is not used anywhere. It only exists
- * to assign &dirlist to it, which keeps gcc from
- * putting it into a register, and avoids the 'clobbered
- * by longjmp' warning. And no, 'volatile' proved insufficient.
- */
-static void * sh_dummy_tmp = NULL;
-
-static void sh_q_delete(const char * fullpath)
-{
-  int    status;
-  char * msg;
-  char * tmp;
-
-  /* Take the address to keep gcc from putting it into a register. 
-   * Avoids the 'clobbered by longjmp' warning. 
-   */
-  sh_dummy_tmp = (void*) &tmp;
-
-  if (do_truncate (fullpath) == -1)
-    {
-      status = errno;
-      msg    = SH_ALLOC(SH_BUFSIZE);
-      tmp    = sh_util_safe_name(fullpath);
-
-      (void) sl_snprintf(msg, SH_BUFSIZE, 
-			 _("Problem quarantining file.  File NOT quarantined.  errno = %ld"), 
-			 status);
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle (ShSuidchkSeverity,
-		       FIL__, __LINE__, 
-		       status,
-		       MSG_SUID_QREPORT, msg,
-		       tmp );
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      SH_FREE(tmp);
-      SH_FREE(msg);
-    }
-  else
-    {
-      tmp    = sh_util_safe_name(fullpath);
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle (ShSuidchkSeverity,
-		       FIL__, __LINE__, 0,
-		       MSG_SUID_QREPORT,
-		       _("Quarantine method applied"),
-		       tmp );
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      SH_FREE(tmp);
-    }
-  return;
-}
-
-/* This variable is not used anywhere. It only exists
- * to assign &dirlist to it, which keeps gcc from
- * putting it into a register, and avoids the 'clobbered
- * by longjmp' warning. And no, 'volatile' proved insufficient.
- */
-static void * sh_dummy_mtmp = NULL;
-static void * sh_dummy_mmsg = NULL;
-
-static void sh_q_move(const char * fullpath, file_type * theFile, 
-		      const char * timestrc, const char * timestra, 
-		      const char * timestrm)
-{
-  volatile int  status;
-  int           readFile  = -1;
-  volatile int  writeFile = -1;
-  struct stat   fileInfo;
-  ssize_t       count;
-  char        * msg;
-  char        * tmp;
-  char        * basetmp;
-  char        * filetmp;
-  char          buffer[1024];
-  char        * dir = SH_ALLOC(PATH_MAX+1);
-  mode_t        umask_old;
-  FILE *        filePtr = NULL;
-
-  /* Take the address to keep gcc from putting it into a register. 
-   * Avoids the 'clobbered by longjmp' warning. 
-   */
-  sh_dummy_mtmp = (void*) &tmp;
-  sh_dummy_mmsg = (void*) &msg;
-
-  (void) sl_strlcpy (dir, DEFAULT_QDIR, PATH_MAX+1);
-
-  if (retry_stat (FIL__, __LINE__, dir, &fileInfo) != 0)
-    {
-      /* Quarantine directory does not exist,
-       */
-      status = errno;
-      msg    = SH_ALLOC(SH_BUFSIZE);
-      tmp    = sh_util_safe_name(fullpath);
-
-      (void) sl_snprintf(msg, SH_BUFSIZE, 
-			 _("Problem quarantining file.  File NOT quarantined.  errno = %ld (stat)"), 
-			 status);
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle (ShSuidchkSeverity,
-		       FIL__, __LINE__, 
-		       status,
-		       MSG_SUID_QREPORT, msg,
-		       tmp );
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      SH_FREE(tmp);
-      SH_FREE(msg);
-    }
-  else
-    {
-      if (retry_lstat (FIL__, __LINE__, 
-		       fullpath, &fileInfo) == -1)
-	{
-	  status = errno;
-	  msg    = SH_ALLOC(SH_BUFSIZE);
-	  tmp    = sh_util_safe_name(fullpath);
-
-	  (void) sl_snprintf(msg, SH_BUFSIZE, _("I/O error.  errno = %ld(stat)"), status);
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle (ShSuidchkSeverity,
-			   FIL__, __LINE__, 
-			   status,
-			   MSG_SUID_QREPORT,
-			   msg, tmp );
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  SH_FREE(tmp);
-	  SH_FREE(msg);
-	}
-      else
-	{
-	  basetmp = sh_util_strdup(fullpath);
-	  filetmp = SH_ALLOC(PATH_MAX+1);
-	  tmp     = sh_util_basename(basetmp);
-
-	  (void) sl_snprintf(filetmp, PATH_MAX+1, "%s/%s", 
-			     DEFAULT_QDIR, tmp);
-	  SH_FREE(tmp);
-	  SH_FREE(basetmp);
-	  
-	  readFile  = open (fullpath, O_RDONLY);
-	  if (readFile != -1)
-	    writeFile = open (filetmp, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IXUSR);
-	  
-	  if ((readFile == -1) || (writeFile == -1))
-	    {
-	      status = errno;
-	      msg    = SH_ALLOC(SH_BUFSIZE);
-	      tmp    = sh_util_safe_name(fullpath);
-
-	      (void) sl_snprintf(msg, SH_BUFSIZE, _("Problem quarantining file.  File NOT quarantined.  errno = %ld (open)"), status);
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle (ShSuidchkSeverity,
-			       FIL__, __LINE__, status,
-			       MSG_SUID_QREPORT,
-			       msg, tmp );
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	      SH_FREE(tmp);
-	      SH_FREE(msg);
-	    }
-	  else
-	    { 
-	      /* sizeof(buffer) is 1024 
-	       */
-	      while ((count = (int) read (readFile, buffer, sizeof (buffer))) > 0)
-		{
-		  if ((int) write (writeFile, buffer, (size_t) count) != count)
-		    {
-		      status = errno;
-		      msg    = SH_ALLOC(SH_BUFSIZE);
-		      tmp    = sh_util_safe_name(fullpath);
-
-		      (void) sl_snprintf(msg, SH_BUFSIZE, 
-					 _("I/O error.  errno = %ld (write)"), status);
-		      SH_MUTEX_LOCK(mutex_thread_nolog);
-		      sh_error_handle (ShSuidchkSeverity,
-				       FIL__,
-				       __LINE__,
-				       status,
-				       MSG_SUID_QREPORT,
-				       msg, tmp );
-		      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-		      SH_FREE(tmp);
-		      SH_FREE(msg);
-		    }
-		}
-	    }
-
-	  (void) sl_close_fd (FIL__, __LINE__, readFile);
-	  (void) fchmod(writeFile, S_IRUSR | S_IWUSR | S_IXUSR);
-	  (void) sl_close_fd (FIL__, __LINE__, writeFile);
-
-	  if (do_truncate (fullpath) == -1)
-	    {
-	      status = errno;
-	      msg    = SH_ALLOC(SH_BUFSIZE);
-	      tmp    = sh_util_safe_name(fullpath);
-
-	      (void) sl_snprintf(msg, SH_BUFSIZE, 
-				 _("Problem quarantining file.  File NOT quarantined.  errno = %ld"), 
-				 status);
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle (ShSuidchkSeverity,
-			       FIL__, __LINE__, status,
-			       MSG_SUID_QREPORT,
-			       msg, tmp );
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	      SH_FREE(tmp);
-	      SH_FREE(msg);
-	    }
-	  else
-	    {
-	      tmp = sh_util_basename(fullpath);
-
-	      (void) sl_snprintf(filetmp, PATH_MAX+1, "%s/%s.info", 
-				 DEFAULT_QDIR, 
-				 tmp);
-
-	      SH_FREE(tmp);
-	      /*
-	       * avoid chmod by setting umask
-	       */
-	      umask_old = umask (0077);
-	      filePtr   = fopen (filetmp, "w+");
-
-	      /*@-usedef@*/
-	      if (filePtr)
-		{
-		  fprintf(filePtr, 
-			  _("File Info:\n filename=%s\n size=%lu\n owner=%s(%d)\n group=%s(%d)\n ctime=%s\n atime=%s\n mtime=%s\n"), 
-			  fullpath, 
-			  (unsigned long) theFile->size, 
-			  theFile->c_owner, (int) theFile->owner, 
-			  theFile->c_group, (int) theFile->group, 
-			  timestrc, timestra, timestrm);
-		  (void) sl_fclose (FIL__, __LINE__, filePtr);
-		}
-	      /*@+usedef@*/
-	      umask (umask_old);
-	      
-	      tmp    = sh_util_safe_name(fullpath);
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle (ShSuidchkSeverity,
-			       FIL__,__LINE__,
-			       0, MSG_SUID_QREPORT,
-			       _("Quarantine method applied"),
-			       tmp );
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	      SH_FREE(tmp);
-	    }
-	  SH_FREE(filetmp);
-	}
-    }
-  SH_FREE(dir);
-  return;
-}
-
-/* This variable is not used anywhere. It only exists
- * to assign &dirlist to it, which keeps gcc from
- * putting it into a register, and avoids the 'clobbered
- * by longjmp' warning. And no, 'volatile' proved insufficient.
- */
-static void * sh_dummy_ctmp = NULL;
-static void * sh_dummy_cmsg = NULL;
-
-static void sh_q_changeperm(const char * fullpath)
-{
-  volatile int    caperr;
-  volatile int    status;
-  char          * msg;
-  char          * tmp;
-  struct stat     fileInfo;
-  struct stat     fileInfo_F;
-  int             cperm_status = 0;
-  volatile int    file_d       = -1;
-  char errbuf[SH_ERRBUF_SIZE];
-
-  /* Take the address to keep gcc from putting it into a register. 
-   * Avoids the 'clobbered by longjmp' warning. 
-   */
-  sh_dummy_ctmp = (void*) &tmp;
-  sh_dummy_cmsg = (void*) &msg;
-
-  if (retry_lstat(FIL__, __LINE__, fullpath, &fileInfo) == -1)
-    {
-      status = errno;
-      msg    = SH_ALLOC(SH_BUFSIZE);
-      tmp    = sh_util_safe_name(fullpath);
-
-      (void) sl_snprintf(msg, SH_BUFSIZE, _("I/O error.  errno = %ld"), status);
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle (ShSuidchkSeverity,
-		       FIL__, __LINE__, 
-		       status,
-		       MSG_SUID_QREPORT, msg,
-		       tmp );
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      SH_FREE(tmp);
-      SH_FREE(msg);
-      cperm_status = -1;
-    }
-  
-  if (cperm_status == 0)
-    {
-      if (0 != (caperr = sl_get_cap_qdel()))
-	{
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle((-1), FIL__, __LINE__, 
-			  caperr, MSG_E_SUBGEN,
-			  sh_error_message (caperr, errbuf, sizeof(errbuf)), 
-			  _("sl_get_cap_qdel"));
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  cperm_status = -1;
-	}
-    }
-  
-  if (cperm_status == 0)
-    {
-      file_d = aud_open (FIL__, __LINE__, SL_YESPRIV,
-			 fullpath, O_RDONLY, 0);
-      if (-1 == file_d)
-	{
-	  status = errno;
-	  msg    = SH_ALLOC(SH_BUFSIZE);
-	  tmp    = sh_util_safe_name(fullpath);
-
-	  (void) sl_snprintf(msg, SH_BUFSIZE, _("I/O error.  errno = %ld"), status);
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle (ShSuidchkSeverity,
-			   FIL__, __LINE__, 
-			   status,
-			   MSG_SUID_QREPORT, msg,
-			   tmp );
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  SH_FREE(tmp);
-	  SH_FREE(msg);
-	  cperm_status = -1;
-	}
-    }
-  
-  if (cperm_status == 0)
-    {
-      if (retry_fstat(FIL__, __LINE__, file_d, &fileInfo_F) == -1)
-	{
-	  status = errno;
-	  msg    = SH_ALLOC(SH_BUFSIZE);
-	  tmp    = sh_util_safe_name(fullpath);
-
-	  (void) sl_snprintf(msg, SH_BUFSIZE, 
-			     _("I/O error.  errno = %ld"), status);
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle (ShSuidchkSeverity,
-			   FIL__, __LINE__, 
-			   status,
-			   MSG_SUID_QREPORT, msg,
-			   tmp );
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  SH_FREE(tmp);
-	  SH_FREE(msg);
-	  cperm_status = -1;
-	}
-    }
-  
-  if (cperm_status == 0)
-    {
-      if (fileInfo_F.st_ino  != fileInfo.st_ino ||
-	  fileInfo_F.st_dev  != fileInfo.st_dev ||
-	  fileInfo_F.st_mode != fileInfo.st_mode)
-	{
-	  status = errno;
-	  msg    = SH_ALLOC(SH_BUFSIZE);
-	  tmp    = sh_util_safe_name(fullpath);
-
-	  (void) sl_snprintf(msg, SH_BUFSIZE, 
-			     _("Race detected.  errno = %ld"), status);
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle (ShSuidchkSeverity,
-			   FIL__, __LINE__, 
-			   status,
-			   MSG_SUID_QREPORT, msg,
-			   tmp );
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  SH_FREE(tmp);
-	  SH_FREE(msg);
-	  cperm_status = -1;
-	}
-    }
-  
-  if ((fileInfo.st_mode & S_ISUID) > 0)
-    fileInfo.st_mode -= S_ISUID;
-  if ((fileInfo.st_mode & S_ISGID) > 0)
-    fileInfo.st_mode -= S_ISGID;
-  
-  if (cperm_status == 0)
-    {
-      if (fchmod(file_d, fileInfo.st_mode) == -1)
-	{
-	  status = errno;
-	  msg    = SH_ALLOC(SH_BUFSIZE);
-	  tmp    = sh_util_safe_name(fullpath);
-
-	  (void) sl_snprintf(msg, SH_BUFSIZE, 
-			     _("Problem quarantining file.  File NOT quarantined.  errno = %ld"), 
-			     status);
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle (ShSuidchkSeverity,
-			   FIL__, __LINE__, 
-			   status,
-			   MSG_SUID_QREPORT,
-			   msg, tmp );
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  SH_FREE(tmp);
-	  SH_FREE(msg);
-	}
-      else
-	{
-	  tmp    = sh_util_safe_name(fullpath);
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle (ShSuidchkSeverity,
-			   FIL__, __LINE__, 
-			   0,
-			   MSG_SUID_QREPORT,
-			   _("Quarantine method applied"),
-			   tmp );
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  SH_FREE(tmp);
-	}
-    }
-  
-  if (0 != (caperr = sl_drop_cap_qdel()))
-    {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle((-1), FIL__, __LINE__, 
-		      caperr, MSG_E_SUBGEN,
-		      sh_error_message (caperr, errbuf, sizeof(errbuf)), 
-		      _("sl_drop_cap_qdel"));
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-    }
-  
-  if (file_d != -1)
-    {
-      do {
-	status = sl_close_fd (FIL__, __LINE__, file_d);
-      } while (status == -1 && errno == EINTR);
-      
-      if (-1 == status)
-	{
-	  status = errno;
-	  msg    = SH_ALLOC(SH_BUFSIZE);
-	  tmp    = sh_util_safe_name(fullpath);
-
-	  (void) sl_snprintf(msg, SH_BUFSIZE, 
-			     _("I/O error.  errno = %ld"), status);
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle (ShSuidchkSeverity,
-			   FIL__, __LINE__, 
-			   status,
-			   MSG_SUID_QREPORT, msg,
-			   tmp );
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  SH_FREE(tmp);
-	  SH_FREE(msg);
-	}
-    }
-  return;
-}
-
-static void report_file (const char * tmpcat, file_type * theFile, 
-			 char * timestrc, char * timestra, char * timestrm)
-{
-  char * msg = SH_ALLOC(SH_BUFSIZE);
-  char * tmp = sh_util_safe_name(tmpcat);
-
-  msg[0] = '\0';
-  /*@-usedef@*/
-
-#ifdef SH_USE_XML
-  (void) sl_snprintf(msg, SH_BUFSIZE, _("owner_new=\"%s\" iowner_new=\"%ld\" group_new=\"%s\" igroup_new=\"%ld\" size_new=\"%lu\" ctime_new=\"%s\" atime_new=\"%s\" mtime_new=\"%s\""), 
-		     theFile->c_owner, theFile->owner, 
-		     theFile->c_group, theFile->group, 
-		     (unsigned long) theFile->size, 
-		     timestrc, timestra, timestrm);
-#else
-  (void) sl_snprintf(msg, SH_BUFSIZE, _("owner_new=<%s>, iowner_new=<%ld>, group_new=<%s>, igroup_new=<%ld>, filesize=<%lu>, ctime=<%s>, atime=<%s>, mtime=<%s>"), 
-		     theFile->c_owner, theFile->owner, 
-		     theFile->c_group, theFile->group, 
-		     (unsigned long) theFile->size, 
-		     timestrc, timestra, timestrm);
-#endif
-  /*@+usedef@*/
-  
-  SH_MUTEX_LOCK(mutex_thread_nolog);
-  sh_error_handle (ShSuidchkSeverity, FIL__, __LINE__, 
-		   0, MSG_SUID_POLICY,
-		   _("suid/sgid file not in database"),
-		   tmp, msg );
-  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-  SH_FREE(tmp);
-  SH_FREE(msg);
-  return;
-}
-
-/* This variable is not used anywhere. It only exists
- * to assign &dirlist to it, which keeps gcc from
- * putting it into a register, and avoids the 'clobbered
- * by longjmp' warning. And no, 'volatile' proved insufficient.
- */
-void * sh_dummy_idirlist = NULL;
-void * sh_dummy_itmp     = NULL;
-
 
 static
 int sh_suidchk_check_internal (char * iname)
 {
+  int             caperr;
   DIR *           thisDir = NULL;
+  FILE *          filePtr = NULL;
   struct dirent * thisEntry;
   char          * tmpcat;
   char          * tmp;
+  char          * msg;
+  char          * dir;
+  char          * filetmp;
+  char          * basetmp;
   char            timestrc[32];
   char            timestra[32];
   char            timestrm[32];
+  char            buffer[1024];
   struct stat     buf;
-  volatile int    status;
-  int             fflags;
+  int             status;
+  int             count;
+  int             readFile = -1;
+  int             writeFile = -1;
   char          * fs;
-  volatile long   sl_status;
-  file_type     * theFile = NULL;
-  char            fileHash[2*(KEY_LEN + 1)];
-
-  struct sh_dirent * dirlist;
-  struct sh_dirent * dirlist_orig;
-  char errbuf[SH_ERRBUF_SIZE];
+  long            sl_status = SL_ENONE;
+  struct stat     fileInfo;
+
+  file_type       theFile;
+  char            fileHash[KEY_LEN + 1];
 
   SL_ENTER(_("sh_suidchk_check_internal"));
@@ -1029,10 +431,4 @@
   }
 
-  /* Take the address to keep gcc from putting it into a register. 
-   * Avoids the 'clobbered by longjmp' warning. 
-   */
-  sh_dummy_idirlist = (void*) &dirlist;
-  sh_dummy_itmp     = (void*) &tmp;
-
   thisDir = opendir (iname);
 
@@ -1041,23 +437,17 @@
       status = errno;
       tmp = sh_util_safe_name(iname);
-      SH_MUTEX_LOCK(mutex_thread_nolog);
       sh_error_handle (ShDFLevel[SH_ERR_T_DIR], FIL__, __LINE__, status, 
 		       MSG_E_OPENDIR,
-		       sh_error_message (status, errbuf, sizeof(errbuf)), tmp);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
+		       sh_error_message (status), tmp);
       SH_FREE(tmp);
-      sh_dummy_idirlist = NULL;
-      sh_dummy_itmp     = NULL;
       SL_RETURN( (-1), _("sh_suidchk_check_internal"));
     }
 
-  /* Loop over directory entries
-   */
-  SH_MUTEX_LOCK(mutex_readdir);
-
-  dirlist      = NULL;
-  dirlist_orig = NULL;
-
   do {
+
+    if (sig_urgent > 0) {
+      SL_RETURN( (0), _("sh_suidchk_check_internal"));
+    }
+
 
     thisEntry = readdir (thisDir);
@@ -1071,325 +461,427 @@
 	continue;
 
-      dirlist = addto_sh_dirlist (thisEntry, dirlist);
-    }
-
-  } while (thisEntry != NULL);
-
-  SH_MUTEX_UNLOCK(mutex_readdir);
-
-  closedir(thisDir);
-
-  dirlist_orig = dirlist;
-
-  sl_status = SL_ENONE;
-
-  do {
-
-    /* If the directory is empty, dirlist = NULL
-     */
-    if (!dirlist)
-      break;
-
-    if (sig_urgent > 0) {
-      SL_RETURN( (0), _("sh_suidchk_check_internal"));
-    }
-
-    tmpcat = SH_ALLOC(PATH_MAX);
-    (void) sl_strlcpy(tmpcat, iname, PATH_MAX);
-    
-    if ((sl_strlen(tmpcat) != sl_strlen(iname)) || (tmpcat[0] == '\0'))
-      {
+      tmpcat = SH_ALLOC(PATH_MAX);
+      (void) sl_strlcpy(tmpcat, iname, PATH_MAX);
+
+      if ((sl_strlen(tmpcat) != sl_strlen(iname)) || (tmpcat[0] == '\0'))
 	sl_status = SL_ETRUNC;
-      }
-    else
-      {
-	if (tmpcat[1] != '\0') 
-	  sl_status = sl_strlcat(tmpcat, "/",                 PATH_MAX);
-      }
-
-    if (! SL_ISERROR(sl_status))
-      sl_status = sl_strlcat(tmpcat, dirlist->sh_d_name,   PATH_MAX);
-
-    if (SL_ISERROR(sl_status))
-      {
-	tmp = sh_util_safe_name(tmpcat);
-	SH_MUTEX_LOCK(mutex_thread_nolog);
-	sh_error_handle ((-1), FIL__, __LINE__, (int) sl_status, 
-			 MSG_E_SUBGPATH,
-			 _("path too long"),
-			 _("sh_suidchk_check_internal"), tmp );
-	SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	SH_FREE(tmp);
-	SH_FREE(tmpcat);
-	dirlist = dirlist->next;
-	continue;
-      }
-
-    ++FileLimNum;
-    ++FileLimTotal;
-
-    /* Rate limit (Fps == Files per second)
-     */
-    if ((ShSuidchkFps > 0 && FileLimNum > ShSuidchkFps) &&
-	(ShSuidchkYield == S_FALSE))
-      {
-	FileLimNum  = 0;
-	FileLimNow  = time(NULL);
-	
-	if ( (FileLimNow  - FileLimStart) > 0 && 
-	     FileLimTotal/(FileLimNow  - FileLimStart) > ShSuidchkFps )
-	  (void) retry_msleep((int)((FileLimTotal/(FileLimNow-FileLimStart))/
-				    ShSuidchkFps) , 0);
-      }
-
-    status = (int) retry_lstat(FIL__, __LINE__, tmpcat, &buf);
-
-    if (status != 0)
-      {
-	volatile int elevel = SH_ERR_ERR;
-	size_t tlen;
-
-	status = errno;
-	tmp = sh_util_safe_name(tmpcat);
-	tlen = strlen(tmp);
-	if (tlen >= 6 && 0 == strcmp(&tmp[tlen-6], _("/.gvfs")))
-	  elevel = SH_ERR_NOTICE;
-	else if (tlen >= 5 && 0 == strcmp(&tmp[tlen-5], _("/gvfs")))
-	  elevel = SH_ERR_NOTICE;
-
-        /* If we are scanning a temporary directory where dirs and files
-	 * can be created/deleted, an lstat() error is something which
-	 * may occur frequently. As a missing dir/file is not an important
-	 * problem for the suidcheck, the error level is only SH_ERR_NOTICE. 
-	 */
-        if (status == ENOENT)
-          elevel = SH_ERR_NOTICE;
-
-	SH_MUTEX_LOCK(mutex_thread_nolog);
-	sh_error_handle (elevel, FIL__, __LINE__, status, MSG_ERR_LSTAT,
-			 sh_error_message(status, errbuf, sizeof(errbuf)),
-			 tmp );
-	SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	SH_FREE(tmp);
-      }
-    else
-      {
-	if (/*@-usedef@*/S_ISDIR(buf.st_mode)/*@+usedef@*/ &&
-	    (ShSuidchkExclude == NULL || 
-	     NULL == zAVL_string_get(ShSuidchkExclude, tmpcat)))
-	  {
-	    /* fs is a STATIC string or NULL
-	     */
-	    fs = filesystem_type (tmpcat, tmpcat, &buf);
-
-	    if (fs != NULL 
-#ifndef SH_SUIDTESTDIR
-		&& 
-		0 != strncmp (_("afs"),     fs, 3) && 
-		0 != strncmp (_("devfs"),   fs, 5) &&
-		0 != strncmp (_("fdesc"),   fs, 5) &&
-		0 != strncmp (_("iso9660"), fs, 7) &&
-		0 != strncmp (_("cd9660"),  fs, 6) &&
-		0 != strncmp (_("lustre"),  fs, 6) &&
-		0 != strncmp (_("mmfs"),    fs, 4) && 
-		0 != strncmp (_("msdos"),   fs, 5) &&
-		0 != strncmp (_("nfs"),     fs, 3) &&
-		0 != strncmp (_("proc"),    fs, 4) &&
-		0 != strncmp (_("sysfs"),   fs, 5) &&
-		0 != strncmp (_("vfat"),    fs, 4)
-#endif 
-		)
-	      {
-		if ((ShSuidchkNosuid == S_TRUE) || 
-		    (0 != strncmp (_("nosuid"),  fs, 6)))
+      else
+	{
+	  if (tmpcat[1] != '\0') 
+	    sl_status = sl_strlcat(tmpcat, "/",                 PATH_MAX);
+	}
+      if (! SL_ISERROR(sl_status))
+	sl_status = sl_strlcat(tmpcat, thisEntry->d_name,   PATH_MAX);
+      if (SL_ISERROR(sl_status))
+	{
+	  tmp = sh_util_safe_name(tmpcat);
+	  sh_error_handle ((-1), FIL__, __LINE__, (int) sl_status, 
+			   MSG_E_SUBGPATH,
+			   _("path too long"),
+			   _("sh_suidchk_check_internal"), tmp );
+	  SH_FREE(tmp);
+	  continue;
+	}
+
+      ++FileLimNum;
+      ++FileLimTotal;
+
+      if ((ShSuidchkFps > 0 && FileLimNum > ShSuidchkFps && FileLimTotal > 0)&&
+	  (ShSuidchkYield == S_FALSE))
+	{
+	  FileLimNum  = 0;
+	  FileLimNow  = time(NULL);
+ 
+	  if ( (FileLimNow  - FileLimStart) > 0 && 
+	       FileLimTotal/(FileLimNow  - FileLimStart) > ShSuidchkFps )
+	    (void) retry_msleep((int)((FileLimTotal/(FileLimNow-FileLimStart))/
+		   ShSuidchkFps) , 0);
+	}
+	      
+      status = (int) retry_lstat(FIL__, __LINE__, tmpcat, &buf);
+
+      if (status != 0)
+	{
+	  status = errno;
+	  tmp = sh_util_safe_name(tmpcat);
+	  sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, status, MSG_ERR_LSTAT,
+			   sh_error_message(status),
+			   tmpcat );
+	  SH_FREE(tmp);
+	}
+      else
+	{
+	  if (/*@-usedef@*/S_ISDIR(buf.st_mode)/*@+usedef@*/ &&
+	      (ShSuidchkExclude == NULL || 
+	      0 != strncmp(tmpcat, ShSuidchkExclude, (size_t) ExcludeLen)))
+	    {
+	      /* fs is a STATIC string
+	       */
+	      fs = filesystem_type (tmpcat, tmpcat, &buf);
+	      if (fs != NULL && 
+		  0 != strncmp (_("nfs"),     fs, 3) && 
+		  0 != strncmp (_("proc"),    fs, 4) &&
+		  0 != strncmp (_("iso9660"), fs, 7) &&
+		  0 != strncmp (_("vfat"),    fs, 4) &&
+		  0 != strncmp (_("msdos"),   fs, 5) &&
+		  0 != strncmp (_("devfs"),   fs, 5) &&
+		  0 != strncmp (_("nosuid"),  fs, 6) 
+		  )
+		{
 		  /* fprintf(stderr, "%s: %s\n", fs, tmpcat); */
 		  (void) sh_suidchk_check_internal(tmpcat);
-	      }
-	  }
-	else if (S_ISREG(buf.st_mode) &&
-		 (0 !=(S_ISUID & buf.st_mode) ||
+		}
+	    }
+	  else if (S_ISREG(buf.st_mode) &&
+		   (0 !=(S_ISUID & buf.st_mode) ||
 #if defined(HOST_IS_LINUX)
-		  (0 !=(S_ISGID & buf.st_mode) && 
-		   0 !=(S_IXGRP & buf.st_mode)) 
+		    (0 !=(S_ISGID & buf.st_mode) && 
+		     0 !=(S_IXGRP & buf.st_mode)) 
 #else  
-		  0 !=(S_ISGID & buf.st_mode)
-#endif
-		  )
-		 )
-	  {
-	    int dummy;
-	    int class;
-	    unsigned long check_flags = 0;
-
-	    theFile = SH_ALLOC(sizeof(file_type));
-
-	    (void) sl_strlcpy (theFile->fullpath, tmpcat, PATH_MAX);
-	    theFile->check_flags  = sh_files_maskof(SH_LEVEL_READONLY);
-	    CLEAR_SH_FFLAG_REPORTED(theFile->file_reported);
-	    theFile->attr_string = NULL;
-	    theFile->link_path   = NULL;
-	    
-	    sh_files_search_file(tmpcat, &class,  &check_flags, &dummy);
-	    if ((check_flags & MODI_PREL) != 0)
-	      MODI_SET(theFile->check_flags, MODI_PREL);
-
-	    status = sh_unix_getinfo (ShDFLevel[SH_ERR_T_RO], 
-				      dirlist->sh_d_name,
-				      theFile, fileHash, 0);
-	    
-	    tmp = sh_util_safe_name(tmpcat);
-	    
-	    if (status != 0)
-	      {
-		SH_MUTEX_LOCK(mutex_thread_nolog);
-		sh_error_handle (ShSuidchkSeverity, FIL__, __LINE__, 
-				 0, MSG_E_SUBGPATH,
-				 _("Could not check suid/sgid file"),
-				 _("sh_suidchk_check_internal"),
-				 tmp);
-		SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	      }
-	    else
-	      {
-		
-		if ( sh.flag.update   == S_TRUE && 
-		     (sh.flag.checkSum == SH_CHECK_INIT  || 
-		      sh.flag.checkSum == SH_CHECK_CHECK))
-		  {
-		    int compret;
-
-		    /* Updating database. Report new files that
-		     * are not in database already. Then compare
-		     * to database and report changes.
-		     */
-		    if (-1 == sh_hash_have_it (tmpcat))
-		      {
-			SH_MUTEX_LOCK(mutex_thread_nolog);
-			sh_error_handle ((-1), FIL__, __LINE__, 
-					 0, MSG_SUID_FOUND, tmp );
-			SH_MUTEX_UNLOCK(mutex_thread_nolog);
-		      }
-		    else
-		      {
-			SH_MUTEX_LOCK(mutex_thread_nolog);
-			sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 
-					 0, MSG_SUID_FOUND, tmp );
-			SH_MUTEX_UNLOCK(mutex_thread_nolog);
-		      }
-		    
-		    SH_MUTEX_LOCK(mutex_thread_nolog);
-		    compret = sh_hash_compdata (SH_LEVEL_READONLY, 
-						theFile, fileHash,
-						_("[SuidCheck]"), 
-						ShSuidchkSeverity);
-		    SH_MUTEX_UNLOCK(mutex_thread_nolog);
-
-		    if (compret == 0)
-		      {
-			sh_hash_pushdata_memory (theFile, fileHash); /* no call to sh_error_handle */
-		      }
-		    
-		    sh_hash_set_flag(tmpcat, SH_FFLAG_SUIDCHK); /* no call to sh_error_handle */
-		    
-		  }
-		
-		else if (sh.flag.checkSum == SH_CHECK_INIT  && 
-			 sh.flag.update == S_FALSE )
-		  {
-		    /* Running init. Report on files detected.
-		     */
-		    sh_dbIO_rootfs_strip(theFile->fullpath);
-		    sh_dbIO_data_write (theFile, fileHash); /* no call to sh_error_handle */
-		    SH_MUTEX_LOCK(mutex_thread_nolog);
-		    sh_error_handle ((-1), FIL__, __LINE__, 
-				     0, MSG_SUID_FOUND, tmp );
-		    SH_MUTEX_UNLOCK(mutex_thread_nolog);
-		  }
-		
-		else if (sh.flag.checkSum == SH_CHECK_CHECK )
-		  {
-		    /* Running file check. Report on new files
-		     * detected, and quarantine them.
-		     */
-		    SH_MUTEX_LOCK(mutex_thread_nolog);
-		    sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 
-				     0, MSG_SUID_FOUND, tmp );
-		    SH_MUTEX_UNLOCK(mutex_thread_nolog);
-		    
-		    fflags = sh_hash_getflags(tmpcat); /* no call to sh_error_handle */
-		    
-		    if ( (-1 == fflags) || (!SH_FFLAG_SUIDCHK_SET(fflags)))
-		      {
-			if (-1 == fflags)
-			  {
-			    (void) sh_unix_gmttime (theFile->ctime, timestrc, sizeof(timestrc)); 
-			    (void) sh_unix_gmttime (theFile->atime, timestra, sizeof(timestra)); 
-			    (void) sh_unix_gmttime (theFile->mtime, timestrm, sizeof(timestrm));
-
-			    report_file(tmpcat, theFile, timestrc, timestra, timestrm);
-			  }
-			/* Quarantine file according to configured method
-			 */
-			if (ShSuidchkQEnable == S_TRUE)
-			  {
-			    switch (ShSuidchkQMethod)
-			      {
-			      case SH_Q_DELETE:
-				sh_q_delete(theFile->fullpath);
-				break;
-			      case SH_Q_CHANGEPERM:
-				sh_q_changeperm(theFile->fullpath);
-				break;
-			      case SH_Q_MOVE:
-				sh_q_move(theFile->fullpath, theFile, timestrc, timestra, timestrm);
-				break;
-			      default:
-				SH_MUTEX_LOCK(mutex_thread_nolog);
-				sh_error_handle (ShSuidchkSeverity, FIL__,
-						 __LINE__, 0, MSG_SUID_QREPORT,
-						 _("Bad quarantine method"), tmp);
-				SH_MUTEX_UNLOCK(mutex_thread_nolog);
-				break;
-			      }
-			  }
-			else
-			  {
-			    /* 1.8.1 push file to in-memory database
-			     */
-			    SH_MUTEX_LOCK(mutex_thread_nolog);
-			    (void) sh_hash_compdata (SH_LEVEL_READONLY,
-						     theFile, fileHash,
-						     _("[SuidCheck]"),
-						     ShSuidchkSeverity);
-			    SH_MUTEX_UNLOCK(mutex_thread_nolog);
-			    
-			    sh_hash_set_flag(tmpcat, SH_FFLAG_SUIDCHK); /* no call to sh_error_handle */
-			    
-			  }
-		      }
-		    else
-		      {
-			/* File exists. Check for modifications.
-			 */
-			SH_MUTEX_LOCK(mutex_thread_nolog);
-			(void) sh_hash_compdata (SH_LEVEL_READONLY, 
-						 theFile, fileHash,
-						 _("[SuidCheck]"),
-						 ShSuidchkSeverity);
-			SH_MUTEX_UNLOCK(mutex_thread_nolog);	
-			sh_hash_set_flag(tmpcat, SH_FFLAG_SUIDCHK); /* no call to sh_error_handle */
-			
-		      }
-		  }
-	      }
-	    SH_FREE(tmp);
-	    if (theFile->attr_string) SH_FREE(theFile->attr_string);
-	    if (theFile->link_path)   SH_FREE(theFile->link_path);
-	    SH_FREE(theFile);
-	  }
-      }
-    SH_FREE(tmpcat);
-
-  
+		    0 !=(S_ISGID & buf.st_mode)
+#endif
+		    )
+		   )
+	    {
+	      
+	      (void) sl_strlcpy (theFile.fullpath, tmpcat, PATH_MAX);
+	      theFile.check_mask = sh_files_maskof(SH_LEVEL_READONLY);
+	      theFile.reported   = S_FALSE;
+	      status = sh_unix_getinfo (ShDFLevel[SH_ERR_T_RO], 
+					thisEntry->d_name,
+					&theFile, fileHash, 0);
+
+	      tmp = sh_util_safe_name(tmpcat);
+
+	      if (status != 0)
+		{
+		  sh_error_handle (ShSuidchkSeverity, FIL__, __LINE__, 
+				   0, MSG_E_SUBGPATH,
+				   _("Could not check suid/sgid file"),
+				   _("sh_suidchk_check_internal"),
+				   tmp);
+		}
+	      else
+		{
+		  if (sh.flag.update == S_TRUE && 
+		      (sh.flag.checkSum == SH_CHECK_INIT  || 
+		       sh.flag.checkSum == SH_CHECK_CHECK))
+		    {
+		      if (-1 == sh_hash_have_it (tmpcat))
+			{
+			  sh_error_handle ((-1), FIL__, __LINE__, 
+					   0, MSG_SUID_FOUND,
+					   tmp );
+			}
+		      else
+			{
+			  sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 
+					   0, MSG_SUID_FOUND,
+					   tmp );
+			}
+		      if (0 == sh_hash_compdata (SH_LEVEL_READONLY, 
+						 &theFile, fileHash,
+						 _("[SuidCheck]"), 
+						 ShSuidchkSeverity))
+			{
+			  sh_hash_pushdata_memory (&theFile, fileHash);
+			}
+		    }
+		  else if (sh.flag.checkSum == SH_CHECK_INIT  && 
+			   sh.flag.update == S_FALSE )
+		    {
+		      sh_hash_pushdata (&theFile, fileHash);
+		      sh_error_handle ((-1), FIL__, __LINE__, 
+				       0, MSG_SUID_FOUND,
+				       tmp );
+		    }
+		  else if (sh.flag.checkSum == SH_CHECK_CHECK )
+		    {
+		      sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 
+				       0, MSG_SUID_FOUND,
+				       tmp );
+		      if (-1 == sh_hash_have_it (tmpcat))
+			{
+			  msg = SH_ALLOC(SH_BUFSIZE);
+			  msg[0] = '\0';
+			  /*@-usedef@*/
+			  (void) sl_strlcpy (timestrc, 
+					     sh_unix_gmttime (theFile.ctime), 
+					     32);
+			  (void) sl_strlcpy (timestra, 
+					     sh_unix_gmttime (theFile.atime), 
+					     32);
+			  (void) sl_strlcpy (timestrm, 
+					     sh_unix_gmttime (theFile.mtime), 
+					     32);
+#ifdef SH_USE_XML
+			  (void) sl_snprintf(msg, SH_BUFSIZE, _("owner_new=\"%s\" iowner_new=\"%ld\" group_new=\"%s\" igroup_new=\"%ld\" size_new=\"%lu\" ctime_new=\"%s\" atime_new=\"%s\" mtime_new=\"%s\""), 
+				      theFile.c_owner, theFile.owner, 
+				      theFile.c_group, theFile.group, 
+				      (unsigned long) theFile.size, 
+				      timestrc, timestra, timestrm);
+#else
+			  (void) sl_snprintf(msg, SH_BUFSIZE, _("owner_new=<%s>, iowner_new=<%ld>, group_new=<%s>, igroup_new=<%ld>, filesize=<%lu>, ctime=<%s>, atime=<%s>, mtime=<%s>"), 
+				      theFile.c_owner, theFile.owner, 
+				      theFile.c_group, theFile.group, 
+				      (unsigned long) theFile.size, 
+				      timestrc, timestra, timestrm);
+#endif
+			  /*@+usedef@*/
+
+			  sh_error_handle (ShSuidchkSeverity, FIL__, __LINE__, 
+					   0, MSG_SUID_POLICY,
+					   _("suid/sgid file not in database"),
+					   tmp, msg );
+			  SH_FREE(msg);
+
+			  /* Quarantine file according to configured method
+                          */
+			  if (ShSuidchkQEnable == S_TRUE)
+			    {
+			      switch (ShSuidchkQMethod)
+				{
+				  case SH_Q_DELETE:
+				    /* if (unlink (theFile.fullpath) == -1) */
+				    if (do_truncate (theFile.fullpath) == -1)
+				      {
+					status = errno;
+					msg = SH_ALLOC(SH_BUFSIZE);
+					(void) sl_snprintf(msg, SH_BUFSIZE, _("Problem quarantining file.  File NOT quarantined.  errno = %ld"), status);
+					sh_error_handle (ShSuidchkSeverity,
+							 FIL__, __LINE__, 
+							 status,
+							 MSG_SUID_QREPORT, msg,
+							 tmp );
+					SH_FREE(msg);
+				      }
+				    else
+				      {
+				        sh_error_handle (ShSuidchkSeverity,
+							 FIL__, __LINE__, 0,
+							 MSG_SUID_QREPORT,
+							 _("Quarantine method applied"),
+							 tmp );
+				      }
+				    break;
+				  case SH_Q_CHANGEPERM:
+				    if (retry_lstat(FIL__, __LINE__, tmpcat, &fileInfo) == -1)
+				      {
+					status = errno;
+					msg = SH_ALLOC(SH_BUFSIZE);
+					(void) sl_snprintf(msg, SH_BUFSIZE, _("I/O error.  errno = %ld"), status);
+					sh_error_handle (ShSuidchkSeverity,
+							 FIL__, __LINE__, 
+							 status,
+							 MSG_SUID_QREPORT, msg,
+							 tmp );
+					SH_FREE(msg);
+				      }
+				    else
+				      {
+					if (0 != (caperr = sl_get_cap_qdel()))
+					  {
+					    sh_error_handle((-1), FIL__, __LINE__, 
+							    caperr, MSG_E_SUBGEN,
+							    sh_error_message (caperr), 
+							    _("sl_get_cap_qdel"));
+					  }
+
+					if ((fileInfo.st_mode & S_ISUID) > 0)
+					  fileInfo.st_mode -= S_ISUID;
+					if ((fileInfo.st_mode & S_ISGID) > 0)
+					  fileInfo.st_mode -= S_ISGID;
+					if (chmod(tmpcat, fileInfo.st_mode) == -1)
+					  {
+					    status = errno;
+					    msg = SH_ALLOC(SH_BUFSIZE);
+					    (void) sl_snprintf(msg, SH_BUFSIZE, _("Problem quarantining file.  File NOT quarantined.  errno = %ld"), status);
+					    sh_error_handle (ShSuidchkSeverity,
+							     FIL__, __LINE__, 
+							     status,
+							     MSG_SUID_QREPORT,
+							     msg, tmp );
+					    SH_FREE(msg);
+					  }
+					else
+					  {
+					    sh_error_handle (ShSuidchkSeverity,
+							     FIL__, __LINE__, 
+							     0,
+							     MSG_SUID_QREPORT,
+							     _("Quarantine method applied"),
+							     tmp );
+					  }
+					if (0 != (caperr = sl_drop_cap_qdel()))
+					  {
+					    sh_error_handle((-1), FIL__, __LINE__, 
+							    caperr, MSG_E_SUBGEN,
+							    sh_error_message (caperr), 
+							    _("sl_drop_cap_qdel"));
+					  }
+				      }
+				    break;
+				  case SH_Q_MOVE:
+				    dir = SH_ALLOC(PATH_MAX+1);
+				    (void) sl_strlcpy (dir, DEFAULT_QDIR, PATH_MAX+1);
+				    if (access (dir, F_OK) != 0)
+				      {
+					status = errno;
+					msg = SH_ALLOC(SH_BUFSIZE);
+					(void) sl_snprintf(msg, SH_BUFSIZE, _("Problem quarantining file.  File NOT quarantined.  errno = %ld (access)"), status);
+					sh_error_handle (ShSuidchkSeverity,
+							 FIL__, __LINE__, 
+							 status,
+							 MSG_SUID_QREPORT, msg,
+							 tmp );
+					SH_FREE(msg);
+				      }
+				    else
+				      {
+					if (retry_lstat (FIL__, __LINE__, 
+							theFile.fullpath, &fileInfo) == -1)
+					  {
+					    status = errno;
+					    msg = SH_ALLOC(SH_BUFSIZE);
+					    (void) sl_snprintf(msg, SH_BUFSIZE, _("I/O error.  errno = %ld(stat)"), status);
+					    sh_error_handle (ShSuidchkSeverity,
+							     FIL__, __LINE__, 
+							     status,
+							     MSG_SUID_QREPORT,
+							     msg, tmp );
+					    SH_FREE(msg);
+					  }
+					else
+					  {
+					    basetmp = SH_ALLOC(1 + sl_strlen(theFile.fullpath));
+					    (void) sl_strlcpy(basetmp, theFile.fullpath, 
+						       1 + sl_strlen(theFile.fullpath));
+					    filetmp = SH_ALLOC(PATH_MAX+1);
+					    (void) sl_snprintf(filetmp, PATH_MAX+1, "%s/%s", 
+							DEFAULT_QDIR, basename(basetmp));
+					    SH_FREE(basetmp);
+					    
+					    readFile  = open (theFile.fullpath, O_RDONLY);
+					    if (readFile != -1)
+					      writeFile = open (filetmp, O_WRONLY|O_CREAT);
+
+					    if ((readFile == -1) || (writeFile == -1))
+					      {
+						status = errno;
+						msg = SH_ALLOC(SH_BUFSIZE);
+						(void) sl_snprintf(msg, SH_BUFSIZE, _("Problem quarantining file.  File NOT quarantined.  errno = %ld (open)"), status);
+						sh_error_handle (ShSuidchkSeverity,
+								 FIL__, __LINE__, status,
+								 MSG_SUID_QREPORT,
+								 msg, tmp );
+						SH_FREE(msg);
+					      }
+					    else
+					      { 
+						/* sizeof(buffer) is 1024 */
+						while ((count = (int) read (readFile, buffer, sizeof (buffer))) > 0)
+						  if ((int) write (writeFile, buffer, (size_t) count) != count)
+						    {
+						      status = errno;
+						      msg = SH_ALLOC(SH_BUFSIZE);
+					    	      (void) sl_snprintf(msg, SH_BUFSIZE, _("I/O error.  errno = %ld (write)"), status);
+						      sh_error_handle (ShSuidchkSeverity,
+								       FIL__,
+								       __LINE__,
+								       status,
+								       MSG_SUID_QREPORT,
+								       msg, tmp );
+						      SH_FREE(msg);
+						    }
+					      }
+					    (void) close (readFile);
+					    (void) fchmod(writeFile, S_IRUSR | S_IWUSR | S_IXUSR);
+					    (void) close (writeFile);
+					    /* if (unlink (theFile.fullpath) == -1) */
+					    if (do_truncate (theFile.fullpath) == -1)
+					      {
+						status = errno;
+						msg = SH_ALLOC(SH_BUFSIZE);
+						(void) sl_snprintf(msg, SH_BUFSIZE, _("Problem quarantining file.  File NOT quarantined.  errno = %ld"), status);
+						sh_error_handle (ShSuidchkSeverity,
+								 FIL__, __LINE__, status,
+								 MSG_SUID_QREPORT,
+								 msg, tmp );
+						SH_FREE(msg);
+					      }
+					    else
+					      {
+						(void) sl_snprintf(filetmp, PATH_MAX+1, "%s/%s.info", 
+								   DEFAULT_QDIR, 
+								   basename(theFile.fullpath));
+						filePtr = fopen (filetmp, "w+");
+						/*@-usedef@*/
+						if (filePtr)
+						  {
+						    fprintf(filePtr, _("File Info:\n filename=%s\n size=%lu\n owner=%s(%d)\n group=%s(%d)\n ctime=%s\n atime=%s\n mtime=%s\n"), 
+							    theFile.fullpath, 
+							    (unsigned long) theFile.size, 
+							    theFile.c_owner, (int) theFile.owner, 
+							    theFile.c_group, (int) theFile.group, 
+							    timestrc, timestra, timestrm);
+						    (void) fclose (filePtr);
+						  }
+						/*@+usedef@*/
+					
+						sh_error_handle (ShSuidchkSeverity,
+							 	 FIL__,__LINE__,
+								 0, MSG_SUID_QREPORT,
+								 _("Quarantine method applied"),
+								 tmp );
+						if (chmod(filetmp, S_IRUSR | S_IWUSR) == -1)
+						  {
+						    status = errno;
+						    msg = SH_ALLOC(SH_BUFSIZE);
+						    (void) sl_snprintf(msg, SH_BUFSIZE, _("Problem setting permissions on quarantined file.  errno = %ld"), status);
+						    sh_error_handle (ShSuidchkSeverity,
+								     FIL__,__LINE__,
+								     status, MSG_SUID_QREPORT,
+								     msg, tmp );
+						    SH_FREE(msg);
+						  }
+					      }
+					    SH_FREE(filetmp);
+					  }
+				      }
+				    SH_FREE(dir);
+				    break;
+				  default:
+				    sh_error_handle (ShSuidchkSeverity, FIL__,
+						     __LINE__, 0, MSG_SUID_QREPORT,
+						     _("Bad quarantine method"),
+						     tmp);
+				    break;
+				}
+			    }
+			  else
+			    {
+			      /* 1.8.1 push file to in-memory database
+			       */
+			      (void) sh_hash_compdata (SH_LEVEL_READONLY,
+						       &theFile, fileHash,
+						       _("[SuidCheck]"),
+						       ShSuidchkSeverity);
+			    }
+			}
+		      else
+			{
+			  (void) sh_hash_compdata (SH_LEVEL_READONLY, 
+						   &theFile, fileHash,
+						   _("[SuidCheck]"),
+						   ShSuidchkSeverity);
+			}
+		    }
+		}
+	      SH_FREE(tmp);
+
+	    }
+	}
+      SH_FREE(tmpcat);
+    }
 #ifdef HAVE_SCHED_YIELD
     if (ShSuidchkYield == S_TRUE)
@@ -1398,23 +890,14 @@
 	  {
 	    status = errno;
-	    SH_MUTEX_LOCK(mutex_thread_nolog);
 	    sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
-			     _("Failed to release time slice"),
+		             _("Failed to release time slice"),
 			     _("sh_suidchk_check_internal") );
-	    SH_MUTEX_UNLOCK(mutex_thread_nolog);
+	    
 	  }
       }
 #endif
-  
-    dirlist = dirlist->next;
-
-  }  while (dirlist != NULL);
-
-
-  kill_sh_dirlist (dirlist_orig);
-
-  sh_dummy_idirlist = NULL;
-  sh_dummy_itmp     = NULL;
-
+  }  while (thisEntry != NULL);
+
+  (void) closedir (thisDir);
   SL_RETURN( (0), _("sh_suidchk_check_internal"));
 }
@@ -1425,28 +908,8 @@
  *
  *************/
-int sh_suidchk_init (struct mod_type * arg)
-{
-#ifndef HAVE_PTHREAD
-  (void) arg;
-#endif
-
+int sh_suidchk_init ()
+{
   if (ShSuidchkActive == S_FALSE)
-    return SH_MOD_FAILED;
-
-#ifdef HAVE_PTHREAD
-  if (arg != NULL && arg->initval < 0 &&
-      (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
-    {
-      if (0 == sh_pthread_create(sh_threaded_module_run, (void *)arg))
-	return SH_MOD_THREAD;
-      else
-	return SH_MOD_FAILED;
-    }
-  else if (arg != NULL && arg->initval == SH_MOD_THREAD &&
-	   (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
-    {
-      return SH_MOD_THREAD;
-    }
-#endif
+    return (-1);
 
   return (0);
@@ -1500,16 +963,11 @@
 int sh_suidchk_check ()
 {
-  volatile int status;
+  int status;
 
   SL_ENTER(_("sh_suidchk_check"));
 
-  if (ShSuidchkActive == S_FALSE)
-    SL_RETURN(-1, _("sh_suidchk_check"));
-
-  SH_MUTEX_LOCK(mutex_thread_nolog);
-  sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, EINVAL, MSG_E_SUBGEN,
+  sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__, EINVAL, MSG_E_SUBGEN,
 		   _("Checking for SUID programs"),
-		   _("sh_suidchk_check") );
-  SH_MUTEX_UNLOCK(mutex_thread_nolog);
+		   _("suidchk_check") );
 
   FileLimNow        = time(NULL);
@@ -1518,15 +976,9 @@
   FileLimTotal      = 0;
 
-#ifdef SH_SUIDTESTDIR
-  status = sh_suidchk_check_internal (SH_SUIDTESTDIR);
-#else
   status = sh_suidchk_check_internal ("/");
-#endif
-
-  SH_MUTEX_LOCK(mutex_thread_nolog);
+
   sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_SUID_SUMMARY,
 		   FileLimTotal,
 		   (long) (time(NULL) - FileLimStart) );
-  SH_MUTEX_UNLOCK(mutex_thread_nolog);
 
   SL_RETURN(status, _("sh_suidchk_check"));
@@ -1539,5 +991,5 @@
  *************/
 
-int sh_suidchk_set_severity  (const char * c)
+int sh_suidchk_set_severity  (char * c)
 {
   int retval;
@@ -1551,9 +1003,7 @@
 }
 
-int sh_suidchk_set_exclude (const char * c)
-{
-  int ret = 0;
+int sh_suidchk_set_exclude (char * c)
+{
   SL_ENTER(_("sh_suidchk_set_exclude"));
-
   if (c == NULL || c[0] == '\0')
     {
@@ -1564,16 +1014,33 @@
     {
       if (ShSuidchkExclude != NULL)
-	sh_suid_exclude_free();
+	SH_FREE(ShSuidchkExclude);
+      ShSuidchkExclude = NULL;
       SL_RETURN(0, _("sh_suidchk_set_exclude"));
     }
 
-  ret = sh_suid_exclude_add(c);
-
-  SL_RETURN(ret, _("sh_suidchk_set_exclude"));
-}
-
-int sh_suidchk_set_timer (const char * c)
-{
-  volatile long val;
+  if (ShSuidchkExclude != NULL)
+    SH_FREE(ShSuidchkExclude);
+
+  /* 1.8.1 add trailing slash
+   */
+  ExcludeLen       = (int) sl_strlen(c);
+  if (c[ExcludeLen-1] != '/')
+    {
+      ExcludeLen++;
+      if ((ExcludeLen <= 0) || (ExcludeLen+1 <= 0)) /* may overflow */
+	{
+	  SL_RETURN(-1, _("sh_suidchk_set_exclude"));
+	}
+    }
+  ShSuidchkExclude = SH_ALLOC((size_t) ExcludeLen + 1);
+  (void) sl_strlcpy(ShSuidchkExclude, c, (size_t)(ExcludeLen + 1));
+  ShSuidchkExclude[ExcludeLen-1] = '/';
+
+  SL_RETURN(0, _("sh_suidchk_set_exclude"));
+}
+
+int sh_suidchk_set_timer (char * c)
+{
+  long val;
 
   SL_ENTER(_("sh_suidchk_set_timer"));
@@ -1581,10 +1048,7 @@
   val = strtol (c, (char **)NULL, 10);
   if (val <= 0)
-    {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
-		       _("suidchk timer"), c);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-    }
+    sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
+                      _("suidchk timer"), c);
+
   val = (val <= 0 ? 7200 : val);
 
@@ -1594,5 +1058,5 @@
 
 
-static void sh_suidchk_free_schedule (void)
+int sh_suidchk_free_schedule ()
 {
   sh_schedule_t * current = ShSuidchkSched;
@@ -1606,17 +1070,8 @@
     }
   ShSuidchkSched = NULL;
-  return;
-}
-
-int sh_suidchk_reconf ()
-{
-  SH_MUTEX_LOCK(mutex_suid_check);
-  sh_suidchk_free_schedule();
-  set_defaults();
-  SH_MUTEX_UNLOCK(mutex_suid_check);
   return 0;
 }
 
-int sh_suidchk_set_schedule (const char * str)
+int sh_suidchk_set_schedule (char * str)
 {
   int status;
@@ -1656,7 +1111,7 @@
 
 
-int sh_suidchk_set_fps (const char * c)
-{
-  volatile long val;
+int sh_suidchk_set_fps (char * c)
+{
+  long val;
 
   SL_ENTER(_("sh_suidchk_set_fps"));
@@ -1664,10 +1119,7 @@
   val = strtol (c, (char **)NULL, 10);
   if (val < 0)
-    {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
-		       _("suidchk fps"), c);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-    }
+    sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
+                      _("suidchk fps"), c);
+
   val = (val < 0 ? 0 : val);
 
@@ -1676,5 +1128,5 @@
 }
 
-int sh_suidchk_set_yield (const char * c)
+int sh_suidchk_set_yield (char * c)
 {
   int i;
@@ -1689,5 +1141,5 @@
 }
 
-int sh_suidchk_set_activate (const char * c)
+int sh_suidchk_set_activate (char * c)
 {
   int i;
@@ -1697,13 +1149,5 @@
 }
 
-int sh_suidchk_set_nosuid (const char * c)
-{
-  int i;
-  SL_ENTER(_("sh_suidchk_set_nosuid"));
-  i = sh_util_flagval(c, &ShSuidchkNosuid);
-  SL_RETURN(i, _("sh_suidchk_set_nosuid"));
-}
-
-int sh_suidchk_set_quarantine (const char * c)
+int sh_suidchk_set_quarantine (char * c)
 {
   int i;
@@ -1713,5 +1157,5 @@
 }
 
-int sh_suidchk_set_qdelete (const char * c)
+int sh_suidchk_set_qdelete (char * c)
 {
   int i;
@@ -1721,9 +1165,8 @@
 }
 
-int sh_suidchk_set_qmethod (const char * c)
-{
-  volatile long val;
-  volatile int  ret = 0;
-  struct stat buf;
+int sh_suidchk_set_qmethod (char * c)
+{
+  long val;
+  int  ret = 0;
 
   SL_ENTER(_("sh_suidchk_set_qmethod"));
@@ -1732,8 +1175,6 @@
   if (val < 0)
     {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
       sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
 		       _("suidchk qmethod"), c);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
       ret = -1;
     }
@@ -1749,13 +1190,11 @@
           break;
         case SH_Q_MOVE:
-          if (retry_stat (FIL__, __LINE__, DEFAULT_QDIR, &buf) != 0)
+          if (access (DEFAULT_QDIR, F_OK) != 0)
 	    {
 	      if (mkdir (DEFAULT_QDIR, 0750) == -1)
 		{
-		  SH_MUTEX_LOCK(mutex_thread_nolog);
 		  sh_error_handle ((-1), FIL__, __LINE__, EINVAL,
 				   MSG_SUID_ERROR,
 				   _("Unable to create quarantine directory"));
-		  SH_MUTEX_UNLOCK(mutex_thread_nolog);
 		}
 	    }
@@ -1763,8 +1202,6 @@
           break;
         default:
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
 	  sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
 			   _("suidchk qmethod"), c);
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
           ShSuidchkQMethod = -1;
 	  ret = -1;
@@ -1943,5 +1380,5 @@
 
 #ifdef FSTYPE_MNTENT		/* 4.3BSD etc.  */
-static int xatoi (const char *cp);
+static int xatoi (char *cp);
 #endif
 
@@ -1992,87 +1429,35 @@
   switch (t)
     {
-#ifdef MOUNT_UFS
     case MOUNT_UFS:
       return _("ufs");
+    case MOUNT_NFS:
+      return _("nfs");
+#ifdef MOUNT_PC
+    case MOUNT_PC:
+      return _("pc");
+#endif
+#ifdef MOUNT_MFS
+    case MOUNT_MFS:
+      return _("mfs");
+#endif
+#ifdef MOUNT_LO
+    case MOUNT_LO:
+      return _("lofs");
+#endif
+#ifdef MOUNT_TFS
+    case MOUNT_TFS:
+      return _("tfs");
+#endif
+#ifdef MOUNT_TMP
+    case MOUNT_TMP:
+      return _("tmp");
+#endif
+#ifdef MOUNT_MSDOS
+    case MOUNT_MSDOS:
+      return _("msdos");
 #endif
 #ifdef MOUNT_ISO9660
     case MOUNT_ISO9660:
       return _("iso9660fs");
-#endif
-#ifdef MOUNT_CD9660
-    case MOUNT_CD9660:
-      return _("cd9660");
-#endif
-#ifdef MOUNT_NFS
-    case MOUNT_NFS:
-      return _("nfs");
-#endif
-#ifdef MOUNT_PC
-    case MOUNT_PC:
-      return _("pc");
-#endif
-#ifdef MOUNT_MFS
-    case MOUNT_MFS:
-      return _("mfs");
-#endif
-#ifdef MOUNT_LO
-    case MOUNT_LO:
-      return _("lofs");
-#endif
-#ifdef MOUNT_TFS
-    case MOUNT_TFS:
-      return _("tfs");
-#endif
-#ifdef MOUNT_TMP
-    case MOUNT_TMP:
-      return _("tmp");
-#endif
-#ifdef MOUNT_MSDOS
-    case MOUNT_MSDOS:
-      return _("msdos");
-#endif
-#ifdef MOUNT_LFS
-    case MOUNT_LFS:
-      return _("lfs");
-#endif
-#ifdef MOUNT_LOFS
-    case MOUNT_LOFS:
-      return _("lofs");
-#endif
-#ifdef MOUNT_FDESC
-    case MOUNT_FDESC:
-      return _("fdesc");
-#endif
-#ifdef MOUNT_PORTAL
-    case MOUNT_PORTAL:
-      return _("portal");
-#endif
-#ifdef MOUNT_NULL
-    case MOUNT_NULL:
-      return _("null");
-#endif
-#ifdef MOUNT_UMAP
-    case MOUNT_UMAP:
-      return _("umap");
-#endif
-#ifdef MOUNT_KERNFS
-    case MOUNT_KERNFS:
-      return _("kernfs");
-#endif
-#ifdef MOUNT_PROCFS
-    case MOUNT_PROCFS:
-      return _("procfs");
-#endif
-#ifdef MOUNT_DEVFS
-    case MOUNT_DEVFS:
-      return _("devfs");
-#endif
-#ifdef MOUNT_EXT2FS
-    case MOUNT_EXT2FS:
-      return _("ext2fs");
-#endif
-#ifdef MOUNT_UNION
-    case MOUNT_UNION:
-      return _("union");
 #endif
     default:
@@ -2198,5 +1583,6 @@
    Return "unknown" if its filesystem type is unknown.  */
 
-static char * filesystem_type (char * path, char * relpath, struct stat * statp)
+static char *
+filesystem_type (char * path, char * relpath, struct stat * statp)
 {
   static char *current_fstype = NULL;
@@ -2211,5 +1597,4 @@
   current_dev = statp->st_dev;
   current_fstype = filesystem_type_uncached (path, relpath, statp);
-  
   return current_fstype;
 }
@@ -2220,7 +1605,9 @@
    Return "unknown" if its filesystem type is unknown.  */
 
-void * sh_dummy_2229_type;
-
-static char * filesystem_type_uncached (char * path, char * relpath, struct stat * statp)
+static char *
+filesystem_type_uncached (path, relpath, statp)
+     char *path;
+     char *relpath;
+     struct stat *statp;
 {
   char * type = NULL;
@@ -2229,5 +1616,5 @@
 #endif
 
-#ifdef FSTYPE_MNTENT		/* 4.3BSD, SunOS, HP-UX, Dynix, Irix,Linux  */
+#ifdef FSTYPE_MNTENT		/* 4.3BSD, SunOS, HP-UX, Dynix, Irix.  */
   char *table = MOUNTED;
   FILE *mfp;
@@ -2237,18 +1624,10 @@
     return NULL;
 
-  /* Take the address to keep gcc from putting it into a register. 
-   * Avoids the 'clobbered by longjmp' warning. 
-   */
-  sh_dummy_2229_type = (void *) &type;
-
   mfp = setmntent (table, "r");
   if (mfp == NULL)
     {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
       sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
 		       _("setmntent() failed"),
 		       _("filesystem_type_uncached") );
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      sh_dummy_2229_type = NULL;
       return NULL;
     }
@@ -2258,5 +1637,5 @@
   while (type == NULL && (mnt = getmntent (mfp)) != NULL)
     {
-      const char *devopt;
+      char *devopt;
       dev_t dev;
       struct stat disk_stats;
@@ -2289,23 +1668,8 @@
 	  if (stat (mnt->mnt_dir, &disk_stats) == -1)
 	    {
-	      char errmsg[256];
-	      volatile int  elevel = SH_ERR_ERR;
-	      size_t tlen = strlen(mnt->mnt_dir);
-	      
-	      if (tlen >= 6 && 0 == strcmp(&((mnt->mnt_dir)[tlen-6]), _("/.gvfs")))
-		elevel = SH_ERR_NOTICE;
-	      else if (tlen >= 5 && 0 == strcmp(&((mnt->mnt_dir)[tlen-5]), _("/gvfs")))
-		elevel = SH_ERR_NOTICE;
-	      else if (0 == strcmp (mnt->mnt_type, _("tracefs")))
-		elevel = SH_ERR_NOTICE;
-	      
-	      sl_snprintf(errmsg, sizeof(errmsg), _("stat(%s) failed"),
-			  mnt->mnt_dir);
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle (elevel, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-			       errmsg,
+	      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
+			       _("stat() failed"),
 			       _("filesystem_type_uncached") );
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	      continue;
+	      return NULL;
 	    }
 	  dev = disk_stats.st_dev;
@@ -2316,12 +1680,8 @@
 	  /* check for the "nosuid" option
 	   */
-#ifdef HAVE_HASMNTOPT
-	  if (NULL == hasmntopt(mnt, "nosuid") || (ShSuidchkNosuid == S_TRUE))
+	  if (NULL == hasmntopt(mnt, "nosuid"))
 	    type = mnt->mnt_type;
 	  else
-	    type = _("nosuid"); /* hasmntopt (nosuid) */
-#else
-	  type = mnt->mnt_type;
-#endif
+	    type = _("nosuid");
 	}
     }
@@ -2329,9 +1689,7 @@
   if (endmntent (mfp) == 0)
     {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
       sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
 		       _("endmntent() failed"),
 		       _("filesystem_type_uncached") );
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
     }
 #endif
@@ -2344,9 +1702,4 @@
     return NULL;
 
-  /* Take the address to keep gcc from putting it into a register. 
-   * Avoids the 'clobbered by longjmp' warning. 
-   */
-  sh_dummy_2229_type = (void*) &type;
-
   while (type == NULL
 	 && getmnt (&offset, &fsd, sizeof (fsd), NOSTAT_MANY, 0) > 0)
@@ -2364,9 +1717,4 @@
     return NULL;
 
-  /* Take the address to keep gcc from putting it into a register. 
-   * Avoids the 'clobbered by longjmp' warning. 
-   */
-  sh_dummy_2229_type = (void*) &type;
-
   if (statfs (relpath, &fss, sizeof (struct statfs), 0) == -1)
     {
@@ -2374,10 +1722,7 @@
       if (errno != ENOENT)
 	{
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
 	  sh_error_handle ((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
 			   _("statfs() failed"),
 			   _("filesystem_type_uncached") );
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  sh_dummy_2229_type = NULL;
 	  return NULL;
 	}
@@ -2392,9 +1737,4 @@
   if (path == NULL || relpath == NULL)
     return NULL;
-
-  /* Take the address to keep gcc from putting it into a register. 
-   * Avoids the 'clobbered by longjmp' warning. 
-   */
-  sh_dummy_2229_type = (void*) &type;
 
   if (statvfs (relpath, &fss) == -1)
@@ -2403,10 +1743,7 @@
       if (errno != ENOENT)
 	{
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
 	  sh_error_handle ((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
 			   _("statvfs() failed"),
 			   _("filesystem_type_uncached") );
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  sh_dummy_2229_type = NULL;
 	  return NULL;
 	}
@@ -2418,5 +1755,5 @@
        /* patch by Konstantin Khrooschev <nathoo@co.ru> 
 	*/
-       if( (fss.f_flag & ST_NOSUID)  && (ShSuidchkNosuid == S_FALSE))
+       if( fss.f_flag & ST_NOSUID )
          type = _("nosuid");
     }
@@ -2435,9 +1772,4 @@
     return NULL;
 
-  /* Take the address to keep gcc from putting it into a register. 
-   * Avoids the 'clobbered by longjmp' warning. 
-   */
-  sh_dummy_2229_type = (void*) &type;
-
   if (S_ISLNK (statp->st_mode))
     p = sh_dirname (relpath);
@@ -2451,10 +1783,7 @@
       if (errno != ENOENT)
 	{
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
 	  sh_error_handle ((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
 			   _("statfs() failed"),
 			   _("filesystem_type_uncached") );
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  sh_dummy_2229_type = NULL;
 	  return NULL;
 	}
@@ -2476,7 +1805,7 @@
 #ifdef MNT_VISFLAGMASK
       flags = fss.f_flags & MNT_VISFLAGMASK;
-      if ((flags & MNT_NOSUID) && (ShSuidchkNosuid == S_FALSE))
+      if (flags & MNT_NOSUID)
 #else 
-      if ((fss.f_flags & MNT_NOSUID) && (ShSuidchkNosuid == S_FALSE)) 
+      if (fss.f_flags & MNT_NOSUID) 
 #endif
          type = _("nosuid");
@@ -2496,5 +1825,4 @@
   fstype_known = (int)(type != NULL);
 
-  sh_dummy_2229_type = NULL;
   return sh_util_strdup (type ? type : "unknown");
 }
@@ -2505,5 +1833,7 @@
    part of CP. */
 
-static int xatoi (const char * cp)
+static int
+xatoi (cp)
+     char *cp;
 {
   int val;
Index: trunk/src/sh_tiger0.c
===================================================================
--- trunk/src/sh_tiger0.c	(revision 591)
+++ trunk/src/sh_tiger0.c	(revision 1)
@@ -24,16 +24,10 @@
 #include "sh_error.h"
 #include "sh_utils.h"
-#include "sh_pthread.h"
-#include "sh_string.h"
-
-#define PRIV_MAX  32768
-
-#if defined(TIGER_64_BIT)
+
+#if defined(HAVE_LONG_64) || defined(HAVE_LONG_LONG_64)
 #if defined(HAVE_LONG_64)
 typedef unsigned long int word64;
-#elif defined(HAVE_LONG_LONG_64)
+#else
 typedef unsigned long long int word64;
-#else
-#error No 64 bit type found !
 #endif
 #endif
@@ -52,27 +46,16 @@
 #define GPGFORMAT (_("%08X %08X %08X  %08X %08X %08X"))
 #else
-#error No 32 bit type found !
-#endif
-
-#if defined(__GNUC__) && (__GNUC__ >= 3)
-#undef  SH_GNUC_ALIGN8
-#define SH_GNUC_ALIGN8   __attribute__((aligned(8)))
-#else
-#undef  SH_GNUC_ALIGN8
-#define SH_GNUC_ALIGN8
-#endif
-
+#error No 32 byte type found !
+#endif
 
 typedef unsigned char sh_byte;
-
-#define SH_KEY_NULL _("000000000000000000000000000000000000000000000000")
 
 #undef  FIL__
 #define FIL__  _("sh_tiger0.c")
 
-#if defined(TIGER_64_BIT)
-
-void tiger_t(const word64 *str, word64 length, word64 * res);
-void tiger(const word64 *str, word64 length, word64 * res);
+#if defined(HAVE_LONG_64) || defined(HAVE_LONG_LONG_64)
+/* #ifdef HAVE_LONG_64 */
+void tiger_t(word64 *str, word64 length, word64 * res);
+void tiger(word64 *str, word64 length, word64 * res);
 
 #ifdef TIGER_DBG
@@ -84,6 +67,6 @@
 #endif
 #else
-void tiger(const sh_word32 *str, sh_word32 length, sh_word32 * res);
-void tiger_t(const sh_word32 *str, sh_word32 length, sh_word32 * res);
+void tiger(sh_word32 *str, sh_word32 length, sh_word32 * res);
+void tiger_t(sh_word32 *str, sh_word32 length, sh_word32 * res);
 
 #ifdef TIGER_DBG
@@ -110,28 +93,28 @@
  * implementation
  */
-
-/* static sh_byte buffer[PRIV_MAX + 72]; */
-
-#if defined(TIGER_64_BIT)
+SL_TICKET tiger_fd = (-1);
+
+
+#if defined(HAVE_LONG_64) || defined(HAVE_LONG_LONG_64)
+/* #ifdef HAVE_LONG_64 */
 static
-word64 * sh_tiger_hash_val (const char * filename, TigerType what, 
-			    UINT64 * Length, int timeout, word64 * res)
+word64 * sh_tiger_hash_val (char * filename, TigerType what, 
+			    unsigned long Length, int timeout)
 #else
 static
-sh_word32 * sh_tiger_hash_val (const char * filename, TigerType what, 
-			       UINT64 * Length, int timeout, sh_word32 * res)
+sh_word32 * sh_tiger_hash_val (char * filename, TigerType what, 
+			       unsigned long Length, int timeout)
 #endif
 {
   SL_TICKET  fd;
-  sh_string * content = NULL;
-  int  i, j, tt;
+  int  i, j;
   int  count = 0;
   int  blk;
   char    * tmp;
   sh_byte * bptr;
-  sh_byte   SH_GNUC_ALIGN8 bbuf[64];
-  UINT64    bcount = 0;
-
-  sh_byte * buffer = SH_ALLOC(PRIV_MAX + 72);
+  sh_byte buffer[PRIV_MAX + 72];
+  sh_byte bbuf[64];
+
+  unsigned long pages_read;
   uid_t   euid;
 
@@ -140,5 +123,4 @@
 
 #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
-  unsigned long pages_read;
   /*@-nestedextern@*/
   extern long IO_Limit;
@@ -146,13 +128,14 @@
 #endif
 
-#if defined(TIGER_64_BIT)
-#define TIGER_CAST (const word64*)
-  /* word64 res[3]; */
+#if defined(HAVE_LONG_64) || defined(HAVE_LONG_LONG_64)
+  /* #ifdef HAVE_LONG_64 */
+#define TIGER_CAST (word64*)
+  static word64 res[3];
   res[0]= (word64) 0x0123456789ABCDEFLL;
   res[1]= (word64) 0xFEDCBA9876543210LL;
   res[2]= (word64) 0xF096A5B4C3B2E187LL;
 #else
-#define TIGER_CAST (const sh_word32*)
-  /* sh_word32 res[6]; */
+#define TIGER_CAST (sh_word32*)
+  static sh_word32 res[6];
   res[0]= (sh_word32) 0x89ABCDEF;
   res[1]= (sh_word32) 0x01234567;
@@ -165,22 +148,21 @@
   SL_ENTER(_("sh_tiger_hash_val"));
 
-  if (what >= TIGER_FILE) 
-    {
-      if (what > TIGER_FILE)
+  if (what == TIGER_FILE || what == TIGER_FD) 
+    {
+      if (what == TIGER_FILE)
 	{
-	  fd      = what;
-	  content = sl_get_content(fd);
-	  TPT((0,FIL__, __LINE__, _("msg=<TIGER_FD>, fd=<%ld>\n"), fd));
+	  TPT((0,FIL__, __LINE__, _("msg=<TIGER_FILE>, path=<%s>\n"),
+	       (filename == NULL ? _("(null)") : filename) ));
+	  fd = sl_open_read (filename, SL_YESPRIV);
 	}
       else
 	{
-	  TPT((0,FIL__, __LINE__, _("msg=<TIGER_FILE>, path=<%s>\n"),
-	       (filename == NULL ? _("(null)") : filename) ));
-	  fd = sl_open_read (FIL__, __LINE__, filename, SL_YESPRIV);
+	  fd = tiger_fd;
+	  TPT((0,FIL__, __LINE__, _("msg=<TIGER_FD>, fd=<%ld>\n"), tiger_fd));
 	}
 
       if (SL_ISERROR (fd)) 
 	{
-	  TPT((0, FIL__, __LINE__, _("msg=<SL_ISERROR (%ld)>\n"), fd));
+	  TPT((0, FIL__, __LINE__, _("msg=<SL_ISERROR (%ld)>\n"), tiger_fd));
 	  tmp = sh_util_safe_name (filename);
 	  (void) sl_get_euid(&euid);
@@ -188,28 +170,16 @@
 			   MSG_E_ACCESS, (long) euid, tmp);
 	  SH_FREE(tmp);
-	  SH_FREE(buffer);
-	  *Length = 0;
 	  SL_RETURN( NULL, _("sh_tiger_hash_val"));
 	}
-
+      
 #if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
-    if (skey->mlock_failed == S_FALSE) 
+    if (skey->mlock_failed == SL_FALSE) 
       {
-        if ( (-1) == sh_unix_mlock(FIL__, __LINE__, 
-				   (char *)buffer, 
-				   (PRIV_MAX)*sizeof(sh_byte))) 
-	  {
-	    SH_MUTEX_LOCK_UNSAFE(mutex_skey);  
-	    skey->mlock_failed = S_TRUE;
-	    SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
-	  }
+        if ( (-1) == sh_unix_mlock((char *)buffer,(PRIV_MAX)*sizeof(sh_byte))) 
+          skey->mlock_failed = SL_TRUE;
       }
 #else
-    if (skey->mlock_failed == S_FALSE)
-      {
-	SH_MUTEX_LOCK_UNSAFE(mutex_skey);  
-	skey->mlock_failed = S_TRUE;
-	SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
-      }
+    if (skey->mlock_failed == SL_FALSE)
+      skey->mlock_failed = SL_TRUE;
 #endif
 
@@ -218,27 +188,18 @@
 #endif
 
-#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) 
     pages_read = 0;
-#endif
-
-    while (1) 
+
+    while (1 == 1) 
       {
 	if (timeout > 0)
-	  count = sl_read_timeout (fd, buffer, PRIV_MAX, timeout, S_TRUE);
+	  count = sl_read_timeout (fd, buffer, PRIV_MAX, timeout);
 	else
 	  count = sl_read         (fd, buffer, PRIV_MAX);
 
-#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) 
 	++pages_read;
-#endif
 
 	if (SL_ISERROR (count)) 
 	  {
-	    int error = errno;
-
 	    if (sig_termfast == 1) {
-	      sh_unix_munlock((char *)buffer, (PRIV_MAX)*sizeof(sh_byte));
-	      SH_FREE(buffer);
-	      *Length = 0;
 	      SL_RETURN( NULL, _("sh_tiger_hash_val"));
 	    }
@@ -253,36 +214,12 @@
 	      }
 	    else
-	      {
-		char errbuf[SH_ERRBUF_SIZE];
-		char errbuf2[SH_ERRBUF_SIZE];
-		sl_strlcpy(errbuf, sl_error_string(count), sizeof(errbuf));
-		sh_error_message(error, errbuf2, sizeof(errbuf2));
-		sh_error_handle (ShDFLevel[SH_ERR_T_FILE], FIL__, __LINE__, 
-				 count, MSG_E_READ, errbuf, errbuf2, tmp);
-	      }
+	      sh_error_handle (ShDFLevel[SH_ERR_T_FILE], FIL__, __LINE__, 
+			       count, MSG_E_READ, tmp);
 	    SH_FREE(tmp);
-	    memset (bbuf,   0, 64);
-	    memset (buffer, 0, PRIV_MAX);
-
-	    sh_unix_munlock((char *)buffer, (PRIV_MAX)*sizeof(sh_byte));
-	    SH_FREE(buffer);
-	    *Length = 0;
+	    (void) sh_unix_munlock((char*)buffer, (PRIV_MAX)*sizeof(sh_byte));
 	    SL_RETURN( NULL, _("sh_tiger_hash_val"));
 	  }
 
-	if (content)
-	  sh_string_cat_lchar(content, (char*)buffer, count);
-
-	bcount += count;
-
-	if (*Length != TIGER_NOLIM)
-	  {
-	    if (bcount > *Length)
-	      {
-		count   = count - (bcount - (*Length));
-		bcount  = *Length;
-		count = (count < 0) ? 0 : count;
-	      }
-	  }
+	/* TPT((0, FIL__, __LINE__ , _("msg=<Read %ld bytes>\n"), count)); */
 
 	blk      = (count / 64); /* number of 64-byte words */
@@ -291,18 +228,14 @@
 	 * count cannot be negative here, see 'if (SL_ISERROR (count))'
 	 */
-	tt = blk*64;
-
-	ncount = (unsigned long) (count - tt);
-
-	nblocks += blk;
-	/* MAY_LOCK */
-	sh.statistics.bytes_hashed += tt;
+	ncount = (unsigned long) (count - ((count/64)*64));
 	
-	tt = 0;
+	bptr = buffer;
 	for (i = 0; i < blk; ++i)
 	  {
-	    bptr = &buffer[tt]; tt += 64;
+	    bptr = &buffer[64 * i];
 	    
 	    tiger_t(TIGER_CAST bptr, 64, res);
+	    ++nblocks;
+	    sh.statistics.bytes_hashed += 64;
 	    
 #ifdef TIGER_DBG
@@ -311,5 +244,5 @@
 	  }
 	
-	if (blk < (PRIV_MAX / 64)) /* this must be (PRIV_MAX / 64) */
+	if (blk < 64) /* this must be (PRIV_MAX / 64) */
 	  break;
 
@@ -319,7 +252,6 @@
 	    memset (bbuf,   0, 64);
 	    memset (buffer, 0, PRIV_MAX);
-	    sh_unix_munlock((char *)buffer, (PRIV_MAX)*sizeof(sh_byte));
-	    SH_FREE(buffer);
-	    *Length = 0;
+	    (void) sh_unix_munlock( (char *) buffer, 
+				    (PRIV_MAX) * sizeof(sh_byte));
 	    SL_RETURN( NULL, _("sh_tiger_hash_val"));
 	  }
@@ -336,12 +268,6 @@
     /* copy incomplete block
      */
-    j = 0; 
-    for (i = 0; i < 64; i += 4) 
-      {
-	bbuf[i]   = (sh_byte) '\0';
-	bbuf[i+1] = (sh_byte) '\0';
-	bbuf[i+2] = (sh_byte) '\0';
-	bbuf[i+3] = (sh_byte) '\0';
-      }
+    j = 0; for (i = 0; i < 64; ++i) 
+      bbuf[i] = (sh_byte) '\0';
     for (i = (count/64) * 64; i < count; ++i)
       /*@-usedef@*/bbuf[j++] = buffer[i];/*@+usedef@*/
@@ -376,10 +302,6 @@
 	  bbuf[j++] = (sh_byte) 0;
 	tiger_t(TIGER_CAST bbuf, 64, res);
-	/* MAY_LOCK */
 	sh.statistics.bytes_hashed += 64;
-	++nblocks; 
-#ifdef TIGER_DBG
-	ncount = 0;
-#endif
+	++nblocks; ncount = 0;
         memset(bbuf, 0, 56 ); 
       }
@@ -405,12 +327,15 @@
 #endif
 
-    memset (bbuf,   0, sizeof(bbuf));
-    memset (buffer, 0, sizeof(buffer));
+    memset (bbuf,   0, 64);
+    memset (buffer, 0, PRIV_MAX);
+
+    (void) sh_unix_munlock( (char *) buffer, (PRIV_MAX) * sizeof(sh_byte));
 
     if (what == TIGER_FILE)
       (void) sl_close (fd);
-    sh_unix_munlock((char *)buffer, (PRIV_MAX)*sizeof(sh_byte));
-    SH_FREE(buffer);
-    *Length = bcount;
+    else
+      tiger_fd = (-1);
+
+
     SL_RETURN( res, _("sh_tiger_hash_val"));
   }
@@ -418,12 +343,8 @@
   if (what == TIGER_DATA && filename != NULL) 
     {
-      tiger(TIGER_CAST filename, (sh_word32) *Length, res);
-      sh_unix_munlock((char *)buffer, (PRIV_MAX)*sizeof(sh_byte));
-      SH_FREE(buffer);
+      /* TPT((0, FIL__, __LINE__, _("msg=<TIGER_DATA>\n"))); */
+      tiger(TIGER_CAST filename,  (sh_word32) Length, res); 
       SL_RETURN(res, _("sh_tiger_hash_val"));
     }
-  sh_unix_munlock((char *)buffer, (PRIV_MAX)*sizeof(sh_byte));
-  SH_FREE(buffer);
-  *Length = 0;
   SL_RETURN( NULL, _("sh_tiger_hash_val"));
 }
@@ -434,449 +355,302 @@
 #ifdef USE_MD5
 /*@-type@*/
-/* md5.c - Functions to compute MD5 message digest of files or memory blocks
- *         according to the definition of MD5 in RFC 1321 from April 1992.
- * Copyright (C) 1995, 1996 Free Software Foundation, Inc.
+/************************************************************************
  *
- * NOTE: The canonical source of this file is maintained with the GNU C
- * Library.  Bugs can be reported to bug-glibc@prep.ai.mit.edu.
+ *  md5.h - Declaration of functions and data types used for MD5 sum
+ *  computing library functions. 
  *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; either version 2, or (at your option) any
- * later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-/* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.  */
-
+ ************************************************************************/
+
+/* Written Bob Deblier <bob@virtualunlimited.com>         */
 /* Hacked to work with samhain by R. Wichmann             */
-
-typedef UINT32 md5_uint32;
+/* Need for 64bit type removed, fix for Mac OS X compiler */
+
+typedef sh_word32     uint32;
+typedef unsigned char uint8;
+
+
+
 
 
 /* Structure to save state of computation between the single steps.  */
-typedef struct md5_ctx
-{
-  md5_uint32 A;
-  md5_uint32 B;
-  md5_uint32 C;
-  md5_uint32 D;
-
-  md5_uint32 total[2];
-  md5_uint32 buflen;
-  char buffer[128];
+typedef struct
+{
+        uint32 h[4];
+        uint32 data[16];
+        uint8  offset;
+        uint32  nblocks;
+        int  count;
 } md5Param;
 
-/*
- * The following three functions are build up the low level used in
- * the functions `md5_stream' and `md5_buffer'.
- */
-
-/* Initialize structure containing state of computation.
-   (RFC 1321, 3.3: Step 3)  */
-static void md5_init_ctx (struct md5_ctx *ctx);
-
-/* Starting with the result of former calls of this function (or the
-   initialization function update the context for the next LEN bytes
-   starting at BUFFER.
-   It is necessary that LEN is a multiple of 64!!! */
-static void md5_process_block (const void *buffer, size_t len,
-				    struct md5_ctx *ctx);
-
-/* Starting with the result of former calls of this function (or the
-   initialization function update the context for the next LEN bytes
-   starting at BUFFER.
-   It is NOT required that LEN is a multiple of 64.  */
-static void md5_process_bytes (const void *buffer, size_t len,
-				    struct md5_ctx *ctx);
-
-/* Process the remaining bytes in the buffer and put result from CTX
-   in first 16 bytes following RESBUF.  The result is always in little
-   endian byte order, so that a byte-wise output yields to the wanted
-   ASCII representation of the message digest.
-
-   IMPORTANT: On some systems it is required that RESBUF is correctly
-   aligned for a 32 bits value.  */
-static void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf);
-
-
-/* Put result from CTX in first 16 bytes following RESBUF.  The result is
-   always in little endian byte order, so that a byte-wise output yields
-   to the wanted ASCII representation of the message digest.
-
-   IMPORTANT: On some systems it is required that RESBUF is correctly
-   aligned for a 32 bits value.  */
-static void *md5_read_ctx (const struct md5_ctx *ctx, void *resbuf);
-
-#if WORDS_BIGENDIAN
-static md5_uint32 swapu32(md5_uint32 n)
-{
-  return (    ((n & 0xffU) << 24) |
-	      ((n & 0xff00U) << 8) |
-	      ((n & 0xff0000U) >> 8) |
-	      ((n & 0xff000000U) >> 24) );
-}
-#define SWAP(n) swapu32(n)
-#else
-#define SWAP(n) (n)
-#endif
-
-/* This array contains the bytes used to pad the buffer to the next
-   64-byte boundary.  (RFC 1321, 3.1: Step 1)  */
-static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */  };
-
-/* Initialize structure containing state of computation.
-   (RFC 1321, 3.3: Step 3)  */
-static void md5_init_ctx(struct md5_ctx *ctx)
-{
-  ctx->A = 0x67452301;
-  ctx->B = 0xefcdab89;
-  ctx->C = 0x98badcfe;
-  ctx->D = 0x10325476;
-
-  ctx->total[0] = ctx->total[1] = 0;
-  ctx->buflen = 0;
-}
-
-/* Put result from CTX in first 16 bytes following RESBUF.  The result
-   must be in little endian byte order.
-
-   IMPORTANT: On some systems it is required that RESBUF is correctly
-   aligned for a 32 bits value.  */
-static void *md5_read_ctx(const struct md5_ctx *ctx, void *resbuf)
-{
-  ((md5_uint32 *) resbuf)[0] = SWAP(ctx->A);
-  ((md5_uint32 *) resbuf)[1] = SWAP(ctx->B);
-  ((md5_uint32 *) resbuf)[2] = SWAP(ctx->C);
-  ((md5_uint32 *) resbuf)[3] = SWAP(ctx->D);
-
-  return resbuf;
-}
-
-/* Process the remaining bytes in the internal buffer and the usual
-   prolog according to the standard and write the result to RESBUF.
-
-   IMPORTANT: On some systems it is required that RESBUF is correctly
-   aligned for a 32 bits value.  */
-static void *md5_finish_ctx(struct md5_ctx *ctx, void *resbuf)
-{
-  /* Take yet unprocessed bytes into account.  */
-  md5_uint32 bytes = ctx->buflen;
-  size_t pad;
-  md5_uint32 temp;
-
-  /* Now count remaining bytes.  */
-  ctx->total[0] += bytes;
-  if (ctx->total[0] < bytes)
-    ++ctx->total[1];
-
-  pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
-  memcpy(&ctx->buffer[bytes], fillbuf, pad);
-
-  /* Put the 64-bit file length in *bits* at the end of the buffer.  */
-  temp = SWAP(ctx->total[0] << 3);
-  memcpy(&(ctx->buffer[bytes + pad]), &temp, sizeof(temp));
-  temp = SWAP((ctx->total[1] << 3) | (ctx->total[0] >> 29));
-  memcpy(&(ctx->buffer[bytes + pad + 4]), &temp, sizeof(temp));
-
-  /* Process last bytes.  */
-  md5_process_block(ctx->buffer, bytes + pad + 8, ctx);
-
-  return md5_read_ctx(ctx, resbuf);
-}
-
-/* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
-   result is always in little endian byte order, so that a byte-wise
-   output yields to the wanted ASCII representation of the message
-   digest.  */
-void *md5_buffer(const char *buffer, size_t len, void *resblock)
-{
-  struct md5_ctx ctx;
-
-  /* Initialize the computation context.  */
-  md5_init_ctx(&ctx);
-
-  /* Process whole buffer but last len % 64 bytes.  */
-  md5_process_bytes(buffer, len, &ctx);
-
-  /* Put result in desired memory area.  */
-  return md5_finish_ctx(&ctx, resblock);
-}
-
-static void md5_process_bytes(const void *buffer, size_t len, struct md5_ctx *ctx)
-{
-  /* When we already have some bits in our internal buffer concatenate
-     both inputs first.  */
-  if (ctx->buflen != 0) {
-    size_t left_over = ctx->buflen;
-    size_t add = 128 - left_over > len ? len : 128 - left_over;
-
-    memcpy(&ctx->buffer[left_over], buffer, add);
-    ctx->buflen += add;
-
-    if (left_over + add > 64) {
-      md5_process_block(ctx->buffer, (left_over + add) & ~63, ctx);
-      /* The regions in the following copy operation cannot overlap.  */
-      memcpy(ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
-	     (left_over + add) & 63);
-      ctx->buflen = (left_over + add) & 63;
-    }
-
-    buffer = (const char *) buffer + add;
-    len -= add;
-  }
-
-  /* Process available complete blocks.  */
-  if (len > 64) {
-    md5_process_block(buffer, len & ~63, ctx);
-    buffer = (const char *) buffer + (len & ~63);
-    len &= 63;
-  }
-
-  /* Move remaining bytes in internal buffer.  */
-  if (len > 0) {
-    memcpy(ctx->buffer, buffer, len);
-    ctx->buflen = len;
-  }
-}
-
-/* These are the four functions used in the four steps of the MD5 algorithm
-   and defined in the RFC 1321.  The first function is a little bit optimized
-   (as found in Colin Plumbs public domain implementation).  */
-/* #define FF(b, c, d) ((b & c) | (~b & d)) */
-#define FF(b, c, d) (d ^ (b & (c ^ d)))
-#define FG(b, c, d) FF (d, b, c)
-#define FH(b, c, d) (b ^ c ^ d)
-#define FI(b, c, d) (c ^ (b | ~d))
-
-/* Process LEN bytes of BUFFER, accumulating context into CTX.
-   It is assumed that LEN % 64 == 0.  */
-static void md5_process_block(const void *buffer, size_t len, struct md5_ctx *ctx)
-{
-  md5_uint32 correct_words[16];
-  const md5_uint32 *words = buffer;
-  size_t nwords = len / sizeof(md5_uint32);
-  const md5_uint32 *endp = words + nwords;
-  md5_uint32 A = ctx->A;
-  md5_uint32 B = ctx->B;
-  md5_uint32 C = ctx->C;
-  md5_uint32 D = ctx->D;
-
-  /* First increment the byte count.  RFC 1321 specifies the possible
-     length of the file up to 2^64 bits.  Here we only compute the
-     number of bytes.  Do a double word increment.  */
-  ctx->total[0] += len;
-  if (ctx->total[0] < len)
-    ++ctx->total[1];
-
-  /* Process all bytes in the buffer with 64 bytes in each round of
-     the loop.  */
-  while (words < endp) {
-    md5_uint32 *cwp = correct_words;
-    md5_uint32 A_save = A;
-    md5_uint32 B_save = B;
-    md5_uint32 C_save = C;
-    md5_uint32 D_save = D;
-
-    /* First round: using the given function, the context and a constant
-       the next context is computed.  Because the algorithms processing
-       unit is a 32-bit word and it is determined to work on words in
-       little endian byte order we perhaps have to change the byte order
-       before the computation.  To reduce the work for the next steps
-       we store the swapped words in the array CORRECT_WORDS.  */
-
-#define OP(a, b, c, d, s, T)						\
-      do								\
-        {								\
-	  a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T;		\
-	  ++words;							\
-	  CYCLIC (a, s);						\
-	  a += b;							\
-        }								\
-      while (0)
-
-    /* It is unfortunate that C does not provide an operator for
-       cyclic rotation.  Hope the C compiler is smart enough.  */
-#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
-
-    /* Before we start, one word to the strange constants.
-       They are defined in RFC 1321 as
-
-       T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
-    */
-
-    /* Round 1.  */
-    OP(A, B, C, D, 7, 0xd76aa478);
-    OP(D, A, B, C, 12, 0xe8c7b756);
-    OP(C, D, A, B, 17, 0x242070db);
-    OP(B, C, D, A, 22, 0xc1bdceee);
-    OP(A, B, C, D, 7, 0xf57c0faf);
-    OP(D, A, B, C, 12, 0x4787c62a);
-    OP(C, D, A, B, 17, 0xa8304613);
-    OP(B, C, D, A, 22, 0xfd469501);
-    OP(A, B, C, D, 7, 0x698098d8);
-    OP(D, A, B, C, 12, 0x8b44f7af);
-    OP(C, D, A, B, 17, 0xffff5bb1);
-    OP(B, C, D, A, 22, 0x895cd7be);
-    OP(A, B, C, D, 7, 0x6b901122);
-    OP(D, A, B, C, 12, 0xfd987193);
-    OP(C, D, A, B, 17, 0xa679438e);
-    OP(B, C, D, A, 22, 0x49b40821);
-    /* For the second to fourth round we have the possibly swapped words
-       in CORRECT_WORDS.  Redefine the macro to take an additional first
-       argument specifying the function to use.  */
-#undef OP
-#define OP(f, a, b, c, d, k, s, T)					\
-      do 								\
-	{								\
-	  a += f (b, c, d) + correct_words[k] + T;			\
-	  CYCLIC (a, s);						\
-	  a += b;							\
-	}								\
-      while (0)
-
-    /* Round 2.  */
-    OP(FG, A, B, C, D, 1, 5, 0xf61e2562);
-    OP(FG, D, A, B, C, 6, 9, 0xc040b340);
-    OP(FG, C, D, A, B, 11, 14, 0x265e5a51);
-    OP(FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
-    OP(FG, A, B, C, D, 5, 5, 0xd62f105d);
-    OP(FG, D, A, B, C, 10, 9, 0x02441453);
-    OP(FG, C, D, A, B, 15, 14, 0xd8a1e681);
-    OP(FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
-    OP(FG, A, B, C, D, 9, 5, 0x21e1cde6);
-    OP(FG, D, A, B, C, 14, 9, 0xc33707d6);
-    OP(FG, C, D, A, B, 3, 14, 0xf4d50d87);
-    OP(FG, B, C, D, A, 8, 20, 0x455a14ed);
-    OP(FG, A, B, C, D, 13, 5, 0xa9e3e905);
-    OP(FG, D, A, B, C, 2, 9, 0xfcefa3f8);
-    OP(FG, C, D, A, B, 7, 14, 0x676f02d9);
-    OP(FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
-
-    /* Round 3.  */
-    OP(FH, A, B, C, D, 5, 4, 0xfffa3942);
-    OP(FH, D, A, B, C, 8, 11, 0x8771f681);
-    OP(FH, C, D, A, B, 11, 16, 0x6d9d6122);
-    OP(FH, B, C, D, A, 14, 23, 0xfde5380c);
-    OP(FH, A, B, C, D, 1, 4, 0xa4beea44);
-    OP(FH, D, A, B, C, 4, 11, 0x4bdecfa9);
-    OP(FH, C, D, A, B, 7, 16, 0xf6bb4b60);
-    OP(FH, B, C, D, A, 10, 23, 0xbebfbc70);
-    OP(FH, A, B, C, D, 13, 4, 0x289b7ec6);
-    OP(FH, D, A, B, C, 0, 11, 0xeaa127fa);
-    OP(FH, C, D, A, B, 3, 16, 0xd4ef3085);
-    OP(FH, B, C, D, A, 6, 23, 0x04881d05);
-    OP(FH, A, B, C, D, 9, 4, 0xd9d4d039);
-    OP(FH, D, A, B, C, 12, 11, 0xe6db99e5);
-    OP(FH, C, D, A, B, 15, 16, 0x1fa27cf8);
-    OP(FH, B, C, D, A, 2, 23, 0xc4ac5665);
-
-    /* Round 4.  */
-    OP(FI, A, B, C, D, 0, 6, 0xf4292244);
-    OP(FI, D, A, B, C, 7, 10, 0x432aff97);
-    OP(FI, C, D, A, B, 14, 15, 0xab9423a7);
-    OP(FI, B, C, D, A, 5, 21, 0xfc93a039);
-    OP(FI, A, B, C, D, 12, 6, 0x655b59c3);
-    OP(FI, D, A, B, C, 3, 10, 0x8f0ccc92);
-    OP(FI, C, D, A, B, 10, 15, 0xffeff47d);
-    OP(FI, B, C, D, A, 1, 21, 0x85845dd1);
-    OP(FI, A, B, C, D, 8, 6, 0x6fa87e4f);
-    OP(FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
-    OP(FI, C, D, A, B, 6, 15, 0xa3014314);
-    OP(FI, B, C, D, A, 13, 21, 0x4e0811a1);
-    OP(FI, A, B, C, D, 4, 6, 0xf7537e82);
-    OP(FI, D, A, B, C, 11, 10, 0xbd3af235);
-    OP(FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
-    OP(FI, B, C, D, A, 9, 21, 0xeb86d391);
-
-    /* Add the starting values of the context.  */
-    A += A_save;
-    B += B_save;
-    C += C_save;
-    D += D_save;
-  }
-
-  /* Put checksum in context given as argument.  */
-  ctx->A = A;
-  ctx->B = B;
-  ctx->C = C;
-  ctx->D = D;
-}
-
-
-/*----------------------------------------------------------------------------
- *--------end of md5.c
- *----------------------------------------------------------------------------*/
+static uint32 md5hinit[4] = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 };
 
  
 int md5Reset(register md5Param* p)
 {
-        unsigned int i;
-
-        md5_init_ctx(p);
-	
-        for (i = 0; i < 16; i += 8)
-	  {
-	    p->buffer[i]   = 0x00;
-	    p->buffer[i+1] = 0x00;
-	    p->buffer[i+2] = 0x00;
-	    p->buffer[i+3] = 0x00;
-	    p->buffer[i+4] = 0x00;
-	    p->buffer[i+5] = 0x00;
-	    p->buffer[i+6] = 0x00;
-	    p->buffer[i+7] = 0x00;
-	  }
-	
+        memcpy(p->h, md5hinit, 16);
+        memset(p->data, 0x00, 64);
+        p->offset = (uint8) 0;
+	p->nblocks = 0;
         return 0;
 }
 
+#if defined(__GNUC__) && defined(__i386__)
+static inline UINT32
+ROTL32( UINT32 x, int s)
+{
+        __asm__("roll %%cl,%0"
+                :"=r" (x)
+                :"0" (x),"c" (s));
+        return x;
+}
+#else
+#define ROTL32(x, s) (((x) << (s)) | ((x) >> (32 - (s))))
+#endif
+
+
+#define FF(a, b, c, d, w, s, t) \
+        a += ((b&(c^d))^d) + w + t;     \
+        a = ROTL32(a, s);       \
+        a += b;
+
+#define GG(a, b, c, d, w, s, t) \
+        a += ((d&(b^c))^c) + w + t;     \
+        a = ROTL32(a, s);       \
+        a += b;
+
+#define HH(a, b, c, d, w, s, t) \
+        a += (b^c^d) + w + t;   \
+        a = ROTL32(a, s);       \
+        a += b;
+
+#define II(a, b, c, d, w, s, t) \
+        a += (c^(b|~d)) + w + t;        \
+        a = ROTL32(a, s);       \
+        a += b;
+
+#if WORDS_BIGENDIAN
+uint32 swapu32(uint32 n)
+{
+        return (    ((n & 0xffU) << 24) |
+                                ((n & 0xff00U) << 8) |
+                                ((n & 0xff0000U) >> 8) |
+                                ((n & 0xff000000U) >> 24) );
+}
+#endif
+
+static
+void md5Process(md5Param* p)
+{
+        register uint32 a,b,c,d;
+        register uint32* w;
+        #if WORDS_BIGENDIAN
+        register sh_byte t;
+        #endif
+
+        w = p->data;
+        #if WORDS_BIGENDIAN
+        t = 16;
+        while (t--)
+        {
+                register uint32 temp = swapu32(*w);
+                *(w++) = temp;
+        }
+        w = p->data;
+        #endif
+
+        a = p->h[0]; b = p->h[1]; c = p->h[2]; d = p->h[3];
+
+        FF(a, b, c, d, (*w++),  7, 0xd76aa478);
+        FF(d, a, b, c, (*w++), 12, 0xe8c7b756);
+        FF(c, d, a, b, (*w++), 17, 0x242070db);
+        FF(b, c, d, a, (*w++), 22, 0xc1bdceee);
+        FF(a, b, c, d, (*w++),  7, 0xf57c0faf);
+        FF(d, a, b, c, (*w++), 12, 0x4787c62a);
+        FF(c, d, a, b, (*w++), 17, 0xa8304613);
+        FF(b, c, d, a, (*w++), 22, 0xfd469501);
+        FF(a, b, c, d, (*w++),  7, 0x698098d8);
+        FF(d, a, b, c, (*w++), 12, 0x8b44f7af);
+        FF(c, d, a, b, (*w++), 17, 0xffff5bb1);
+        FF(b, c, d, a, (*w++), 22, 0x895cd7be);
+        FF(a, b, c, d, (*w++),  7, 0x6b901122);
+        FF(d, a, b, c, (*w++), 12, 0xfd987193);
+        FF(c, d, a, b, (*w++), 17, 0xa679438e);
+        FF(b, c, d, a, (*w++), 22, 0x49b40821);
+
+	w = p->data;
+
+        GG(a, b, c, d, w[ 1],  5, 0xf61e2562);
+        GG(d, a, b, c, w[ 6],  9, 0xc040b340);
+        GG(c, d, a, b, w[11], 14, 0x265e5a51);
+        GG(b, c, d, a, w[ 0], 20, 0xe9b6c7aa);
+        GG(a, b, c, d, w[ 5],  5, 0xd62f105d);
+        GG(d, a, b, c, w[10],  9, 0x02441453);
+        GG(c, d, a, b, w[15], 14, 0xd8a1e681);
+        GG(b, c, d, a, w[ 4], 20, 0xe7d3fbc8);
+        GG(a, b, c, d, w[ 9],  5, 0x21e1cde6);
+        GG(d, a, b, c, w[14],  9, 0xc33707d6);
+        GG(c, d, a, b, w[ 3], 14, 0xf4d50d87);
+        GG(b, c, d, a, w[ 8], 20, 0x455a14ed);
+        GG(a, b, c, d, w[13],  5, 0xa9e3e905);
+        GG(d, a, b, c, w[ 2],  9, 0xfcefa3f8);
+        GG(c, d, a, b, w[ 7], 14, 0x676f02d9);
+        GG(b, c, d, a, w[12], 20, 0x8d2a4c8a);
+
+        HH(a, b, c, d, w[ 5],  4, 0xfffa3942);
+        HH(d, a, b, c, w[ 8], 11, 0x8771f681);
+        HH(c, d, a, b, w[11], 16, 0x6d9d6122);
+        HH(b, c, d, a, w[14], 23, 0xfde5380c);
+        HH(a, b, c, d, w[ 1],  4, 0xa4beea44);
+        HH(d, a, b, c, w[ 4], 11, 0x4bdecfa9);
+        HH(c, d, a, b, w[ 7], 16, 0xf6bb4b60);
+        HH(b, c, d, a, w[10], 23, 0xbebfbc70);
+        HH(a, b, c, d, w[13],  4, 0x289b7ec6);
+        HH(d, a, b, c, w[ 0], 11, 0xeaa127fa);
+        HH(c, d, a, b, w[ 3], 16, 0xd4ef3085);
+        HH(b, c, d, a, w[ 6], 23, 0x04881d05);
+        HH(a, b, c, d, w[ 9],  4, 0xd9d4d039);
+        HH(d, a, b, c, w[12], 11, 0xe6db99e5);
+        HH(c, d, a, b, w[15], 16, 0x1fa27cf8);
+        HH(b, c, d, a, w[ 2], 23, 0xc4ac5665);
+
+        II(a, b, c, d, w[ 0],  6, 0xf4292244);
+        II(d, a, b, c, w[ 7], 10, 0x432aff97);
+        II(c, d, a, b, w[14], 15, 0xab9423a7);
+        II(b, c, d, a, w[ 5], 21, 0xfc93a039);
+        II(a, b, c, d, w[12],  6, 0x655b59c3);
+        II(d, a, b, c, w[ 3], 10, 0x8f0ccc92);
+        II(c, d, a, b, w[10], 15, 0xffeff47d);
+        II(b, c, d, a, w[ 1], 21, 0x85845dd1);
+        II(a, b, c, d, w[ 8],  6, 0x6fa87e4f);
+        II(d, a, b, c, w[15], 10, 0xfe2ce6e0);
+        II(c, d, a, b, w[ 6], 15, 0xa3014314);
+        II(b, c, d, a, w[13], 21, 0x4e0811a1);
+        II(a, b, c, d, w[ 4],  6, 0xf7537e82);
+        II(d, a, b, c, w[11], 10, 0xbd3af235);
+        II(c, d, a, b, w[ 2], 15, 0x2ad7d2bb);
+        II(b, c, d, a, w[ 9], 21, 0xeb86d391);
+
+        p->h[0] += a;
+        p->h[1] += b;
+        p->h[2] += c;
+        p->h[3] += d;
+}
+
 int md5Update(md5Param* p, const sh_byte* data, int size)
 {
-  md5_process_bytes(data, size, p);
-  return 0;
-}
-
-static void md5Finish(md5Param* p, void *resblock)
-{
-  (void) md5_finish_ctx(p, resblock);
-}
-
-int md5Digest(md5Param* p, md5_uint32* data)
-{
-        md5Finish(p, data);
+        register int proclength;
+
+        while (size > 0)
+        {
+	  proclength = (((int)p->offset + size) > 64) ? 
+	    (64 - (int)p->offset) : size;
+	  memcpy(((sh_byte *) p->data) + p->offset, data, (size_t) proclength);
+	  size -= proclength;
+	  data += proclength;
+	  p->offset += proclength;
+	  
+	  if (p->offset == (uint8) 64)
+	    {
+	      md5Process(p);
+	      p->offset = (uint8) 0;
+	      p->nblocks++;
+	    }
+        }
+        return 0;
+}
+
+static void md5Finish(md5Param* p)
+{
+        uint32 t, msb, lsb;
+	uint8 * pp;
+        register uint8 *ptr;
+
+        msb = 0;
+	t = p->nblocks;
+	if( (lsb = t << 6) < t ) /* multiply by 64 to make a byte count */
+	  msb++;
+	msb += t >> 26;
+	t = lsb;
+	if( (lsb = t + (uint32)p->offset) < t ) /* add the count */
+	  msb++;
+	t = lsb;
+	if( (lsb = t << 3) < t ) /* multiply by 8 to make a bit count */
+	  msb++;
+	msb += t >> 29;
+
+	ptr = ((uint8 *) p->data) + p->offset++;
+
+ 
+        *(ptr++) = (uint8) 0x80;
+
+        if (p->offset > (uint8)56)
+        {
+                while (p->offset++ < 64)
+                        *(ptr++) = 0;
+
+                md5Process(p);
+                p->offset = 0;
+        }
+
+        ptr = ((uint8 *) p->data) + p->offset;
+        while (p->offset++ < 56)
+                *(ptr++) = 0;
+
+	/* append the 64 bit count */
+	*(ptr++) = lsb	   ;
+	*(ptr++) = lsb >>  8;
+	*(ptr++) = lsb >> 16;
+	*(ptr++) = lsb >> 24;
+	*(ptr++) = msb	   ;
+	*(ptr++) = msb >>  8;
+	*(ptr++) = msb >> 16;
+	*(ptr++) = msb >> 24;
+
+        md5Process(p);
+
+	pp = (uint8 *) p->data;
+#ifdef WORDS_BIGENDIAN
+#define X(a) do { *pp++ = (*p).a; *pp++ = (*p).a >> 8;      \
+		  *pp++ = (*p).a >> 16; *pp++ = (*p).a >> 24; } while(0)
+#else /* little endian */
+    /*#define X(a) do { *(uint32*)p = p->##a ; p += 4; } while(0)*/
+    /* Unixware's cpp doesn't like the above construct so we do it his way:
+     * (reported by Allan Clark) */
+#define X(a) do { *(uint32*)pp = (*p).a ; pp += 4; } while(0)
+#endif
+	X(h[0]);
+	X(h[1]);
+	X(h[2]);
+	X(h[3]);
+#undef X
+
+        p->offset = 0;
+}
+
+int md5Digest(md5Param* p, uint32* data)
+{
+        md5Finish(p);
+        memcpy(data, p->h, 16);
         (void) md5Reset(p);
         return 0;
 }
 /*@+type@*/
-
 
 /* Compute MD5 message digest for bytes read from STREAM.  The
    resulting message digest number will be written into the 16 bytes
    beginning at RESBLOCK.  */
-static int md5_stream(char * filename, void *resblock, 
-		      UINT64 * Length, int timeout, SL_TICKET fd)
+static int md5_stream(char * filename, void *resblock, int timeout)
 {
   /* Important: BLOCKSIZE must be a multiple of 64.  */
   static const int BLOCKSIZE = 8192;
-  struct md5_ctx ctx;
-  char * buffer = SH_ALLOC(8264); /* BLOCKSIZE + 72  AIX compiler chokes */
-  size_t sum;
-
+  md5Param ctx;
+  char buffer[8264]; /* BLOCKSIZE + 72  AIX compiler chokes */
+  off_t sum = 0;
+  SL_TICKET  fd;
   char * tmp;
   uid_t   euid;
-  UINT64  bcount = 0;
-  sh_string * content;
-
-#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
+
   unsigned long pages_read;
-
+#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) 
   /*@-nestedextern@*/
   extern long IO_Limit;
@@ -887,7 +661,9 @@
   (void) md5Reset (&ctx);
 
+  fd = tiger_fd;
+
   if (SL_ISERROR (fd))
     {
-      TPT((0, FIL__, __LINE__, _("msg=<SL_ISERROR (%ld)>\n"), fd));
+      TPT((0, FIL__, __LINE__, _("msg=<SL_ISERROR (%ld)>\n"), tiger_fd));
       tmp = sh_util_safe_name (filename);
       (void) sl_get_euid(&euid);
@@ -895,17 +671,11 @@
 		       MSG_E_ACCESS, (long) euid, tmp);
       SH_FREE(tmp);
-      *Length = 0;
-      SH_FREE(buffer);
       return -1;
     }
 
-#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
   pages_read = 0;
-#endif
-  
-  content = sl_get_content(fd);
 
   /* Iterate over full file contents.  */
-  while (1) {
+  while (1 == 1) {
     /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
        computation function processes the whole buffer so that with the
@@ -918,15 +688,10 @@
 
       n = (off_t) sl_read_timeout (fd, buffer + sum, 
-				   (size_t) BLOCKSIZE - sum, timeout, S_FALSE);
+				   (size_t) BLOCKSIZE - sum, timeout);
 
       if (SL_ISERROR (n))
 	{
-	  int error = errno;
-
 	  if (sig_termfast == 1)
-	    {
-	      SH_FREE(buffer);
-	      return -1;
-	    }
+	    return -1;
 	  TPT((0, FIL__ , __LINE__ , _("msg=<SL_ISERROR (%ld)>\n"), n));
 	  tmp = sh_util_safe_name (filename);
@@ -939,41 +704,16 @@
 	    }
 	  else
-	    {
-	      char errbuf[SH_ERRBUF_SIZE];
-	      char errbuf2[SH_ERRBUF_SIZE];
-	      sl_strlcpy(errbuf, sl_error_string(n), sizeof(errbuf));
-	      sh_error_message(error, errbuf2, sizeof(errbuf2));
-	      sh_error_handle (ShDFLevel[SH_ERR_T_FILE], FIL__, __LINE__, n,
-			       MSG_E_READ, errbuf, errbuf2, tmp);
-	    }
+	    sh_error_handle (ShDFLevel[SH_ERR_T_FILE], FIL__, __LINE__, n,
+			     MSG_E_READ, tmp);
 	  SH_FREE(tmp);
-	  *Length = 0;
-	  SH_FREE(buffer);
 	  return -1;
 	}
 
-      if (content)
-	sh_string_cat_lchar(content, buffer, n);
-
-      bcount += n;
-
-      if (*Length != TIGER_NOLIM)
-	{
-	  if (bcount > *Length) 
-	    {
-	      n = n - (bcount - (*Length));
-	      bcount = *Length;
-	      n = (n < 0) ? 0 : n;
-	    }
-	}
-
       sum += n;
     }
-    while (sum < (size_t) BLOCKSIZE 
+    while (sum < (off_t) BLOCKSIZE 
 	   && n != 0);
 
-#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
     ++pages_read;
-#endif
 
     /* If end of file is reached, end the loop.  */
@@ -984,5 +724,5 @@
        BLOCKSIZE % 64 == 0
     */
-    md5_process_block(buffer, BLOCKSIZE, &ctx);
+    (void) md5Update(&ctx, (sh_byte*) buffer, BLOCKSIZE);
     sh.statistics.bytes_hashed += BLOCKSIZE;
 
@@ -995,6 +735,4 @@
     if (sig_termfast == 1) 
       {
-	*Length = 0;
-	SH_FREE(buffer);
 	return -1;
       }
@@ -1005,5 +743,5 @@
   if (sum > 0)
     {
-      md5_process_bytes(buffer, sum, &ctx);
+      (void) md5Update(&ctx, (sh_byte*) buffer, (int) sum);
       sh.statistics.bytes_hashed += BLOCKSIZE;
     }
@@ -1012,6 +750,4 @@
   (void) md5Digest(&ctx, resblock);
 
-  *Length = bcount;
-  SH_FREE(buffer);
   return 0;
 }
@@ -1019,22 +755,29 @@
 static
 char * sh_tiger_md5_hash  (char * filename, TigerType what, 
-			   UINT64 * Length, int timeout, char * out, size_t len)
-{
-  int cnt;
-  char outbuf[KEY_LEN+1];
+			   unsigned long Length, int timeout)
+{
+  int cnt = (int) Length;
+  static char out[KEY_LEN+1];
   unsigned char md5buffer[16];
 
-  (void) md5_stream (filename, md5buffer, Length, timeout, what);
+  if (what != TIGER_FD)
+    {
+      sh_error_handle (ShDFLevel[SH_ERR_T_FILE], FIL__, __LINE__, 0,
+		       MSG_E_SUBGEN, _("Not TIGER_FD"), 
+		       _("sh_tiger_md5_hash"));
+      return out;
+    }
+
+  (void) md5_stream (filename, md5buffer, timeout);
 
   /*@-bufferoverflowhigh -usedef@*/
   for (cnt = 0; cnt < 16; ++cnt)
-    sl_snprintf (&outbuf[cnt*2], 3, _("%02X"),                 /* known to fit  */
-	      (unsigned int) md5buffer[cnt]);
+    sprintf (&out[cnt*2], _("%02X"),                 /* known to fit  */
+	     (unsigned int) md5buffer[cnt]);
   /*@+bufferoverflowhigh +usedef@*/
   for (cnt = 32; cnt < KEY_LEN; ++cnt)
-    outbuf[cnt] = '0';
-  outbuf[KEY_LEN] = '\0';
-
-  sl_strlcpy(out, outbuf, len);
+    out[cnt] = '0';
+  out[KEY_LEN] = '\0';
+
   return out;
 }
@@ -1403,29 +1146,20 @@
 /*@+type@*/
 
-#include "sh_checksum.h"
-
-#define SH_VAR_SHA1   0
-#define SH_VAR_SHA256 1
-
 /* Compute SHA1 message digest for bytes read from STREAM.  The
    resulting message digest number will be written into the 16 bytes
    beginning at RESBLOCK.  */
-static int SHA_stream(char * filename, void *resblock, 
-		      UINT64 * Length, int timeout, SL_TICKET fd, int variant)
+static int sha1_stream(char * filename, void *resblock, int timeout)
 {
   /* Important: BLOCKSIZE must be a multiple of 64.  */
   static const int BLOCKSIZE = 4096;
   struct sha_ctx ctx;
-  SHA256_CTX ctx_sha2;
-  char * buffer = SH_ALLOC(4168); /* BLOCKSIZE + 72 AIX compiler chokes */
+  char buffer[4168]; /* BLOCKSIZE + 72 AIX compiler chokes */
   off_t sum = 0;
+  SL_TICKET  fd;
   char * tmp;
-  uid_t  euid;
-  UINT64 bcount = 0;
-  sh_string * content;
-
+  uid_t   euid;
+
+  unsigned long pages_read;
 #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) 
-  unsigned long pages_read;
-
   /*@-nestedextern@*/
   extern long IO_Limit;
@@ -1434,12 +1168,11 @@
 
   /* Initialize the computation context.  */
-  if (variant == SH_VAR_SHA256)
-    (void) SHA256_Init(&ctx_sha2);
-  else
-    (void) sha_init(&ctx);
+  (void) sha_init(&ctx);
+
+  fd = tiger_fd;
 
   if (SL_ISERROR (fd))
     {
-      TPT((0, FIL__, __LINE__, _("msg=<SL_ISERROR (%ld)>\n"), fd));
+      TPT((0, FIL__, __LINE__, _("msg=<SL_ISERROR (%ld)>\n"), tiger_fd));
       tmp = sh_util_safe_name (filename);
       (void) sl_get_euid(&euid);
@@ -1447,6 +1180,4 @@
 		       MSG_E_ACCESS, (long) euid, tmp);
       SH_FREE(tmp);
-      *Length = 0;
-      SH_FREE(buffer);
       return -1;
     }
@@ -1454,9 +1185,5 @@
   /* Iterate over full file contents.  */
 
-#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) 
   pages_read = 0;
-#endif
-  
-  content = sl_get_content(fd);
 
   while (1 == 1) {
@@ -1470,15 +1197,10 @@
     do {
       n = (off_t) sl_read_timeout(fd, buffer + sum, 
-				  (size_t) BLOCKSIZE - sum, timeout, S_FALSE);
+				  (size_t) BLOCKSIZE - sum, timeout);
 
       if (SL_ISERROR (n))
 	{
-	  int error = errno;
-
 	  if (sig_termfast == 1)
-	    {
-	      SH_FREE(buffer);
-	      return -1;
-	    }
+	    return -1;
 
 	  TPT((0, FIL__ , __LINE__ , _("msg=<SL_ISERROR (%ld)>\n"), n));
@@ -1493,32 +1215,11 @@
 	  else 
 	    {
-	      char errbuf[SH_ERRBUF_SIZE];
-	      char errbuf2[SH_ERRBUF_SIZE];
-	      sl_strlcpy(errbuf, sl_error_string(n), sizeof(errbuf));
-	      sh_error_message(error, errbuf2, sizeof(errbuf2));
 	      sh_error_handle (ShDFLevel[SH_ERR_T_FILE], FIL__, __LINE__, n,
-			       MSG_E_READ, errbuf, errbuf2, tmp);
+			       MSG_E_READ, tmp);
 	    }
 	  SH_FREE(tmp);
-	  *Length = 0;
-	  SH_FREE(buffer);
 	  return -1;
 	}
 
-      if (content)
-	sh_string_cat_lchar(content, buffer, n);
-
-      bcount += n;
-
-      if (*Length != TIGER_NOLIM)
-	{
-	  if (bcount > *Length)
-	    {
-	      n = n - (bcount - (*Length));
-	      bcount = *Length;
-	      n = (n < 0) ? 0 : n;
-	    }
-	}
-
       sum += n;
     }
@@ -1526,8 +1227,6 @@
 	   && n != 0);
 
-#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) 
     ++pages_read;
-#endif
-    
+
     /* If end of file is reached, end the loop.  */
     if (n == 0)
@@ -1537,8 +1236,5 @@
        BLOCKSIZE % 64 == 0
     */
-    if (variant == SH_VAR_SHA256)
-      SHA256_Update(&ctx_sha2, (sha2_byte*) buffer, (size_t) BLOCKSIZE);
-    else
-      sha_update(&ctx, (sha_word8*) buffer, (sha_word32) BLOCKSIZE);
+    sha_update(&ctx, (sha_word8*) buffer, (sha_word32) BLOCKSIZE);
     sh.statistics.bytes_hashed += BLOCKSIZE;
 
@@ -1551,6 +1247,4 @@
     if (sig_termfast == 1) 
       {
-	*Length = 0;
-	SH_FREE(buffer);
 	return -1;
       }
@@ -1562,24 +1256,12 @@
   if (sum > 0)
     {
-      if (variant == SH_VAR_SHA256)
-	SHA256_Update(&ctx_sha2, (sha2_byte*) buffer, (size_t) sum);
-      else
-	sha_update(&ctx, (sha_word8*) buffer, (sha_word32) sum);
+      sha_update(&ctx, (sha_word8*) buffer, (sha_word32) sum);
       sh.statistics.bytes_hashed += sum;
     }
 
+  sha_final (&ctx);
+
   /* Construct result in desired memory.  */
-  if (variant == SH_VAR_SHA256)
-    {
-      SHA256_End(&ctx_sha2, resblock);
-    }
-  else
-    {
-      sha_final (&ctx);
-      sha_digest (&ctx, resblock);
-    }
-
-  *Length = bcount;
-  SH_FREE(buffer);
+  sha_digest (&ctx, resblock);
   return 0;
 }
@@ -1587,42 +1269,36 @@
 
 static char * sh_tiger_sha1_hash  (char * filename, TigerType what, 
-				   UINT64 * Length, int timeout, 
-				   char * out, size_t len)
-{
-  int cnt;
-  char outbuf[KEY_LEN+1];
+				   unsigned long Length, int timeout)
+{
+  int cnt = (int) Length;  /* fix compiler warning */
+  static char out[KEY_LEN+1];
   unsigned char sha1buffer[20];
 
-  (void) SHA_stream (filename, sha1buffer, Length, timeout, what, SH_VAR_SHA1);
+  if (what != TIGER_FD)
+    {
+      sh_error_handle (ShDFLevel[SH_ERR_T_FILE], FIL__, __LINE__, 0,
+		       MSG_E_SUBGEN, _("Not TIGER_FD"), 
+		       _("sh_tiger_sha1_hash"));
+      return out;
+    }
+
+  (void) sha1_stream (filename, sha1buffer, timeout);
 
   /*@-bufferoverflowhigh -usedef@*/
   for (cnt = 0; cnt < 20; ++cnt)
-    sl_snprintf (&outbuf[cnt*2], 3, _("%02X"),              /* known to fit  */
+    sprintf (&out[cnt*2], _("%02X"),                 /* known to fit  */
 	     (unsigned int) sha1buffer[cnt]);
   /*@+bufferoverflowhigh +usedef@*/
   for (cnt = 40; cnt < KEY_LEN; ++cnt)
-    outbuf[cnt] = '0';
-  outbuf[KEY_LEN] = '\0';
-
-  sl_strlcpy(out, outbuf, len);
+    out[cnt] = '0';
+  out[KEY_LEN] = '\0';
+
   return out;
 }
 
-static char * sh_tiger_sha256_hash  (char * filename, TigerType what, 
-				     UINT64 * Length, int timeout, 
-				     char * out, size_t len)
-{
-  char outbuf[KEYBUF_SIZE];
-
-  (void) SHA_stream (filename, outbuf, Length, timeout, what, SH_VAR_SHA256);
-
-  sl_strlcpy(out, outbuf, len);
-  return out;
-}
-
 /* ifdef USE_SHA1 */
 #endif
 
-static int hash_type = SH_TIGER192;
+static int hash_type = 0;
 
 int sh_tiger_get_hashtype ()
@@ -1631,34 +1307,5 @@
 }
 
-void sh_tiger_get_mask_hashtype(unsigned long * mask)
-{
-  if (hash_type == SH_TIGER192) 
-    *mask |= MODI_TIGER192;
-  else if (hash_type == SH_SHA1)
-    *mask |= MODI_SHA1;
-  else if (hash_type == SH_MD5)
-    *mask |= MODI_MD5;
-  else if (hash_type == SH_SHA256)
-    *mask |= MODI_SHA256;
-  return;
-}
-
-void sh_tiger_set_hashtype_mask(unsigned long mask)
-{
-  unsigned long type = mask & MODI_HASHTYPE;
-
-  if (type == MODI_TIGER192)
-    hash_type = SH_TIGER192;
-  else if (type == MODI_SHA1)
-    hash_type = SH_SHA1;
-  else if (type == MODI_MD5)
-    hash_type = SH_MD5;
-  else if (type == MODI_SHA256)
-    hash_type = SH_SHA256;
-  return;
-}
-
-
-int sh_tiger_hashtype (const char * c)
+int sh_tiger_hashtype (char * c)
 {
   SL_ENTER( _("sh_tiger_hashtype"));
@@ -1670,53 +1317,41 @@
 
   if (0 == strcmp(c, _("TIGER192")))
-    hash_type = SH_TIGER192;
+    hash_type = 0;
+#ifdef USE_MD5
+  else if (0 == strcmp(c, _("SHA1")))    
+    hash_type = 1;
+#endif
 #ifdef USE_SHA1
-  else if (0 == strcmp(c, _("SHA1")))    
-    hash_type = SH_SHA1;
+  else if (0 == strcmp(c, _("MD5")))    
+    hash_type = 2;
+#endif
+  else
+    {
+      SL_RETURN( -1, _("sh_tiger_hashtype"));
+    }
+  SL_RETURN( 0, _("sh_tiger_hashtype"));
+}
+
+static char * sh_tiger_hash_internal (char * filename, TigerType what, 
+				      unsigned long Length, int timeout);
+
+char * sh_tiger_hash (char * filename, TigerType what, 
+		      unsigned long Length)
+{
+  return sh_tiger_hash_internal (filename, what, Length, 0);
+}
+
+char * sh_tiger_generic_hash (char * filename, TigerType what, 
+			      unsigned long Length, int timeout)
+{
+#ifdef USE_SHA1
+  if (hash_type == 1)
+    return sh_tiger_sha1_hash    (filename, what, Length, timeout);
 #endif
 #ifdef USE_MD5
-  else if (0 == strcmp(c, _("MD5")))    
-    hash_type = SH_MD5;
-#endif
-#ifdef USE_SHA1
-  else if (0 == strcmp(c, _("SHA256")))    
-    hash_type = SH_SHA256;
-#endif
-  else
-    {
-      SL_RETURN( -1, _("sh_tiger_hashtype"));
-    }
-  SL_RETURN( 0, _("sh_tiger_hashtype"));
-}
-
-static char * sh_tiger_hash_internal (const char * filename, TigerType what, 
-				      UINT64 * Length, int timeout,
-				      char * out, size_t len);
-
-char * sh_tiger_hash (const char * filename, TigerType what, 
-		      UINT64 Length, char * out, size_t len)
-{
-  UINT64 local_length = Length;
-  char * retval = sh_tiger_hash_internal (filename, what, &local_length, 0, out,len);
-  return retval;
-}
-
-char * sh_tiger_generic_hash (char * filename, TigerType what, 
-			      UINT64 * Length, int timeout,
-			      char * out, size_t len)
-{
-#ifdef USE_SHA1
-  if (hash_type == SH_SHA1)
-    return sh_tiger_sha1_hash    (filename, what, Length, timeout, out, len);
-#endif
-#ifdef USE_MD5
-  if (hash_type == SH_MD5)
-    return sh_tiger_md5_hash     (filename, what, Length, timeout, out, len);
-#endif
-#ifdef USE_SHA1
-  if (hash_type == SH_SHA256)
-    return sh_tiger_sha256_hash  (filename, what, Length, timeout, out, len);
-#endif
-  return sh_tiger_hash_internal  (filename, what, Length, timeout, out, len);
+  if (hash_type == 2)
+    return sh_tiger_md5_hash     (filename, what, Length, timeout);
+#endif
+  return sh_tiger_hash_internal  (filename, what, Length, timeout);
 }
 
@@ -1724,95 +1359,103 @@
  * -------   end new ---------  */
   
-static char * sh_tiger_hash_internal (const char * filename, TigerType what, 
-				      UINT64 * Length, int timeout, 
-				      char * out, size_t len)
-{
-#if defined(TIGER_64_BIT)
-  word64 res[3];
+static char * sh_tiger_hash_internal (char * filename, TigerType what, 
+				      unsigned long Length, int timeout)
+{
+#if defined(HAVE_LONG_64) || defined(HAVE_LONG_LONG_64)
+  /* #ifdef HAVE_LONG_64 */
+  word64 * res;
 #else
-  sh_word32 res[6];
-#endif
-
-  SL_ENTER( _("sh_tiger_hash_internal"));
-
-  SH_VALIDATE_GE(len, (KEY_LEN+1));
-
-  if (NULL != sh_tiger_hash_val (filename, what, Length, timeout, res))
-    {
-#if defined(TIGER_64_BIT)
-      sl_snprintf(out, len,
-		  MYFORMAT,
-		  (sh_word32)(res[0]>>32), 
-		  (sh_word32)(res[0]), 
-		  (sh_word32)(res[1]>>32), 
-		  (sh_word32)(res[1]), 
-		  (sh_word32)(res[2]>>32), 
-		  (sh_word32)(res[2]) );
+  sh_word32 * res;
+#endif
+  static char out[KEY_LEN+1];
+
+  SL_ENTER( _("sh_tiger_hash"));
+
+  res = sh_tiger_hash_val (filename, what, Length, timeout);
+
+  if (res != NULL)
+    {
+      /*@-bufferoverflowhigh -formatconst@*/
+#if defined(HAVE_LONG_64) || defined(HAVE_LONG_LONG_64)
+      /* #ifdef HAVE_LONG_64 */
+      sprintf(out,                                   /* known to fit  */
+	      MYFORMAT,
+	      (sh_word32)(res[0]>>32), 
+	      (sh_word32)(res[0]), 
+	      (sh_word32)(res[1]>>32), 
+	      (sh_word32)(res[1]), 
+	      (sh_word32)(res[2]>>32), 
+	      (sh_word32)(res[2]) );
 #else
-      sl_snprintf(out, len,
-		  MYFORMAT,
-		  (sh_word32)(res[1]), 
-		  (sh_word32)(res[0]), 
-		  (sh_word32)(res[3]), 
-		  (sh_word32)(res[2]), 
-		  (sh_word32)(res[5]), 
-		  (sh_word32)(res[4]) );
-#endif
-      out[len-1] = '\0';
-      SL_RETURN( out, _("sh_tiger_hash_internal"));
-
-    }
-
-   SL_RETURN( SH_KEY_NULL, _("sh_tiger_hash_internal"));
-}
-
-char * sh_tiger_hash_gpg (const char * filename, TigerType what, 
-			  UINT64 Length)
+      sprintf(out,                                   /* known to fit  */
+	      MYFORMAT,
+	      (sh_word32)(res[1]), 
+	      (sh_word32)(res[0]), 
+	      (sh_word32)(res[3]), 
+	      (sh_word32)(res[2]), 
+	      (sh_word32)(res[5]), 
+	      (sh_word32)(res[4]) );
+#endif
+      /*@+bufferoverflowhigh@*/
+      out[KEY_LEN] = '\0';
+      SL_RETURN( out, _("sh_tiger_hash"));
+
+    }
+
+   SL_RETURN( _("000000000000000000000000000000000000000000000000"), 
+	      _("sh_tiger_hash"));
+}
+
+char * sh_tiger_hash_gpg (char * filename, TigerType what, 
+			  unsigned long Length)
 {
   size_t  len;
   char * out;
   char   outhash[48+6+1];
-#if defined(TIGER_64_BIT)
-  word64 res[3];
+#if defined(HAVE_LONG_64) || defined(HAVE_LONG_LONG_64)
+  /* #ifdef HAVE_LONG_64 */
+  word64 * res;
 #else
-  sh_word32 res[6];
-#endif
-  UINT64 local_length = Length;
+  sh_word32 * res;
+#endif
 
   SL_ENTER(_("sh_tiger_hash_gpg"));
 
-  if (NULL != sh_tiger_hash_val (filename, what, &local_length, 0, res))
-    {
-#if defined(TIGER_64_BIT)
-      sl_snprintf(outhash,
-		  sizeof(outhash),
-		  GPGFORMAT,
-		  (sh_word32)(res[0]>>32), 
-		  (sh_word32)(res[0]), 
-		  (sh_word32)(res[1]>>32), 
-		  (sh_word32)(res[1]), 
-		  (sh_word32)(res[2]>>32), 
-		  (sh_word32)(res[2]) );
+  res = sh_tiger_hash_val (filename, what, Length, 0);
+  if (res != NULL)
+    {
+      /*@-bufferoverflowhigh -formatconst@*/
+#if defined(HAVE_LONG_64) || defined(HAVE_LONG_LONG_64)
+      /* #ifdef HAVE_LONG_64 */
+      sprintf(outhash,                               /* known to fit  */
+	      GPGFORMAT,
+	      (sh_word32)(res[0]>>32), 
+	      (sh_word32)(res[0]), 
+	      (sh_word32)(res[1]>>32), 
+	      (sh_word32)(res[1]), 
+	      (sh_word32)(res[2]>>32), 
+	      (sh_word32)(res[2]) );
 #else
-      sl_snprintf(outhash,
-		  sizeof(outhash),
-		  GPGFORMAT,
-		  (sh_word32)(res[1]), 
-		  (sh_word32)(res[0]), 
-		  (sh_word32)(res[3]), 
-		  (sh_word32)(res[2]), 
-		  (sh_word32)(res[5]), 
-		  (sh_word32)(res[4]) );
-#endif
-      outhash[sizeof(outhash)-1] = '\0';
+      sprintf(outhash,                               /* known to fit  */
+	      GPGFORMAT,
+	      (sh_word32)(res[1]), 
+	      (sh_word32)(res[0]), 
+	      (sh_word32)(res[3]), 
+	      (sh_word32)(res[2]), 
+	      (sh_word32)(res[5]), 
+	      (sh_word32)(res[4]) );
+#endif
+      /*@+bufferoverflowhigh@*/
+      outhash[48 + 6] = '\0';
     }
   else
     {
-      sl_strlcpy(outhash,
-		 _("00000000 00000000 00000000  00000000 00000000 00000000"),
-		 sizeof(outhash));
-    }
-
-  if (what == TIGER_FILE && sl_ok_adds(sl_strlen (filename), (2 + 48 + 6)))
+      /*@-bufferoverflowhigh@*/
+      sprintf(outhash,                               /* known to fit  */
+	      _("00000000 00000000 00000000  00000000 00000000 00000000"));
+      /*@+bufferoverflowhigh@*/
+    }
+
+  if (what == TIGER_FILE)
     len = sl_strlen (filename) + 2 + 48 + 6;
   else
@@ -1837,23 +1480,25 @@
 UINT32 * sh_tiger_hash_uint32 (char * filename, 
 			       TigerType what, 
-			       UINT64 Length, UINT32 * out, size_t len)
-{
-#if defined(TIGER_64_BIT)
-  word64 res[3];
+			       unsigned long Length)
+{
+#if defined(HAVE_LONG_64) || defined(HAVE_LONG_LONG_64)
+  /* #ifdef HAVE_LONG_64 */
+  word64 * res;
 #else
-  sh_word32 res[6];
-#endif
-  UINT64 local_length = Length;
+  sh_word32 * res;
+#endif
+
+  static UINT32 out[6];
 
   SL_ENTER(_("sh_tiger_hash_uint32"));
 
-  SH_VALIDATE_GE(len, 6);
-
-  out[0] = 0; out[1] = 0; out[2] = 0;
-  out[3] = 0; out[4] = 0; out[5] = 0;
-
-  if (NULL != sh_tiger_hash_val (filename,  what,  &local_length, 0, res))
-    {
-#if defined(TIGER_64_BIT)
+  memset(out, 0, 6 * sizeof(UINT32));
+
+  res = sh_tiger_hash_val (filename,  what,  Length, 0);
+
+  if (res != NULL)
+    {
+#if defined(HAVE_LONG_64) || defined(HAVE_LONG_LONG_64)
+      /* #ifdef HAVE_LONG_64 */
 	out[0] =  (UINT32)(res[0]>>32); 
 	out[1] =  (UINT32)(res[0]);
Index: trunk/src/sh_tiger1.c
===================================================================
--- trunk/src/sh_tiger1.c	(revision 591)
+++ trunk/src/sh_tiger1.c	(revision 1)
@@ -5,9 +5,6 @@
 #include "config_xor.h"
 
-/* we already inline in the function used for file checksums */
-/* #define UNROLL_COMPRESS */
-#undef UNROLL_COMPRESS
-
-#if !defined(TIGER_64_BIT)
+
+#if !defined(HAVE_LONG_64) && !defined(HAVE_LONG_LONG_64)
 
 /* Tiger: A Fast New Hash Function
@@ -40,5 +37,5 @@
 typedef unsigned short sh_word32;
 #else
-#error No 32 bit type found !
+#error No 32 byte type found !
 #endif
 
@@ -49,4 +46,9 @@
 #define BIG_ENDIAN
 #endif
+
+/* NOTE that this code is NOT FULLY OPTIMIZED for any  */
+/* machine. Assembly code might be much faster on some */
+/* machines, especially if the code is compiled with   */
+/* gcc.                                                */
 
 /* The number of passes of the hash function.          */
@@ -63,5 +65,5 @@
 #define t4 (tiger_table+256*3)
 
-#define sub64(s0, s1, p0, p1) \
+#define sh_sub64(s0, s1, p0, p1) \
       temps0 = (p0); \
       tcarry = s0 < temps0; \
@@ -69,5 +71,5 @@
       s1 -= (p1) + tcarry;
 
-#define add64(s0, s1, p0, p1) \
+#define sh_add64(s0, s1, p0, p1) \
       temps0 = (p0); \
       s0 += temps0; \
@@ -75,26 +77,26 @@
       s1 += (p1) + tcarry;
 
-#define xor64(s0, s1, p0, p1) \
+#define sh_xor64(s0, s1, p0, p1) \
       s0 ^= (p0); \
       s1 ^= (p1);
 
-#define mul5(s0, s1) \
+#define sh_mul5(s0, s1) \
       tempt0 = s0<<2; \
       tempt1 = (s1<<2)|(s0>>30); \
-      add64(s0, s1, tempt0, tempt1);
-
-#define mul7(s0, s1) \
+      sh_add64(s0, s1, tempt0, tempt1);
+
+#define sh_mul7(s0, s1) \
       tempt0 = s0<<3; \
       tempt1 = (s1<<3)|(s0>>29); \
-      sub64(tempt0, tempt1, s0, s1); \
+      sh_sub64(tempt0, tempt1, s0, s1); \
       s0 = tempt0; \
       s1 = tempt1;
 
-#define mul9(s0, s1) \
+#define sh_mul9(s0, s1) \
       tempt0 = s0<<3; \
       tempt1 = (s1<<3)|(s0>>29); \
-      add64(s0, s1, tempt0, tempt1);
-
-#define save_abc \
+      sh_add64(s0, s1, tempt0, tempt1);
+
+#define sh_save_abc \
       aa0 = a0; \
       aa1 = a1; \
@@ -104,6 +106,6 @@
       cc1 = c1;
 
-#define roundX(a0,a1,b0,b1,c0,c1,x0,x1) \
-      xor64(c0, c1, x0, x1); \
+#define sh_round(a0,a1,b0,b1,c0,c1,x0,x1,mul) \
+      sh_xor64(c0, c1, x0, x1); \
       temp0  = t1[((c0)>>(0*8))&0xFF][0] ; \
       temp1  = t1[((c0)>>(0*8))&0xFF][1] ; \
@@ -114,5 +116,5 @@
       temp0 ^= t4[((c1)>>(2*8))&0xFF][0] ; \
       temp1 ^= t4[((c1)>>(2*8))&0xFF][1] ; \
-      sub64(a0, a1, temp0, temp1); \
+      sh_sub64(a0, a1, temp0, temp1); \
       temp0  = t4[((c0)>>(1*8))&0xFF][0] ; \
       temp1  = t4[((c0)>>(1*8))&0xFF][1] ; \
@@ -123,108 +125,79 @@
       temp0 ^= t1[((c1)>>(3*8))&0xFF][0] ; \
       temp1 ^= t1[((c1)>>(3*8))&0xFF][1] ; \
-      add64(b0, b1, temp0, temp1); 
-
-
-#define round5(a0,a1,b0,b1,c0,c1,x0,x1) \
-      roundX(a0,a1,b0,b1,c0,c1,x0,x1); \
-      mul5(b0, b1);
-
-#define round7(a0,a1,b0,b1,c0,c1,x0,x1) \
-      roundX(a0,a1,b0,b1,c0,c1,x0,x1); \
-      mul7(b0, b1);
-
-#define round9(a0,a1,b0,b1,c0,c1,x0,x1) \
-      roundX(a0,a1,b0,b1,c0,c1,x0,x1); \
-      mul9(b0, b1);
-
-
-/* mixed with key_schedule
- */
-#define pass5(a0,a1,b0,b1,c0,c1) \
-      round5(a0,a1,b0,b1,c0,c1,x00,x01); \
-      sub64(x00, x01, x70^0xA5A5A5A5, x71^0xA5A5A5A5); \
-      round5(b0,b1,c0,c1,a0,a1,x10,x11); \
-      xor64(x10, x11, x00, x01); \
-      round5(c0,c1,a0,a1,b0,b1,x20,x21); \
-      add64(x20, x21, x10, x11); \
-      round5(a0,a1,b0,b1,c0,c1,x30,x31); \
-      sub64(x30, x31, x20^((~x10)<<19), ~x21^(((x11)<<19)|((x10)>>13))); \
-      round5(b0,b1,c0,c1,a0,a1,x40,x41); \
-      xor64(x40, x41, x30, x31); \
-      round5(c0,c1,a0,a1,b0,b1,x50,x51); \
-      add64(x50, x51, x40, x41); \
-      round5(a0,a1,b0,b1,c0,c1,x60,x61); \
-      sub64(x60, x61, ~x50^(((x40)>>23)|((x41)<<9)), x51^((~x41)>>23)); \
-      round5(b0,b1,c0,c1,a0,a1,x70,x71);
-
-/* mixed with key_schedule
- */
-#define pass7(a0,a1,b0,b1,c0,c1) \
-      round7(a0,a1,b0,b1,c0,c1,x00,x01); \
-      sub64(x00, x01, x70^0xA5A5A5A5, x71^0xA5A5A5A5); \
-      round7(b0,b1,c0,c1,a0,a1,x10,x11); \
-      xor64(x10, x11, x00, x01); \
-      round7(c0,c1,a0,a1,b0,b1,x20,x21); \
-      add64(x20, x21, x10, x11); \
-      round7(a0,a1,b0,b1,c0,c1,x30,x31); \
-      sub64(x30, x31, x20^((~x10)<<19), ~x21^(((x11)<<19)|((x10)>>13))); \
-      round7(b0,b1,c0,c1,a0,a1,x40,x41); \
-      xor64(x40, x41, x30, x31); \
-      round7(c0,c1,a0,a1,b0,b1,x50,x51); \
-      add64(x50, x51, x40, x41); \
-      round7(a0,a1,b0,b1,c0,c1,x60,x61); \
-      sub64(x60, x61, ~x50^(((x40)>>23)|((x41)<<9)), x51^((~x41)>>23)); \
-      round7(b0,b1,c0,c1,a0,a1,x70,x71);
-
-/* mixed with key_schedule
- */
-#define pass9(a0,a1,b0,b1,c0,c1) \
-      round9(a0,a1,b0,b1,c0,c1,x00,x01); \
-      sub64(x00, x01, x70^0xA5A5A5A5, x71^0xA5A5A5A5); \
-      round9(b0,b1,c0,c1,a0,a1,x10,x11); \
-      xor64(x10, x11, x00, x01); \
-      round9(c0,c1,a0,a1,b0,b1,x20,x21); \
-      add64(x20, x21, x10, x11); \
-      round9(a0,a1,b0,b1,c0,c1,x30,x31); \
-      sub64(x30, x31, x20^((~x10)<<19), ~x21^(((x11)<<19)|((x10)>>13))); \
-      round9(b0,b1,c0,c1,a0,a1,x40,x41); \
-      xor64(x40, x41, x30, x31); \
-      round9(c0,c1,a0,a1,b0,b1,x50,x51); \
-      add64(x50, x51, x40, x41); \
-      round9(a0,a1,b0,b1,c0,c1,x60,x61); \
-      sub64(x60, x61, ~x50^(((x40)>>23)|((x41)<<9)), x51^((~x41)>>23)); \
-      round9(b0,b1,c0,c1,a0,a1,x70,x71);
-
-#define key_schedule \
-      xor64(x70, x71, x60, x61); \
-      add64(x00, x01, x70, x71); \
-      sub64(x10, x11, x00^((~x70)<<19), ~x01^(((x71)<<19)|((x70)>>13))); \
-      xor64(x20, x21, x10, x11); \
-      add64(x30, x31, x20, x21); \
-      sub64(x40, x41, ~x30^(((x20)>>23)|((x21)<<9)), x31^((~x21)>>23)); \
-      xor64(x50, x51, x40, x41); \
-      add64(x60, x61, x50, x51); \
-      sub64(x70, x71, x60^0x89ABCDEF, x61^0x01234567);
-
-#define feedforward \
-      xor64(a0, a1, aa0, aa1); \
-      sub64(b0, b1, bb0, bb1); \
-      add64(c0, c1, cc0, cc1);
-
-#define compress \
-      pass5(a0,a1,b0,b1,c0,c1); \
-      key_schedule; \
-      pass7(c0,c1,a0,a1,b0,b1); \
-      key_schedule; \
-      pass9(b0,b1,c0,c1,a0,a1); \
-      feedforward
+      sh_add64(b0, b1, temp0, temp1); \
+      if((mul)==5) \
+	{sh_mul5(b0, b1);} \
+      else \
+	if((mul)==7) \
+	  {sh_mul7(b0, b1);} \
+	else \
+	  {sh_mul9(b0, b1)};
+
+#define sh_pass(a0,a1,b0,b1,c0,c1,mul) \
+      sh_round(a0,a1,b0,b1,c0,c1,x00,x01,mul); \
+      sh_round(b0,b1,c0,c1,a0,a1,x10,x11,mul); \
+      sh_round(c0,c1,a0,a1,b0,b1,x20,x21,mul); \
+      sh_round(a0,a1,b0,b1,c0,c1,x30,x31,mul); \
+      sh_round(b0,b1,c0,c1,a0,a1,x40,x41,mul); \
+      sh_round(c0,c1,a0,a1,b0,b1,x50,x51,mul); \
+      sh_round(a0,a1,b0,b1,c0,c1,x60,x61,mul); \
+      sh_round(b0,b1,c0,c1,a0,a1,x70,x71,mul);
+
+#define sh_key_schedule \
+      sh_sub64(x00, x01, x70^0xA5A5A5A5, x71^0xA5A5A5A5); \
+      sh_xor64(x10, x11, x00, x01); \
+      sh_add64(x20, x21, x10, x11); \
+      sh_sub64(x30, x31, x20^((~x10)<<19), ~x21^(((x11)<<19)|((x10)>>13))); \
+      sh_xor64(x40, x41, x30, x31); \
+      sh_add64(x50, x51, x40, x41); \
+      sh_sub64(x60, x61, ~x50^(((x40)>>23)|((x41)<<9)), x51^((~x41)>>23)); \
+      sh_xor64(x70, x71, x60, x61); \
+      sh_add64(x00, x01, x70, x71); \
+      sh_sub64(x10, x11, x00^((~x70)<<19), ~x01^(((x71)<<19)|((x70)>>13))); \
+      sh_xor64(x20, x21, x10, x11); \
+      sh_add64(x30, x31, x20, x21); \
+      sh_sub64(x40, x41, ~x30^(((x20)>>23)|((x21)<<9)), x31^((~x21)>>23)); \
+      sh_xor64(x50, x51, x40, x41); \
+      sh_add64(x60, x61, x50, x51); \
+      sh_sub64(x70, x71, x60^0x89ABCDEF, x61^0x01234567);
+
+#define sh_feedforward \
+      sh_xor64(a0, a1, aa0, aa1); \
+      sh_sub64(b0, b1, bb0, bb1); \
+      sh_add64(c0, c1, cc0, cc1);
+
+#ifdef UNROLL_COMPRESS
+#define sh_compress \
+      sh_save_abc \
+      sh_pass(a0,a1,b0,b1,c0,c1,5); \
+      sh_key_schedule; \
+      sh_pass(c0,c1,a0,a1,b0,b1,7); \
+      sh_key_schedule; \
+      sh_pass(b0,b1,c0,c1,a0,a1,9); \
+      for(pass_no=3; pass_no<PASSES; pass_no++) { \
+        sh_key_schedule \
+	sh_pass(a0,a1,b0,b1,c0,c1,9); \
+	tmpa=a0; a0=c0; c0=b0; b0=tmpa; \
+	tmpa=a1; a1=c1; c1=b1; b1=tmpa;} \
+      sh_feedforward
+#else
+#define sh_compress \
+      sh_save_abc \
+      for(pass_no=0; pass_no<PASSES; pass_no++) { \
+        if(pass_no != 0) {sh_key_schedule} \
+	sh_pass(a0,a1,b0,b1,c0,c1,(pass_no==0?5:pass_no==1?7:9)) \
+	tmpa=a0; a0=c0; c0=b0; b0=tmpa; \
+	tmpa=a1; a1=c1; c1=b1; b1=tmpa;} \
+      sh_feedforward
+#endif
 
 #define tiger_compress_macro(str, state) \
 { \
-  register sh_word32 a0, a1, b0, b1, c0, c1; \
+  register sh_word32 a0, a1, b0, b1, c0, c1, tmpa; \
   sh_word32 aa0, aa1, bb0, bb1, cc0, cc1; \
   sh_word32 x00, x01, x10, x11, x20, x21, x30, x31, \
-                  x40, x41, x50, x51, x60, x61, x70, x71; \
-  sh_word32 temp0, temp1, tempt0, tempt1, temps0, tcarry; \
+         x40, x41, x50, x51, x60, x61, x70, x71; \
+  register sh_word32 temp0, temp1, tempt0, tempt1, temps0, tcarry; \
+  int pass_no; \
 \
   a0 = state[0]; \
@@ -235,6 +208,4 @@
   c1 = state[5]; \
 \
-      save_abc \
-\
   x00=str[0*2]; x01=str[0*2+1]; x10=str[1*2]; x11=str[1*2+1]; \
   x20=str[2*2]; x21=str[2*2+1]; x30=str[3*2]; x31=str[3*2+1]; \
@@ -242,5 +213,5 @@
   x60=str[6*2]; x61=str[6*2+1]; x70=str[7*2]; x71=str[7*2+1]; \
 \
-  compress; \
+  sh_compress; \
 \
   state[0] = a0; \
@@ -252,11 +223,10 @@
 }
 
-#if defined(UNROLL_COMPRESS)
+#ifdef UNROLL_COMPRESS
 /* The compress function is inlined */
 #define tiger_compress(str, state) \
   tiger_compress_macro(((sh_word32*)str), ((sh_word32*)state))
-
-#else
-
+#else
+/* The compress function is a function */
 void
 tiger_compress(sh_word32 *str, sh_word32 state[6])
@@ -267,5 +237,5 @@
 
 void
-tiger_t(const sh_word32 *str, sh_word32 length, sh_word32 res[6])
+tiger_t(sh_word32 *str, sh_word32 length, sh_word32 res[6])
 {
   register sh_word32 i;
@@ -279,8 +249,8 @@
 #ifdef BIG_ENDIAN
       for(j=0; j<64; j++)
-        temp[j^3] = ((sh_byte*)str)[j];
-      tiger_compress_macro(((sh_word32*)temp), res);
-#else
-      tiger_compress_macro(str, res);
+	temp[j^3] = ((sh_byte*)str)[j];
+      tiger_compress(((sh_word32*)temp), res);
+#else
+      tiger_compress(str, res);
 #endif
       str += 16;
@@ -288,18 +258,9 @@
 }
 
-
-void tiger(sh_word32 *str, sh_word32 length, sh_word32 res[6])
+void
+tiger(sh_word32 *str, sh_word32 length, sh_word32 res[6])
 {
   register sh_word32 i, j;
   sh_byte temp[64];
-
-  /*
-   * res[0]=0x89ABCDEF;
-   * res[1]=0x01234567;
-   * res[2]=0x76543210;
-   * res[3]=0xFEDCBA98;
-   * res[4]=0xC3B2E187;
-   * res[5]=0xF096A5B4;
-   */
 
   for(i=length; i>=64; i-=64)
@@ -346,4 +307,18 @@
 }
 
-#endif
-
+#else
+void dummy_1 (int a)
+{
+  (void) a;
+  return;
+}
+#endif
+
+
+
+
+
+
+
+
+
Index: trunk/src/sh_tiger1.s
===================================================================
--- trunk/src/sh_tiger1.s	(revision 591)
+++ 	(revision )
@@ -1,5756 +1,0 @@
-	.file	"sh_tiger1.c"
-	.text
-.globl tiger_compress
-	.type	tiger_compress, @function
-tiger_compress:
-	pushl	%ebp
-	movl	%esp, %ebp
-	pushl	%edi
-	pushl	%esi
-	pushl	%ebx
-	subl	$1140, %esp
-	movl	12(%ebp), %ecx
-	movl	12(%ebp), %esi
-	call	__i686.get_pc_thunk.bx
-	addl	$_GLOBAL_OFFSET_TABLE_, %ebx
-	movl	8(%ebp), %eax
-	movl	(%ecx), %edi
-	movl	12(%ebp), %ecx
-	movl	%edi, -1088(%ebp)
-	movl	4(%esi), %edx
-	movl	12(%ebp), %esi
-	movl	%edx, -1092(%ebp)
-	movl	8(%ecx), %edi
-	movl	12(%ebp), %ecx
-	movl	%edi, -1096(%ebp)
-	movl	12(%esi), %edx
-	movl	-1096(%ebp), %esi
-	movl	%edx, -1100(%ebp)
-	movl	16(%ecx), %edx
-	movl	20(%ecx), %edi
-	movl	%esi, -16(%ebp)
-	movl	%edx, -20(%ebp)
-	movl	%edi, -1104(%ebp)
-	movl	(%eax), %ecx
-	movl	%ecx, -24(%ebp)
-	movl	4(%eax), %edi
-	movl	%edi, -28(%ebp)
-	movl	8(%eax), %esi
-	movl	%esi, -32(%ebp)
-	movl	12(%eax), %ecx
-	movl	%ecx, -36(%ebp)
-	movl	16(%eax), %edi
-	movl	%edi, -40(%ebp)
-	movl	20(%eax), %esi
-	movl	%esi, -44(%ebp)
-	movl	24(%eax), %ecx
-	movl	%ecx, -48(%ebp)
-	movl	28(%eax), %edi
-	movl	%edi, -52(%ebp)
-	movl	32(%eax), %esi
-	movl	%esi, -56(%ebp)
-	movl	36(%eax), %ecx
-	movl	%ecx, -60(%ebp)
-	movl	40(%eax), %edi
-	movl	%edi, -64(%ebp)
-	movl	44(%eax), %esi
-	movl	%esi, -68(%ebp)
-	movl	48(%eax), %ecx
-	movl	%ecx, -72(%ebp)
-	movl	52(%eax), %edi
-	movl	%edi, -76(%ebp)
-	movl	56(%eax), %esi
-	movl	-28(%ebp), %edi
-	movl	%esi, -80(%ebp)
-	movl	60(%eax), %ecx
-	movl	-24(%ebp), %eax
-	movl	-1104(%ebp), %esi
-	movl	%ecx, -84(%ebp)
-	xorl	%eax, %edx
-	movl	tiger_table@GOT(%ebx), %ecx
-	movl	%edx, -152(%ebp)
-	movl	%edx, %eax
-	xorl	%edi, %esi
-	movl	%esi, -156(%ebp)
-	shrl	$13, %eax
-	movzbl	%dl,%edi
-	andl	$2040, %eax
-	movl	%ecx, %esi
-	movl	2048(%eax,%ecx), %edx
-	movl	(%ecx,%edi,8), %ecx
-	movl	2052(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	4(%esi,%edi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-156(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	4100(%eax,%esi), %edi
-	movl	-156(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	6148(%eax,%esi), %edi
-	xorl	%eax, %eax
-	xorl	%edi, %edx
-	movl	-1088(%ebp), %edi
-	cmpl	%ecx, -1088(%ebp)
-	setb	%al
-	subl	%ecx, %edi
-	movl	%edi, -168(%ebp)
-	movl	-1092(%ebp), %ecx
-	addl	%eax, %edx
-	subl	%edx, %ecx
-	movl	-152(%ebp), %edx
-	movl	%ecx, -172(%ebp)
-	movzbl	%dh, %eax
-	leal	0(,%eax,8), %ecx
-	movl	%edx, %eax
-	movl	6144(%ecx,%esi), %edi
-	shrl	$24, %eax
-	sall	$3, %eax
-	movl	6148(%ecx,%esi), %ecx
-	movl	4096(%eax,%esi), %edx
-	xorl	%edi, %edx
-	movl	4100(%eax,%esi), %edi
-	movl	-156(%ebp), %eax
-	xorl	%edi, %ecx
-	movzbl	%ah, %edi
-	leal	0(,%edi,8), %eax
-	movl	2048(%eax,%esi), %edi
-	xorl	%edi, %edx
-	movl	2052(%eax,%esi), %edi
-	movl	-156(%ebp), %eax
-	xorl	%edi, %ecx
-	shrl	$24, %eax
-	movl	(%esi,%eax,8), %edi
-	xorl	%edi, %edx
-	movl	4(%esi,%eax,8), %edi
-	movl	-1096(%ebp), %esi
-	xorl	%eax, %eax
-	xorl	%edi, %ecx
-	addl	%edx, %esi
-	cmpl	%edx, %esi
-	movl	-1100(%ebp), %edx
-	setb	%al
-	movl	%esi, %edi
-	leal	(%ecx,%eax), %eax
-	addl	%edx, %eax
-	leal	0(,%esi,4), %ecx
-	movl	%eax, %edx
-	movl	%eax, -160(%ebp)
-	addl	%ecx, %esi
-	shrl	$30, %edi
-	movl	%esi, -164(%ebp)
-	sall	$2, %edx
-	xorl	%eax, %eax
-	orl	%edi, %edx
-	cmpl	%ecx, %esi
-	movl	-80(%ebp), %edi
-	setb	%al
-	xorl	%ecx, %ecx
-	addl	%eax, %edx
-	movl	-160(%ebp), %eax
-	xorl	$-1515870811, %edi
-	movl	-24(%ebp), %esi
-	addl	%eax, %edx
-	movl	-84(%ebp), %eax
-	cmpl	%edi, -24(%ebp)
-	movl	%edx, -184(%ebp)
-	movl	-32(%ebp), %edx
-	setb	%cl
-	xorl	$-1515870811, %eax
-	subl	%edi, %esi
-	addl	%ecx, %eax
-	movl	%esi, -196(%ebp)
-	movl	-28(%ebp), %ecx
-	movl	-36(%ebp), %edi
-	movl	-172(%ebp), %esi
-	subl	%eax, %ecx
-	movl	-168(%ebp), %eax
-	movl	%ecx, -200(%ebp)
-	movl	tiger_table@GOT(%ebx), %ecx
-	xorl	%edi, %esi
-	movl	%esi, -180(%ebp)
-	xorl	%edx, %eax
-	movzbl	%al,%edi
-	movl	%eax, -176(%ebp)
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	%ecx, %esi
-	movl	2048(%eax,%ecx), %edx
-	movl	(%ecx,%edi,8), %ecx
-	movl	2052(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	4(%esi,%edi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-180(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	4100(%eax,%esi), %edi
-	movl	-180(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	6148(%eax,%esi), %edi
-	xorl	%eax, %eax
-	xorl	%edi, %edx
-	movl	-164(%ebp), %edi
-	cmpl	%ecx, -164(%ebp)
-	setb	%al
-	subl	%ecx, %edi
-	movl	%edi, -204(%ebp)
-	movl	-176(%ebp), %ecx
-	movl	-184(%ebp), %edi
-	addl	%eax, %edx
-	movzbl	%ch, %eax
-	subl	%edx, %edi
-	movl	%edi, -208(%ebp)
-	leal	0(,%eax,8), %edi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	movl	6144(%edi,%esi), %ecx
-	sall	$3, %eax
-	movl	4096(%eax,%esi), %edx
-	movl	4100(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%edi,%esi), %edx
-	xorl	%eax, %edx
-	movl	-180(%ebp), %eax
-	movzbl	%ah, %edi
-	leal	0(,%edi,8), %eax
-	movl	2048(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	2052(%eax,%esi), %edi
-	movl	-180(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$24, %eax
-	movl	(%esi,%eax,8), %edi
-	xorl	%edi, %ecx
-	movl	4(%esi,%eax,8), %edi
-	xorl	%eax, %eax
-	xorl	%edi, %edx
-	movl	-152(%ebp), %edi
-	addl	%ecx, %edi
-	cmpl	%ecx, %edi
-	movl	-156(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%edi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -188(%ebp)
-	leal	0(,%edi,4), %ecx
-	addl	%ecx, %edi
-	movl	%edi, -192(%ebp)
-	shrl	$30, %eax
-	sall	$2, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %edi
-	movl	-188(%ebp), %edi
-	setb	%al
-	addl	%eax, %edx
-	movl	-32(%ebp), %ecx
-	addl	%edi, %edx
-	movl	-200(%ebp), %eax
-	movl	%edx, -220(%ebp)
-	movl	-36(%ebp), %edi
-	movl	-196(%ebp), %edx
-	xorl	%eax, %edi
-	xorl	%edx, %ecx
-	movl	%edi, -240(%ebp)
-	movl	-40(%ebp), %edx
-	movl	%ecx, -232(%ebp)
-	movl	-204(%ebp), %eax
-	movl	-44(%ebp), %ecx
-	movl	-208(%ebp), %edi
-	xorl	%edx, %eax
-	movl	%eax, -212(%ebp)
-	xorl	%ecx, %edi
-	movl	%edi, -216(%ebp)
-	movzbl	%al,%edi
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	(%esi,%edi,8), %ecx
-	movl	2048(%eax,%esi), %edx
-	movl	2052(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	4(%esi,%edi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-216(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	4100(%eax,%esi), %edi
-	movl	-216(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	6148(%eax,%esi), %edi
-	xorl	%eax, %eax
-	movl	-192(%ebp), %esi
-	xorl	%edi, %edx
-	cmpl	%ecx, -192(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -244(%ebp)
-	movl	-220(%ebp), %edi
-	addl	%eax, %edx
-	movl	tiger_table@GOT(%ebx), %ecx
-	subl	%edx, %edi
-	movl	-212(%ebp), %edx
-	movl	%edi, -248(%ebp)
-	movl	%edx, %eax
-	movzbl	%dh, %esi
-	shrl	$24, %eax
-	leal	0(,%esi,8), %edi
-	movl	%ecx, %esi
-	sall	$3, %eax
-	movl	4096(%eax,%ecx), %edx
-	movl	6144(%edi,%ecx), %ecx
-	movl	4100(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%edi,%esi), %edx
-	xorl	%eax, %edx
-	movl	-216(%ebp), %eax
-	movzbl	%ah, %edi
-	leal	0(,%edi,8), %eax
-	movl	2048(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	2052(%eax,%esi), %edi
-	movl	-216(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$24, %eax
-	movl	(%esi,%eax,8), %edi
-	xorl	%edi, %ecx
-	movl	4(%esi,%eax,8), %edi
-	xorl	%eax, %eax
-	xorl	%edi, %edx
-	movl	-176(%ebp), %edi
-	addl	%ecx, %edi
-	cmpl	%ecx, %edi
-	movl	-180(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%edi, %eax
-	addl	%ecx, %edx
-	leal	0(,%edi,4), %ecx
-	movl	%edx, -224(%ebp)
-	addl	%ecx, %edi
-	shrl	$30, %eax
-	movl	%edi, -228(%ebp)
-	sall	$2, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %edi
-	movl	-224(%ebp), %ecx
-	setb	%al
-	movl	-232(%ebp), %edi
-	addl	%eax, %edx
-	addl	%ecx, %edx
-	movl	-232(%ebp), %eax
-	movl	-240(%ebp), %ecx
-	movl	%edx, -260(%ebp)
-	movl	-40(%ebp), %edx
-	addl	%eax, %edx
-	movl	%edx, -236(%ebp)
-	xorl	%eax, %eax
-	cmpl	%edi, %edx
-	movl	-44(%ebp), %edi
-	setb	%al
-	addl	%ecx, %eax
-	movl	-48(%ebp), %edx
-	addl	%edi, %eax
-	movl	-52(%ebp), %ecx
-	movl	%eax, -272(%ebp)
-	movl	-248(%ebp), %edi
-	movl	-244(%ebp), %eax
-	xorl	%ecx, %edi
-	xorl	%edx, %eax
-	movl	%edi, -256(%ebp)
-	movzbl	%al,%edi
-	movl	%eax, -252(%ebp)
-	shrl	$13, %eax
-	movl	(%esi,%edi,8), %ecx
-	andl	$2040, %eax
-	movl	2048(%eax,%esi), %edx
-	movl	2052(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	4(%esi,%edi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-256(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	4100(%eax,%esi), %edi
-	movl	-256(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	6148(%eax,%esi), %edi
-	xorl	%eax, %eax
-	movl	-228(%ebp), %esi
-	xorl	%edi, %edx
-	cmpl	%ecx, -228(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -276(%ebp)
-	movl	-260(%ebp), %edi
-	addl	%eax, %edx
-	movl	tiger_table@GOT(%ebx), %ecx
-	subl	%edx, %edi
-	movl	-252(%ebp), %edx
-	movl	%edi, -280(%ebp)
-	movl	%edx, %eax
-	movzbl	%dh, %esi
-	shrl	$24, %eax
-	leal	0(,%esi,8), %edi
-	movl	%ecx, %esi
-	sall	$3, %eax
-	movl	4096(%eax,%ecx), %edx
-	movl	6144(%edi,%ecx), %ecx
-	movl	4100(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%edi,%esi), %edx
-	xorl	%eax, %edx
-	movl	-256(%ebp), %eax
-	movzbl	%ah, %edi
-	leal	0(,%edi,8), %eax
-	movl	2048(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	2052(%eax,%esi), %edi
-	movl	-256(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$24, %eax
-	movl	(%esi,%eax,8), %edi
-	xorl	%edi, %ecx
-	movl	4(%esi,%eax,8), %edi
-	xorl	%eax, %eax
-	xorl	%edi, %edx
-	movl	-212(%ebp), %edi
-	addl	%ecx, %edi
-	cmpl	%ecx, %edi
-	movl	-216(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%edi, %eax
-	addl	%ecx, %edx
-	leal	0(,%edi,4), %ecx
-	movl	%edx, -264(%ebp)
-	addl	%ecx, %edi
-	shrl	$30, %eax
-	movl	%edi, -268(%ebp)
-	sall	$2, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %edi
-	setb	%al
-	addl	%eax, %edx
-	movl	-264(%ebp), %eax
-	movl	-236(%ebp), %edi
-	addl	%eax, %edx
-	movl	-232(%ebp), %eax
-	movl	%edx, -292(%ebp)
-	movl	-48(%ebp), %edx
-	notl	%eax
-	sall	$19, %eax
-	xorl	%edi, %eax
-	cmpl	%eax, -48(%ebp)
-	setb	%cl
-	movzbl	%cl, %edi
-	subl	%eax, %edx
-	movl	-232(%ebp), %ecx
-	movl	-240(%ebp), %eax
-	movl	%edx, -304(%ebp)
-	movl	-272(%ebp), %edx
-	shrl	$13, %ecx
-	sall	$19, %eax
-	orl	%ecx, %eax
-	notl	%edx
-	xorl	%eax, %edx
-	movl	-52(%ebp), %eax
-	addl	%edi, %edx
-	movl	-60(%ebp), %ecx
-	subl	%edx, %eax
-	movl	-280(%ebp), %edi
-	movl	%eax, -308(%ebp)
-	movl	-56(%ebp), %edx
-	movl	-276(%ebp), %eax
-	xorl	%ecx, %edi
-	xorl	%edx, %eax
-	movl	%eax, -284(%ebp)
-	movl	%edi, -288(%ebp)
-	movzbl	%al,%edi
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	(%esi,%edi,8), %ecx
-	movl	2048(%eax,%esi), %edx
-	movl	2052(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	4(%esi,%edi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-288(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	4100(%eax,%esi), %edi
-	movl	-288(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	6148(%eax,%esi), %edi
-	xorl	%eax, %eax
-	movl	-268(%ebp), %esi
-	xorl	%edi, %edx
-	cmpl	%ecx, -268(%ebp)
-	movl	-292(%ebp), %edi
-	setb	%al
-	addl	%eax, %edx
-	subl	%ecx, %esi
-	subl	%edx, %edi
-	movl	%esi, -312(%ebp)
-	movl	-284(%ebp), %edx
-	movl	tiger_table@GOT(%ebx), %ecx
-	movl	%edi, -316(%ebp)
-	movl	%edx, %eax
-	movzbl	%dh, %esi
-	shrl	$24, %eax
-	leal	0(,%esi,8), %edi
-	movl	%ecx, %esi
-	sall	$3, %eax
-	movl	4096(%eax,%ecx), %edx
-	movl	6144(%edi,%ecx), %ecx
-	movl	4100(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%edi,%esi), %edx
-	xorl	%eax, %edx
-	movl	-288(%ebp), %eax
-	movzbl	%ah, %edi
-	leal	0(,%edi,8), %eax
-	movl	2048(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	2052(%eax,%esi), %edi
-	movl	-288(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$24, %eax
-	movl	(%esi,%eax,8), %edi
-	xorl	%edi, %ecx
-	movl	4(%esi,%eax,8), %edi
-	xorl	%eax, %eax
-	xorl	%edi, %edx
-	movl	-252(%ebp), %edi
-	addl	%ecx, %edi
-	cmpl	%ecx, %edi
-	movl	-256(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%edi, %eax
-	addl	%ecx, %edx
-	leal	0(,%edi,4), %ecx
-	movl	%edx, -296(%ebp)
-	addl	%ecx, %edi
-	shrl	$30, %eax
-	movl	%edi, -300(%ebp)
-	sall	$2, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %edi
-	movl	-296(%ebp), %edi
-	setb	%al
-	movl	-56(%ebp), %ecx
-	addl	%eax, %edx
-	addl	%edi, %edx
-	movl	%edx, -328(%ebp)
-	movl	-304(%ebp), %edx
-	xorl	%edx, %ecx
-	movl	%ecx, -340(%ebp)
-	movl	-308(%ebp), %eax
-	movl	-60(%ebp), %edi
-	movl	-64(%ebp), %edx
-	movl	-68(%ebp), %ecx
-	xorl	%eax, %edi
-	movl	%edi, -348(%ebp)
-	movl	-312(%ebp), %eax
-	movl	-316(%ebp), %edi
-	xorl	%edx, %eax
-	xorl	%ecx, %edi
-	movl	%edi, -324(%ebp)
-	movzbl	%al,%edi
-	movl	%eax, -320(%ebp)
-	shrl	$13, %eax
-	movl	(%esi,%edi,8), %ecx
-	andl	$2040, %eax
-	movl	2048(%eax,%esi), %edx
-	movl	2052(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	4(%esi,%edi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-324(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	4100(%eax,%esi), %edi
-	movl	-324(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	6148(%eax,%esi), %edi
-	xorl	%eax, %eax
-	movl	-300(%ebp), %esi
-	xorl	%edi, %edx
-	cmpl	%ecx, -300(%ebp)
-	movl	-328(%ebp), %edi
-	setb	%al
-	addl	%eax, %edx
-	subl	%ecx, %esi
-	subl	%edx, %edi
-	movl	%esi, -352(%ebp)
-	movl	-320(%ebp), %edx
-	movl	tiger_table@GOT(%ebx), %ecx
-	movl	%edi, -356(%ebp)
-	movl	%edx, %eax
-	movzbl	%dh, %esi
-	shrl	$24, %eax
-	leal	0(,%esi,8), %edi
-	movl	%ecx, %esi
-	sall	$3, %eax
-	movl	4096(%eax,%ecx), %edx
-	movl	6144(%edi,%ecx), %ecx
-	movl	4100(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%edi,%esi), %edx
-	xorl	%eax, %edx
-	movl	-324(%ebp), %eax
-	movzbl	%ah, %edi
-	leal	0(,%edi,8), %eax
-	movl	2048(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	2052(%eax,%esi), %edi
-	movl	-324(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$24, %eax
-	movl	(%esi,%eax,8), %edi
-	xorl	%edi, %ecx
-	movl	4(%esi,%eax,8), %edi
-	xorl	%eax, %eax
-	xorl	%edi, %edx
-	movl	-284(%ebp), %edi
-	addl	%ecx, %edi
-	cmpl	%ecx, %edi
-	movl	-288(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%edi, %eax
-	addl	%ecx, %edx
-	leal	0(,%edi,4), %ecx
-	movl	%edx, -332(%ebp)
-	addl	%ecx, %edi
-	shrl	$30, %eax
-	movl	%edi, -336(%ebp)
-	sall	$2, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %edi
-	movl	-332(%ebp), %ecx
-	setb	%al
-	movl	-340(%ebp), %edi
-	addl	%eax, %edx
-	addl	%ecx, %edx
-	movl	-340(%ebp), %eax
-	movl	-348(%ebp), %ecx
-	movl	%edx, -368(%ebp)
-	movl	-64(%ebp), %edx
-	addl	%eax, %edx
-	movl	%edx, -344(%ebp)
-	xorl	%eax, %eax
-	cmpl	%edi, %edx
-	movl	-68(%ebp), %edi
-	setb	%al
-	addl	%ecx, %eax
-	movl	-72(%ebp), %edx
-	addl	%edi, %eax
-	movl	-76(%ebp), %ecx
-	movl	%eax, -380(%ebp)
-	movl	-356(%ebp), %edi
-	movl	-352(%ebp), %eax
-	xorl	%ecx, %edi
-	xorl	%edx, %eax
-	movl	%edi, -364(%ebp)
-	movzbl	%al,%edi
-	movl	%eax, -360(%ebp)
-	shrl	$13, %eax
-	movl	(%esi,%edi,8), %ecx
-	andl	$2040, %eax
-	movl	2048(%eax,%esi), %edx
-	movl	2052(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	4(%esi,%edi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-364(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	4100(%eax,%esi), %edi
-	movl	-364(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	6148(%eax,%esi), %edi
-	xorl	%eax, %eax
-	movl	-336(%ebp), %esi
-	xorl	%edi, %edx
-	cmpl	%ecx, -336(%ebp)
-	movl	-368(%ebp), %edi
-	setb	%al
-	addl	%eax, %edx
-	subl	%ecx, %esi
-	subl	%edx, %edi
-	movl	%esi, -384(%ebp)
-	movl	-360(%ebp), %edx
-	movl	tiger_table@GOT(%ebx), %ecx
-	movl	%edi, -388(%ebp)
-	movl	%edx, %eax
-	movzbl	%dh, %esi
-	shrl	$24, %eax
-	leal	0(,%esi,8), %edi
-	movl	%ecx, %esi
-	sall	$3, %eax
-	movl	4096(%eax,%ecx), %edx
-	movl	6144(%edi,%ecx), %ecx
-	movl	4100(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%edi,%esi), %edx
-	xorl	%eax, %edx
-	movl	-364(%ebp), %eax
-	movzbl	%ah, %edi
-	leal	0(,%edi,8), %eax
-	movl	2048(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	2052(%eax,%esi), %edi
-	movl	-364(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$24, %eax
-	movl	(%esi,%eax,8), %edi
-	xorl	%edi, %ecx
-	movl	4(%esi,%eax,8), %edi
-	xorl	%eax, %eax
-	xorl	%edi, %edx
-	movl	-320(%ebp), %edi
-	addl	%ecx, %edi
-	cmpl	%ecx, %edi
-	setb	%al
-	leal	0(,%edi,4), %ecx
-	addl	%eax, %edx
-	movl	-324(%ebp), %eax
-	movl	%edi, %esi
-	shrl	$30, %esi
-	addl	%ecx, %edi
-	addl	%eax, %edx
-	xorl	%eax, %eax
-	movl	%edx, -372(%ebp)
-	sall	$2, %edx
-	orl	%esi, %edx
-	movl	%edi, -376(%ebp)
-	cmpl	%ecx, %edi
-	movl	-372(%ebp), %esi
-	setb	%al
-	movl	-344(%ebp), %ecx
-	addl	%eax, %edx
-	addl	%esi, %edx
-	movl	-340(%ebp), %eax
-	notl	%ecx
-	movl	%edx, -400(%ebp)
-	movl	-348(%ebp), %edx
-	shrl	$23, %eax
-	movl	-72(%ebp), %edi
-	sall	$9, %edx
-	movl	-380(%ebp), %esi
-	orl	%edx, %eax
-	xorl	%eax, %ecx
-	xorl	%edx, %edx
-	cmpl	%ecx, -72(%ebp)
-	movl	-348(%ebp), %eax
-	notl	%eax
-	setb	%dl
-	subl	%ecx, %edi
-	shrl	$23, %eax
-	movl	%edi, -412(%ebp)
-	movl	-76(%ebp), %ecx
-	xorl	%esi, %eax
-	addl	%edx, %eax
-	movl	-80(%ebp), %edx
-	movl	-84(%ebp), %edi
-	subl	%eax, %ecx
-	movl	-384(%ebp), %eax
-	movl	-388(%ebp), %esi
-	movl	%ecx, -416(%ebp)
-	xorl	%edx, %eax
-	movl	tiger_table@GOT(%ebx), %ecx
-	movl	%eax, -392(%ebp)
-	xorl	%edi, %esi
-	movzbl	%al,%edi
-	movl	%esi, -396(%ebp)
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	%ecx, %esi
-	movl	2048(%eax,%ecx), %edx
-	movl	(%ecx,%edi,8), %ecx
-	movl	2052(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	4(%esi,%edi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-396(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	4100(%eax,%esi), %edi
-	movl	-396(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	6148(%eax,%esi), %edi
-	xorl	%eax, %eax
-	xorl	%edi, %edx
-	movl	-376(%ebp), %edi
-	cmpl	%ecx, -376(%ebp)
-	setb	%al
-	addl	%eax, %edx
-	movl	-400(%ebp), %eax
-	subl	%ecx, %edi
-	movl	%edi, -488(%ebp)
-	subl	%edx, %eax
-	movl	%eax, -492(%ebp)
-	movl	-392(%ebp), %ecx
-	movzbl	%ch, %edi
-	movl	%ecx, %eax
-	sall	$3, %edi
-	shrl	$24, %eax
-	movl	%edi, -88(%ebp)
-	sall	$3, %eax
-	movl	4096(%eax,%esi), %ecx
-	movl	-88(%ebp), %edx
-	movl	%esi, %edi
-	movl	%ecx, -1116(%ebp)
-	movl	4100(%eax,%esi), %eax
-	xorl	6144(%edx,%esi), %ecx
-	movl	%eax, %edx
-	movl	-88(%ebp), %eax
-	xorl	6148(%eax,%esi), %edx
-	movl	-396(%ebp), %eax
-	movzbl	%ah, %eax
-	movl	%eax, -1112(%ebp)
-	sall	$3, %eax
-	xorl	2048(%eax,%esi), %ecx
-	xorl	2052(%eax,%esi), %edx
-	movl	-396(%ebp), %eax
-	shrl	$24, %eax
-	xorl	(%esi,%eax,8), %ecx
-	xorl	4(%esi,%eax,8), %edx
-	movl	-360(%ebp), %esi
-	xorl	%eax, %eax
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-364(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	addl	%ecx, %edx
-	movl	%edx, -404(%ebp)
-	leal	0(,%esi,4), %ecx
-	movl	%esi, %eax
-	shrl	$30, %eax
-	addl	%ecx, %esi
-	sall	$2, %edx
-	movl	%esi, -408(%ebp)
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %esi
-	setb	%al
-	movl	-412(%ebp), %ecx
-	addl	%eax, %edx
-	movl	-404(%ebp), %eax
-	movl	-416(%ebp), %esi
-	addl	%eax, %edx
-	xorl	%eax, %eax
-	movl	%edx, -504(%ebp)
-	movl	-80(%ebp), %edx
-	xorl	%ecx, %edx
-	movl	%edx, -420(%ebp)
-	movl	-84(%ebp), %ecx
-	xorl	%esi, %ecx
-	movl	%ecx, -428(%ebp)
-	movl	-196(%ebp), %esi
-	addl	%edx, %esi
-	movl	%esi, -424(%ebp)
-	cmpl	%edx, %esi
-	setb	%al
-	addl	%ecx, %eax
-	addl	-200(%ebp), %eax
-	movl	%eax, -432(%ebp)
-	movl	%edx, %eax
-	notl	%eax
-	sall	$19, %eax
-	xorl	%edx, %edx
-	xorl	%esi, %eax
-	cmpl	%eax, -232(%ebp)
-	setb	%dl
-	movl	%edx, -436(%ebp)
-	movl	-232(%ebp), %esi
-	movl	-436(%ebp), %edx
-	subl	%eax, %esi
-	movl	%ecx, %eax
-	movl	-420(%ebp), %ecx
-	movl	%esi, -440(%ebp)
-	movl	-432(%ebp), %esi
-	sall	$19, %eax
-	shrl	$13, %ecx
-	orl	%ecx, %eax
-	notl	%esi
-	xorl	%eax, %esi
-	movl	-240(%ebp), %ecx
-	addl	%edx, %esi
-	movl	-272(%ebp), %edx
-	subl	%esi, %ecx
-	movl	-440(%ebp), %eax
-	movl	%ecx, -444(%ebp)
-	movl	-236(%ebp), %esi
-	xorl	%ecx, %edx
-	movl	%edx, -456(%ebp)
-	movl	-304(%ebp), %edx
-	xorl	%eax, %esi
-	movl	%esi, -448(%ebp)
-	movl	-456(%ebp), %ecx
-	xorl	%eax, %eax
-	addl	%esi, %edx
-	cmpl	%esi, %edx
-	setb	%al
-	movl	%edx, -452(%ebp)
-	addl	%ecx, %eax
-	movl	-308(%ebp), %ecx
-	addl	%ecx, %eax
-	movl	%edx, %ecx
-	notl	%ecx
-	movl	%eax, -460(%ebp)
-	movl	%esi, %eax
-	movl	-456(%ebp), %esi
-	shrl	$23, %eax
-	xorl	%edx, %edx
-	sall	$9, %esi
-	orl	%esi, %eax
-	xorl	%eax, %ecx
-	movl	-340(%ebp), %esi
-	cmpl	%ecx, -340(%ebp)
-	movl	-456(%ebp), %eax
-	setb	%dl
-	subl	%ecx, %esi
-	movl	-460(%ebp), %ecx
-	notl	%eax
-	movl	%esi, -464(%ebp)
-	shrl	$23, %eax
-	xorl	%ecx, %eax
-	movl	-344(%ebp), %ecx
-	addl	%edx, %eax
-	movl	-348(%ebp), %edx
-	xorl	%esi, %ecx
-	movl	-380(%ebp), %esi
-	subl	%eax, %edx
-	movl	%edx, -468(%ebp)
-	movl	%ecx, -472(%ebp)
-	xorl	%edx, %esi
-	movl	-412(%ebp), %edx
-	movl	%esi, -480(%ebp)
-	addl	%ecx, %edx
-	cmpl	%ecx, %edx
-	movl	%edx, -476(%ebp)
-	setb	%cl
-	movzbl	%cl, %eax
-	addl	%esi, %eax
-	movl	-476(%ebp), %edx
-	movl	-416(%ebp), %esi
-	movl	-420(%ebp), %ecx
-	xorl	$-1985229329, %edx
-	addl	%esi, %eax
-	movl	%eax, -484(%ebp)
-	xorl	%eax, %eax
-	cmpl	%edx, -420(%ebp)
-	setb	%al
-	subl	%edx, %ecx
-	movl	%ecx, -524(%ebp)
-	movl	-484(%ebp), %esi
-	movl	-428(%ebp), %ecx
-	movl	-424(%ebp), %edx
-	xorl	$19088743, %esi
-	addl	%eax, %esi
-	subl	%esi, %ecx
-	movl	-432(%ebp), %eax
-	movl	-492(%ebp), %esi
-	movl	%ecx, -528(%ebp)
-	movl	-488(%ebp), %ecx
-	xorl	%eax, %esi
-	movl	%esi, -500(%ebp)
-	xorl	%edx, %ecx
-	movl	-408(%ebp), %esi
-	movl	%ecx, -496(%ebp)
-	movl	%ecx, %eax
-	movzbl	%cl,%edx
-	movl	%edx, -92(%ebp)
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	2048(%eax,%edi), %ecx
-	movl	%ecx, -1120(%ebp)
-	movl	2052(%eax,%edi), %eax
-	xorl	(%edi,%edx,8), %ecx
-	movl	%eax, %edx
-	movl	-92(%ebp), %eax
-	xorl	4(%edi,%eax,8), %edx
-	movzbl	-500(%ebp),%eax
-	sall	$3, %eax
-	xorl	4096(%eax,%edi), %ecx
-	xorl	4100(%eax,%edi), %edx
-	movl	-500(%ebp), %eax
-	shrl	$13, %eax
-	andl	$2040, %eax
-	xorl	6144(%eax,%edi), %ecx
-	xorl	6148(%eax,%edi), %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, -408(%ebp)
-	setb	%al
-	addl	%eax, %edx
-	movl	-504(%ebp), %eax
-	subl	%ecx, %esi
-	movl	%esi, -532(%ebp)
-	subl	%edx, %eax
-	movl	-496(%ebp), %edx
-	movl	%eax, -536(%ebp)
-	movzbl	%dh, %esi
-	movl	%edx, %eax
-	sall	$3, %esi
-	shrl	$24, %eax
-	sall	$3, %eax
-	movl	%esi, -96(%ebp)
-	movl	4096(%eax,%edi), %ecx
-	movl	-96(%ebp), %esi
-	movl	%ecx, -1124(%ebp)
-	movl	4100(%eax,%edi), %eax
-	xorl	6144(%esi,%edi), %ecx
-	movl	%eax, %edx
-	movl	-500(%ebp), %eax
-	xorl	6148(%esi,%edi), %edx
-	movzbl	%ah, %eax
-	movl	%eax, -1112(%ebp)
-	sall	$3, %eax
-	movl	-392(%ebp), %esi
-	xorl	2048(%eax,%edi), %ecx
-	xorl	2052(%eax,%edi), %edx
-	movl	-500(%ebp), %eax
-	shrl	$24, %eax
-	xorl	(%edi,%eax,8), %ecx
-	xorl	4(%edi,%eax,8), %edx
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	%esi, -508(%ebp)
-	setb	%al
-	movl	-396(%ebp), %ecx
-	movzbl	%al, %esi
-	addl	%esi, %edx
-	movl	-508(%ebp), %eax
-	addl	%ecx, %edx
-	movl	-508(%ebp), %esi
-	movl	%edx, -512(%ebp)
-	movl	-512(%ebp), %ecx
-	shrl	$29, %eax
-	movl	-508(%ebp), %edx
-	sall	$3, %ecx
-	sall	$3, %edx
-	orl	%eax, %ecx
-	xorl	%eax, %eax
-	cmpl	-508(%ebp), %edx
-	setb	%al
-	subl	%esi, %edx
-	movl	%edx, -516(%ebp)
-	movl	-512(%ebp), %esi
-	movl	-524(%ebp), %edx
-	addl	%esi, %eax
-	xorl	$-1515870811, %edx
-	subl	%eax, %ecx
-	movl	%ecx, -520(%ebp)
-	xorl	%eax, %eax
-	cmpl	%edx, -424(%ebp)
-	movl	-424(%ebp), %ecx
-	movl	-528(%ebp), %esi
-	setb	%al
-	subl	%edx, %ecx
-	xorl	$-1515870811, %esi
-	movl	%ecx, -564(%ebp)
-	movl	-432(%ebp), %ecx
-	addl	%eax, %esi
-	movl	-440(%ebp), %edx
-	movl	-444(%ebp), %eax
-	subl	%esi, %ecx
-	movl	%ecx, -568(%ebp)
-	movl	-532(%ebp), %ecx
-	movl	-536(%ebp), %esi
-	xorl	%edx, %ecx
-	xorl	%eax, %esi
-	movl	%ecx, %eax
-	movl	%esi, -544(%ebp)
-	movzbl	%cl,%edx
-	shrl	$13, %eax
-	movl	%edx, -100(%ebp)
-	andl	$2040, %eax
-	movl	-516(%ebp), %esi
-	movl	%ecx, -540(%ebp)
-	movl	2048(%eax,%edi), %ecx
-	movl	%ecx, -1128(%ebp)
-	movl	2052(%eax,%edi), %eax
-	xorl	(%edi,%edx,8), %ecx
-	movl	%eax, %edx
-	movl	-100(%ebp), %eax
-	xorl	4(%edi,%eax,8), %edx
-	movzbl	-544(%ebp),%eax
-	sall	$3, %eax
-	xorl	4096(%eax,%edi), %ecx
-	xorl	4100(%eax,%edi), %edx
-	movl	-544(%ebp), %eax
-	shrl	$13, %eax
-	andl	$2040, %eax
-	xorl	6144(%eax,%edi), %ecx
-	xorl	6148(%eax,%edi), %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, -516(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -572(%ebp)
-	addl	%eax, %edx
-	movl	-520(%ebp), %eax
-	subl	%edx, %eax
-	movl	-540(%ebp), %edx
-	movl	%eax, -576(%ebp)
-	movzbl	%dh, %esi
-	movl	%edx, %eax
-	sall	$3, %esi
-	shrl	$24, %eax
-	sall	$3, %eax
-	movl	%esi, -104(%ebp)
-	movl	4096(%eax,%edi), %ecx
-	movl	-104(%ebp), %esi
-	movl	%ecx, -1132(%ebp)
-	movl	4100(%eax,%edi), %eax
-	xorl	6144(%esi,%edi), %ecx
-	movl	%eax, %edx
-	movl	-544(%ebp), %eax
-	xorl	6148(%esi,%edi), %edx
-	movzbl	%ah, %eax
-	movl	%eax, -1112(%ebp)
-	sall	$3, %eax
-	xorl	2048(%eax,%edi), %ecx
-	xorl	2052(%eax,%edi), %edx
-	movl	-544(%ebp), %eax
-	shrl	$24, %eax
-	xorl	(%edi,%eax,8), %ecx
-	xorl	4(%edi,%eax,8), %edx
-	movl	-496(%ebp), %eax
-	addl	%ecx, %eax
-	movl	%eax, -548(%ebp)
-	cmpl	%ecx, %eax
-	setb	%cl
-	movzbl	%cl, %eax
-	movl	-500(%ebp), %ecx
-	addl	%eax, %edx
-	movl	-548(%ebp), %eax
-	addl	%ecx, %edx
-	shrl	$29, %eax
-	movl	%edx, -552(%ebp)
-	movl	-552(%ebp), %ecx
-	movl	-548(%ebp), %edx
-	sall	$3, %ecx
-	sall	$3, %edx
-	orl	%eax, %ecx
-	xorl	%eax, %eax
-	cmpl	-548(%ebp), %edx
-	setb	%al
-	subl	-548(%ebp), %edx
-	movl	%edx, -556(%ebp)
-	movl	-552(%ebp), %edx
-	addl	%edx, %eax
-	subl	%eax, %ecx
-	movl	-440(%ebp), %edx
-	movl	-568(%ebp), %eax
-	movl	%ecx, -560(%ebp)
-	movl	-564(%ebp), %ecx
-	xorl	%ecx, %edx
-	movl	%edx, -604(%ebp)
-	movl	-444(%ebp), %ecx
-	xorl	%eax, %ecx
-	movl	%ecx, -612(%ebp)
-	movl	-448(%ebp), %edx
-	movl	-572(%ebp), %eax
-	movl	-456(%ebp), %ecx
-	xorl	%edx, %eax
-	movl	-576(%ebp), %edx
-	movl	%eax, -580(%ebp)
-	xorl	%ecx, %edx
-	movzbl	%al,%ecx
-	movl	%edx, -584(%ebp)
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	%ecx, -108(%ebp)
-	movl	2048(%eax,%edi), %esi
-	movl	-108(%ebp), %edx
-	movl	2052(%eax,%edi), %eax
-	movl	%esi, %ecx
-	movl	-556(%ebp), %esi
-	xorl	(%edi,%edx,8), %ecx
-	movl	%eax, %edx
-	movl	-108(%ebp), %eax
-	xorl	4(%edi,%eax,8), %edx
-	movzbl	-584(%ebp),%eax
-	sall	$3, %eax
-	xorl	4096(%eax,%edi), %ecx
-	xorl	4100(%eax,%edi), %edx
-	movl	-584(%ebp), %eax
-	shrl	$13, %eax
-	andl	$2040, %eax
-	xorl	6144(%eax,%edi), %ecx
-	xorl	6148(%eax,%edi), %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, -556(%ebp)
-	setb	%al
-	addl	%eax, %edx
-	movl	-560(%ebp), %eax
-	subl	%ecx, %esi
-	movl	%esi, -616(%ebp)
-	subl	%edx, %eax
-	movl	-580(%ebp), %edx
-	movl	%eax, -620(%ebp)
-	movzbl	%dh, %esi
-	movl	%edx, %eax
-	sall	$3, %esi
-	shrl	$24, %eax
-	sall	$3, %eax
-	movl	%esi, -112(%ebp)
-	movl	4096(%eax,%edi), %ecx
-	movl	-112(%ebp), %esi
-	movl	%ecx, -1140(%ebp)
-	movl	4100(%eax,%edi), %eax
-	xorl	6144(%esi,%edi), %ecx
-	movl	%eax, %edx
-	movl	-584(%ebp), %eax
-	xorl	6148(%esi,%edi), %edx
-	movzbl	%ah, %eax
-	movl	%eax, -1112(%ebp)
-	sall	$3, %eax
-	xorl	2048(%eax,%edi), %ecx
-	xorl	2052(%eax,%edi), %edx
-	movl	-584(%ebp), %eax
-	shrl	$24, %eax
-	xorl	(%edi,%eax,8), %ecx
-	xorl	4(%edi,%eax,8), %edx
-	movl	-540(%ebp), %eax
-	addl	%ecx, %eax
-	movl	%eax, -588(%ebp)
-	cmpl	%ecx, %eax
-	setb	%cl
-	movzbl	%cl, %eax
-	movl	-544(%ebp), %ecx
-	addl	%eax, %edx
-	movl	-588(%ebp), %eax
-	addl	%ecx, %edx
-	shrl	$29, %eax
-	movl	%edx, -592(%ebp)
-	movl	-592(%ebp), %ecx
-	movl	-588(%ebp), %edx
-	sall	$3, %ecx
-	sall	$3, %edx
-	orl	%eax, %ecx
-	xorl	%eax, %eax
-	cmpl	-588(%ebp), %edx
-	setb	%al
-	subl	-588(%ebp), %edx
-	movl	%edx, -596(%ebp)
-	movl	-592(%ebp), %edx
-	addl	%edx, %eax
-	subl	%eax, %ecx
-	movl	-448(%ebp), %edx
-	movl	-604(%ebp), %eax
-	movl	%ecx, -600(%ebp)
-	movl	-604(%ebp), %ecx
-	addl	%eax, %edx
-	movl	%edx, -608(%ebp)
-	xorl	%eax, %eax
-	cmpl	%ecx, %edx
-	movl	-612(%ebp), %edx
-	setb	%al
-	movl	-456(%ebp), %ecx
-	addl	%edx, %eax
-	addl	%ecx, %eax
-	movl	-452(%ebp), %edx
-	movl	%eax, -644(%ebp)
-	movl	-616(%ebp), %eax
-	movl	-460(%ebp), %ecx
-	xorl	%edx, %eax
-	movl	-620(%ebp), %edx
-	movl	%eax, -624(%ebp)
-	xorl	%ecx, %edx
-	movzbl	%al,%ecx
-	movl	%edx, -628(%ebp)
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	%ecx, -116(%ebp)
-	movl	2048(%eax,%edi), %esi
-	movl	-116(%ebp), %edx
-	movl	2052(%eax,%edi), %eax
-	movl	%esi, %ecx
-	movl	-596(%ebp), %esi
-	xorl	(%edi,%edx,8), %ecx
-	movl	%eax, %edx
-	movl	-116(%ebp), %eax
-	xorl	4(%edi,%eax,8), %edx
-	movzbl	-628(%ebp),%eax
-	sall	$3, %eax
-	xorl	4096(%eax,%edi), %ecx
-	xorl	4100(%eax,%edi), %edx
-	movl	-628(%ebp), %eax
-	shrl	$13, %eax
-	andl	$2040, %eax
-	xorl	6144(%eax,%edi), %ecx
-	xorl	6148(%eax,%edi), %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, -596(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -652(%ebp)
-	addl	%eax, %edx
-	movl	-600(%ebp), %eax
-	subl	%edx, %eax
-	movl	-624(%ebp), %edx
-	movl	%eax, -656(%ebp)
-	movzbl	%dh, %esi
-	movl	%edx, %eax
-	sall	$3, %esi
-	shrl	$24, %eax
-	sall	$3, %eax
-	movl	%esi, -120(%ebp)
-	movl	4096(%eax,%edi), %ecx
-	movl	-120(%ebp), %esi
-	movl	%ecx, -1148(%ebp)
-	movl	4100(%eax,%edi), %eax
-	xorl	6144(%esi,%edi), %ecx
-	movl	%eax, %edx
-	movl	-628(%ebp), %eax
-	xorl	6148(%esi,%edi), %edx
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-628(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-580(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-584(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	addl	%ecx, %edx
-	movl	%edx, -632(%ebp)
-	movl	%esi, %eax
-	movl	-632(%ebp), %ecx
-	leal	0(,%esi,8), %edx
-	shrl	$29, %eax
-	sall	$3, %ecx
-	orl	%eax, %ecx
-	xorl	%eax, %eax
-	cmpl	%esi, %edx
-	setb	%al
-	subl	%esi, %edx
-	movl	-632(%ebp), %esi
-	movl	%edx, -636(%ebp)
-	xorl	%edx, %edx
-	addl	%esi, %eax
-	subl	%eax, %ecx
-	movl	-604(%ebp), %eax
-	movl	-452(%ebp), %esi
-	movl	%ecx, -640(%ebp)
-	movl	-608(%ebp), %ecx
-	notl	%eax
-	sall	$19, %eax
-	xorl	%ecx, %eax
-	movl	-604(%ebp), %ecx
-	cmpl	%eax, -452(%ebp)
-	setb	%dl
-	subl	%eax, %esi
-	movl	%esi, -680(%ebp)
-	movl	-612(%ebp), %eax
-	shrl	$13, %ecx
-	movl	%edx, -648(%ebp)
-	movl	-644(%ebp), %esi
-	movl	-648(%ebp), %edx
-	sall	$19, %eax
-	orl	%ecx, %eax
-	notl	%esi
-	xorl	%eax, %esi
-	movl	-460(%ebp), %eax
-	addl	%edx, %esi
-	subl	%esi, %eax
-	movl	%eax, -684(%ebp)
-	movl	-464(%ebp), %ecx
-	movl	-652(%ebp), %eax
-	movl	-468(%ebp), %edx
-	movl	-656(%ebp), %esi
-	xorl	%ecx, %eax
-	movl	%eax, -660(%ebp)
-	movzbl	%al,%ecx
-	xorl	%edx, %esi
-	movl	%esi, -664(%ebp)
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	%ecx, -124(%ebp)
-	movl	%ecx, %esi
-	movl	2048(%eax,%edi), %edx
-	movl	(%edi,%ecx,8), %ecx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-664(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-664(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-636(%ebp), %esi
-	cmpl	%ecx, -636(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -688(%ebp)
-	movl	-660(%ebp), %ecx
-	addl	%eax, %edx
-	movl	-640(%ebp), %eax
-	movzbl	%ch, %esi
-	sall	$3, %esi
-	movl	%esi, -128(%ebp)
-	subl	%edx, %eax
-	movl	-128(%ebp), %esi
-	movl	%eax, -692(%ebp)
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	sall	$3, %eax
-	movl	6144(%esi,%edi), %ecx
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-664(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-664(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-624(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-628(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	shrl	$29, %eax
-	movl	%edx, -668(%ebp)
-	movl	-668(%ebp), %ecx
-	leal	0(,%esi,8), %edx
-	sall	$3, %ecx
-	orl	%eax, %ecx
-	xorl	%eax, %eax
-	cmpl	%esi, %edx
-	setb	%al
-	subl	%esi, %edx
-	movl	%edx, -672(%ebp)
-	movl	-668(%ebp), %esi
-	movl	-464(%ebp), %edx
-	addl	%esi, %eax
-	subl	%eax, %ecx
-	movl	-468(%ebp), %esi
-	movl	%ecx, -676(%ebp)
-	movl	-684(%ebp), %eax
-	movl	-680(%ebp), %ecx
-	xorl	%eax, %esi
-	xorl	%ecx, %edx
-	movl	-688(%ebp), %eax
-	movl	%edx, -716(%ebp)
-	movl	-472(%ebp), %ecx
-	movl	-480(%ebp), %edx
-	movl	%esi, -724(%ebp)
-	movl	-692(%ebp), %esi
-	xorl	%ecx, %eax
-	movl	%eax, -696(%ebp)
-	movzbl	%al,%ecx
-	shrl	$13, %eax
-	movl	%ecx, -132(%ebp)
-	xorl	%edx, %esi
-	andl	$2040, %eax
-	movl	%esi, -700(%ebp)
-	movl	2048(%eax,%edi), %edx
-	movl	%ecx, %esi
-	movl	(%edi,%ecx,8), %ecx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-700(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-700(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-672(%ebp), %esi
-	cmpl	%ecx, -672(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -728(%ebp)
-	movl	-696(%ebp), %ecx
-	addl	%eax, %edx
-	movl	-676(%ebp), %eax
-	movzbl	%ch, %esi
-	subl	%edx, %eax
-	movl	%eax, -732(%ebp)
-	sall	$3, %esi
-	movl	%ecx, %eax
-	movl	%esi, -136(%ebp)
-	movl	-136(%ebp), %esi
-	shrl	$24, %eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %edx
-	movl	6144(%esi,%edi), %ecx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-700(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-700(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-660(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-664(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	shrl	$29, %eax
-	movl	%edx, -704(%ebp)
-	movl	-704(%ebp), %ecx
-	leal	0(,%esi,8), %edx
-	sall	$3, %ecx
-	orl	%eax, %ecx
-	xorl	%eax, %eax
-	cmpl	%esi, %edx
-	setb	%al
-	subl	%esi, %edx
-	movl	%edx, -708(%ebp)
-	movl	-704(%ebp), %esi
-	movl	-472(%ebp), %edx
-	addl	%esi, %eax
-	subl	%eax, %ecx
-	movl	-716(%ebp), %eax
-	movl	%ecx, -712(%ebp)
-	movl	-716(%ebp), %ecx
-	movl	-480(%ebp), %esi
-	addl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %edx
-	movl	%edx, -720(%ebp)
-	movl	-724(%ebp), %edx
-	setb	%al
-	movl	-476(%ebp), %ecx
-	addl	%edx, %eax
-	addl	%esi, %eax
-	movl	%eax, -756(%ebp)
-	movl	-728(%ebp), %eax
-	movl	-484(%ebp), %edx
-	movl	-732(%ebp), %esi
-	xorl	%ecx, %eax
-	movzbl	%al,%ecx
-	movl	%eax, -736(%ebp)
-	xorl	%edx, %esi
-	shrl	$13, %eax
-	movl	%esi, -740(%ebp)
-	andl	$2040, %eax
-	movl	%ecx, %esi
-	movl	%ecx, -140(%ebp)
-	movl	2048(%eax,%edi), %edx
-	movl	(%edi,%ecx,8), %ecx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-740(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-740(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-708(%ebp), %esi
-	cmpl	%ecx, -708(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -760(%ebp)
-	movl	-736(%ebp), %ecx
-	addl	%eax, %edx
-	movl	-712(%ebp), %eax
-	movzbl	%ch, %esi
-	sall	$3, %esi
-	movl	%esi, -144(%ebp)
-	subl	%edx, %eax
-	movl	-144(%ebp), %esi
-	movl	%eax, -764(%ebp)
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	sall	$3, %eax
-	movl	6144(%esi,%edi), %ecx
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-740(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	%edi, %esi
-	movl	2048(%eax,%edi), %edi
-	xorl	%edi, %ecx
-	movl	2052(%eax,%esi), %edi
-	movl	-740(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$24, %eax
-	movl	(%esi,%eax,8), %edi
-	xorl	%edi, %ecx
-	movl	4(%esi,%eax,8), %edi
-	xorl	%eax, %eax
-	xorl	%edi, %edx
-	movl	-696(%ebp), %edi
-	addl	%ecx, %edi
-	cmpl	%ecx, %edi
-	movl	-700(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%edi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -744(%ebp)
-	shrl	$29, %eax
-	movl	-744(%ebp), %ecx
-	leal	0(,%edi,8), %edx
-	sall	$3, %ecx
-	orl	%eax, %ecx
-	xorl	%eax, %eax
-	cmpl	%edi, %edx
-	setb	%al
-	subl	%edi, %edx
-	movl	-744(%ebp), %edi
-	movl	%edx, -748(%ebp)
-	movl	-724(%ebp), %edx
-	addl	%edi, %eax
-	subl	%eax, %ecx
-	movl	-716(%ebp), %eax
-	sall	$9, %edx
-	movl	%ecx, -752(%ebp)
-	movl	-720(%ebp), %ecx
-	shrl	$23, %eax
-	orl	%edx, %eax
-	movl	-476(%ebp), %edi
-	notl	%ecx
-	xorl	%eax, %ecx
-	xorl	%eax, %eax
-	movl	-756(%ebp), %edx
-	cmpl	%ecx, -476(%ebp)
-	setb	%al
-	subl	%ecx, %edi
-	movl	%edi, -788(%ebp)
-	movl	-724(%ebp), %edi
-	movl	-484(%ebp), %ecx
-	notl	%edi
-	shrl	$23, %edi
-	xorl	%edx, %edi
-	addl	%eax, %edi
-	movl	-524(%ebp), %edx
-	subl	%edi, %ecx
-	movl	%ecx, -792(%ebp)
-	movl	-760(%ebp), %ecx
-	xorl	%edx, %ecx
-	movl	%ecx, -768(%ebp)
-	movl	-528(%ebp), %eax
-	movl	-764(%ebp), %edi
-	xorl	%eax, %edi
-	movl	%ecx, %eax
-	shrl	$13, %eax
-	movl	%edi, -772(%ebp)
-	andl	$2040, %eax
-	movzbl	%cl,%edi
-	movl	2048(%eax,%esi), %edx
-	movl	(%esi,%edi,8), %ecx
-	movl	2052(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	4(%esi,%edi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-772(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	4100(%eax,%esi), %edi
-	movl	-772(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	6148(%eax,%esi), %edi
-	xorl	%eax, %eax
-	movl	-748(%ebp), %esi
-	xorl	%edi, %edx
-	cmpl	%ecx, -748(%ebp)
-	movl	-752(%ebp), %edi
-	setb	%al
-	addl	%eax, %edx
-	subl	%ecx, %esi
-	subl	%edx, %edi
-	movl	%esi, -860(%ebp)
-	movl	-768(%ebp), %edx
-	movl	tiger_table@GOT(%ebx), %ecx
-	movl	%edi, -864(%ebp)
-	movl	%edx, %eax
-	movzbl	%dh, %esi
-	shrl	$24, %eax
-	leal	0(,%esi,8), %edi
-	movl	%ecx, %esi
-	sall	$3, %eax
-	movl	4096(%eax,%ecx), %edx
-	movl	6144(%edi,%ecx), %ecx
-	movl	4100(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%edi,%esi), %edx
-	xorl	%eax, %edx
-	movl	-772(%ebp), %eax
-	movzbl	%ah, %edi
-	leal	0(,%edi,8), %eax
-	movl	2048(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	2052(%eax,%esi), %edi
-	movl	-772(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$24, %eax
-	movl	(%esi,%eax,8), %edi
-	xorl	%edi, %ecx
-	movl	4(%esi,%eax,8), %edi
-	movl	-740(%ebp), %esi
-	xorl	%eax, %eax
-	xorl	%edi, %edx
-	movl	-736(%ebp), %edi
-	addl	%ecx, %edi
-	cmpl	%ecx, %edi
-	setb	%al
-	leal	0(,%edi,8), %ecx
-	addl	%eax, %edx
-	addl	%esi, %edx
-	movl	%edi, %eax
-	movl	%edx, -776(%ebp)
-	shrl	$29, %eax
-	movl	-776(%ebp), %esi
-	sall	$3, %esi
-	orl	%eax, %esi
-	xorl	%eax, %eax
-	cmpl	%edi, %ecx
-	setb	%al
-	subl	%edi, %ecx
-	movl	%ecx, -780(%ebp)
-	movl	-776(%ebp), %edx
-	movl	-788(%ebp), %ecx
-	movl	-568(%ebp), %edi
-	addl	%edx, %eax
-	movl	-524(%ebp), %edx
-	subl	%eax, %esi
-	xorl	%eax, %eax
-	xorl	%ecx, %edx
-	movl	%esi, -784(%ebp)
-	movl	-528(%ebp), %ecx
-	movl	-792(%ebp), %esi
-	movl	%edx, -796(%ebp)
-	xorl	%esi, %ecx
-	movl	-564(%ebp), %esi
-	movl	%ecx, -804(%ebp)
-	addl	%edx, %esi
-	cmpl	%edx, %esi
-	movl	%esi, -800(%ebp)
-	setb	%al
-	addl	%ecx, %eax
-	addl	%edi, %eax
-	movl	%eax, -808(%ebp)
-	movl	%edx, %eax
-	notl	%eax
-	sall	$19, %eax
-	xorl	%esi, %eax
-	movl	-604(%ebp), %esi
-	cmpl	%eax, -604(%ebp)
-	setb	%dl
-	movzbl	%dl, %edi
-	subl	%eax, %esi
-	movl	%ecx, %edx
-	movl	-796(%ebp), %ecx
-	movl	%esi, -812(%ebp)
-	movl	-808(%ebp), %esi
-	sall	$19, %edx
-	movl	-612(%ebp), %eax
-	shrl	$13, %ecx
-	orl	%ecx, %edx
-	notl	%esi
-	movl	-680(%ebp), %ecx
-	xorl	%edx, %esi
-	addl	%edi, %esi
-	movl	-812(%ebp), %edi
-	subl	%esi, %eax
-	movl	%eax, -816(%ebp)
-	movl	-608(%ebp), %esi
-	movl	-684(%ebp), %edx
-	xorl	%edi, %esi
-	movl	-644(%ebp), %edi
-	addl	%esi, %ecx
-	movl	%ecx, -824(%ebp)
-	movl	%esi, -820(%ebp)
-	xorl	%eax, %edi
-	cmpl	%esi, %ecx
-	movl	%edi, -828(%ebp)
-	setb	%al
-	movzbl	%al, %ecx
-	addl	%edi, %ecx
-	movl	%esi, %eax
-	addl	%edx, %ecx
-	movl	%ecx, -832(%ebp)
-	movl	-824(%ebp), %ecx
-	movl	%edi, %esi
-	sall	$9, %esi
-	xorl	%edx, %edx
-	shrl	$23, %eax
-	orl	%esi, %eax
-	notl	%ecx
-	xorl	%eax, %ecx
-	cmpl	%ecx, -716(%ebp)
-	movl	%edi, %eax
-	movl	-716(%ebp), %esi
-	notl	%eax
-	movl	-724(%ebp), %edi
-	setb	%dl
-	subl	%ecx, %esi
-	movl	-832(%ebp), %ecx
-	shrl	$23, %eax
-	movl	%esi, -836(%ebp)
-	xorl	%ecx, %eax
-	addl	%edx, %eax
-	subl	%eax, %edi
-	movl	%edi, -840(%ebp)
-	movl	-720(%ebp), %edx
-	movl	-788(%ebp), %ecx
-	xorl	%esi, %edx
-	movl	-756(%ebp), %esi
-	addl	%edx, %ecx
-	movl	%edx, -844(%ebp)
-	movl	%ecx, -848(%ebp)
-	xorl	%edi, %esi
-	cmpl	%edx, %ecx
-	movl	%esi, -852(%ebp)
-	setb	%al
-	movl	-792(%ebp), %edx
-	movzbl	%al, %eax
-	movl	%ecx, %edi
-	addl	%esi, %eax
-	addl	%edx, %eax
-	xorl	$-1985229329, %edi
-	movl	-796(%ebp), %esi
-	movl	%eax, -856(%ebp)
-	xorl	%ecx, %ecx
-	cmpl	%edi, -796(%ebp)
-	movl	-856(%ebp), %eax
-	movl	-800(%ebp), %edx
-	setb	%cl
-	xorl	$19088743, %eax
-	subl	%edi, %esi
-	movl	%esi, -884(%ebp)
-	addl	%ecx, %eax
-	movl	-804(%ebp), %ecx
-	movl	-808(%ebp), %edi
-	movl	-864(%ebp), %esi
-	subl	%eax, %ecx
-	movl	%ecx, -888(%ebp)
-	movl	-860(%ebp), %eax
-	movl	tiger_table@GOT(%ebx), %ecx
-	xorl	%edi, %esi
-	xorl	%edx, %eax
-	movzbl	%al,%edi
-	movl	%eax, -868(%ebp)
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	%esi, -872(%ebp)
-	movl	2048(%eax,%ecx), %edx
-	movl	%ecx, %esi
-	movl	(%ecx,%edi,8), %ecx
-	movl	2052(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	4(%esi,%edi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-872(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	4100(%eax,%esi), %edi
-	movl	-872(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	6148(%eax,%esi), %edi
-	xorl	%eax, %eax
-	xorl	%edi, %edx
-	movl	-780(%ebp), %edi
-	cmpl	%ecx, -780(%ebp)
-	setb	%al
-	subl	%ecx, %edi
-	movl	%edi, -892(%ebp)
-	movl	-868(%ebp), %ecx
-	movl	-784(%ebp), %edi
-	addl	%eax, %edx
-	movzbl	%ch, %eax
-	subl	%edx, %edi
-	movl	%edi, -896(%ebp)
-	leal	0(,%eax,8), %edi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	movl	6144(%edi,%esi), %ecx
-	sall	$3, %eax
-	movl	4096(%eax,%esi), %edx
-	movl	4100(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%edi,%esi), %edx
-	xorl	%eax, %edx
-	movl	-872(%ebp), %eax
-	movzbl	%ah, %edi
-	leal	0(,%edi,8), %eax
-	movl	2048(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	2052(%eax,%esi), %edi
-	movl	-872(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$24, %eax
-	movl	(%esi,%eax,8), %edi
-	xorl	%edi, %ecx
-	movl	4(%esi,%eax,8), %edi
-	xorl	%eax, %eax
-	xorl	%edi, %edx
-	movl	-768(%ebp), %edi
-	addl	%ecx, %edi
-	cmpl	%ecx, %edi
-	movl	-772(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%edi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -876(%ebp)
-	leal	0(,%edi,8), %ecx
-	addl	%ecx, %edi
-	movl	%edi, -880(%ebp)
-	shrl	$29, %eax
-	sall	$3, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %edi
-	movl	-876(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	-812(%ebp), %eax
-	addl	%ecx, %edx
-	movl	-816(%ebp), %edi
-	movl	%edx, -908(%ebp)
-	movl	-892(%ebp), %edx
-	movl	-896(%ebp), %ecx
-	xorl	%eax, %edx
-	movl	%edx, %eax
-	xorl	%edi, %ecx
-	movl	%ecx, -904(%ebp)
-	shrl	$13, %eax
-	movzbl	%dl,%edi
-	movl	%edx, -900(%ebp)
-	andl	$2040, %eax
-	movl	2048(%eax,%esi), %edx
-	movl	(%esi,%edi,8), %ecx
-	movl	2052(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	4(%esi,%edi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-904(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	4100(%eax,%esi), %edi
-	movl	-904(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	6148(%eax,%esi), %edi
-	xorl	%eax, %eax
-	movl	-880(%ebp), %esi
-	xorl	%edi, %edx
-	cmpl	%ecx, -880(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -920(%ebp)
-	movl	-908(%ebp), %edi
-	addl	%eax, %edx
-	movl	tiger_table@GOT(%ebx), %ecx
-	subl	%edx, %edi
-	movl	-900(%ebp), %edx
-	movl	%edi, -924(%ebp)
-	movl	%edx, %eax
-	movzbl	%dh, %esi
-	shrl	$24, %eax
-	leal	0(,%esi,8), %edi
-	movl	%ecx, %esi
-	sall	$3, %eax
-	movl	4096(%eax,%ecx), %edx
-	movl	6144(%edi,%ecx), %ecx
-	movl	4100(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%edi,%esi), %edx
-	xorl	%eax, %edx
-	movl	-904(%ebp), %eax
-	movzbl	%ah, %edi
-	leal	0(,%edi,8), %eax
-	movl	2048(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	2052(%eax,%esi), %edi
-	movl	-904(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$24, %eax
-	movl	(%esi,%eax,8), %edi
-	xorl	%edi, %ecx
-	movl	4(%esi,%eax,8), %edi
-	xorl	%eax, %eax
-	xorl	%edi, %edx
-	movl	-868(%ebp), %edi
-	addl	%ecx, %edi
-	cmpl	%ecx, %edi
-	movl	-872(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%edi, %eax
-	addl	%ecx, %edx
-	leal	0(,%edi,8), %ecx
-	movl	%edx, -912(%ebp)
-	addl	%ecx, %edi
-	shrl	$29, %eax
-	movl	%edi, -916(%ebp)
-	sall	$3, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %edi
-	movl	-912(%ebp), %ecx
-	setb	%al
-	movl	-828(%ebp), %edi
-	addl	%eax, %edx
-	addl	%ecx, %edx
-	movl	-820(%ebp), %eax
-	movl	-924(%ebp), %ecx
-	movl	%edx, -936(%ebp)
-	movl	-920(%ebp), %edx
-	xorl	%edi, %ecx
-	movl	%ecx, -932(%ebp)
-	xorl	%eax, %edx
-	movl	%edx, %eax
-	movl	%edx, -928(%ebp)
-	shrl	$13, %eax
-	movzbl	%dl,%edi
-	andl	$2040, %eax
-	movl	(%esi,%edi,8), %ecx
-	movl	2048(%eax,%esi), %edx
-	movl	2052(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	4(%esi,%edi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-932(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	4100(%eax,%esi), %edi
-	movl	-932(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	6148(%eax,%esi), %edi
-	xorl	%eax, %eax
-	movl	-916(%ebp), %esi
-	xorl	%edi, %edx
-	cmpl	%ecx, -916(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -948(%ebp)
-	movl	-936(%ebp), %edi
-	addl	%eax, %edx
-	movl	tiger_table@GOT(%ebx), %ecx
-	subl	%edx, %edi
-	movl	-928(%ebp), %edx
-	movl	%edi, -952(%ebp)
-	movl	%edx, %eax
-	movzbl	%dh, %esi
-	shrl	$24, %eax
-	leal	0(,%esi,8), %edi
-	movl	%ecx, %esi
-	sall	$3, %eax
-	movl	4096(%eax,%ecx), %edx
-	movl	6144(%edi,%ecx), %ecx
-	movl	4100(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%edi,%esi), %edx
-	xorl	%eax, %edx
-	movl	-932(%ebp), %eax
-	movzbl	%ah, %edi
-	leal	0(,%edi,8), %eax
-	movl	2048(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	2052(%eax,%esi), %edi
-	movl	-932(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$24, %eax
-	movl	(%esi,%eax,8), %edi
-	xorl	%edi, %ecx
-	movl	4(%esi,%eax,8), %edi
-	xorl	%eax, %eax
-	xorl	%edi, %edx
-	movl	-900(%ebp), %edi
-	addl	%ecx, %edi
-	cmpl	%ecx, %edi
-	movl	-904(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%edi, %eax
-	addl	%ecx, %edx
-	leal	0(,%edi,8), %ecx
-	movl	%edx, -940(%ebp)
-	addl	%ecx, %edi
-	shrl	$29, %eax
-	movl	%edi, -944(%ebp)
-	sall	$3, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %edi
-	movl	-940(%ebp), %ecx
-	setb	%al
-	movl	-832(%ebp), %edi
-	addl	%eax, %edx
-	addl	%ecx, %edx
-	movl	-824(%ebp), %eax
-	movl	-952(%ebp), %ecx
-	movl	%edx, -964(%ebp)
-	movl	-948(%ebp), %edx
-	xorl	%edi, %ecx
-	movl	%ecx, -960(%ebp)
-	xorl	%eax, %edx
-	movl	%edx, %eax
-	movl	%edx, -956(%ebp)
-	shrl	$13, %eax
-	movzbl	%dl,%edi
-	andl	$2040, %eax
-	movl	(%esi,%edi,8), %ecx
-	movl	2048(%eax,%esi), %edx
-	movl	2052(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	4(%esi,%edi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-960(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	4100(%eax,%esi), %edi
-	movl	-960(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	6148(%eax,%esi), %edi
-	xorl	%eax, %eax
-	movl	-944(%ebp), %esi
-	xorl	%edi, %edx
-	cmpl	%ecx, -944(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -976(%ebp)
-	movl	-964(%ebp), %edi
-	addl	%eax, %edx
-	movl	tiger_table@GOT(%ebx), %ecx
-	subl	%edx, %edi
-	movl	-956(%ebp), %edx
-	movl	%edi, -980(%ebp)
-	movl	%edx, %eax
-	movzbl	%dh, %esi
-	shrl	$24, %eax
-	leal	0(,%esi,8), %edi
-	movl	%ecx, %esi
-	sall	$3, %eax
-	movl	4096(%eax,%ecx), %edx
-	movl	6144(%edi,%ecx), %ecx
-	movl	4100(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%edi,%esi), %edx
-	xorl	%eax, %edx
-	movl	-960(%ebp), %eax
-	movzbl	%ah, %edi
-	leal	0(,%edi,8), %eax
-	movl	2048(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	2052(%eax,%esi), %edi
-	movl	-960(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$24, %eax
-	movl	(%esi,%eax,8), %edi
-	xorl	%edi, %ecx
-	movl	4(%esi,%eax,8), %edi
-	xorl	%eax, %eax
-	xorl	%edi, %edx
-	movl	-928(%ebp), %edi
-	addl	%ecx, %edi
-	cmpl	%ecx, %edi
-	movl	-932(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%edi, %eax
-	addl	%ecx, %edx
-	leal	0(,%edi,8), %ecx
-	movl	%edx, -968(%ebp)
-	addl	%ecx, %edi
-	shrl	$29, %eax
-	movl	%edi, -972(%ebp)
-	sall	$3, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %edi
-	movl	-968(%ebp), %ecx
-	setb	%al
-	movl	-840(%ebp), %edi
-	addl	%eax, %edx
-	addl	%ecx, %edx
-	movl	-836(%ebp), %eax
-	movl	-980(%ebp), %ecx
-	movl	%edx, -992(%ebp)
-	movl	-976(%ebp), %edx
-	xorl	%edi, %ecx
-	movl	%ecx, -988(%ebp)
-	xorl	%eax, %edx
-	movl	%edx, %eax
-	movl	%edx, -984(%ebp)
-	shrl	$13, %eax
-	movzbl	%dl,%edi
-	andl	$2040, %eax
-	movl	(%esi,%edi,8), %ecx
-	movl	2048(%eax,%esi), %edx
-	movl	2052(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	4(%esi,%edi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-988(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	4100(%eax,%esi), %edi
-	movl	-988(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	6148(%eax,%esi), %edi
-	xorl	%eax, %eax
-	movl	-972(%ebp), %esi
-	xorl	%edi, %edx
-	cmpl	%ecx, -972(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -1004(%ebp)
-	movl	-992(%ebp), %edi
-	addl	%eax, %edx
-	movl	tiger_table@GOT(%ebx), %ecx
-	subl	%edx, %edi
-	movl	-984(%ebp), %edx
-	movl	%edi, -1008(%ebp)
-	movl	%edx, %eax
-	movzbl	%dh, %esi
-	shrl	$24, %eax
-	leal	0(,%esi,8), %edi
-	movl	%ecx, %esi
-	sall	$3, %eax
-	movl	4096(%eax,%ecx), %edx
-	movl	6144(%edi,%ecx), %ecx
-	movl	4100(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%edi,%esi), %edx
-	xorl	%eax, %edx
-	movl	-988(%ebp), %eax
-	movzbl	%ah, %edi
-	leal	0(,%edi,8), %eax
-	movl	2048(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	2052(%eax,%esi), %edi
-	movl	-988(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$24, %eax
-	movl	(%esi,%eax,8), %edi
-	xorl	%edi, %ecx
-	movl	4(%esi,%eax,8), %edi
-	xorl	%eax, %eax
-	xorl	%edi, %edx
-	movl	-956(%ebp), %edi
-	addl	%ecx, %edi
-	cmpl	%ecx, %edi
-	movl	-960(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%edi, %eax
-	addl	%ecx, %edx
-	leal	0(,%edi,8), %ecx
-	movl	%edx, -996(%ebp)
-	addl	%ecx, %edi
-	shrl	$29, %eax
-	movl	%edi, -1000(%ebp)
-	sall	$3, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %edi
-	movl	-996(%ebp), %ecx
-	setb	%al
-	movl	-852(%ebp), %edi
-	addl	%eax, %edx
-	addl	%ecx, %edx
-	movl	-844(%ebp), %eax
-	movl	-1008(%ebp), %ecx
-	movl	%edx, -1020(%ebp)
-	movl	-1004(%ebp), %edx
-	xorl	%edi, %ecx
-	movl	%ecx, -1016(%ebp)
-	xorl	%eax, %edx
-	movl	%edx, %eax
-	movl	%edx, -1012(%ebp)
-	shrl	$13, %eax
-	movzbl	%dl,%edi
-	andl	$2040, %eax
-	movl	(%esi,%edi,8), %ecx
-	movl	2048(%eax,%esi), %edx
-	movl	2052(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	4(%esi,%edi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-1016(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	4100(%eax,%esi), %edi
-	movl	-1016(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	6148(%eax,%esi), %edi
-	xorl	%eax, %eax
-	movl	-1000(%ebp), %esi
-	xorl	%edi, %edx
-	cmpl	%ecx, -1000(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -1032(%ebp)
-	movl	-1020(%ebp), %edi
-	addl	%eax, %edx
-	movl	tiger_table@GOT(%ebx), %ecx
-	subl	%edx, %edi
-	movl	-1012(%ebp), %edx
-	movl	%edi, -1036(%ebp)
-	movl	%edx, %eax
-	movzbl	%dh, %esi
-	shrl	$24, %eax
-	leal	0(,%esi,8), %edi
-	movl	%ecx, %esi
-	sall	$3, %eax
-	movl	4096(%eax,%ecx), %edx
-	movl	6144(%edi,%ecx), %ecx
-	movl	4100(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%edi,%esi), %edx
-	xorl	%eax, %edx
-	movl	-1016(%ebp), %eax
-	movzbl	%ah, %edi
-	leal	0(,%edi,8), %eax
-	movl	2048(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	2052(%eax,%esi), %edi
-	movl	-1016(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$24, %eax
-	movl	(%esi,%eax,8), %edi
-	xorl	%edi, %ecx
-	movl	4(%esi,%eax,8), %edi
-	xorl	%eax, %eax
-	movl	%esi, -148(%ebp)
-	xorl	%edi, %edx
-	movl	-984(%ebp), %edi
-	addl	%ecx, %edi
-	cmpl	%ecx, %edi
-	movl	-988(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	addl	%ecx, %edx
-	movl	%edx, -1024(%ebp)
-	leal	0(,%edi,8), %ecx
-	movl	%edi, %eax
-	shrl	$29, %eax
-	addl	%ecx, %edi
-	sall	$3, %edx
-	movl	%edi, -1028(%ebp)
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %edi
-	movl	-1024(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	-848(%ebp), %eax
-	movl	-856(%ebp), %edi
-	addl	%ecx, %edx
-	movl	-1036(%ebp), %ecx
-	movl	%edx, -1048(%ebp)
-	movl	-1032(%ebp), %edx
-	xorl	%edi, %ecx
-	movl	%ecx, -1044(%ebp)
-	xorl	%eax, %edx
-	movl	%edx, %eax
-	movl	%edx, -1040(%ebp)
-	shrl	$13, %eax
-	movzbl	%dl,%edi
-	andl	$2040, %eax
-	movl	(%esi,%edi,8), %ecx
-	movl	2048(%eax,%esi), %edx
-	movl	2052(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	4(%esi,%edi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-1044(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	4100(%eax,%esi), %edi
-	movl	-1044(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	6148(%eax,%esi), %edi
-	xorl	%eax, %eax
-	movl	-1028(%ebp), %esi
-	xorl	%edi, %edx
-	cmpl	%ecx, -1028(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -1060(%ebp)
-	movl	-1048(%ebp), %edi
-	addl	%eax, %edx
-	movl	tiger_table@GOT(%ebx), %ecx
-	subl	%edx, %edi
-	movl	-1040(%ebp), %edx
-	movl	%edi, -1064(%ebp)
-	movl	%edx, %eax
-	movzbl	%dh, %esi
-	shrl	$24, %eax
-	leal	0(,%esi,8), %edi
-	movl	%ecx, %esi
-	sall	$3, %eax
-	movl	4096(%eax,%ecx), %edx
-	movl	6144(%edi,%ecx), %ecx
-	movl	4100(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%edi,%esi), %edx
-	xorl	%eax, %edx
-	movl	-1044(%ebp), %eax
-	movzbl	%ah, %edi
-	leal	0(,%edi,8), %eax
-	movl	2048(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	2052(%eax,%esi), %edi
-	movl	-1044(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$24, %eax
-	movl	(%esi,%eax,8), %edi
-	xorl	%edi, %ecx
-	movl	4(%esi,%eax,8), %edi
-	xorl	%eax, %eax
-	xorl	%edi, %edx
-	movl	-1012(%ebp), %edi
-	addl	%ecx, %edi
-	cmpl	%ecx, %edi
-	movl	-1016(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%edi, %eax
-	addl	%ecx, %edx
-	leal	0(,%edi,8), %ecx
-	movl	%edx, -1052(%ebp)
-	addl	%ecx, %edi
-	shrl	$29, %eax
-	movl	%edi, -1056(%ebp)
-	sall	$3, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %edi
-	movl	-1052(%ebp), %ecx
-	setb	%al
-	movl	-888(%ebp), %edi
-	addl	%eax, %edx
-	addl	%ecx, %edx
-	movl	-884(%ebp), %eax
-	movl	-1064(%ebp), %ecx
-	movl	%edx, -1076(%ebp)
-	movl	-1060(%ebp), %edx
-	xorl	%edi, %ecx
-	movl	%ecx, -1072(%ebp)
-	xorl	%eax, %edx
-	movl	%edx, %eax
-	movl	%edx, -1068(%ebp)
-	shrl	$13, %eax
-	movzbl	%dl,%edi
-	andl	$2040, %eax
-	movl	(%esi,%edi,8), %ecx
-	movl	2048(%eax,%esi), %edx
-	movl	2052(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	4(%esi,%edi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-1072(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	4100(%eax,%esi), %edi
-	movl	-1072(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	6148(%eax,%esi), %edi
-	xorl	%eax, %eax
-	movl	-1056(%ebp), %esi
-	xorl	%edi, %edx
-	cmpl	%ecx, -1056(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -1080(%ebp)
-	movl	-1076(%ebp), %edi
-	addl	%eax, %edx
-	movl	tiger_table@GOT(%ebx), %ecx
-	subl	%edx, %edi
-	movl	-1068(%ebp), %edx
-	movl	%edi, -1084(%ebp)
-	movl	%edx, %eax
-	movzbl	%dh, %esi
-	shrl	$24, %eax
-	leal	0(,%esi,8), %edi
-	movl	%ecx, %esi
-	sall	$3, %eax
-	movl	4096(%eax,%ecx), %edx
-	movl	6144(%edi,%ecx), %ecx
-	movl	4100(%eax,%esi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%edi,%esi), %edx
-	xorl	%eax, %edx
-	movl	-1072(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	-148(%ebp), %esi
-	movl	2048(%eax,%esi), %edi
-	xorl	%edi, %ecx
-	movl	2052(%eax,%esi), %edi
-	movl	-1072(%ebp), %eax
-	xorl	%edi, %edx
-	shrl	$24, %eax
-	movl	(%esi,%eax,8), %edi
-	xorl	%edi, %ecx
-	movl	4(%esi,%eax,8), %edi
-	movl	-1040(%ebp), %esi
-	xorl	%eax, %eax
-	xorl	%edi, %edx
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-1044(%ebp), %edi
-	setb	%al
-	addl	%eax, %edx
-	leal	0(,%esi,8), %ecx
-	addl	%edx, %edi
-	movl	%esi, %eax
-	leal	0(,%edi,8), %edx
-	addl	%ecx, %esi
-	shrl	$29, %eax
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %esi
-	setb	%al
-	addl	%eax, %edx
-	leal	(%edi,%edx), %ecx
-	movl	-1088(%ebp), %edi
-	movl	-1092(%ebp), %edx
-	xorl	%esi, %edi
-	movl	-16(%ebp), %esi
-	xorl	%edx, %ecx
-	cmpl	%esi, -1068(%ebp)
-	movl	%ecx, -1108(%ebp)
-	movl	-16(%ebp), %edx
-	movl	-1068(%ebp), %esi
-	movl	-1100(%ebp), %ecx
-	setb	%al
-	subl	%edx, %esi
-	movl	-1072(%ebp), %edx
-	movzbl	%al, %eax
-	addl	%ecx, %eax
-	movl	-1080(%ebp), %ecx
-	subl	%eax, %edx
-	movl	-20(%ebp), %eax
-	addl	%eax, %ecx
-	xorl	%eax, %eax
-	cmpl	-20(%ebp), %ecx
-	setb	%al
-	addl	-1104(%ebp), %eax
-	addl	-1084(%ebp), %eax
-	movl	%eax, -1152(%ebp)
-	movl	12(%ebp), %eax
-	movl	%edi, (%eax)
-	movl	-1108(%ebp), %edi
-	movl	%esi, 8(%eax)
-	movl	%edx, 12(%eax)
-	movl	%edi, 4(%eax)
-	movl	%ecx, 16(%eax)
-	movl	-1152(%ebp), %edx
-	movl	%edx, 20(%eax)
-	addl	$1140, %esp
-	popl	%ebx
-	popl	%esi
-	popl	%edi
-	popl	%ebp
-	ret
-	.size	tiger_compress, .-tiger_compress
-.globl tiger_t
-	.type	tiger_t, @function
-tiger_t:
-	pushl	%ebp
-	movl	%esp, %ebp
-	pushl	%edi
-	pushl	%esi
-	pushl	%ebx
-	subl	$1032, %esp
-	movl	12(%ebp), %eax
-	call	__i686.get_pc_thunk.bx
-	addl	$_GLOBAL_OFFSET_TABLE_, %ebx
-	movl	%eax, -16(%ebp)
-	cmpl	$63, %eax
-	jbe	.L8
-	movl	tiger_table@GOT(%ebx), %edi
-.L6:
-	movl	16(%ebp), %esi
-	movl	16(%ebp), %edx
-	movl	(%esi), %ecx
-	movl	16(%ebp), %esi
-	movl	%ecx, -948(%ebp)
-	movl	4(%edx), %eax
-	movl	16(%ebp), %edx
-	movl	%eax, -952(%ebp)
-	movl	8(%esi), %ecx
-	movl	16(%ebp), %esi
-	movl	%ecx, -956(%ebp)
-	movl	12(%edx), %eax
-	movl	-956(%ebp), %edx
-	movl	%eax, -960(%ebp)
-	movl	16(%esi), %eax
-	movl	20(%esi), %ecx
-	movl	%edx, -20(%ebp)
-	movl	8(%ebp), %esi
-	movl	8(%ebp), %edx
-	movl	%eax, -24(%ebp)
-	movl	%ecx, -964(%ebp)
-	movl	(%esi), %ecx
-	movl	%ecx, -968(%ebp)
-	movl	4(%edx), %esi
-	movl	8(%ebp), %ecx
-	movl	%esi, -972(%ebp)
-	movl	8(%ecx), %edx
-	movl	8(%ebp), %esi
-	movl	%edx, -976(%ebp)
-	movl	12(%esi), %ecx
-	movl	8(%ebp), %edx
-	movl	%ecx, -980(%ebp)
-	movl	16(%edx), %esi
-	movl	%esi, -984(%ebp)
-	movl	8(%ebp), %ecx
-	movl	8(%ebp), %esi
-	movl	20(%ecx), %edx
-	movl	%edx, -988(%ebp)
-	movl	24(%esi), %ecx
-	movl	8(%ebp), %edx
-	movl	%ecx, -992(%ebp)
-	movl	28(%edx), %esi
-	movl	8(%ebp), %ecx
-	movl	%esi, -996(%ebp)
-	movl	32(%ecx), %edx
-	movl	8(%ebp), %esi
-	movl	%edx, -1000(%ebp)
-	movl	36(%esi), %ecx
-	movl	8(%ebp), %edx
-	movl	%ecx, -1004(%ebp)
-	movl	40(%edx), %esi
-	movl	8(%ebp), %ecx
-	movl	%esi, -1008(%ebp)
-	movl	44(%ecx), %edx
-	movl	8(%ebp), %esi
-	movl	%edx, -1012(%ebp)
-	movl	48(%esi), %ecx
-	movl	8(%ebp), %edx
-	movl	%ecx, -1016(%ebp)
-	movl	52(%edx), %esi
-	movl	8(%ebp), %ecx
-	movl	%esi, -1020(%ebp)
-	movl	56(%ecx), %edx
-	movl	8(%ebp), %esi
-	movl	%edx, -1024(%ebp)
-	movl	-968(%ebp), %edx
-	movl	60(%esi), %ecx
-	xorl	%edx, %eax
-	movl	%ecx, -1028(%ebp)
-	movl	%eax, -28(%ebp)
-	movl	-972(%ebp), %esi
-	movl	-964(%ebp), %ecx
-	movl	-28(%ebp), %eax
-	xorl	%esi, %ecx
-	shrl	$13, %eax
-	movzbl	-28(%ebp),%esi
-	movl	%ecx, -32(%ebp)
-	andl	$2040, %eax
-	movl	2048(%eax,%edi), %edx
-	movl	2052(%eax,%edi), %eax
-	movl	(%edi,%esi,8), %ecx
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-32(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-32(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-948(%ebp), %esi
-	cmpl	%ecx, -948(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -44(%ebp)
-	movl	-952(%ebp), %ecx
-	addl	%eax, %edx
-	subl	%edx, %ecx
-	movl	-28(%ebp), %edx
-	movl	%ecx, -48(%ebp)
-	movzbl	%dh, %eax
-	leal	0(,%eax,8), %ecx
-	movl	%edx, %eax
-	movl	6144(%ecx,%edi), %esi
-	shrl	$24, %eax
-	sall	$3, %eax
-	movl	6148(%ecx,%edi), %ecx
-	movl	4096(%eax,%edi), %edx
-	xorl	%esi, %edx
-	movl	4100(%eax,%edi), %esi
-	movl	-32(%ebp), %eax
-	xorl	%esi, %ecx
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %edx
-	movl	2052(%eax,%edi), %esi
-	movl	-32(%ebp), %eax
-	xorl	%esi, %ecx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %edx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %ecx
-	movl	-956(%ebp), %esi
-	addl	%edx, %esi
-	cmpl	%edx, %esi
-	movl	-960(%ebp), %edx
-	setb	%al
-	leal	(%ecx,%eax), %eax
-	addl	%edx, %eax
-	movl	%eax, -36(%ebp)
-	leal	0(,%esi,4), %ecx
-	movl	%eax, %edx
-	movl	%esi, %eax
-	sall	$2, %edx
-	addl	%ecx, %esi
-	movl	%esi, -40(%ebp)
-	shrl	$30, %eax
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %esi
-	setb	%al
-	addl	%eax, %edx
-	movl	-36(%ebp), %eax
-	xorl	%ecx, %ecx
-	movl	-968(%ebp), %esi
-	addl	%eax, %edx
-	movl	-1028(%ebp), %eax
-	movl	%edx, -60(%ebp)
-	movl	-1024(%ebp), %edx
-	xorl	$-1515870811, %edx
-	cmpl	%edx, -968(%ebp)
-	setb	%cl
-	subl	%edx, %esi
-	movl	%esi, -72(%ebp)
-	movl	-972(%ebp), %esi
-	xorl	$-1515870811, %eax
-	addl	%ecx, %eax
-	movl	-976(%ebp), %edx
-	movl	-980(%ebp), %ecx
-	subl	%eax, %esi
-	movl	-44(%ebp), %eax
-	movl	%esi, -76(%ebp)
-	movl	-48(%ebp), %esi
-	xorl	%edx, %eax
-	movl	%eax, -52(%ebp)
-	xorl	%ecx, %esi
-	movl	%esi, -56(%ebp)
-	movzbl	%al,%esi
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	(%edi,%esi,8), %ecx
-	movl	2048(%eax,%edi), %edx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-56(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-56(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-40(%ebp), %esi
-	cmpl	%ecx, -40(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -80(%ebp)
-	movl	-52(%ebp), %ecx
-	addl	%eax, %edx
-	movl	-60(%ebp), %esi
-	movzbl	%ch, %eax
-	subl	%edx, %esi
-	movl	%esi, -84(%ebp)
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	movl	6144(%esi,%edi), %ecx
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-56(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-56(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-28(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-32(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -64(%ebp)
-	leal	0(,%esi,4), %ecx
-	addl	%ecx, %esi
-	movl	%esi, -68(%ebp)
-	shrl	$30, %eax
-	sall	$2, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %esi
-	movl	-64(%ebp), %esi
-	setb	%al
-	addl	%eax, %edx
-	movl	-976(%ebp), %ecx
-	addl	%esi, %edx
-	movl	-76(%ebp), %eax
-	movl	%edx, -96(%ebp)
-	movl	-980(%ebp), %esi
-	movl	-72(%ebp), %edx
-	xorl	%eax, %esi
-	xorl	%edx, %ecx
-	movl	%esi, -116(%ebp)
-	movl	-984(%ebp), %edx
-	movl	%ecx, -108(%ebp)
-	movl	-80(%ebp), %eax
-	movl	-988(%ebp), %ecx
-	movl	-84(%ebp), %esi
-	xorl	%edx, %eax
-	movl	%eax, -88(%ebp)
-	xorl	%ecx, %esi
-	movl	%esi, -92(%ebp)
-	movzbl	%al,%esi
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	(%edi,%esi,8), %ecx
-	movl	2048(%eax,%edi), %edx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-92(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-92(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-68(%ebp), %esi
-	cmpl	%ecx, -68(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -120(%ebp)
-	movl	-88(%ebp), %ecx
-	addl	%eax, %edx
-	movl	-96(%ebp), %esi
-	movzbl	%ch, %eax
-	subl	%edx, %esi
-	movl	%esi, -124(%ebp)
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	movl	6144(%esi,%edi), %ecx
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-92(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-92(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-52(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-56(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -100(%ebp)
-	leal	0(,%esi,4), %ecx
-	addl	%ecx, %esi
-	movl	%esi, -104(%ebp)
-	shrl	$30, %eax
-	sall	$2, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %esi
-	movl	-100(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	-108(%ebp), %eax
-	addl	%ecx, %edx
-	movl	-108(%ebp), %esi
-	movl	%edx, -136(%ebp)
-	movl	-984(%ebp), %edx
-	movl	-116(%ebp), %ecx
-	addl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%esi, %edx
-	movl	%edx, -112(%ebp)
-	movl	-988(%ebp), %esi
-	setb	%al
-	addl	%ecx, %eax
-	movl	-992(%ebp), %edx
-	movl	-996(%ebp), %ecx
-	addl	%esi, %eax
-	movl	-124(%ebp), %esi
-	movl	%eax, -148(%ebp)
-	movl	-120(%ebp), %eax
-	xorl	%ecx, %esi
-	movl	%esi, -132(%ebp)
-	xorl	%edx, %eax
-	movzbl	%al,%esi
-	movl	%eax, -128(%ebp)
-	shrl	$13, %eax
-	movl	(%edi,%esi,8), %ecx
-	andl	$2040, %eax
-	movl	2048(%eax,%edi), %edx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-132(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-132(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-104(%ebp), %esi
-	cmpl	%ecx, -104(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -152(%ebp)
-	movl	-128(%ebp), %ecx
-	addl	%eax, %edx
-	movl	-136(%ebp), %esi
-	movzbl	%ch, %eax
-	subl	%edx, %esi
-	movl	%esi, -156(%ebp)
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	movl	6144(%esi,%edi), %ecx
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-132(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-132(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-88(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-92(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -140(%ebp)
-	leal	0(,%esi,4), %ecx
-	addl	%ecx, %esi
-	movl	%esi, -144(%ebp)
-	shrl	$30, %eax
-	sall	$2, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %esi
-	setb	%al
-	movl	-112(%ebp), %esi
-	addl	%eax, %edx
-	movl	-140(%ebp), %eax
-	addl	%eax, %edx
-	movl	-108(%ebp), %eax
-	movl	%edx, -168(%ebp)
-	movl	-992(%ebp), %edx
-	notl	%eax
-	sall	$19, %eax
-	xorl	%esi, %eax
-	cmpl	%eax, -992(%ebp)
-	setb	%cl
-	movzbl	%cl, %esi
-	subl	%eax, %edx
-	movl	-108(%ebp), %ecx
-	movl	-116(%ebp), %eax
-	movl	%edx, -180(%ebp)
-	movl	-148(%ebp), %edx
-	shrl	$13, %ecx
-	sall	$19, %eax
-	orl	%ecx, %eax
-	notl	%edx
-	xorl	%eax, %edx
-	movl	-996(%ebp), %eax
-	addl	%esi, %edx
-	movl	-1004(%ebp), %ecx
-	subl	%edx, %eax
-	movl	-156(%ebp), %esi
-	movl	%eax, -184(%ebp)
-	movl	-1000(%ebp), %edx
-	movl	-152(%ebp), %eax
-	xorl	%ecx, %esi
-	movl	%esi, -164(%ebp)
-	xorl	%edx, %eax
-	movzbl	%al,%esi
-	movl	%eax, -160(%ebp)
-	shrl	$13, %eax
-	movl	(%edi,%esi,8), %ecx
-	andl	$2040, %eax
-	movl	2048(%eax,%edi), %edx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-164(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-164(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-144(%ebp), %esi
-	cmpl	%ecx, -144(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -188(%ebp)
-	movl	-168(%ebp), %esi
-	addl	%eax, %edx
-	subl	%edx, %esi
-	movl	%esi, -192(%ebp)
-	movl	-160(%ebp), %ecx
-	movzbl	%ch, %eax
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	sall	$3, %eax
-	movl	6144(%esi,%edi), %ecx
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-164(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-164(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-128(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-132(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	leal	0(,%esi,4), %ecx
-	movl	%edx, -172(%ebp)
-	addl	%ecx, %esi
-	shrl	$30, %eax
-	movl	%esi, -176(%ebp)
-	sall	$2, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %esi
-	movl	-172(%ebp), %esi
-	setb	%al
-	movl	-1000(%ebp), %ecx
-	addl	%eax, %edx
-	addl	%esi, %edx
-	movl	-184(%ebp), %eax
-	movl	-1004(%ebp), %esi
-	movl	%edx, -204(%ebp)
-	movl	-180(%ebp), %edx
-	xorl	%eax, %esi
-	movl	%esi, -224(%ebp)
-	movl	-188(%ebp), %eax
-	xorl	%edx, %ecx
-	movl	%ecx, -216(%ebp)
-	movl	-1008(%ebp), %edx
-	movl	-1012(%ebp), %ecx
-	movl	-192(%ebp), %esi
-	xorl	%edx, %eax
-	movl	%eax, -196(%ebp)
-	xorl	%ecx, %esi
-	movl	%esi, -200(%ebp)
-	movzbl	%al,%esi
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	(%edi,%esi,8), %ecx
-	movl	2048(%eax,%edi), %edx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-200(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-200(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-176(%ebp), %esi
-	cmpl	%ecx, -176(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -228(%ebp)
-	movl	-196(%ebp), %ecx
-	movl	-204(%ebp), %esi
-	addl	%eax, %edx
-	movzbl	%ch, %eax
-	subl	%edx, %esi
-	movl	%esi, -232(%ebp)
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	movl	6144(%esi,%edi), %ecx
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-200(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-200(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-160(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-164(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -208(%ebp)
-	leal	0(,%esi,4), %ecx
-	addl	%ecx, %esi
-	movl	%esi, -212(%ebp)
-	shrl	$30, %eax
-	sall	$2, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %esi
-	movl	-208(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	-216(%ebp), %eax
-	addl	%ecx, %edx
-	movl	-216(%ebp), %esi
-	movl	%edx, -244(%ebp)
-	movl	-1008(%ebp), %edx
-	movl	-224(%ebp), %ecx
-	addl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%esi, %edx
-	movl	%edx, -220(%ebp)
-	movl	-1012(%ebp), %esi
-	setb	%al
-	addl	%ecx, %eax
-	movl	-1016(%ebp), %edx
-	movl	-1020(%ebp), %ecx
-	addl	%esi, %eax
-	movl	-232(%ebp), %esi
-	movl	%eax, -256(%ebp)
-	movl	-228(%ebp), %eax
-	xorl	%ecx, %esi
-	movl	%esi, -240(%ebp)
-	xorl	%edx, %eax
-	movzbl	%al,%esi
-	movl	%eax, -236(%ebp)
-	shrl	$13, %eax
-	movl	(%edi,%esi,8), %ecx
-	andl	$2040, %eax
-	movl	2048(%eax,%edi), %edx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-240(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-240(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-212(%ebp), %esi
-	cmpl	%ecx, -212(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -260(%ebp)
-	movl	-236(%ebp), %ecx
-	movl	-244(%ebp), %esi
-	addl	%eax, %edx
-	movzbl	%ch, %eax
-	subl	%edx, %esi
-	movl	%esi, -264(%ebp)
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	movl	6144(%esi,%edi), %ecx
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-240(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-240(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-196(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-200(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -248(%ebp)
-	leal	0(,%esi,4), %ecx
-	addl	%ecx, %esi
-	movl	%esi, -252(%ebp)
-	shrl	$30, %eax
-	sall	$2, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %esi
-	movl	-248(%ebp), %esi
-	setb	%al
-	addl	%eax, %edx
-	movl	-216(%ebp), %eax
-	addl	%esi, %edx
-	movl	-220(%ebp), %ecx
-	movl	%edx, -276(%ebp)
-	movl	-224(%ebp), %edx
-	shrl	$23, %eax
-	notl	%ecx
-	movl	-1016(%ebp), %esi
-	sall	$9, %edx
-	orl	%edx, %eax
-	xorl	%eax, %ecx
-	xorl	%edx, %edx
-	cmpl	%ecx, -1016(%ebp)
-	movl	-224(%ebp), %eax
-	setb	%dl
-	subl	%ecx, %esi
-	movl	-256(%ebp), %ecx
-	notl	%eax
-	movl	%esi, -288(%ebp)
-	shrl	$23, %eax
-	movl	-1020(%ebp), %esi
-	xorl	%ecx, %eax
-	addl	%edx, %eax
-	movl	-1028(%ebp), %ecx
-	subl	%eax, %esi
-	movl	-1024(%ebp), %edx
-	movl	-260(%ebp), %eax
-	movl	%esi, -292(%ebp)
-	movl	-264(%ebp), %esi
-	xorl	%edx, %eax
-	movl	%eax, -268(%ebp)
-	xorl	%ecx, %esi
-	movl	%esi, -272(%ebp)
-	movzbl	%al,%esi
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	(%edi,%esi,8), %ecx
-	movl	2048(%eax,%edi), %edx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-272(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-272(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-252(%ebp), %esi
-	cmpl	%ecx, -252(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -360(%ebp)
-	movl	-268(%ebp), %ecx
-	addl	%eax, %edx
-	movl	-276(%ebp), %esi
-	movzbl	%ch, %eax
-	subl	%edx, %esi
-	movl	%esi, -364(%ebp)
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	movl	6144(%esi,%edi), %ecx
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-272(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-272(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-236(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-240(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -280(%ebp)
-	leal	0(,%esi,4), %ecx
-	addl	%ecx, %esi
-	movl	%esi, -284(%ebp)
-	shrl	$30, %eax
-	sall	$2, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %esi
-	movl	-280(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	-288(%ebp), %esi
-	addl	%ecx, %edx
-	movl	%edx, -376(%ebp)
-	movl	-1024(%ebp), %edx
-	xorl	%esi, %edx
-	movl	%edx, -296(%ebp)
-	movl	-72(%ebp), %ecx
-	movl	-292(%ebp), %eax
-	movl	-1028(%ebp), %esi
-	addl	%edx, %ecx
-	movl	%ecx, -300(%ebp)
-	xorl	%eax, %esi
-	cmpl	%edx, %ecx
-	movl	%esi, -304(%ebp)
-	setb	%al
-	movl	-76(%ebp), %ecx
-	movzbl	%al, %eax
-	addl	%esi, %eax
-	addl	%ecx, %eax
-	movl	%eax, -308(%ebp)
-	movl	-300(%ebp), %esi
-	movl	%edx, %eax
-	notl	%eax
-	movl	-108(%ebp), %edx
-	sall	$19, %eax
-	xorl	%esi, %eax
-	cmpl	%eax, -108(%ebp)
-	setb	%cl
-	movzbl	%cl, %esi
-	subl	%eax, %edx
-	movl	-296(%ebp), %ecx
-	movl	-304(%ebp), %eax
-	movl	%edx, -312(%ebp)
-	movl	-308(%ebp), %edx
-	shrl	$13, %ecx
-	sall	$19, %eax
-	orl	%ecx, %eax
-	notl	%edx
-	xorl	%eax, %edx
-	movl	-112(%ebp), %ecx
-	addl	%esi, %edx
-	movl	-116(%ebp), %esi
-	subl	%edx, %esi
-	movl	-312(%ebp), %edx
-	movl	%esi, -316(%ebp)
-	xorl	%edx, %ecx
-	movl	-148(%ebp), %edx
-	movl	%ecx, -320(%ebp)
-	xorl	%esi, %edx
-	movl	-180(%ebp), %esi
-	movl	%edx, -328(%ebp)
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	%esi, -324(%ebp)
-	setb	%al
-	movl	-184(%ebp), %ecx
-	movzbl	%al, %eax
-	addl	%edx, %eax
-	addl	%ecx, %eax
-	movl	%eax, -332(%ebp)
-	movl	-320(%ebp), %eax
-	sall	$9, %edx
-	movl	%esi, %ecx
-	notl	%ecx
-	movl	-216(%ebp), %esi
-	shrl	$23, %eax
-	orl	%edx, %eax
-	xorl	%eax, %ecx
-	xorl	%edx, %edx
-	cmpl	%ecx, -216(%ebp)
-	movl	-328(%ebp), %eax
-	setb	%dl
-	subl	%ecx, %esi
-	movl	-332(%ebp), %ecx
-	notl	%eax
-	movl	%esi, -336(%ebp)
-	shrl	$23, %eax
-	xorl	%ecx, %eax
-	addl	%edx, %eax
-	movl	-336(%ebp), %esi
-	movl	-224(%ebp), %edx
-	movl	-220(%ebp), %ecx
-	subl	%eax, %edx
-	xorl	%esi, %ecx
-	movl	%edx, -340(%ebp)
-	movl	%ecx, -344(%ebp)
-	movl	-256(%ebp), %eax
-	xorl	%edx, %eax
-	movl	%eax, -352(%ebp)
-	movl	-288(%ebp), %edx
-	movl	-352(%ebp), %esi
-	xorl	%eax, %eax
-	addl	%ecx, %edx
-	cmpl	%ecx, %edx
-	movl	%edx, -348(%ebp)
-	movl	-292(%ebp), %ecx
-	setb	%al
-	addl	%esi, %eax
-	xorl	$-1985229329, %edx
-	movl	-296(%ebp), %esi
-	addl	%ecx, %eax
-	xorl	%ecx, %ecx
-	cmpl	%edx, -296(%ebp)
-	movl	%eax, -356(%ebp)
-	movl	-356(%ebp), %eax
-	setb	%cl
-	subl	%edx, %esi
-	xorl	$19088743, %eax
-	addl	%ecx, %eax
-	movl	%esi, -392(%ebp)
-	movl	-304(%ebp), %esi
-	movl	-300(%ebp), %edx
-	movl	-308(%ebp), %ecx
-	subl	%eax, %esi
-	movl	-360(%ebp), %eax
-	movl	%esi, -396(%ebp)
-	movl	-364(%ebp), %esi
-	xorl	%edx, %eax
-	movl	%eax, -368(%ebp)
-	xorl	%ecx, %esi
-	movl	%esi, -372(%ebp)
-	movzbl	%al,%esi
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	(%edi,%esi,8), %ecx
-	movl	2048(%eax,%edi), %edx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-372(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-372(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-284(%ebp), %esi
-	cmpl	%ecx, -284(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -400(%ebp)
-	movl	-376(%ebp), %esi
-	addl	%eax, %edx
-	subl	%edx, %esi
-	movl	%esi, -404(%ebp)
-	movl	-368(%ebp), %ecx
-	movzbl	%ch, %eax
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	sall	$3, %eax
-	movl	6144(%esi,%edi), %ecx
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-372(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-372(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-268(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-272(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	shrl	$29, %eax
-	movl	%edx, -380(%ebp)
-	movl	-380(%ebp), %ecx
-	leal	0(,%esi,8), %edx
-	sall	$3, %ecx
-	orl	%eax, %ecx
-	xorl	%eax, %eax
-	cmpl	%esi, %edx
-	setb	%al
-	subl	%esi, %edx
-	movl	%edx, -384(%ebp)
-	movl	-380(%ebp), %esi
-	movl	-392(%ebp), %edx
-	addl	%esi, %eax
-	subl	%eax, %ecx
-	xorl	$-1515870811, %edx
-	movl	%ecx, -388(%ebp)
-	xorl	%ecx, %ecx
-	cmpl	%edx, -300(%ebp)
-	movl	-300(%ebp), %esi
-	movl	-396(%ebp), %eax
-	setb	%cl
-	subl	%edx, %esi
-	xorl	$-1515870811, %eax
-	movl	%esi, -428(%ebp)
-	movl	-308(%ebp), %esi
-	addl	%ecx, %eax
-	movl	-312(%ebp), %edx
-	movl	-316(%ebp), %ecx
-	subl	%eax, %esi
-	movl	%esi, -432(%ebp)
-	movl	-400(%ebp), %eax
-	movl	-404(%ebp), %esi
-	xorl	%edx, %eax
-	xorl	%ecx, %esi
-	movl	%esi, -412(%ebp)
-	movzbl	%al,%esi
-	movl	%eax, -408(%ebp)
-	shrl	$13, %eax
-	movl	(%edi,%esi,8), %ecx
-	andl	$2040, %eax
-	movl	2048(%eax,%edi), %edx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-412(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-412(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-384(%ebp), %esi
-	cmpl	%ecx, -384(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -436(%ebp)
-	movl	-408(%ebp), %ecx
-	movl	-388(%ebp), %esi
-	addl	%eax, %edx
-	movzbl	%ch, %eax
-	subl	%edx, %esi
-	movl	%esi, -440(%ebp)
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	movl	6144(%esi,%edi), %ecx
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-412(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-412(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-368(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-372(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -416(%ebp)
-	shrl	$29, %eax
-	movl	-416(%ebp), %ecx
-	leal	0(,%esi,8), %edx
-	sall	$3, %ecx
-	orl	%eax, %ecx
-	xorl	%eax, %eax
-	cmpl	%esi, %edx
-	setb	%al
-	subl	%esi, %edx
-	movl	-416(%ebp), %esi
-	movl	%edx, -420(%ebp)
-	movl	-428(%ebp), %edx
-	addl	%esi, %eax
-	subl	%eax, %ecx
-	movl	-316(%ebp), %esi
-	movl	-432(%ebp), %eax
-	movl	%ecx, -424(%ebp)
-	movl	-312(%ebp), %ecx
-	xorl	%eax, %esi
-	movl	%esi, -472(%ebp)
-	movl	-436(%ebp), %eax
-	xorl	%edx, %ecx
-	movl	%ecx, -464(%ebp)
-	movl	-320(%ebp), %edx
-	movl	-328(%ebp), %ecx
-	movl	-440(%ebp), %esi
-	xorl	%edx, %eax
-	movl	%eax, -444(%ebp)
-	xorl	%ecx, %esi
-	movl	%esi, -448(%ebp)
-	movzbl	%al,%esi
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	(%edi,%esi,8), %ecx
-	movl	2048(%eax,%edi), %edx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-448(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-448(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-420(%ebp), %esi
-	cmpl	%ecx, -420(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -476(%ebp)
-	movl	-444(%ebp), %ecx
-	movl	-424(%ebp), %esi
-	addl	%eax, %edx
-	movzbl	%ch, %eax
-	subl	%edx, %esi
-	movl	%esi, -480(%ebp)
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	movl	6144(%esi,%edi), %ecx
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-448(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-448(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-408(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-412(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -452(%ebp)
-	shrl	$29, %eax
-	movl	-452(%ebp), %ecx
-	leal	0(,%esi,8), %edx
-	sall	$3, %ecx
-	orl	%eax, %ecx
-	xorl	%eax, %eax
-	cmpl	%esi, %edx
-	setb	%al
-	subl	%esi, %edx
-	movl	-452(%ebp), %esi
-	movl	%edx, -456(%ebp)
-	movl	-320(%ebp), %edx
-	addl	%esi, %eax
-	subl	%eax, %ecx
-	movl	-464(%ebp), %eax
-	movl	-464(%ebp), %esi
-	movl	%ecx, -460(%ebp)
-	addl	%eax, %edx
-	movl	-472(%ebp), %ecx
-	movl	%edx, -468(%ebp)
-	xorl	%eax, %eax
-	cmpl	%esi, %edx
-	movl	-328(%ebp), %esi
-	setb	%al
-	addl	%ecx, %eax
-	movl	-324(%ebp), %edx
-	addl	%esi, %eax
-	movl	-332(%ebp), %ecx
-	movl	%eax, -504(%ebp)
-	movl	-480(%ebp), %esi
-	movl	-476(%ebp), %eax
-	xorl	%ecx, %esi
-	xorl	%edx, %eax
-	movl	%esi, -488(%ebp)
-	movzbl	%al,%esi
-	movl	%eax, -484(%ebp)
-	shrl	$13, %eax
-	movl	(%edi,%esi,8), %ecx
-	andl	$2040, %eax
-	movl	2048(%eax,%edi), %edx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-488(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-488(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-456(%ebp), %esi
-	cmpl	%ecx, -456(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -508(%ebp)
-	movl	-484(%ebp), %ecx
-	movl	-460(%ebp), %esi
-	addl	%eax, %edx
-	movzbl	%ch, %eax
-	subl	%edx, %esi
-	movl	%esi, -512(%ebp)
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	movl	6144(%esi,%edi), %ecx
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-488(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-488(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-444(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-448(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -492(%ebp)
-	shrl	$29, %eax
-	movl	-492(%ebp), %ecx
-	leal	0(,%esi,8), %edx
-	sall	$3, %ecx
-	orl	%eax, %ecx
-	xorl	%eax, %eax
-	cmpl	%esi, %edx
-	setb	%al
-	subl	%esi, %edx
-	movl	-468(%ebp), %esi
-	movl	%edx, -496(%ebp)
-	movl	-492(%ebp), %edx
-	addl	%edx, %eax
-	subl	%eax, %ecx
-	movl	-464(%ebp), %eax
-	movl	-324(%ebp), %edx
-	movl	%ecx, -500(%ebp)
-	notl	%eax
-	sall	$19, %eax
-	xorl	%esi, %eax
-	cmpl	%eax, -324(%ebp)
-	setb	%cl
-	movzbl	%cl, %esi
-	subl	%eax, %edx
-	movl	-464(%ebp), %ecx
-	movl	-472(%ebp), %eax
-	movl	%edx, -536(%ebp)
-	movl	-504(%ebp), %edx
-	shrl	$13, %ecx
-	sall	$19, %eax
-	orl	%ecx, %eax
-	notl	%edx
-	xorl	%eax, %edx
-	movl	-332(%ebp), %eax
-	addl	%esi, %edx
-	movl	-340(%ebp), %ecx
-	subl	%edx, %eax
-	movl	-512(%ebp), %esi
-	movl	%eax, -540(%ebp)
-	movl	-336(%ebp), %edx
-	movl	-508(%ebp), %eax
-	xorl	%ecx, %esi
-	xorl	%edx, %eax
-	movl	%eax, -516(%ebp)
-	movl	%esi, -520(%ebp)
-	movzbl	%al,%esi
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	(%edi,%esi,8), %ecx
-	movl	2048(%eax,%edi), %edx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-520(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-520(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-496(%ebp), %esi
-	cmpl	%ecx, -496(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -544(%ebp)
-	movl	-516(%ebp), %ecx
-	addl	%eax, %edx
-	movl	-500(%ebp), %esi
-	movzbl	%ch, %eax
-	subl	%edx, %esi
-	movl	%esi, -548(%ebp)
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	movl	6144(%esi,%edi), %ecx
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-520(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-520(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-484(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-488(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -524(%ebp)
-	shrl	$29, %eax
-	movl	-524(%ebp), %ecx
-	leal	0(,%esi,8), %edx
-	sall	$3, %ecx
-	orl	%eax, %ecx
-	xorl	%eax, %eax
-	cmpl	%esi, %edx
-	setb	%al
-	subl	%esi, %edx
-	movl	-524(%ebp), %esi
-	movl	%edx, -528(%ebp)
-	movl	-536(%ebp), %edx
-	addl	%esi, %eax
-	subl	%eax, %ecx
-	movl	%ecx, -532(%ebp)
-	movl	-336(%ebp), %ecx
-	xorl	%edx, %ecx
-	movl	%ecx, -572(%ebp)
-	movl	-540(%ebp), %eax
-	movl	-340(%ebp), %esi
-	movl	-344(%ebp), %edx
-	movl	-352(%ebp), %ecx
-	xorl	%eax, %esi
-	movl	%esi, -580(%ebp)
-	movl	-544(%ebp), %eax
-	movl	-548(%ebp), %esi
-	xorl	%edx, %eax
-	xorl	%ecx, %esi
-	movl	%esi, -556(%ebp)
-	movzbl	%al,%esi
-	movl	%eax, -552(%ebp)
-	shrl	$13, %eax
-	movl	(%edi,%esi,8), %ecx
-	andl	$2040, %eax
-	movl	2048(%eax,%edi), %edx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-556(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-556(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-528(%ebp), %esi
-	cmpl	%ecx, -528(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -584(%ebp)
-	movl	-552(%ebp), %ecx
-	addl	%eax, %edx
-	movl	-532(%ebp), %esi
-	movzbl	%ch, %eax
-	subl	%edx, %esi
-	movl	%esi, -588(%ebp)
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	movl	6144(%esi,%edi), %ecx
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-556(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-556(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-516(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-520(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -560(%ebp)
-	movl	-560(%ebp), %ecx
-	leal	0(,%esi,8), %edx
-	shrl	$29, %eax
-	sall	$3, %ecx
-	orl	%eax, %ecx
-	xorl	%eax, %eax
-	cmpl	%esi, %edx
-	setb	%al
-	subl	%esi, %edx
-	movl	-560(%ebp), %esi
-	movl	%edx, -564(%ebp)
-	movl	-344(%ebp), %edx
-	addl	%esi, %eax
-	subl	%eax, %ecx
-	movl	-572(%ebp), %eax
-	movl	-572(%ebp), %esi
-	movl	%ecx, -568(%ebp)
-	addl	%eax, %edx
-	movl	-580(%ebp), %ecx
-	movl	%edx, -576(%ebp)
-	xorl	%eax, %eax
-	cmpl	%esi, %edx
-	movl	-352(%ebp), %esi
-	setb	%al
-	addl	%ecx, %eax
-	movl	-348(%ebp), %edx
-	addl	%esi, %eax
-	movl	-356(%ebp), %ecx
-	movl	%eax, -612(%ebp)
-	movl	-588(%ebp), %esi
-	movl	-584(%ebp), %eax
-	xorl	%ecx, %esi
-	xorl	%edx, %eax
-	movl	%esi, -596(%ebp)
-	movzbl	%al,%esi
-	movl	%eax, -592(%ebp)
-	shrl	$13, %eax
-	movl	(%edi,%esi,8), %ecx
-	andl	$2040, %eax
-	movl	2048(%eax,%edi), %edx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-596(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-596(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-564(%ebp), %esi
-	cmpl	%ecx, -564(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -616(%ebp)
-	movl	-592(%ebp), %ecx
-	addl	%eax, %edx
-	movl	-568(%ebp), %esi
-	movzbl	%ch, %eax
-	subl	%edx, %esi
-	movl	%esi, -620(%ebp)
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	movl	6144(%esi,%edi), %ecx
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-596(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-596(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-552(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-556(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -600(%ebp)
-	movl	-600(%ebp), %ecx
-	leal	0(,%esi,8), %edx
-	shrl	$29, %eax
-	sall	$3, %ecx
-	orl	%eax, %ecx
-	xorl	%eax, %eax
-	cmpl	%esi, %edx
-	setb	%al
-	subl	%esi, %edx
-	movl	-600(%ebp), %esi
-	movl	%edx, -604(%ebp)
-	movl	-580(%ebp), %edx
-	addl	%esi, %eax
-	subl	%eax, %ecx
-	movl	-572(%ebp), %eax
-	sall	$9, %edx
-	movl	%ecx, -608(%ebp)
-	movl	-576(%ebp), %ecx
-	shrl	$23, %eax
-	orl	%edx, %eax
-	xorl	%edx, %edx
-	notl	%ecx
-	xorl	%eax, %ecx
-	movl	-348(%ebp), %esi
-	movl	-580(%ebp), %eax
-	cmpl	%ecx, -348(%ebp)
-	notl	%eax
-	setb	%dl
-	subl	%ecx, %esi
-	movl	-612(%ebp), %ecx
-	shrl	$23, %eax
-	movl	%esi, -644(%ebp)
-	movl	-356(%ebp), %esi
-	xorl	%ecx, %eax
-	addl	%edx, %eax
-	movl	-396(%ebp), %ecx
-	movl	-392(%ebp), %edx
-	subl	%eax, %esi
-	movl	-616(%ebp), %eax
-	movl	%esi, -648(%ebp)
-	movl	-620(%ebp), %esi
-	xorl	%edx, %eax
-	movl	%eax, -624(%ebp)
-	xorl	%ecx, %esi
-	movl	%esi, -628(%ebp)
-	movzbl	%al,%esi
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	(%edi,%esi,8), %ecx
-	movl	2048(%eax,%edi), %edx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-628(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-628(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-604(%ebp), %esi
-	cmpl	%ecx, -604(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -716(%ebp)
-	movl	-608(%ebp), %esi
-	addl	%eax, %edx
-	subl	%edx, %esi
-	movl	%esi, -720(%ebp)
-	movl	-624(%ebp), %ecx
-	movzbl	%ch, %eax
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	sall	$3, %eax
-	movl	6144(%esi,%edi), %ecx
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-628(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-628(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-592(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-596(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	shrl	$29, %eax
-	movl	%edx, -632(%ebp)
-	movl	-632(%ebp), %ecx
-	leal	0(,%esi,8), %edx
-	sall	$3, %ecx
-	orl	%eax, %ecx
-	xorl	%eax, %eax
-	cmpl	%esi, %edx
-	setb	%al
-	subl	%esi, %edx
-	movl	%edx, -636(%ebp)
-	movl	-632(%ebp), %edx
-	movl	-644(%ebp), %esi
-	addl	%edx, %eax
-	movl	-392(%ebp), %edx
-	subl	%eax, %ecx
-	movl	%ecx, -640(%ebp)
-	movl	-428(%ebp), %ecx
-	movl	-648(%ebp), %eax
-	xorl	%esi, %edx
-	movl	-396(%ebp), %esi
-	addl	%edx, %ecx
-	movl	%ecx, -656(%ebp)
-	movl	%edx, -652(%ebp)
-	xorl	%eax, %esi
-	cmpl	%edx, %ecx
-	movl	%esi, -660(%ebp)
-	setb	%al
-	movl	-432(%ebp), %ecx
-	movzbl	%al, %eax
-	addl	%esi, %eax
-	addl	%ecx, %eax
-	movl	%eax, -664(%ebp)
-	movl	-656(%ebp), %esi
-	movl	%edx, %eax
-	notl	%eax
-	movl	-464(%ebp), %edx
-	sall	$19, %eax
-	xorl	%esi, %eax
-	cmpl	%eax, -464(%ebp)
-	setb	%cl
-	movzbl	%cl, %esi
-	subl	%eax, %edx
-	movl	-652(%ebp), %ecx
-	movl	-660(%ebp), %eax
-	movl	%edx, -668(%ebp)
-	movl	-664(%ebp), %edx
-	shrl	$13, %ecx
-	sall	$19, %eax
-	orl	%ecx, %eax
-	notl	%edx
-	xorl	%eax, %edx
-	addl	%esi, %edx
-	movl	-472(%ebp), %esi
-	subl	%edx, %esi
-	movl	%esi, -672(%ebp)
-	movl	-668(%ebp), %edx
-	movl	-468(%ebp), %ecx
-	xorl	%edx, %ecx
-	movl	-504(%ebp), %edx
-	movl	%ecx, -676(%ebp)
-	xorl	%esi, %edx
-	movl	-536(%ebp), %esi
-	movl	%edx, -684(%ebp)
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	%esi, -680(%ebp)
-	setb	%al
-	movl	-540(%ebp), %ecx
-	movzbl	%al, %eax
-	addl	%edx, %eax
-	addl	%ecx, %eax
-	movl	%eax, -688(%ebp)
-	movl	-676(%ebp), %eax
-	sall	$9, %edx
-	movl	%esi, %ecx
-	notl	%ecx
-	movl	-572(%ebp), %esi
-	shrl	$23, %eax
-	orl	%edx, %eax
-	xorl	%eax, %ecx
-	xorl	%edx, %edx
-	cmpl	%ecx, -572(%ebp)
-	movl	-684(%ebp), %eax
-	setb	%dl
-	subl	%ecx, %esi
-	movl	-688(%ebp), %ecx
-	notl	%eax
-	movl	%esi, -692(%ebp)
-	shrl	$23, %eax
-	xorl	%ecx, %eax
-	addl	%edx, %eax
-	movl	-580(%ebp), %edx
-	movl	-692(%ebp), %esi
-	movl	-576(%ebp), %ecx
-	subl	%eax, %edx
-	movl	-612(%ebp), %eax
-	movl	%edx, -696(%ebp)
-	xorl	%esi, %ecx
-	movl	%ecx, -700(%ebp)
-	xorl	%edx, %eax
-	movl	-644(%ebp), %edx
-	movl	%eax, -708(%ebp)
-	movl	-708(%ebp), %esi
-	xorl	%eax, %eax
-	addl	%ecx, %edx
-	cmpl	%ecx, %edx
-	movl	-648(%ebp), %ecx
-	movl	%edx, -704(%ebp)
-	setb	%al
-	addl	%esi, %eax
-	xorl	$-1985229329, %edx
-	addl	%ecx, %eax
-	movl	-652(%ebp), %esi
-	movl	%eax, -712(%ebp)
-	xorl	%ecx, %ecx
-	movl	-712(%ebp), %eax
-	cmpl	%edx, -652(%ebp)
-	setb	%cl
-	subl	%edx, %esi
-	movl	%esi, -740(%ebp)
-	movl	-660(%ebp), %esi
-	xorl	$19088743, %eax
-	addl	%ecx, %eax
-	movl	-656(%ebp), %edx
-	subl	%eax, %esi
-	movl	%esi, -744(%ebp)
-	movl	-716(%ebp), %eax
-	xorl	%edx, %eax
-	movl	%eax, -724(%ebp)
-	movl	-664(%ebp), %ecx
-	movl	-720(%ebp), %esi
-	xorl	%ecx, %esi
-	movl	%esi, -728(%ebp)
-	movzbl	%al,%esi
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	(%edi,%esi,8), %ecx
-	movl	2048(%eax,%edi), %edx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-728(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-728(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-636(%ebp), %esi
-	cmpl	%ecx, -636(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -748(%ebp)
-	movl	-724(%ebp), %ecx
-	addl	%eax, %edx
-	movl	-640(%ebp), %esi
-	movzbl	%ch, %eax
-	subl	%edx, %esi
-	movl	%esi, -752(%ebp)
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	movl	6144(%esi,%edi), %ecx
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-728(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-728(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-624(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-628(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -732(%ebp)
-	leal	0(,%esi,8), %ecx
-	addl	%ecx, %esi
-	movl	%esi, -736(%ebp)
-	shrl	$29, %eax
-	sall	$3, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %esi
-	movl	-732(%ebp), %esi
-	setb	%al
-	addl	%eax, %edx
-	addl	%esi, %edx
-	movl	%edx, -764(%ebp)
-	movl	-668(%ebp), %eax
-	movl	-748(%ebp), %edx
-	movl	-672(%ebp), %ecx
-	movl	-752(%ebp), %esi
-	xorl	%eax, %edx
-	movl	%edx, -756(%ebp)
-	movl	%edx, %eax
-	xorl	%ecx, %esi
-	movl	%esi, -760(%ebp)
-	shrl	$13, %eax
-	movzbl	%dl,%esi
-	andl	$2040, %eax
-	movl	(%edi,%esi,8), %ecx
-	movl	2048(%eax,%edi), %edx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-760(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-760(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-736(%ebp), %esi
-	cmpl	%ecx, -736(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -776(%ebp)
-	movl	-756(%ebp), %ecx
-	addl	%eax, %edx
-	movl	-764(%ebp), %esi
-	movzbl	%ch, %eax
-	subl	%edx, %esi
-	movl	%esi, -780(%ebp)
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	movl	6144(%esi,%edi), %ecx
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-760(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-760(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-724(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-728(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -768(%ebp)
-	leal	0(,%esi,8), %ecx
-	addl	%ecx, %esi
-	movl	%esi, -772(%ebp)
-	shrl	$29, %eax
-	sall	$3, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %esi
-	movl	-768(%ebp), %esi
-	setb	%al
-	addl	%eax, %edx
-	movl	-676(%ebp), %eax
-	addl	%esi, %edx
-	movl	-684(%ebp), %ecx
-	movl	%edx, -792(%ebp)
-	movl	-776(%ebp), %edx
-	movl	-780(%ebp), %esi
-	xorl	%eax, %edx
-	movl	%edx, %eax
-	xorl	%ecx, %esi
-	movl	%esi, -788(%ebp)
-	shrl	$13, %eax
-	movzbl	%dl,%esi
-	movl	%edx, -784(%ebp)
-	andl	$2040, %eax
-	movl	2048(%eax,%edi), %edx
-	movl	(%edi,%esi,8), %ecx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-788(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-788(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-772(%ebp), %esi
-	cmpl	%ecx, -772(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -804(%ebp)
-	movl	-784(%ebp), %ecx
-	addl	%eax, %edx
-	movl	-792(%ebp), %esi
-	movzbl	%ch, %eax
-	subl	%edx, %esi
-	movl	%esi, -808(%ebp)
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	movl	6144(%esi,%edi), %ecx
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-788(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-788(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-756(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-760(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -796(%ebp)
-	leal	0(,%esi,8), %ecx
-	addl	%ecx, %esi
-	movl	%esi, -800(%ebp)
-	shrl	$29, %eax
-	sall	$3, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %esi
-	movl	-796(%ebp), %esi
-	setb	%al
-	addl	%eax, %edx
-	movl	-680(%ebp), %eax
-	addl	%esi, %edx
-	movl	-688(%ebp), %ecx
-	movl	%edx, -820(%ebp)
-	movl	-804(%ebp), %edx
-	movl	-808(%ebp), %esi
-	xorl	%eax, %edx
-	movl	%edx, %eax
-	xorl	%ecx, %esi
-	movl	%esi, -816(%ebp)
-	shrl	$13, %eax
-	movzbl	%dl,%esi
-	movl	%edx, -812(%ebp)
-	andl	$2040, %eax
-	movl	2048(%eax,%edi), %edx
-	movl	(%edi,%esi,8), %ecx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-816(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-816(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-800(%ebp), %esi
-	cmpl	%ecx, -800(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -832(%ebp)
-	movl	-812(%ebp), %ecx
-	addl	%eax, %edx
-	movl	-820(%ebp), %esi
-	movzbl	%ch, %eax
-	subl	%edx, %esi
-	movl	%esi, -836(%ebp)
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	movl	6144(%esi,%edi), %ecx
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-816(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-816(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-784(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-788(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -824(%ebp)
-	leal	0(,%esi,8), %ecx
-	addl	%ecx, %esi
-	movl	%esi, -828(%ebp)
-	shrl	$29, %eax
-	sall	$3, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %esi
-	movl	-824(%ebp), %esi
-	setb	%al
-	addl	%eax, %edx
-	movl	-692(%ebp), %eax
-	addl	%esi, %edx
-	movl	-696(%ebp), %ecx
-	movl	%edx, -848(%ebp)
-	movl	-832(%ebp), %edx
-	movl	-836(%ebp), %esi
-	xorl	%eax, %edx
-	movl	%edx, %eax
-	xorl	%ecx, %esi
-	movl	%esi, -844(%ebp)
-	shrl	$13, %eax
-	movzbl	%dl,%esi
-	movl	%edx, -840(%ebp)
-	andl	$2040, %eax
-	movl	2048(%eax,%edi), %edx
-	movl	(%edi,%esi,8), %ecx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-844(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-844(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-828(%ebp), %esi
-	cmpl	%ecx, -828(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -860(%ebp)
-	movl	-840(%ebp), %ecx
-	addl	%eax, %edx
-	movl	-848(%ebp), %esi
-	movzbl	%ch, %eax
-	subl	%edx, %esi
-	movl	%esi, -864(%ebp)
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	movl	6144(%esi,%edi), %ecx
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-844(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-844(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-812(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-816(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -852(%ebp)
-	leal	0(,%esi,8), %ecx
-	addl	%ecx, %esi
-	movl	%esi, -856(%ebp)
-	shrl	$29, %eax
-	sall	$3, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %esi
-	movl	-852(%ebp), %esi
-	setb	%al
-	addl	%eax, %edx
-	movl	-700(%ebp), %eax
-	addl	%esi, %edx
-	movl	-708(%ebp), %ecx
-	movl	%edx, -876(%ebp)
-	movl	-860(%ebp), %edx
-	movl	-864(%ebp), %esi
-	xorl	%eax, %edx
-	movl	%edx, %eax
-	xorl	%ecx, %esi
-	movl	%esi, -872(%ebp)
-	shrl	$13, %eax
-	movzbl	%dl,%esi
-	movl	%edx, -868(%ebp)
-	andl	$2040, %eax
-	movl	2048(%eax,%edi), %edx
-	movl	(%edi,%esi,8), %ecx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-872(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-872(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-856(%ebp), %esi
-	cmpl	%ecx, -856(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -888(%ebp)
-	movl	-868(%ebp), %ecx
-	addl	%eax, %edx
-	movl	-876(%ebp), %esi
-	movzbl	%ch, %eax
-	subl	%edx, %esi
-	movl	%esi, -892(%ebp)
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	movl	6144(%esi,%edi), %ecx
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-872(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-872(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-840(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-844(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -880(%ebp)
-	leal	0(,%esi,8), %ecx
-	addl	%ecx, %esi
-	movl	%esi, -884(%ebp)
-	shrl	$29, %eax
-	sall	$3, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %esi
-	movl	-880(%ebp), %esi
-	setb	%al
-	addl	%eax, %edx
-	movl	-704(%ebp), %eax
-	addl	%esi, %edx
-	movl	-712(%ebp), %ecx
-	movl	%edx, -904(%ebp)
-	movl	-888(%ebp), %edx
-	movl	-892(%ebp), %esi
-	xorl	%eax, %edx
-	movl	%edx, %eax
-	xorl	%ecx, %esi
-	movl	%esi, -900(%ebp)
-	shrl	$13, %eax
-	movzbl	%dl,%esi
-	movl	%edx, -896(%ebp)
-	andl	$2040, %eax
-	movl	2048(%eax,%edi), %edx
-	movl	(%edi,%esi,8), %ecx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-900(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-900(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-884(%ebp), %esi
-	cmpl	%ecx, -884(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -916(%ebp)
-	movl	-896(%ebp), %ecx
-	addl	%eax, %edx
-	movl	-904(%ebp), %esi
-	movzbl	%ch, %eax
-	subl	%edx, %esi
-	movl	%esi, -920(%ebp)
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	movl	6144(%esi,%edi), %ecx
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-900(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-900(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-868(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-872(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -908(%ebp)
-	leal	0(,%esi,8), %ecx
-	addl	%ecx, %esi
-	movl	%esi, -912(%ebp)
-	shrl	$29, %eax
-	sall	$3, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %esi
-	movl	-908(%ebp), %esi
-	setb	%al
-	addl	%eax, %edx
-	movl	-740(%ebp), %eax
-	addl	%esi, %edx
-	movl	-744(%ebp), %ecx
-	movl	%edx, -932(%ebp)
-	movl	-916(%ebp), %edx
-	movl	-920(%ebp), %esi
-	xorl	%eax, %edx
-	movl	%edx, %eax
-	xorl	%ecx, %esi
-	movl	%esi, -928(%ebp)
-	shrl	$13, %eax
-	movzbl	%dl,%esi
-	movl	%edx, -924(%ebp)
-	andl	$2040, %eax
-	movl	2048(%eax,%edi), %edx
-	movl	(%edi,%esi,8), %ecx
-	movl	2052(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	4(%edi,%esi,8), %edx
-	xorl	%eax, %edx
-	movzbl	-928(%ebp),%eax
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	4100(%eax,%edi), %esi
-	movl	-928(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$13, %eax
-	andl	$2040, %eax
-	movl	6144(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	6148(%eax,%edi), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-912(%ebp), %esi
-	cmpl	%ecx, -912(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	%esi, -940(%ebp)
-	movl	-924(%ebp), %ecx
-	addl	%eax, %edx
-	movl	-932(%ebp), %esi
-	movzbl	%ch, %eax
-	subl	%edx, %esi
-	movl	%esi, -944(%ebp)
-	leal	0(,%eax,8), %esi
-	movl	%ecx, %eax
-	shrl	$24, %eax
-	movl	6144(%esi,%edi), %ecx
-	sall	$3, %eax
-	movl	4096(%eax,%edi), %edx
-	movl	4100(%eax,%edi), %eax
-	xorl	%edx, %ecx
-	movl	6148(%esi,%edi), %edx
-	xorl	%eax, %edx
-	movl	-928(%ebp), %eax
-	movzbl	%ah, %esi
-	leal	0(,%esi,8), %eax
-	movl	2048(%eax,%edi), %esi
-	xorl	%esi, %ecx
-	movl	2052(%eax,%edi), %esi
-	movl	-928(%ebp), %eax
-	xorl	%esi, %edx
-	shrl	$24, %eax
-	movl	(%edi,%eax,8), %esi
-	xorl	%esi, %ecx
-	movl	4(%edi,%eax,8), %esi
-	xorl	%eax, %eax
-	xorl	%esi, %edx
-	movl	-896(%ebp), %esi
-	addl	%ecx, %esi
-	cmpl	%ecx, %esi
-	movl	-900(%ebp), %ecx
-	setb	%al
-	addl	%eax, %edx
-	movl	%esi, %eax
-	addl	%ecx, %edx
-	movl	%edx, -936(%ebp)
-	leal	0(,%esi,8), %ecx
-	shrl	$29, %eax
-	addl	%ecx, %esi
-	sall	$3, %edx
-	orl	%eax, %edx
-	xorl	%eax, %eax
-	cmpl	%ecx, %esi
-	setb	%al
-	addl	%eax, %edx
-	movl	-936(%ebp), %eax
-	movl	-20(%ebp), %ecx
-	addl	%edx, %eax
-	movl	-948(%ebp), %edx
-	xorl	%edx, %esi
-	movl	%esi, -1032(%ebp)
-	movl	-952(%ebp), %esi
-	movl	-960(%ebp), %edx
-	xorl	%esi, %eax
-	movl	-924(%ebp), %esi
-	movl	%eax, -1036(%ebp)
-	xorl	%eax, %eax
-	cmpl	%ecx, -924(%ebp)
-	setb	%al
-	subl	%ecx, %esi
-	movl	-928(%ebp), %ecx
-	addl	%edx, %eax
-	movl	-964(%ebp), %edx
-	subl	%eax, %ecx
-	movl	-24(%ebp), %eax
-	movl	%ecx, -1040(%ebp)
-	movl	-940(%ebp), %ecx
-	addl	%eax, %ecx
-	xorl	%eax, %eax
-	cmpl	-24(%ebp), %ecx
-	setb	%al
-	addl	%edx, %eax
-	movl	-944(%ebp), %edx
-	addl	%edx, %eax
-	movl	-1032(%ebp), %edx
-	movl	%eax, -1044(%ebp)
-	movl	16(%ebp), %eax
-	movl	%edx, (%eax)
-	movl	-1036(%ebp), %edx
-	movl	%esi, 8(%eax)
-	movl	%edx, 4(%eax)
-	movl	-1040(%ebp), %esi
-	movl	%ecx, 16(%eax)
-	movl	%esi, 12(%eax)
-	movl	-1044(%ebp), %edx
-	movl	%edx, 20(%eax)
-	subl	$64, -16(%ebp)
-	addl	$64, 8(%ebp)
-	cmpl	$63, -16(%ebp)
-	ja	.L6
-.L8:
-	addl	$1032, %esp
-	popl	%ebx
-	popl	%esi
-	popl	%edi
-	popl	%ebp
-	ret
-	.size	tiger_t, .-tiger_t
-.globl tiger
-	.type	tiger, @function
-tiger:
-	pushl	%ebp
-	movl	%esp, %ebp
-	pushl	%edi
-	pushl	%esi
-	subl	$72, %esp
-	movl	12(%ebp), %edi
-	movl	8(%ebp), %esi
-	cmpl	$63, %edi
-	jbe	.L33
-.L42:
-	movl	%esi, (%esp)
-	movl	16(%ebp), %eax
-	subl	$64, %edi
-	addl	$64, %esi
-	movl	%eax, 4(%esp)
-	call	tiger_compress
-	cmpl	$63, %edi
-	ja	.L132
-.L33:
-	xorl	%ecx, %ecx
-	cmpl	%edi, %ecx
-	jb	.L140
-.L35:
-	movb	$1, -72(%ecx,%ebp)
-	leal	1(%ecx), %edx
-	testb	$7, %dl
-	je	.L37
-.L44:
-	movb	$0, -72(%ebp,%edx)
-	incl	%edx
-	testb	$7, %dl
-	jne	.L135
-.L37:
-	cmpl	$56, %edx
-	jbe	.L23
-	cmpl	$63, %edx
-	jbe	.L141
-.L39:
-	movl	16(%ebp), %ecx
-	leal	-72(%ebp), %edx
-	movl	%edx, (%esp)
-	movl	%ecx, 4(%esp)
-	call	tiger_compress
-	xorl	%edx, %edx
-.L23:
-	cmpl	$55, %edx
-	ja	.L41
-	movb	$0, -72(%ebp,%edx)
-	movl	%edx, %eax
-	leal	1(%edx), %ecx
-	notl	%eax
-	andl	$7, %eax
-	cmpl	$55, %ecx
-	ja	.L41
-	testl	%eax, %eax
-	je	.L46
-	cmpl	$1, %eax
-	je	.L143
-	cmpl	$2, %eax
-	je	.L144
-	cmpl	$3, %eax
-	je	.L145
-	cmpl	$4, %eax
-	je	.L146
-	cmpl	$5, %eax
-	je	.L147
-	cmpl	$6, %eax
-	je	.L148
-	movb	$0, -72(%ebp,%ecx)
-	leal	2(%edx), %ecx
-.L148:
-	movb	$0, -72(%ebp,%ecx)
-	incl	%ecx
-.L147:
-	movb	$0, -72(%ebp,%ecx)
-	incl	%ecx
-.L146:
-	movb	$0, -72(%ebp,%ecx)
-	incl	%ecx
-.L145:
-	movb	$0, -72(%ebp,%ecx)
-	incl	%ecx
-.L144:
-	movb	$0, -72(%ebp,%ecx)
-	incl	%ecx
-.L143:
-	movb	$0, -72(%ebp,%ecx)
-	incl	%ecx
-	cmpl	$55, %ecx
-	ja	.L41
-.L46:
-	movb	$0, -72(%ebp,%ecx)
-	movb	$0, -71(%ecx,%ebp)
-	movb	$0, -70(%ecx,%ebp)
-	movb	$0, -69(%ecx,%ebp)
-	movb	$0, -68(%ecx,%ebp)
-	movb	$0, -67(%ecx,%ebp)
-	movb	$0, -66(%ecx,%ebp)
-	movb	$0, -65(%ecx,%ebp)
-	addl	$8, %ecx
-	cmpl	$55, %ecx
-	ja	.L41
-	jmp	.L46
-.L141:
-	movb	$0, -72(%ebp,%edx)
-	movl	%edx, %eax
-	leal	1(%edx), %ecx
-	notl	%eax
-	andl	$7, %eax
-	cmpl	$63, %ecx
-	ja	.L39
-	testl	%eax, %eax
-	je	.L45
-	cmpl	$1, %eax
-	je	.L149
-	cmpl	$2, %eax
-	je	.L150
-	cmpl	$3, %eax
-	je	.L151
-	cmpl	$4, %eax
-	je	.L152
-	cmpl	$5, %eax
-	je	.L153
-	cmpl	$6, %eax
-	je	.L154
-	movb	$0, -72(%ebp,%ecx)
-	leal	2(%edx), %ecx
-.L154:
-	movb	$0, -72(%ebp,%ecx)
-	incl	%ecx
-.L153:
-	movb	$0, -72(%ebp,%ecx)
-	incl	%ecx
-.L152:
-	movb	$0, -72(%ebp,%ecx)
-	incl	%ecx
-.L151:
-	movb	$0, -72(%ebp,%ecx)
-	incl	%ecx
-.L150:
-	movb	$0, -72(%ebp,%ecx)
-	incl	%ecx
-.L149:
-	movb	$0, -72(%ebp,%ecx)
-	incl	%ecx
-	cmpl	$63, %ecx
-	ja	.L39
-.L45:
-	movb	$0, -72(%ebp,%ecx)
-	movb	$0, -71(%ecx,%ebp)
-	movb	$0, -70(%ecx,%ebp)
-	movb	$0, -69(%ecx,%ebp)
-	movb	$0, -68(%ecx,%ebp)
-	movb	$0, -67(%ecx,%ebp)
-	movb	$0, -66(%ecx,%ebp)
-	movb	$0, -65(%ecx,%ebp)
-	addl	$8, %ecx
-	cmpl	$63, %ecx
-	ja	.L39
-	jmp	.L45
-.L135:
-	movb	$0, -72(%ebp,%edx)
-	incl	%edx
-	testb	$7, %dl
-	je	.L37
-	movb	$0, -72(%ebp,%edx)
-	incl	%edx
-	testb	$7, %dl
-	je	.L37
-	movb	$0, -72(%ebp,%edx)
-	incl	%edx
-	testb	$7, %dl
-	je	.L37
-	movb	$0, -72(%ebp,%edx)
-	incl	%edx
-	testb	$7, %dl
-	je	.L37
-	movb	$0, -72(%ebp,%edx)
-	incl	%edx
-	testb	$7, %dl
-	je	.L37
-	movb	$0, -72(%ebp,%edx)
-	incl	%edx
-	testb	$7, %dl
-	je	.L37
-	movb	$0, -72(%ebp,%edx)
-	incl	%edx
-	testb	$7, %dl
-	je	.L37
-	jmp	.L44
-.L140:
-	movl	%ecx, %eax
-	notl	%eax
-	addl	%edi, %eax
-	movl	%eax, %edx
-	andl	$7, %edx
-	movzbl	(%esi,%ecx), %eax
-	movb	%al, -72(%ebp,%ecx)
-	incl	%ecx
-	cmpl	%edi, %ecx
-	jae	.L35
-	testl	%edx, %edx
-	je	.L43
-	cmpl	$1, %edx
-	je	.L155
-	cmpl	$2, %edx
-	je	.L156
-	cmpl	$3, %edx
-	je	.L157
-	cmpl	$4, %edx
-	je	.L158
-	cmpl	$5, %edx
-	je	.L159
-	cmpl	$6, %edx
-	je	.L160
-	movzbl	(%esi,%ecx), %edx
-	movb	%dl, -72(%ebp,%ecx)
-	incl	%ecx
-.L160:
-	movzbl	(%esi,%ecx), %eax
-	movb	%al, -72(%ebp,%ecx)
-	incl	%ecx
-.L159:
-	movzbl	(%esi,%ecx), %edx
-	movb	%dl, -72(%ebp,%ecx)
-	incl	%ecx
-.L158:
-	movzbl	(%esi,%ecx), %eax
-	movb	%al, -72(%ebp,%ecx)
-	incl	%ecx
-.L157:
-	movzbl	(%esi,%ecx), %edx
-	movb	%dl, -72(%ebp,%ecx)
-	incl	%ecx
-.L156:
-	movzbl	(%esi,%ecx), %eax
-	movb	%al, -72(%ebp,%ecx)
-	incl	%ecx
-.L155:
-	movzbl	(%esi,%ecx), %edx
-	movb	%dl, -72(%ebp,%ecx)
-	incl	%ecx
-	cmpl	%edi, %ecx
-	jae	.L35
-.L43:
-	movzbl	(%esi,%ecx), %edx
-	movb	%dl, -72(%ebp,%ecx)
-	leal	1(%ecx), %edx
-	movzbl	(%esi,%edx), %eax
-	movb	%al, -72(%ebp,%edx)
-	leal	2(%ecx), %edx
-	movzbl	(%esi,%edx), %eax
-	movb	%al, -72(%ebp,%edx)
-	leal	3(%ecx), %edx
-	movzbl	(%esi,%edx), %eax
-	movb	%al, -72(%ebp,%edx)
-	leal	4(%ecx), %edx
-	movzbl	(%esi,%edx), %eax
-	movb	%al, -72(%ebp,%edx)
-	leal	5(%ecx), %edx
-	movzbl	(%esi,%edx), %eax
-	movb	%al, -72(%ebp,%edx)
-	leal	6(%ecx), %edx
-	movzbl	(%esi,%edx), %eax
-	movb	%al, -72(%ebp,%edx)
-	leal	7(%ecx), %edx
-	addl	$8, %ecx
-	movzbl	(%esi,%edx), %eax
-	cmpl	%edi, %ecx
-	movb	%al, -72(%ebp,%edx)
-	jae	.L35
-	jmp	.L43
-.L132:
-	movl	%esi, (%esp)
-	movl	16(%ebp), %ecx
-	subl	$64, %edi
-	addl	$64, %esi
-	movl	%ecx, 4(%esp)
-	call	tiger_compress
-	cmpl	$63, %edi
-	jbe	.L33
-	movl	%esi, (%esp)
-	movl	16(%ebp), %eax
-	subl	$64, %edi
-	addl	$64, %esi
-	movl	%eax, 4(%esp)
-	call	tiger_compress
-	cmpl	$63, %edi
-	jbe	.L33
-	movl	%esi, (%esp)
-	movl	16(%ebp), %edx
-	subl	$64, %edi
-	addl	$64, %esi
-	movl	%edx, 4(%esp)
-	call	tiger_compress
-	cmpl	$63, %edi
-	jbe	.L33
-	movl	%esi, (%esp)
-	movl	16(%ebp), %ecx
-	subl	$64, %edi
-	addl	$64, %esi
-	movl	%ecx, 4(%esp)
-	call	tiger_compress
-	cmpl	$63, %edi
-	jbe	.L33
-	movl	%esi, (%esp)
-	movl	16(%ebp), %eax
-	subl	$64, %edi
-	addl	$64, %esi
-	movl	%eax, 4(%esp)
-	call	tiger_compress
-	cmpl	$63, %edi
-	jbe	.L33
-	movl	%esi, (%esp)
-	movl	16(%ebp), %edx
-	subl	$64, %edi
-	addl	$64, %esi
-	movl	%edx, 4(%esp)
-	call	tiger_compress
-	cmpl	$63, %edi
-	jbe	.L33
-	movl	%esi, (%esp)
-	movl	16(%ebp), %ecx
-	subl	$64, %edi
-	addl	$64, %esi
-	movl	%ecx, 4(%esp)
-	call	tiger_compress
-	cmpl	$63, %edi
-	jbe	.L33
-	jmp	.L42
-.L41:
-	movl	$0, -12(%ebp)
-	movl	12(%ebp), %eax
-	leal	-72(%ebp), %esi
-	movl	%esi, (%esp)
-	movl	16(%ebp), %edi
-	sall	$3, %eax
-	movl	%edi, 4(%esp)
-	movl	%eax, -16(%ebp)
-	call	tiger_compress
-	addl	$72, %esp
-	popl	%esi
-	popl	%edi
-	popl	%ebp
-	ret
-	.size	tiger, .-tiger
-	.section	.gnu.linkonce.t.__i686.get_pc_thunk.bx,"ax",@progbits
-.globl __i686.get_pc_thunk.bx
-	.hidden	__i686.get_pc_thunk.bx
-	.type	__i686.get_pc_thunk.bx, @function
-__i686.get_pc_thunk.bx:
-	movl	(%esp), %ebx
-	ret
-	.section	.note.GNU-stack,"",@progbits
-	.ident	"GCC: (GNU) 3.4.0"
Index: trunk/src/sh_tiger1_64.c
===================================================================
--- trunk/src/sh_tiger1_64.c	(revision 591)
+++ trunk/src/sh_tiger1_64.c	(revision 1)
@@ -5,25 +5,13 @@
 #include "config_xor.h"
 
-#if defined(__clang__)
-#undef TIGER_OPT_ASM
-#endif
-
-#if defined(TIGER_64_BIT)
-
-#if defined(GCC_VERSION_MAJOR) && !defined(__clang__)
-#if ((GCC_VERSION_MAJOR == 4) && (GCC_VERSION_MINOR > 6))
-#pragma GCC optimize ("O1")
-#endif
-#endif
-
-
-/* #if defined(HAVE_LONG_64) || defined(HAVE_LONG_LONG_64) */
-
-#undef USE_MEMSET
-
+#if defined(HAVE_LONG_64) || defined(HAVE_LONG_LONG_64)
+
+/*@-incondefs -macroparens -macroassign -macroparams -macrostmt @*/
+/*@-fixedformalarray +charindex -type -paramuse -predboolint -exportlocal@*/
 /* Big endian:                                         */
 #ifdef WORDS_BIGENDIAN
 #define BIG_ENDIAN
 #endif
+
 
 /* Tiger: A Fast New Hash Function
@@ -50,8 +38,6 @@
 #if defined(HAVE_LONG_64)
 typedef unsigned long int word64;
-#elif defined(HAVE_LONG_LONG_64)
+#else
 typedef unsigned long long int word64;
-#else
-#error No 64 bit type found !
 #endif
 
@@ -63,26 +49,34 @@
 typedef unsigned short sh_word32;
 #else
-#error No 32 bit type found !
+#error No 32 byte type found !
 #endif
 
 typedef unsigned char sh_byte;
 
-#if defined(TIGER_OPT_ASM)
-#define TIGER_ASM64_2 1
-#else
-#define TIGER_C 1
-#endif
-
-/* The number of passes of the hash function.		   */
-/* Three passes are recommended.			   */
-/* Use four passes when you need extra security.	   */
-/* Must be at least three.				   */
+/* Big endian:                                         
+   #if !(defined(__alpha)||defined(__i386__)||defined(__vax__))
+   #define BIG_ENDIAN
+   #endif
+*/
+
+/* The following macro denotes that an optimization    */
+/* for Alpha is required. It is used only for          */
+/* optimization of time. Otherwise it does nothing.    */
+#ifdef __alpha
+#define OPTIMIZE_FOR_ALPHA
+#endif
+
+/* NOTE that this code is NOT FULLY OPTIMIZED for any  */
+/* machine. Assembly code might be much faster on some */
+/* machines, especially if the code is compiled with   */
+/* gcc.                                                */
+
+/* The number of passes of the hash function.          */
+/* Three passes are recommended.                       */
+/* Use four passes when you need extra security.       */
+/* Must be at least three.                             */
 #define PASSES 3
 
 extern word64 tiger_table[4*256];
-
-/* Volatile can help if compiler is smart enough to use memory operand */
-static /*volatile*/ const word64 XOR_CONST1=0xA5A5A5A5A5A5A5A5LL;
-static /*volatile*/ const word64 XOR_CONST2=0x0123456789ABCDEFLL;
 
 #define t1 (tiger_table)
@@ -91,279 +85,97 @@
 #define t4 (tiger_table+256*3)
 
-#define pass_start
-#define pass_end
-
-
-
 #define save_abc \
-	  aa = a; \
-	  bb = b; \
-	  cc = c;
-
-#ifdef TIGER_C
-
-#define BN(x,n) (((x)>>((n)*8))&0xFF)
-
-
-/* Depending on outer code one of these two can be better*/
-#define roundX(a,b,c,x) \
-	c ^= x; \
-	a -= t1[BN(c,0)] ^ t2[BN(c,2)] ^ \
-	     t3[BN(c,4)] ^ t4[BN(c,6)] ; \
-	b += t4[BN(c,1)] ^ t3[BN(c,3)] ^ \
-	     t2[BN(c,5)] ^ t1[BN(c,7)] ;
-
-#define round5(a,b,c,x) roundX(a,b,c,x) b = b+b*4;
-#define round7(a,b,c,x) roundX(a,b,c,x) b = b*8-b;
-#define round9(a,b,c,x) roundX(a,b,c,x) b = b+b*8;
-
-#endif
-
-
-#ifdef TIGER_OPT_ASM
-
-#define MASK0		0xFFL
-#define MASK8		0xFF00L
-#define MASK16		0xFF0000L
-#define MASK32		0xFF00000000LL
-#define MASK40		0xFF0000000000LL
-#define MASK48		0xFF000000000000LL
-
-#define roundstart	__asm__ (
-
-/* a will be moved into different reg each round
- * using register substitution feature of  GCC asm
- * b will be moved in 2-nd pass rounds only
- */
-
-
-#define roundend(a,b,c,x) \
- : "+r" (a), "+r" (b), "+r" (c) \
- : "g" (a), "g" (b), "g" (c), "m" (x), "r" (&tiger_table),\
-  "i" (MASK0), "i" (MASK8), "i" (MASK16), "r" (MASK32), "r" (MASK40), "r" (MASK48) \
- : "3", "%rax","%rbx","%rcx","%rdx","%rsi", "%edi", "%r8"  );
-
-
-/*	c ^= x; 
-	a -= t1[BN(c,0)] ^ t2[BN(c,2)] ^ 
-	t3[BN(c,4)] ^ t4[BN(c,6)] ; 
-	b += t4[BN(c,1)] ^ t3[BN(c,3)] ^ 
-	t2[BN(c,5)] ^ t1[BN(c,7)] ; 	*/
-
-#define roundX(a,b,c,x)   \
-"	movl	%10, %%ebx	\n"\
-"	movq	%11, %%rcx	\n"\
-"	movq	%13, %%rdx	\n"\
-"	movq	%6, %%r8  \n"\
-"	xorq	%%r8, %2		 \n" \
-"	andq	%2, %%rbx  \n"\
-"	andq	%2, %%rcx  \n"\
-"	andq	%2, %%rdx  \n"\
-"	shrl	$(16-3), %%ebx	\n"\
-"	shrq	$(32-3), %%rcx	\n"\
-"	shrq	$(48-3), %%rdx	\n"\
-"	movzbl	%2b, %%eax	\n"\
-"	movzwl	%2w, %%edi	\n"\
-"	movq	(%7,%%rax,8), %%rsi  \n"\
-"	shrl	$(8), %%edi  \n" \
-"	movq	%2, %%rax  \n" \
-"	xorq	(2048*1)(%7,%%rbx), %%rsi  \n"\
-"	movq	%2, %%rbx  \n"\
-"	shrl	$24, %%eax \n"\
-"	andq	%12, %%rbx	\n"\
-"	xorq	(2048*2)(%7,%%rcx), %%rsi  \n"\
-"	shrq	$(40-3), %%rbx \n"\
-"	movq	%2, %%rcx  \n"\
-"	xorq	(2048*3)(%7,%%rdx), %%rsi  \n"\
-"	movq	(2048*3)(%7,%%rdi,8), %%rdx  \n"\
-"	shrq	$56, %%rcx \n"\
-"	xorq	(2048*2)(%7,%%rax,8), %%rdx  \n"\
-"	xorq	(2048*1)(%7,%%rbx), %%rdx  \n" \
-"	subq	 %%rsi, %0 \n"\
-"	xorq	(%7,%%rcx,8), %%rdx  \n"\
-"	addq	 %%rdx, %1 \n"
-
-#define round5(a,b,c,x) \
-	roundstart \
-	roundX(a,b,c,x) \
-	/* b*=5; */ \
-	"leaq	(%1,%1,4), %1\n" \
-	roundend(a,b,c,x)
-
-
-#define round7(a,b,c,x) \
-	roundstart \
-	roundX(a,b,c,x) \
-	roundend(a,b,c,x) \
-	/* b*=7; */ \
-	__asm__ ( \
-	"leaq	(%1,%1,8), %0\n" \
-	"addq  %1, %1 \n" \
-	"subq  %1, %0 " \
-	:"=&r" (b): "r"(b): "1" );
-
-#define round9(a,b,c,x) \
-	roundstart \
-	roundX(a,b,c,x) \
-	"leaq	(%1,%1,8), %1\n" \
-	roundend(a,b,c,x)
-
-#endif
-
-
-
-
-/* ============== Common macros ================== */
+      aa = a; \
+      bb = b; \
+      cc = c;
+
+#ifdef OPTIMIZE_FOR_ALPHA
+/* This is the official definition of round */
+#define round(a,b,c,x,mul) \
+      c ^= x; \
+      a -= t1[((c)>>(0*8))&0xFF] ^ t2[((c)>>(2*8))&0xFF] ^ \
+	   t3[((c)>>(4*8))&0xFF] ^ t4[((c)>>(6*8))&0xFF] ; \
+      b += t4[((c)>>(1*8))&0xFF] ^ t3[((c)>>(3*8))&0xFF] ^ \
+	   t2[((c)>>(5*8))&0xFF] ^ t1[((c)>>(7*8))&0xFF] ; \
+      b *= mul;
+#else
+/* This code works faster when compiled on 32-bit machines */
+/* (but works slower on Alpha) */
+#define round(a,b,c,x,mul) \
+      c ^= x; \
+      a -= t1[(sh_byte)(c)] ^ \
+           t2[(sh_byte)(((sh_word32)(c))>>(2*8))] ^ \
+	   t3[(sh_byte)((c)>>(4*8))] ^ \
+           t4[(sh_byte)(((sh_word32)((c)>>(4*8)))>>(2*8))] ; \
+      b += t4[(sh_byte)(((sh_word32)(c))>>(1*8))] ^ \
+           t3[(sh_byte)(((sh_word32)(c))>>(3*8))] ^ \
+	   t2[(sh_byte)(((sh_word32)((c)>>(4*8)))>>(1*8))] ^ \
+           t1[(sh_byte)(((sh_word32)((c)>>(4*8)))>>(3*8))]; \
+      b *= mul;
+#endif
+
+#define pass(a,b,c,mul) \
+      round(a,b,c,x0,mul) \
+      round(b,c,a,x1,mul) \
+      round(c,a,b,x2,mul) \
+      round(a,b,c,x3,mul) \
+      round(b,c,a,x4,mul) \
+      round(c,a,b,x5,mul) \
+      round(a,b,c,x6,mul) \
+      round(b,c,a,x7,mul)
 
 #define key_schedule \
-	x0 -= x7 ^ XOR_CONST1; \
-	x1 ^= x0; \
-	x2 += x1;\
-	x3 -= x2 ^ ((~x1)<<19);\
-	x4 ^= x3;\
-	x5 += x4;\
-	x6 -= x5 ^ ((~x4)>>23); \
-	x7 ^= x6; \
-	x0 += x7; \
-	x1 -= x0 ^ ((~x7)<<19); \
-	x2 ^= x1; \
-	x3 += x2; \
-	x4 -= x3 ^ ((~x2)>>23); \
-	x5 ^= x4; \
-	x6 += x5; \
-	x7 -= x6 ^ XOR_CONST2;
-
-#define pass5n(a,b,c) \
-	  round5(a,b,c,x0) \
-	x0 -= x7 ^ XOR_CONST1; \
-	  round5(b,c,a,x1) \
-	x1 ^= x0; \
-	  round5(c,a,b,x2) \
-	x2 += x1; \
-	  round5(a,b,c,x3) \
-	x3 -= x2 ^ ((~x1)<<19); \
-	  round5(b,c,a,x4) \
-	x4 ^= x3; \
-	  round5(c,a,b,x5) \
-	x5 += x4; \
-	  round5(a,b,c,x6) \
-	x6 -= x5 ^ ((~x4)>>23); \
-	  round5(b,c,a,x7) \
-	x7 ^= x6; \
-	x0 += x7; \
-	x1 -= x0 ^ ((~x7)<<19); \
-	x2 ^= x1; \
-	x3 += x2; \
-	x4 -= x3 ^ ((~x2)>>23); \
-	x5 ^= x4; \
-	x6 += x5; \
-	x7 -= x6 ^ XOR_CONST2;
-
-#define pass7n(a,b,c) \
-	  round7(a,b,c,x0) \
-	x0 -= x7 ^ XOR_CONST1; \
-	  round7(b,c,a,x1) \
-	x1 ^= x0; \
-	  round7(c,a,b,x2) \
-	x2 += x1; \
-	  round7(a,b,c,x3) \
-	x3 -= x2 ^ ((~x1)<<19); \
-	  round7(b,c,a,x4) \
-	x4 ^= x3; \
-	  round7(c,a,b,x5) \
-	x5 += x4; \
-	  round7(a,b,c,x6) \
-	x6 -= x5 ^ ((~x4)>>23); \
-	  round7(b,c,a,x7) \
-	x7 ^= x6; \
-	x0 += x7; \
-	x1 -= x0 ^ ((~x7)<<19); \
-	x2 ^= x1; \
-	x3 += x2; \
-	x4 -= x3 ^ ((~x2)>>23); \
-	x5 ^= x4; \
-	x6 += x5; \
-	x7 -= x6 ^ XOR_CONST2;
-
-#define pass5(a,b,c) \
-	pass_start \
-	  round5(a,b,c,x0) \
-	  round5(b,c,a,x1) \
-	  round5(c,a,b,x2) \
-	  round5(a,b,c,x3) \
-	  round5(b,c,a,x4) \
-	  round5(c,a,b,x5) \
-	  round5(a,b,c,x6) \
-	  round5(b,c,a,x7) \
-	pass_end
-
-#define pass7(a,b,c) \
-	pass_start \
-	  round7(a,b,c,x0) \
-	  round7(b,c,a,x1) \
-	  round7(c,a,b,x2) \
-	  round7(a,b,c,x3) \
-	  round7(b,c,a,x4) \
-	  round7(c,a,b,x5) \
-	  round7(a,b,c,x6) \
-	  round7(b,c,a,x7) \
-	pass_end
-
-
-#define pass9(a,b,c) \
-	pass_start \
-	  round9(a,b,c,x0) \
-	  round9(b,c,a,x1) \
-	  round9(c,a,b,x2) \
-	  round9(a,b,c,x3) \
-	  round9(b,c,a,x4) \
-	  round9(c,a,b,x5) \
-	  round9(a,b,c,x6) \
-	  round9(b,c,a,x7) \
-	pass_end
+      x0 -= x7 ^ 0xA5A5A5A5A5A5A5A5LL; \
+      x1 ^= x0; \
+      x2 += x1; \
+      x3 -= x2 ^ ((~x1)<<19); \
+      x4 ^= x3; \
+      x5 += x4; \
+      x6 -= x5 ^ ((~x4)>>23); \
+      x7 ^= x6; \
+      x0 += x7; \
+      x1 -= x0 ^ ((~x7)<<19); \
+      x2 ^= x1; \
+      x3 += x2; \
+      x4 -= x3 ^ ((~x2)>>23); \
+      x5 ^= x4; \
+      x6 += x5; \
+      x7 -= x6 ^ 0x0123456789ABCDEFLL;
 
 #define feedforward \
-	  a ^= aa; \
-	  b -= bb; \
-	  c += cc;
-
-
-/* This version works ok with C variant and also with new asm version 
- * that just wastes a register r8 
- * reason? who knows, write forwarding is faster than keeping value 
- * in register? :) 
- */
+      a ^= aa; \
+      b -= bb; \
+      c += cc;
+
+#ifdef OPTIMIZE_FOR_ALPHA
+/* The loop is unrolled: works better on Alpha */
 #define compress \
-	save_abc \
-	  pass5n(a,b,c) \
-	  pass7n(c,a,b) \
-	  pass9(b,c,a) \
-	  for(pass_no=3; pass_no<PASSES; pass_no++) { \
-		key_schedule \
-		pass9(a,b,c) \
-		tmpa=a; a=c; c=b; b=tmpa; \
-	  } \
-	feedforward
-
-#define compress_old \
-	save_abc \
-	  pass5(a,b,c) \
-	  key_schedule \
-	  pass7(c,a,b) \
-	  key_schedule \
-	  pass9(b,c,a) \
-	  for(pass_no=3; pass_no<PASSES; pass_no++) { \
-		key_schedule \
-		pass9(a,b,c) \
-		tmpa=a; a=c; c=b; b=tmpa; \
-	  } \
-	feedforward
+      save_abc \
+      pass(a,b,c,5) \
+      key_schedule \
+      pass(c,a,b,7) \
+      key_schedule \
+      pass(b,c,a,9) \
+      for(pass_no=3; pass_no<PASSES; pass_no++) { \
+        key_schedule \
+	pass(a,b,c,9) \
+	tmpa=a; a=c; c=b; b=tmpa;} \
+      feedforward
+#else
+/* loop: works better on PC and Sun (smaller cache?) */
+#define compress \
+      save_abc \
+      for(pass_no=0; pass_no<PASSES; pass_no++) { \
+        if(pass_no != 0) {key_schedule} \
+	pass(a,b,c,(pass_no==0?5:pass_no==1?7:9)); \
+	tmpa=a; a=c; c=b; b=tmpa;} \
+      feedforward
+#endif
 
 #define tiger_compress_macro(str, state) \
 { \
-  register word64 a, b, c; \
-  register word64 tmpa; \
+  register word64 a, b, c, tmpa; \
   word64 aa, bb, cc; \
-  word64 x0, x1, x2, x3, x4, x5, x6, x7; \
+  register word64 x0, x1, x2, x3, x4, x5, x6, x7; \
   int pass_no; \
 \
@@ -382,10 +194,21 @@
 }
 
-void tiger_compress(const word64 *str, word64 state[3])
+/* The compress function is a function. Requires smaller cache?    */
+void tiger_compress(word64 *str, word64 state[3])
 {
-  tiger_compress_macro(((const word64*)str), ((word64*)state));
-}
-
-void tiger_t(const word64 *str, word64 length, word64 res[3])
+#ifndef S_SPLINT_S
+  tiger_compress_macro(((word64*)str), ((word64*)state));
+#endif
+}
+
+#ifdef OPTIMIZE_FOR_ALPHA
+/* The compress function is inlined: works better on Alpha.        */
+/* Still leaves the function above in the code, in case some other */
+/* module calls it directly.                                       */
+#define tiger_compress(str, state) \
+  tiger_compress_macro(((word64*)str), ((word64*)state))
+#endif
+
+void tiger_t(word64 *str, word64 length, word64 res[3])
 {
   register word64 i;
@@ -397,8 +220,8 @@
 
   /*
-   * res[0]=0x0123456789ABCDEFLL;
-   * res[1]=0xFEDCBA9876543210LL;
-   * res[2]=0xF096A5B4C3B2E187LL;
-   */
+    res[0]=0x0123456789ABCDEFLL;
+    res[1]=0xFEDCBA9876543210LL;
+    res[2]=0xF096A5B4C3B2E187LL;
+  */
 
   for(i=length; i>=64; i-=64)
@@ -406,5 +229,5 @@
 #ifdef BIG_ENDIAN
       for(j=0; j<64; j++)
-        temp[j^7] = ((sh_byte*)str)[j];
+	temp[j^7] = ((sh_byte*)str)[j];
       tiger_compress(((word64*)temp), res);
 #else
@@ -413,24 +236,18 @@
       str += 8;
     }
-}
-
-void tiger(const word64 *str, word64 length, word64 res[3])
+
+}
+
+void tiger(word64 *str, word64 length, word64 res[3])
 {
   register word64 i;
   register word64 j = 0;
-  union {
-    word64 w64_temp[8];
-    unsigned char temp[64];
-  } dd;
-  union {
-    word64 itmp;
-    unsigned char ctmp[8];
-  } uu;
+  unsigned char temp[64];
 
   /*
-   * res[0]=0x0123456789ABCDEFLL;
-   * res[1]=0xFEDCBA9876543210LL;
-   * res[2]=0xF096A5B4C3B2E187LL;
-   */
+    res[0]=0x0123456789ABCDEFLL;
+    res[1]=0xFEDCBA9876543210LL;
+    res[2]=0xF096A5B4C3B2E187LL;
+  */
 
   for(i=length; i>=64; i-=64)
@@ -438,6 +255,6 @@
 #ifdef BIG_ENDIAN
       for(j=0; j<64; j++)
-        dd.temp[j^7] = ((sh_byte*)str)[j];
-      tiger_compress((dd.w64_temp), res);
+	temp[j^7] = ((sh_byte*)str)[j];
+      tiger_compress(((word64*)temp), res);
 #else
       tiger_compress(str, res);
@@ -448,51 +265,48 @@
 #ifdef BIG_ENDIAN
   for(j=0; j<i; j++)
-    dd.temp[j^7] = ((sh_byte*)str)[j];
-
-  dd.temp[j^7] = 0x01;
+    temp[j^7] = ((sh_byte*)str)[j];
+
+  temp[j^7] = 0x01;
   j++;
   for(; j&7; j++)
-    dd.temp[j^7] = 0;
-#else
-
-#ifndef USE_MEMSET
+    temp[j^7] = 0;
+#else
   for(j=0; j<i; j++)
-    dd.temp[j] = ((const sh_byte*)str)[j];
-#else
-  memcpy( dd.temp, str, j=i );
-#endif
-  dd.temp[j++] = 0x01;
+    temp[j] = ((sh_byte*)str)[j];
+
+  temp[j++] = 0x01;
   for(; j&7; j++)
-	dd.temp[j] = 0;
-
-#endif
-
+    temp[j] = 0;
+#endif
   if(j>56)
     {
-#ifndef USE_MEMSET
       for(; j<64; j++)
-	dd.temp[j] = 0;
-#else
-      memset( (dd.temp)+j, 0, 64-j);
-#endif
-      tiger_compress((dd.w64_temp), res);
+	temp[j] = 0;
+      tiger_compress(((word64*)temp), res);
       j=0;
     }
 
-#ifndef USE_MEMSET
   for(; j<56; j++)
-    dd.temp[j] = 0;
-#else
-  memset( (dd.temp)+j, 0, 56-j);
-#endif
-
-  /* Avoid gcc warning for type-punned pointer 
-   */
-  uu.itmp = ((word64)length)<<3;
-  for (j=0; j<8; j++)
-        dd.temp[56+j] = uu.ctmp[j];
-
-  tiger_compress((dd.w64_temp), res);
-}
-
-#endif
+    temp[j] = 0;
+  ((word64*)(&(temp[56])))[0] = ((word64)length)<<3;
+  tiger_compress(((word64*)temp), res);
+}
+
+#else
+
+void dummy_1_64 (int a)
+{
+  (void) a;
+  return;
+}
+
+#endif
+
+
+
+
+
+
+
+
+
Index: trunk/src/sh_tiger2.c
===================================================================
--- trunk/src/sh_tiger2.c	(revision 591)
+++ trunk/src/sh_tiger2.c	(revision 1)
@@ -23,20 +23,10 @@
 
 
-#if !defined(TIGER_64_BIT)
+#if !defined(HAVE_LONG_64) && !defined(HAVE_LONG_LONG_64)
 
-/* #if !defined(HAVE_LONG_64) && !defined(HAVE_LONG_LONG_64) */
 
 /* sboxes32.c: Tiger S boxes for 32-bit-only compilers */
-#if defined(HAVE_INT_32)
-typedef unsigned int sh_word32;
-#elif defined(HAVE_LONG_32)
-typedef unsigned long sh_word32;
-#elif defined(HAVE_SHORT_32)
-typedef unsigned short sh_word32;
-#else
-#error No 32 bit type found !
-#endif
-
-sh_word32 tiger_table[4*256][2] = {
+typedef unsigned long word32;
+word32 tiger_table[4*256][2] = {
   { 0xF7E90C5E, 0x02AAB17C /*    0 */},  {  0xE243A8EC, 0xAC424B03 /*    1 */},
   { 0x0DD5FCD3, 0x72CD5BE3 /*    2 */},  {  0xF6F97F3A, 0x6D019B93 /*    3 */},
Index: trunk/src/sh_tiger2_64.c
===================================================================
--- trunk/src/sh_tiger2_64.c	(revision 591)
+++ trunk/src/sh_tiger2_64.c	(revision 1)
@@ -22,7 +22,6 @@
 #include "config_xor.h"
 
-#if defined(TIGER_64_BIT)
-
-/* #if defined(HAVE_LONG_64) || defined(HAVE_LONG_LONG_64) */
+
+#if defined(HAVE_LONG_64) || defined(HAVE_LONG_LONG_64)
 
 /*@-type@*/
@@ -31,8 +30,6 @@
 #if defined(HAVE_LONG_64)
 typedef unsigned long int word64;
-#elif defined(HAVE_LONG_LONG_64)
+#else
 typedef unsigned long long int word64;
-#else
-#error No 64 bit type found !
 #endif
 
Index: trunk/src/sh_tools.c
===================================================================
--- trunk/src/sh_tools.c	(revision 591)
+++ trunk/src/sh_tools.c	(revision 1)
@@ -70,5 +70,5 @@
 #endif
 #ifndef FD_ZERO
-#define FD_ZERO(p)      memset((char *)(p), 0, sizeof(*(p)))
+#define FD_ZERO(p)      memset((char *)(p), '\0', sizeof(*(p)))
 #endif
 
@@ -77,6 +77,4 @@
 #include <sys/mman.h>
 #endif
-
-#define SH_REAL_SET
 
 #include "samhain.h"
@@ -88,166 +86,53 @@
 #define SH_NEED_GETHOSTBYXXX
 #include "sh_static.h"
-#include "sh_pthread.h"
-#include "sh_ipvx.h"
 
 #undef  FIL__
 #define FIL__  _("sh_tools.c")
 
-static int tools_debug = 0;
-
 #ifdef SH_ENCRYPT
 #include "rijndael-api-fst.h"
-char * errorExplain (int err_num, char * buffer, size_t len)
-{
-  char * p;
-
+char * errorExplain (int err_num)
+{
   if      (err_num == BAD_KEY_DIR)
-    p = (_("Key direction is invalid"));
+    return (_("Key direction is invalid"));
   else if (err_num == BAD_KEY_MAT) 
-    p = (_("Key material not of correct length"));
+    return (_("Key material not of correct length"));
   else if (err_num == BAD_KEY_INSTANCE) 
-    p = (_("Key passed is not valid"));
+    return (_("Key passed is not valid"));
   else if (err_num == BAD_CIPHER_MODE) 
-    p = (_("Params struct passed to rijndael_cipherInit invalid"));
+    return (_("Params struct passed to cipherInit invalid"));
   else if (err_num == BAD_CIPHER_STATE) 
-    p = (_("Cipher in wrong state"));
+    return (_("Cipher in wrong state"));
   else if (err_num == BAD_BLOCK_LENGTH) 
-    p = (_("Bad block length"));
+    return (_("Bad block length"));
   else if (err_num == BAD_CIPHER_INSTANCE) 
-    p = (_("Bad cipher instance"));
+    return (_("Bad cipher instance"));
   else if (err_num == BAD_DATA) 
-    p = (_("Data contents are invalid"));
+    return (_("Data contents are invalid"));
   else  
-    p = (_("Unknown error"));
-  sl_strlcpy (buffer, p, len);
-  return buffer;
-}
-
-#endif
-
-/* --- check for an interface ---
+    return (_("Unknown error"));
+}
+
+#endif
+
+/* --- recode all \blah escapes to '=XX' format, and also code all
+ *     remaining unprintable chars                                 ---
  */
-int sh_tools_iface_is_present(char *str)
-{
-#if defined(USE_IPVX)
-  struct addrinfo *ai;
-  struct addrinfo hints;
-  int             res;
-
-  memset (&hints, 0, sizeof (hints));
-  hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
-  hints.ai_socktype = SOCK_STREAM;
-  res = getaddrinfo (str, _("2543"), &hints, &ai);
-  
-  if (res == 0)
-    {
-      struct addrinfo *p = ai;
-      while (p != NULL)
-	{
-	  int fd = socket (p->ai_family, p->ai_socktype,
-			   p->ai_protocol);
-
-	  if (fd < 0)
-	    {
-	      freeaddrinfo (ai);
-	      return 0;
-	    }
-
-	  if (bind (fd, p->ai_addr, p->ai_addrlen) != 0)
-	    {
-	      /* bind() fails for access reasons, iface exists
-	       */
-	      if (errno == EACCES || errno == EADDRINUSE)
-		{
-		  sl_close_fd (FIL__, __LINE__, fd);
-		  freeaddrinfo (ai);
-		  return 1;
-		}
-
-	      sl_close_fd (FIL__, __LINE__, fd);
-	      freeaddrinfo (ai);
-	      return 0;
-	    }
-
-	  sl_close_fd (FIL__, __LINE__, fd);
-	  freeaddrinfo (ai);
-	  return 1;
-	  /* p = p->ai_next; */ 
-	}
-    }
-#else
-  struct sockaddr_in sin;
-  int sd;
-
-  memset(&sin, 0, sizeof(sin));
-  sin.sin_family = AF_INET;
-  if (inet_aton(str, &(sin.sin_addr)))
-    {
-      sin.sin_port = htons(2543);
-
-      if (-1 == (sd = socket(AF_INET, SOCK_STREAM, 0)))
-	{
-	  /* cppcheck-suppress resourceLeak */
-	  return 0;
-	}
-
-      if (-1 == bind(sd, (struct sockaddr *)&sin, sizeof(sin)))
-	{
-	  int retval = 0;
-
-	  /* bind() fails for access reasons, iface exists
-	   */
-	  if (errno == EACCES || errno == EADDRINUSE)
-	    retval = 1;
-	  sl_close_fd (FIL__, __LINE__, sd);
-	  return retval;
-	}
-
-      /* bind() succeeds, iface exists
-       */
-      sl_close_fd(FIL__, __LINE__, sd);
-      return 1;
-    }
-#endif
-  return 0;
-}
-
-/* --- recode all \blah escapes to qp (quoted printable) '=XX' format, and 
- *     also code all remaining unprintable chars                           ---
- */
-#define SH_PUT_4(p, a, b, c) (p)[0] = (a); (p)[1] = (b); (p)[2] = (c);
-  
 char * sh_tools_safe_name (const char * instr, int flag)
 {
-  unsigned char c, d;
+  unsigned char c;
   const  char * p;
   char   tmp[4];
   char * outstr;
-  size_t len = 1;
+  int    len;
   int    i = 0;
   unsigned char   val_octal = '\0';
-  static char ctable[16] = { '0', '1', '2', '3', '4', '5', '6', '7', 
-			     '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; 
 
   SL_ENTER(_("sh_tools_safe_name"));
 
   if (instr)
-    {
-      len = strlen(instr);
-      if (sl_ok_muls (3, len) && sl_ok_adds ((3*len), 4))
-	{
-	  len = (3 * len) + 4;
-	  p = instr;
-	}
-      else
-	{
-	  len = 1;
-	  p   = NULL;
-	}
-    }
+    len = (3 * strlen(instr)) + 4;
   else
-    {
-      p = NULL;
-    }
+    len = 1;
 
   outstr = SH_ALLOC(len);
@@ -256,12 +141,7 @@
   tmp[3]    = '\0';
 
-#if !defined(SH_USE_XML)
-  (void) flag; /* fix compiler warning */
-#endif
-
-  if (!p)
-    goto end;
-
-  while (*p)
+  p = instr;
+
+  while (p && *p)
     {
       c = *p;
@@ -274,53 +154,44 @@
 
 #ifdef SH_USE_XML
-      if (flag == 1)
-	{
-	  if ((*p) == '"')
-	    { 
-	      SH_PUT_4(&outstr[i], '=', '2', '2');
-	      i+=3; ++p;
-	      continue;
-	    } 
-	  else if ((*p) == '&')
-	    { 
-	      SH_PUT_4(&outstr[i], '=', '2', '6');
-	      i+=3; ++p;
-	      continue;
-	    } 
-	  else if ((*p) == '<') 
-	    {     /* left angle       */
-	      SH_PUT_4(&outstr[i], '=', '3', 'c');
-	      i+=3; ++p;
-	      continue;
-	    } 
-	  else if ((*p) == '>') 
-	    {     /* right angle      */
-	      SH_PUT_4(&outstr[i], '=', '3', 'e');
-	      i+=3; ++p;
-	      continue;
-	    }
+      if (flag == 1 && (*p) == '"')
+	{ 
+	  sprintf(&outstr[i], "=%02x", c);   i+=3; ++p;  /* known to fit  */
+	  continue;
+	} 
+      else if (flag == 1 && (*p) == '&')
+	{ 
+	  sprintf(&outstr[i], "=%02x", c);   i+=3; ++p;  /* known to fit  */
+	  continue;
+	} 
+      else if (flag == 1 && (*p) == '<') 
+	{     /* left angle       */
+	  sprintf(&outstr[i], "=%02x", c);   i+=3; ++p;  /* known to fit  */
+	  continue;
+	} 
+      else if (flag == 1 && (*p) == '>') 
+	{     /* right angle      */
+	  sprintf(&outstr[i], "=%02x", c);   i+=3; ++p;  /* known to fit  */
+	  continue;
 	}
+#else
+      (void) flag; /* fix compiler warning */
 #endif
 
       if ( (*p) != '\\' && (*p) != '&' && (*p) != '='  && (*p) != '\'') 
         {
-	  outstr[i] = *p; ++i;
-	  ++p;
-	      
-	  if (c < 32 || c > 126)
+	  if (c < 31 || c > 126)
 	    {
-	      --i;
-	      d = c % 16; c = c / 16;
-	      outstr[i] = '=';       ++i;
-	      outstr[i] = ctable[c]; ++i;
-	      outstr[i] = ctable[d]; ++i;
+	      sprintf(&outstr[i], "=%02x", c); i+=3; ++p;/* known to fit  */
 	    }
-
+	  else
+	    {
+	      outstr[i] = *p;
+	      ++i; ++p;
+	    }
 	  continue;
 	}
       else if ((*p) == '\'')
 	{
-	  SH_PUT_4(&outstr[i], '=', '2', '7');
-	  i+=3; ++p;
+	  sprintf(&outstr[i], "=%02x", c);   i+=3; ++p;  /* known to fit  */
 	}
       else if (*p == '=')
@@ -328,6 +199,5 @@
 	  if (p[1] != '"' && p[1] != '<')
 	    { 
-	      SH_PUT_4(&outstr[i], '=', '3', 'd');
-	      i+=3; ++p;
+	      sprintf(&outstr[i], "=%02x", c); i+=3; ++p;/* known to fit  */
 	    }
 	  else
@@ -342,49 +212,48 @@
 	    break;
 
-
+	  c = *p;
 
 	  switch (*p) {
 	  case '\\':
-	    SH_PUT_4(&outstr[i], '=', '5', 'c');
-	    i+=3; ++p;
+	    sprintf(&outstr[i], "=%02x", c);  i+=3; ++p; /* known to fit  */
 	    break;
 	  case 'n':
-	    SH_PUT_4(&outstr[i], '=', '0', 'a');
+	    sprintf(&outstr[i], "=%02x", '\n');          /* known to fit  */
 	    i+=3; ++p;
 	    break;
 	  case 'b':
-	    SH_PUT_4(&outstr[i], '=', '0', '8');
+	    sprintf(&outstr[i], "=%02x", '\b');          /* known to fit  */
 	    i+=3; ++p;
 	    break;		       
 	  case 'r':		       
-	    SH_PUT_4(&outstr[i], '=', '0', 'd');
+	    sprintf(&outstr[i], "=%02x", '\r');          /* known to fit  */
 	    i+=3; ++p;
 	    break;		       
 	  case 't':		       
-	    SH_PUT_4(&outstr[i], '=', '0', '9');
+	    sprintf(&outstr[i], "=%02x", '\t');          /* known to fit  */
 	    i+=3; ++p;
 	    break;		       
 	  case 'v':		       
-	    SH_PUT_4(&outstr[i], '=', '0', 'b');
+	    sprintf(&outstr[i], "=%02x", '\v');          /* known to fit  */
 	    i+=3; ++p;
 	    break;		       
 	  case 'f':		       
-	    SH_PUT_4(&outstr[i], '=', '0', 'c');
+	    sprintf(&outstr[i], "=%02x", '\f');          /* known to fit  */
 	    i+=3; ++p;
 	    break;		       
 	  case '\'':		       
-	    SH_PUT_4(&outstr[i], '=', '2', '7');
+	    sprintf(&outstr[i], "=%02x", '\'');          /* known to fit  */
 	    i+=3; ++p;
 	    break;		       
-	  case '"':	/* also encode quoted '"' */ 	       
-	    SH_PUT_4(&outstr[i], '=', '2', '2');
+	  case '\"':	/* also encode quoted '"' */ 	       
+	    sprintf(&outstr[i], "=%02x", '\"');          /* known to fit  */
 	    i+=3; ++p;
 	    break;		       
 	  case ' ':		       
-	    SH_PUT_4(&outstr[i], '=', '2', '0');
+	    sprintf(&outstr[i], "=%02x", ' ');           /* known to fit  */
 	    i+=3; ++p;
 	    break;
 	  default:
-	    if (strlen(p) < 3) /* certainly not an octal number, skip */
+	    if (strlen(p) < 3)
 	      {
 		p += strlen(p);
@@ -396,8 +265,6 @@
 		if (val_octal != '\0') { 
 		  c = val_octal;
-		  d = c % 16; c = c / 16;
-		  outstr[i] = '=';       ++i;
-		  outstr[i] = ctable[c]; ++i;
-		  outstr[i] = ctable[d]; ++i;
+		  sprintf(&outstr[i], "=%02x", c);       /* known to fit  */
+		  i+=3;
 		} 
 		p += 3;
@@ -405,16 +272,13 @@
 	  }
 	}
-      else if (*p == '&')
+      else /* *p == '&' */
 	{
 	  ++p;
-	  if (!p || !(*p))
-	    {
-	      outstr[i] = '&'; ++i;
-	      break;
-	    }
+	  if (!p || !(*p)) 
+	    break;
 
 	  if (p[0] == 'a' && p[1] == 'm' && p[2] == 'p' && p[3] == ';')
 	    {
-	      SH_PUT_4(&outstr[i], '=', '2', '6');
+	      sprintf(&outstr[i], "=%02x", '&');         /* known to fit  */
 	      i+=3; p += 4;
 	    }
@@ -422,15 +286,15 @@
 		   p[4] == ';')
 	    {
-	      SH_PUT_4(&outstr[i], '=', '2', '2');
+	      sprintf(&outstr[i], "=%02x", '"');         /* known to fit  */
 	      i+=3; p += 5;
 	    }
 	  else if (p[0] == 'l' && p[1] == 't' && p[2] == ';')
 	    {
-	      SH_PUT_4(&outstr[i], '=', '3', 'c');
+	      sprintf(&outstr[i], "=%02x", '<');         /* known to fit  */
 	      i+=3; p += 3;
 	    }
 	  else if (p[0] == 'g' && p[1] == 't' && p[2] == ';')
 	    {
-	      SH_PUT_4(&outstr[i], '=', '3', 'e');
+	      sprintf(&outstr[i], "=%02x", '>');         /* known to fit  */
 	      i+=3; p += 3;
 	    }
@@ -439,13 +303,6 @@
 	      outstr[i] = '&'; ++i;
 	    }
-	}
-      else
-	{
-	  outstr[i] = *p; ++i;
-	  ++p;
-	}
+	}     
     } /* while (p && *p) */
-
- end:
   
   outstr[i] = '\0';
@@ -456,26 +313,35 @@
 /* extern int h_errno; */ 
 
-char * sh_tools_errmessage (int tellme, char * errbuf, size_t len)
-{
-  char * p = NULL;
+char * sh_tools_errmessage (int tellme)
+{
+
 #ifdef HOST_NOT_FOUND
     if (tellme == HOST_NOT_FOUND)  
-      p = _("The specified host is unknown: ");
+      return _("The specified host is unknown: ");
 #endif
 #ifdef NO_ADDRESS
     if (tellme == NO_ADDRESS)  
-      p = _("The requested name is valid but does not have an IP address: ");
+      return _("The requested name is valid but does not have an IP address: ");
 #endif
 #ifdef NO_RECOVERY
     if (tellme == NO_RECOVERY)  
-      p = _("A non-recoverable name server error occurred: ");
+      return _("A non-recoverable name server error occurred: ");
 #endif
 #ifdef TRY_AGAIN
     if (tellme == TRY_AGAIN)  
-      p = _("A temporary error occurred on an authoritative name server. The specified host is unknown: ");
-#endif
-    if (!p) p =  _("Unknown error");
-    sl_strlcpy(errbuf, p, len);
-    return errbuf;
+      return _("A temporary error occurred on an authoritative name server. The specified host is unknown: ");
+#endif
+    return _("Unknown error");
+}
+
+int is_numeric (const char * address)
+{
+  int j;
+  int len = sl_strlen(address);
+  
+  for (j = 0; j < len; ++j)
+    if ( (address[j] < '0' || address[j] > '9') && address[j] != '.')
+      return (1 == 0);
+  return (1 == 1);
 }
 
@@ -509,5 +375,5 @@
 typedef struct _sin_cache {
   char * address;
-  struct sh_sockaddr  saddr;
+  struct sockaddr_in sin;
   struct _sin_cache * next;
 } sin_cache;
@@ -519,5 +385,5 @@
 {
   sin_cache * check_cache = conn_cache;
-  sin_cache * old_entry;
+  sin_cache * old_entry   = conn_cache;
 
   SL_ENTER(_("delete_cache"));
@@ -539,10 +405,9 @@
 int DoReverseLookup = S_TRUE;
 
-int set_reverse_lookup (const char * c)
+int set_reverse_lookup (char * c)
 {
   return sh_util_flagval(c, &DoReverseLookup);
 }
 
-#if !defined(USE_IPVX)
 int connect_port (char * address, int port, 
 		  char * ecall, int * errnum, char * errmsg, int errsiz)
@@ -555,20 +420,14 @@
   char   * host_name;
 
-  volatile int    fd = (-1);
+  int    fd = (-1);
   int    status;
-  volatile int    fail   = 0;
+  int    fail   = 0;
   int    cached = 0;
 
   int    retval;
-  char   errbuf[SH_ERRBUF_SIZE];
 
   sin_cache * check_cache = conn_cache;
 
   SL_ENTER(_("connect_port"));
-
-  if (tools_debug)
-    fprintf(stderr, _("-00- <%s> <%d> no IPv6 support\n"), address, port);
-
-  if (errsiz > 0) errmsg[0] = '\0';
 
   /* paranoia -- should not happen
@@ -581,26 +440,12 @@
       while (check_cache && check_cache->address)
 	{
-          if (tools_debug)
-            fprintf(stderr, _("-01- <%s> <%s>\n"), 
-                    address, check_cache->address);
-
 	  if ( 0 == sl_strncmp(check_cache->address, 
-			       address, sl_strlen(address)) )
+			       address, sl_strlen(address)))
 	    {
-	      memcpy (&sinr, &((check_cache->saddr).sin), sizeof(struct sockaddr_in));
+	      memcpy (&sinr, &(check_cache->sin), sizeof(struct sockaddr_in));
 	      sinr.sin_family = AF_INET;
 	      sinr.sin_port   = htons (port);
 	      cached = 1;
 	      break;
-	    }
-	  if (tools_debug)
-	    {
-	      char eaddr[SH_IP_BUF];
-	      sl_strlcpy(eaddr, 
-			 inet_ntoa(*(struct in_addr *) &(sinr.sin_addr)), 
-			 sizeof(eaddr));
-	      fprintf(stderr, _("-02- <AF_INET> <%s> <%d> <%d>\n"), 
-		      eaddr,
-		      port, cached);
 	    }
 	  if (check_cache->next)
@@ -615,6 +460,4 @@
   if (cached == 0)
     {
-      if (tools_debug)
-        fputs(_("-03- not cached\n"), stderr); 
 #ifdef HAVE_INET_ATON
       if (0 == inet_aton(address, &haddr))
@@ -623,8 +466,5 @@
 #endif
 	{
-	  SH_MUTEX_LOCK(mutex_resolv);
-
-	  host_name = NULL;
-
+      
 	  host_entry = sh_gethostbyname(address);
 
@@ -637,5 +477,5 @@
 	      *errnum = 666;
 #endif
-	      (void) sh_tools_errmessage (*errnum, errmsg, errsiz);
+	      sl_strlcpy(errmsg, sh_tools_errmessage (*errnum), errsiz);
 	      sl_strlcat(errmsg, address, errsiz); 
 	      fail = (-1);
@@ -647,11 +487,4 @@
 	      sinr.sin_addr   = *(struct in_addr *) host_entry->h_addr;
 
-	      if (tools_debug)
-                fprintf(stderr, 
-			_("-04- <%s> <%s> hostent->h_name %s <%s> hostent->h_addr\n"), 
-                        address, 
-			(host_entry->h_name == NULL) ? _("NULL") : host_entry->h_name,
-			(host_entry->h_addrtype == AF_INET) ? _("AF_INET") : _("AF_INET6"),
-			inet_ntoa(*(struct in_addr *) &(sinr.sin_addr)));
 
 	      /* reverse DNS lookup
@@ -666,5 +499,10 @@
 		  else
 		    {
-		      host_name = sh_util_strdup(host_entry->h_name);
+		      host_name = SH_ALLOC(sl_strlen(host_entry->h_name) + 1);
+		      if (sl_strlen(host_entry->h_name) > 0)
+			strcpy(host_name,                /* known to fit  */
+			       host_entry->h_name);
+		      else
+			host_name[0] = '\0';
 		    }
 
@@ -680,5 +518,6 @@
 		      *errnum = 666;
 #endif
-		      (void) sh_tools_errmessage (*errnum, errmsg, errsiz);
+		      sl_strlcpy(errmsg, 
+				 sh_tools_errmessage (*errnum), errsiz);
 		      sl_strlcat(errmsg, 
 				 inet_ntoa (*(struct in_addr *) &(sinr.sin_addr)),
@@ -690,5 +529,5 @@
 		      *errnum = 0;
 		      if (sl_strlen(host_entry->h_name) == 0 || 
-			  (*errnum = sl_strcasecmp(host_name,host_entry->h_name)) != 0)
+			  (*errnum = sl_strcmp(host_name,host_entry->h_name)) != 0)
 			{ 
 			  if (*errnum)
@@ -696,5 +535,5 @@
 			  else
 			    sl_strlcpy(ecall, _("strlen"), SH_MINIBUF);
-			  sl_strlcpy(errmsg, _("Reverse lookup failed: "), 
+			  sl_strlcpy(errmsg, _("Reverse lookup failed."), 
 				     errsiz);
 			  sl_strlcat(errmsg, address, errsiz);
@@ -706,8 +545,8 @@
 			}
 		    }
+
+		  SH_FREE(host_name);
 		}
 	    }
-	  SH_MUTEX_UNLOCK(mutex_resolv);
-	  if (host_name) SH_FREE(host_name);
 	}
   
@@ -717,10 +556,4 @@
 	  sinr.sin_port   = htons (port);
 	  sinr.sin_addr   = haddr;
-
-	  if (tools_debug)
-	    fprintf(stderr, 
-		    _("-04- <%s> is_numeric AF_INET <%s> \n"), 
-		    address, 
-		    inet_ntoa(*(struct in_addr *) &(sinr.sin_addr)));
 	}
 
@@ -733,7 +566,5 @@
 	  check_cache->address = SH_ALLOC(sl_strlen(address) + 1);
 	  sl_strlcpy (check_cache->address, address, sl_strlen(address) + 1);
-
-	  sh_ipvx_save(&(check_cache->saddr), AF_INET, (struct sockaddr *) &sinr);
-
+	  memcpy(&(check_cache->sin), &sinr, sizeof(struct sockaddr_in));
 	  ++cached_addr;
 	  
@@ -763,5 +594,5 @@
 	sl_strlcpy(ecall, _("socket"), SH_MINIBUF);
 	*errnum = status;
-	sl_strlcpy(errmsg, sh_error_message (status, errbuf, sizeof(errbuf)), errsiz);
+	sl_strlcpy(errmsg, sh_error_message (status), errsiz);
 	sl_strlcat(errmsg, _(", address "), errsiz);
 	sl_strlcat(errmsg, address, errsiz);
@@ -777,11 +608,8 @@
 	sl_strlcpy(ecall, _("connect"), SH_MINIBUF);
 	*errnum = status;
-	sl_strlcpy(errmsg, sh_error_message (status, errbuf, sizeof(errbuf)), errsiz);
-	sl_strlcat(errmsg, 
-		   (sinr.sin_family == AF_INET) ? _(", AF_INET ") : _(", AF_INET6 "),
-		   errsiz);
+	sl_strlcpy(errmsg, sh_error_message (status), errsiz);
 	sl_strlcat(errmsg, _(", address "), errsiz);
 	sl_strlcat(errmsg, address, errsiz);
-	sl_close_fd(FIL__, __LINE__, fd);
+	close(fd);
 	fail = (-1); 
       }
@@ -791,291 +619,4 @@
   SL_RETURN(retval, _("connect_port"));
 }
-#else
-int connect_port (char * address, int port, 
-		  char * ecall, int * errnum, char * errmsg, int errsiz)
-{
-  struct sockaddr_in *sin;
-  struct sockaddr_in6 *sin6;
-  struct sh_sockaddr ss;
-  sin_cache * check_cache = conn_cache;
-  int    cached = 0;
-  int    fail   = 0;
-  int    fd     = -1;
-  int    status = 0;
-
-  int    retval;
-  char   errbuf[SH_ERRBUF_SIZE];
-
-  SL_ENTER(_("connect_port"));
-
-  /* paranoia -- should not happen
-   */
-  if (cached_addr > 128)
-    delete_cache();
-
-  if (tools_debug)
-    fprintf(stderr, _("-00- <%s> <%d>\n"), address, port);
-
-  if (check_cache != NULL)
-    {
-      while (check_cache && check_cache->address)
-	{
-	  if (tools_debug)
-	    fprintf(stderr, _("-01- <%s> <%s>\n"), 
-		    address, check_cache->address);
-
-	  if ( 0 == sl_strcmp(check_cache->address, address) )
-	    {
-	      memcpy (&ss, &(check_cache->saddr), sizeof(struct sh_sockaddr));
-	      switch (ss.ss_family) 
-		{
-		case AF_INET:
-		  sin = &(ss.sin);
-		  sin->sin_port   = htons (port);
-		  cached = 1;
-		  break;
-		case AF_INET6:
-		  sin6 = &(ss.sin6);
-		  sin6->sin6_port  = htons (port);
-		  cached = 1;
-		  break;
-		default:
-		  break;
-		}
-	      if (tools_debug)
-		{
-		  char eaddr[SH_IP_BUF];
-		  sh_ipvx_ntoa(eaddr, sizeof(eaddr), &ss);
-		  fprintf(stderr, _("-02- <%s> <%s> <%d> <%d>\n"), 
-			  (ss.ss_family == AF_INET) ? _("AF_INET") : _("AF_INET6"),
-			  eaddr,
-			  port, cached);
-		}
-	      break;
-	    }
-	  if (check_cache->next)
-	    check_cache = check_cache->next;
-	  else
-	    check_cache = NULL;
-	}
-    }
-
-  if (cached != 0)
-    {
-      if (tools_debug)
-	fputs(_("-03- cached\n"), stderr); 
-      /* cppcheck-suppress uninitStructMember */
-      fd = socket(ss.ss_family, SOCK_STREAM, 0);
-      if (fd < 0) 
-	{
-	  status = errno;
-	  fail   = (-1);
-	  sl_strlcpy(ecall, _("socket"), SH_MINIBUF);
-	  *errnum = status;
-	  sl_strlcpy(errmsg, sh_error_message (status, errbuf, sizeof(errbuf)), errsiz);
-	  sl_strlcat(errmsg, _(", address "), errsiz);
-	  sl_strlcat(errmsg, address, errsiz);
-	}
-
-
-      if (fail != (-1)) 
-	{
-	  /* cppcheck-suppress uninitStructMember */
-	  int addrlen = SH_SS_LEN(ss);
-
-	  if ( retry_connect(FIL__, __LINE__, fd, 
-			     sh_ipvx_sockaddr_cast(&ss), addrlen) < 0) 
-	    {
-	      status = errno;
-	      sl_strlcpy(ecall, _("connect"), SH_MINIBUF);
-	      *errnum = status;
-	      sl_strlcpy(errmsg, sh_error_message (status, errbuf, sizeof(errbuf)), errsiz);
-	      sl_strlcat(errmsg, _(", address "), errsiz);
-	      sl_strlcat(errmsg, address, errsiz);
-	      sl_close_fd(FIL__, __LINE__, fd);
-	      fail = (-1); 
-	    }
-	}
-
-      if (fail != 0)
-	{
-	  delete_cache();
-	  cached = 0;
-	}
-    }
-
-  if (cached == 0)
-    {
-      int    res;
-      char   sport[32];
-      struct addrinfo *ai;
-      struct addrinfo hints;
-
-      if (tools_debug)
-	fputs(_("-03- not cached\n"), stderr);
-
-      memset (&hints, 0, sizeof (hints));
-      hints.ai_flags = AI_ADDRCONFIG;
-#if defined(AI_CANONNAME)
-      hints.ai_flags |= AI_CANONNAME;
-#endif 
-      hints.ai_family   = AF_UNSPEC;
-      hints.ai_socktype = SOCK_STREAM;
-      sl_snprintf(sport, sizeof(sport), "%d", port);
-
-      res = getaddrinfo (address, sport, &hints, &ai);
-      if (res != 0)
-	{
-	  fail = (-1);
-	  status = errno;
-	  sl_strlcpy(ecall, _("getaddrinfo"), SH_MINIBUF);
-	  *errnum = status;
-	  sl_strlcpy(errmsg, gai_strerror (res), errsiz);
-	  sl_strlcat(errmsg, _(", address "), errsiz);
-	  sl_strlcat(errmsg, address, errsiz);
-	}
-
-      if (fail != (-1) && (DoReverseLookup == S_TRUE) && !sh_ipvx_is_numeric(address))
-	{
-	  struct addrinfo *p = ai;
-	  int    success = 0;
-	  char hostname[SH_BUFSIZE];
-	  const char * canonical;
-
-
-#if defined(AI_CANONNAME)
-	  if (ai->ai_canonname && strlen(ai->ai_canonname) > 0)
-	    {
-	      canonical = ai->ai_canonname;
-	      if (tools_debug)
-		fprintf(stderr, _("-04- <%s> <%s> ai->ai_canonname\n"), 
-			address, canonical);
-	    }
-	  else
-	    {
-	      canonical = address;
-	      if (tools_debug)
-		fprintf(stderr, _("-04- <%s> <%s> defined ai_canonname\n"), 
-			address, canonical);
-	    }
-#else
-	  canonical = address;
-	  if (tools_debug)
-	    fprintf(stderr, _("-04- <%s> <%s> not defined ai_canonname\n"), 
-		    address, canonical);
-#endif
-
-	  while (p != NULL)
-	    {
-	      int e = getnameinfo (p->ai_addr, p->ai_addrlen, 
-				   hostname, sizeof(hostname),
-				   NULL, 0, NI_NAMEREQD);
-	      
-	      if (e == 0)
-		{
-		  if (tools_debug)
-		    {
-		      fprintf(stderr, _("-05- <%s> <%s> <%s>\n"), 
-			      (p->ai_family == AF_INET) ? _("AF_INET") : _("AF_INET6"),
-			      sh_ipvx_print_sockaddr (p->ai_addr, p->ai_family),
-			      hostname);
-		    }
-
-		  if (sl_strcasecmp(hostname, canonical) == 0)
-		    {
-		      if (tools_debug)
-			fprintf(stderr, _("-06- <%s> <%s> match\n"), 
-				hostname, canonical);
-		      success = 1;
-		      break;
-		    }
-
-		}
-	    
-	      p = p->ai_next;
-	    }
-
-	  if (success == 0)
-	    {
-	      sl_strlcpy(ecall, _("strcmp"), SH_MINIBUF);
-	      sl_strlcpy(errmsg, _("Reverse lookup failed: "), 
-			 errsiz);
-	      sl_strlcat(errmsg, address, errsiz);
-	      fail = -1;
-	      freeaddrinfo (ai);
-	    }
-	}
-
-      if (fail != (-1))
-	{
-	  struct addrinfo *p = ai;
-
-	  while (p != NULL)
-	    {
-	      if ( (SOCK_STREAM == p->ai_socktype) &&
-		   ((p->ai_family == AF_INET) || (p->ai_family == AF_INET6)) )
-		{
-		
-		  fd = socket(p->ai_family, SOCK_STREAM, 0);
-		  
-		  if (fd != (-1))
-		    {
-		      if (retry_connect(FIL__, __LINE__, fd, 
-					p->ai_addr, p->ai_addrlen) >= 0)
-			{
-			  /* put it into the cache
-			   */
-			  check_cache          = SH_ALLOC(sizeof(sin_cache));
-			  check_cache->address = SH_ALLOC(sl_strlen(address) + 1);
-			  sl_strlcpy (check_cache->address, address, sl_strlen(address) + 1);
-			  
-			  sh_ipvx_save(&(check_cache->saddr), p->ai_family, p->ai_addr);
-			  
-			  ++cached_addr;
-			  
-			  if (conn_cache)
-			    {
-			      if (conn_cache->next)
-				check_cache->next    = conn_cache->next;
-			      else
-				check_cache->next    = NULL;
-			      conn_cache->next     = check_cache;
-			    }
-			  else
-			    {
-			      check_cache->next    = NULL;
-			      conn_cache           = check_cache;
-			    }
-			  
-			  freeaddrinfo (ai);
-			  goto end;
-			}
-		      status = errno;
-		      sl_close_fd(FIL__, __LINE__, fd);
-		    }
-		  else
-		    {
-		      status = errno;
-		    }
-		}
-	      p = p->ai_next;
-	    }
-	  fail = (-1);
-	  freeaddrinfo (ai);
-
-	  sl_strlcpy(ecall, _("connect"), SH_MINIBUF);
-	  *errnum = status;
-	  sl_strlcpy(errmsg, sh_error_message (status, errbuf, sizeof(errbuf)), errsiz);
-	  sl_strlcat(errmsg, _(", address "), errsiz);
-	  sl_strlcat(errmsg, address, errsiz);
-	}
-    }
-
- end:
-  retval = (fail < 0) ? (-1) : fd;
-  SL_RETURN(retval, _("connect_port"));
-
-}
-#endif
 
 int connect_port_2 (char * address1, char * address2, int port, 
@@ -1117,5 +658,5 @@
 }
 
-#if defined(HAVE_NTIME) || defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
+#if defined(HAVE_NTIME) || defined(SH_WITH_CLIENT)
 static
 int sh_write_select(int type, int sockfd, 
@@ -1129,8 +670,14 @@
   int    num_sel;
   
-  char    errbuf[SH_ERRBUF_SIZE];
+  struct  sigaction  new_act;
+  struct  sigaction  old_act;
 
   SL_ENTER(_("sh_write_select"));
 
+  /* ignore SIGPIPE (instead get EPIPE if connection is closed)
+   */
+  new_act.sa_handler = SIG_IGN;
+  sigaction (SIGPIPE, &new_act, &old_act);
+  
   FD_ZERO(&fds);
   FD_SET(sockfd, &fds);
@@ -1157,13 +704,10 @@
 		continue;
 	      }
-	    if ( errno == EINTR || errno == EINPROGRESS ) /* try again */
+	    if ( errno == EINTR) /* try again */
 	      continue;
 	    *w_error = errno;
-
-	    sh_error_message(*w_error, errbuf, sizeof(errbuf));
-	    sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, errno, MSG_E_SUBGEN,
-			     errbuf,
-			     _("sh_write_select (ws)") ); 
-	    TPT(( 0, FIL__, __LINE__, _("msg=<select: %s>\n"), errbuf ));
+	    TPT(( 0, FIL__, __LINE__, _("msg=<select: %s>\n"), 
+		  sh_error_message(*w_error)));
+	    sigaction (SIGPIPE, &old_act, NULL);
 	    SL_RETURN( countbytes, _("sh_write_select"));
 	  }
@@ -1178,13 +722,10 @@
 		continue;
 	      }
-	    if ( errno == EINTR || errno == EINPROGRESS ) /* try again */
+	    if ( errno == EINTR ) /* try again */
 	      continue;
 	    *w_error = errno;
-
-	    sh_error_message(*w_error, errbuf, sizeof(errbuf));
-	    sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, errno, MSG_E_SUBGEN,
-			     errbuf,
-			     _("sh_write_select (rs)") ); 
-	    TPT(( 0, FIL__, __LINE__, _("msg=<select: %s>\n"), errbuf ));
+	    TPT(( 0, FIL__, __LINE__, _("msg=<select: %s>\n"), 
+		  sh_error_message(*w_error)));
+	    sigaction (SIGPIPE, &old_act, NULL);
 	    SL_RETURN( countbytes, _("sh_write_select"));
 	  }
@@ -1210,5 +751,5 @@
 	    *w_error = 0;
 #endif
-
+	    sigaction (SIGPIPE, &old_act, NULL);
 	    TPT(( 0, FIL__, __LINE__, _("msg=<Timeout>\n")));
 	    SL_RETURN( countbytes, _("sh_write_select"));
@@ -1236,10 +777,5 @@
 	  {
 	    *w_error = errno;
-
-	    sh_error_message(*w_error, errbuf, sizeof(errbuf));
-	    sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, errno, MSG_E_SUBGEN,
-			     errbuf,
-			     (type == SH_DO_WRITE) ? 
-			     _("sh_write_select (w)") : _("sh_write_select (r)")); 
+	    sigaction (SIGPIPE, &old_act, NULL);
 	    TPT(( 0, FIL__, __LINE__, _("msg=<count < 0>\n")));
 	    SL_RETURN( countbytes, _("sh_write_select"));
@@ -1248,5 +784,5 @@
 	  {
 	    *w_error = errno;
-
+	    sigaction (SIGPIPE, &old_act, NULL);
 	    TPT(( 0, FIL__, __LINE__, _("msg=<count == 0>\n")));
 	    SL_RETURN( countbytes, _("sh_write_select"));
@@ -1255,4 +791,9 @@
   }
 
+
+  /* restore signal handler
+   */
+  sigaction (SIGPIPE, &old_act, NULL);
+
   *w_error = 0;
 
@@ -1262,9 +803,10 @@
 #endif
 
-#if defined (SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
+#if defined (SH_WITH_CLIENT)
 unsigned long write_port (int sockfd, char *buf, unsigned long nbytes, 
 			  int * w_error, int timeout)
 {
   unsigned long bytes;
+  char errmsg[256];
 
   SL_ENTER(_("write_port"));
@@ -1273,8 +815,7 @@
   if (*w_error != 0)
     {
-      char errbuf[SH_ERRBUF_SIZE];
+      sl_strlcpy(errmsg, sh_error_message (*w_error), sizeof(errmsg));
       sh_error_handle((-1), FIL__, __LINE__, *w_error, MSG_TCP_NETRP, 
-		      sh_error_message (*w_error, errbuf, sizeof(errbuf)),
-		      (long) sockfd, _("write_port"));
+		      errmsg, (long) sockfd, _("write_port"));
     }
   SL_RETURN( bytes, _("write_port"));
@@ -1282,5 +823,5 @@
 #endif
 
-#if defined(HAVE_NTIME) || defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
+#if defined(HAVE_NTIME) || defined(SH_WITH_CLIENT)
 
 unsigned long read_port (int sockfd, char *buf, unsigned long nbytes, 
@@ -1288,4 +829,5 @@
 {
   unsigned long bytes;
+  char errmsg[256];
 
   SL_ENTER(_("read_port"));
@@ -1294,8 +836,7 @@
   if (*w_error != 0)
     {
-      char errbuf[SH_ERRBUF_SIZE];
+      sl_strlcpy(errmsg, sh_error_message (*w_error), sizeof(errmsg));
       sh_error_handle((-1), FIL__, __LINE__, *w_error, MSG_TCP_NETRP, 
-		      sh_error_message (*w_error, errbuf, sizeof(errbuf)),
-		      (long) sockfd, _("read_port"));
+		      errmsg, (long) sockfd, _("read_port"));
     }
   SL_RETURN( bytes, _("read_port"));
@@ -1318,5 +859,5 @@
 #endif
 
-#if defined (SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
+#if defined (SH_WITH_CLIENT)
 
 int check_request (char * have, char * need)
@@ -1372,95 +913,9 @@
 #endif
 
-#if defined (SH_WITH_CLIENT) || defined (SH_WITH_SERVER)
-
 #if defined (SH_WITH_CLIENT)
 
-static int           probe_done = S_FALSE;
-static unsigned char probe_flag = '\0';
-
-void sh_tools_probe_reset()
-{
-  probe_done = S_FALSE;
-  probe_flag = '\0';
-  return;
-}
-
-#ifdef SH_ENCRYPT
-static int probe_ok(int flag)
-{
-  (void) flag;
-  if ((probe_flag & SH_PROTO_IVA) != 0)
-    return S_TRUE;
-  return S_FALSE;
-}
-#endif
-
-static unsigned char probe_header_set(unsigned char protocol)
-{
-  if (probe_done || (protocol & SH_PROTO_SRP) == 0)
-    return 0;
-
-  return (char) SH_PROTO_IVA;
-}
-
-static void probe_header_get(unsigned char protocol)
-{
-  if (probe_done || (protocol & SH_PROTO_SRP) == 0)
-    return;
-
-  /* If the server doesn't know about it,
-   * it will simply mirror it back. */
-  
-  if ((protocol & SH_PROTO_IVA) != 0)
-    {
-      /* probe was mirrored */;
-    }
-  else
-    {
-      /* probe was UNset */
-      probe_flag |= SH_PROTO_IVA;
-    }
-  probe_done = S_TRUE;
-  return;
-}
-
-#else
-static unsigned char probe_header_set(unsigned char protocol) { 
-  (void) protocol; return 0; }
-static void probe_header_get(unsigned char protocol) { 
-  (void) protocol; return; }
-void sh_tools_probe_reset() { return; }
-
-unsigned char sh_tools_probe_store(unsigned char protocol, int * probe_flag)
-{
-  if ((protocol & SH_PROTO_SRP) == 0)
-    return protocol;
-
-  if ((protocol & SH_PROTO_IVA) != 0)
-    {
-      /* probe received */
-      *probe_flag |=  SH_PROTO_IVA;
-      protocol    &= ~SH_PROTO_IVA;
-    }
-  return protocol; 
-}
-
-#ifdef SH_ENCRYPT
-static int probe_ok(int flag)
-{
-  if ((flag & SH_PROTO_IVA) != 0)
-    return S_TRUE;
-  return S_FALSE;
-}
-#endif
-
-#endif
-
-
 void get_header (unsigned char * head, unsigned long * bytes, char * u)
 {
   SL_ENTER(_("get_header"));
-
-  probe_header_get(head[0]);
 
   *bytes = 
@@ -1482,5 +937,5 @@
 #if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
 
-#ifdef  SH_ENCRYPT
+#ifdef  SH_ENCRYPT_2
 #define TRANS_BYTES 65120
 #else
@@ -1488,8 +943,7 @@
 #endif
 
-void put_header (unsigned char * head, const int protocol, 
+void put_header (unsigned char * head, int protocol, 
 		 unsigned long * length, char * u)
 {
-  unsigned char probe = probe_header_set(protocol);
 
   /* static long transfer_limit = (8 * SH_BUFSIZE); V0.8 */
@@ -1498,5 +952,5 @@
   SL_ENTER(_("put_header"));
 
-  head[0]   = protocol|probe;
+  head[0]   = protocol;
 
   ASSERT((*length < transfer_limit), _("*length < transfer_limit"))
@@ -1524,4 +978,5 @@
   SL_RET0(_("put_header"));
 }
+#endif
 
 /* ------------------------------------------
@@ -1538,10 +993,13 @@
  * (msg_size = payload_size - key_len = payload_size - 48)
  */ 
-
-/* 
- * only SH_V2_FULLSIZE is used, and only once
- */
-
+#ifdef SH_WITH_SERVER
+#define SH_V2_FULLSIZE  240
+#define SH_V2_PAYLOAD   224
+#define SH_V2_MESSAGE   176
+#else
 #define SH_V2_FULLSIZE 1024
+#define SH_V2_PAYLOAD  1008
+#define SH_V2_MESSAGE   960
+#endif
 
 #ifdef SH_ENCRYPT
@@ -1577,8 +1035,8 @@
       SH_IS_ASCII(head[5]) && isalpha(head[5]) &&			   
       SH_IS_ASCII(head[6]) && isalpha(head[6])) {
-    fprintf(stderr, _("%c %3o %s %5d  %c  %c  %c  %c\n"), sign,
+    fprintf(stderr, "%c %3o %s %5d  %c  %c  %c  %c\n", sign,
 	    head[0], code, msg_size, head[3], head[4], head[5], head[6]); 
   } else {
-    fprintf(stderr, _("%c %3o %s %5d %2X %2X %2X %2X\n"), sign,
+    fprintf(stderr, "%c %3o %s %5d %2X %2X %2X %2X\n", sign,
 	    head[0], code, msg_size, head[3], head[4], head[5], head[6]); 
   }
@@ -1587,5 +1045,4 @@
 
 #ifdef SH_ENCRYPT
-
 /*
  * #define DEBUG_EN2
@@ -1594,29 +1051,25 @@
  * last 4 bytes of outgoing header are set to dummy value
  */
-char * sh_tools_makePack (unsigned char * header, int flag,
-			  char * payload, unsigned long payload_size,
+char * sh_tools_makePack (unsigned char * header, 
+			  char * payload, int payload_size,
 			  keyInstance * keyInstE)
 {
-  BYTE            inBlock[B_SIZ]; 
-  BYTE            outBlock[B_SIZ];
-  char            ivBlock[B_SIZ];
-
-  UINT32          rpad[3];
+  UINT32 rpad[3];
   unsigned char   head[16];
-  double          epad;
-  unsigned long   i_epad = 0;
-  unsigned long   i_blk = payload_size / 16;
-  unsigned long   i_blkmax = SH_V2_FULLSIZE / 16;
-  unsigned long   pads = 0;
-  size_t          full_size;
-  char          * full_ret;
-
-  unsigned char * p;
-  int             j;
-  cipherInstance  cipherInst;
-  int             err_num;
-  int             blkfac;
-  int             oflow = 0;
-  char expbuf[SH_ERRBUF_SIZE];
+  double epad;
+  int    i_epad = 0;
+  int    i_blk = payload_size / 16;
+  int    i_blkmax = SH_V2_FULLSIZE / 16;
+  int    pads = 0;
+  int    full_size;
+  char * full_ret;
+
+  char                  * p;
+  RIJ_BYTE                    inBlock[B_SIZ]; 
+  RIJ_BYTE                    outBlock[B_SIZ];
+  int                     j;
+  cipherInstance          cipherInst;
+  int                     err_num;
+  int                     blkfac;
 
   /* 
@@ -1624,13 +1077,19 @@
   */
   if ((i_blk * 16) != payload_size) ++i_blk;
-
+#ifdef DEBUG_EN2
+  fprintf(stderr, "SEND <%d> blocks <%d>\n", payload_size, i_blk);
+#endif
   /* random_pad
    */
-  rpad[1] = taus_get ();
+  rpad[1] = taus_get (&(skey->rng0[0]), &(skey->rng1[0]), &(skey->rng2[0]));
   memcpy (head,      &rpad[1],    4);
-  rpad[0] = taus_get ();
+  rpad[0] = taus_get (&(skey->rng0[0]), &(skey->rng1[0]), &(skey->rng2[0]));
   memcpy (&head[4],  &rpad[0],    4);
-  rpad[2] = taus_get ();
+  rpad[2] = taus_get (&(skey->rng0[0]), &(skey->rng1[0]), &(skey->rng2[0]));
   memcpy (&head[8],  &rpad[2],    4);
+
+  /* protocol
+   */
+  /* memcpy (&head[8],  &header[3], 4); */
 
   /* size (payload)
@@ -1643,38 +1102,39 @@
   if (i_blk < i_blkmax) 
   {
-    pads   = i_blkmax - i_blk;
-    epad   = taus_get_double (&rpad);
-    i_epad = (unsigned long) (pads * epad);
+    pads = i_blkmax - i_blk;
+    /* memcpy((char *) &rpad[2], &head[12], 4); */
+    epad = taus_get_double (&rpad);
+#ifdef DEBUG_EN2
+    fprintf(stderr, "PAD1 <%d> <%f>\n", pads, epad);
+#endif
+    i_epad = (int) (pads * epad);
+#ifdef DEBUG_EN2
+    fprintf(stderr, "PAD2 <%d> <%d>\n", i_epad, (i_epad*16));
+#endif
   }
 
-  full_size =  16;                        /* head     */
-  if (sl_ok_muls(i_blk, 16) && sl_ok_adds(full_size, (i_blk*16)))
-    full_size =  full_size + (i_blk*16);  /* payload  */
-  else
-    oflow = 1;
-  if (sl_ok_adds(full_size, (i_epad*16)))
-    full_size =  full_size + (i_epad*16); /* pad      */
-  else
-    i_epad = 0;
-
-  if (oflow)
-    {
-      sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
-		      _("integer overflow"), 
-		      _("sh_tools_makePack"));
-    }
+  full_size = 
+  /* head     */ 16 + 
+  /* payload  */ (i_blk*16) + /* payload_size + */ 
+  /* pad      */ (i_epad * 16);
 
   full_ret = SH_ALLOC(full_size);
-
   memcpy(full_ret,                   head,    16);
-  if (payload != NULL && !oflow)
-    memcpy(&full_ret[16],              payload, payload_size);
-
-  if ((i_blk*16) > payload_size && !oflow) 
-    {
-      memset(&full_ret[16+payload_size], 0, (i_blk*16) - payload_size);
+  if (payload != NULL)
+    {
+      memcpy(&full_ret[16],              payload, payload_size);
+    }
+  if ((i_blk*16) > payload_size) 
+    {
+#ifdef DEBUG_EN2
+      fprintf(stderr, "SEN2 <%d>\n", (i_blk*16) - payload_size);
+#endif
+      memset(&full_ret[16+payload_size], '\0', (i_blk*16) - payload_size);
       payload_size = i_blk * 16;
     }
-  memset(&full_ret[16+payload_size], 0, i_epad*16);
+  memset(&full_ret[16+payload_size], '\0', i_epad*16);
+#ifdef DEBUG_EN2
+  fprintf(stderr, "SEN3 <%d> <%d>\n", full_size, i_epad*16);
+#endif
 
   /* rewrite header
@@ -1682,46 +1142,31 @@
   header[1]   = (unsigned int)(full_size/256);
   header[2]   = (unsigned int)(full_size - (256 * header[1]));
-
-  p      = (unsigned char *) full_ret; 
-  blkfac = full_size / B_SIZ;
-
-  err_num = rijndael_cipherInit (&cipherInst, MODE_CBC, NULL);
+  /* don't erase protocol from header 
+     memset(&header[3], '\0', 4);
+  */
+  p = full_ret; blkfac = full_size / 16;
+
+  err_num = cipherInit (&cipherInst, MODE_CBC, NULL);
   
-  if (err_num < 0) {
-    sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
-		    errorExplain(err_num, expbuf, sizeof(expbuf)), 
-		    _("sh_tools_makePack: rijndael_cipherInit")); }
-
-  if (probe_ok(flag)) {
-    memcpy(inBlock, p, B_SIZ);
-    err_num = rijndael_blockEncrypt(&cipherInst, keyInstE, 
-				    inBlock, 128, outBlock);
-    if (err_num >= 0) {
-      memcpy(p, outBlock, B_SIZ); p += B_SIZ;
-      memcpy(ivBlock, outBlock, sizeof(ivBlock));
-      err_num = rijndael_cipherInit (&cipherInst, MODE_CBC, ivBlock);
-      if (err_num >= 0) {
-	err_num = rijndael_blockEncrypt(&cipherInst, keyInstE, 
-					p, 128*(blkfac-1), p);
-      }
-    }
-  }
-
-  else {
-    for (j = 0; j < blkfac; ++j) {
+  if (err_num < 0) 
+    {
+      sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
+		      errorExplain(err_num), 
+		      _("sh_tools_makePack: cipherInit"));
+    }
+  for (j = 0; j < blkfac; ++j)
+    {
       memcpy(inBlock, p, B_SIZ);
-      err_num = rijndael_blockEncrypt(&cipherInst, keyInstE, 
-				      inBlock, 128, outBlock);
-      
-      if (err_num < 0) {
-	sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
-			errorExplain(err_num, expbuf, sizeof(expbuf)), 
-			_("sh_tools_makePack: rijndael_blockEncrypt"));
-	break; }
-      
+      err_num = blockEncrypt(&cipherInst, keyInstE, 
+			     inBlock, 128 * BNUM, outBlock);
+      if (err_num < 0)
+	{
+	  sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
+			  errorExplain(err_num), 
+			  _("sh_tools_makePack: blockEncrypt"));
+	}
       memcpy(p, outBlock, B_SIZ);
       p += B_SIZ;
     }
-  }
 
   return full_ret;
@@ -1731,68 +1176,55 @@
  * last 4 bytes of incoming header are dummy
  */
-char * sh_tools_revertPack (unsigned char * header, int flag, char * message, 
+char * sh_tools_revertPack (unsigned char * header, char * message, 
 			    keyInstance * keyInstD,
 			    unsigned long message_size)
 {
-  BYTE                    inBlock[B_SIZ]; 
-  BYTE                    outBlock[B_SIZ];
-  char                    ivBlock[B_SIZ];
   unsigned long   msg_size;
   char          * msg_ret;
 
-  unsigned char         * p;
+  char                  * p;
+  RIJ_BYTE                    inBlock[B_SIZ]; 
+  RIJ_BYTE                    outBlock[B_SIZ];
   int                     j;
   cipherInstance          cipherInst;
   int                     err_num;
   int                     blkfac;
-  char expbuf[SH_ERRBUF_SIZE];
 
   msg_size = (256 * (unsigned int)header[1] + (unsigned int)header[2]);
-  if (msg_size > message_size) 
+#ifdef DEBUG_EN2
+  fprintf(stderr, "RECV <%lu>\n", msg_size);
+#endif
+  if (msg_size > message_size) {
     msg_size = message_size;
-
-  p = (unsigned char *) message; blkfac = msg_size / 16;
-
-  err_num = rijndael_cipherInit (&cipherInst, MODE_CBC, NULL);
+#ifdef DEBUG_EN2
+    fprintf(stderr, "RECV TRUNC1 <%lu>\n", msg_size);
+#endif
+  }
+
+  p = message; blkfac = msg_size / 16;
+
+  err_num = cipherInit (&cipherInst, MODE_CBC, NULL);
   
   if (err_num < 0) 
     {
       sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
-		      errorExplain(err_num, expbuf, sizeof(expbuf)), 
-		      _("sh_tools_revertPack: rijndael_cipherInit"));
-    }
-
-  if (probe_ok(flag)) {
-    memcpy(inBlock, p, B_SIZ);
-    err_num = rijndael_blockDecrypt(&cipherInst, keyInstD, 
-				    inBlock, 128, outBlock);
-    if (err_num >= 0) {
-      memcpy(p, outBlock, B_SIZ); p += B_SIZ;
-      memcpy(ivBlock, inBlock, sizeof(ivBlock));
-      err_num = rijndael_cipherInit (&cipherInst, MODE_CBC, ivBlock);
-      if (err_num >= 0) {
-	err_num = rijndael_blockDecrypt(&cipherInst, keyInstD, 
-					p, 128*(blkfac-1), p);
-      }
-    }
-  }
-
-  else {
-    for (j = 0; j < blkfac; ++j) {
+		      errorExplain(err_num), 
+		      _("sh_tools_revertPack: cipherInit"));
+    }
+  for (j = 0; j < blkfac; ++j)
+    {
       memcpy(inBlock, p, B_SIZ);
-      err_num = rijndael_blockDecrypt(&cipherInst, keyInstD, 
-				      inBlock, 128, outBlock);
-  
-      if (err_num < 0) {
-	sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
-			errorExplain(err_num, expbuf, sizeof(expbuf)), 
-			_("sh_tools_revertPack: rijndael_blockDecrypt"));
-	break; }
-      
+      err_num = blockDecrypt(&cipherInst, keyInstD, 
+			     inBlock, 128 * BNUM, outBlock);
+      if (err_num < 0)
+	{
+	  sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
+			  errorExplain(err_num), 
+			  _("sh_tools_revertPack: blockDecrypt"));
+	}
       memcpy(p, outBlock, B_SIZ);
       p += B_SIZ;
     }
-  }
-
+  
   /* rewrite size in header
    */
@@ -1806,5 +1238,14 @@
       header[1]   = (unsigned int)(msg_size/256);
       header[2]   = (unsigned int)(msg_size - (256 * header[1]));
-    }
+#ifdef DEBUG_EN2
+      fprintf(stderr, "RECV TRUNC2 <%lu>\n", msg_size);
+#endif
+    }
+#ifdef DEBUG_EN2
+  fprintf(stderr, "REC2 <%lu>\n", msg_size);
+#endif
+  /* protocol
+   */
+  /* memcpy(&header[3], &message[8], 4); */
 
   /* payload 
@@ -1812,20 +1253,24 @@
   msg_ret = SH_ALLOC(msg_size+1);
   if (msg_size > 0)
-    memcpy(msg_ret, &message[16], msg_size);
+    {
+      memcpy(msg_ret, &message[16], msg_size);
+    }
   msg_ret[msg_size] = '\0';
-
+#ifdef DEBUG_EN2
+  fprintf(stderr, "REC3 <%lu>\n", msg_size);
+#endif
   SH_FREE(message);
+
   return msg_ret;
 }
-#endif /* #ifdef SH_ENCRYPT */
+#endif
 
 int sh_tools_hash_add(char * key, char * buf, int buflen)
 {
   char         * theSig;
-  char sigbuf[KEYBUF_SIZE];
 
   SL_ENTER(_("sh_tools_hash_add"));
 
-  theSig = sh_util_siggen (key, buf, buflen, sigbuf, sizeof(sigbuf));
+  theSig = sh_util_siggen (key, buf, buflen);
   sl_strlcat(buf, theSig, buflen + KEY_LEN + 1);
       
@@ -1841,9 +1286,8 @@
   register int   i;
   char         * theSig;
-  char sigbuf[KEYBUF_SIZE];
 
   SL_ENTER(_("sh_tools_hash_vfy"));
 
-  theSig = sh_util_siggen (key, buf, buflen, sigbuf, sizeof(sigbuf));
+  theSig = sh_util_siggen (key, buf, buflen);
   sl_strlcpy(hash, theSig, KEY_LEN+1);
 
@@ -1856,7 +1300,4 @@
   SL_RETURN((1), _("sh_tools_hash_vfy"));
 }
-
-#endif /* defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER) */
-
 
 /* ------------------------------------------ */
@@ -1869,9 +1310,8 @@
 {
   char           hash[KEY_LEN+1];
-  char         * temp = NULL;
+  char         * temp;
   register int   i;
   int            total = 0;
   char         * theSig;
-  char sigbuf[KEYBUF_SIZE];
 
 
@@ -1887,30 +1327,22 @@
   ASSERT_RET((buflen >= 0), _("buflen >= 0"), (NULL));
 
-  theSig = sh_util_siggen (key, buf, buflen, sigbuf, sizeof(sigbuf));
+  theSig = sh_util_siggen (key, buf, buflen);
   sl_strlcpy(hash, theSig, KEY_LEN+1);
 
-  if (sl_ok_adds(buflen, KEY_LEN))
-    {
-      total = KEY_LEN + buflen;
-      temp  = SH_ALLOC (total);
-
-      for (i = 0; i < KEY_LEN; ++i)
-	temp[i] = hash[i];
-
-      for (i = 0; i < buflen; ++i)
-	temp[i+KEY_LEN] = buf[i];
-    }
-  else
-    {
-      sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
-		      _("integer overflow"), 
-		      _("hash_me"));
-      temp = sh_util_strdup(buf);
-    }
+
+  total = KEY_LEN + buflen;
+  temp  = SH_ALLOC (total);
+
+  for (i = 0; i < KEY_LEN; ++i)
+    temp[i] = hash[i];
+
+  for (i = 0; i < buflen; ++i)
+    temp[i+KEY_LEN] = buf[i];
+
   SL_RETURN(temp, _("hash_me"));
 }
 #endif
 
-#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
+#if defined (SH_WITH_CLIENT)
 
 /* verify the checksum of a buffer; checksum comes first
@@ -1922,5 +1354,4 @@
   register int   i;
   char         * theSig;
-  char sigbuf[KEYBUF_SIZE];
 
   SL_ENTER(_("hash_check"));
@@ -1930,6 +1361,5 @@
 	  (key == NULL) ? "NULL" : key, buflen);
 #endif
-  theSig = sh_util_siggen (key, &buf[KEY_LEN], buflen-KEY_LEN,
-			   sigbuf, sizeof(sigbuf));
+  theSig = sh_util_siggen (key, &buf[KEY_LEN], buflen-KEY_LEN);
   sl_strlcpy(hash, theSig, KEY_LEN+1);
       
@@ -1946,8 +1376,8 @@
 #if defined (SH_WITH_SERVER)
 
-char * get_client_conf_file (const char * peer, unsigned long * length)
+char * get_client_conf_file (char * peer, unsigned long * length)
 {
   char * ret;
-  int    status;
+  int    size, status;
   struct stat buf;
   char * base;
@@ -1955,10 +1385,18 @@
   SL_ENTER(_("get_client_conf_file"));
 
-  base = sh_util_strdup(DEFAULT_DATAROOT);
-  ret = sh_util_strconcat(base, _("/rc."), peer, NULL);
-  if (!ret) 
-    { SH_FREE(base); *length = 0; SL_RETURN(NULL, _("get_client_conf_file")); }
-
+  size = sl_strlen(DEFAULT_DATAROOT);
+  base = SH_ALLOC(size + 1);
+  sl_strlcpy(base, DEFAULT_DATAROOT, size + 1); 
+
+  size = sl_strlen(base) + sl_strlen(peer) + 5;
+  ++size;
+
+  ret = SH_ALLOC(size);
+  sl_strlcpy(ret, base, size);
+  sl_strlcat(ret, _("/rc."), size);
+  sl_strlcat(ret, peer, size);
+  
   status = retry_stat (FIL__, __LINE__, ret, &buf);
+
   if (status == 0)
     goto lab_end;
@@ -1967,9 +1405,9 @@
 		    (long) sh.effective.uid, ret);
 
-  SH_FREE(ret); ret = sh_util_strconcat(base, _("/rc"), NULL);
-  if (!ret) 
-    { SH_FREE(base); *length = 0; SL_RETURN(NULL, _("get_client_conf_file")); }
+  sl_strlcpy(ret, base, size);
+  sl_strlcat(ret, "/rc", size);
   
   status = retry_stat (FIL__, __LINE__, ret, &buf);
+
   if (status == 0)
     goto lab_end;
@@ -1978,5 +1416,6 @@
 		    (long) sh.effective.uid, ret);
 
-  SH_FREE(base); SH_FREE(ret); *length=0;
+  SH_FREE(base);
+  *length=0;
   SL_RETURN(NULL, _("get_client_conf_file"));
 
@@ -1986,27 +1425,37 @@
       sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_E_SUBGEN,
 		    _("File too large"), _("get_client_conf_file"));
-      SH_FREE(base); SH_FREE(ret); *length = 0;
+      SH_FREE(base);
       SL_RETURN(NULL, _("get_client_conf_file"));
     }
-
-  SH_FREE(base); *length = (unsigned long) buf.st_size;
+  *length = (unsigned long) buf.st_size;
+  SH_FREE(base);
   SL_RETURN(ret, _("get_client_conf_file"));
 }
 
-char * get_client_data_file (const char * peer, unsigned long * length)
+char * get_client_data_file (char * peer, unsigned long * length)
 {
   char * ret;
-  int    status;
+  int    size, status;
   struct stat buf;
+
   char * base;
 
   SL_ENTER(_("get_client_data_file"));
 
-  base = sh_util_strdup(DEFAULT_DATAROOT);
-  ret = sh_util_strconcat(base, _("/file."), peer, NULL);
-  if (!ret) 
-    { SH_FREE(base); *length = 0; SL_RETURN(NULL, _("get_client_data_file")); }
-
+  size = sl_strlen(DEFAULT_DATAROOT);
+  base = SH_ALLOC(size + 1);
+  sl_strlcpy(base, DEFAULT_DATAROOT, size + 1); 
+
+  size = sl_strlen(base) + sl_strlen(peer) + 7;
+
+  ++size;
+
+  ret = SH_ALLOC(size);
+  sl_strlcpy(ret, base, size);
+  sl_strlcat(ret, _("/file."), size);
+  sl_strlcat(ret, peer, size);
+  
   status = retry_stat (FIL__, __LINE__, ret, &buf);
+
   if (status == 0)
     goto lab1_end;
@@ -2015,10 +1464,10 @@
 		    (long) sh.effective.uid, ret);
 
-  SH_FREE(ret); 
-  ret = sh_util_strconcat(base, _("/file"), NULL);
-  if (!ret) 
-    { SH_FREE(base); *length = 0; SL_RETURN(NULL, _("get_client_data_file")); }
-
+
+  sl_strlcpy(ret, base, size);
+  sl_strlcat(ret, _("/file"), size);
+  
   status = retry_stat (FIL__, __LINE__, ret, &buf);
+
   if (status == 0)
     goto lab1_end;
@@ -2027,5 +1476,7 @@
 		    (long) sh.effective.uid, ret);
 
-  *length = 0; SH_FREE(base); SH_FREE(ret);
+
+  *length = 0;
+  SH_FREE(base);
   SL_RETURN(NULL, _("get_client_data_file"));
 
@@ -2035,50 +1486,15 @@
       sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_E_SUBGEN,
 		    _("File too large"), _("get_client_data_file"));
-      SH_FREE(base); SH_FREE(ret); *length = 0;
+      SH_FREE(base);
       SL_RETURN(NULL, _("get_client_data_file"));
     }
-
-  *length = (unsigned long) buf.st_size; SH_FREE(base);
+  *length = (unsigned long) buf.st_size;
+  SH_FREE(base);
   SL_RETURN(ret, _("get_client_data_file"));
-}
-
-char * get_client_uuid_file (const char * peer, unsigned long * length, const char * uuid)
-{
-  char * ret;
-  int    status;
-  struct stat buf;
-  char * base;
-
-  SL_ENTER(_("get_client_uuid_file"));
-
-  base = sh_util_strdup(DEFAULT_DATAROOT);
-  ret = sh_util_strconcat(base, _("/file."), peer, ".", uuid, NULL);
-  SH_FREE(base);
-  if (!ret) 
-    { *length = 0; SL_RETURN(NULL, _("get_client_uuid_file")); }
-
-  status = retry_stat (FIL__, __LINE__, ret, &buf);
-  if (status != 0)
-    {
-      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_E_ACCESS,
-		      (long) sh.effective.uid, ret);
-      SH_FREE(ret); *length = 0;
-      SL_RETURN(NULL, _("get_client_uuid_file"));
-    }
-  else if (buf.st_size > 0x7fffffff)
-    {
-      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_E_SUBGEN,
-		    _("File too large"), _("get_client_uuid_file"));
-      SH_FREE(ret); *length = 0;
-      SL_RETURN(NULL, _("get_client_data_file"));
-    }
-
-  *length = (unsigned long) buf.st_size;
-  SL_RETURN(ret, _("get_client_uuid_file"));
-}
-
-#endif
-
-#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER) || defined(SH_STEALTH) || defined(WITH_SIG)
+  
+}
+#endif
+
+#if defined(SH_WITH_CLIENT) || defined(SH_STEALTH) || defined(WITH_GPG) || defined(WITH_PGP)
 
 /* --------- secure temporary file ------------ */
@@ -2093,15 +1509,21 @@
   int           status = BAD;
   char        * my_tmp_dir;
-  char hashbuf[KEYBUF_SIZE];
+  int           len;
 
   SL_ENTER(_("open_tmp"));
 
 #if defined(SH_TMPDIR)
-  my_tmp_dir = sh_util_strdup(SH_TMPDIR); 
+  len        = sl_strlen(SH_TMPDIR) + 1;
+  my_tmp_dir = SH_ALLOC(len);
+  sl_strlcpy(my_tmp_dir, SH_TMPDIR, len); 
 #else
 #if defined(SH_WITH_SERVER)
-  my_tmp_dir = sh_util_strdup(DEFAULT_LOGDIR); 
+  len        = sl_strlen(DEFAULT_LOGDIR) + 1;
+  my_tmp_dir = SH_ALLOC(len);
+  sl_strlcpy(my_tmp_dir, DEFAULT_LOGDIR, len); 
 #else
-  my_tmp_dir = sh_util_strdup(sh.effective.home);
+  len        = sl_strlen(sh.effective.home) + 1;
+  my_tmp_dir = SH_ALLOC(len);
+  sl_strlcpy(my_tmp_dir, sh.effective.home, len);
 #endif 
 #endif
@@ -2123,15 +1545,13 @@
     /* create random filename in effective users home directory
      */
-    ticks = taus_get ();
+    ticks = taus_get (&(skey->rng0[0]), &(skey->rng1[0]), &(skey->rng2[0]));
     if (my_tmp_dir[0] == '/' && my_tmp_dir[1] == '\0')
       file = sh_util_strconcat (my_tmp_dir, 
-				sh_tiger_hash( (char *) &ticks, TIGER_DATA, 4,
-					       hashbuf, sizeof(hashbuf)),
+				sh_tiger_hash( (char *) &ticks, TIGER_DATA, 4),
 				NULL);
     else
       file = sh_util_strconcat (my_tmp_dir, 
 				"/", 
-				sh_tiger_hash( (char *) &ticks, TIGER_DATA, 4,
-					       hashbuf, sizeof(hashbuf)),
+				sh_tiger_hash( (char *) &ticks, TIGER_DATA, 4),
 				NULL);
 
@@ -2148,6 +1568,4 @@
 	SH_FREE (file);
 	SH_FREE(my_tmp_dir);
-	sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, status, MSG_E_SUBGEN, 
-			_("Error (lstat) while opening temporary file"), _("open_tmp"));
 	TPT(( 0, FIL__, __LINE__, _("msg=<Unexpected error %d>\n"), error));
 	SL_RETURN((-1), _("open_tmp"));
@@ -2175,12 +1593,13 @@
   } while (status == BAD);
 
-  fd = sl_open_safe_rdwr (FIL__, __LINE__, file, SL_YESPRIV);
+  fd = sl_open_safe_rdwr (file, SL_YESPRIV);
   if (SL_ISERROR(fd))
     {
-      sh_error_handle((-1), FIL__, __LINE__, fd, MSG_E_SUBGEN, 
+      sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, fd, MSG_E_SUBGEN, 
 		      _("Error opening temporary file"), _("open_tmp"));
       TPT(( 0, FIL__, __LINE__, _("msg=<Error %d temporary file %s>\n"), 
 	    fd, file));
     }
+  
 
   SH_FREE (file);
@@ -2216,72 +1635,2 @@
 }
 #endif
-
-/********************************************************
- * Search rotated logfile
- */
-#include <unistd.h>
-#include <libgen.h>
-#include <dirent.h>
-
-char * sh_rotated_log_search(const char * path, struct stat * buf)
-{
-
-  size_t size;
-  int    i;
-  char * searchpath;
-  struct stat sbuf;
-  DIR  * dp;
-  char * dname;
-  char * bname;
-
-  dname  = sh_util_dirname(path);
-  bname  = sh_util_basename(path);
-
-  size = strlen(dname) + strlen(bname) + 4;
-  searchpath = SH_ALLOC(size);
-
-  for (i = 0; i < 2; ++i)
-    {
-      snprintf(searchpath, size, "%s/%s.%1d", dname, bname, i);
-      if (0 == stat(searchpath, &sbuf) && sbuf.st_ino == buf->st_ino)
-	{
-	  SH_FREE(dname);
-	  SH_FREE(bname);
-	  return searchpath;
-	}
-    }
-
-  SH_FREE(searchpath);
-
-  if (NULL != (dp = opendir(dname)))
-    {
-      struct dirent * de;
-
-      while (NULL != (de = readdir(dp)))
-	{
-	  if (0 == strcmp(de->d_name, ".") || 0 == strcmp(de->d_name, ".."))
-	    continue;
-
-	  size = strlen(dname) + strlen(de->d_name) + 2;
-	  searchpath = SH_ALLOC(size);
-	  snprintf(searchpath, size, "%s/%s", dname, de->d_name);
-
-	  if (0 == stat(searchpath, &sbuf) && sbuf.st_ino == buf->st_ino)
-	    {
-	      SH_FREE(dname);
-	      SH_FREE(bname);
-	      closedir(dp);
-	      return searchpath;
-	    }
-	  
-	  SH_FREE(searchpath);
-	}
-      closedir(dp);
-    }
-
-  SH_FREE(dname);
-  SH_FREE(bname);
-
-  return NULL;
-}
-
Index: trunk/src/sh_unix.c
===================================================================
--- trunk/src/sh_unix.c	(revision 591)
+++ trunk/src/sh_unix.c	(revision 1)
@@ -18,6 +18,4 @@
 /*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
 
-/* cppcheck-suppress-file unknownMacro */
-
 #include "config_xor.h"
 
@@ -27,7 +25,5 @@
 #include <string.h>
 #include <ctype.h>
-#ifdef HAVE_LINUX_FS_H
-#include <linux/fs.h>
-#endif
+
 
 #ifdef HAVE_MEMORY_H
@@ -45,10 +41,4 @@
 #include <fcntl.h>
 #include <unistd.h>
-/* need to undef these, since the #define's may be picked up from
- * linux/wait.h, and will clash with a typedef in sys/wait.h
- */
-#undef P_ALL
-#undef P_PID
-#undef P_PGID
 #include <sys/wait.h>
 
@@ -60,8 +50,14 @@
 #endif
 
+#if TIME_WITH_SYS_TIME
+#include <sys/time.h>
+#include <time.h>
+#else
 #if HAVE_SYS_TIME_H
 #include <sys/time.h>
-#endif
+#else
 #include <time.h>
+#endif
+#endif
 
 #ifdef HAVE_SYS_SELECT_H
@@ -79,5 +75,5 @@
 #endif
 #ifndef FD_ZERO
-#define FD_ZERO(p)      memset((char *)(p), 0, sizeof(*(p)))
+#define FD_ZERO(p)      memset((char *)(p), '\0', sizeof(*(p)))
 #endif
 
@@ -94,10 +90,6 @@
 #include "sh_hash.h"
 #include "sh_tools.h"
-#include "sh_restrict.h"
-#include "sh_ipvx.h"
 #include "sh_tiger.h"
 #include "sh_prelink.h"
-#include "sh_pthread.h"
-#include "sh_sem.h"
 
 /* moved here from far below
@@ -129,22 +121,4 @@
 #endif
 
-#if defined(S_IFDOOR) && !defined(S_ISDOOR)
-#define S_ISDOOR(mode) (((mode) & S_IFMT) == S_IFDOOR)
-#else
-#if !defined(S_ISDOOR)
-#define S_ISDOOR(mode) (0)
-#endif
-#endif
-
-#if defined(S_IFPORT) && !defined(S_ISPORT)
-#define S_ISPORT(mode) (((mode) & S_IFMT) == S_IFPORT)
-#else
-#if !defined(S_ISPORT)
-#define S_ISPORT(mode) (0)
-#endif
-#endif
-
-#define SH_KEY_NULL _("000000000000000000000000000000000000000000000000")
-
 #undef  FIL__
 #define FIL__  _("sh_unix.c")
@@ -153,7 +127,4 @@
 unsigned long mask_USER0        = MASK_USER_;
 unsigned long mask_USER1        = MASK_USER_;
-unsigned long mask_USER2        = MASK_USER_;
-unsigned long mask_USER3        = MASK_USER_;
-unsigned long mask_USER4        = MASK_USER_;
 unsigned long mask_ALLIGNORE    = MASK_ALLIGNORE_;
 unsigned long mask_ATTRIBUTES   = MASK_ATTRIBUTES_;
@@ -171,7 +142,4 @@
   mask_USER0        = MASK_USER_;
   mask_USER1        = MASK_USER_;
-  mask_USER2        = MASK_USER_;
-  mask_USER3        = MASK_USER_;
-  mask_USER4        = MASK_USER_;
   mask_ALLIGNORE    = MASK_ALLIGNORE_;
   mask_ATTRIBUTES   = MASK_ATTRIBUTES_;
@@ -339,6 +307,6 @@
     *p = '0' + (u % 10);
     u /= 10;
-  } while (u && (p != str));
-  if ((iisneg == 1) && (p != str)) {
+  } while (u);
+  if (iisneg == 1) {
     --p;
     *p = '-';
@@ -355,7 +323,7 @@
 extern int OnlyStderr; 
 
-int safe_logger (int thesignal, int method, char * details)
-{
-  unsigned int i = 0;
+int safe_logger (int signal, int method, pid_t thepid)
+{
+  int i = 0;
   int status = -1;
   struct stat buf;
@@ -363,19 +331,17 @@
   char  str[128];
   char  * p;
-  
+
   char l0[64], l1[64], l2[64], l3[64];
-  char a0[32];
+  char a0[32], a1[32], a2[32];
   char e0[128];
   char msg[128];
-  
+
   char * locations[] = { NULL, NULL, NULL, NULL, NULL };
   char * envp[]      = { NULL, NULL };
-  char * argp[]      = { NULL, NULL, NULL };
-  
-  pid_t  thepid = getpid();
-  
+  char * argp[]      = { NULL, NULL, NULL, NULL, NULL };
+
   if ((sh.flag.isdaemon == S_FALSE) || (OnlyStderr == S_TRUE))
     method = 1;
-  
+
   /* seems that solaris cc needs this way of initializing ...
    */
@@ -384,25 +350,35 @@
   locations[2] = l2;
   locations[3] = l3;
-  
+
   envp[0] = e0;
+
   argp[0] = a0;
-  
+  argp[1] = a1;
+  argp[2] = a2;
+
+  strcpy (l0, _("/usr/bin/logger"));                   /* known to fit  */
+  strcpy (l1, _("/usr/sbin/logger"));                  /* known to fit  */
+  strcpy (l2, _("/usr/ucb/logger"));                   /* known to fit  */
+  strcpy (l3, _("/bin/logger"));                       /* known to fit  */
+
+  strcpy (a0, _("logger"));                            /* known to fit  */
+  strcpy (a1, _("-p"));                                /* known to fit  */
+  strcpy (a2, _("daemon.alert"));                      /* known to fit  */
+
+  strcpy (e0,                                          /* known to fit  */
+	  _("PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/ucb:/usr/local/bin"));
+
   sl_strlcpy(msg, _("samhain["), 128);
   p = safe_itoa((int) thepid, str, 128);
   if (p && *p)
     sl_strlcat(msg, p, 128);
-  if (thesignal == 0)
-    {
-      if (details == NULL) {
-	sl_strlcat(msg, _("]: out of memory"), 128);
-      } else {
-	sl_strlcat(msg, _("]: "), 128);
-	sl_strlcat(msg, details, 128);
-      }
+  if (signal == 0)
+    {
+      sl_strlcat(msg, _("]: out of memory"), 128);
     }
   else 
     {
       sl_strlcat(msg, _("]: exit on signal "), 128);
-      p = safe_itoa(thesignal, str, 128);
+      p = safe_itoa(signal, str, 128);
       if (p && *p)
 	sl_strlcat(msg, p, 128);
@@ -413,24 +389,8 @@
 #define STDERR_FILENO 2
 #endif
-    int retval = 0;
-    do {
-      retval = write(STDERR_FILENO,  msg, strlen(msg));
-    } while (retval < 0 && errno == EINTR);
-    do {
-      retval = write(STDERR_FILENO, "\n", 1);
-    } while (retval < 0 && errno == EINTR);
+    write(STDERR_FILENO,  msg, strlen(msg));
+    write(STDERR_FILENO, "\n", 1);
     return 0;
   }
-
-  sl_strlcpy (l0, _("/usr/bin/logger"), 64);
-  sl_strlcpy (l1, _("/usr/sbin/logger"), 64);
-  sl_strlcpy (l2, _("/usr/ucb/logger"), 64);
-  sl_strlcpy (l3, _("/bin/logger"), 64);
-
-  sl_strlcpy (a0, _("logger"), 32);
-  sl_strlcpy (e0,
-	      _("PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/ucb:/usr/local/bin"),
-	      128);
-
   while (locations[i] != NULL) {
     status = stat(locations[i], &buf);
@@ -441,5 +401,5 @@
 
   if (locations[i] != NULL) {
-    argp[1] = msg;
+    argp[3] = msg;
     newpid = fork();
     if (newpid == 0) {
@@ -454,27 +414,4 @@
 }
 
-void safe_fatal (const char * details, 
-		 const char * file, int line)
-{
-  char msg[128];
-  char str[128];
-  char * p;
-  int  thesignal = 0;
-  int  method = 0;
-
-  p = safe_itoa((int) line, str, 128);
-  sl_strlcpy(msg, _("FATAL: "), 128);
-  sl_strlcat(msg, file, 128);
-  sl_strlcat(msg, ": ", 128);
-  if (p && (*p)) {
-    sl_strlcat(msg, p   , 128);
-    sl_strlcat(msg, ": ", 128);
-  }
-  sl_strlcat(msg, details, 128);
-  (void) safe_logger (thesignal, method, msg);
-
-  close_ipc ();
-  raise(SIGKILL);
-}
 
 extern char sh_sig_msg[64];
@@ -510,17 +447,11 @@
       ++immediate_exit_normal;
       if ((skey != NULL) && (immediate_exit_normal == 2))
-	memset (skey, 0, sizeof(sh_key_t));
+	memset (skey, '\0', sizeof(sh_key_t));
       if (immediate_exit_normal == 2)
 	{
-	  int val_return;
-
-	  do {
-	    val_return = chdir ("/");
-	  } while (val_return < 0 && errno == EINTR);
-
-          close_ipc ();
-	  safe_logger (mysignal, 0, NULL);
+	  chdir ("/");
+	  safe_logger (mysignal, 0, getpid());
 	}
-      raise(SIGKILL);
+      _exit(mysignal);
     }
   else
@@ -553,8 +484,4 @@
 #endif
 {
-#if defined(SL_DEBUG) && (defined(USE_SYSTEM_MALLOC) || !defined(USE_MALLOC_LOCK))
-  int retval;
-#endif
-
 #if defined(SA_SIGACTION_WORKS)
   if (signal_info != NULL && signal_info->si_code == SI_USER)
@@ -572,11 +499,11 @@
   /* Check whether the heap is ok; otherwise _exit 
    */
-#if !defined(SL_DEBUG) || (!defined(USE_SYSTEM_MALLOC) && defined(USE_MALLOC_LOCK))
+#if !defined(SL_DEBUG)
   ++immediate_exit_fast;
   if (skey != NULL && immediate_exit_fast < 2)
-    memset (skey, 0, sizeof(sh_key_t));
+    memset (skey, '\0', sizeof(sh_key_t));
   if (immediate_exit_fast < 2)
-    safe_logger (mysignal, 0, NULL);
-  raise(SIGKILL);
+    safe_logger (mysignal, 0, getpid());
+  _exit(mysignal);
 #else
 
@@ -587,22 +514,20 @@
       ++immediate_exit_fast;
       if (skey != NULL)
-	memset (skey, 0, sizeof(sh_key_t));
+	memset (skey, '\0', sizeof(sh_key_t));
+#ifdef WITH_MESSAGE_QUEUE
       close_ipc ();
-      safe_logger (mysignal, 0, NULL);
-      do {
-	retval = chdir ("/");
-      } while (retval < 0 && errno == EINTR);
+#endif
+      safe_logger (mysignal, 0, getpid());
+      chdir ("/");
       raise(SIGFPE);
     }
   else if (immediate_exit_fast == 2)
     {
-      do {
-	retval = chdir ("/");
-      } while (retval < 0 && errno == EINTR);
+      chdir ("/");
       raise(SIGFPE);
     }
   else if (immediate_exit_fast != 0)
     {
-      raise(SIGKILL);
+      _exit(mysignal);
     }
 
@@ -617,5 +542,4 @@
   strncpy (sh_sig_msg, sh_unix_siglist(mysignal), 40);
 #endif
-  sh_sig_msg[63] = '\0';
 
   sl_stack_print();
@@ -627,11 +551,10 @@
 
   if (skey != NULL)
-    memset (skey, 0, sizeof(sh_key_t));
+    memset (skey, '\0', sizeof(sh_key_t));
+#ifdef WITH_MESSAGE_QUEUE
   close_ipc ();
-
-  do {
-    retval = chdir ("/");
-  } while (retval < 0 && errno == EINTR);
-
+#endif
+
+  chdir ("/");
   raise(SIGFPE);
 #endif
@@ -650,5 +573,5 @@
   if (mysignal == SIGUSR2)
     {
-      ++sig_suspend_switch;
+      sig_suspend_switch    = 1;
       ++sig_urgent;
     }
@@ -659,14 +582,6 @@
 #endif
 #ifdef SIGTTOU
-  if (mysignal == SIGTTOU) {
-    sig_force_check = 1; sh_sem_trylock(); }
-#endif
-#ifdef SIGTSTP
-  if (mysignal == SIGTSTP) {
-    sig_force_check = 1; sig_force_silent = 1; sh_sem_trylock(); }
-#endif
-#ifdef SIGTTIN
-  if (mysignal == SIGTTIN)
-    sig_fresh_trail = 1;
+  if (mysignal == SIGTTOU)
+    sig_force_check = 1;
 #endif
 #ifdef SIGABRT
@@ -676,5 +591,8 @@
 #ifdef SIGQUIT
   if (mysignal == SIGQUIT)
-    sig_terminate       = 1;
+    {
+      sig_terminate         = 1;
+      ++sig_urgent;
+    }
 #endif
 #ifdef SIGTERM
@@ -690,18 +608,4 @@
 }
 
-void sh_unix_ign_sigpipe()
-{
-  struct sigaction ignact;
-
-  ignact.sa_handler = SIG_IGN;            /* signal action           */
-  sigemptyset( &ignact.sa_mask );         /* set an empty mask       */
-  ignact.sa_flags = 0;                    /* init sa_flags           */
-
-#ifdef SIGPIPE
-  retry_sigaction(FIL__, __LINE__, SIGPIPE,   &ignact, NULL);
-#endif
-
-  return;
-}
 static
 void sh_unix_siginstall (int goDaemon)
@@ -810,5 +714,5 @@
 #endif
 #ifdef SIGTERM
-  retry_sigaction(FIL__, __LINE__, SIGTERM,    &act,  &oldact);
+  retry_sigaction(FIL__, __LINE__, SIGTERM,     &act,  &oldact);
 #endif
 
@@ -836,10 +740,15 @@
 #endif
 #ifdef SIGPIPE
-  retry_sigaction(FIL__, __LINE__, SIGPIPE,   &ignact, &oldact);
+  retry_sigaction(FIL__, __LINE__, SIGPIPE,      &act, &oldact);
 #endif
 #ifdef SIGALRM
   retry_sigaction(FIL__, __LINE__, SIGALRM,   &ignact, &oldact);
 #endif
-
+#ifdef SIGTSTP
+  retry_sigaction(FIL__, __LINE__, SIGTSTP,   &ignact, &oldact);
+#endif
+#ifdef SIGTTIN
+  retry_sigaction(FIL__, __LINE__, SIGTTIN,   &ignact, &oldact);
+#endif
 #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
 #ifdef SIGTTOU
@@ -849,25 +758,7 @@
     retry_sigaction(FIL__, __LINE__, SIGTTOU,   &ignact, &oldact);
 #endif
-#ifdef SIGTSTP
-  if (goDaemon == 1)
-    retry_sigaction(FIL__, __LINE__, SIGTSTP,     &act2, &oldact);
-  else
-    retry_sigaction(FIL__, __LINE__, SIGTSTP,   &ignact, &oldact);
-#endif
-#ifdef SIGTTIN
-  if (goDaemon == 1)
-    retry_sigaction(FIL__, __LINE__, SIGTTIN,     &act2, &oldact);
-  else
-    retry_sigaction(FIL__, __LINE__, SIGTTIN,   &ignact, &oldact);
-#endif
 #else
-#ifdef SIGTSTP
-  retry_sigaction(FIL__, __LINE__, SIGTSTP,   &ignact, &oldact);
-#endif
 #ifdef SIGTTOU
   retry_sigaction(FIL__, __LINE__, SIGTTOU,   &ignact, &oldact);
-#endif
-#ifdef SIGTTIN
-  retry_sigaction(FIL__, __LINE__, SIGTTIN,   &ignact, &oldact);
 #endif
 #endif
@@ -928,8 +819,7 @@
 /* checksum the own binary
  */
-int sh_unix_self_hash (const char * c)
+int sh_unix_self_hash (char * c)
 {
   char message[512];
-  char hashbuf[KEYBUF_SIZE];
 
   SL_ENTER(_("sh_unix_self_hash"));
@@ -943,5 +833,5 @@
 
   sl_strlcpy(sh.exec.hash,
-	     sh_tiger_hash (c, TIGER_FILE, TIGER_NOLIM, hashbuf, sizeof(hashbuf)), 
+	     sh_tiger_hash (c, TIGER_FILE, 0), 
 	     KEY_LEN+1);
   sl_snprintf(message, 512, _("%s has checksum: %s"),
@@ -950,5 +840,7 @@
   sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
 		  message, _("sh_unix_self_hash"));
-  if (0 == sl_strcmp(sh.exec.hash, SH_KEY_NULL ))
+  if (0 == sl_strcmp(sh.exec.hash,
+		     _("000000000000000000000000000000000000000000000000")
+		     ))
     {
       dlog(1, FIL__, __LINE__, 
@@ -966,26 +858,15 @@
   char newhash[KEY_LEN+1];
   char message[512];
-  char hashbuf[KEYBUF_SIZE];
 
   SL_ENTER(_("sh_unix_self_check"));
-  if (sh.exec.path[0] == '\0')
+  if (sh.exec.path == NULL || sh.exec.path[0] == '\0')
     SL_RETURN((0), _("sh_unix_self_check"));
 
-  sl_strlcpy(newhash, 
-	     sh_tiger_hash (sh.exec.path, TIGER_FILE, TIGER_NOLIM, hashbuf, sizeof(hashbuf)), 
-	     KEY_LEN+1);
-  if (0 == sl_strncmp(sh.exec.hash, newhash, KEY_LEN))
-    {
-      sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		      _("Checksum ok"), _("sh_unix_self_check"));
-      SL_RETURN((0), _("sh_unix_self_check"));
-    }
-
-  if (0 == sl_strncmp(SH_KEY_NULL, newhash, KEY_LEN))
-    {
-      sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		      _("Could not read samhain executable"), _("sh_unix_self_check"));
-      SL_RETURN((0), _("sh_unix_self_check"));
-    }
+  sl_strlcpy(newhash, sh_tiger_hash (sh.exec.path, TIGER_FILE, 0), KEY_LEN+1);
+  if (0 == sl_strncmp(sh.exec.hash, 
+		      newhash,
+		      KEY_LEN))
+    SL_RETURN((0), _("sh_unix_self_check"));
+
  
   dlog(1, FIL__, __LINE__, 
@@ -1008,121 +889,17 @@
 /* ---------------------------------------------------------------- */
 
-long sh_group_to_gid (const char * g, int * fail)
-{
-  struct group *           w;
-  gid_t                    gid  = 0;
-  int                      status = 0;
-
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-  struct group     grp;
-  char           * buffer;
-  static size_t    gbufsize = SH_GRBUF_SIZE;       
-#endif
-
-  *fail = -1;
-
-  if (g)
-    {
-      size_t i;
-      size_t len = strlen(g);
-
-      *fail = 0;
-
-      for (i = 0; i < len; ++i)
-	{
-	  char c = g[i];
-
-	  if (!isdigit((int) c))
-	    goto is_a_name;
-	}
-      return atol(g);
-
-    is_a_name:
-
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-
-      buffer = SH_ALLOC(gbufsize);
-      status = sh_getgrnam_r(g, &grp, buffer, gbufsize, &w);
-
-      if ((status == ERANGE) && (w == NULL)) 
-	{
-	  if (S_TRUE ==  sl_ok_adds( gbufsize, SH_GRBUF_SIZE ))
-	    {
-	      SH_FREE(buffer);
-	      gbufsize += SH_GRBUF_SIZE;
-	      goto is_a_name;
-	    }
-	}
-
-#else
-
-      errno = 0;
-      w = sh_getgrnam(g);
-      status = errno;
-
-#endif
-
-      if ((status == ERANGE) && (w == NULL)) 
-	{
-	  static int seen = 0;
-	  
-	  if (seen == 0)
-	    {
-	      char errbuf[SH_ERRBUF_SIZE];
-
-	      sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, EINVAL, MSG_E_GRNULL,
-			       sh_error_message(status, errbuf, sizeof(errbuf)),
-			       _("sh_group_to_gid"), (long) -1, _("line too long in group entry"));
-	      ++seen;
-	    }
-	  *fail = -1;
-	}
-      else if (w == NULL)
-	{
-	  char * tmp = sh_util_strdup(g);
-	  sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS, 
-			   _("sh_group_to_gid"), tmp);
-	  SH_FREE(tmp);
-	  *fail = -1;
-	}
-      else
-	{
-	  gid = w->gr_gid;
-	}
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-      SH_FREE(buffer);
-#endif
-    }
-
-  return gid;
-}
-
-/* ---------------------------------------------------------------- */
-
 
 /* added    Tue Feb 22 10:36:44 NFT 2000 Rainer Wichmann            */
-static int tf_add_trusted_user_int(const char * c)
-{
-  struct passwd *          w;
+static int tf_add_trusted_user_int(char * c)
+{
+  register struct passwd *          w;
   int                           count;
   uid_t                     pwid  = (uid_t)-1;
 
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-  struct passwd    pwd;
-  char           * buffer;
-#endif
-  
   SL_ENTER(_("tf_add_trusted_user_int"));
 
   /* First check for a user name.
    */
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-  buffer = SH_ALLOC(SH_PWBUF_SIZE);
-  sh_getpwnam_r(c, &pwd, buffer, SH_PWBUF_SIZE, &w);
-#else
-  w = sh_getpwnam(c);
-#endif
-
-  if ((w != NULL) && ((pwid = w->pw_uid) > 0))
+  if ((w = sh_getpwnam(c)) != NULL && ((pwid = w->pw_uid) > 0))
     goto succe;
 	
@@ -1135,33 +912,20 @@
   sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS, 
 		   _("add trusted user"), c);
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-  SH_FREE(buffer);
-#endif
   SL_RETURN((-1), _("tf_add_trusted_user_int"));
 
  succe:
   count = sl_trust_add_user(pwid);
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-  SH_FREE(buffer);
-#endif
   SL_RETURN((count), _("tf_add_trusted_user_int"));
 }
 
-int tf_add_trusted_user(const char * c)
+int tf_add_trusted_user(char * c)
 {
   int    i;
   char * q;
   char * p = sh_util_strdup (c);
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
-  char * saveptr;
-#endif
-
+  
   SL_ENTER(_("tf_add_trusted_user"));
 
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
-  q = strtok_r(p, ", \t", &saveptr);
-#else
   q = strtok(p, ", \t");
-#endif
   if (!q)
     {
@@ -1177,9 +941,5 @@
 	  SL_RETURN((i), _("tf_add_trusted_user"));
 	}
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
-      q = strtok_r(NULL, ", \t", &saveptr);
-#else
       q = strtok(NULL, ", \t");
-#endif
     }
   SH_FREE(p);
@@ -1187,9 +947,9 @@
 }
 
-extern uid_t   sl_trust_baduid(void);
-extern gid_t   sl_trust_badgid(void);
+extern uid_t   sl_trust_baduid();
+extern gid_t   sl_trust_badgid();
 
 #if defined(HOST_IS_CYGWIN) || defined(__cygwin__) || defined(__CYGWIN32__) || defined(__CYGWIN__)
-int tf_trust_check (const char * file, int mode)
+int tf_trust_check (char * file, int mode)
 {
   (void) file;
@@ -1198,5 +958,5 @@
 }
 #else
-int tf_trust_check (const char * file, int mode)
+int tf_trust_check (char * file, int mode)
 {
   char * tmp;
@@ -1205,7 +965,5 @@
   int    status;
   int    level;
-  uid_t  ff_euid = (uid_t) -1;
-  uid_t  baduid;
-  gid_t  badgid;
+  uid_t  ff_euid;
 
   SL_ENTER(_("tf_trust_check"));
@@ -1219,12 +977,5 @@
   if (0 == sl_ret_euid())   /* privileges not dropped yet */
     {
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-      struct passwd    pwd;
-      char          *  buffer = SH_ALLOC(SH_PWBUF_SIZE);
-      struct passwd *  tempres;
-      sh_getpwnam_r(DEFAULT_IDENT, &pwd, buffer, SH_PWBUF_SIZE, &tempres);
-#else
       struct passwd * tempres = sh_getpwnam(DEFAULT_IDENT);
-#endif
 
       if (!tempres)
@@ -1235,11 +986,5 @@
 	  aud_exit (FIL__, __LINE__, EXIT_FAILURE);
 	}
-      else
-	{
-	  ff_euid = tempres->pw_uid;
-	}
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-      SH_FREE(buffer);
-#endif
+      ff_euid = tempres->pw_uid;
     }
 #endif
@@ -1254,12 +999,9 @@
 	level = SH_ERR_ERR;
 
-      tmp    = sh_util_safe_name (file);
-      p      = sh_util_strdup(sl_trust_errfile());
-      baduid = sl_trust_baduid();
-      badgid = sl_trust_badgid();
-      
+      tmp  = sh_util_safe_name (file);
+      p    = sl_trust_errfile();
       if (p && *p != '\0')
 	{
-	  tmp2  = sh_util_safe_name (p);
+	  tmp2  = sh_util_safe_name (sl_trust_errfile());
 	  sh_error_handle(level, FIL__, __LINE__, status, MSG_E_TRUST2,
 			  sl_error_string(status), tmp, tmp2);
@@ -1271,4 +1013,5 @@
 			  sl_error_string(status), tmp);
 	}
+      SH_FREE(tmp);
 
       if (status == SL_EBADUID   || status == SL_EBADGID || 
@@ -1282,35 +1025,41 @@
 	    break;
 	  case SL_ETRUNC:
+	    tmp  = sh_util_safe_name (file);
 	    dlog(1, FIL__, __LINE__, 
 		 _("A filename truncation occured in the trustfile function.\nProbably the normalized filename for %s\nis too long. This may be due e.g. to deep or circular softlinks.\n"), 
 		 tmp);
+	    SH_FREE(tmp);
 	    break;
 	  case SL_EBADOTH:
+	    tmp  = sh_util_safe_name (file);
+	    p    = sl_trust_errfile();
 	    dlog(1, FIL__, __LINE__, 
 		 _("The path element: %s\nin the filename: %s is world writeable.\n"),
-		 (p) ? p : _("(null)"), tmp);
+		 p, tmp);
+	    SH_FREE(tmp);
 	    break;
 	  case SL_EBADUID:
+	    tmp  = sh_util_safe_name (file);
+	    p    = sl_trust_errfile();
 	    dlog(1, FIL__, __LINE__, 
 		 _("The owner (UID = %ld) of the path element: %s\nin the filename: %s\nis not in the list of trusted users.\nTo fix the problem, you can:\n - run ./configure again with the option --with-trusted=0,...,UID\n   where UID is the UID of the untrusted user, or\n - use the option TrustedUser=UID in the configuration file.\n"),
-		 (UID_CAST)baduid, (p) ? p : _("(null)"), tmp);
+		 (UID_CAST)sl_trust_baduid(), p, tmp);
+	    SH_FREE(tmp);
 	    break;
 	  case SL_EBADGID:
+	    tmp  = sh_util_safe_name (file);
+	    p    = sl_trust_errfile();
 	    dlog(1, FIL__, __LINE__, 
 		 _("The path element: %s\nin the filename: %s\nis group writeable (GID = %ld), and at least one of the group\nmembers (UID = %ld) is not in the list of trusted users.\nTo fix the problem, you can:\n - run ./configure again with the option --with-trusted=0,...,UID\n   where UID is the UID of the untrusted user, or\n - use the option TrustedUser=UID in the configuration file.\n"),
-		 (p) ? p : _("(null)"), tmp, (UID_CAST)badgid, 
-		 (UID_CAST)baduid);
+		 p, tmp, (UID_CAST)sl_trust_badgid(), 
+		 (UID_CAST)sl_trust_baduid());
+	    SH_FREE(tmp);
 	    break;
 	  default:
 	    break;
 	  }
-	  SH_FREE(tmp);
-	  if (p) SH_FREE(p);
+	    
 	  SL_RETURN((-1), _("tf_trust_check"));
 	}
-      else {
-	SH_FREE(tmp);
-	if (p) SH_FREE(p);
-      }
     }
 
@@ -1348,13 +1097,14 @@
 
 #ifdef HAVE_INITGROUPS
-char *  sh_unix_getUIDname (int level, uid_t uid, char * out, size_t len);
+char *  sh_unix_getUIDname (int level, uid_t uid);
 int  sh_unix_initgroups2 (uid_t in_pid, gid_t in_gid)
 {
   int status  = -1;
-  char user[SH_MINIBUF];
+  char * user = NULL;
 
   SL_ENTER(_("sh_unix_initgroups2"));
 
-  if (NULL == sh_unix_getUIDname (SH_ERR_ERR, in_pid, user, sizeof(user)))
+  user = sh_unix_getUIDname (SH_ERR_ERR, in_pid);
+  if (user == NULL)
     SL_RETURN((-1), _("sh_unix_initgroups2"));
   status = sh_initgroups (user, in_gid);
@@ -1377,5 +1127,5 @@
 #endif
 
-void sh_unix_closeall (int fd, int except, int inchild)
+void sh_unix_closeall (int fd, int except)
 {
   int fdx = fd;
@@ -1395,13 +1145,8 @@
    */
   if (fdlimit < 0)
-    fdlimit = 20;  /* POSIX lower limit */
+    fdlimit = 8;  /* POSIX lower limit */
 
   if (fdlimit > 65536)
     fdlimit = 65536;
-
-  if (!inchild)
-    sl_dropall (fdx, except);
-  else
-    sl_dropall_dirty (fdx, except);
 
   /* Close everything from fd (inclusive) up to fdlimit (exclusive). 
@@ -1414,6 +1159,8 @@
 	fd++;
       else
-	sl_close_fd(FIL__, __LINE__, fd++);
-    }
+	close(fd++);
+    }
+
+  sl_dropall (fdx, except);
 
   SL_RET0(_("sh_unix_closeall"));
@@ -1446,4 +1193,9 @@
 #ifdef RLIMIT_NPROC
   setrlimit (RLIMIT_NPROC,   &limits);
+#endif
+#if defined(RLIMIT_NOFILE)
+  setrlimit (RLIMIT_NOFILE,  &limits);
+#elif defined(RLIMIT_OFILE)
+  setrlimit (RLIMIT_OFILE,   &limits);
 #endif
 #ifdef RLIMIT_MEMLOCK
@@ -1465,12 +1217,4 @@
 #endif
 
-  limits.rlim_cur = 1024;
-  limits.rlim_max = 1024;
-
-#if defined(RLIMIT_NOFILE)
-  setrlimit (RLIMIT_NOFILE,  &limits);
-#elif defined(RLIMIT_OFILE)
-  setrlimit (RLIMIT_OFILE,   &limits);
-#endif
 
   SL_RET0(_("sh_unix_setlimits"));
@@ -1482,5 +1226,4 @@
   char ** env1;
   int   envlen = 0;
-  size_t len;
 
   SL_ENTER(_("sh_unix_copyenv"));
@@ -1493,5 +1236,5 @@
 
   /* printf("-> %2d: slots allocated\n", envlen); */
-  env1 = calloc(1,sizeof(char *) * envlen);      /* only once */
+  env1 = malloc (sizeof(char *) * envlen);      /* only once */
   if (env1 == NULL)
     {
@@ -1502,16 +1245,12 @@
   envlen = 0;
 
-  while (env0 != NULL && env0[envlen] != NULL) {
-    len = strlen(env0[envlen]) + 1;
-    env1[envlen] = calloc(1,len); /* only once */
+  while (env0 != NULL && env0[envlen] != NULL) { 
+    env1[envlen] = malloc (strlen(env0[envlen]) + 1); /* only once */
     if (env1[envlen] == NULL)
       {
-	int i;
 	fprintf(stderr, _("%s: %d: Out of memory\n"), FIL__, __LINE__);
-	for (i = 0; i < envlen; ++i) free(env1[len]);
-	free(env1);
 	SL_RET0(_("sh_unix_copyenv"));
       }
-    sl_strlcpy(env1[envlen], env0[envlen], len);
+    strcpy(env1[envlen], env0[envlen]);                /* known to fit  */
     ++envlen;
   }
@@ -1545,26 +1284,10 @@
 	continue;
       }
-    if (0 == sl_strncmp((*env), _("MYSQL_TCP_PORT="), 15))
-      {
-	++(env);
-	continue;
-      }
-    if (0 == sl_strncmp((*env), _("MYSQL_HOME="), 11))
-      {
-	++(env);
-	continue;
-      }
 #endif
 #ifdef WITH_ORACLE
     /* 
-     * Skip the ORACLE_HOME and TNS_ADMIN environment variables; 
-     * Oracle may need them.
+     * Skip the ORACLE_HOME environment variable; Oracle may need it.
      */
     if (0 == sl_strncmp((*env), _("ORACLE_HOME="), 12))
-      {
-	++(env);
-	continue;
-      }
-    if (0 == sl_strncmp((*env), _("TNS_ADMIN="), 10))
       {
 	++(env);
@@ -1591,8 +1314,4 @@
   }
 
-#ifdef HAVE_TZSET
-  tzset();
-#endif
-
   SL_RET0(_("sh_unix_zeroenv"));
 }
@@ -1629,9 +1348,6 @@
 #endif
   int  test;
+  struct sigaction act, oldact;
   int  status;
-  struct sigaction act;
-#if !defined(SH_PROFILE)
-  struct sigaction oldact;
-#endif
 
   sigset_t set_proc;
@@ -1646,5 +1362,5 @@
   do {
     errno = 0;
-    test  = SH_SETSIGMASK(SIG_UNBLOCK, &set_proc, NULL);
+    test  = sigprocmask(SIG_UNBLOCK, &set_proc, NULL);
   } while (test < 0 && errno == EINTR);
 
@@ -1666,8 +1382,7 @@
       if ((test == -1) && (errno != EINVAL)) 
 	{
-	  char errbuf[SH_ERRBUF_SIZE];
 	  status = errno;
 	  sh_error_handle ((-1), FIL__, __LINE__, status, MSG_W_SIG,
-			   sh_error_message (status, errbuf, sizeof(errbuf)), sig_num);
+			   sh_error_message (status), sig_num);
 	}
     }
@@ -1678,18 +1393,4 @@
 /* Get the local hostname (FQDN)
  */
-static char * sh_tolower (char * s)
-{
-  char * ret = s;
-  if (s)
-    {
-      for (; *s; ++s)
-	{ 
-	  *s = tolower((unsigned char) *s);
-	}
-    }
-  return ret;
-}
-
-
 #include <sys/socket.h> 
 
@@ -1701,18 +1402,4 @@
 
 #include <arpa/inet.h>
-
-const char * sh_unix_h_name (struct hostent * host_entry)
-{
-	char ** p;
-	if (strchr(host_entry->h_name, '.')) {
-		return host_entry->h_name;
-	} else {
-		for (p = host_entry->h_aliases; *p; ++p) {
-			if (strchr(*p, '.'))
-				return *p;
-		}
-	}
-	return host_entry->h_name;
-}
 
 /* uname() on FreeBSD is broken, because the 'nodename' buf is too small
@@ -1724,11 +1411,10 @@
 {
   struct utsname   buf;
+  struct hostent * he1;
   int              i;
-  unsigned int     ddot;
+  int              ddot = 0;
   int              len;
   char           * p;
   char             hostname[256];
-  char             numeric[SH_IP_BUF];
-  char           * canonical;
 
 
@@ -1736,5 +1422,5 @@
 
   (void) uname (&buf);
-  /* flawfinder: ignore */ /* ff bug, ff sees system() */
+
   sl_strlcpy (sh.host.system,  buf.sysname, SH_MINIBUF);
   sl_strlcpy (sh.host.release, buf.release, SH_MINIBUF);
@@ -1776,34 +1462,37 @@
     }
 
-  canonical = sh_ipvx_canonical(hostname, numeric, sizeof(numeric));
-
-  if (canonical == NULL)
-    {
+  he1 = sh_gethostbyname(hostname);
+
+  if (he1 == NULL)
+    {
+      dlog(1, FIL__, __LINE__, 
+	   _("According to uname, your nodename is %s, but your resolver\nlibrary cannot resolve this nodename to a FQDN. For more information, see the entry about self-resolving under 'Most frequently' in the FAQ that you will find in the docs/ subdirectory.\n"),
+	   hostname);
       sl_strlcpy (sh.host.name, hostname,    SH_PATHBUF);
-      sh_tolower (sh.host.name);
     }
   else
     {
-      sl_strlcpy (sh.host.name, canonical,   SH_PATHBUF);
-      SH_FREE(canonical);
-    }
+      sl_strlcpy (sh.host.name, he1->h_name, SH_PATHBUF);
+    }
+  
 
   /* check whether it looks like a FQDN
    */
   len = sl_strlen(sh.host.name);
-  ddot = 0;
   for (i = 0; i < len; ++i) 
     if (sh.host.name[i] == '.') ++ddot; 
 
-  if (ddot == 0)
+  if (ddot == 0 && he1 != NULL)
     { 
       dlog(1, FIL__, __LINE__, 
 	   _("According to uname, your nodename is %s, but your resolver\nlibrary cannot resolve this nodename to a FQDN.\nRather, it resolves this to %s.\nFor more information, see the entry about self-resolving under\n'Most frequently' in the FAQ that you will find in the docs/ subdirectory.\n"),
 	   hostname, sh.host.name);
-      sl_strlcpy (sh.host.name, numeric, SH_PATHBUF);
+      sl_strlcpy (sh.host.name, 
+		  inet_ntoa (*(struct in_addr *) he1->h_addr), 
+		  SH_PATHBUF);
       SL_RET0(_("sh_unix_localhost"));
     } 
 
-  if (sh_ipvx_is_numeric(sh.host.name)) 
+  if (is_numeric(sh.host.name)) 
     {
       dlog(1, FIL__, __LINE__, 
@@ -1814,49 +1503,31 @@
   SL_RET0(_("sh_unix_localhost"));
 }
-
 #else
-
-/* 
- * --FreeBSD code 
- */
-#if defined(HAVE_UNAME)
-#include <sys/utsname.h>
-#endif
 void sh_unix_localhost()
 {
-#if defined(HAVE_UNAME)
-  struct utsname   buf;
-#endif
+  struct hostent * he1;
   int              i;
-  int              ddot;
+  int              ddot = 0;
   int              len;
   char             hostname[1024];
-  char             numeric[SH_IP_BUF];
-  char           * canonical;
+
 
   SL_ENTER(_("sh_unix_localhost"));
-
-#if defined(HAVE_UNAME)
-  (void) uname (&buf);
-  /* flawfinder: ignore */ /* ff bug, ff sees system() */
-  sl_strlcpy (sh.host.system,  buf.sysname, SH_MINIBUF);
-  sl_strlcpy (sh.host.release, buf.release, SH_MINIBUF);
-  sl_strlcpy (sh.host.machine, buf.machine, SH_MINIBUF);
-#endif
 
   (void) gethostname (hostname, 1024);
   hostname[1023] = '\0';
-
-  canonical = sh_ipvx_canonical(hostname, numeric, sizeof(numeric));
-
-  if (canonical == NULL)
-    {
-      sl_strlcpy (sh.host.name, hostname, SH_PATHBUF);
-      sh_tolower (sh.host.name);
+  he1 = sh_gethostbyname(hostname);
+
+  if (he1 != NULL)
+    {
+      sl_strlcpy (sh.host.name, he1->h_name, SH_PATHBUF);
     }
   else
     {
-      sl_strlcpy (sh.host.name, canonical,   SH_PATHBUF);
-      SH_FREE(canonical);
+      dlog(1, FIL__, __LINE__, 
+	   _("According to gethostname, your nodename is %s, but your resolver\nlibrary cannot resolve this nodename to a FQDN.\nFor more information, see the entry about self-resolving under\n'Most frequently' in the FAQ that you will find in the docs/ subdirectory.\n"),
+	   hostname);
+      sl_strlcpy (sh.host.name, _("localhost"), SH_PATHBUF);
+      SL_RET0(_("sh_unix_localhost"));
     }
 
@@ -1864,5 +1535,4 @@
    */
   len = sl_strlen(sh.host.name);
-  ddot = 0;
   for (i = 0; i < len; ++i) 
     if (sh.host.name[i] == '.') ++ddot; 
@@ -1872,9 +1542,11 @@
 	   _("According to uname, your nodename is %s, but your resolver\nlibrary cannot resolve this nodename to a FQDN.\nRather, it resolves this to %s.\nFor more information, see the entry about self-resolving under\n'Most frequently' in the FAQ that you will find in the docs/ subdirectory.\n"),
 	   hostname, sh.host.name);
-      sl_strlcpy (sh.host.name, numeric, SH_PATHBUF);
+      sl_strlcpy (sh.host.name, 
+		  inet_ntoa (*(struct in_addr *) he1->h_addr), 
+		  SH_PATHBUF);
       SL_RET0(_("sh_unix_localhost"));
     }
 
-  if (sh_ipvx_is_numeric(sh.host.name)) 
+  if (is_numeric(sh.host.name)) 
     {
       dlog(1, FIL__, __LINE__, 
@@ -1895,20 +1567,15 @@
    */
 #if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
-  if (skey->mlock_failed == S_FALSE)
-    {
-      if ( (-1) == sh_unix_mlock( FIL__, __LINE__, 
-				  (char *) skey, sizeof (sh_key_t)) ) 
+  if (skey->mlock_failed == SL_FALSE)
+    {
+      if ( (-1) == sh_unix_mlock( (char *) skey, sizeof (sh_key_t)) ) 
 	{
-	  SH_MUTEX_LOCK_UNSAFE(mutex_skey);
-	  skey->mlock_failed = S_TRUE;
-	  SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
+	  skey->mlock_failed = SL_TRUE;
 	}
     }
 #else
-  if (skey->mlock_failed == S_FALSE)
-    {
-      SH_MUTEX_LOCK_UNSAFE(mutex_skey);
-      skey->mlock_failed = S_TRUE;
-      SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
+  if (skey->mlock_failed == SL_FALSE)
+    {
+      skey->mlock_failed = SL_TRUE;
     }
 #endif
@@ -1920,5 +1587,5 @@
 char * chroot_dir = NULL;
 
-int sh_unix_set_chroot(const char * str)
+int sh_unix_set_chroot(char * str)
 {
   size_t len;
@@ -1931,5 +1598,5 @@
     {
       len = strlen(str) + 1;
-      chroot_dir = calloc(1,strlen(str) + 1);  /* only once */
+      chroot_dir = malloc(strlen(str) + 1);  /* only once */
       if (!chroot_dir)
 	{
@@ -1944,5 +1611,5 @@
 }
 
-int sh_unix_chroot(void)
+int sh_unix_chroot()
 {
   int status;
@@ -1953,11 +1620,9 @@
       if ( (-1) == status ) 
 	{
-	  char errbuf[SH_ERRBUF_SIZE];
 	  status = errno;
 	  sh_error_handle ((-1), FIL__, __LINE__, status, MSG_W_CHDIR,
-			   sh_error_message (status, errbuf, sizeof(errbuf)), chroot_dir);
+			   sh_error_message (status), chroot_dir);
 	  aud_exit(FIL__, __LINE__, EXIT_FAILURE);
 	}
-      /* flawfinder: ignore */
       return (chroot(chroot_dir));
     }
@@ -1966,5 +1631,5 @@
 /* #ifdef SH_WITH_SERVER */
 #else
-int sh_unix_chroot(void) { return 0; }
+int sh_unix_chroot() { return 0; }
 #endif
 
@@ -1973,5 +1638,5 @@
 static int block_setdeamon = 0;
 
-int sh_unix_setdeamon(const char * dummy)
+int sh_unix_setdeamon(char * dummy)
 {
   int    res = 0;
@@ -1983,5 +1648,5 @@
 
   if (dummy == NULL)
-    sh.flag.isdaemon = S_TRUE;
+    sh.flag.isdaemon = ON;
   else 
     res = sh_util_flagval (dummy, &sh.flag.isdaemon);
@@ -1996,8 +1661,8 @@
 #endif
 
-int sh_unix_setnodeamon(const char * dummy)
+int sh_unix_setnodeamon(char * dummy)
 {
   int    res = 0;
-  
+
   SL_ENTER(_("sh_unix_setnodeamon"));
 
@@ -2006,5 +1671,5 @@
 
   if (dummy == NULL)
-    sh.flag.isdaemon = S_FALSE;
+    sh.flag.isdaemon = OFF;
   else 
     res = sh_util_flagval (dummy, &sh.flag.isdaemon);
@@ -2022,9 +1687,6 @@
   pid_t  oldpid = getpid();
 #if defined(SH_WITH_SERVER) 
-  extern int sh_socket_open_int (void);
-#endif
-  char errbuf[SH_ERRBUF_SIZE];
-
-  extern void sh_kill_sub();
+  extern int sh_socket_open_int ();
+#endif
 
   SL_ENTER(_("sh_unix_init"));
@@ -2037,7 +1699,5 @@
     case 0:  break;                             /* child process continues */
     case -1: SL_RETURN((-1),_("sh_unix_init")); /* error                   */
-    default:                                    /* parent process exits    */
-      sh_kill_sub(); 
-      aud__exit(FIL__, __LINE__, 0);
+    default: aud__exit(FIL__, __LINE__, 0);     /* parent process exits    */
     }
 
@@ -2048,12 +1708,9 @@
 
     setsid();            /* should not fail         */
-    sh.pid = (UINT64) getpid();
 
     switch (aud_fork(FIL__, __LINE__)) {
     case 0:  break;                             /* child process continues */
     case -1: SL_RETURN((-1),_("sh_unix_init")); /* error                   */
-    default:                                    /* parent process exits    */
-      sh_kill_sub(); 
-      aud__exit(FIL__, __LINE__, 0);
+    default: aud__exit(FIL__, __LINE__, 0);     /* parent process exits    */
     }
 
@@ -2061,5 +1718,4 @@
      */
     sh_unix_memlock();
-    sh.pid = (UINT64) getpid();
 
   } else {
@@ -2078,5 +1734,5 @@
       status = errno;
       sh_error_handle ((-1), FIL__, __LINE__, status, MSG_W_CHDIR,
-		       sh_error_message (status, errbuf, sizeof(errbuf)), "/");
+		       sh_error_message (status), "/");
       aud_exit(FIL__, __LINE__, EXIT_FAILURE);
     }
@@ -2107,40 +1763,14 @@
   sh_unix_zeroenv();
 
+
+  /* close all file descriptors, and 
+   * open first three streams to /dev/null 
+   */
   if (goDaemon == 1)
     {
-      /* Close first tree file descriptors 
-       */ 
-      sl_close_fd (FIL__, __LINE__, 0);  /* if running as daemon */
-      sl_close_fd (FIL__, __LINE__, 1);  /* if running as daemon */
-      sl_close_fd (FIL__, __LINE__, 2);  /* if running as daemon */
-
-      /* Enable full error logging
-       */
-      sh_error_only_stderr (S_FALSE);
-
-      /* open first three streams to /dev/null 
-       */
-      status = aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 0);
-      if (status < 0)
-	{
-	  status = errno;
-	  sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN, 
-			  sh_error_message(status, errbuf, sizeof(errbuf)), _("open"));
-	  aud_exit(FIL__, __LINE__, EXIT_FAILURE);
-	}
-
-      status = retry_aud_dup(FIL__, __LINE__, 0); 
-      if (status >= 0)
-	retry_aud_dup(FIL__, __LINE__, 0);
-
-      if (status < 0)
-	{
-	  status = errno;
-	  sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN, 
-			  sh_error_message(status, errbuf, sizeof(errbuf)), _("dup"));
-	  aud_exit(FIL__, __LINE__, EXIT_FAILURE);
-	}
-
-      sh_error_enable_unsafe (S_TRUE);
+      sh_unix_closeall (0, -1);  /* if running as daemon */
+      aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 0);
+      retry_aud_dup(FIL__, __LINE__, 0); 
+      retry_aud_dup(FIL__, __LINE__, 0);
 #if defined(HAVE_LIBPRELUDE)
       sh_prelude_reset ();
@@ -2175,5 +1805,5 @@
   else
     {
-      sh_error_enable_unsafe (S_TRUE);
+      sh_unix_closeall(3, -1);  /* if not daemon */
 #if defined(HAVE_LIBPRELUDE)
       sh_prelude_reset ();
@@ -2191,5 +1821,5 @@
       status = errno;
       sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN, 
-			  sh_error_message(status, errbuf, sizeof(errbuf)), _("chroot"));
+			  sh_error_message(status), _("chroot"));
       aud_exit(FIL__, __LINE__, EXIT_FAILURE);
     }
@@ -2200,75 +1830,4 @@
 
   SL_RETURN((0),_("sh_unix_init"));
-}
-
-/* --- run a command, securely --- */
-
-int sh_unix_run_command (const char * str)
-{
-  pid_t  pid;
-  char * arg[4];
-  char * env[5];
-  char * path = sh_util_strdup(_("/bin/sh"));
-
-  int  status = -1;
-
-  arg[0] = sh_util_strdup(_("/bin/sh"));
-  arg[1] = sh_util_strdup(_("-c"));
-  arg[2] = sh_util_strdup(str);
-  arg[3] = NULL;
-
-  env[0] = sh_util_strdup(_("PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/ucb"));
-  env[1] = sh_util_strdup(_("SHELL=/bin/sh"));
-  env[2] = sh_util_strdup(_("IFS= \t\n"));
-  if (getenv("TZ")) {                         /* flawfinder: ignore */
-    char * tz = sh_util_strdup(getenv("TZ")); /* flawfinder: ignore */
-    size_t tzlen = strlen(tz);
-    if (S_TRUE == sl_ok_adds (4, tzlen)) {
-	env[3] = SH_ALLOC(4+tzlen);
-	sl_strlcpy(env[3], "TZ=", 4);
-	sl_strlcat(env[3], tz   , 4+tzlen);
-    } else {
-      env[3] = NULL;
-    }
-  } else {
-    env[3] = NULL;
-  }
-  env[4] = NULL;
-
-  pid = fork();
-
-  if (pid == (pid_t)(-1))
-    {
-      return -1;
-    }
-
-  else if (pid == 0) /* child */
-    {
-      memset(skey, 0, sizeof(sh_key_t));
-      (void) umask(S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH);
-      sh_unix_closeall (3, -1, S_TRUE); /* in child process */
-      execve(path, arg, env);
-      _exit(EXIT_FAILURE);
-    }
-
-  else /* parent */
-    {
-      int r;
-
-      while((r = waitpid(pid, &status, WUNTRACED)) != pid && r != -1) ;
-
-#if !defined(USE_UNO)
-      if (r == -1 || !WIFEXITED(status)) 
-	{
-	  status = -1;
-	}
-      else
-	{
-	  status = WEXITSTATUS(status);
-	}
-#endif
-     }
-
-  return status;
 }
 
@@ -2286,8 +1845,5 @@
   struct tm   aa;
   struct tm   bb;
-
-  struct tm * aptr;
-  struct tm * bptr;
-
+  struct tm * cc;
   int  sign =  0;
   int  diff =  0;
@@ -2297,50 +1853,32 @@
   SL_ENTER(_("t_zone"));
 
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GMTIME_R)
-  aptr = gmtime_r (xx, &aa);
-#else
-  aptr = gmtime(xx);
-  if (aptr)
-    memcpy (&aa, aptr, sizeof(struct tm));
-#endif
-
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
-  bptr = localtime_r (xx, &bb);
-#else
-  bptr = localtime(xx);
-  if (bptr) 
-    memcpy (&bb, bptr, sizeof(struct tm));
-#endif
-
-  if (bptr && aptr)
-    {
-      /* Check for datum wrap-around.
-       */
-      if      ((aa.tm_mday == 1) && (aa.tm_mday < bb.tm_mday) && (aa.tm_hour < bb.tm_hour))
-	sign = ( 1);
-      else if (aa.tm_year < bb.tm_year)
-	sign = (-1);
-      else if (aa.tm_mon  < bb.tm_mon)
-	sign = (-1);
-      else if (aa.tm_mday < bb.tm_mday)
-	sign = (-1);
-      else if (bb.tm_year < aa.tm_year)
-	sign = ( 1);
-      else if (bb.tm_mon  < aa.tm_mon)
-	sign = ( 1);
-      else if (bb.tm_mday < aa.tm_mday)
-	sign = ( 1);
-      
-      diff = aa.tm_hour * 60 + aa.tm_min;
-      diff = (bb.tm_hour * 60 + bb.tm_min) - diff;
-      diff = diff - (sign * 24 * 60);   /* datum wrap-around correction */
-      hh = diff / 60;
-      mm = diff - (hh * 60);
-      sl_snprintf (tz, sizeof(tz), _("%+03d%02d"), hh, mm);                /* known to fit  */
-    }
-  else
-    {
-      sl_snprintf (tz, sizeof(tz), _("%+03d%02d"), 0, 0);
-    }
+
+  cc = gmtime (xx);
+  memcpy (&aa, cc, sizeof(struct tm));
+  cc = localtime (xx);
+  memcpy (&bb, cc, sizeof(struct tm));
+
+  /* Check for datum wrap-around.
+   */
+  if      (aa.tm_year < bb.tm_year)
+    sign = (-1);
+  else if (aa.tm_mon  < bb.tm_mon)
+    sign = (-1);
+  else if (aa.tm_mday < bb.tm_mday)
+    sign = (-1);
+  else if (bb.tm_year < aa.tm_year)
+    sign = ( 1);
+  else if (bb.tm_mon  < aa.tm_mon)
+    sign = ( 1);
+  else if (bb.tm_mday < aa.tm_mday)
+    sign = ( 1);
+
+  diff = aa.tm_hour * 60 + aa.tm_min;
+  diff = (bb.tm_hour * 60 + bb.tm_min) - diff;
+  diff = diff - (sign * 24 * 60);   /* datum wrap-around correction */
+  hh = diff / 60;
+  mm = diff - (hh * 60);
+  sprintf (tz, _("%+03d%02d"), hh, mm);                /* known to fit  */
+
   SL_RETURN(tz, _("t_zone"));
 }
@@ -2371,5 +1909,5 @@
 }
 
-int sh_unix_settimeserver (const char * address)
+int sh_unix_settimeserver (char * address)
 {
 
@@ -2408,14 +1946,13 @@
 #endif
 
-char * sh_unix_time (time_t thetime, char * buffer, size_t len)
+char * sh_unix_time (time_t thetime)
 {
 
   int           status;
-  char          AsciiTime[81];                       /* local time   */
+
   time_t        time_now;
-  struct tm   * time_ptr = NULL;
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
-  struct tm     time_tm;
-#endif
+  struct tm   * time_ptr;
+  static char   AsciiTime[81];                       /* local time   */
+  static char   RetTime[81];                         /* local time   */
 #ifdef SH_USE_XML
   static char   deftime[] = N_("0000-00-00T00:00:00"); /* default time */
@@ -2447,17 +1984,4 @@
       else /* have a timeserver address */
 	{ 
-	  /* don't call timeserver more than once per second */
-	  static time_t  time_old   = 0;
-	  time_t         time_new;
-	  static time_t  time_saved = 0;
-	  (void) time (&time_new);
-	  if ((time_new == time_old) && (time_saved != 0))
-	    {
-	      time_now = time_saved;
-	      goto end;
-	    }
-	  time_old = time_new;
-
-
 	  fd = connect_port_2 (sh.srvtime.name, sh.srvtime.alt, 
 			       IPPORT_TIMESERVER, 
@@ -2472,5 +1996,5 @@
 				   _("time"), sh.srvtime.name);
 		}
-	      sl_close_fd(FIL__, __LINE__, fd);
+	      close(fd);
 	    }
 	  else
@@ -2479,5 +2003,4 @@
 			       MSG_E_NET, errmsg, error_call,
 			       _("time"), sh.srvtime.name);
-	      errflag = error_num;
 	      fail = 1;
 	    }
@@ -2485,10 +2008,6 @@
 	  if (fail == 0) 
 	    { 
-	      unsigned long   ltmp;
-	      UINT32          ttmp;
-	      memcpy(&ttmp, net_time, sizeof(UINT32)); ltmp = ttmp;
-	      time_now = ntohl(ltmp) - UNIXEPOCH;
-	      time_saved = time_now;
-
+	      time_now = ntohl(* (long *) net_time) - UNIXEPOCH;
+	      /* fprintf(stderr, "TIME IS %ld\n", time_now); */
 	      if (failerr == 1) {
 		failerr = 0;
@@ -2501,6 +2020,4 @@
 	    {
 	      (void) time (&time_now);
-	      time_saved = 0;
-
 	      if (failerr == 0)
 		{
@@ -2511,6 +2028,4 @@
 		}
 	    }
-	end:
-	  ; /* 'label at end of compound statement' */
 	}
     }
@@ -2535,21 +2050,12 @@
 #endif
 
-  if (time_now == (-1) )
-    {
-      sl_strlcpy(buffer, _(deftime), len);
-      SL_RETURN(buffer, _("sh_unix_time"));
-    }
+  if (time_now == (-1) ) 
+    SL_RETURN( _(deftime), _("sh_unix_time"));
   else
-    {
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
-      time_ptr   = localtime_r (&time_now, &time_tm);
-#else
-      time_ptr   = localtime (&time_now);
-#endif
-    }
+    time_ptr   = localtime (&time_now);
 
   if (time_ptr != NULL) 
     {
-      status = strftime (AsciiTime, sizeof(AsciiTime),
+      status = strftime (AsciiTime, 80,
 #ifdef SH_USE_XML
 			 _("%Y-%m-%dT%H:%M:%S%%s"),
@@ -2559,28 +2065,22 @@
 			 time_ptr);
 
-      sl_snprintf(buffer, len, AsciiTime, t_zone(&time_now));
-
-      if ( (status == 0) || (status == sizeof(AsciiTime)) )
-	{
-	  sl_strlcpy(buffer, _(deftime), len);
-	  SL_RETURN( buffer, _("sh_unix_time"));
-	}
+      sl_snprintf(RetTime, 80, AsciiTime, t_zone(&time_now));
+
+      if ( (status == 0) || (status == 80) ) 
+	SL_RETURN( _(deftime), _("sh_unix_time"));
       else
-	{
-	  SL_RETURN(buffer, _("sh_unix_time"));
-	}
+	SL_RETURN( &RetTime[0], _("sh_unix_time"));
     }
 
   /* last resort
    */
-  sl_strlcpy(buffer, _(deftime), len);
-  SL_RETURN( buffer, _("sh_unix_time"));
+  SL_RETURN( _(deftime), _("sh_unix_time"));
 }
 
 static int sh_unix_use_localtime = S_FALSE;
 
-/* whether to use localtime for file timestamps in logs
+/* whether to use localtime for file timesatams in logs
  */
-int sh_unix_uselocaltime (const char * c)
+int sh_unix_uselocaltime (char * c)
 {
   int i;
@@ -2591,5 +2091,5 @@
 }
     
-char * sh_unix_gmttime (time_t thetime, char * buffer, size_t len)
+char * sh_unix_gmttime (time_t thetime)
 {
 
@@ -2597,8 +2097,5 @@
 
   struct tm   * time_ptr;
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS)
-  struct tm     time_tm;
-#endif
-  char   AsciiTime[81];                       /* GMT time   */
+  static char   AsciiTime[81];                       /* GMT time   */
 #ifdef SH_USE_XML
   static char   deftime[] = N_("0000-00-00T00:00:00"); /* default time */
@@ -2610,19 +2107,8 @@
 
   if (sh_unix_use_localtime == S_FALSE)
-    {
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GMTIME_R)
-      time_ptr   = gmtime_r (&thetime, &time_tm);
-#else
-      time_ptr   = gmtime (&thetime);
-#endif
-    }
+    time_ptr   = gmtime (&thetime);
   else
-    {
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
-      time_ptr   = localtime_r (&thetime, &time_tm);
-#else
-      time_ptr   = localtime (&thetime);
-#endif
-    }
+    time_ptr   = localtime (&thetime);
+
   if (time_ptr != NULL) 
     {
@@ -2635,375 +2121,117 @@
 			 time_ptr);
 
-      if ( (status == 0) || (status == 80) )
-	sl_strlcpy(buffer, _(deftime), len);
+      if ( (status == 0) || (status == 80) ) 
+	SL_RETURN( _(deftime), _("sh_unix_gmttime"));
       else
-	sl_strlcpy(buffer, AsciiTime, len);
-      SL_RETURN( buffer, _("sh_unix_gmttime"));
+	SL_RETURN( &AsciiTime[0], _("sh_unix_gmttime"));
     }
 
   /* last resort
    */
-  sl_strlcpy(buffer, _(deftime), len);
-  SL_RETURN( buffer, _("sh_unix_gmttime"));
-}
-
-
-char *  sh_unix_getUIDdir (int level, uid_t uid, char * out, size_t len)
+  SL_RETURN( _(deftime), _("sh_unix_gmttime"));
+}
+
+
+
+char *  sh_unix_getUIDdir (int level, uid_t uid)
 {
   struct passwd * tempres;
   int    status = 0;
 
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
-  struct passwd pwd;
-  char   * buffer;
-#endif
-  char errbuf[SH_ERRBUF_SIZE];
-
   SL_ENTER(_("sh_unix_getUIDdir"));
 
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
-  buffer = SH_ALLOC(SH_PWBUF_SIZE);
-  sh_getpwuid_r(uid, &pwd, buffer, SH_PWBUF_SIZE, &tempres);
-#else
   errno = 0;
   tempres = sh_getpwuid(uid);
   status = errno;
-#endif
 
   if (tempres == NULL) {
     sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_PWNULL,
-		     sh_error_message(status, errbuf, sizeof(errbuf)),
+		     sh_error_message(status),
 		     _("getpwuid"), (long) uid, _("completely missing"));
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
-    SH_FREE(buffer);
-#endif
     SL_RETURN( NULL, _("sh_unix_getUIDdir"));
   }
 
   if (tempres->pw_dir != NULL) {
-    sl_strlcpy(out, tempres->pw_dir, len);
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
-    SH_FREE(buffer);
-#endif
-    SL_RETURN( out, _("sh_unix_getUIDdir"));
+    SL_RETURN( tempres->pw_dir, _("sh_unix_getUIDdir"));
   } else {
     sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_PWNULL,
-		     sh_error_message(status, errbuf, sizeof(errbuf)),
+		     sh_error_message(status),
 		     _("getpwuid"), (long) uid, _("pw_dir"));
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
-    SH_FREE(buffer);
-#endif
     SL_RETURN( NULL, _("sh_unix_getUIDdir"));
   }
 }
 
-/* ------------------- Caching ----------------*/
-#include "zAVLTree.h"
-
-#define CACHE_GID 0
-#define CACHE_UID 1
-
-struct user_id {
-  char  * name;
-  uid_t   id;
-  struct user_id * next;
-};
-
-static struct user_id  * uid_list = NULL;
-static struct user_id  * gid_list = NULL;
-
-SH_MUTEX_STATIC(mutex_cache, PTHREAD_MUTEX_INITIALIZER);
-
-static void sh_userid_free(struct user_id * item)
-{
-  while (item)
-    {
-      struct user_id * user = item;
-      item = item->next;
-
-      SH_FREE(user->name);
-      SH_FREE(user);
-    }
-  return;
-}
-
-void sh_userid_destroy ()
-{
-  struct user_id * tmp_uid;
-  struct user_id * tmp_gid;
-
-  SH_MUTEX_LOCK_UNSAFE(mutex_cache);
-  tmp_gid  = gid_list;
-  gid_list = NULL;
-  tmp_uid  = uid_list;
-  uid_list = NULL;
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_cache);
-
-  sh_userid_free(tmp_uid);
-  sh_userid_free(tmp_gid);
-  return;
-}
-
-static void sh_userid_additem(struct user_id * list, struct user_id * item)
-{
-  if (list)
-    {
-      while (list && list->next)
-	list = list->next;
-      list->next = item;
-    }
-  return;
-}
-
-static void sh_userid_add(uid_t id, char * username, int which)
-{
-  size_t len;
-  struct user_id * user = SH_ALLOC(sizeof(struct user_id));
-
-  if (username)
-    len  = strlen(username) + 1;
-  else
-    len = 1;
-
-  user->name = SH_ALLOC(len);
-  user->id   = id;
-  if (username)
-    sl_strlcpy(user->name, username, len);
-  else
-    user->name[0] = '\0';
-  user->next = NULL;
-
-  SH_MUTEX_LOCK(mutex_cache);
-  if (which == CACHE_UID)
-    {
-      if (!uid_list)
-	uid_list = user;
-      else
-	sh_userid_additem(uid_list, user);
-    }
-  else
-    {
-      if (!gid_list)
-	gid_list = user;
-      else
-	sh_userid_additem(gid_list, user);
-    }
-  SH_MUTEX_UNLOCK(mutex_cache);
-
-  return;
-}
-
-static char * sh_userid_search(struct user_id * list, uid_t id)
-{
-  while (list)
-    {
-      if (list->id == id)
-	return list->name;
-      list = list->next;
-    }
-  return NULL;
-}
-
-static char * sh_userid_get (uid_t id, int which, char * out, size_t len)
-{
-  char * user = NULL;
-
-  SH_MUTEX_LOCK_UNSAFE(mutex_cache);
-  if (which == CACHE_UID)
-    user = sh_userid_search(uid_list, id);
-  else
-    user = sh_userid_search(gid_list, id);
-  if (user)
-    {
-      sl_strlcpy(out, user, len);
-      user = out;
-    }
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_cache);
-
-  return user;
-}
-
-/* --------- end caching code --------- */
-
-#include "sh_subuid.h"
-
-char *  sh_unix_getUIDname (int level, uid_t uid, char * out, size_t len)
+char *  sh_unix_getUIDname (int level, uid_t uid)
 {
   struct passwd * tempres;
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
-  struct passwd pwd;
-  char   * buffer;
-#endif
   int             status = 0;
-  char errbuf[SH_ERRBUF_SIZE];
-  char * tmp;
+  static uid_t    old_uid;
+  static char     name[32] = { '\0' };
 
   SL_ENTER(_("sh_unix_getUIDname"));
 
-  tmp = sh_userid_get(uid, CACHE_UID, out, len);
-
-  if (tmp)
-    {
-      if (tmp[0] != '\0')
-	{
-	  SL_RETURN( out, _("sh_unix_getUIDname"));
-	}
-      else
-	{
-	  SL_RETURN( NULL, _("sh_unix_getUIDname"));
-	}
-    }
-
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
-  buffer = SH_ALLOC(SH_PWBUF_SIZE);
-  sh_getpwuid_r(uid, &pwd, buffer, SH_PWBUF_SIZE, &tempres);
-#else
+  if ((uid == old_uid) && (name[0] != '\0')) {
+    SL_RETURN( name, _("sh_unix_getUIDname"));
+  }
+
   errno = 0;
   tempres = sh_getpwuid(uid);
   status = errno;
-#endif
-
-  /* case 1: we have it
-   */
-  if (tempres && tempres->pw_name != NULL) 
-    {
-
-      sl_strlcpy(out, tempres->pw_name, len);
-      sh_userid_add(uid, out, CACHE_UID);
-      
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
-      SH_FREE(buffer);
-#endif
-
-      SL_RETURN( out, _("sh_unix_getUIDname"));
-    }
-
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
-  SH_FREE(buffer);
-#endif
-  
-  if (tempres == NULL) 
-    {
-      char * pwname = sh_get_subuid ((unsigned long) uid);
-
-      if (pwname)
-	{
-	  sl_strlcpy(out, pwname, len);
-	  sh_userid_add(uid, out, CACHE_UID);
-	  SL_RETURN( out, _("sh_unix_getUIDname"));
-	}
-
-      sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_PWNULL,
-		       sh_error_message(status, errbuf, sizeof(errbuf)),
-		       _("getpwuid"), (long) uid, _("completely missing"));
-      sh_userid_add(uid, NULL, CACHE_UID);
-      SL_RETURN( NULL, _("sh_unix_getUIDname"));
-    }
-
-
-  /* getwpuid returns struct, but no pw_name
-   */
-  sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_PWNULL,
-		   sh_error_message(status, errbuf, sizeof(errbuf)),
-		   _("getpwuid"), (long) uid, _("pw_user"));
-  SL_RETURN( NULL, _("sh_unix_getUIDname"));
-}
-
-char *  sh_unix_getGIDname (int level, gid_t gid, char * out, size_t len)
+ 
+  if (tempres == NULL) {
+    sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_PWNULL,
+		     sh_error_message(status),
+		     _("getpwuid"), (long) uid, _("completely missing"));
+    SL_RETURN( NULL, _("sh_unix_getUIDname"));
+  }
+
+
+  if (tempres->pw_name != NULL) {
+    sl_strlcpy(name, tempres->pw_name, sizeof(name));
+    old_uid = uid;
+    SL_RETURN( name, _("sh_unix_getUIDname"));
+  } else {
+    sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_PWNULL,
+		     sh_error_message(status),
+		     _("getpwuid"), (long) uid, _("pw_user"));
+    SL_RETURN( NULL, _("sh_unix_getUIDname"));
+  }
+}
+
+char *  sh_unix_getGIDname (int level, gid_t gid)
 {
   struct group  * tempres;
   int             status = 0;
-
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
-  struct group    grp;
-  char          * buffer;
-#endif
-  char errbuf[SH_ERRBUF_SIZE];
-  char * tmp;
+  static gid_t    old_gid;
+  static char     name[32] = { '\0' };
+
+  SL_ENTER(_("sh_unix_getGIDname"));
+
+  if ((gid == old_gid) && (name[0] != '\0')) {
+    SL_RETURN( name, _("sh_unix_getUIDname"));
+  }
   
-  SL_ENTER(_("sh_unix_getGIDname"));
-
-  tmp = sh_userid_get((uid_t)gid, CACHE_GID, out, len);
-
-  if (tmp)
-    {
-      if (tmp[0] != '\0')
-	{
-	  SL_RETURN( out, _("sh_unix_getGIDname"));
-	}
-      else
-	{
-	  SL_RETURN( NULL, _("sh_unix_getGIDname"));
-	}
-    }
-
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
-  buffer = SH_ALLOC(SH_GRBUF_SIZE);
-  status = sh_getgrgid_r(gid, &grp, buffer, SH_GRBUF_SIZE, &tempres);
-#else
   errno = 0;
   tempres = sh_getgrgid(gid);
   status = errno;
-#endif
-
-  if (status == ERANGE) 
-    {
-      static int seen = 0;
-
-      if (seen == 0)
-	{
-	  sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, EINVAL, MSG_E_GRNULL,
-			   sh_error_message(status, errbuf, sizeof(errbuf)),
-			   _("getgrgid"), (long) gid, _("line too long in group entry"));
-	  ++seen;
-	}
+
+  if (tempres == NULL) {
+    sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_GRNULL,
+		     sh_error_message(status),
+		     _("getgrgid"), (long) gid, _("completely missing"));
       
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
-      SH_FREE(buffer);
-#endif
-
-      sh_userid_add(gid, NULL, CACHE_GID);
-      SL_RETURN( NULL, _("sh_unix_getGIDname"));
-    }
-
-  if (tempres && tempres->gr_name != NULL) 
-    {
-
-      sl_strlcpy(out, tempres->gr_name, len);
-      sh_userid_add((uid_t)gid, out, CACHE_GID);
-      
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
-      SH_FREE(buffer);
-#endif
-
-      SL_RETURN( out, _("sh_unix_getGIDname"));
-    } 
-
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
-  SH_FREE(buffer);
-#endif
-
-  if (tempres == NULL) 
-    {
-      char * grname = sh_get_subgid ((unsigned long) gid);
-
-      if (grname)
-	{
-	  sl_strlcpy(out, grname, len);
-	  sh_userid_add((uid_t)gid, out, CACHE_GID);
-	  SL_RETURN( out, _("sh_unix_getGIDname"));
-	}
-
-      sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_GRNULL,
-		       sh_error_message(status, errbuf, sizeof(errbuf)),
-		       _("getgrgid"), (long) gid, _("completely missing"));
-      sh_userid_add(gid, NULL, CACHE_GID);
-      SL_RETURN( NULL, _("sh_unix_getGIDname"));
-    }
-
-  sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_GRNULL,
-		   sh_error_message(status, errbuf, sizeof(errbuf)),
-		   _("getgrgid"), (long) gid, _("gr_name"));
-  SL_RETURN( NULL, _("sh_unix_getGIDname"));
+    SL_RETURN( NULL, _("sh_unix_getGIDname"));
+  }
+
+  if (tempres->gr_name != NULL) {
+    sl_strlcpy(name, tempres->gr_name, sizeof(name));
+    old_gid = gid;
+    SL_RETURN( name, _("sh_unix_getGIDname"));
+  } else {
+    sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_GRNULL,
+		     sh_error_message(status),
+		     _("getgrgid"), (long) gid, _("gr_name"));
+    SL_RETURN( NULL, _("sh_unix_getGIDname"));
+  }
 }
 
@@ -3012,6 +2240,4 @@
   char          * p;
   uid_t  seuid, sruid;
-  char   user[USER_MAX];
-  char   dir[SH_PATHBUF];
 
   SL_ENTER(_("sh_unix_getUser"));
@@ -3021,5 +2247,5 @@
   sh.effective.uid = seuid;
 
-  p = sh_unix_getUIDdir (SH_ERR_ERR, seuid, dir, sizeof(dir));
+  p = sh_unix_getUIDdir (SH_ERR_ERR, seuid);
 
   if (p == NULL)
@@ -3040,5 +2266,5 @@
   sh.real.uid = sruid;
 
-  p = sh_unix_getUIDname (SH_ERR_ERR, sruid, user, sizeof(user));
+  p = sh_unix_getUIDname (SH_ERR_ERR, sruid);
   if (p == NULL)
     SL_RETURN((-1), _("sh_unix_getUser"));
@@ -3054,5 +2280,5 @@
     }
 
-  p = sh_unix_getUIDdir (SH_ERR_ERR, sruid, dir, sizeof(dir));
+  p = sh_unix_getUIDdir (SH_ERR_ERR, sruid);
 
   if (p == NULL)
@@ -3074,5 +2300,5 @@
 }
 
-/* return >0 on success, -1 on EOF */
+
 int sh_unix_getline (SL_TICKET fd, char * line, int sizeofline)
 {
@@ -3084,9 +2310,7 @@
 
   if (sizeofline < 2) {
-    line[0] = '\0';
+    line[n] = '\0';
     SL_RETURN((0), _("sh_unix_getline"));
   }
-
-  --sizeofline;
 
   while (n < sizeofline) {
@@ -3136,7 +2360,7 @@
  **************************************************************/
 
-#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
-
-#if defined(__linux__)
+#if defined(__linux__) 
+
+#if defined(HAVE_LINUX_EXT2_FS_H) || defined(HAVE_EXT2FS_EXT2_FS_H)
 
 /* --- Determine ext2fs file attributes. ---
@@ -3145,9 +2369,6 @@
 #if defined(HAVE_EXT2FS_EXT2_FS_H)
 #include <ext2fs/ext2_fs.h>
-#elif defined(HAVE_LINUX_EXT2_FS_H)
+#else
 #include <linux/ext2_fs.h>
-#endif
-
-/* __linux__ includes */
 #endif
 
@@ -3156,5 +2377,5 @@
 			  unsigned long * flags, 
 			  char * c_attr,
-			  int fd, struct stat * buf)
+			  int fd)
 {
 
@@ -3172,4 +2393,6 @@
 
 #ifdef HAVE_STAT_FLAGS
+  struct stat buf;
+  int    i;
 
   SL_ENTER(_("sh_unix_getinfo_attr"));
@@ -3177,76 +2400,30 @@
   *flags = 0;
 
-  /* cast to void to avoid compiler warning about unused parameters */
-  (void) fd;
-  (void) name;
-
+  i = retry_stat (FIL__, __LINE__, name, &buf);
+
+  if (i == -1)
+    SL_RETURN(-1, _("sh_unix_getinfo_attr"));
+
+#ifdef UF_IMMUTABLE
+  if (buf.st_flags & UF_IMMUTABLE)
+    *flags |= EXT2_IMMUTABLE_FL;
+#endif
+#ifdef UF_APPEND
+  if (buf.st_flags & UF_APPEND)
+    *flags |= EXT2_APPEND_FL;
+#endif
 #ifdef UF_NODUMP
-  if (buf->st_flags & UF_NODUMP) {
-    *flags |= UF_NODUMP;
-    c_attr[0] = 'd';
-  }
-#endif
-#ifdef UF_IMMUTABLE
-  if (buf->st_flags & UF_IMMUTABLE) {
-    *flags |= UF_IMMUTABLE;
-    c_attr[1] = 'i';
-  }
-#endif
-#ifdef UF_APPEND
-  if (buf->st_flags & UF_APPEND) {
-    *flags |= UF_APPEND;
-    c_attr[2] = 'a';
-  }
-#endif
-#ifdef UF_NOUNLINK
-  if (buf->st_flags & UF_NOUNLINK) {
-    *flags |= UF_NOUNLINK;
-    c_attr[3] = 'u';
-  }
-#endif
-#ifdef UF_OPAQUE
-  if (buf->st_flags & UF_OPAQUE) {
-    *flags |= UF_OPAQUE;
-    c_attr[4] = 'o';
-  }
-#endif
-#ifdef SF_ARCHIVED
-  if (buf->st_flags & SF_ARCHIVED) {
-    *flags |= SF_ARCHIVED;
-    c_attr[5] = 'R';
-  }
-    
-#endif
-#ifdef SF_IMMUTABLE
-  if (buf->st_flags & SF_IMMUTABLE) {
-    *flags |= SF_IMMUTABLE;
-    c_attr[6] = 'I';
-  }
-#endif
-#ifdef SF_APPEND
-  if (buf->st_flags & SF_APPEND) {
-    *flags |= SF_APPEND;
-    c_attr[7] = 'A';
-  }
-#endif
-#ifdef SF_NOUNLINK
-  if (buf->st_flags & SF_NOUNLINK) {
-    *flags |= SF_NOUNLINK;
-    c_attr[8] = 'U';
-  }
-#endif
-
-  /* ! HAVE_STAT_FLAGS */
+  if (buf.st_flags & UF_NODUMP)
+    *flags |= EXT2_NODUMP_FL;
+#endif
+
 #else
 
 #ifdef HAVE_EXT2_IOCTLS
   int /* fd, */ r, f;
-  unsigned long keep_flags = 0;
   
   SL_ENTER(_("sh_unix_getinfo_attr"));
 
   *flags = 0;
-  
-  (void) buf;
 
   /* open() -> aud_open() R.Wichmann 
@@ -3258,16 +2435,13 @@
 
   
-  r = ioctl (fd, FS_IOC_GETFLAGS, &f);
-  /* sl_close_fd (FIL__, __LINE__, fd); */
+  r = ioctl (fd, EXT2_IOC_GETFLAGS, &f);
+  /* close (fd); */
 
   if (r == -1)
     SL_RETURN(-1, _("sh_unix_getinfo_attr"));
 
-  if (f == 0)
-    SL_RETURN(0, _("sh_unix_getinfo_attr"));
-
   *flags = f;
 
-/* ! HAVE_FS_IOCTLS */
+/* ! HAVE_EXT2_IOCTLS */
 #else 
 
@@ -3276,6 +2450,7 @@
   *flags = 0;                                     /* modified by R.Wichmann */
 
-/* ! HAVE_FS_IOCTLS */
+/* ! HAVE_EXT2_IOCTLS */
 #endif 
+#endif
 /*
  * END
@@ -3284,57 +2459,57 @@
  */
 
-  if (*flags == 0)
-    goto theend;
-
-#ifdef FS_SECRM_FL
-  if ( (*flags & FS_SECRM_FL) != 0  )  { c_attr[0] = 's'; keep_flags |= FS_SECRM_FL; }
-#endif
-#ifdef FS_UNRM_FL 
-  if ( (*flags & FS_UNRM_FL) != 0   )  { c_attr[1] = 'u'; keep_flags |= FS_UNRM_FL; }
-#endif
-#ifdef FS_SYNC_FL
-  if ( (*flags & FS_SYNC_FL) != 0    ) { c_attr[2] = 'S'; keep_flags |= FS_SYNC_FL; }
-#endif
-#ifdef FS_IMMUTABLE_FL
-  if ( (*flags & FS_IMMUTABLE_FL) != 0) { c_attr[3] = 'i'; keep_flags |= FS_IMMUTABLE_FL; }
-#endif
-#ifdef FS_APPEND_FL
-  if ( (*flags & FS_APPEND_FL) != 0  )  { c_attr[4] = 'a'; keep_flags |= FS_APPEND_FL; }
-#endif
-#ifdef FS_NODUMP_FL
-  if ( (*flags & FS_NODUMP_FL) != 0  )  { c_attr[5] = 'd'; keep_flags |= FS_NODUMP_FL; }
-#endif
-#ifdef FS_NOATIME_FL
-  if ( (*flags & FS_NOATIME_FL) != 0)   { c_attr[6] = 'A'; keep_flags |= FS_NOATIME_FL; }
-#endif
-#ifdef FS_COMPR_FL
-  if ( (*flags & FS_COMPR_FL) != 0   )  { c_attr[7] = 'c'; keep_flags |= FS_COMPR_FL; }
-#endif
-
-#ifdef FS_TOPDIR_FL
-  if ( (*flags & FS_TOPDIR_FL) != 0  )  { c_attr[8] = 'T'; keep_flags |= FS_TOPDIR_FL; }
-#endif
-#ifdef FS_DIRSYNC_FL
-  if ( (*flags & FS_DIRSYNC_FL) != 0 )  { c_attr[9] = 'D'; keep_flags |= FS_DIRSYNC_FL; }
-#endif
-#ifdef FS_NOTAIL_FL
-  if ( (*flags & FS_NOTAIL_FL) != 0  )  { c_attr[10] = 't'; keep_flags |= FS_NOTAIL_FL; }
-#endif
-#ifdef FS_JOURNAL_DATA_FL
-  if ( (*flags & FS_JOURNAL_DATA_FL) != 0)  { c_attr[11] = 'j'; keep_flags |= FS_JOURNAL_DATA_FL; }
-#endif
-
-  *flags = keep_flags;
-  
- theend:
-  /* !HAVE_STAT_FLAGS */
-#endif
-
-  c_attr[ATTRBUF_USED] = '\0';
+#ifdef EXT2_SECRM_FL
+  if ( (*flags & EXT2_SECRM_FL) != 0  )   c_attr[0] = 's';
+#endif
+#ifdef EXT2_UNRM_FL 
+  if ( (*flags & EXT2_UNRM_FL) != 0   )   c_attr[1] = 'u';
+#endif
+#ifdef EXT2_SYNC_FL
+  if ( (*flags & EXT2_SYNC_FL) != 0    )  c_attr[2] = 'S';
+#endif
+#ifdef EXT2_IMMUTABLE_FL
+  if ( (*flags & EXT2_IMMUTABLE_FL) != 0) c_attr[3] = 'i';
+#endif
+#ifdef EXT2_APPEND_FL
+  if ( (*flags & EXT2_APPEND_FL) != 0  )  c_attr[4] = 'a';
+#endif
+#ifdef EXT2_NODUMP_FL
+  if ( (*flags & EXT2_NODUMP_FL) != 0  )  c_attr[5] = 'd';
+#endif
+#ifdef EXT2_NOATIME_FL
+  if ( (*flags & EXT2_NOATIME_FL) != 0)   c_attr[6] = 'A';
+#endif
+#ifdef EXT2_COMPR_FL
+  if ( (*flags & EXT2_COMPR_FL) != 0   )  c_attr[7] = 'c';
+#endif
+#ifdef EXT2_COMPRBLK_FL
+  if ( (*flags & EXT2_COMPRBLK_FL) != 0)  c_attr[8] = 'B';
+#endif
+#ifdef EXT2_DIRTY_FL
+  if ( (*flags & EXT2_DIRTY_FL) != 0   )  c_attr[9] = 'D';
+#endif
+#ifdef EXT2_NOCOMPR_FL
+  if ( (*flags & EXT2_NOCOMPR_FL) != 0 )  c_attr[10] = 'D';
+#endif
+#ifdef EXT2_ECOMPR_FL
+  if ( (*flags & EXT2_ECOMPR_FL) != 0  )  c_attr[11] = 'D';
+#endif
+
+  c_attr[12] = '\0';
 
   SL_RETURN(0, _("sh_unix_getinfo_attr"));
 }
-
-/* defined(__linux__) || defined(HAVE_STAT_FLAGS) */
+#else
+static 
+int sh_unix_getinfo_attr (char * name, 
+			  unsigned long * flags, 
+			  char * c_attr,
+			  int fd)
+{
+  return 0;
+}
+#endif
+
+/* __LINUX__ */
 #endif
 
@@ -3376,12 +2551,4 @@
     c_mode[0] = 's';
   }
-  else if ( S_ISDOOR(buf->st_mode) ) {
-    (*type)   = SH_FILE_DOOR;
-    c_mode[0] = 'D';
-  }
-  else if ( S_ISPORT(buf->st_mode) ) {
-    (*type)   = SH_FILE_PORT;
-    c_mode[0] = 'P';
-  }
   else                              {
     (*type)   = SH_FILE_UNKNOWN;
@@ -3394,5 +2561,5 @@
 int sh_unix_get_ftype(char * fullpath)
 {
-  char        c_mode[CMODE_SIZE];
+  char        c_mode[16];
   struct stat buf;
   ShFileType  type;
@@ -3483,4 +2650,5 @@
 	      someval *= 1000;  /* milliseconds in a second */
 	      sometime = (unsigned long) someval;
+	      /* fprintf(stderr, "FIXME PAUSE %ld\n", sometime); */
 	      retry_msleep(0, sometime);
 	    }
@@ -3488,4 +2656,5 @@
 	    {
 	      sometime = (unsigned long) someval;
+	      /* fprintf(stderr, "FIXME PAUSE %ld sec\n", sometime); */
 	      retry_msleep (sometime, 0);
 	    }
@@ -3495,5 +2664,5 @@
 }
 
-int sh_unix_set_io_limit (const char * c)
+int sh_unix_set_io_limit (char * c)
 {
   long val;
@@ -3518,308 +2687,5 @@
 #include "sh_ignore.h"
 
-int sh_unix_checksum_size (char * filename, off_t size, int is_max_size, 
-			   char * fileHash, int alert_timeout, SL_TICKET fd, unsigned long mask)
-{
-  file_type * tmpFile;
-  int status;
-
-  SL_ENTER(_("sh_unix_checksum_size"));
-
-  tmpFile = SH_ALLOC(sizeof(file_type));
-  tmpFile->link_path = NULL;
-
-  if (sh.flag.checkSum != SH_CHECK_INIT)
-    {
-      /* lookup file in database */
-      if (is_max_size == S_TRUE) {
-	status = sh_hash_get_it (filename, tmpFile, NULL);
-	if ((status != 0) || (tmpFile->size > size)) {
-	  goto out;
-	}
-      } else {
-	tmpFile->size = size;
-      }
-    }
-  else
-    {
-      tmpFile->size = size;
-    }
-
-  /* if last <= current get checksum */
-  if (tmpFile->size <= size)
-    {
-      char hashbuf[KEYBUF_SIZE];
-      UINT64 local_length = (UINT64) (tmpFile->size < 0 ? 0 : tmpFile->size);
-      if (sh.flag.opts == S_TRUE) sh_tiger_set_hashtype_mask(mask);
-      sl_strlcpy(fileHash,
-		 sh_tiger_generic_hash (filename, fd, &(local_length), 
-					alert_timeout, hashbuf, sizeof(hashbuf)),
-		 KEY_LEN+1);
-      
-       /* return */
-      if (tmpFile->link_path)   SH_FREE(tmpFile->link_path);
-      SH_FREE(tmpFile);
-      SL_RETURN( 0, _("sh_unix_checksum_size"));
-    }
-
- out:
-  if (tmpFile->link_path)   SH_FREE(tmpFile->link_path);
-  SH_FREE(tmpFile);
-  sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
-  SL_RETURN( -1, _("sh_unix_checksum_size"));
-}
-
-/********************************************************
- * Search rotated logfile
- */
-extern char * sh_rotated_log_search(const char * path, struct stat * buf);
-
-int sh_check_rotated_log (const char * path,  
-			  UINT64 old_size, UINT64 old_inode, const char * old_hash, unsigned long mask)
-{
-  struct stat obuf;
-  UINT64 length_nolim = TIGER_NOLIM;
-  int retval = S_FALSE;
-
-  if (old_size != length_nolim)
-    {
-      char hashbuf[KEYBUF_SIZE];
-      char * rotated_file;
-
-      obuf.st_ino = old_inode;
-      rotated_file = sh_rotated_log_search(path, &obuf);
-
-      if (rotated_file && (0 != strcmp(path, rotated_file)))
-	{
-	  SL_TICKET fd = sl_open_fastread (FIL__, __LINE__, rotated_file, SL_YESPRIV);
-	  if (!SL_ISERROR(fd))
-	    {
-	      sh_unix_checksum_size (rotated_file, old_size, S_FALSE, 
-				     hashbuf, 120 /* alert_timeout */, fd, mask);
-	      
-	      sl_close(fd);
-	      
-	      if (strncmp (old_hash, hashbuf, KEY_LEN) == 0) {
-		retval = S_TRUE;
-	      }
-	    }
-	}
-      if (rotated_file) {
-	SH_FREE(rotated_file);
-      }
-    }
-  return retval;
-}
-
-
-int sh_unix_check_selinux = S_FALSE;
-int sh_unix_check_acl     = S_FALSE;
-int sh_unix_check_attributes  = S_TRUE;
-
-#ifdef USE_ACL
-
-#include <sys/acl.h>
-static char * sh_unix_getinfo_acl (char * path, int fd, struct stat * buf)
-{
-  /* system.posix_acl_access, system.posix_acl_default
-   */
-  char *  out  = NULL;
-  char *  collect = NULL;
-  char *  tmp;
-  char *  out_compact;
-  ssize_t len;
-  acl_t   result;
-
-  SL_ENTER(_("sh_unix_getinfo_acl"));
-
-  result = (fd == -1) ? 
-    acl_get_file (path, ACL_TYPE_ACCESS) :
-    acl_get_fd   (fd);
-
-  if (result)
-    {
-      out = acl_to_text (result, &len);
-      if (out && (len > 0)) {
-	out_compact = sh_util_acl_compact (out, len);
-	acl_free(out);
-	if (out_compact) 
-	  {
-	    collect = sh_util_strconcat (_("acl_access:"), out_compact, NULL);
-	    SH_FREE(out_compact);
-	  }
-      }
-      acl_free(result);
-    }
-  
-  
-  if ( S_ISDIR(buf->st_mode) ) 
-    {
-      result = acl_get_file (path, ACL_TYPE_DEFAULT);
-      
-      if (result)
-	{
-	  out = acl_to_text (result, &len);
-	  if (out && (len > 0)) {
-	    out_compact = sh_util_acl_compact (out, len);
-	    acl_free(out);
-	    if (out_compact) {
-	      if (collect) {
-		tmp = sh_util_strconcat (_("acl_default:"), 
-					 out_compact, ":", collect, NULL);
-		SH_FREE(collect);
-	      }
-	      else {
-		tmp = sh_util_strconcat (_("acl_default:"), out_compact, NULL);
-	      }
-	      SH_FREE(out_compact);
-	      collect = tmp;
-	    }
-	  }
-	  acl_free(result);
-	}
-    }
-  
-  SL_RETURN((collect),_("sh_unix_getinfo_acl"));
-}
-#endif
-
-#ifdef USE_XATTR
-
-#include <attr/xattr.h>
-static char * sh_unix_getinfo_xattr_int (char * path, int fd, char * name)
-{
-  char *  out   = NULL;
-  char *  tmp   = NULL;
-  size_t  size  = 256;
-  ssize_t result;
-
-  SL_ENTER(_("sh_unix_getinfo_xattr_int"));
-
-  out = SH_ALLOC(size);
-
-  result = (fd == -1) ? 
-    lgetxattr (path, name, out, size-1) :
-    fgetxattr (fd,   name, out, size-1);
-
-  if (result == -1 && errno == ERANGE) 
-    {
-      SH_FREE(out);
-      result = (fd == -1) ? 
-	lgetxattr (path, name, NULL, 0) :
-	fgetxattr (fd,   name, NULL, 0);
-      size = result + 1;
-      out  = SH_ALLOC(size);
-      result = (fd == -1) ? 
-	lgetxattr (path, name, out, size-1) :
-	fgetxattr (fd,   name, out, size-1);
-    }
-
-  if ((result > 0) && ((size_t)result < size))
-    {
-      out[size-1] = '\0';
-      tmp = out;
-    }
-  else
-    {
-      SH_FREE(out);
-    }
-
-  SL_RETURN((tmp),_("sh_unix_getinfo_xattr_int"));
-}
-
-
-static char * sh_unix_getinfo_xattr (char * path, int fd, struct stat * buf)
-{
-  /* system.posix_acl_access, system.posix_acl_default, security.selinux 
-   */
-  char *  tmp;
-  char *  out  = NULL;
-  char *  collect = NULL;
-
-  SL_ENTER(_("sh_unix_getinfo_xattr"));
-
-#ifdef USE_ACL
-  /*
-   * we need the acl_get_fd/acl_get_file functions, getxattr will only
-   * yield the raw bytes
-   */
-  if (sh_unix_check_acl == S_TRUE) 
-    {
-      out = sh_unix_getinfo_acl(path, fd, buf);
-      
-      if (out)
-	{
-	  collect = out;
-	}
-  }
-#else
-  (void) buf;
-#endif
-
-  if (sh_unix_check_selinux == S_TRUE)
-    {
-      out = sh_unix_getinfo_xattr_int(path, fd, _("security.selinux"));
-
-      if (out)
-	{
-	  if (collect) {
-	    tmp = sh_util_strconcat(_("selinux:"), out, ":", collect, NULL);
-	    SH_FREE(collect);
-	  }
-	  else {
-	    tmp = sh_util_strconcat(_("selinux:"), out, NULL);
-	  }
-	  SH_FREE(out);
-	  collect = tmp;
-	}
-    }
-
-  SL_RETURN((collect),_("sh_unix_getinfo_xattr"));
-}
-#endif
-
-#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
-int sh_unix_setcheckattributes (const char * c)
-{
-  int i;
-  SL_ENTER(_("sh_unix_setcheckattributes"));
-  i = sh_util_flagval(c, &(sh_unix_check_attributes));
-
-  SL_RETURN(i, _("sh_unix_setcheckattributes"));
-}
-#endif
-
-#ifdef USE_XATTR
-int sh_unix_setcheckselinux (const char * c)
-{
-  int i;
-  SL_ENTER(_("sh_unix_setcheckselinux"));
-  i = sh_util_flagval(c, &(sh_unix_check_selinux));
-
-  SL_RETURN(i, _("sh_unix_setcheckselinux"));
-}
-#endif
-
-#ifdef USE_ACL
-int sh_unix_setcheckacl (const char * c)
-{
-  int i;
-  SL_ENTER(_("sh_unix_setcheckacl"));
-  i = sh_util_flagval(c, &(sh_unix_check_acl));
-
-  SL_RETURN(i, _("sh_unix_setcheckacl"));
-}
-#endif
-
-#ifdef HAVE_LIBZ
-#include <zlib.h>
-#endif    
-
-
-static void * sh_dummy_filename;
-void * sh_dummy_tmp;
-void * sh_dummy_tmp2;
-
-int sh_unix_getinfo (int level, const char * filename, file_type * theFile, 
+int sh_unix_getinfo (int level, char * filename, file_type * theFile, 
 		     char * fileHash, int policy)
 {
@@ -3829,27 +2695,24 @@
   struct stat   lbuf;
   struct stat   fbuf;
-  volatile int  stat_return;
-  volatile int  stat_errno = 0;
-
+  int           stat_return;
+  register int  i;
   ShFileType    type;
   unsigned int  mode;
+  char        * name;
   char        * tmp;
   char        * tmp2;
-
+  /* char        * p; */
   char        * linknamebuf;
-  volatile int  linksize;
+  int           linksize;
 
   extern int get_the_fd (SL_TICKET ticket);
 
-  volatile SL_TICKET     rval_open;
-  volatile int           err_open = 0;
-
-  volatile int           fd;
-  volatile int           fstat_return;
-  volatile int           fstat_errno = 0;
-  volatile int           try         = 0;
-
-  sh_string   * content = NULL;
-      
+  SL_TICKET     rval_open;
+  int           fd;
+#if defined(__linux__)
+  int           fd_2;
+#endif
+  int           fstat_return;
+
   time_t        tend;
   time_t        tstart;
@@ -3858,83 +2721,44 @@
   char * path = NULL;
 
-  volatile int alert_timeout   = 120;
+  int alert_timeout   = 120;
 
   path = theFile->fullpath;
 
+#if 0
+  {
+    char pwd[256];
+    printf("%d %s %s %s (%s)\n", flagrel, theFile->fullpath, filename, path,
+	   getcwd (pwd, 256)); 
+  }
+#endif
+
   SL_ENTER(_("sh_unix_getinfo"));
 
-  if (!MODI_INITIALIZED(theFile->check_flags))
-    {
-      tmp2 = sh_util_safe_name (theFile->fullpath);
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_SUBGPATH,
-		       _("Uninitialized check mask"), _("sh_unix_getinfo"),
-		       tmp2);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      SH_FREE(tmp2);
-      SL_RETURN((-1),_("sh_unix_getinfo"));
-    }
-
-  /* Take the address to keep gcc from putting it into a register. 
-   * Avoids the 'clobbered by longjmp' warning. 
-   */
-  sh_dummy_filename = (void *) &filename;
-  sh_dummy_tmp      = (void *) &tmp;
-  sh_dummy_tmp2     = (void *) &tmp2;
-
   /* --- Stat the file, and get checksum. ---
    */
   tstart = time(NULL);
-
   stat_return = retry_lstat (FIL__, __LINE__, 
 			     path /* theFile->fullpath */, &buf);
 
-  if (stat_return)
-    stat_errno = errno;
-
-  theFile->link_path = NULL;
-
- try_again:
-
-  fd           = -1;
-  fstat_return = -1;
-  rval_open    = -1;
-
   if (stat_return == 0 && S_ISREG(buf.st_mode)) 
     {
-      rval_open = sl_open_fastread (FIL__, __LINE__, 
-				    path /* theFile->fullpath */, SL_YESPRIV);
-      if (SL_ISERROR(rval_open))
-	{
-	  char * stale = sl_check_stale();
-	  
-	  if (stale)
-	    {
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, err_open, MSG_E_SUBGEN,
-			      stale, _("sh_unix_getinfo_open"));
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	    }
-
-	  if (errno == EBADF && try == 0) /* obsolete, but we keep this, just in case */
-	    {
-	      /* cppcheck-suppress syntaxError */
-	      ++try;
-	      goto try_again;
-	    }
-	  err_open = errno;
-	}
+      rval_open = sl_open_fastread (path /* theFile->fullpath */, SL_YESPRIV);
 
       alert_timeout = 120; /* this is per 8K block now ! */
 
-      if (path[1] == 'p' && path[5] == '/' && path[2] == 'r' &&
-	  path[3] == 'o' && path[4] == 'c' && path[0] == '/')
+      if (path[0] == '/' && path[1] == 'p' && path[2] == 'r' &&
+	  path[3] == 'o' && path[4] == 'c' && path[5] == '/')
 	{
 	  /* seven is magic */
 	  alert_timeout = 7;
 	}
-
-      fd = get_the_fd(rval_open);
-    }
+    }
+  else
+    rval_open = -1;
+
+  if (SL_ISERROR(rval_open))
+    fd = -1;
+  else
+    fd = get_the_fd(rval_open);
 
   tend = time(NULL);
@@ -3946,45 +2770,21 @@
     {
       tmp2 = sh_util_safe_name (theFile->fullpath);
-      SH_MUTEX_LOCK(mutex_thread_nolog);
       sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_TOOLATE,
 		       (long)(tend - tstart), tmp2);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
       SH_FREE(tmp2);
     }
-
+      
   if (fd >= 0) 
     {
       fstat_return = retry_fstat (FIL__, __LINE__, fd, &fbuf);
-
-      if (fstat_return)
-	{
-	  char * stale;
-
-	  fstat_errno = errno;
-
-	  stale = sl_check_stale();
-
-	  if (stale)
-	    {
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, fstat_errno, 
-			      MSG_E_SUBGEN,
-			      stale, _("sh_unix_getinfo_fstat"));
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	    }
-
-	  if (try == 0) /* obsolete, but we keep this, just in case */
-	    {
-	      ++try;
-	      sl_close(rval_open);
-	      goto try_again;
-	    }
-	}
     }
   else
     {
-      fd = -1;
-    }
-      
+      fstat_return = -1;
+      fd           = -1;
+    }
+#if defined(__linux__)
+  fd_2 = fd;
+#endif
 
   /* ---  case 1: lstat failed  --- 
@@ -3997,24 +2797,11 @@
       if (sh.flag.checkSum == SH_CHECK_INIT || 
 	  (sh_hash_have_it (theFile->fullpath) >= 0 && 
-	   (!SH_FFLAG_REPORTED_SET(theFile->file_reported))))
+	   theFile->reported == S_FALSE))
 	{
 	  if (S_FALSE == sh_ignore_chk_del(theFile->fullpath)) {
-	    int flags = sh_hash_getflags (theFile->fullpath);
-
-	    if ((flags >= 0) && (flags & SH_FFLAG_ENOENT) == 0) {
-	      char errbuf[SH_ERRBUF_SIZE];
-	      uid_t euid;
-	      (void) sl_get_euid(&euid);
-	      tmp2 = sh_util_safe_name (theFile->fullpath);
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle (level, FIL__, __LINE__, stat_return, MSG_FI_STAT,
-			       _("lstat"),
-			       sh_error_message (stat_errno, errbuf, sizeof(errbuf)),
-			       (long) euid,
-			       tmp2);
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	      SH_FREE(tmp2);
-	      sh_hash_set_flag (theFile->fullpath, SH_FFLAG_ENOENT);
-	    }
+	    tmp2 = sh_util_safe_name (theFile->fullpath);
+	    sh_error_handle (level, FIL__, __LINE__, stat_return, MSG_FI_LSTAT,
+			     sh_error_message (stat_return), tmp2);
+	    SH_FREE(tmp2);
 	  }
 	}
@@ -4022,10 +2809,11 @@
     }
 
-  /* ---  case 2: not a regular file  --- 
+  /* ---  case 2: not a regular file, or [IgnoreAll]  --- 
    */
   else if (! S_ISREG(buf.st_mode))
     {
       if (fileHash != NULL)
-	sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
+	strcpy(fileHash,                              /* known to fit */
+	       _("000000000000000000000000000000000000000000000000"));
     }
   
@@ -4041,50 +2829,24 @@
       if (fileHash != NULL)
 	{
-	  if ((theFile->check_flags & MODI_CHK) == 0 ||
-	      sh_restrict_this(theFile->fullpath, (UINT64) fbuf.st_size, 
-			       (UINT64) fbuf.st_mode, rval_open))
+	  if ((theFile->check_mask & MODI_CHK) == 0)
 	    {
-	      sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
+	      strcpy(fileHash,                         /* known to fit */
+		     _("000000000000000000000000000000000000000000000000"));
 	    }
-	  else if ((theFile->check_flags & MODI_PREL) != 0 &&
+	  else if (policy == SH_LEVEL_PRELINK && 
 		   S_TRUE == sh_prelink_iself(rval_open, fbuf.st_size, 
-					      alert_timeout, theFile->fullpath))
+					      alert_timeout))
 	    {
 	      if (0 != sh_prelink_run (theFile->fullpath, 
-				       fileHash, alert_timeout, theFile->check_flags))
-		sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
+				       fileHash, alert_timeout))
+		strcpy(fileHash,                       /* known to fit */
+		       _("000000000000000000000000000000000000000000000000"));
 	    }
 	  else
 	    {
-	      char hashbuf[KEYBUF_SIZE];
-	      UINT64 length_current = TIGER_NOLIM;
-
-	      if (MODI_TXT_ENABLED(theFile->check_flags) && fbuf.st_size < (10 * SH_TXT_MAX))
-		{
-		  sl_init_content (rval_open, fbuf.st_size);
-		}
-
-	      if (sh.flag.opts == S_TRUE) sh_tiger_set_hashtype_mask(theFile->check_flags);
-	      sl_strlcpy(fileHash,
-			 sh_tiger_generic_hash (theFile->fullpath, 
-						rval_open, &length_current, 
-						alert_timeout, 
-						hashbuf, sizeof(hashbuf)),
-			 KEY_LEN+1);
-
-	      content = sl_get_content(rval_open);
-	      content = sh_string_copy(content);
-
-	      if ((theFile->check_flags & MODI_SGROW) != 0)
-		{
-		  /* Update size so it matches the one for which the checksum
-		     has been computed */
-		  fbuf.st_size = length_current;
-		  buf.st_size  = fbuf.st_size;
-		  sl_rewind(rval_open);
-		  sh_unix_checksum_size (theFile->fullpath, length_current, S_TRUE,
-					 &fileHash[KEY_LEN + 1], 
-					 alert_timeout, rval_open, theFile->check_flags);
-		}
+	      tiger_fd = rval_open;
+	      strcpy(fileHash,                         /* known to fit */
+		     sh_tiger_generic_hash (theFile->fullpath, TIGER_FD, 0, 
+					    alert_timeout));
 	    }
 	}
@@ -4099,50 +2861,25 @@
       if (fileHash != NULL)
 	{
-	  if ((theFile->check_flags & MODI_CHK) == 0 ||
-	      sh_restrict_this(theFile->fullpath, (UINT64) fbuf.st_size, 
-			       (UINT64) fbuf.st_mode, rval_open))
+	  if ((theFile->check_mask & MODI_CHK) == 0)
 	    {
-	      sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
+	      strcpy(fileHash,                         /* known to fit */
+		     _("000000000000000000000000000000000000000000000000"));
 	    }
 	  else if (policy == SH_LEVEL_PRELINK &&
 		   S_TRUE == sh_prelink_iself(rval_open, fbuf.st_size, 
-					      alert_timeout, theFile->fullpath))
+					      alert_timeout))
 	    {
 	      if (0 != sh_prelink_run (theFile->fullpath, 
-				       fileHash, alert_timeout, theFile->check_flags))
-		sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
+				       fileHash, alert_timeout))
+		strcpy(fileHash,                       /* known to fit */
+		       _("000000000000000000000000000000000000000000000000"));
 	    }
 	  else
 	    {
-	      char hashbuf[KEYBUF_SIZE];
-	      UINT64 length_current = TIGER_NOLIM;
-
-	      if (MODI_TXT_ENABLED(theFile->check_flags) && fbuf.st_size < (10 * SH_TXT_MAX))
-		{
-		  sl_init_content (rval_open, fbuf.st_size);
-		}
-
-	      if (sh.flag.opts == S_TRUE) sh_tiger_set_hashtype_mask(theFile->check_flags);
+	      tiger_fd = rval_open;
 	      sl_strlcpy(fileHash, 
-			 sh_tiger_generic_hash (theFile->fullpath, rval_open, 
-						&length_current,
-						alert_timeout,
-						hashbuf, sizeof(hashbuf)),
+			 sh_tiger_generic_hash (theFile->fullpath, TIGER_FD, 0,
+						alert_timeout),
 			 KEY_LEN + 1);
-
-	      content = sl_get_content(rval_open);
-	      content = sh_string_copy(content);
-
-	      if ((theFile->check_flags & MODI_SGROW) != 0) 
-		{
-		  /* Update size so it matches the one for which the checksum
-		     has been computed */
-		  fbuf.st_size = length_current;
-		  buf.st_size  = fbuf.st_size;
-		  sl_rewind(rval_open);
-		  sh_unix_checksum_size (theFile->fullpath, length_current, S_TRUE,
-					 &fileHash[KEY_LEN + 1], 
-					 alert_timeout, rval_open, theFile->check_flags);
-		}
 	    }
 	}
@@ -4152,49 +2889,16 @@
    */
 
-  else    /* fstat_return != 0 or !S_ISREG(fbuf.st_mode) or open() failed */
-    {
-      uid_t   euid;
-
+  else    /* fstat_return != 0 or !S_ISREG(fbuf->st_mode) */
+    {
+      fstat_return = errno;
       if (fileHash != NULL)
-	sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
-
-      if ((theFile->check_flags & MODI_CHK) != 0)
-	{
-	  tmp2 = sh_util_safe_name (theFile->fullpath);
-
-
-	  if (fd >= 0 && fstat_return != 0)
-	    {
-	      char errbuf[SH_ERRBUF_SIZE];
-	      (void) sl_get_euid(&euid);
-
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle (level, FIL__, __LINE__, stat_return, MSG_FI_STAT,
-			       _("fstat"),
-			       sh_error_message (fstat_errno, errbuf, sizeof(errbuf)),
-			       (long) euid,
-			       tmp2);
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	    }
-	  else if (fd >= 0 && !S_ISREG(fbuf.st_mode))
-	    {
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle (level, FIL__, __LINE__, fstat_errno, 
-			       MSG_E_NOTREG, tmp2);
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	    }
-	  else
-	    {
-	      char errbuf[SH_ERRBUF_SIZE];
-	      char errbuf2[SH_ERRBUF_SIZE];
-	      sl_strlcpy(errbuf, sl_error_string(rval_open), sizeof(errbuf));
-	      sh_error_message(err_open, errbuf2, sizeof(errbuf2));
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle (level, FIL__, __LINE__, err_open, 
-			       MSG_E_READ, errbuf, errbuf2, tmp2);
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	    }
-	  SH_FREE(tmp2);
-	}
+	sl_strlcpy(fileHash, 
+		   _("000000000000000000000000000000000000000000000000"), 
+		   KEY_LEN + 1);
+
+      tmp2 = sh_util_safe_name (theFile->fullpath);
+      sh_error_handle (level, FIL__, __LINE__, fstat_return, MSG_E_READ,
+		       tmp2);
+      SH_FREE(tmp2);
     }	  
 
@@ -4202,43 +2906,25 @@
   /* --- Determine file type. ---
    */
-  memset (theFile->c_mode, '-', CMODE_SIZE-1);
-  theFile->c_mode[CMODE_SIZE-1] = '\0';
-
-  memset (theFile->link_c_mode, '-', CMODE_SIZE-1);
-  theFile->link_c_mode[CMODE_SIZE-1] = '\0';
+  for (i = 0; i < 10; ++i) theFile->c_mode[i] = '-';
+  theFile->c_mode[10] = '\0';
+  for (i = 0; i < 11; ++i) theFile->link_c_mode[i] = '-';
+  theFile->link_c_mode[10] = '\0';
 
   sh_unix_getinfo_type (&buf, &type, theFile->c_mode);
   theFile->type = type;
 
-#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
+#if defined(__linux__)
 
   /* --- Determine file attributes. ---
    */
-  memset (theFile->c_attributes, '-', ATTRBUF_SIZE);
-  theFile->c_attributes[ATTRBUF_USED] = '\0';
+  for (i = 0; i < 12; ++i) theFile->c_attributes[i] = '-';
+  theFile->c_attributes[12] = '\0';
   theFile->attributes      =    0;
 
-#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
   if (theFile->c_mode[0] != 'c' && theFile->c_mode[0] != 'b' &&
-      theFile->c_mode[0] != 'l' ) {
-    if (sh_unix_check_attributes == S_TRUE)
-      sh_unix_getinfo_attr(theFile->fullpath, 
-			   &theFile->attributes, theFile->c_attributes, 
-			   fd, &buf);
-  }
-#endif
-#endif
-
-#if defined(USE_XATTR) && defined(USE_ACL)
-  if (sh_unix_check_selinux == S_TRUE || sh_unix_check_acl == S_TRUE)
-    theFile->attr_string = sh_unix_getinfo_xattr (theFile->fullpath, fd, &buf);
-#elif defined(USE_XATTR)
-  if (sh_unix_check_selinux == S_TRUE)
-    theFile->attr_string = sh_unix_getinfo_xattr (theFile->fullpath, fd, &buf);
-#elif defined(USE_ACL)
-  if (sh_unix_check_acl == S_TRUE)
-    theFile->attr_string = sh_unix_getinfo_acl (theFile->fullpath, fd, &buf);
-#else
-  theFile->attr_string = NULL;
+      theFile->c_mode[0] != 'l' )
+    sh_unix_getinfo_attr(theFile->fullpath, 
+			 &theFile->attributes, theFile->c_attributes, fd_2);
+
 #endif
 
@@ -4281,5 +2967,8 @@
    */
 
-  if (NULL == sh_unix_getGIDname(SH_ERR_ALL, buf.st_gid, theFile->c_group, GROUP_MAX+1)) {
+  if ( (name = sh_unix_getGIDname(SH_ERR_ALL,
+				  buf.st_gid)) != NULL) {
+    sl_strlcpy (theFile->c_group, name, GROUP_MAX+1);
+  } else {
 
     tmp2 = sh_util_safe_name (theFile->fullpath);
@@ -4287,17 +2976,13 @@
     if (policy == SH_LEVEL_ALLIGNORE)
       {
-	SH_MUTEX_LOCK(mutex_thread_nolog);
 	sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, ENOENT, 
 			 MSG_FI_NOGRP,
 			 (long) buf.st_gid, tmp2);
-	SH_MUTEX_UNLOCK(mutex_thread_nolog);
       }
     else
       {
-	SH_MUTEX_LOCK(mutex_thread_nolog);
 	sh_error_handle (ShDFLevel[SH_ERR_T_NAME], FIL__, __LINE__, ENOENT, 
 			 MSG_FI_NOGRP,
 			 (long) buf.st_gid, tmp2);
-	SH_MUTEX_UNLOCK(mutex_thread_nolog);
       }
     SH_FREE(tmp2);
@@ -4306,5 +2991,8 @@
 
   
-  if (NULL == sh_unix_getUIDname(SH_ERR_ALL, buf.st_uid, theFile->c_owner, USER_MAX+1)) {
+  if ( (name = sh_unix_getUIDname(SH_ERR_ALL,
+				  buf.st_uid)) != NULL) {
+    sl_strlcpy (theFile->c_owner, name, USER_MAX+1);
+  } else {
 
     tmp2 = sh_util_safe_name (theFile->fullpath);
@@ -4312,17 +3000,13 @@
     if (policy == SH_LEVEL_ALLIGNORE)
       {
-	SH_MUTEX_LOCK(mutex_thread_nolog);
 	sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, ENOENT, 
 			 MSG_FI_NOUSR,
 			 (long) buf.st_uid, tmp2);
-	SH_MUTEX_UNLOCK(mutex_thread_nolog);
       }
     else
       {
-	SH_MUTEX_LOCK(mutex_thread_nolog);
 	sh_error_handle (ShDFLevel[SH_ERR_T_NAME], FIL__, __LINE__, ENOENT, 
 			 MSG_FI_NOUSR,
 			 (long) buf.st_uid, tmp2);
-	SH_MUTEX_UNLOCK(mutex_thread_nolog);
       }
     SH_FREE(tmp2);
@@ -4332,10 +3016,9 @@
   /* --- Output the file. ---
    */
-  if (flag_err_debug == S_TRUE)
+  if (flag_err_debug == SL_TRUE)
     {
       tmp2 = sh_util_safe_name ((filename == NULL) ? 
 				theFile->fullpath : filename);
-      (void) sh_unix_time(theFile->mtime, timestr, sizeof(timestr));
-      SH_MUTEX_LOCK(mutex_thread_nolog);
+      sl_strlcpy(timestr, sh_unix_time(theFile->mtime), 81);
       sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_LIST,
 		       theFile->c_mode,
@@ -4346,5 +3029,4 @@
 		       timestr,
 		       tmp2);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
       SH_FREE(tmp2);
     }
@@ -4354,7 +3036,7 @@
   if (theFile->c_mode[0] == 'l') 
     {
+
       linknamebuf = SH_ALLOC(PATH_MAX);
 
-      /* flawfinder: ignore */
       linksize    = readlink (theFile->fullpath, linknamebuf, PATH_MAX-1);
 
@@ -4366,165 +3048,92 @@
       if (linksize < 0) 
 	{
-	  char errbuf[SH_ERRBUF_SIZE];
 	  linksize = errno;
 	  tmp2 = sh_util_safe_name (theFile->fullpath);
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
 	  sh_error_handle (level, FIL__, __LINE__, linksize, MSG_FI_RDLNK,
-			   sh_error_message (linksize, errbuf, sizeof(errbuf)), tmp2);
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
+			   sh_error_message (linksize), tmp2);
 	  SH_FREE(tmp2);
 	  SH_FREE(linknamebuf);
-	  theFile->link_path = sh_util_strdup("-");
 	  SL_RETURN((-1),_("sh_unix_getinfo"));
 	}
-
-      if (linknamebuf[0] == '/') 
-	{
-	  theFile->link_path = sh_util_strdup (linknamebuf);
-	} 
-      else 
-	{
-	  tmp = sh_util_dirname(theFile->fullpath);
-	  if (tmp) {
-	    theFile->link_path = SH_ALLOC(PATH_MAX);
-	    sl_strlcpy (theFile->link_path, tmp, PATH_MAX);
-	    SH_FREE(tmp);
-	  } else {
-	    theFile->link_path = SH_ALLOC(PATH_MAX);
-	    theFile->link_path[0] = '\0';
+    
+    if (linknamebuf[0] == '/') 
+      {
+	sl_strlcpy (theFile->linkpath, linknamebuf, PATH_MAX);
+      } 
+    else 
+      {
+	tmp = sh_util_basename(theFile->fullpath);
+	sl_strlcpy (theFile->linkpath, 
+		    tmp, 
+		    PATH_MAX);
+	SH_FREE(tmp);
+	sl_strlcat (theFile->linkpath, 
+		    "/", PATH_MAX);
+	sl_strlcat (theFile->linkpath, 
+		    linknamebuf, 
+		    PATH_MAX);
+      }
+    
+    /* stat the link
+     */
+    stat_return = retry_lstat (FIL__, __LINE__, theFile->linkpath, &lbuf); 
+
+    /* check for error
+     */
+    if (stat_return != 0) 
+      { 
+	stat_return = errno;
+	tmp  = sh_util_safe_name (theFile->fullpath);
+	tmp2 = sh_util_safe_name (theFile->linkpath);
+	if (stat_return != ENOENT)
+	  { 
+	    sh_error_handle (level, FIL__, __LINE__, stat_return, 
+			     MSG_FI_LSTAT,
+			     sh_error_message (stat_return), tmp2);
 	  }
-	  /*
-	   * Only attach '/' if not root directory. Handle "//", which
-	   * according to POSIX is implementation-defined, and may be
-	   * different from "/" (however, three or more '/' will collapse
-	   * to one).
-	   */
-	  tmp = theFile->link_path; while (*tmp == '/') ++tmp;
-	  if (*tmp != '\0')
-	    {
-	      sl_strlcat (theFile->link_path, "/", PATH_MAX);
-	    }
-	  sl_strlcat (theFile->link_path, linknamebuf, PATH_MAX);
-	}
+	else 
+	  {
+	    /* a dangling link -- everybody seems to have plenty of them 
+	     */
+	    sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DLNK,
+			     tmp, tmp2);
+	  }
+	theFile->linkisok = BAD;
+	SH_FREE(tmp);
+	SH_FREE(tmp2);
+	SH_FREE(linknamebuf);
+	/* 
+	 * changed Tue Feb 10 16:16:13 CET 2004:
+	 *  add dangling symlinks into database
+	 * SL_RETURN((-1),_("sh_unix_getinfo")); 
+	 */
+	theFile->linkmode = 0;
+	SL_RETURN((0),_("sh_unix_getinfo")); 
+      }
+
+    theFile->linkisok = GOOD;
       
-      /* stat the link
-       */
-      stat_return = retry_lstat (FIL__, __LINE__, theFile->link_path, &lbuf); 
-      
-      /* check for error
-       */
-      if (stat_return != 0) 
-	{ 
-	  stat_return = errno;
-	  tmp  = sh_util_safe_name (theFile->fullpath);
-	  tmp2 = sh_util_safe_name (theFile->link_path);
-	  if (stat_return != ENOENT)
-	    { 
-	      uid_t euid;
-	      char errbuf[SH_ERRBUF_SIZE];
-
-	      (void) sl_get_euid(&euid);
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle (level, FIL__, __LINE__, stat_return, 
-			       MSG_FI_STAT,
-			       _("lstat (link target)"),
-			       sh_error_message (stat_return,errbuf, sizeof(errbuf)), 
-			       (long) euid,
-			       tmp2);
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	    }
-	  else 
-	    {
-	      /* a dangling link -- everybody seems to have plenty of them 
-	       */
-	      SH_MUTEX_LOCK(mutex_thread_nolog);
-	      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DLNK,
-			       tmp, tmp2);
-	      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	    }
-	  theFile->linkisok = BAD;
-	  SH_FREE(tmp);
-	  SH_FREE(tmp2);
-	  SH_FREE(linknamebuf);
-	  /* 
-	   * changed Tue Feb 10 16:16:13 CET 2004:
-	   *  add dangling symlinks into database
-	   * SL_RETURN((-1),_("sh_unix_getinfo")); 
-	   */
-	  theFile->linkmode = 0;
-	  SL_RETURN((0),_("sh_unix_getinfo")); 
-	}
-      
-      theFile->linkisok = GOOD;
-      
-      
-      /* --- Determine file type. ---
-       */
-      sh_unix_getinfo_type (&lbuf, &type, theFile->link_c_mode);
-      theFile->type = type;
-      
-      /* --- Determine permissions. ---
-       */
-      sh_unix_getinfo_mode (&lbuf, &mode, theFile->link_c_mode);
-      theFile->linkmode = lbuf.st_mode;
-      
-      /* --- Output the link. ---
-       */
-      if (theFile->linkisok == GOOD) 
-	{
-	  tmp2 = sh_util_safe_name (linknamebuf);      
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
-	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_LLNK,
-			   theFile->link_c_mode, tmp2);
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-	  SH_FREE(tmp2);
-	}
-      SH_FREE(linknamebuf);
-    }
-  else /* not a link, theFile->c_mode[0] != 'l' */
-    {
-      if (content)
-	{
-#ifdef HAVE_LIBZ
-	  unsigned long   clen;
-	  unsigned char * compressed;
-#ifdef HAVE_COMPRESSBOUND
-	  clen       = compressBound(sh_string_len(content));
-#else
-	  if (sh_string_len(content) > 10*SH_TXT_MAX)
-	    clen = SH_TXT_MAX;
-	  else
-	    clen = 13 + (int)(1.0001*sh_string_len(content));
-#endif
-	  compressed = SH_ALLOC(clen);
-	  if (Z_OK == compress(compressed, &clen, 
-			       (unsigned char *) sh_string_str(content), 
-			       sh_string_len(content)))
-	      {
-		if (clen < SH_TXT_MAX)
-		  {
-		    sh_util_base64_enc_alloc (&(theFile->link_path), 
-					      (char *) compressed, clen);
-		  }
-		else
-		  {
-		    char tmsg[128];
-		    char * tpath = sh_util_safe_name (theFile->fullpath);
-		    sl_snprintf(tmsg, sizeof(tmsg), 
-				_("compressed file too large (%lu bytes)"),
-				clen);
-		    SH_MUTEX_LOCK(mutex_thread_nolog);
-		    sh_error_handle (SH_ERR_WARN, FIL__, __LINE__, -1, 
-				     MSG_E_SUBGPATH, tmsg, 
-				     _("sh_unix_getinfo"), tpath);
-		    SH_MUTEX_UNLOCK(mutex_thread_nolog);
-		    SH_FREE(tpath);
-		  }
-	      }
-	  SH_FREE(compressed);
-#endif
-	  sh_string_destroy(&content);
-	}
-    } 
+
+    /* --- Determine file type. ---
+     */
+    sh_unix_getinfo_type (&lbuf, &type, theFile->link_c_mode);
+    theFile->type = type;
+  
+    /* --- Determine permissions. ---
+     */
+    sh_unix_getinfo_mode (&lbuf, &mode, theFile->link_c_mode);
+    theFile->linkmode = lbuf.st_mode;
+
+    /* --- Output the link. ---
+     */
+    if (theFile->linkisok == GOOD) 
+      {
+	tmp2 = sh_util_safe_name (linknamebuf);      
+	sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_LLNK,
+			 theFile->link_c_mode, tmp2);
+	SH_FREE(tmp2);
+      }
+    SH_FREE(linknamebuf);
+  }  
   SL_RETURN((0),_("sh_unix_getinfo"));
 }
@@ -4533,128 +3142,6 @@
 #endif
 
-int sh_unix_unlock(char * lockfile, char * flag)
-{
-  int         error = 0;
-  
-  SL_ENTER(_("sh_unix_unlock"));
-
-  if (sh.flag.isdaemon == S_FALSE && flag == NULL)
-    SL_RETURN((0),_("sh_unix_unlock"));
-
-  /* --- Logfile is not locked to us. ---
-   */
-  if (sh.flag.islocked == BAD && flag != NULL) 
-    SL_RETURN((-1),_("sh_unix_unlock"));
-
-  /* --- Check whether the directory is secure. ---
-   */
-  if (0 != tf_trust_check (lockfile, SL_YESPRIV))
-    SL_RETURN((-1),_("sh_unix_unlock"));
-
-  /* --- Delete the lock file. --- 
-   */
-  error = retry_aud_unlink (FIL__, __LINE__, lockfile);
-  
-  if (error == 0)
-    {
-      if (flag != NULL)
-	sh.flag.islocked = BAD; /* not locked anymore */
-    }
-  else if (flag != NULL)
-    {
-      char errbuf[SH_ERRBUF_SIZE];
-      error = errno;
-      sh_error_handle ((-1), FIL__, __LINE__, error, MSG_E_UNLNK,
-		       sh_error_message(error, errbuf, sizeof(errbuf)), 
-		       lockfile);
-      SL_RETURN((-1),_("sh_unix_unlock"));
-    }
-  SL_RETURN((0),_("sh_unix_unlock"));
-}
-
-int sh_unix_check_piddir (char * pidpath)
-{
-  static        struct stat   buf;
-  int           status = 0;
-  char        * pid_dir;
-  
-  SL_ENTER(_("sh_unix_check_piddir"));
-
-  pid_dir = sh_util_dirname (pidpath);
-
-  status = retry_lstat (FIL__, __LINE__, pid_dir, &buf);
-
-  if (status < 0 && errno == ENOENT)
-    {
-      status = mkdir (pid_dir, 0777);
-      if (status < 0)
-	{
-	  sh_error_handle ((-1), FIL__, __LINE__, status,
-			   MSG_E_SUBGEN, 
-			   _("Cannot create PID directory"),
-			   _("sh_unix_check_piddir"));
-	  SH_FREE(pid_dir);
-	  SL_RETURN((-1),_("sh_unix_check_piddir"));
-	}
-    }
-  else if (!S_ISDIR(buf.st_mode))
-    {
-      sh_error_handle ((-1), FIL__, __LINE__, status,
-		       MSG_E_SUBGEN, 
-		       _("Path of PID directory refers to a non-directory object"),
-		       _("sh_unix_check_piddir"));
-      SH_FREE(pid_dir);
-      SL_RETURN((-1),_("sh_unix_check_piddir"));
-    }
-  SH_FREE(pid_dir);
-  SL_RETURN((0),_("sh_unix_check_piddir"));
-}
-
-int sh_unix_lock (char * lockfile, char * flag)
-{
-  int filed;
-  int errnum;
-  char myPid[64];
-  SL_TICKET  fd;
-  extern int get_the_fd (SL_TICKET ticket);
-
-  SL_ENTER(_("sh_unix_lock"));
-
-  sl_snprintf (myPid, sizeof(myPid), "%ld\n", (long) sh.pid);             /* known to fit  */
-
-  if (flag == NULL) /* PID file, check for directory */
-    {
-      if (0 != sh_unix_check_piddir (lockfile))
-	{
-	  SL_RETURN((-1),_("sh_unix_lock"));
-	}
-    }
-
-  fd = sl_open_safe_rdwr (FIL__, __LINE__, 
-			  lockfile, SL_YESPRIV);      /* fails if file exists */
-
-  if (!SL_ISERROR(fd))
-    {
-      errnum = sl_write (fd, myPid, sl_strlen(myPid));
-      filed = get_the_fd(fd);
-      fchmod (filed, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
-      sl_close (fd);
-
-      if (!SL_ISERROR(errnum))
-	{
-	  if (flag != NULL)
-	    sh.flag.islocked = GOOD;
-	  SL_RETURN((0),_("sh_unix_lock"));
-	}
-    }
-
-  TPT((0, FIL__, __LINE__, _("msg=<open pid file failed>\n")));
-  if (flag != NULL)
-    sh.flag.islocked       = BAD;
-  SL_RETURN((-1),_("sh_unix_lock"));
-
-  /* notreached */
-}
-
+int sh_unix_unlock (char * lockfile, char * flag);
+int sh_unix_lock   (char * lockfile, char * flag);
 
 /* check whether file is locked
@@ -4670,4 +3157,14 @@
 
   SL_ENTER(_("sh_unix_test_and_lock"));
+
+  if (filename != NULL)
+    {
+      status = retry_lstat (FIL__, __LINE__, filename, &buf);
+
+      /* no logfile to lock
+       */
+      if (status < 0)
+	SL_RETURN((-1),_("sh_unix_test_and_lock"));
+    }
 
   status = retry_lstat (FIL__, __LINE__, lockfile, &buf);
@@ -4683,12 +3180,4 @@
 	    sh.flag.islocked = GOOD;
 	  SL_RETURN((0),_("sh_unix_test_and_lock"));
-	}
-      else
-	{
-	  sh_error_handle ((-1), FIL__, __LINE__, status,
-			   MSG_E_SUBGEN, 
-			   (filename == NULL) ? _("Cannot create PID file (1)") : _("Cannot create lock file (1)"),
-			   _("sh_unix_test_and_lock"));
-	  SL_RETURN((-1),_("sh_unix_test_and_lock"));
 	}
     }
@@ -4708,8 +3197,8 @@
       else
 	{
-	  sh_error_handle ((-1), FIL__, __LINE__, status,
-			   MSG_E_SUBGEN, 
-			   (filename == NULL) ? _("Cannot create PID file (2)") : _("Cannot create lock file (2)"),
-			   _("sh_unix_test_and_lock"));
+	   sh_error_handle ((-1), FIL__, __LINE__, status,
+			    MSG_E_SUBGEN, 
+			    (filename == NULL) ? _("Cannot create PID file") : _("Cannot create lock file"),
+			    _("sh_unix_test_and_lock"));
 	  SL_RETURN((-1),_("sh_unix_test_and_lock"));
 	}
@@ -4721,5 +3210,5 @@
   if (status >= 0)
     {
-       fd = sl_open_read (FIL__, __LINE__, lockfile, SL_YESPRIV);
+       fd = sl_open_read (lockfile, SL_YESPRIV);
        if (SL_ISERROR(fd))
 	 sh_error_handle ((-1), FIL__, __LINE__, fd,
@@ -4735,6 +3224,5 @@
       /* read the PID in the lock file
        */
-      status = sl_read (fd, line_in, sizeof(line_in));
-      line_in[sizeof(line_in)-1] = '\0';
+      status = sh_unix_getline (fd, line_in, sizeof(line_in)-1);
 
       /* convert to numeric
@@ -4763,5 +3251,5 @@
       sl_close(fd);
 
-      if (status > 0 && (unsigned int) status == sh.pid)
+      if (status == (int) getpid())
 	{
 	  if (filename != NULL)
@@ -4799,5 +3287,5 @@
 		       sh_error_handle ((-1), FIL__, __LINE__, status,
 					MSG_E_SUBGEN, 
-					(filename == NULL) ? _("Cannot create PID file (3)") : _("Cannot create lock file (3)"),
+					(filename == NULL) ? _("Cannot create PID file") : _("Cannot create lock file"),
 					_("sh_unix_test_and_lock"));
 		    }
@@ -4831,6 +3319,5 @@
 int sh_unix_write_lock_file(char * filename)
 {
-  size_t len;
-  int    res;
+  int i;
   char * lockfile;
 
@@ -4838,13 +3325,46 @@
     return (-1);
 
-  len = sl_strlen(filename);
-  if (sl_ok_adds(len, 6))
-    len += 6;
-  lockfile = SH_ALLOC(len);
-  sl_strlcpy(lockfile, filename,   len);
-  sl_strlcat(lockfile, _(".lock"), len);
-  res = sh_unix_test_and_lock(filename, lockfile);
+  i        = 6 + sl_strlen(filename);
+  lockfile = SH_ALLOC(i);
+  sl_strlcpy(lockfile, filename,   i);
+  sl_strlcat(lockfile, _(".lock"), i);
+  i = sh_unix_test_and_lock(filename, lockfile);
   SH_FREE(lockfile);
-  return res;
+  return i;
+}
+
+int sh_unix_unlock(char * lockfile, char * flag)
+{
+  int         error = 0;
+  
+  SL_ENTER(_("sh_unix_unlock"));
+
+  /* --- Logfile is not locked to us. ---
+   */
+  if (sh.flag.islocked == BAD && flag != NULL) 
+    SL_RETURN((-1),_("sh_unix_unlock"));
+
+  /* --- Check whether the directory is secure. ---
+   */
+  if (0 != tf_trust_check (lockfile, SL_YESPRIV))
+    SL_RETURN((-1),_("sh_unix_unlock"));
+
+  /* --- Delete the lock file. --- 
+   */
+  error = retry_aud_unlink (FIL__, __LINE__, lockfile);
+  
+  if (error == 0)
+    {
+      if (flag != NULL)
+	sh.flag.islocked = BAD; /* not locked anymore */
+    }
+  else if (flag != NULL)
+    {
+      error = errno;
+      sh_error_handle ((-1), FIL__, __LINE__, error, MSG_E_UNLNK,
+		       sh_error_message(error), lockfile);
+      SL_RETURN((-1),_("sh_unix_unlock"));
+    }
+  SL_RETURN((0),_("sh_unix_unlock"));
 }
 
@@ -4853,6 +3373,5 @@
 int sh_unix_rm_lock_file(char * filename)
 {
-  size_t len;
-  int res;
+  int i;
   char * lockfile;
 
@@ -4860,14 +3379,11 @@
     return (-1);
 
-  len = sl_strlen(filename);
-  if (sl_ok_adds(len, 6))
-    len += 6;
-  lockfile = SH_ALLOC(len);
-  sl_strlcpy(lockfile, filename,   len);
-  sl_strlcat(lockfile, _(".lock"), len);
-
-  res = sh_unix_unlock(lockfile, filename);
+  i        = 6 + sl_strlen(filename);
+  lockfile = SH_ALLOC(i);
+  sl_strlcpy(lockfile, filename,   i);
+  sl_strlcat(lockfile, _(".lock"), i);
+  i = sh_unix_unlock(lockfile, filename);
   SH_FREE(lockfile);
-  return res;
+  return i;
 }
 
@@ -4879,34 +3395,89 @@
 }
 
+int sh_unix_lock (char * lockfile, char * flag)
+{
+  struct stat buf;
+  int status;
+  int filed;
+  int errnum;
+  char myPid[64];
+  SL_TICKET  fd;
+  extern int get_the_fd (SL_TICKET ticket);
+
+
+  status = retry_lstat (FIL__, __LINE__, lockfile, &buf);
+  
+  SL_ENTER(_("sh_unix_lock"));
+
+  if (0 == status) 
+    {
+      if (flag != NULL)
+	sh.flag.islocked       = BAD;
+      SL_RETURN((-1),_("sh_unix_lock"));
+    }
+
+  sprintf (myPid, "%ld\n", (long) getpid());           /* known to fit  */
+
+  fd = sl_open_write (lockfile, SL_YESPRIV);
+
+  if (!SL_ISERROR(fd))
+    {
+      errnum = sl_write (fd, myPid, sl_strlen(myPid));
+      filed = get_the_fd(fd);
+      fchmod (filed, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
+      sl_close (fd);
+
+      if (!SL_ISERROR(errnum))
+	{
+	  if (flag != NULL)
+	    sh.flag.islocked = GOOD;
+	  SL_RETURN((0),_("sh_unix_lock"));
+	}
+    }
+
+  TPT((0, FIL__, __LINE__, _("msg=<open pid file failed>\n")));
+  if (flag != NULL)
+    sh.flag.islocked       = BAD;
+  SL_RETURN((-1),_("sh_unix_lock"));
+
+  /* notreached */
+}
+
 /* Test whether file exists
  */
-int sh_unix_file_exists(char * path)
+int sh_unix_file_stat(char * path)
 {
   struct stat buf;
 
+  SL_ENTER(_("sh_unix_file_stat"));
+
+  if (-1 == retry_stat(FIL__, __LINE__, path, &buf))
+    SL_RETURN(  (0), _("sh_unix_file_stat"));
+  else 
+    SL_RETURN( (-1), _("sh_unix_file_stat"));
+}
+
+
+/* Test whether file exists, is a character device, allows read
+ * access, and is not world writeable.
+ */
+int sh_unix_file_exists(int fd)
+{
+  struct stat buf;
+
   SL_ENTER(_("sh_unix_file_exists"));
 
-  if (0 == retry_lstat(FIL__, __LINE__, path, &buf))
-    SL_RETURN( S_TRUE,   _("sh_unix_file_exists"));
-
-  SL_RETURN( S_FALSE,  _("sh_unix_file_exists"));
-}
-
-
-/* Test whether file exists, is a character device, and allows read
- * access.
- */
-int sh_unix_device_readable(int fd)
-{
-  struct stat buf;
-
-  SL_ENTER(_("sh_unix_device_readable"));
-
   if (retry_fstat(FIL__, __LINE__, fd, &buf) == -1)
-    SL_RETURN( (-1), _("sh_unix_device_readable"));
-  if ( S_ISCHR(buf.st_mode) &&  0 != (S_IROTH & buf.st_mode) ) 
-    SL_RETURN( (0), _("sh_unix_device_readable"));
-
-  SL_RETURN( (-1), _("sh_unix_device_readable"));
+    SL_RETURN( (-1), _("sh_unix_file_exists"));
+  else if ( S_ISCHR(buf.st_mode) &&  0 != (S_IROTH & buf.st_mode)
+	    /*
+	     * #if !defined(__CYGWIN32__) && !defined(__CYGWIN__) 
+	     * && 0 == (S_IWOTH & buf.st_mode)
+	     * #endif
+	     */
+	    ) 
+    SL_RETURN( (0), _("sh_unix_file_exists"));
+  else 
+    SL_RETURN( (-1), _("sh_unix_file_exists"));
 }
 
@@ -4918,5 +3489,4 @@
 {
   static int init = 0;
-  struct stat buf;
 
   SL_ENTER(_("file_is_remote"));
@@ -4929,26 +3499,4 @@
   if (0 == sl_strncmp (sh.data.path, preq, 15))
     {
-      if (sh.data.path[15] != '\0') /* should be start of path */
-	{
-	  if (0 == stat(&(sh.data.path[15]), &buf))
-	    {
-	      SL_RETURN( S_FALSE, _("file_is_remote"));
-	    }
-	  else
-	    {
-	      char * tmp  = sh_util_safe_name (&(sh.data.path[15]));
-	      sh_error_handle((-1), FIL__, __LINE__, S_FALSE, MSG_E_SUBGPATH, 
-			      _("No local baseline database at expected path"), 
-			      _("file_is_remote"),
-			      tmp);
-	      SH_FREE(tmp);
-	    }
-	}
-      else
-	{
-	  sh_error_handle((-1), FIL__, __LINE__, S_FALSE, MSG_E_SUBGEN, 
-			  _("No local baseline database path known"), 
-			  _("file_is_remote"));
-	}
       SL_RETURN( S_TRUE, _("file_is_remote"));
     }
@@ -5042,6 +3590,4 @@
   unsigned long  page_start;
   int            page_refcount;
-  char           file[64];
-  int            line;
   struct sh_page_lt * next;
 } sh_page_l;
@@ -5087,13 +3633,10 @@
 
 #if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
-
-SH_MUTEX_STATIC(mutex_mlock,PTHREAD_MUTEX_INITIALIZER);
-
-int sh_unix_mlock (const char * file, int line, void * in_addr, size_t len)
+int sh_unix_mlock (void * in_addr, size_t len)
 {
   int         num_pages;
   int         status = 0;
   int         pagesize;
-  sh_page_l * page_list;
+  sh_page_l * page_list = sh_page_locked;
   unsigned long addr;
 #ifdef TEST_MLOCK
@@ -5103,16 +3646,8 @@
   SL_ENTER(_("sh_unix_mlock"));
 
-  /* There's no cancellation point here, except if tracing is on
-   */
-  SH_MUTEX_LOCK_UNSAFE(mutex_mlock);
-
-  page_list = sh_page_locked;
-
   if (0 != page_locking)
     {
-      status = -1;
-      goto exit_mlock;
-    }
-
+      SL_RETURN((-1), _("sh_unix_mlock"));
+    }
   page_locking = 1;
 
@@ -5128,5 +3663,4 @@
    * addr is first page; num_pages is #(consecutive pages) to lock
    */
-
   while ((page_list != NULL) && (num_pages > 0))
     {
@@ -5163,17 +3697,13 @@
       page_list->page_start = addr;
       page_list->page_refcount = 1;
-      sl_strlcpy(page_list->file, file, 64);
-      page_list->line = line;
       status = mlock( (void *) addr, pagesize);
       if (status != 0)
 	{
 #ifdef TEST_MLOCK
-	  char errbuf[SH_ERRBUF_SIZE];
-	  fprintf(stderr, "mlock: error: %s\n", 
-		  sh_error_message(errno, errbuf, sizeof(errbuf)));
+	  fprintf(stderr, "mlock: error: %s\n", sh_error_message(errno));
 #endif
 	  SH_FREE(page_list);
 	  page_locking = 0;
-	  goto exit_mlock;
+	  SL_RETURN((status), _("sh_unix_mlock"));
 	}
       page_list->next = sh_page_locked;
@@ -5182,15 +3712,11 @@
       addr += pagesize;
     }
+
   page_locking = 0;
-
- exit_mlock:
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_mlock);
-
   SL_RETURN((status), _("sh_unix_mlock"));
 }
 #else
-int sh_unix_mlock (const char * file, int line, void * in_addr, size_t len)
-{
-  (void) file;    (void) line;
+int sh_unix_mlock (void * in_addr, size_t len)
+{
   (void) in_addr; (void) len;
   return -1;
@@ -5202,8 +3728,8 @@
 {
   int         num_pages;
-  int         unlocked;
-  int         status;
+  int         unlocked = 0;
+  int         status   = 0;
   int         pagesize;
-  sh_page_l * page_list;
+  sh_page_l * page_list = sh_page_locked;
   sh_page_l * page_last;
   unsigned long addr;
@@ -5219,16 +3745,7 @@
   SL_ENTER(_("sh_unix_munlock"));
 
-  /* There's no cancellation point here, except if tracing is on
-   */
-  SH_MUTEX_LOCK_UNSAFE(mutex_mlock);
-
-  unlocked  = 0;
-  status    = 0;
-  page_list = sh_page_locked;
-
   if (0 != page_locking)
     {
-      status = -1;
-      goto exit_munlock;
+      SL_RETURN((-1), _("sh_unix_munlock"));
     }
   page_locking = 1;
@@ -5335,7 +3852,4 @@
 
   page_locking = 0;
-
- exit_munlock:
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_mlock);
   SL_RETURN((status), _("sh_unix_munlock"));
 }
@@ -5350,48 +3864,17 @@
 int sh_unix_count_mlock()
 {
-  unsigned int i = 0;
-  char str[32][64];
-  sh_page_l * page_list;
+  int i = 0;
+  char str[128];
+  sh_page_l * page_list = sh_page_locked;
 
   SL_ENTER(_("sh_unix_count_mlock"));
-
-#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
-  /* There's no cancellation point here, except if tracing is on
-   */
-  SH_MUTEX_LOCK_UNSAFE(mutex_mlock);
-#endif
-
-  page_list = sh_page_locked;
-
   while (page_list != NULL)
     {
-#ifdef WITH_TPT
-      if (i < 32)
-	sl_snprintf(str[i], 64, _("file: %s line: %d page: %d"), 
-		    page_list->file, page_list->line, i+1);
-#endif
       page_list = page_list->next;
       ++i;
     }
-
-#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_mlock);
-#endif
-
-#ifdef WITH_TPT
-  {
-    unsigned int j = 0;
-    while (j < i && j < 32)
-      {
-	sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, j, MSG_E_SUBGEN,
-			str[j], _("sh_unix_count_mlock"));
-	++j;
-      }
-  }
-#endif
-
-  sl_snprintf(str[0], 64, _("%d pages locked"), i);
-  sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, i, MSG_E_SUBGEN,
-		  str[0], _("sh_unix_count_mlock"));
+  sprintf(str, _("%d pages locked"), i);               /* known to fit  */
+  sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, i, MSG_E_SUBGEN,
+		  str, _("sh_unix_count_mlock"));
   SL_RETURN((i), _("sh_unix_count_mlock"));
 }
@@ -5414,7 +3897,9 @@
 #if  !defined(SH_STEALTH_MICRO)
 
-
-int hideout_hex_block(SL_TICKET fd, unsigned char * str, int len,
-		      unsigned long * bytes_read);
+static unsigned long off_data = 0;
+static unsigned long max_data = 0;
+static int           stealth_init = BAD;
+
+int hideout_hex_block(SL_TICKET fd, unsigned char * str, int len);
 unsigned long first_hex_block(SL_TICKET fd, unsigned long * max);
 
@@ -5424,21 +3909,7 @@
 int sh_unix_getline_stealth (SL_TICKET fd, char * str, int len)
 {
-  int                  add_off = 0, llen;
-  unsigned long        bread;
-  static unsigned long off_data   = 0;
-  static unsigned long max_data   = 0;
-  static unsigned long bytes_read = 0;
-  static int           stealth_init = BAD;
+  int add_off, llen;
 
   SL_ENTER(_("sh_unix_getline_stealth"));
-
-  if (str == NULL)
-    {
-      off_data   = 0;
-      max_data   = 0;
-      bytes_read = 0;
-      stealth_init = BAD;
-      SL_RETURN(0, _("sh_unix_getline_stealth"));
-    }
 
   /* --- Initialize. ---
@@ -5456,28 +3927,15 @@
 	}
       stealth_init = GOOD;
-      max_data += off_data;
     }
   
   /* --- Seek to proper position. ---
    */
-  if (bytes_read >= max_data || add_off < 0)
-    {
-      dlog(1, FIL__, __LINE__, 
-	   _("The capacity of the container image file for the stealth config file seems to be too small. Your config file is likely truncated.\n"));
-      sh_error_handle ((-1), FIL__, __LINE__,  EIO, MSG_P_NODATA,
-		       _("Stealth config file."));
-      aud_exit (FIL__, __LINE__, EXIT_FAILURE);
-    }
   sl_seek(fd, off_data);
      
   /* --- Read one line. ---
    */
-  add_off   = hideout_hex_block(fd, (unsigned char *) str, len, &bread);
-  if (add_off > 0)
-    off_data += add_off;
-  bytes_read += bread;
-
-  if (bread == 0 || add_off <= 0) /* EOF */
-    str[0] = '\0';
+  add_off   = hideout_hex_block(fd, (unsigned char *) str, len);
+  off_data += add_off;
+
 
   llen = sl_strlen(str);
@@ -5485,6 +3943,5 @@
 }
 
-int hideout_hex_block(SL_TICKET fd, unsigned char * str, int len, 
-		      unsigned long * bytes_read)
+int hideout_hex_block(SL_TICKET fd, unsigned char * str, int len)
 {
 
@@ -5495,13 +3952,6 @@
   unsigned long here   = 0;
   unsigned long retval = 0;
-  unsigned long bread  = 0;
 
   SL_ENTER(_("hideout_hex_block"));
-
-  ASSERT_RET((len > 1), _("len > 1"), (0));
-
-  str[0] = '\0';
-  *bytes_read = 0;
-  --len;
 
   i = 0;
@@ -5520,15 +3970,9 @@
 	      do {
 		do {
-		  errno = 0;
 		  num = sl_read (fd, &c, 1);
 		} while (num == 0 && errno == EINTR);
-		if (num > 0)
-		  ++here;
-		else if (num == 0) {
+		if (num == 0) 
 		  SL_RETURN((-1), _("hideout_hex_block"));
-		}
-		else {
-		  SL_RETURN((-1), _("hideout_hex_block"));
-		}
+		++here; 
 	      } while (c == '\n' || c == '\t' || c == '\r' || 
 		       c == ' ');
@@ -5544,5 +3988,4 @@
 	    str[i] &= ~mask[j];
 
-	  bread += 1;
 	}
       if (str[i] == '\n') break;
@@ -5552,10 +3995,7 @@
   if (i != 0)
     str[i] = '\0';
-  else if (str[0] == '\n')
-    str[i+1] = '\0'; /* keep newline and terminate */
   else
-    str[0] = '\0';
+    str[i+1] = '\0';
   retval += here;
-  *bytes_read += (bread/8);
 
   SL_RETURN(retval, _("hideout_hex_block"));
@@ -5566,11 +4006,10 @@
 unsigned long first_hex_block(SL_TICKET fd, unsigned long * max)
 {
-  unsigned int  i;
-  long          num = 1;
-  unsigned long lnum;
+  int           i;
+  register int  num = 1;
   char          c;
   int           nothex = 0;
   unsigned long retval = 0;
-  unsigned int  this_line = 0;
+  int           this_line = 0;
   char          theline[SH_BUFSIZE];
 
@@ -5584,5 +4023,5 @@
       this_line  = 0;
       c          = '\0';
-      while (c != '\n' && this_line < (sizeof(theline)-1))
+      while (c != '\n' && num > 0)
 	{
 	  do {
@@ -5593,5 +4032,5 @@
 	  else           
 	    SL_RETURN((0), _("first_hex_block"));
-	  ++this_line;
+	  this_line += num;
 	}
       theline[this_line] = '\0';
@@ -5622,17 +4061,13 @@
 	      num = sl_read (fd, theline, SH_BUFSIZE);
 	    } while (num == 0 && errno == EINTR);
-	    if (num > 0)
-	      {
-		lnum = (unsigned long) num;
-		for (i = 0; i < lnum; ++i)
-		  { 
-		    c = theline[i];
-		    if (c == '\n' || c == '\t' || c == '\r' || c == ' ') 
-		      ;
-		    else if (!isxdigit((int)c))
-		      break;
-		    else
-		      *max += 1;
-		  }
+	    for (i = 0; i < num; ++i)
+	      { 
+		c = theline[i];
+		if (c == '\n' || c == '\t' || c == '\r' || c == ' ') 
+		  ;
+		else if (!isxdigit((int)c))
+		  break;
+		else
+		  *max += 1;
 	      }
 	  } while (num > 0);
@@ -5656,93 +4091,23 @@
  */
 #if defined(SCREW_IT_UP)
-
-#if defined(HAVE_PTHREAD)
-
-static pthread_key_t  gSigtrapVariables_key;
-static pthread_once_t gSigtrapVariables_key_once = PTHREAD_ONCE_INIT;
-
-static inline void make_gSigtrapVariables_key()
-{
-    (void) pthread_key_create(&gSigtrapVariables_key, free);
-}
-
-struct sh_sigtrap_variables * sh_sigtrap_variables_get()
-{
-  void * ptr;
-
-  (void) pthread_once(&gSigtrapVariables_key_once, make_gSigtrapVariables_key);
- 
-  ptr = pthread_getspecific(gSigtrapVariables_key);
-  if (ptr == NULL) {
-    ptr = calloc(1,sizeof(struct sh_sigtrap_variables));
-    if (ptr == NULL) {
-      return NULL;
-    }
-    (void) pthread_setspecific(gSigtrapVariables_key, ptr);
-  }
-
-  return (struct sh_sigtrap_variables *) ptr;
-}
-
-/* !defined(HAVE_PTHREAD) */
-#else
-
-static struct sh_sigtrap_variables global_sigtrap_variables;
-struct sh_sigtrap_variables * sh_sigtrap_variables_get()
-{
-  return &global_sigtrap_variables;
-}
-
-#endif
-
-int sh_sigtrap_max_duration_set (const char * str)
-{
-  /* For security (prevent reloading with larger value)
-   * this value can only be set once.
-   */
-  static int once = 0;
-  int i;
-
-  SL_ENTER(_("sh_sigtrap_max_duration_set"));
-
-  i = atoi (str);
-
-  if (i >= 0 && once == 0)
-    {
-      sh.sigtrap_max_duration = i;
-      once = 1;
-    }
-  else
-    {
-      SL_RETURN ((-1), _("sh_sigtrap_max_duration_set"));
-    }
-  SL_RETURN( (0), _("sh_sigtrap_max_duration_set"));
-}
+volatile int sh_not_traced = 0;
+
+#ifdef HAVE_GETTIMEOFDAY
+struct timeval  save_tv;
+#endif
 
 void sh_sigtrap_handler (int signum)
 {
-  struct sh_sigtrap_variables * sigtrap_variables;
-  sigtrap_variables = sh_sigtrap_variables_get();
-  if (sigtrap_variables == NULL) {
-    /* Perhaps, it's better to not die, and to continue using Samhain,
-       even if this part does not work. */
-    return;
-  }
-
 #ifdef HAVE_GETTIMEOFDAY
-  {
-    struct timeval  tv;
-    long   difftv;
-    
-    gettimeofday(&tv, NULL);
-    difftv = (tv.tv_sec - sigtrap_variables->save_tv.tv_sec) * 1000000 + 
-      (tv.tv_usec - sigtrap_variables->save_tv.tv_usec);
-    if (difftv > sh.sigtrap_max_duration)
-      raise(SIGKILL);
-  }
-#endif
-
-  sigtrap_variables->not_traced = signum;
-  /* cppcheck-suppress memleak */
+  struct timeval  tv;
+  long   difftv;
+  
+  gettimeofday(&tv, NULL);
+  difftv = (tv.tv_sec - save_tv.tv_sec) * 1000000 + 
+    (tv.tv_usec - save_tv.tv_usec);
+  if (difftv > 500000)
+    _exit(6);
+#endif
+  sh_not_traced += signum;
   return;
 }
Index: trunk/src/sh_userfiles.c
===================================================================
--- trunk/src/sh_userfiles.c	(revision 591)
+++ trunk/src/sh_userfiles.c	(revision 1)
@@ -30,4 +30,5 @@
 #include <pwd.h>
 
+
 #include "samhain.h"
 #include "sh_modules.h"
@@ -38,7 +39,4 @@
 #include "sh_hash.h"
 #include "sh_files.h"
-#define SH_NEED_PWD_GRP 1
-#include "sh_static.h"
-#include "sh_pthread.h"
 
 #ifdef SH_USE_USERFILES
@@ -117,8 +115,8 @@
 }
   
-int sh_userfiles_set_uid (const char * str)
+int sh_userfiles_set_uid (char * str)
 {
   char * end;
-  const  char * p = str;
+  char * p = str;
   unsigned long lower;
   unsigned long upper = 0;
@@ -185,5 +183,5 @@
  * directory that should be checked. */
 
-int sh_userfiles_add_file(const char *c) {
+int sh_userfiles_add_file(char *c) {
     struct userfileslist *new;
     char *s, *orig;
@@ -215,16 +213,13 @@
      * So, compare longest strings first */
     if( s == NULL ) /* The default */          new->level = default_level;
-    else if ( strstr(s, _("attributes"))!= NULL ) new->level = SH_LEVEL_ATTRIBUTES;
-    else if ( strstr(s, _("allignore")) != NULL ) new->level = SH_LEVEL_ALLIGNORE;
-    else if ( strstr(s, _("noignore"))  != NULL ) new->level = SH_LEVEL_NOIGNORE;
-    else if ( strstr(s, _("logfiles"))  != NULL ) new->level = SH_LEVEL_LOGFILES;
-    else if ( strstr(s, _("readonly"))  != NULL ) new->level = SH_LEVEL_READONLY;
-    else if ( strstr(s, _("loggrow"))   != NULL ) new->level = SH_LEVEL_LOGGROW;
-    else if ( strstr(s, _("user0"))     != NULL ) new->level = SH_LEVEL_USER0;
-    else if ( strstr(s, _("user1"))     != NULL ) new->level = SH_LEVEL_USER1;
-    else if ( strstr(s, _("user2"))     != NULL ) new->level = SH_LEVEL_USER2;
-    else if ( strstr(s, _("user3"))     != NULL ) new->level = SH_LEVEL_USER3;
-    else if ( strstr(s, _("user4"))     != NULL ) new->level = SH_LEVEL_USER4;
-    else if ( strstr(s, _("prelink"))   != NULL ) new->level = SH_LEVEL_PRELINK;
+    else if ( strstr(s, "attributes")!= NULL ) new->level = SH_LEVEL_ATTRIBUTES;
+    else if ( strstr(s, "allignore") != NULL ) new->level = SH_LEVEL_ALLIGNORE;
+    else if ( strstr(s, "noignore")  != NULL ) new->level = SH_LEVEL_NOIGNORE;
+    else if ( strstr(s, "logfiles")  != NULL ) new->level = SH_LEVEL_LOGFILES;
+    else if ( strstr(s, "readonly")  != NULL ) new->level = SH_LEVEL_READONLY;
+    else if ( strstr(s, "loggrow")   != NULL ) new->level = SH_LEVEL_LOGGROW;
+    else if ( strstr(s, "user0")     != NULL ) new->level = SH_LEVEL_USER0;
+    else if ( strstr(s, "user1")     != NULL ) new->level = SH_LEVEL_USER1;
+    else if ( strstr(s, "prelink")   != NULL ) new->level = SH_LEVEL_PRELINK;
     else            /* The default */          new->level = default_level;
 
@@ -234,11 +229,21 @@
 }
 
-/* Decide if we're active. 
- */
-int sh_userfiles_set_active(const char *c) {
+/* Decide if we're active.  1 means yes, all else means no */
+
+int sh_userfiles_set_active(char *c) {
     int value;
     
     SL_ENTER(_("sh_userfiles_set_active"));
+
     value = sh_util_flagval(c, &ShUserfilesActive);
+
+    /*
+    if( value == 1 ) {
+        ShUserfilesActive = S_TRUE;
+    } else {
+        ShUserfilesActive = S_FALSE;
+    }
+    */
+
     SL_RETURN((value), _("sh_userfiles_set_active"));
 }
@@ -246,11 +251,9 @@
 /* Build the list of users, then use this to construct the filenames to
  * be checked. */
-int sh_userfiles_init(struct mod_type * arg) {
+int sh_userfiles_init(void) {
     struct passwd *cur_user;
     struct userhomeslist *end;
     struct userhomeslist *new;
     struct userhomeslist *homes;
-    char * filepath;
-    (void) arg;
 
     SL_ENTER(_("sh_userfiles_init"));
@@ -269,9 +272,8 @@
     /* We build a list in here because the samhain internals want to use
      * getpwent() too */
-    SH_MUTEX_LOCK(mutex_pwent);
     /*@-unrecog@*/
-    sh_setpwent();
+    setpwent();
     /*@+unrecog@*/
-    while( ( cur_user = /*@-unrecog@*/sh_getpwent()/*@+unrecog@*/ ) != NULL ) {
+    while( ( cur_user = /*@-unrecog@*/getpwent()/*@+unrecog@*/ ) != NULL ) {
         int found = 0;
 
@@ -295,11 +297,8 @@
         }
     }
-    sh_endpwent();
-    SH_MUTEX_UNLOCK(mutex_pwent);
-
-    filepath = SH_ALLOC(PATH_MAX);
 
     for (homes = userHomes; homes != NULL; homes = homes->next ) {
         struct userfileslist *file_ptr;
+        char filepath[PATH_MAX];
 
         for (file_ptr = userFiles; file_ptr != NULL; file_ptr = file_ptr->next) {
@@ -333,13 +332,4 @@
                     (void) sh_files_pushfile_user1(filepath);
                     break;
-                case SH_LEVEL_USER2:
-                    (void) sh_files_pushfile_user2(filepath);
-                    break;
-                case SH_LEVEL_USER3:
-                    (void) sh_files_pushfile_user3(filepath);
-                    break;
-                case SH_LEVEL_USER4:
-                    (void) sh_files_pushfile_user4(filepath);
-                    break;
                 case SH_LEVEL_PRELINK:
                     (void) sh_files_pushfile_prelink(filepath);
@@ -350,6 +340,4 @@
         }
     }
-
-    SH_FREE(filepath);
 
     SL_RETURN(0, _("sh_userfiles_init"));
@@ -393,7 +381,4 @@
     userHomes = NULL;
     userFiles = NULL;
-    userUids  = NULL;
-
-    ShUserfilesActive   = S_TRUE;
 
     SL_RETURN(0, _("sh_userfiles_reconf"));
Index: trunk/src/sh_utils.c
===================================================================
--- trunk/src/sh_utils.c	(revision 591)
+++ trunk/src/sh_utils.c	(revision 1)
@@ -27,8 +27,15 @@
 #include <unistd.h>
 
+#if TIME_WITH_SYS_TIME
+#include <sys/time.h>
+#include <time.h>
+#else
 #if HAVE_SYS_TIME_H
 #include <sys/time.h>
-#endif
+#else
 #include <time.h>
+#endif
+#endif
+
 
 #include "samhain.h"
@@ -38,5 +45,4 @@
 #include "sh_tiger.h"
 #include "sh_entropy.h"
-#include "sh_pthread.h"
 
 #undef  FIL__
@@ -45,5 +51,5 @@
 UINT32 ErrFlag[2];
 
-int sh_util_flagval(const char * c, int * fval)
+int sh_util_flagval(char * c, int * fval)
 {
   SL_ENTER(_("sh_util_flagval"));
@@ -95,45 +101,15 @@
 static int sh_ask_update = S_FALSE;
 
-int sh_util_set_interactive(const char * str)
-{
-  (void) str;
-
-  sh_ask_update = S_TRUE;
+int sh_util_set_interactive(char * str)
+{
+  if (str == NULL)
+    sh_ask_update = S_TRUE;
+  else
+    sh_ask_update = S_TRUE;
+
   sh_unix_setnodeamon(NULL);
 
   return 0;
 }
-
-static char * sh_update_file = NULL;
-
-int sh_util_update_file (const char * str)
-{
-  if (str)
-    {
-      if (0 == access(str, R_OK)) /* flawfinder: ignore */
-	{
-	  if (NULL != sh_update_file)
-	    SH_FREE(sh_update_file);
-	  sh_update_file = sh_util_strdup(str);
-	  sh_ask_update = S_TRUE;
-	  sh_unix_setnodeamon(NULL);
-	  return 0;
-	}
-      else
-	{
-	  char ebuf[SH_ERRBUF_SIZE];
-	  int  errnum = errno;
-
-	  sh_error_message(errnum, ebuf, sizeof(ebuf));
-	  sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, errnum, MSG_E_SUBGEN,
-			   ebuf, _("sh_util_update_file") );
-	  
-	  return -1;
-	}
-    }
-
-  return -1;
-}
-
 
 #if !defined(STDIN_FILENO)
@@ -144,54 +120,5 @@
 #endif
 
-/* Returns S_FALSE if no update desired 
- */
-int sh_util_update_checkfile(const char * path)
-{
-  FILE * fd = fopen(sh_update_file, "r");
-  char * line;
-
-  if (!fd)
-    {
-      uid_t  euid;
-      int errnum = errno;
-      sl_get_euid(&euid);
-      sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, errnum, MSG_NOACCESS,
-		       (long) euid, sh_update_file);
-      aud_exit (FIL__, __LINE__, EXIT_FAILURE);
-      return S_FALSE;
-    }
-
-  line = SH_ALLOC(8192);
-
-  while (NULL != fgets(line, 8192, fd))
-    {
-      char * nl = strrchr(line, '\n');
-
-      if (nl)
-	{
-	  *nl = '\0';
-
-	  /* Check for MS Windows line terminator 
-	   */
-	  if (nl > line) --nl;
-	  if (*nl == '\r')
-	    *nl = '\0';
-	}
-
-      if (0 == sl_strcmp(line, path))
-	{
-	  SH_FREE(line);
-	  fclose(fd);
-	  return S_TRUE;
-	}
-    }
-  SH_FREE(line);
-  fclose(fd);
-  return S_FALSE;
-}
-
-/* Returns S_FALSE if no update desired 
- */
-int sh_util_ask_update(const char * path)
+int sh_util_ask_update(char * path)
 {
   int    inchar, c;
@@ -206,10 +133,4 @@
     }
 
-  if (sh_update_file)
-    {
-      i = sh_util_update_checkfile(path);
-      SL_RETURN(i, _("sh_util_ask_update"));
-    }
-
 #ifdef HAVE_TTYNAME
   if (!ttyname(STDIN_FILENO))
@@ -217,5 +138,4 @@
       if (NULL != ttyname(STDERR_FILENO))
         {
-	  /* cppcheck-suppress leakReturnValNotUsed */
           if (NULL == freopen(ttyname(STDERR_FILENO), "r", stdin))
             {
@@ -261,5 +181,5 @@
 	  else
 	    {
-	      fprintf(stderr, "%s", _("Please answer y(es) or n(o)\n"));
+	      fprintf(stderr, _("Please answer y(es) or n(o)\n"));
 	    }
 	  /*@-charintliteral@*/
@@ -270,5 +190,5 @@
 }
 
-int sh_util_hidesetup(const char * c)
+int sh_util_hidesetup(char * c)
 {
   int i;
@@ -278,98 +198,5 @@
   SL_RETURN(i, _("sh_util_hidesetup"));
 }
-
-char * sh_util_acl_compact(char * buf, ssize_t len)
-{
-  unsigned char  * p = (unsigned char *) buf;
-  int       state = 0;
-  ssize_t   rem = 0;
-  char    * out;
-  
-  SH_VALIDATE_NE(buf, NULL);
-  SH_VALIDATE_GE(len, 0);
-
-  out = SH_ALLOC(len + 1);
-
-  while (*p != '\0')  {
-
-    /* -- not at start or after newline
-     */
-    if (state == 1) {
-      if (*p == '\n' || *p == ' ' || *p == '\t' || *p == '#') {
-	while (*p != '\n') {
-	  ++p;
-	  if (*p == '\0') {
-	    goto exit_it;
-	  }
-	}
-	out[rem] = ','; ++rem;    /* <-- ensures (rem > 0) is true    */ 
-	while (p[1] == '\n') ++p; /* scan over consecutive newlines   */
-	state = 0;
-	if (p[1] == '\0') {
-	  out[rem-1] = '\0';      /* rem > 0 because of 4 lines above */
-	  break;
-	}
-      }
-      else {
-	if (*p <= 0x7F && isgraph((int) *p)) {
-	  out[rem] = (char) *p; ++rem;
-	}
-      }
-    }
-
-    /* -- at start or after newline
-     */
-    else /* if (state == 0) */ {
-      if        (0 == strncmp((char *) p, "user", 4)) {
-	out[rem] = 'u'; ++rem;
-	p += 3;
-      } else if (0 == strncmp((char *) p, "group", 5)) {
-	out[rem] = 'g'; ++rem;
-	p += 4; 
-      } else if (0 == strncmp((char *) p, "mask", 4)) {
-	out[rem] = 'm'; ++rem;
-	p += 3;
-      } else if (0 == strncmp((char *) p, "other", 5)) {
-	out[rem] = 'o';
-	p += 4; ++rem;
-      } else if (*p == '\0') {
-	if (rem > 0) { out[rem-1] = '\0'; }
-	break;
-      } else {
-	if (*p <= 0x7F && isprint((int) *p)) {
-	  out[rem] = (char) *p; ++rem;
-	}
-      }
-      state = 1;
-    }
-    ++p;
-  }
- exit_it:
-  out[rem] = '\0';
-  return out;
-}
-
-
-char * sh_util_strdup_l (const char * str, size_t len)
-{
-  char * p = NULL;
-
-  SL_ENTER(_("sh_util_strdup_l"));
-
-  SH_VALIDATE_NE(str, NULL);
-  SH_VALIDATE_NE(len, 0);
-
-  if (str && sl_ok_adds (len, 1))
-    {
-      p   = SH_ALLOC (len + 1);
-      (void) memcpy (p, str, len+1);
-    }
-  else
-    {
-      safe_fatal(_("integer overflow in sh_util_strdup_l"), FIL__, __LINE__);
-    }
-  SL_RETURN( p, _("sh_util_strdup_l"));
-}
-
+    
 char * sh_util_strdup (const char * str) 
 {
@@ -379,31 +206,11 @@
   SL_ENTER(_("sh_util_strdup"));
 
-  SH_VALIDATE_NE(str, NULL);
-
-  if (str)
+  if (str != NULL)
     {
       len = sl_strlen(str);
       p   = SH_ALLOC (len + 1);
-      (void) memcpy (p, str, len+1);
+      (void) sl_strlcpy (p, str, len+1);
     }
   SL_RETURN( p, _("sh_util_strdup"));
-}
-
-char * sh_util_strdup_track (const char * str, char * file, int line) 
-{
-  char * p = NULL;
-  size_t len;
-
-  SL_ENTER(_("sh_util_strdup_track"));
-
-  SH_VALIDATE_NE(str, NULL);
-
-  if (str)
-    {
-      len = sl_strlen(str);
-      p   = SH_OALLOC (len + 1, file, line);
-      (void) memcpy (p, str, len+1);
-    }
-  SL_RETURN( p, _("sh_util_strdup_track"));
 }
 
@@ -413,24 +220,22 @@
 char * sh_util_strsep (char **str, const char *delim) 
 {
-  char *ret, *c;
-  const char *d;
+  char *ret, *c, *d;
 
   SL_ENTER(_("sh_util_strsep"));
   ret = *str;
 
-  SH_VALIDATE_NE(ret, NULL);
-
-  if (*str)
-    {
-      for (c = *str; *c != '\0'; c++) {
-	for (d = delim; *d != '\0'; d++) {
-	  if (*c == *d) {
-	    *c = '\0';
-	    *str = c + 1;
-	    SL_RETURN(ret, _("sh_util_strsep"));
-	  }
-	}
+  if (ret == NULL) {
+    SL_RETURN(ret, _("sh_util_strsep"));
+  }
+
+  for (c = *str; *c != '\0'; c++) {
+    for (d = (char *) delim; *d != '\0'; d++) {
+      if (*c == *d) {
+        *c = '\0';
+        *str = c + 1;
+        SL_RETURN(ret, _("sh_util_strsep"));
       }
     }
+  }
 
   /* If we get to here, there's no delimiters in the string */
@@ -440,5 +245,5 @@
 
 
-/* returned string must be free'd by caller.
+/* returned string must be free'd by caller
  */
 char * sh_util_formatted (const char * formatt, st_format * ftab)
@@ -455,11 +260,10 @@
   time_t inpp;
 
-  char * clist[16] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
-		       NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
+  char * clist[16];
   int    nn = 0;
 
   SL_ENTER(_("sh_util_formatted"));
 
-  if (formatt == NULL || ftab == NULL || *formatt == '\0')
+  if (formatt == NULL || ftab == NULL)
     SL_RETURN(NULL, _("sh_util_formatted"));
 
@@ -467,21 +271,25 @@
    */
   size = sl_strlen(formatt);
-
-  if (!sl_ok_adds(size, 1))
+  if (size > 0)
+    {
+      fmt = (char *) SH_ALLOC(size + 1);
+      (void) sl_strlcpy(fmt, formatt, size + 1);
+    }
+  else
     SL_RETURN(NULL, _("sh_util_formatted"));
 
-  ++size;
-  fmt = SH_ALLOC(size);
-  (void) sl_strlcpy(fmt, formatt, size);
-
   p = fmt;
 
   j = 0;
-  while (ftab[j].fchar != '\0') {
-    if (ftab[j].type != S_FMT_STRING)
-      ftab[j].data_str = NULL;
-    ++j;
-  }
- 
+  while (ftab[j].fchar != '\0')
+    {
+      if (ftab[j].type != S_FMT_STRING)
+	ftab[j].data_str = NULL;
+      ++j;
+    }
+
+  for (j = 0; j < 16; ++j)
+    clist[j] = NULL;
+
   while (p != NULL && *p != '\0' && NULL != (q = strchr(p, '%')))
     {
@@ -516,10 +324,8 @@
 	      i  = 1;
 	      
-	      switch(ftab[j].type) {
-
-	      case S_FMT_STRING:
+	      if (ftab[j].type == S_FMT_STRING)
 		{
 		  isiz = sl_strlen(ftab[j].data_str);
-		  if (isiz > 0 && sl_ok_adds(size, isiz))
+		  if (isiz > 0)
 		    {
 		      size += isiz;
@@ -529,9 +335,7 @@
 		  else
 		    *q = '%';
-		  goto endsrch;
+		  break;
 		}
-		break;
-
-	      case S_FMT_ULONG:
+	      else if (ftab[j].type == S_FMT_ULONG)
 		{
 		  ftab[j].data_str = (char *) SH_ALLOC(64);
@@ -541,5 +345,5 @@
 		  /*@+bufferoverflowhigh@*/
 		  isiz = sl_strlen(ftab[j].data_str);
-		  if (isiz > 0 && sl_ok_adds(size, isiz))
+		  if (isiz > 0)
 		    {
 		      size += isiz;
@@ -549,9 +353,7 @@
 		  else
 		    *q = '%';
-		  goto endsrch;
+		  break;
 		}
-		break;
-
-	      case S_FMT_LONG:
+	      else if (ftab[j].type == S_FMT_LONG)
 		{
 		  ftab[j].data_str = (char *) SH_ALLOC(64);
@@ -561,5 +363,5 @@
 		  /*@+bufferoverflowhigh@*/
 		  isiz = sl_strlen(ftab[j].data_str);
-		  if (isiz > 0 && sl_ok_adds(size, isiz))
+		  if (isiz > 0)
 		    {
 		      size += isiz;
@@ -569,9 +371,7 @@
 		  else
 		    *q = '%';
-		  goto endsrch;
+		  break;
 		}
-		break;
-
-	      case S_FMT_TIME:
+	      else if (ftab[j].type == S_FMT_TIME)
 		{
 		  ftab[j].data_str = (char *) SH_ALLOC(64);
@@ -593,5 +393,5 @@
 		    }
 		  isiz = sl_strlen(ftab[j].data_str);
-		  if (isiz > 0 && sl_ok_adds(size, isiz))
+		  if (isiz > 0)
 		    {
 		      size += isiz;
@@ -601,19 +401,11 @@
 		  else
 		    *q = '%';
-		  goto endsrch;
+		  break;
 		}
-		break;
-
-	      default:
-		/* do nothing */;
-	      }
 
 	    }
-	  ++j;
-	}
-
-    endsrch:
-
-      p = q;
+	  else
+	    ++j;
+	}
 
       /* -- not found -- */
@@ -621,7 +413,11 @@
 	{
 	  *q = '%';
+	  p = q;
 	  ++p;
 	}
-
+      else
+	{
+	  p = q;
+	}
     }
 
@@ -633,6 +429,5 @@
   /* -- closing '\0' --
    */
-  if (sl_ok_adds(size, 1))
-    size++;
+  size++;
   outstr = (char *) SH_ALLOC(size);
 
@@ -644,14 +439,14 @@
 		      clist[8],  clist[9], clist[10], clist[11], 
 		      clist[12], clist[13], clist[14], clist[15]); 
-  outstr[size-1] = '\0';
-
+  
   /* -- cleanup --
    */
   j = 0;
-  while (ftab[j].fchar != '\0') {
-    if (ftab[j].type != S_FMT_STRING && ftab[j].data_str != NULL)
-      SH_FREE(ftab[j].data_str);
-    ++j;
-  }
+  while (ftab[j].fchar != '\0')
+    {
+      if (ftab[j].type != S_FMT_STRING && ftab[j].data_str != NULL)
+	SH_FREE(ftab[j].data_str);
+      ++j;
+    }
   SH_FREE(fmt);
 
@@ -659,8 +454,7 @@
 }
 
-/* read a hexchar, return int value (0-15)
- * can't inline (AIX)
- */
-int sh_util_hexchar( const char c )
+/* can't inline (AIX)
+ */
+int sh_util_hexchar( char c )
 {
   /*@+charint@*/
@@ -675,51 +469,24 @@
 }
 
-char * sh_util_charhex( unsigned char i , char * i2h)
-{
-  int j, k;
-
-  j = i / 16;
-  k = i - (j*16);
-
-  if (j < 10) i2h[0] = '0'+j;
-  else        i2h[0] = 'A'+(j-10);
-  
-  if (k < 10) i2h[1] = '0'+k;
-  else        i2h[1] = 'A'+(k-10);
-
-  return i2h;
-}
-
 /* read a hexadecimal key, convert to binary
  */
-int sh_util_hextobinary (char * binary, const char * hex, int bytes)
+int sh_util_hextobinary (char * binary, char * hex, int bytes)
 {
   int i = 0, j, k, l = 0;
-  char c;
-
-#define SH_HEXCHAR(x, y) \
-    c = (x); \
-    if ( c >= '0' && c <= '9' ) \
-      y = c - '0'; \
-    else if ( c >= 'a' && c <= 'f' ) \
-      y = c - 'a' + 10; \
-    else if ( c >= 'A' && c <= 'F' ) \
-      y = c - 'A' + 10; \
-    else \
-      SL_RETURN((-1), _("sh_util_hextobinary"))
-
 
   SL_ENTER(_("sh_util_hextobinary"));
 
-  if (bytes < 2)
-    SL_RETURN((-1), _("sh_util_hextobinary"));
-
-  while (i < (bytes-1))
-    {
-      SH_HEXCHAR(hex[i],   k);
-      SH_HEXCHAR(hex[i+1], j);
-      
-      binary[l] = (char)(k * 16 + j);
-      ++l; i+= 2;
+  while (i < bytes)
+    {
+      k = sh_util_hexchar(hex[i]); j = sh_util_hexchar(hex[i+1]); 
+      if (k != -1 && j != -1) 
+        {
+          binary[l] = (char)(k * 16 + j);
+          ++l; i+= 2;
+        }
+      else
+        {
+	  SL_RETURN((-1), _("sh_util_hextobinary"));
+        }
     }
   
@@ -746,6 +513,5 @@
  */
 static char * sh_util_hmac_tiger (char * hexkey,  
-				  char * text, size_t textlen,
-				  char * res, size_t len)
+				  char * text, size_t textlen)
 {
   static char opad[KEY_BLOCK] = { 
@@ -761,102 +527,84 @@
     (char)0x36, (char)0x36, (char)0x36, (char)0x36, (char)0x36, (char)0x36
   };
-  static char  zap[KEY_BLOCK] = { 
-    (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00,  
-    (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00,  
-    (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00,  
-    (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00
-  };
   char        K[KEY_BLOCK];
   char        outer[KEY_BLOCK];
-  char      * inner = NULL;
+  char      * inner;
   UINT32    * h1;
   UINT32    * h2;
   UINT32      cc[KEY_LEN/4];
-  UINT32      kbuf[KEY_BYT/sizeof(UINT32)];
-  char hashbuf[KEYBUF_SIZE];
-
-  int         result;
+  char      * res;
+
   size_t      i;
 
   SL_ENTER(_("sh_util_hmac_tiger"));
-
-  ASSERT((KEY_BLOCK <= (KEY_LEN/2)), _("KEY_BLOCK <= (KEY_LEN/2)"));
-
-  memcpy (K, zap, KEY_BLOCK);
-
-  result = sh_util_hextobinary (K, hexkey, KEY_LEN);
-
-  ASSERT((result >= 0), _("result >= 0"));
-
-  if ((result >= 0) && sl_ok_adds(textlen, KEY_BLOCK))
-    {
-      inner = (char *) SH_ALLOC (textlen + KEY_BLOCK); 
-
-      for (i = 0; i < KEY_BLOCK; ++i)
-	{
-	  outer[i]  = K[i] ^ opad[i];
-	  inner[i]  = K[i] ^ ipad[i];
-	}
-      for (i = KEY_BLOCK; i < (KEY_BLOCK+textlen); ++i)
-	{
-	  inner[i] = text[i - KEY_BLOCK];
-	}
-    }
-  else
-    {
-      sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
-		      _("integer overflow"), 
-		      _("sh_util_hmac_tiger"));
-      (void) sh_tiger_hash (NULL, TIGER_DATA, 0, hashbuf, sizeof(hashbuf));
-      sl_strlcpy(res, hashbuf, len);
+  ASSERT((KEY_BLOCK <= (KEY_LEN/2)), _("KEY_BLOCK <= (KEY_LEN/2)"))
+
+  if (KEY_BLOCK > (KEY_LEN/2))
+    {
+      res = sh_tiger_hash (NULL, TIGER_DATA, 0);
       SL_RETURN(res, _("sh_util_hmac_tiger"));
+    }
+
+  memset (K, 0x00, KEY_BLOCK);
+
+  if (sh_util_hextobinary (K, hexkey, KEY_LEN) < 0)
+    {
+      res = sh_tiger_hash (NULL, TIGER_DATA, 0);
+      SL_RETURN(res, _("sh_util_hmac_tiger"));
+    }
+
+  inner = (char *) SH_ALLOC (textlen + KEY_BLOCK); 
+
+  for (i = 0; i < KEY_BLOCK; ++i)
+    {
+      outer[i]  = K[i] ^ opad[i];
+      inner[i]  = K[i] ^ ipad[i];
+    }
+  for (i = KEY_BLOCK; i < (KEY_BLOCK+textlen); ++i)
+    {
+      inner[i] = text[i - KEY_BLOCK];
     }
 
   /* now compute the hash 
    */
-  h1 = sh_tiger_hash_uint32 ( outer, TIGER_DATA, KEY_BLOCK,
-			      kbuf, KEY_BYT/sizeof(UINT32));
+  h1 = sh_tiger_hash_uint32 ( outer,
+			      TIGER_DATA,
+			      KEY_BLOCK);
   for (i = 0; i < (KEY_LEN/8); ++i)
-    copy_four ( (unsigned char *) &(cc[i]), h1[i]);
-
-  /* cppcheck-suppress uninitvar */
-  h2 = sh_tiger_hash_uint32 ( inner, TIGER_DATA,
-			      (unsigned long) KEY_BLOCK+textlen,
-			      kbuf, KEY_BYT/sizeof(UINT32));
+    {
+      /* cc[i] = h1[i]; */
+      copy_four ( (unsigned char *) &(cc[i]), h1[i]);
+    }
+
+  h2 = sh_tiger_hash_uint32 ( inner,
+			      TIGER_DATA,
+			      (unsigned long) KEY_BLOCK+textlen);
   for (i = KEY_LEN/8; i < (KEY_LEN/4); ++i)
-    copy_four ( (unsigned char *) &(cc[i]), h2[i - (KEY_LEN/8)]);
-
-  /* cppcheck-suppress uninitvar */
+    {
+      copy_four ( (unsigned char *) &(cc[i]), h2[i - (KEY_LEN/8)]);
+      /* cc[i] = h2[i - (KEY_LEN/8)]; */
+    }
   SH_FREE(inner);
   
-  (void) sh_tiger_hash ((char *) &cc[0],
-			TIGER_DATA,
-			(unsigned long) (KEY_LEN/4 * sizeof(UINT32)),
-			hashbuf, sizeof(hashbuf));
-
-  sl_strlcpy(res, hashbuf, len);
+  res = sh_tiger_hash ((char *) &cc[0],
+		       TIGER_DATA,
+		       (unsigned long) (KEY_LEN/4 * sizeof(UINT32)));
+
   SL_RETURN(res, _("sh_util_hmac_tiger"));
 }
 
 static char * sh_util_hash_tiger ( char * hexkey,  
-				   char * text, size_t textlen,
-				   char * res, size_t len)
-{
+				   char * text, size_t textlen)
+{
+  char         * res;
   char           h2[2*KEY_LEN+1];
-  char hashbuf[KEYBUF_SIZE];
-
   SL_ENTER(_("sh_util_hash_tiger"));
 
   (void) sl_strlcpy(h2, hexkey, KEY_LEN+1); 
-  (void) sl_strlcat(h2, 
-		    sh_tiger_hash(text, TIGER_DATA, 
-				  (unsigned long) textlen,
-				  hashbuf, sizeof(hashbuf)), 
-		    2*KEY_LEN+1
-		    );
-
-  (void) sh_tiger_hash(h2, TIGER_DATA, 2*KEY_LEN, hashbuf, sizeof(hashbuf));
-
-  sl_strlcpy(res, hashbuf, len);
+  (void) sl_strlcat(h2, sh_tiger_hash(text, TIGER_DATA, 
+				      (unsigned long) textlen), 2*KEY_LEN+1);
+
+  res = sh_tiger_hash(h2, TIGER_DATA, 2*KEY_LEN);
+
   SL_RETURN(res, _("sh_util_hash_tiger"));
 }
@@ -869,5 +617,5 @@
 static int sigtype = TYPE_HMAC;
 
-int sh_util_sigtype (const char * c)
+int sh_util_sigtype (char * c)
 {
   SL_ENTER(_("sh_util_sigtype"));
@@ -886,6 +634,5 @@
 
 char * sh_util_siggen (char * hexkey,  
-		       char * text, size_t textlen,
-		       char * res, size_t len)  
+		       char * text, size_t textlen)  
 {
   char * p;
@@ -894,15 +641,16 @@
   if (sigtype == TYPE_HMAC)
     p = sh_util_hmac_tiger (hexkey,  
-			    text, textlen, res, len);
+			    text, textlen);
   else
     p = sh_util_hash_tiger (hexkey,  
-			    text, textlen, res, len);
+			    text, textlen);
   SL_RETURN(p, _("sh_util_siggen"));
 }    
 
+  
  
 /* a simple compressor
  */
-size_t sh_util_compress (char * dest, char * src, size_t dest_size)
+long sh_util_compress (char * dest, char * src, size_t dest_size)
 {
   char * add;
@@ -945,5 +693,5 @@
 
   dest[dest_size-1] = '\0'; /* paranoia       */
-  SL_RETURN((count), _("sh_util_compress")); /* no of chars copied */    
+  SL_RETURN(((long)count), _("sh_util_compress")); /* no of chars copied */    
 }
 
@@ -959,8 +707,4 @@
     char c[sizeof(long)];
   } u;
-#ifdef WORDS_BIGENDIAN
-  unsigned char swap;
-  unsigned char * ii = (unsigned char *) dest;
-#endif
 
   SL_ENTER(_("sh_util_cpylong"));    
@@ -985,8 +729,4 @@
       ++i;
     }
-#ifdef WORDS_BIGENDIAN
-  swap = ii[0]; ii[0] = ii[3]; ii[3] = swap;
-  swap = ii[1]; ii[1] = ii[2]; ii[2] = swap;
-#endif
   SL_RET0(_("sh_util_cpylong"));
 }
@@ -1012,5 +752,5 @@
 
 
-/* interval [0, 4294967295]
+/* interval [0, 4294967296]
  */
 static UINT32 taus_get_long (void *vstate)
@@ -1018,8 +758,6 @@
   UINT32 * state = (UINT32 *) vstate;
 
-  /*
   if (skey->rngI == BAD)
     (void)taus_seed();
-  */
 
 #define TAUSWORTHE(s,a,b,c,d) ((s &c) <<d) ^ (((s <<a) ^s) >>b)
@@ -1035,53 +773,28 @@
  * input for a one-way hash function.
  */
-
-UINT32 taus_get ()
-{
-#define TAUS_SAMPLE 12
-
-  UINT32   taus_svec[TAUS_SAMPLE];
+UINT32 taus_get (void *state1, void *state2, void *state3)
+{
+  UINT32   svec[6];
   UINT32   retval;
   UINT32 * res;
-  UINT32 * res_vec = &(skey->res_vec[0]);
-  static   int      res_num = 0;
   register int i;
-  UINT32       kbuf[KEY_BYT/sizeof(UINT32)];
-
-  SH_MUTEX_LOCK_UNSAFE(mutex_skey);
-  if (res_num > 0)
-    {
-      retval  = res_vec[res_num];
-      res_num = (res_num == 5) ? 0 : (res_num + 1);
-      SH_MUTEX_UNLOCK_UNSAFE(mutex_skey); /* alternative path */
-      return  retval;
-    }
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
-
-  (void)taus_seed();
-
-  SH_MUTEX_LOCK_UNSAFE(mutex_skey);
-  for (i = 0; i < (TAUS_SAMPLE/3); ++i)
-    {
-      taus_svec[i*3]   = taus_get_long (&(skey->rng0[0]));
-      taus_svec[i*3+1] = taus_get_long (&(skey->rng1[0]));
-      taus_svec[i*3+2] = taus_get_long (&(skey->rng2[0]));
-    }
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
-
-  res     = sh_tiger_hash_uint32 ( (char *) &taus_svec[0], 
+
+  svec[0] = taus_get_long (state1);
+  svec[1] = taus_get_long (state2);
+  svec[2] = taus_get_long (state3);
+  svec[3] = taus_get_long (state1);
+  svec[4] = taus_get_long (state2);
+  svec[5] = taus_get_long (state3);
+
+  res     = sh_tiger_hash_uint32 ( (char *) &svec[0], 
 				   TIGER_DATA, 
-				   (unsigned long)(TAUS_SAMPLE * sizeof(UINT32)),
-				   kbuf, KEY_BYT/sizeof(UINT32));
-
-  SH_MUTEX_LOCK_UNSAFE(mutex_skey);
-  for (i = 1; i < 6; ++i)
-    { 
-      res_vec[i] = res[i];
-    }
-  retval  = res[0];
-  res_num = 1;
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
-
-  memset(taus_svec, 0, TAUS_SAMPLE * sizeof(UINT32));
+				   (unsigned long)(6 * sizeof(UINT32)));
+
+  for (i = 1; i < KEY_BYT/4; ++i)
+    res[0] ^= res[i];
+  retval = res[0];
+
+  memset (res,  0,            KEY_BYT);
+  memset (svec, 0, 6 * sizeof(UINT32));
 
   return retval;
@@ -1139,7 +852,5 @@
   char                 bufx[9 * sizeof(UINT32) + 1];
   int                  status;
-  static unsigned long seed_time    = 0;
-  static unsigned long seed_counter = 3000;
-  unsigned long        gtime;
+  static unsigned long seed_time = 0;
 
   SL_ENTER(_("taus_seed"));
@@ -1147,13 +858,9 @@
   if (skey->rngI == GOOD)
     {
-      ++seed_counter;
-
-      if ( ((sh_unix_longtime () - seed_time) < 1800) &&
-	   ( seed_counter                     < 3000))
+      if ( (sh_unix_longtime () - seed_time) < 3600)
 	SL_RETURN( (0), _("taus_seed"));
     }
   
-  seed_time    = sh_unix_longtime ();
-  seed_counter = 0;
+  seed_time = sh_unix_longtime ();
 
   status = sh_entropy (24, bufx);
@@ -1161,5 +868,5 @@
   if (!SL_ISERROR(status))
     {
-      SH_MUTEX_LOCK_UNSAFE(mutex_skey);
+      skey->rngI = GOOD;
       memcpy (&skey->rng0[0], &bufx[0],                  2*sizeof(UINT32));
       memcpy (&skey->rng1[0], &bufx[2*sizeof(UINT32)],   2*sizeof(UINT32));
@@ -1175,6 +882,4 @@
       taus_set_from_state( &(skey->rng2[0]), &(skey->rng2[0]));
 
-      skey->rngI = GOOD;
-      SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
       SL_RETURN( (0), _("taus_seed"));
     }
@@ -1185,16 +890,13 @@
   /* emergency backup - unsafe !
    */
+  skey->rngI = GOOD;
 #ifdef HAVE_GETTIMEOFDAY
-  gtime = sh_unix_notime();
+  taus_set_from_ulong ( &(skey->rng0[0]), LCG (sh_unix_notime())      );
 #else
-  gtime = seed_time;
-#endif
-
-  SH_MUTEX_LOCK_UNSAFE(mutex_skey);
-  taus_set_from_ulong ( &(skey->rng0[0]), LCG (gtime)          );
+  taus_set_from_ulong ( &(skey->rng0[0]), LCG (seed_time)      );
+#endif
   taus_set_from_ulong ( &(skey->rng1[0]), LCG (skey->rng0[0])  );
   taus_set_from_ulong ( &(skey->rng2[0]), LCG (skey->rng1[0])  );
   skey->rngI = BAD;
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
 
   SL_RETURN( (-1), _("taus_seed"));
@@ -1206,5 +908,5 @@
 static void copy_four (unsigned char * dest, UINT32 in);
 
-int sh_util_set_newkey (const char * new_in)
+int sh_util_set_newkey (char * new)
 {
   size_t i, j = 0;
@@ -1213,6 +915,6 @@
   SL_TICKET fout;
   char * key;
-  char * path = NULL;
-  char * outpath = NULL;
+  char * path;
+  char * outpath;
   unsigned char * image = NULL;
   long s = 0;
@@ -1220,9 +922,8 @@
   long ii, k = 0;
   UINT32    * h1;
-  char * new = NULL;
 
   if (0 != sl_is_suid())
     {
-      fprintf(stderr, "%s", _("ERROR: insufficient privilege\n"));
+      fprintf(stderr, _("ERROR: insufficient privilege\n"));
       _exit (EXIT_FAILURE);
       /*@notreached@*/
@@ -1230,7 +931,7 @@
     }
         
-  if (new_in == NULL || new_in[0] == '\0')
-    {
-      fprintf(stderr, "%s", 
+  if (new == NULL || new[0] == '\0')
+    {
+      fprintf(stderr, 
 	      _("ERROR: no key given\n Argument must be 'key@path'\n"));
       _exit (EXIT_FAILURE);
@@ -1238,10 +939,4 @@
       return -1;
     }
-
-  len = strlen(new_in) + 1;
-  if (NULL == (new = calloc(1, len)))
-    goto bail_mem;
-  memcpy(new, new_in, len);
-
   key = new;
   len = strlen(new);
@@ -1255,7 +950,6 @@
   if (j == 0)
     {
-      fprintf(stderr, "%s",
+      fprintf(stderr, 
 	      _("ERROR: no path to executable given\n Argument must be 'key@path'\n"));
-      free(new);
       _exit (EXIT_FAILURE);
       /*@notreached@*/
@@ -1264,18 +958,17 @@
   else
     path = &new[j];
-
-  len = strlen(path) + 1 + 4;
   /*@-usedef@*/
-  if (NULL == (outpath = calloc(1,len)))
+  if (NULL == (outpath = malloc(strlen(path) + 1 + 4)))
     goto bail_mem;
   /*@-usedef@*/
-  sl_snprintf (outpath, len, _("%s.out"), path);
-
-  fp = sl_open_read(FIL__, __LINE__, path, SL_NOPRIV);
+  /*@-bufferoverflowhigh@*/
+  sprintf (outpath, _("%s.out"), path);               /* known to fit  */ 
+  /*@+bufferoverflowhigh@*/
+
+  fp = sl_open_read(path, SL_NOPRIV);
   if (SL_ISERROR(fp))
     {
       fprintf(stderr, 
 	      _("ERROR: cannot open %s for read (errnum = %ld)\n"), path, fp);
-      free(new); free (outpath);
       _exit (EXIT_FAILURE);
       /*@notreached@*/
@@ -1283,10 +976,9 @@
     }
   
-  fout = sl_open_write(FIL__, __LINE__, outpath, SL_NOPRIV);
+  fout = sl_open_write(outpath, SL_NOPRIV);
   if (SL_ISERROR(fout))
     {
       fprintf(stderr, 
 	      _("ERROR: cannot open %s (errnum = %ld)\n"), outpath, fout);
-      free(new); free (outpath);
       _exit (EXIT_FAILURE);
       /*@notreached@*/
@@ -1295,17 +987,12 @@
 
 
-  image = calloc(1,4096);
+  image = malloc (4096);
   if (!image)
     goto bail_mem;
   while (0 < (ii = sl_read (fp, &image[s], 4096)))
     {
-      unsigned char * ptr;
       ilen += ii;
       s    += 4096;
-      ptr = realloc (image, (size_t) (4096 + s));
-      if (ptr)
-	image = ptr;
-      else
-	{ free(image); image = NULL; }
+      image = realloc (image, (size_t) (4096 + s));
       if (!image)
 	goto bail_mem;
@@ -1326,10 +1013,7 @@
 	  image[k+7] == new_key[7])
 	{
-	  UINT32 kbuf[KEY_BYT/sizeof(UINT32)];
-
-	  printf("%s", _("old key found\n")); 
+	  printf(_("old key found\n")); 
 	  h1 = sh_tiger_hash_uint32 (key, TIGER_DATA, 
-				     (unsigned long)strlen(key),
-				     kbuf, KEY_BYT/sizeof(UINT32));
+				     (unsigned long)strlen(key));
 	  copy_four( (unsigned char *) &(image[k]),   h1[0]);
 	  copy_four( (unsigned char *) &(image[k+4]), h1[1]);
@@ -1337,5 +1021,4 @@
 	  (void) sl_close (fout);
 	  printf(_("new file %s written\n"), outpath);
-	  free(new); free (outpath); free(image);
 	  _exit (EXIT_SUCCESS);
 	  /*@notreached@*/
@@ -1344,7 +1027,6 @@
     }
 
-  fprintf(stderr, "%s",
+  fprintf(stderr, 
 	  _("ERROR: old key not found\n"));
-  free(new); free (outpath); free(image);
   _exit (EXIT_FAILURE);
   /*@notreached@*/
@@ -1353,9 +1035,6 @@
 
  bail_mem:
-  fprintf(stderr, "%s",
+  fprintf(stderr, 
 	  _("ERROR: out of memory\n"));
-  if (new) free(new); 
-  if (outpath) free (outpath);
-  if (image) free (image);
   _exit (EXIT_FAILURE);
   /*@notreached@*/
@@ -1378,5 +1057,4 @@
   register int    i, j, j1 = 0, j2 = 0, j3;
   char          * dez; 
-  char hashbuf[KEYBUF_SIZE];
 
   SL_ENTER(_("sh_util_encode"));
@@ -1398,16 +1076,13 @@
 
   (void) sl_strlcpy(skey->vernam, 
-		    sh_tiger_hash(skey->vernam, TIGER_DATA, KEY_LEN,
-				  hashbuf, sizeof(hashbuf)), 
+		    sh_tiger_hash(skey->vernam, TIGER_DATA, KEY_LEN), 
 		    KEY_LEN+1);
 
   (void) sl_strlcpy(skey->vernam, 
-		    sh_util_hmac_tiger (skey->vernam, salt, strlen(salt),
-					hashbuf, sizeof(hashbuf)),
-		    KEY_LEN+1);
+		    sh_util_hmac_tiger (skey->vernam, salt, strlen(salt)),
+	     KEY_LEN+1);
 
   (void) sl_strlcpy(skey->vernam, 
-		    sh_util_hmac_tiger (skey->vernam, (char*) new_key, 8,
-					hashbuf, sizeof(hashbuf)),
+		    sh_util_hmac_tiger (skey->vernam, (char*) new_key, 8),
 		    KEY_LEN+1);
 
@@ -1441,15 +1116,17 @@
 /* server mode 
  */
-int sh_util_setserver (const char * dummy)
+int sh_util_setserver (char * dummy)
 {
   SL_ENTER(_("sh_util_setserver"));
 
-  (void) dummy;
-  sh.flag.isserver = GOOD;
+  if (dummy)
+    sh.flag.isserver = GOOD;
+  else
+    sh.flag.isserver = GOOD;
   SL_RETURN((0),_("sh_util_setserver"));
 }
 
 
-int sh_util_setlooptime (const char * str)
+int sh_util_setlooptime (char * str)
 {
   int i = atoi (str);
@@ -1468,5 +1145,5 @@
 
 #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
-int  sh_util_setchecksum (const char * str)
+int  sh_util_setchecksum (char * str)
 {
   static int reject = 0;
@@ -1566,5 +1243,4 @@
   int          status = 0;
   char       * p;
-  char hashbuf[KEYBUF_SIZE];
 
   SL_ENTER(_("sh_util_keyinit"));
@@ -1584,9 +1260,9 @@
 
   for (i = 0; i < 6; ++i)
-    bufy[i] = taus_get();
+    bufy[i] = taus_get(&(skey->rng0[0]), &(skey->rng1[0]), &(skey->rng2[0]));
 
   p = sh_tiger_hash ((char *) bufy, TIGER_DATA, 
-		     (unsigned long)(6*sizeof(UINT32)),
-		     hashbuf, sizeof(hashbuf));
+		     (unsigned long)(6*sizeof(UINT32)));
+  p[size-1] = '\0';
 
   i = sl_strlcpy(buf, p, (size_t)size);
@@ -1607,160 +1283,11 @@
 
 static unsigned char sh_obscure_index[256];
-static int sh_obscure_no_check = S_FALSE;
-
-int sh_util_valid_utf8 (const unsigned char * str) 
-{
-  const int     sh_val_utf8_1 = 1;
-  const int     sh_val_utf8_2 = 2;
-  const int     sh_val_utf8_3 = 3;
-  const int     sh_val_utf8_4 = 4;
-
-  size_t        len = strlen((const char *)str);
-  size_t        l   = 0;
-  int           typ = 0;
-  unsigned char c     = '\0';
-  unsigned char c2[2] = { 0x00, 0x00 };
-  unsigned char c3[3] = { 0x00, 0x00, 0x00 };
-
-
-#define SH_VAL_UTF8_1 ((c != '\0') && ((c & 0x80) == 0x00))
-#define SH_VAL_UTF8_2 ((c != '\0') && ((c & 0xE0) == 0xC0)) /* 110x xxxx */
-#define SH_VAL_UTF8_3 ((c != '\0') && ((c & 0xF0) == 0xE0)) /* 1110 xxxx */
-#define SH_VAL_UTF8_4 ((c != '\0') && ((c & 0xF8) == 0xF0)) /* 1111 0xxx */
-#define SH_VAL_UTF8_N ((c != '\0') && ((c & 0xC0) == 0x80)) /* 10xx xxxx */
-#define SH_VAL_BAD    ((c == '"')  || (c == '\t') || (c == '\b') || \
-                       (c == '\f') || (c == '\n') || \
-                       (c == '\r') || (c == '\v') || iscntrl((int) c) || \
-                       (c != ' ' && !isgraph ((int) c)))
-   
-  while(l < len) 
-    {
-      c = str[l];
-
-      if      (SH_VAL_UTF8_1) 
-	{
-	  if (!(SH_VAL_BAD && (sh_obscure_index[c] != 1)))
-	    {
-	      typ = sh_val_utf8_1;
-	      ++l; continue;
-	    }
-	  else
-	    {
-	      return S_FALSE;
-	    }
-	} 
-      else if (SH_VAL_UTF8_2) 
-	{ 
-	  typ = sh_val_utf8_2;
-	  c2[0] = c;
-	  if ((c & 0x3e) != 0x00) /* !(overlong 2-byte seq.) */
-	    {
-	      ++l; 
-	      if (l != len) {
-		c = str[l];
-		if(SH_VAL_UTF8_N) {
-		  c2[1] = c;
-		  ++l; continue;
-		} 
-		else {
-		  return S_FALSE;
-		} 
-	      } 
-	      else {
-		return S_FALSE; 
-	      }
-	    }
-	  else
-	    {
-	      return S_FALSE; /* overlong 2-byte seq. */
-	    }
-	} 
-      else if (SH_VAL_UTF8_3) 
-	{
-	  typ = sh_val_utf8_3;
-	  c3[0] = c;
-	  ++l; if (l == len) return S_FALSE; c = str[l];
-	  if(!SH_VAL_UTF8_N) return S_FALSE;
-	  if (((str[l-1] & 0x1F) == 0x00) && ((c & 0x60) == 0x00))
-	    return S_FALSE; /* overlong 3-byte seq. */
-	  c3[1] = c;
-	  ++l; if (l == len) return S_FALSE; c = str[l];
-	  if(!SH_VAL_UTF8_N) return S_FALSE;
-	  c3[2] = c;
-	  ++l; continue;
-	} 
-      else if (SH_VAL_UTF8_4) 
-	{
-	  typ = sh_val_utf8_4;
-	  ++l; if (l == len) return S_FALSE; c = str[l];
-	  if(!SH_VAL_UTF8_N) return S_FALSE;
-	  if (((str[l-1] & 0x0F) == 0x00) && ((c & 0x70) == 0x00))
-	    return S_FALSE; /* overlong 4-byte seq. */
-	  ++l; if (l == len) return S_FALSE; c = str[l];
-	  if(!SH_VAL_UTF8_N) return S_FALSE;
-	  ++l; if (l == len) return S_FALSE; c = str[l];
-	  if(!SH_VAL_UTF8_N) return S_FALSE;
-	  ++l; continue;
-	}
-      return S_FALSE;
-    }
-
-  /* last character is invisible (space or else)
-   */
-  if (typ == sh_val_utf8_1)
-    { 
-      if (c != ' ')
-	return S_TRUE;
-      else
-	return S_FALSE;
-    }
-  else if (typ == sh_val_utf8_2)
-    {
-      if (c2[0] == 0xC2 && c2[1] == 0xA0) /* nbsp */
-	return S_FALSE;
-      else
-	return S_TRUE;
-    }
-  else if (typ == sh_val_utf8_3)
-    {
-      if (c3[0] == 0xE2) 
-	{
-	  if (c3[1] == 0x80 && c3[2] >= 0x80 && c3[2] <= 0x8F)
-	    return S_FALSE; /* various spaces, left-to-right, right-to-left */
-	  else if (c3[1] == 0x80 && (c3[2] == 0xA8 || c3[2] == 0xA9 || 
-				     c3[2] == 0xAD || c3[2] == 0xAF))
-	    return S_FALSE; /* line sep, para sep, zw word joiner, nnbsp */
-	  else if (c3[1] == 0x81 && (c3[2] == 0xA0 || c3[2] == 0xA1 || 
-				     c3[2] == 0x9F))
-	    return S_FALSE; /* word joiner, function app, math space */
-	  else
-	    return S_TRUE;
-	}
-      else if (c3[0] == 0xE3 && c3[1] == 0x80 && c3[2] == 0x80)
-	{
-	  return S_FALSE; /* ideographic space */
-	}
-      else if (c3[0] == 0xEF && c3[1] == 0xBB && c3[2] == 0xBF)
-	{
-	  return S_FALSE; /* zwnbsp */
-	}
-      else
-	{
-	  return S_TRUE;
-	}
-    }
-  else
-    {
-      return S_TRUE;
-    }
-}
-
-
-int sh_util_obscure_ok (const char * str)
+
+int sh_util_obscure_ok (char * str)
 {
   unsigned long   i;
-  char * endptr = NULL;
-
-  SL_ENTER(_("sh_util_obscure_ok"));
+  char * endptr = str;
+
+  SL_ENTER(_("sh_util_obscure_ex"));
 
   if (0 == sl_strncmp("all", str, 3))
@@ -1770,9 +1297,6 @@
 	  sh_obscure_index[i] = (unsigned char)1;
 	}
-      sh_obscure_no_check = S_TRUE;
-      SL_RETURN(0, _("sh_util_obscure_ok"));
-    }
-
-  sh_obscure_no_check = S_FALSE;
+      SL_RETURN(0, _("sh_util_obscure_ex"));
+    }
 
   for (i = 0; i < 255; ++i)
@@ -1780,13 +1304,4 @@
       sh_obscure_index[i] = (unsigned char)0;
     }
-
-  i = strtoul (str, &endptr, 0);
-  if (i > 255)
-    {
-      SL_RETURN(-1, _("sh_util_obscure_ok"));
-    }
-  sh_obscure_index[i] = (unsigned char)1;
-  if (*endptr == ',')
-    ++endptr;
 
   while (*endptr != '\0')
@@ -1795,5 +1310,5 @@
       if (i > 255)
 	{
-	  SL_RETURN(-1, _("sh_util_obscure_ok"));
+	  SL_RETURN(-1, _("sh_util_obscure_ex"));
 	}
       sh_obscure_index[i] = (unsigned char)1;
@@ -1801,26 +1316,12 @@
 	++endptr;
     }
-  SL_RETURN(0, _("sh_util_obscure_ok"));
-}
-
-static int sh_obscure_check_utf8 = S_FALSE;
-
-int sh_util_obscure_utf8 (const char * c)
-{
-  int i;
-  SL_ENTER(_("sh_util_obscure_utf8"));
-  i = sh_util_flagval(c, &(sh_obscure_check_utf8));
-  if (sh_obscure_check_utf8 == S_TRUE)
-    sh_obscure_no_check = S_FALSE;
-  SL_RETURN(i, _("sh_util_obscure_utf8"));
-}
-
-
-int sh_util_obscurename (ShErrLevel level, const char * name_orig, int flag)
-{
-  const unsigned char * name = (const unsigned char *) name_orig;
+  SL_RETURN(0, _("sh_util_obscure_ex"));
+}
+
+int sh_util_obscurename (ShErrLevel level, char * name_orig, int flag)
+{
+  char * name = name_orig;
   char * safe;
   unsigned int i;
-  size_t len = 0;
 
   SL_ENTER(_("sh_util_obscurename"));
@@ -1828,184 +1329,93 @@
   ASSERT_RET((name != NULL), _("name != NULL"), (0))
 
-  if (sh_obscure_no_check == S_FALSE)
-    {
-      if (sh_obscure_check_utf8 != S_TRUE)
-	{
-	  /* -- Check name. --
-	   */
-	  while (*name != '\0') 
+  /* -- Check name. --
+   */
+  while (*name != '\0') 
+    {
+      if ( (*name) == '"'  || (*name) == '\t' ||
+	   (*name) == '\b' || (*name) == '\f' || 
+	   (*name) == '\n' || (*name) == '\r' ||
+	   (*name) == '\v' || iscntrl((int) *name) || 
+	   ((*name) != ' ' && !isgraph ((int) *name)) ) 
+	{
+	  i = (unsigned char) *name;
+	  if (sh_obscure_index[i] != (unsigned char)1)
 	    {
-	      if ( (*name) >  0x7F || (*name) == '"'  || (*name) == '\t' ||
-		   (*name) == '\b' || (*name) == '\f' || 
-		   (*name) == '\n' || (*name) == '\r' ||
-		   (*name) == '\v' || iscntrl((int) *name) || 
-		   ((*name) != ' ' && !isgraph ((int) *name)) ) 
+	      if (flag == S_TRUE)
 		{
-		  i = (unsigned char) *name;
-		  if (sh_obscure_index[i] != (unsigned char)1)
-		    {
-		      goto err;
-		    }
+		  safe = sh_util_safe_name (name_orig);  
+		  sh_error_handle (level, FIL__, __LINE__, 0, MSG_FI_OBSC, 
+				   safe);
+		  SH_FREE(safe);
 		}
-	      name++; ++len;
+	      SL_RETURN((-1),_("sh_util_obscurename"));
 	    }
-
-	  /* Check for blank at end of name
-	   */
-	  if ((len > 0) && (name_orig[len-1] == ' '))
-	    {
-	      goto err;
-	    }
-	}
-      else
-	{
-	  if (S_FALSE == sh_util_valid_utf8(name))
-	    {
-	      goto err;
-	    }
-	  SL_RETURN((0),_("sh_util_obscurename"));
-	}
-    }
-      
+	}
+      name++;
+    }
+
   SL_RETURN((0),_("sh_util_obscurename"));
-
- err:
-  
-  if (flag == S_TRUE)
-    {
-      safe = sh_util_safe_name (name_orig);  
-      sh_error_handle (level, FIL__, __LINE__, 0, MSG_FI_OBSC, 
-		       safe);
-      SH_FREE(safe);
-    }
-  SL_RETURN((-1),_("sh_util_obscurename"));
-}
-
+}
 #endif
 
 /* returns freshly allocated memory, return value should be free'd
  */
-char * sh_util_dirname(const char * fullpath)
+char * sh_util_basename(char * fullpath)
 {
   char * retval;
-  size_t len;
-  char * tmp;
-
-  SL_ENTER(_("sh_util_dirname"));
+  size_t i;
+
+  SL_ENTER(_("sh_util_basename"));
 
   ASSERT_RET ((fullpath != NULL), _("fullpath != NULL"), (NULL))
-  ASSERT_RET ((*fullpath == '/'), _("*fullpath == '/'"), (NULL))
-
-  retval = sh_util_strdup(fullpath);
-
-  tmp    = retval;
-  while (*tmp == '/') ++tmp;
-
-  /* (1) only leading slashes -- return exact copy 
+
+  i = sl_strlen (fullpath);  /* fullpath[i] is terminating '\0' */
+
+  while (i > 0) {
+    --i;
+    if (fullpath[i] == '/') break;
+  }
+
+  /* -- Not a valid path. --
    */
-  if (*tmp == '\0')
-    {
-      SL_RETURN(retval, _("sh_util_dirname"));
-    }
-
-  /* (2) there are non-slash characters, so delete trailing slashes
-   */
-  len    = sl_strlen (retval);     /* retval[len] is terminating '\0' */
-
-  while (len > 1 && retval[len-1] == '/')    /* delete trailing slash */
-    {
-      retval[len-1] = '\0';
-      --len;
-    }
-
-  /* (3) now delete all non-slash characters up to the preceding slash
-   */
-  while (len > 1 && retval[len-1] != '/') {
-    retval[len-1] = '\0';
-    --len;
-  }
-
-  /* (4a) only leading slashes left, so return this
-   */
-  if (&(retval[len]) == tmp)
-    {
-      SL_RETURN(retval, _("sh_util_dirname"));
-    }
-
-  /* (4b) strip trailing slash(es) of parent directory
-   */
-  while (len > 1 && retval[len-1] == '/') {
-    retval[len-1] = '\0';
-    --len;
-  }
-  SL_RETURN(retval, _("sh_util_dirname"));
-
+  if ((fullpath[i] != '/') && (i == 0) ) 
+    SL_RETURN(NULL, _("sh_util_basename"));
+
+  retval = SH_ALLOC(i + 1);
+
+  (void) sl_strlcpy (retval, fullpath, i+1);
+
+  SL_RETURN(retval, _("sh_util_basename"));
 }
 
 /* returns freshly allocated memory, return value should be free'd
  */
-char * sh_util_basename(const char * fullpath)
-{
-  char       * retval = NULL;
-  const char * tmp;
-  char       * tmp2;
-  char       * c;
-  size_t       len;
-
-  SL_ENTER(_("sh_util_basename"));
+char * sh_util_filename(char * fullpath)
+{
+  char * retval;
+  char * c;
+  size_t i;
+
+  SL_ENTER(_("sh_util_filename"));
 
   ASSERT_RET ((fullpath != NULL), _("fullpath != NULL"), (NULL))
 
-  tmp = fullpath; while (*tmp == '/') ++tmp;
-  if (*tmp == '\0')
-    {
-      retval = sh_util_strdup(fullpath);
-    }
-  else
-    {
-      tmp2 = sh_util_strdup(tmp);
-      len  = sl_strlen (tmp2);
-
-      while (len > 1 && tmp2[len-1] == '/')
-	{
-	  tmp2[len-1] = '\0';
-	  --len;
-	}
-
-      if (tmp2) /* for llvm/clang analyzer */
-	{
-	  c = strrchr(tmp2, '/');
-	  if (c)
-	    {
-	      retval = sh_util_strdup(++c);
-	      SH_FREE(tmp2);
-	    }
-	  else
-	    {
-	      retval = tmp2;
-	    }
-	}
-    }
-
-  SL_RETURN(retval, _("sh_util_basename"));
-}
-
-#define SH_ESCAPE_SPACE      1
-#define SH_DONT_ESCAPE_SPACE 0    
-char * sh_util_safe_name_int (const char * name, int escape_space);
-
+  c = strrchr(fullpath, '/');
+  i = sl_strlen (c);
+  if (i <= 1) SL_RETURN(NULL, _("sh_util_filename")); /* ends in '/' */
+  ++c;
+  --i;
+
+  retval = SH_ALLOC(i + 1);
+
+  (void) sl_strlcpy (retval, c, i+1);
+
+  SL_RETURN(retval, _("sh_util_filename"));
+}
+
+    
+/* returns freshly allocated memory, return value should be free'd
+ */
 char * sh_util_safe_name (const char * name)
-{
-  return sh_util_safe_name_int (name, SH_ESCAPE_SPACE); 
-}
-
-char * sh_util_safe_name_keepspace (const char * name)
-{
-  return sh_util_safe_name_int (name, SH_DONT_ESCAPE_SPACE); 
-}
-
-/* returns freshly allocated memory, return value should be free'd
- */
-char * sh_util_safe_name_int (const char * name, int escape_space)
 {
   register int  i = 0;
@@ -2014,5 +1424,4 @@
   char          oct[32];
   char          format[16];
-  size_t        len;
 
   SL_ENTER(_("sh_util_safe_name"));
@@ -2031,32 +1440,13 @@
   */
 
-  len = sl_strlen(name);
-  p   = name;
-
 #ifdef SH_USE_XML
-  if (sl_ok_muls (6, len) && sl_ok_adds ((6*len), 2))
-    { retval = SH_ALLOC(6 * len + 2); }
-  else
-    {
-      /* return an allocated array
-       */
-      retval = SH_ALLOC(11);
-      (void) sl_strlcpy(retval, _("(overflow)"), 11);
-      SL_RETURN(retval, _("sh_util_safe_name"));
-    }
+  retval = SH_ALLOC(6 * sl_strlen(name) + 2);
 #else
-  if (sl_ok_muls (4, len) && sl_ok_adds ((4*len), 2))
-    { retval = SH_ALLOC(4 * len + 2); }
-  else
-    {
-      /* return an allocated array
-       */
-      retval = SH_ALLOC(11);
-      (void) sl_strlcpy(retval, _("(overflow)"), 11);
-      SL_RETURN(retval, _("sh_util_safe_name"));
-    }
+  retval = SH_ALLOC(4 * sl_strlen(name) + 2);
 #endif 
 
   (void) sl_strncpy(format, _("%c%03o"), 16);
+
+  p = name;
 
   while (*p != '\0') {
@@ -2094,11 +1484,6 @@
 #endif
     } else if ( (*p) == ' ') {     /* space            */
-      if (escape_space) {
-	retval[i] = '\\'; ++i; 
-	retval[i] = ' ';
-      }
-      else {
-	retval[i] = *p;
-      }
+      retval[i] = '\\'; ++i; 
+      retval[i] = ' ';
 #ifdef SH_USE_XML
     } else if ( (*p) == '"') {     /* double quote     */
@@ -2132,5 +1517,4 @@
     } else if (!isgraph ((int) *p)) {    /* not printable    */
       /*@-bufferoverflowhigh -formatconst@*/
-      /* flawfinder: ignore */
       sprintf(oct, format, '\\',                 /* known to fit  */
 	      (unsigned char) *p);
@@ -2150,7 +1534,7 @@
 }
 
-int sh_util_isnum (const char *str)
-{
-  const char *p = str;
+int sh_util_isnum (char *str)
+{
+  char *p = str;
 
   SL_ENTER(_("sh_util_isnum"));
@@ -2168,5 +1552,5 @@
 char * sh_util_strconcat (const char * arg1, ...)
 {
-  size_t    length, l2;
+  size_t    length;
   char    * s;
   char    * strnew;
@@ -2183,19 +1567,10 @@
   while (s != NULL)
     {
-      l2 = sl_strlen (s);
-      if (sl_ok_adds(length, l2))
-	length += l2;
-      else
-	SL_RETURN(NULL, _("sh_util_strconcat"));
+      length = length + sl_strlen (s);
       s = va_arg (vl, char * );
     }
   va_end (vl);
 
-  if (sl_ok_adds(length, 2))
-    strnew = SH_ALLOC( length + 2 );
-  else
-    SL_RETURN(NULL, _("sh_util_strconcat"));
-
-  /* cppcheck-suppress uninitvar */
+  strnew = SH_ALLOC( length + 2 ); 
   strnew[0] = '\0';
 
@@ -2212,172 +1587,4 @@
 
   SL_RETURN(strnew, _("sh_util_strconcat"));
-}
-
-static const char bto64_0[] = N_("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789()");
-static char bto64[65] = { '\0' };
-
-  
-size_t sh_util_base64_enc (unsigned char * out, 
-			   const unsigned char * instr, 
-			   size_t lin)
-{
-  int             ll;
-  unsigned char   a, b, c;
-  size_t          len  = 0;
-  size_t          j    = 0;
-
- start:
-  if (bto64[0] != '\0')
-    {
-      if (instr /* && *instr *//* need to handle binary data */)
-	{
-	  if (lin == 0)
-	    lin = strlen((const char *)instr);
-
-	  if (lin > 0)
-	    {
-	      do {
-		ll = 0;
-		
-		if (len < lin) 
-		  { a = *instr; ++instr; ++len; ++ll; }
-		else 
-		  { a = 0; }
-		if (len < lin) 
-		  { b = *instr; ++instr; ++len; ++ll; }
-		else 
-		  { b = 0; }
-		if (len < lin) 
-		  { c = *instr; ++instr; ++len; ++ll; }
-		else 
-		  { c = 0; }
-		
-		*out = bto64[ a >> 2 ];
-		++j; ++out;
-		*out = bto64[ ((a & 0x03) << 4) | ((b & 0xf0) >> 4) ];
-		++j; ++out;
-		*out = (unsigned char) (ll > 1 ? bto64[ ((b & 0x0f) << 2) | ((c & 0xc0) >> 6) ] : '?');
-		++j; ++out;
-		*out = (unsigned char) (ll > 2 ? bto64[ c & 0x3f ] : '?');
-		++j; ++out;
-	      } while (len < lin);
-	    }
-	}
-      *out = '\0';
-      return j;
-    }
-
-  memcpy(bto64, _(bto64_0), 65);
-  goto start;
-}
-
-size_t sh_util_base64_enc_alloc (char **out, const char *in, size_t inlen)
-{
-  size_t outlen = SH_B64_SIZ(inlen);
-
-  if (inlen > outlen) /* overflow */
-    {
-      *out = NULL;
-      return 0;
-    }
-
-  *out = SH_ALLOC(outlen);
-  return sh_util_base64_enc((unsigned char *)*out, (const unsigned char *)in, inlen);
-}
-
-size_t sh_util_base64_dec (unsigned char *out, 
-			   const unsigned char *in, 
-			   size_t lin)
-{
-  size_t i;
-  unsigned char c;
-  unsigned char b;
-  size_t lout = 0;
-  unsigned int  w = 0;
-
-  if (out && in)
-    {
-      if (lin == 0)
-	lin = strlen((const char *)in);
-
-      for (i = 0; i < lin; i++)
-	{
-	  c = *in; ++in;
-	  b = 0;
-	  
-	  if ((c >= 'A') && (c <= 'Z'))
-	    {
-	      b = (c - 'A');
-	    }
-	  else if ((c >= 'a') && (c <= 'z'))
-	    {
-	      b = (c - 'a' + 26);
-	    }
-	  else if ((c >= '0') && (c <= '9'))
-	    {
-	      b = (c - '0' + 52);
-	    }
-	  else if (c == '(' || c == '+')
-	    {
-	      b = 62;
-	    }
-	  else if (c == ')' || c == '/')
-	    {
-	      b = 63;
-	    }
-	  else if (c == '?' || c == '=')
-	    {
-	      /* last byte was written to, but will now get
-	       * truncated
-	       */
-	      if (lout > 0) --lout;
-	      break;
-	    }
-	  
-	  if (w == 0)
-	    {
-	      *out = (b << 2) & 0xfc;
-	      ++lout;
-	    }
-	  else if (w == 1)
-	    {
-	      *out |= (b >> 4) & 0x03;
-	      ++out;
-	      *out = (b << 4) & 0xf0;
-	      ++lout;
-	    }
-	  else if (w == 2)
-	    {
-	      *out |= (b >> 2) & 0x0f;
-	      ++out;
-	      *out = (b << 6) & 0xc0;
-	      ++lout;
-	    }
-	  else if (w == 3)
-	    {
-	      *out |= b & 0x3f;
-	      ++out;
-	    }
-	  
-	  ++w;
-	  
-	  if (w == 4)
-	    {
-	      w = 0;
-	    }
-	}
-      *out = '\0';
-    }
-  return lout;
-}
-
-size_t sh_util_base64_dec_alloc (unsigned char **out, const unsigned char *in, 
-				 size_t lin)
-{
-  size_t lout = 3 * (lin / 4) + 2;
-
-  *out = SH_ALLOC(lout);
-
-  return sh_util_base64_dec (*out, in, lin);
 }
 
@@ -2412,7 +1619,7 @@
   if (status != 0 && status != REG_NOMATCH) 
     {
-      errbuf = SH_ALLOC(BUFSIZ);
+      errbuf = SH_ALLOC(BUFSIZ+2);
       (void) regerror(status, &preg, errbuf, BUFSIZ); 
-      errbuf[BUFSIZ-1] = '\0';
+      errbuf[BUFSIZ] = '\0';
       sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_REGEX,
 		       errbuf, regex_str);
Index: trunk/src/sh_utmp.c
===================================================================
--- trunk/src/sh_utmp.c	(revision 591)
+++ trunk/src/sh_utmp.c	(revision 1)
@@ -79,8 +79,5 @@
 #include "sh_modules.h"
 #include "sh_utmp.h"
-#include "sh_pthread.h"
-#include "sh_inotify.h"
-
-SH_MUTEX_EXTERN(mutex_thread_nolog);
+
 
 #ifdef TM_IN_SYS_TIME
@@ -141,42 +138,57 @@
 #ifdef HAVE_UTMPX_H
 
-#if defined(_PATH_UTMPX)
-#define SH_PATH_UTMP _PATH_UTMPX
-#elif defined(UTMPX_FILE)
-#define SH_PATH_UTMP UTMPX_FILE
-#elif defined(_PATH_UTMP)
-#define SH_PATH_UTMP _PATH_UTMP
-#else
+#ifndef _PATH_UTMP
+#ifdef   UTMPX_FILE
+#define _PATH_UTMP   UTMPX_FILE
+#else  
 #error  You must define UTMPX_FILE in the file config.h 
 #endif
-
-#else
-
-#if defined(_PATH_UTMP)
-#define SH_PATH_UTMP _PATH_UTMP
-#elif defined(UTMP_FILE)
-#define SH_PATH_UTMP UTMP_FILE
+#endif
+#ifndef _PATH_WTMP
+#ifdef   WTMPX_FILE
+#define _PATH_WTMP   WTMPX_FILE
+#else
+#error  You must define WTMPX_FILE in the file config.h
+#endif
+#endif
+
+#else
+
+#ifndef _PATH_UTMP
+#ifdef   UTMP_FILE
+#define _PATH_UTMP   UTMP_FILE
 #else  
 #error  You must define UTMP_FILE in the file config.h 
 #endif
+#endif
+#ifndef _PATH_WTMP
+#ifdef   WTMP_FILE
+#define _PATH_WTMP   WTMP_FILE
+#else
+#error  You must define WTMP_FILE in the file config.h
+#endif
+#endif
 
 #endif
 
 typedef struct log_user {
-  time_t              last_checked;
   char                ut_tty[UT_LINESIZE+1];    
   char                name[UT_NAMESIZE+1];
   char                ut_host[UT_HOSTSIZE+1];
-  char                ut_ship[SH_IP_BUF]; /* IP address */
+  char                ut_ship[16]; /* IP address */
   time_t              time;
   struct log_user   * next;
 } blah_utmp;
 
-static char * utmp_path = SH_PATH_UTMP;
+#ifdef HAVE_UTTYPE
+static char   terminated_line[UT_HOSTSIZE]; 
+#endif
+
+static struct SH_UTMP_S save_utmp;
 
 static void sh_utmp_logout_morechecks(struct log_user   * user);
 static void sh_utmp_login_morechecks(struct SH_UTMP_S * ut);
-static void sh_utmp_checklogin (struct SH_UTMP_S * ut, time_t start_read);
-static void sh_utmp_check_internal();
+static void sh_utmp_addlogin (struct SH_UTMP_S * ut);
+static void sh_utmp_check_internal(int mode);
 
 static int    ShUtmpLoginSolo    = SH_ERR_INFO;
@@ -208,20 +220,4 @@
   },
   {
-    N_("logincheckfirst"),
-    sh_login_set_checklevel
-  },
-  {
-    N_("logincheckoutlier"),
-    sh_login_set_siglevel
-  },
-  {
-    N_("logincheckdate"),
-    sh_login_set_def_allow
-  },
-  {
-    N_("logincheckuserdate"),
-    sh_login_set_user_allow
-  },
-  {
     NULL,
     NULL
@@ -229,28 +225,17 @@
 };
 
-static void set_defaults(void)
-{
-  ShUtmpLoginSolo    = SH_ERR_INFO;
-  ShUtmpLoginMulti   = SH_ERR_WARN;
-  ShUtmpLogout       = SH_ERR_INFO;
-  ShUtmpActive       = S_TRUE;
-  ShUtmpInterval     = 300;
-
-  sh_login_reset();
-  return;
-}
-
-#if defined(HAVE_UTMPX_H) && defined(HAVE_GETUTXENT)
-#define USE_SETUTENT 1
-#elif defined(HAVE_GETUTENT)
-#define USE_SETUTENT 1
-#endif
-
-
-#if defined (USE_SETUTENT)
-
-#ifdef HAVE_UTMPX_H 
-
-#define sh_utmp_utmpname(a)  (void)(a)
+int sh_utmp_null()
+{
+  return 0;
+}
+
+
+
+
+#if defined (HAVE_SETUTENT) && defined (USE_SETUTENT)
+
+#ifdef HAVE_UTMPX_H
+
+#define sh_utmp_utmpname     utmpxname
 #define sh_utmp_setutent     setutxent
 #define sh_utmp_endutent     endutxent
@@ -281,6 +266,10 @@
 
 static FILE * sh_utmpfile = NULL;
-static char   sh_utmppath[80] = SH_PATH_UTMP;
-
+static char   sh_utmppath[80] = _PATH_UTMP;
+
+/* sh_utmp_feed_forward is for optimizing
+ * (fseek instead of getutent loop)
+ */
+static long   sh_utmp_feed_forward = 0;
 
 static void sh_utmp_utmpname(const char * str)
@@ -289,9 +278,9 @@
   if (sh_utmpfile != NULL)
     {
-      (void) sl_fclose (FIL__, __LINE__, sh_utmpfile);
+      (void) fclose (sh_utmpfile);
       sh_utmpfile = NULL;
     }
 
-  (void) sl_strlcpy (sh_utmppath, str, sizeof(sh_utmppath));
+  (void) sl_strlcpy (sh_utmppath, str, 80);
   SL_RET0(_("sh_utmp_utmpname"));
 }
@@ -304,10 +293,13 @@
   SL_ENTER(_("sh_utmp_setutent"));
 
+  ASSERT((sh_utmppath != NULL), _("sh_utmppath != NULL"));
+
+  if (sh_utmppath == NULL)
+    SL_RET0(_("sh_utmp_setutent"));
+
   if (sh_utmpfile == NULL) 
     {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
       fd = (int) aud_open (FIL__, __LINE__, SL_NOPRIV, 
 			   sh_utmppath, O_RDONLY, 0);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
       if (fd >= 0)
 	{
@@ -321,12 +313,15 @@
 	{
 	  error = errno;
-	  SH_MUTEX_LOCK(mutex_thread_nolog);
 	  sh_error_handle ((-1), FIL__, __LINE__, error, MSG_E_ACCESS,
 			   (long) sh.real.uid, sh_utmppath);
-	  SH_MUTEX_UNLOCK(mutex_thread_nolog);
 	  SL_RET0(_("sh_utmp_setutent"));
 	}
     }
   (void) fseek (sh_utmpfile, 0L, SEEK_SET);
+  if (-1 == fseek (sh_utmpfile, sh_utmp_feed_forward, SEEK_CUR))
+    {
+      sh_utmp_feed_forward = 0; /* modified Apr 4, 2004 */
+      (void) fseek (sh_utmpfile, 0L, SEEK_SET);
+    }
   clearerr (sh_utmpfile);
   SL_RET0(_("sh_utmp_setutent"));
@@ -336,6 +331,5 @@
 {
   SL_ENTER(_("sh_utmp_endutent"));
-  if (NULL != sh_utmpfile)
-    (void) sl_fclose(FIL__, __LINE__, sh_utmpfile);
+  (void) fclose(sh_utmpfile);
   sh_utmpfile = NULL;
   SL_RET0(_("sh_utmp_endutent"));
@@ -368,36 +362,84 @@
 }
 
+#ifdef USE_UNUSED
+
+static struct SH_UTMP_S * sh_utmp_getutline(struct SH_UTMP_S * ut)
+{
+  struct SH_UTMP_S * out;
+ 
+  while (1) {
+      if ((out = sh_utmp_getutent()) == NULL) {
+       	return NULL;
+      }
+#ifdef HAVE_UTTYPE  
+      if (out->ut_type == USER_PROCESS || out->ut_type == LOGIN_PROCESS)
+	if (sl_strcmp(ut->ut_line, out->ut_line) == 0) 
+	  return out;
+#else
+      if ( 0 != sl_strncmp (out->ut_name, "reboot",   6) &&
+	   0 != sl_strncmp (out->ut_name, "shutdown", 8) &&
+	   0 != sl_strncmp (out->ut_name, "date",     4) )
+	return out;
+#endif
+  }
+  return NULL;
+}
+
+static struct SH_UTMP_S * sh_utmp_getutid(struct SH_UTMP_S * ut)
+{
+#ifdef HAVE_UTTYPE  
+  struct SH_UTMP_S * out;
+
+  if (ut->ut_type == RUN_LVL  || ut->ut_type == BOOT_TIME ||
+      ut->ut_type == NEW_TIME || ut->ut_type == OLD_TIME) 
+    {
+      while (1) {
+	if ((out = sh_utmp_getutent()) == NULL) {
+	  return NULL;
+	}
+	if (out->ut_type == ut->ut_type) 
+	  return out;
+      }
+    } 
+  else if (ut->ut_type == INIT_PROCESS || ut->ut_type == LOGIN_PROCESS ||
+	   ut->ut_type == USER_PROCESS || ut->ut_type == DEAD_PROCESS ) 
+    {
+      while (1) {
+	if ((out = sh_utmp_getutent()) == NULL) {
+	  return NULL;
+	}
+	if (sl_strcmp(ut->ut_id, out->ut_id) == 0) 
+	  return out;
+      }
+    }
+#endif
+  return NULL;
+}
+/* #ifdef USE_UNUSED */
+#endif
+
 /* #ifdef HAVE_SETUTENT */
 #endif
 
 #ifdef HAVE_UTADDR
-#ifdef HAVE_UTADDR_V6
-static char * my_inet_ntoa(SINT32 * ut_addr_v6, char * buf, size_t buflen)
-{
-  struct in_addr in;
-
-  buf[0] = '\0';
-
-  if (0 == (ut_addr_v6[1] + ut_addr_v6[2] + ut_addr_v6[3]))
-    {
-      memcpy(&in, ut_addr_v6, sizeof(struct in_addr));
-      sl_strlcpy(buf, inet_ntoa(in), buflen);
-    }
-  else
-    {
-      inet_ntop(AF_INET6, ut_addr_v6, buf, buflen);
-    }
-  return buf;
-}
-#else
-static char * my_inet_ntoa(SINT32 ut_addr, char * buf, size_t buflen)
-{
-  struct in_addr in;
-
-  buf[0] = '\0';
-
-  memcpy(&in, ut_addr, sizeof(struct in_addr));
-  sl_strlcpy(buf, inet_ntoa(in), buflen);
-  return buf;
+#ifdef HAVE_INET_ATON
+static char * my_inet_ntoa(struct in_addr in)
+{
+  return /*@-unrecog@*/inet_ntoa(in)/*@+unrecog@*/;
+}
+#else
+static char * my_inet_ntoa(struct in_addr in)
+{
+  unsigned char a, b, c, d;
+  static char   foo[16];
+  char          bar[4];
+  memcpy (bar, &(in.s_addr), 4); /* memory alignment (?) */
+  memcpy (&a, &bar[0], 1);
+  memcpy (&b, &bar[1], 1);
+  memcpy (&c, &bar[2], 1);
+  memcpy (&d, &bar[3], 1);
+  sprintf(foo, "%d.%d.%d.%d",                          /* known to fit  */
+	  (int) a, (int) b, (int) c, (int) d);
+  return foo;
 }
 #endif
@@ -412,5 +454,4 @@
 static struct log_user   * userlist   = NULL;
 static time_t  lastcheck;
-static int     init_done = 0;
 
 /*************
@@ -419,7 +460,7 @@
  *
  *************/
-
-static int sh_utmp_init_internal (void)
-{
+int sh_utmp_init ()
+{
+  static int done = 0;
 
   SL_ENTER(_("sh_utmp_init"));
@@ -429,37 +470,14 @@
   /* do not re-initialize after a re-configuration
    */
-  if (init_done == 1) {
+  if (done == 1) {
     SL_RETURN( (0), _("sh_utmp_init"));
   }
   lastcheck  = time (NULL);
   userlist   = NULL;
-  sh_utmp_check_internal (); /* current logins */
-  init_done = 1;
+  memset (&save_utmp, 0, sizeof(struct SH_UTMP_S));
+  sh_utmp_check_internal (2); /* current logins */
+  sh_utmp_check_internal (0);
+  done = 1;
   SL_RETURN( (0), _("sh_utmp_init"));
-}
-
-int sh_utmp_init (struct mod_type * arg)
-{
-#if !defined(HAVE_PTHREAD)
-  (void) arg;
-#endif
-  if (ShUtmpActive == BAD)
-    return SH_MOD_FAILED;
-#ifdef HAVE_PTHREAD
-  if (arg != NULL && arg->initval < 0 && 
-      (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
-    {
-      if (0 == sh_pthread_create(sh_threaded_module_run, (void *)arg))
-	return SH_MOD_THREAD;
-      else
-	return SH_MOD_FAILED;
-    }
-  else if (arg != NULL && arg->initval == SH_MOD_THREAD &&
-	   (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
-    {
-      return SH_MOD_THREAD;
-    }
-#endif
-  return sh_utmp_init_internal();
 }
 
@@ -470,9 +488,5 @@
  *************/
 #ifdef HAVE_UTTYPE
-static int sh_utmp_login_clean(void);
-#endif
-
-#if defined(HAVE_PTHREAD)
-static sh_watches inotify_watch = SH_INOTIFY_INITIALIZER;
+static int sh_utmp_login_clean();
 #endif
 
@@ -493,25 +507,5 @@
   (void) sh_utmp_login_clean();
 #endif
-  /* Reset the flag, such that the module
-   * can be re-enabled.
-   */
-  set_defaults();
-  init_done          = 0;
-
-#if defined(HAVE_PTHREAD)
-  sh_inotify_remove(&inotify_watch);
-#endif
-
   SL_RETURN( (0), _("sh_utmp_end"));
-}
-
-
-int sh_utmp_reconf()
-{
-  set_defaults();
-#if defined(HAVE_PTHREAD)
-  sh_inotify_remove(&inotify_watch);
-#endif
-  return 0;
 }
 
@@ -524,7 +518,4 @@
 int sh_utmp_timer (time_t tcurrent)
 {
-#if !defined(HAVE_PTHREAD)
-  retry_msleep(1, 0);
-
   if ((time_t) (tcurrent - lastcheck) >= ShUtmpInterval)
     {
@@ -533,29 +524,4 @@
     }
   return 0;
-#else
-  int errnum = 0;
-  
-  if ( (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE) &&
-       sh.flag.checkSum != SH_CHECK_INIT )
-    {
-      sh_inotify_wait_for_change(utmp_path, &inotify_watch, 
-				 &errnum, ShUtmpInterval);
-    }
-  
-  lastcheck  = tcurrent;
-
-  if (SH_INOTIFY_ERROR(errnum))
-    {
-      char ebuf[SH_ERRBUF_SIZE];
-
-      SH_MUTEX_LOCK(mutex_thread_nolog);
-      sh_error_message(errnum, ebuf, sizeof(ebuf));
-      sh_error_handle (SH_ERR_WARN, FIL__, __LINE__, errnum, MSG_E_SUBGEN,
-		       ebuf,
-		       _("sh_utmp_timer") );
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);    
-    }
-  return -1;
-#endif
 }
 
@@ -568,15 +534,6 @@
 {
   SL_ENTER(_("sh_utmp_check"));
-  if (ShUtmpActive == BAD)
-    {
-#if defined(HAVE_PTHREAD)
-      sh_inotify_remove(&inotify_watch);
-#endif
-      SL_RETURN( (-1), _("sh_utmp_check"));
-    }
-  SH_MUTEX_LOCK(mutex_thread_nolog);
   sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_UT_CHECK);
-  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-  sh_utmp_check_internal ();
+  sh_utmp_check_internal (1);
 
   SL_RETURN(0, _("sh_utmp_check"));
@@ -589,5 +546,5 @@
  *************/
 
-int sh_utmp_set_login_solo  (const char * c)
+int sh_utmp_set_login_solo  (char * c)
 {
   int retval;
@@ -597,11 +554,9 @@
   tmp[0] = '='; tmp[1] = '\0';
   (void) sl_strlcat (tmp, c, 32);
-  SH_MUTEX_LOCK(mutex_thread_nolog);
   retval = sh_error_set_level (tmp, &ShUtmpLoginSolo);
-  SH_MUTEX_UNLOCK(mutex_thread_nolog);
   SL_RETURN(retval, _("sh_utmp_set_login_solo"));
 }
 
-int sh_utmp_set_login_multi (const char * c)
+int sh_utmp_set_login_multi (char * c)
 {
   int retval;
@@ -611,11 +566,9 @@
   tmp[0] = '='; tmp[1] = '\0';
   (void) sl_strlcat (tmp, c, 32);
-  SH_MUTEX_LOCK(mutex_thread_nolog);
   retval = sh_error_set_level (tmp, &ShUtmpLoginMulti);
-  SH_MUTEX_UNLOCK(mutex_thread_nolog);
   SL_RETURN(retval, _("sh_utmp_set_login_multi"));
 }
 
-int sh_utmp_set_logout_good (const char * c)
+int sh_utmp_set_logout_good (char * c)
 {
   int retval;
@@ -625,12 +578,11 @@
   tmp[0] = '='; tmp[1] = '\0';
   (void) sl_strlcat (tmp, c, 32);
-  SH_MUTEX_LOCK(mutex_thread_nolog);
   retval = sh_error_set_level (tmp, &ShUtmpLogout);
-  SH_MUTEX_UNLOCK(mutex_thread_nolog);
   SL_RETURN(retval, _("sh_utmp_set_logout_good"));
 }
 
-int sh_utmp_set_login_timer (const char * c)
-{
+int sh_utmp_set_login_timer (char * c)
+{
+  int retval = 0;
   long val;
 
@@ -639,10 +591,10 @@
   if (val <= 0)
     {
-      SH_MUTEX_LOCK(mutex_thread_nolog);
       sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
 		       _("utmp timer"), c);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
-      SL_RETURN((-1), _("sh_utmp_set_login_timer"));
-    }
+      retval = -1;
+    }
+
+  val = (val <= 0 ? 60 : val);
 
   ShUtmpInterval = (time_t) val;
@@ -650,5 +602,5 @@
 }
 
-int sh_utmp_set_login_activate (const char * c)
+int sh_utmp_set_login_activate (char * c)
 {
   int i;
@@ -658,5 +610,5 @@
 }
 
-
+#ifdef HAVE_UTTYPE
 struct login_ct {
   char name[UT_NAMESIZE+1];
@@ -667,5 +619,5 @@
 static struct login_ct * login_ct_list = NULL;
 
-static int sh_utmp_login_clean(void)
+static int sh_utmp_login_clean()
 {
   struct login_ct * list = login_ct_list;
@@ -738,4 +690,5 @@
 }
 
+#endif
 
 
@@ -745,282 +698,297 @@
  *    - link user.ut_record   -> log_record
  */
-
-#include <ctype.h>
+#ifdef HAVE_UTTYPE  
 static int sh_utmp_is_virtual (char * in_utline, char * in_uthost)
 {
-
-  if (in_uthost != NULL   &&
-      in_utline != NULL   &&
+  if (in_utline != NULL)
+    {
+      if (0 == sl_strncmp(in_utline, _("ttyp"), 4))
+	{ return 0; }
+      else if (0 == sl_strncmp(in_utline, _("ttyq"), 4))
+	{ return 0; }
+    }
+  if (in_uthost != NULL   && 
       in_uthost[0] == ':' && 
-      isdigit((int) in_uthost[1]) && 
-      0 == sl_strncmp(in_utline, _("pts/"), 4))
-    {
-      return 1;
-    }
-
-  return 0;
-}
-
-
-static void sh_utmp_log_out(int sev, struct log_user * user, int n)
-{
-  char ttt[TIM_MAX];
-  
-  SH_MUTEX_LOCK(mutex_thread_nolog);
-  (void) sh_unix_time (user->time, ttt, TIM_MAX);
-  sh_error_handle( sev, FIL__, __LINE__, 0,
-#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
-		   MSG_UT_LG3X,
-#elif defined(HAVE_UTHOST)
-		   MSG_UT_LG3A,
-#else
-		   MSG_UT_LG3B,
-#endif
-		   user->name,
-		   user->ut_tty,
-#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
-		   user->ut_host,
-		   user->ut_ship,
-#elif defined(HAVE_UTHOST)
-		   user->ut_host,
-#endif
-		   ttt,
-		   n
-		   );
-  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-}
-
-static void sh_utmp_log_multi(int sev, struct log_user * user, int n)
-{
-  char ttt[TIM_MAX];
-  
-  SH_MUTEX_LOCK(mutex_thread_nolog);
-  (void) sh_unix_time (user->time, ttt, TIM_MAX);
-  sh_error_handle( sev, FIL__, __LINE__, 0,
-#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
-		   MSG_UT_LG2X,
-#elif defined(HAVE_UTHOST)
-		   MSG_UT_LG2A,
-#else
-		   MSG_UT_LG2B,
-#endif
-		   user->name,
-		   user->ut_tty,
-#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
-		   user->ut_host,
-		   user->ut_ship,
-#elif defined(HAVE_UTHOST)
-		   user->ut_host,
-#endif
-		   ttt,
-		   n
-		   );
-  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-}
-
-static void sh_utmp_log_one(int sev, struct log_user * user, int n)
-{
-  char ttt[TIM_MAX];
-  
-  SH_MUTEX_LOCK(mutex_thread_nolog);
-  (void) sh_unix_time (user->time, ttt, TIM_MAX);
-  sh_error_handle( sev, FIL__, __LINE__, 0,
-#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
-		   MSG_UT_LG1X,
-#elif defined(HAVE_UTHOST)
-		   MSG_UT_LG1A,
-#else
-		   MSG_UT_LG1B,
-#endif
-		   user->name,
-		   user->ut_tty,
-#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
-		   user->ut_host,
-		   user->ut_ship,
-#elif defined(HAVE_UTHOST)
-		   user->ut_host,
-#endif
-		   ttt,
-		   n
-		   );
-  SH_MUTEX_UNLOCK(mutex_thread_nolog);
-}
-
-static void sh_utmp_purge_old (time_t start_read)
+      in_uthost[1] == '0' && 
+      in_uthost[2] == '\0')
+    {
+      /* return 0; */
+      return 1;  /* actually does not seem portable, not even among linuxes */
+    }
+  return 1;
+}
+#endif
+
+static void sh_utmp_addlogin (struct SH_UTMP_S * ut)
 {
   struct log_user   * user     = userlist;
   struct log_user   * userold  = userlist;
-  volatile int        status;
-
-  /* ------- find old entries -------- 
+#ifdef HAVE_UTTYPE  
+  struct log_user   * username = userlist;
+#endif
+
+  char   ttt[TIM_MAX];
+#ifdef HAVE_UTTYPE
+  int    status;
+#endif
+
+  SL_ENTER(_("sh_utmp_addlogin"));
+
+  if (ut->ut_line[0] == '\0')
+    SL_RET0(_("sh_utmp_addlogin"));
+
+  /* for some stupid reason, AIX repeats the wtmp entry for logouts
+   * with ssh
+   */
+  if (memcmp (&save_utmp, ut, sizeof(struct SH_UTMP_S)) == 0)
+    {
+      memset(&save_utmp, (int) '\0', sizeof(struct SH_UTMP_S));
+      SL_RET0(_("sh_utmp_addlogin"));
+    }
+  memcpy (&save_utmp, ut, sizeof(struct SH_UTMP_S));
+
+
+  /* ------- find user -------- 
    */
   while (user != NULL) 
     {
-      if (user->last_checked < start_read)
+      if (0 == sl_strncmp(user->ut_tty, ut->ut_line, UT_LINESIZE) ) 
+	break;
+      userold = user;
+      user = user->next;
+    }
+
+#ifdef HAVE_UTTYPE  
+  while (username != NULL) 
+    {
+      if (0 == sl_strncmp(username->name, ut->ut_name, UT_NAMESIZE) ) 
+	break;
+      username = username->next;
+    }
+#endif
+  
+#ifdef HAVE_UTTYPE  
+  /* ---------- LOGIN -------------- */
+  if (ut->ut_type == USER_PROCESS) 
+    {
+      if (user == NULL)
 	{
-	  /* report logout */
-	  if (0 == sh_utmp_is_virtual(user->ut_tty, user->ut_host))
-	    {
-	      /* reference count down on list of logged in users */
-	      status = sh_utmp_login_r(user->name);
-	      
-	      sh_utmp_log_out(ShUtmpLogout, user, status);
-	      sh_utmp_logout_morechecks((struct log_user *)user);
-	    }
-
-	  /* remove entry */
-	  if (userold == user && userold == userlist)
-	    {
-	      /* first element in userlist, advance userlist & userold */
-	      userold  = user->next;
-	      userlist = user->next;
-	      SH_FREE((struct log_user *)user);
-	      user     = userlist;
-	    }
-	  else
-	    {
-	      /* other element in userlist, cut it out */
-	      userold->next = user->next;
-	      SH_FREE((struct log_user *)user);
-	      user = userold->next;
-	    }
+	  user = SH_ALLOC(sizeof(struct log_user));
+	  user->next       = userlist;
+	  userlist         = user;
+	}
+      (void) sl_strlcpy(user->ut_tty,  ut->ut_line, UT_LINESIZE+1);
+      (void) sl_strlcpy(user->name,    ut->ut_name, UT_NAMESIZE+1);
+#ifdef HAVE_UTHOST
+      (void) sl_strlcpy(user->ut_host, ut->ut_host, UT_HOSTSIZE+1);
+#else
+      user->ut_host[0] = '\0';
+#endif
+#ifdef HAVE_UTADDR
+      /*@-type@*//* ut_addr does exist !!! */
+      (void) sl_strlcpy(user->ut_ship, 
+			my_inet_ntoa(*(struct in_addr*)&(ut->ut_addr)), 16);
+      /*@+type@*/
+#endif
+      user->time = ut->ut_time;
+
+      if (username == NULL                              /* not yet logged in */
+          || 0 == sl_strncmp(ut->ut_line, _("ttyp"), 4) /* in virt. console  */
+          || 0 == sl_strncmp(ut->ut_line, _("ttyq"), 4) /* in virt. console  */
+	  ) {
+	status = sh_utmp_login_a(user->name);
+	(void) sl_strlcpy(ttt, sh_unix_time (user->time), TIM_MAX);
+	sh_error_handle( ShUtmpLoginSolo, FIL__, __LINE__, 0,
+#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
+			 MSG_UT_LG1X,
+#elif defined(HAVE_UTHOST)
+			 MSG_UT_LG1A,
+#else
+			 MSG_UT_LG1B,
+#endif
+			 user->name,
+			 user->ut_tty,
+#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
+			 user->ut_host,
+			 user->ut_ship,
+#elif defined(HAVE_UTHOST)
+			 user->ut_host,
+#endif
+			 ttt,
+			 status
+			 );
+      } else
+	if (0 != sh_utmp_is_virtual(ut->ut_line, user->ut_host))
+	  {       
+	    status = sh_utmp_login_a(user->name);
+	    (void) sl_strlcpy(ttt, sh_unix_time (user->time), TIM_MAX);
+	    sh_error_handle( ShUtmpLoginMulti, FIL__, __LINE__, 0,
+#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
+			     MSG_UT_LG2X,
+#elif defined(HAVE_UTHOST)
+			     MSG_UT_LG2A,
+#else
+			     MSG_UT_LG2B,
+#endif
+			     user->name,
+			     user->ut_tty,
+#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
+			     user->ut_host,
+			     user->ut_ship,
+#elif defined(HAVE_UTHOST)
+			     user->ut_host,
+#endif
+			     ttt,
+			     status
+			     );
+	  }
+      
+      sh_utmp_login_morechecks(ut);
+      SL_RET0(_("sh_utmp_addlogin"));
+    }
+
+
+  /* ---------  LOGOUT ---------------- */
+  else if (ut->ut_name[0] == '\0'
+	   || ut->ut_type == DEAD_PROCESS  /* solaris does not clear ut_name */
+	   )
+    {
+      if (user != NULL)
+	{
+	  status = sh_utmp_login_r(user->name);
+	  /* sl_strlcpy(ttt, sh_unix_time (user->time), TIM_MAX); */
+	  (void) sl_strlcpy(ttt, sh_unix_time (ut->ut_time), TIM_MAX);
+	  sh_error_handle( ShUtmpLogout, FIL__, __LINE__, 0,
+#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
+			   MSG_UT_LG3X,
+#elif defined(HAVE_UTHOST)
+			   MSG_UT_LG3A,
+#else
+			   MSG_UT_LG3B,
+#endif
+			   user->name,
+			   user->ut_tty,
+#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
+			   user->ut_host,
+			   user->ut_ship,
+#elif defined(HAVE_UTHOST)
+			   user->ut_host,
+#endif
+			   ttt,
+			   status
+			   );
+	  userold->next = user->next;
+	  if (user == userlist)
+	    userlist = user->next;
+	  sh_utmp_logout_morechecks(user);
+	  SH_FREE(user);
+	  user = NULL;
 	}
       else
 	{
-	  userold = user;
-	  user    = user->next;
+	  (void) sl_strlcpy(terminated_line, ut->ut_line, UT_HOSTSIZE);
+	  (void) sl_strlcpy(ttt, sh_unix_time (ut->ut_time), TIM_MAX);
+	  sh_error_handle( ShUtmpLogout, FIL__, __LINE__, 0,
+			   MSG_UT_LG3C,
+			   terminated_line,
+			   ttt, 0
+			   );
 	}
-    }
-  return;
-}
-
-/* These variables are not used anywhere. They only exist
- * to assign &userold, &user to them, which keeps gcc from
- * putting them into a register, and avoids the 'clobbered
- * by longjmp' warning. And no, 'volatile' proved insufficient.
- */
-void * sh_dummy_850_userold = NULL;
-void * sh_dummy_851_user    = NULL;
-
-
-static void sh_utmp_checklogin (struct SH_UTMP_S * ut, time_t start_read)
-{
-  struct log_user   * user     = userlist;
-  struct log_user   * userold  = userlist;
-
-  struct log_user   * username = userlist;
-  volatile int    status;
-
-
-  SL_ENTER(_("sh_utmp_checklogin"));
-
-  if (ut->ut_line[0] == '\0')
-    SL_RET0(_("sh_utmp_checklogin"));
-
-  /* Take the address to keep gcc from putting them into registers. 
-   * Avoids the 'clobbered by longjmp' warning. 
-   */
-  sh_dummy_850_userold = (void*) &userold;
-  sh_dummy_851_user    = (void*) &user;
-
-  /* ------- find user -------- 
-   */
-  while (user != NULL) 
-    {
-      if (0 == sl_strncmp(user->ut_tty, ut->ut_line, UT_LINESIZE) &&
-	  0 == sl_strncmp(user->name, ut->ut_name, UT_NAMESIZE)) 
-	break;
-      userold = user;
-      user = user->next;
-    }
-
-
-  while (username != NULL) 
-    {
-      if (0 == sl_strncmp(username->name, ut->ut_name, UT_NAMESIZE) ) 
-	break;
-      username = username->next;
-    }
-
-  
-  if (user == NULL)
+      SL_RET0(_("sh_utmp_addlogin"));
+    }
+
+  /* default */
+  SL_RET0(_("sh_utmp_addlogin"));
+
+  /* #ifdef HAVE_UTTYPE                   */
+#else
+
+  if (user == NULL)   /* probably a login */
     {
       user = SH_ALLOC(sizeof(struct log_user));
+      sl_strlcpy(user->ut_tty,  ut->ut_line, UT_LINESIZE+1);
+      sl_strlcpy(user->name,    ut->ut_name, UT_NAMESIZE+1);
+#ifdef HAVE_UTHOST
+      sl_strlcpy(user->ut_host, ut->ut_host, UT_HOSTSIZE+1);
+#endif
+#ifdef HAVE_UTADDR
+      sl_strlcpy(user->ut_ship,my_inet_ntoa((struct in_addr)ut->ut_addr),16);
+#endif
+      user->time       = ut->ut_time;
       user->next       = userlist;
-      userlist         = (struct log_user *) user;
-    }
-  else if ( (user->time == ut->ut_time) &&
-	    0 == sl_strcmp (user->name, ut->ut_name))
-    {
-      /* we have it on record and nothing has changed */
-      user->last_checked = start_read;
-      goto out;
-    }
-  else
-    {
-      /* we have it on record and something has changed */
-      if (0 == sh_utmp_is_virtual(user->ut_tty, user->ut_host))
-	{
-	  /* reference count down on list of logged in users */
-	  status = sh_utmp_login_r(user->name);
-	  
-	  sh_utmp_log_out(ShUtmpLogout, user, status);
-	  sh_utmp_logout_morechecks((struct log_user *)user);
-	}
-    }
-
-  user->last_checked = start_read;
-  
-  (void) sl_strlcpy(user->ut_tty,  ut->ut_line, UT_LINESIZE+1);
-  (void) sl_strlcpy(user->name,    ut->ut_name, UT_NAMESIZE+1);
-#ifdef HAVE_UTHOST
-  (void) sl_strlcpy(user->ut_host, ut->ut_host, UT_HOSTSIZE+1);
-#else
-  user->ut_host[0] = '\0';
-#endif
-#ifdef HAVE_UTADDR
-#ifdef HAVE_UTADDR_V6
-  my_inet_ntoa(ut->ut_addr_v6, user->ut_ship, SH_IP_BUF);
-#else
-  my_inet_ntoa(ut->ut_addr, user->ut_ship, SH_IP_BUF);
-#endif
-#endif
-  user->time = ut->ut_time;
-  
-  if (username == NULL)        /* not yet logged in */
-    {
-      /* add this username to the list of logged in users */
-      status = sh_utmp_login_a(user->name);
-      
-      sh_utmp_log_one(ShUtmpLoginSolo, user, status);
-      
-    }
-  else if (0 == sh_utmp_is_virtual(user->ut_tty, (char*)user->ut_host))
-    {
-      /* add this username to the list of logged in users */
-      status = sh_utmp_login_a((char*)user->name);
-      
-      sh_utmp_log_multi(ShUtmpLoginMulti, user, status);
-    }
-  sh_utmp_login_morechecks(ut);
-  goto out;
-
-
- out:
-  sh_dummy_851_user    = NULL;
-  sh_dummy_850_userold = NULL;
-
-  SL_RET0(_("sh_utmp_checklogin"));
+      userlist         = user;
+
+      sl_strlcpy(ttt, sh_unix_time (user->time), TIM_MAX);
+
+
+      sh_error_handle( ShUtmpLoginSolo, FIL__, __LINE__, 0,
+#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
+		       MSG_UT_LG1X,
+#elif defined(HAVE_UTHOST)
+		       MSG_UT_LG1A,
+#else
+		       MSG_UT_LG1B,
+#endif
+		       user->name,
+		       user->ut_tty,
+#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
+		       user->ut_host,
+		       user->ut_ship,
+#elif defined(HAVE_UTHOST)
+		       user->ut_host,
+#endif
+		       ttt,
+		       1
+		       );
+      sh_utmp_login_morechecks(ut);
+    }
+  else  /* probably a logout */
+    {
+      sl_strlcpy(ttt, sh_unix_time (ut->ut_time), TIM_MAX);
+      /* sl_strlcpy(ttt, sh_unix_time (user->time), TIM_MAX); */
+
+      sh_error_handle( ShUtmpLogout, FIL__, __LINE__, 0,
+#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
+		       MSG_UT_LG2X,
+#elif defined(HAVE_UTHOST)
+		       MSG_UT_LG2A,
+#else
+		       MSG_UT_LG2B,
+#endif
+		       user->name,
+		       user->ut_tty,
+#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
+		       user->ut_host,
+		       user->ut_ship,
+#elif defined(HAVE_UTHOST)
+		       user->ut_host,
+#endif
+		       ttt,
+		       1
+		       );
+      sh_utmp_logout_morechecks(user);
+      userold->next = user->next;
+      if (user == userlist)       /* inserted Apr 4, 2004 */
+	userlist = user->next;
+      SH_FREE(user);
+      user = NULL;
+    }
+
+  SL_RET0(_("sh_utmp_addlogin"));
+#endif
 }
 
 static time_t        lastmod  = 0;
-
-static void sh_utmp_check_internal ()
+static off_t         lastsize = 0;
+static unsigned long lastread = 0;
+
+static char * mode_path[] = { _PATH_WTMP, _PATH_WTMP, _PATH_UTMP };
+
+static void sh_utmp_check_internal (int mode)
 {
   struct stat   buf;
   int           error;
   struct SH_UTMP_S * ut;
-  int           val_retry;
-  time_t        start_read;
+  unsigned long this_read = 0;
 
   SL_ENTER(_("sh_utmp_check_internal"));
@@ -1028,34 +996,60 @@
   /* error if no access
    */
-  do {
-    val_retry = /*@-unrecog@*/lstat ( utmp_path, &buf)/*@+unrecog@*/;
-  } while (val_retry < 0 && errno == EINTR);
-
-  if (0 != val_retry) 
+  if (0 != retry_lstat(FIL__, __LINE__, mode_path[mode], &buf)) 
     {
       error = errno;
-      SH_MUTEX_LOCK(mutex_thread_nolog);
       sh_error_handle((-1), FIL__, __LINE__, error, MSG_E_ACCESS,
-		      (long) sh.real.uid, utmp_path);
-      SH_MUTEX_UNLOCK(mutex_thread_nolog);
+		      (long) sh.real.uid, mode_path[mode]);
       SL_RET0(_("sh_utmp_check_internal"));
     }
 
-  /* check modification time
+  /* modification time
    */
-  if (/*@-usedef@*/buf.st_mtime <= lastmod/*@+usedef@*/)
+  if (mode < 2)
+    {
+      if (/*@-usedef@*/buf.st_mtime <= lastmod/*@+usedef@*/)
+	{ 
+	  SL_RET0(_("sh_utmp_check_internal"));
+	}
+      else
+	lastmod = buf.st_mtime;
+    }
+
+  /* file size
+   */
+  if (/*@-usedef@*/buf.st_size < lastsize/*@+usedef@*/ && mode < 2) 
     { 
-      SL_RET0(_("sh_utmp_check_internal"));
-    }
-  else
-    lastmod = buf.st_mtime;
-
-  sh_utmp_utmpname(utmp_path);
+      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_UT_ROT,
+		      mode_path[mode]);
+      lastread = 0;
+#ifndef USE_SETUTENT
+      sh_utmp_feed_forward = 0L;
+#endif
+    }
+
+  if (mode < 2)
+    lastsize = buf.st_size;
+
+  if (buf.st_size == 0) 
+    SL_RET0(_("sh_utmp_check_internal"));
+
+  sh_utmp_utmpname(mode_path[mode]);
   sh_utmp_setutent();
+
+  /* 
+   * feed forward if initializing
+   * we need to do this here
+   */
+  if (mode < 2)
+    {
+      while (this_read < lastread) {
+	ut = sh_utmp_getutent();
+	++this_read;
+      }
+    }
 
   /* start reading
    */
-  start_read = time(NULL);
-  
+  this_read = 0;
   while (1 == 1) {
     ut = sh_utmp_getutent();
@@ -1063,24 +1057,38 @@
       break;
     /* modified: ut_user --> ut_name */
-    if (ut->ut_name[0] != '\0'
+    if (mode == 1 || (mode == 2 && ut->ut_name[0] != '\0'
 #ifdef HAVE_UTTYPE
-	&& ut->ut_type == USER_PROCESS
-#endif
-	)
-      sh_utmp_checklogin (ut, start_read);
+		      && ut->ut_type != DEAD_PROCESS
+#endif
+		      ))
+      sh_utmp_addlogin (ut);
+    /*************************************************
+    printf("%8s | %10s | %10s | %3d %5d | %16s | %ld\n", 
+	   ut->ut_name, ut->ut_id, ut->ut_line, 
+	   (int) ut->ut_type, (int) ut->ut_pid, 
+	   ut->ut_host, ut->ut_time);
+    ***************************************************/
+    ++this_read;
   }
 
   sh_utmp_endutent();
 
-  sh_utmp_purge_old (start_read);
+  if (mode < 2)
+    {
+      lastread += this_read;
+#ifndef USE_SETUTENT
+      sh_utmp_feed_forward += (long) (this_read * sizeof(struct SH_UTMP_S));
+      lastread = 0;
+#endif
+    }
 
   SL_RET0(_("sh_utmp_check_internal"));
 }
 
-extern void sh_ltrack_check(struct SH_UTMP_S * ut);
 
 static void sh_utmp_login_morechecks(struct SH_UTMP_S * ut)
 {
-  sh_ltrack_check(ut);
+  if (ut)
+    return;
   return;
 }
@@ -1088,5 +1096,6 @@
 static void sh_utmp_logout_morechecks(struct log_user * user)
 {
-  (void) user;
+  if (user)
+    return;
   return;
 }
Index: trunk/src/sh_xfer_client.c
===================================================================
--- trunk/src/sh_xfer_client.c	(revision 591)
+++ 	(revision )
@@ -1,1632 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 1999, 2000, 2015 Rainer Wichmann                          */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-#include "config_xor.h"
-
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-
-/* Must be early on FreeBSD
- */
-#include <sys/types.h>
-
-/* must be .le. than (1020 * 64)
- * (see sh_tools.c -- put_header)
- *
- * also: must be  (N * 16), otherwise
- * binary files cannot be transferred encrypted
- *
- * 65280 = (1020*64)
- * #define TRANS_BYTES 8000  V0.8
- */
-#ifdef  SH_ENCRYPT
-#define TRANS_BYTES 65120
-#else
-#define TRANS_BYTES 65280
-#endif
-
-/* timeout for session key
- */
-#define TIMEOUT_KEY 7200
-
-/* max time between connection attempts
- */
-#define TIMEOUT_CON 2048 
-
-/* #undef  SRP_DEBUG */
-/* #define SRP_DEBUG */
-
-#ifdef HAVE_MEMORY_H
-#include <memory.h>
-#endif
-
-#if HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
-#include <time.h>
-
-#ifdef HAVE_SYS_SELECT_H
-#include <sys/select.h>
-#endif
-
-#ifdef  HAVE_UNISTD_H
-#include <errno.h>
-#include <signal.h>
-#include <setjmp.h>
-#include <pwd.h>
-#include <grp.h>
-#include <sys/stat.h>
-#include <sys/resource.h>
-#include <fcntl.h>
-#include <sys/wait.h>
-#include <unistd.h>
-#endif
-
-#ifndef FD_SET
-#define NFDBITS         32
-#define FD_SET(n, p)    ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
-#define FD_CLR(n, p)    ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
-#define FD_ISSET(n, p)  ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
-#endif /* !FD_SET */
-#ifndef FD_SETSIZE
-#define FD_SETSIZE      32
-#endif
-#ifndef FD_ZERO
-#define FD_ZERO(p)      memset((char *)(p), 0, sizeof(*(p)))
-#endif
-
-#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
-#include <sys/mman.h>
-#endif
-
-
-#include <netdb.h> 
-#include <sys/types.h> 
-#include <netinet/in.h> 
-#include <sys/socket.h> 
-#ifndef S_SPLINT_S
-#include <arpa/inet.h>
-#endif
-
-#include "sh_ipvx.h"
-#include "samhain.h"
-#include "sh_tiger.h"
-#include "sh_utils.h"
-#include "sh_unix.h"
-#include "sh_xfer.h"
-#include "sh_srp.h"
-#include "sh_fifo.h"
-#include "sh_tools.h"
-#include "sh_entropy.h"
-#include "sh_html.h"
-#include "sh_nmail.h"
-#include "sh_socket.h"
-#define SH_NEED_GETHOSTBYXXX
-#include "sh_static.h"
-
-#ifdef SH_ENCRYPT
-#include "rijndael-api-fst.h"
-char * sh_tools_makePack (unsigned char * header, int flag,
-			  char * payload, unsigned long payload_size,
-			  keyInstance * keyInstE);
-char * sh_tools_revertPack (unsigned char * header, int flag, char * message,
-			    keyInstance * keyInstE, 
-			    unsigned long message_size);
-#endif
-
-/* define this if you want to debug the client/server communication */
-/* #define SH_DBG_PROT 1 */
-
-#ifdef  SH_DBG_PROT
-#define SH_SHOWPROT(c,d) sh_tools_show_header((c), (d))
-#else
-#define SH_SHOWPROT(c,d) 
-#endif
-
-/* the port client will be connecting to 
- */
-#ifndef SH_DEFAULT_PORT
-#define SH_DEFAULT_PORT 49777    
-#endif
-
-#ifndef SH_SELECT_REPEAT
-#define SH_SELECT_REPEAT 60
-#endif
-
-#ifndef SH_HEADER_SIZE
-#define SH_HEADER_SIZE 7
-#endif
-
-#ifndef SH_CHALLENGE_SIZE
-#define SH_CHALLENGE_SIZE 9
-#endif
-
-#undef  FIL__
-#define FIL__  _("sh_xfer_client.c")
-
-extern int flag_err_debug;
-extern int flag_err_info;
-
-#ifndef SH_STANDALONE
-
-#if defined(WITH_TRACE) || defined(WITH_TPT) 
-char * hu_trans(const char * ihu)
-{
-  static char ohu[17];
-  sprintf(ohu, _("%c%03o"), '\\',                   /* known to fit  */
-	  (unsigned char) ihu[0]);
-  sprintf(&(ohu[4]), _("%c%03o"), '\\',             /* known to fit  */
-	  (unsigned char) ihu[1]);
-  sprintf(&(ohu[8]), _("%c%03o"), '\\',             /* known to fit  */
-	  (unsigned char) ihu[2]);
-  sprintf(&(ohu[12]), _("%c%03o"), '\\',            /* known to fit  */
-	  (unsigned char) ihu[3]);
-  ohu[16] = '\0';
-  return ohu;
-}
-#endif
-/* #ifndef SH_STANDALONE */
-#endif
-
-#if !defined(USE_SRP_PROTOCOL)
-void sh_passwd (char * salt, char * password, char * nounce, char *hash)
-{
-
-  char           *combi;
-  size_t          len;
-  unsigned char * tmp = NULL;
-  char hashbuf[KEYBUF_SIZE];
-
-  if (password == NULL)
-    {
-      tmp = (unsigned char *) &(skey->pw[0]);
-      memcpy(skey->vernam, tmp, PW_LEN);
-      sl_strlcpy (skey->vernam,
-		  sh_tiger_hash(skey->vernam, TIGER_DATA, PW_LEN,
-				hashbuf, sizeof(hashbuf)), 
-		  KEY_LEN+1);
-    }
-  else if (sl_strlen(password) < PW_LEN)
-    {
-      fprintf(stderr, _("Password has less than %d chars !\n"),
-		   PW_LEN);
-      _exit(EXIT_FAILURE);
-    }
-  else
-    {
-      sl_strlcpy (skey->vernam, password, KEY_LEN+1);
-    }
-
-  len = sl_strlen(salt) + 1;
-  if (sl_ok_adds(len, sl_strlen(skey->vernam)))
-    len += sl_strlen(skey->vernam);
-  if (nounce != NULL && sl_ok_adds(len, sl_strlen(nounce))) 
-    len += sl_strlen(nounce);
-  
-  /* H(s,P)
-   */
-  combi = SH_ALLOC(len);
-  (void) sl_strlcpy (combi, salt, len);
-  (void) sl_strlcat (combi, skey->vernam, len);
-  if (nounce != NULL)
-    (void) sl_strlcat (combi, nounce, len);
-  (void) sl_strlcpy (hash, 
-		     sh_tiger_hash(combi, TIGER_DATA, 
-				   (unsigned long) sl_strlen(combi),
-				   hashbuf, sizeof(hashbuf)),
-		     KEY_LEN+1);
-  SH_FREE (combi);
-  hash[KEY_LEN] = '\0';
-  return;
-}
-#endif
-
-#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
-
-/* Server addresses in use
- */
-static int count_dev_server = 0;
-
-void reset_count_dev_server(void)
-{
-  count_dev_server = 0;
-  return;
-}
-
-int sh_xfer_set_logserver (const char * address)
-{
-  SL_ENTER(_("sh_xfer_set_logserver"));
-
-  if (address != NULL && count_dev_server < 2 
-      && sl_strlen(address) < SH_PATHBUF && sl_strlen(address) > 0) 
-    {
-      if (count_dev_server == 0)
-	(void) sl_strlcpy (sh.srvexport.name, address, SH_PATHBUF);
-      else
-	(void) sl_strlcpy (sh.srvexport.alt,  address, SH_PATHBUF);
-
-      ++count_dev_server;
-      SL_RETURN (0, _("sh_xfer_set_logserver"));
-    }
-  SL_RETURN (-1, _("sh_xfer_set_logserver"));
-}
-
-static
-int xfer_send_intern (int mysocket, const int protocol, char * micro, 
-		      char * msgbuf, unsigned long length, int docrypt)
-{
-  unsigned long           numbytes, countbytes;
-  int                     flag_err = 0;
-  unsigned char           head[SH_HEADER_SIZE];
-  char                  * outbuf;
-#ifdef SH_ENCRYPT
-  char                  * msg2buf = NULL;
-#else
-  (void) docrypt;
-#endif
-
-  SL_ENTER(_("xfer_send_intern"));
-
-#ifdef SH_ENCRYPT
-  if  ((S_TRUE == docrypt) && ((protocol & SH_PROTO_ENC) != 0))
-    {
-      put_header (head, protocol, &length, micro);
-      msg2buf  = sh_tools_makePack (head, 0, msgbuf, length, 
-				    &(skey->keyInstE));
-      length   = (unsigned long) (256 * (unsigned int)head[1] + 
-				  (unsigned int)head[2]);
-      outbuf   = msg2buf;
-    }
-  else
-    {
-      outbuf = msgbuf;
-      put_header (head, protocol, &length, micro);
-    }
-#else
-  outbuf = msgbuf;
-  put_header (head, protocol, &length, micro);
-#endif
-
-  SH_SHOWPROT(head,'>');
-  
-  numbytes     = SH_HEADER_SIZE;
-  countbytes   = write_port (mysocket, (char *)head, numbytes, &flag_err, 300);
-
-  if (countbytes == numbytes && outbuf != NULL)
-    {
-      numbytes     = length;
-      countbytes   = write_port (mysocket, outbuf, numbytes, &flag_err, 300);
-    }
-
-#ifdef SH_ENCRYPT
-  if (msg2buf != NULL)
-    SH_FREE(msg2buf);
-#endif
-
-  if (countbytes == numbytes)
-    SL_RETURN( 0, _("xfer_send_intern"));
-  else
-    SL_RETURN( flag_err, _("xfer_send_intern"));
-}
-
-static
-int xfer_send (int mysocket, const int protocol, char * micro, 
-		     char * msgbuf, unsigned long length)
-{
-  int i;
-  SL_ENTER(_("xfer_send"));
-  TPT(( 0, FIL__, __LINE__, _("msg=<Send.>\n")));
-  i =  xfer_send_intern (mysocket, protocol, micro, 
-			       msgbuf, length, S_FALSE);
-  SL_RETURN(i, _("xfer_send"));
-}
-static
-int xfer_send_crypt (int mysocket, const int protocol, char * micro, 
-			   char * msgbuf, unsigned long length)
-{
-  int i;
-  SL_ENTER(_("xfer_send_crypt"));
-#ifdef SH_ENCRYPT
-  TPT(( 0, FIL__, __LINE__, _("msg=<Send encrypted.>\n")));
-#else
-  TPT(( 0, FIL__, __LINE__, _("msg=<Send.>\n")));
-#endif
-  i = xfer_send_intern (mysocket, protocol, micro, 
-			      msgbuf, length, S_TRUE);
-  SL_RETURN(i, _("xfer_send_crypt"));
-}
-
-
-/* receive answer, add a trailing NULL to terminate string
- * decrypt answer
- */
-static
-long xfer_receive_intern (int mysocket, const int protocol, char * micro,     
-			  char *  msgbuf, unsigned long length, 
-			  int docrypt)
-{
-  unsigned long numbytes, countbytes;
-  int           flag_err = -1;
-  unsigned char head[SH_HEADER_SIZE];
-#ifndef SH_ENCRYPT
-  (void) docrypt;
-#endif
-
-  SL_ENTER(_("xfer_receive_intern"));
-
-#ifdef SH_ENCRYPT
-  /* make sure length is not multiple of B_SIZ, see below 
-   */
-  ASSERT_RET((length % B_SIZ != 0), _("length % 16 != 0"), flag_err);
-#endif
-
-  if (micro != NULL)
-    micro[4]     = '\0';
-  if (msgbuf != NULL)
-    msgbuf[0]     = '\0';
-
-  numbytes     = SH_HEADER_SIZE;
-  countbytes   = read_port (mysocket, (char *)head, numbytes, &flag_err, 300);
-
-  if (countbytes != numbytes)
-    {
-      TPT(( 0, FIL__, __LINE__, _("msg=<countbytes != numbytes>\n")));
-      SL_RETURN(flag_err, _("xfer_receive_intern"));
-    }
-  else if (msgbuf == NULL)
-    {
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		      _("msgbuf is NULL"), _("xfer_receive_intern"));
-      SL_RETURN((-1), _("xfer_receive_intern"));
-    }
-  else if (head[0] != protocol && (head[0] & SH_PROTO_SRP) == 0)
-    {
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_MISMATCH);
-      SL_RETURN((-1), _("xfer_receive_intern"));
-    }
-  else
-    {
-      get_header (head, &numbytes, micro);
-      SH_SHOWPROT(head, '<');
-
-      if (numbytes > 0)
-	{
-	  numbytes = (numbytes > length ? length : numbytes);
-
-	  countbytes = read_port (mysocket, msgbuf, numbytes, &flag_err, 300);
-
-	  if (countbytes < length)
-	    msgbuf[countbytes] = '\0';
-	  else
-	    msgbuf[length-1] = '\0';
-
-	  if (flag_err != 0)
-	    {
-	      TPT(( 0, FIL__, __LINE__, _("msg=<read error>\n")));
-	      SL_RETURN((-1), _("xfer_receive_intern"));
-	    }
-	}
-    }
-
-#ifdef SH_ENCRYPT
-  if      ((S_TRUE == docrypt) && ((protocol & SH_PROTO_ENC) != 0))
-    {
-      unsigned long head_length;
-      char * tmp = SH_ALLOC((size_t)length);
-
-      memcpy(tmp, msgbuf, (size_t)length);
-      tmp = sh_tools_revertPack (head, 0, tmp, &(skey->keyInstD), countbytes);
-
-      head_length = (unsigned long) (256 * (unsigned int)head[1] + 
-				     (unsigned int)head[2]);
-
-      /* 
-       * revertPack returns header with length <= (original_length-16), so
-       * the following msgbuf[length] = '\0' is always safe.
-       * Nevertheless, check for proper length.
-       */
-      if (head_length <= (length-1))
-	length      = head_length;
-      else
-	--length;
-
-      memcpy(msgbuf, tmp, (size_t)length);
-      msgbuf[length] = '\0';
-      SH_FREE(tmp);
-      if (countbytes == numbytes) 
-	countbytes = length; /* to avoid error on return, see below */
-      numbytes = length;
-    }
-#endif
-
-  if (countbytes == numbytes)
-    SL_RETURN(((long)numbytes), _("xfer_receive_intern"));
-  else
-    {
-      TPT(( 0, FIL__, __LINE__, _("msg=<short read>\n")));
-      SL_RETURN(flag_err, _("xfer_receive_intern"));
-    }
-}
-
-static
-long xfer_receive (int mysocket, const int protocol, char * micro,     
-		   char * msgbuf, unsigned long length)
-{
-  long i;
-  SL_ENTER(_("xfer_receive"));
-  TPT(( 0, FIL__, __LINE__, _("msg=<Receive.>\n")));
-  i = xfer_receive_intern (mysocket, protocol, micro, 
-			   msgbuf, length, S_FALSE);
-  SL_RETURN(i, _("xfer_receive"));
-}
-
-static
-long xfer_receive_crypt (int mysocket, const int protocol, char * micro,     
-			 char * msgbuf, unsigned long length)
-{
-  long i;
-  SL_ENTER(_("xfer_receive_crypt"));
-#ifdef SH_ENCRYPT
-  TPT(( 0, FIL__, __LINE__, _("msg=<Receive encrypted.>\n")));
-#else
-  TPT(( 0, FIL__, __LINE__, _("msg=<Receive.>\n")));
-#endif
-  i = xfer_receive_intern (mysocket, protocol, micro, 
-			   msgbuf, length, S_TRUE);
-  SL_RETURN(i, _("xfer_receive_crypt"));
-}
-
-/**************************************************
- *
- *
- *  C L I E N T  
- *
- *
- ***************************************************/
-
-
-#include <time.h>
-
-static SH_FIFO * fifo = NULL;
-
-static long xfer_try_report (char * errmsg);
-
-unsigned int ServerPort = SH_DEFAULT_PORT;
-
-int sh_xfer_server_port (const char * str)
-{
-  unsigned long l;
-  char * endptr;
-
-  SL_ENTER(_("sh_xfer_server_port"));
-
-  l = strtoul (str, &endptr, 0);
-  if (l > 65535 || endptr == str)
-    {
-      SL_RETURN (-1, _("sh_xfer_server_port"));
-    }
-  ServerPort = (unsigned int) l;
-  SL_RETURN (0, _("sh_xfer_server_port"));
-}
-
-long sh_xfer_report (char * errmsg)
-{
-  static int have_server = S_TRUE;
-  long   status;
-  char * popmsg;
-  static int nofail = S_TRUE;
-
-  SL_ENTER(_("sh_xfer_report"));
-
-  /* --- No log server available. ---
-   */
-  if (have_server == S_TRUE && sh.srvexport.name[0] == '\0')
-    {
-      have_server = S_FALSE;
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NONAME);
-      SL_RETURN (-1, _("sh_xfer_report"));
-    }
-  else if (have_server == BAD)
-    {
-      SL_RETURN (-1, _("sh_xfer_report"));
-    }
-
-  /* --- Allocate fifo. ---
-   */
-  if (fifo == NULL)
-    {
-      fifo = SH_ALLOC(sizeof(SH_FIFO));
-      fifo_init(fifo);
-    }
-
-  /* --- Check for messages on the queue, and send them first. ---
-   */
-  while (NULL != (popmsg = pop_list(fifo)) )
-    {
-      status = xfer_try_report (popmsg);
-      if (status != 0)
-	{
-	  (void) push_tail_list (fifo, popmsg, 0, NULL); 
-	  SH_FREE(popmsg);
-	  if (SH_FIFO_MAX == push_list (fifo, errmsg, 0,NULL))
-	    SL_RETURN (-2, _("sh_xfer_report"));
-	  SL_RETURN (-1, _("sh_xfer_report"));
-	}
-      SH_FREE(popmsg);
-    }
-
-  /* --- Now send the error message. ---
-   */ 
-  status = xfer_try_report (errmsg);
-  if (status != 0)
-    {
-      if (nofail == S_TRUE)
-	sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_SRV_FAIL,
-			 _("log server"), sh.srvexport.name);
-      nofail = S_FALSE;
-      if (SH_FIFO_MAX == push_list (fifo, errmsg, 0, NULL))
-	SL_RETURN (-2, _("sh_xfer_report"));
-      SL_RETURN (-1, _("sh_xfer_report"));
-    }
-
-  nofail = S_TRUE;
-  SL_RETURN (0, _("sh_xfer_report"));  
-}
-
-static long xfer_try_report_int (char * errmsg, const int what);
-
-static long xfer_try_report (char * errmsg)
-{
-  long i;
-  SL_ENTER(_("xfer_try_report"));
-  i = xfer_try_report_int (errmsg, SH_PROTO_MSG);
-  SL_RETURN(i, _("xfer_try_report")); 
-}
-
-long sh_xfer_request_file (const char * file)
-{
-  long i;
-  char tmp_file[64];
-  SL_ENTER(_("sh_xfer_request_file"));
-  sl_strlcpy(tmp_file, file, sizeof(tmp_file));
-  i = xfer_try_report_int (tmp_file, SH_PROTO_BIG);
-  SL_RETURN(i, _("sh_xfer_request_file")); 
-}
-
-static unsigned long sh_throttle_delay = 0;
-
-int sh_xfer_set_throttle_delay (const char * c)
-{
-  long val;
-
-  SL_ENTER(_("sh_xfer_set_throttle_delay"));
-  val = strtol (c, (char **)NULL, 10);
-  if (val < 0)
-    SL_RETURN( (-1), _("sh_xfer_set_throttle_delay"));
-
-  val = (val > 1000) ? 1000 : val;
-
-  sh_throttle_delay = (unsigned long) val;
-  SL_RETURN( (0), _("sh_xfer_set_throttle_delay"));
-}
-
-static time_t xfer_timeout_val =  1;
-
-static int xfer_conn_state(int initialized, int conn_state)
-{
-  static time_t time_now  = 1200;
-  static time_t time_last =    0;
-
-  if (initialized == S_FALSE || conn_state == S_FALSE)
-    {
-      xfer_timeout_val = 
-	((xfer_timeout_val > TIMEOUT_CON) ? TIMEOUT_CON : xfer_timeout_val);
-
-      /* --- Retry bad attempt only after some time. ---
-       */
-      time_now  = time (NULL);
-      if ((time_now - time_last) < xfer_timeout_val) 
-	{
-	  TPT(( 0, FIL__, __LINE__, _("msg=<Within deadtime, no retry.>\n")));
-	  return -1;
-	}
-      TPT(( 0, FIL__, __LINE__, _("msg=<Retry.>\n")));
-    }
-  time_last  = time (NULL);
-  return 0;
-}
-
-static int xfer_connect(int * conn_state)
-{
-  char         error_msg[256];
-  char         error_call[SH_MINIBUF] = { 0 };
-  int          error_num = 0;
-
-  int sockfd = connect_port_2 (sh.srvexport.name, sh.srvexport.alt, ServerPort, 
-			       error_call, &error_num, error_msg, 256);
-
-  if (sockfd < 3)
-    {
-      *conn_state = S_FALSE;
-      xfer_timeout_val *= 2;
-      sh_error_handle ((-1), FIL__, __LINE__, error_num, 
-		       MSG_E_NET, error_msg, error_call,
-		       _("export"), sh.srvexport.name);
-      return -1;
-    }
-
-  *conn_state = S_TRUE;
-  return sockfd;
-}
-
-int xfer_greet_server(int sockfd, char * answer)
-{
-  int    flag_err;
-  char   head_u[5];
-  int    theProto = SH_PROTO_SRP;
-  
-  TPT(( 0, FIL__, __LINE__, _("msg=<c/r: entry>\n")));
-  
-  sl_strlcpy (answer, sh.host.name, 512);
-      
-  flag_err = xfer_send (sockfd, theProto, _("SALT"), 
-			answer,  (unsigned long)sl_strlen(answer));
-      
-  TPT(( 0, FIL__, __LINE__, _("msg=<c/r: sent SALT, flag_err = %d>\n"), 
-	flag_err));
-      
-  /* get nonce from server
-   */
-  if (flag_err == 0)
-    {
-      flag_err = xfer_receive (sockfd, (char)theProto, head_u, 
-				   answer,  511);
-      flag_err = (flag_err < 0) ? flag_err : 0;
-      TPT(( 0, FIL__, __LINE__, 
-	    _("msg=<c/r: rcvt nonce, flag_err = %d>\n"), 
-	    flag_err));
-    }
-  
-  if ( 0 != check_request (head_u, _("INIT")) )
-    {
-      TPT(( 0, FIL__, __LINE__, _("msg=<srp: u = %03o-%03o-%03o-%03o>\n"), head_u[0], head_u[1], head_u[2], head_u[3]));
-      flag_err = -1;
-    }
-  return flag_err;
-}
-  
-
-#if !defined(USE_SRP_PROTOCOL)
-
-static int xfer_auth(int is_reinit, int * initialized, 
-		     int sockfd, char * answer)
-{
-  /**************************************************
-   *
-   * --- challenge/response authentication ---
-   *
-   **************************************************/
-
-  int                  flag_err = 0;
-  int                  theProto = 0;
-  char   nounce[KEY_LEN+1];
-  char   temp[2*KEY_LEN+1];
-  char   nonce_u[KEY_LEN+1];
-  UINT32 ticks;
-
-  char   head_u[5];
-  char   foo_M1[KEY_LEN+1];
-  char   hashbuf[KEYBUF_SIZE];
-#ifdef SH_ENCRYPT
-  int err_num;
-  char expbuf[SH_ERRBUF_SIZE];
-#endif
-
-  SL_REQUIRE((sockfd > 2), _("sockfd > 2"));
-
-  if (is_reinit == S_FALSE)
-    flag_err = xfer_greet_server(sockfd, answer);
-  else
-    sh_tools_probe_reset();
-
-  /* entry point for jump from message forward if session key must
-   * be re-initialized
-   */	 
-
-  if ( flag_err == 0 && sl_strlen(answer) >  KEY_LEN )
-    (void) sl_strlcpy(nounce, &answer[KEY_LEN], KEY_LEN+1);
-  else
-    flag_err = (-1);
-  
-  TPT(( 0, FIL__, __LINE__, _("msg=<c/r: rcvt INIT, flag_err = %d>\n"), 
-	flag_err));
-  
-  /* verify random nonce v from server H(v, P)v
-   */
-  sh_passwd (nounce, NULL, NULL, temp);
-  if ( 0 != sl_ts_strncmp(temp, answer, KEY_LEN))
-    flag_err = (-1);
-  
-  TPT(( 0, FIL__, __LINE__, _("msg=<c/r: vrfy nonce, flag_err = %d>\n"), 
-	flag_err));
-  
-  
-  /* --- Create own nonce. ---
-   */
-  ticks = (UINT32) taus_get ();
-  
-  (void) sl_strlcpy(nonce_u, 
-		    sh_tiger_hash((char *) &ticks, 
-				  TIGER_DATA, 
-				  (unsigned long)sizeof(UINT32), 
-				  hashbuf, sizeof(hashbuf)),
-		    KEY_LEN+1);
-  
-  /* --- Form the message H(H(u,v),P)u ---
-   */
-  (void) sl_strlcpy(temp, nonce_u, 2*KEY_LEN+1); 
-  (void) sl_strlcat(temp,  nounce, 2*KEY_LEN+1); 
-  (void) sl_strlcpy(temp, 
-		    sh_tiger_hash(temp, 
-				  TIGER_DATA, 
-				  (unsigned long)sl_strlen(temp), 
-				  hashbuf, sizeof(hashbuf)),
-		    KEY_LEN+1);
-  sh_passwd (temp, NULL, NULL, foo_M1);
-  (void) sl_strlcpy(temp, foo_M1, 2*KEY_LEN+1);
-  (void) sl_strlcat(temp, nonce_u, 2*KEY_LEN+1);
-  
-  /* --- Send it to server. ---
-   */
-  if (flag_err == 0)
-    {
-      flag_err = xfer_send (sockfd, 
-			    (theProto|SH_PROTO_SRP), 
-			    _("PASS"), temp, 
-			    (unsigned long)sl_strlen(temp));
-      TPT(( 0, FIL__, __LINE__, _("msg=<c/r: sent PASS, flag_err = %d>\n"),
-	    flag_err));
-    }
-  
-  if (flag_err == 0)
-    {
-      flag_err = xfer_receive (sockfd,
-			       (theProto|SH_PROTO_SRP), 
-			       head_u, answer,  511);  
-      sh_passwd (nounce, NULL, nonce_u, foo_M1);
-      (void) sl_strlcpy (skey->session, foo_M1, KEY_LEN+1);
-#ifdef SH_ENCRYPT
-      err_num = rijndael_makeKey(&(skey->keyInstE), 
-				 (BYTE)DIR_ENCRYPT, 192, skey->session);
-      if (err_num < 0)
-	sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
-			errorExplain(err_num, expbuf, sizeof(expbuf)), 
-			_("xfer_try_report_int: makeKey"));
-      
-      err_num = rijndael_makeKey(&(skey->keyInstD), 
-				 (BYTE)DIR_DECRYPT, 192, skey->session);
-      if (err_num < 0)
-	sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
-			errorExplain(err_num, expbuf, sizeof(expbuf)), 
-			_("xfer_try_report_int: make_key"));
-#endif
-      *initialized = S_TRUE;
-    }
-  
-  if (*initialized == S_FALSE)
-    {
-      xfer_timeout_val *= 2;
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NOAUTH);
-      memset(answer, 0, 512);
-      MUNLOCK(answer, 512);
-      SH_FREE(answer);
-      return -1;
-    }
-  else
-    {
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_AUTH);
-    }
-  return 0;
-}    
-#else
-
-static void noise()
-{
-  UINT32 n = taus_get();
-  retry_msleep(0, (n & 0x0000007F));
-  return;
-}
-  
-
-static int xfer_auth(int is_reinit, int * initialized, 
-		     int sockfd, char * answer)
-{
-  /* This is the SRP authenticated key exchange protocol.
-   * Produces a session key skey->session.
-   */
-  
-  int                  flag_err = 0;
-  int                  theProto = 0;
-
-  char   head_u[5];
-  char   u_real[SH_CHALLENGE_SIZE];
-  char * foo_A;
-  char * foo_Sc;
-  char * M;
-  char   foo_M1[KEY_LEN+1];
-  char   hashbuf[KEYBUF_SIZE];
-#ifdef SH_ENCRYPT
-  int err_num;
-  char expbuf[SH_ERRBUF_SIZE];
-#endif
-
-  SL_REQUIRE((sockfd > 2), _("sockfd > 2"));
-
-  if (is_reinit == S_FALSE)
-    flag_err = xfer_greet_server(sockfd, answer);
-  else
-    sh_tools_probe_reset();
-
-  /* Entry point for jump from message forward if session key must
-   * be re-initialized.
-   */	 
-  TPT(( 0, FIL__, __LINE__, _("msg=<srp: INIT>\n")));
-  
-  if ( flag_err == 0 )
-    {
-      if (0 != sh_srp_init())
-	sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_EBGN);
-      else 
-	{
-	  TPT(( 0, FIL__, __LINE__, _("msg=<srp: bignum initialized>\n")));
-
-	  sh_srp_x (answer, NULL);  /* x        password      */
-	  sh_srp_make_a ();         /* a        random number */
-	  foo_A = sh_srp_A();       /* g^a                    */
-
-	  TPT(( 0, FIL__, __LINE__, _("msg=<srp: A = %s>\n"), foo_A));
-
-	  if (foo_A == NULL)
-	    flag_err = (-1);
-
-	  noise();
-
-	  if (flag_err == 0)
-	    flag_err = xfer_send (sockfd, 
-				  (theProto|SH_PROTO_SRP), 
-				  _("PC01"),
-				  foo_A, sl_strlen(foo_A)+1); 
-	  if (flag_err == 0)
-	    {
-	      flag_err = xfer_receive (sockfd, 
-				       (theProto|SH_PROTO_SRP),
-				       head_u,
-				       answer, 511);
-	      flag_err = (flag_err < 0) ? flag_err : 0;
-	      TPT(( 0, FIL__, __LINE__, _("msg=<srp: B = %s>\n"), answer));
-	      TPT(( 0, FIL__, __LINE__, _("msg=<srp: u = %03o-%03o-%03o-%03o>\n"), head_u[0], head_u[1], head_u[2], head_u[3]));
-	    }
-
-	  /*                     u        nounce        */
-	  /*                     B        answer        */
-	  /*                     S = (B-g^x)^(a+ux)     */
-	  
-    
-	  if (flag_err == 0)
-	    { 
-	      noise();
-
-	      if (0 != sh_srp_check_zero (answer))
-		sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_EZERO);
-	      else 
-		{
-		  sl_strlcpy(u_real, sh_tiger_hash(head_u, TIGER_DATA, 4, 
-						   hashbuf, sizeof(hashbuf)), 
-			     SH_CHALLENGE_SIZE);
-		  foo_Sc = sh_srp_S_c (u_real, answer);
-		  
-		  TPT(( 0, FIL__, __LINE__, _("msg=<srp: U = %s>\n"), 
-			u_real));
-		  TPT(( 0, FIL__, __LINE__, _("msg=<srp:Sc = %s>\n"), 
-			foo_Sc));
-		  
-		  /* --- Now send H(A,B,H(Sc)) and check. --- 
-		   */
-		  if (foo_Sc != NULL && 0 == sh_srp_check_zero (foo_Sc))
-		    {
-		      sh_srp_M(foo_A, 
-			       answer, 
-			       sh_tiger_hash(foo_Sc, 
-					     TIGER_DATA, 
-					     sl_strlen(foo_Sc), 
-					     hashbuf, sizeof(hashbuf)),
-			       foo_M1, KEY_LEN+1);
-		      
-		      
-		      TPT(( 0, FIL__, __LINE__, _("msg=<srp:M1 = %s>\n"), 
-			    foo_M1));
-		      
-		      flag_err = xfer_send(sockfd, 
-					   (theProto|SH_PROTO_SRP), 
-					   _("PC02"),
-					   foo_M1, KEY_LEN+1);
-		    }
-		  else
-		    flag_err = (-1);
-		  
-		  if (flag_err == 0)
-		    {
-		      flag_err = xfer_receive(sockfd, 
-					      (theProto|SH_PROTO_SRP),
-					      head_u, 
-					      answer, 511);
-		      flag_err = (flag_err < 0) ? flag_err : 0;
-		      TPT(( 0, FIL__, __LINE__, _("msg=<srp: M = %s>\n"), 
-			    answer));
-		    }
-		  
-		  if (flag_err == 0   && 0 == check_request (head_u, _("PARP")) )
-		    {
-		      /* ------  verify M2 = H(A, M1, K) --------
-		       */
-		      char M_buf[KEY_LEN+1];
-		      M = sh_srp_M (foo_A, foo_M1,
-				    sh_tiger_hash(foo_Sc,
-						  TIGER_DATA,
-						  sl_strlen(foo_Sc), 
-						  hashbuf, sizeof(hashbuf)),
-				    M_buf, sizeof(M_buf)
-				    );
-		      if (M != NULL && 
-			  0 == sl_ts_strncmp (answer, M, KEY_LEN+1))
-			{
-			  sl_strlcpy (skey->session, 
-				      sh_tiger_hash(foo_Sc, 
-						    TIGER_DATA,
-						    sl_strlen(foo_Sc), 
-						    hashbuf, sizeof(hashbuf)),
-				      KEY_LEN+1);
-			  TPT(( 0, FIL__, __LINE__, 
-				_("msg=<srp: Key = %s>\n"), 
-				skey->session));
-
-#ifdef SH_ENCRYPT
-			  err_num = rijndael_makeKey(&(skey->keyInstE), 
-						     DIR_ENCRYPT, 
-						     192, skey->session);
-			  if (err_num < 0)
-			    sh_error_handle((-1), FIL__, __LINE__, -1, 
-					    MSG_E_SUBGEN,
-					    errorExplain(err_num, expbuf, sizeof(expbuf)), 
-					    _("xfer_try_report_int: makeKey"));
-			  err_num = rijndael_makeKey(&(skey->keyInstD), 
-						     DIR_DECRYPT, 
-						     192, skey->session);
-			  if (err_num < 0)
-			    sh_error_handle((-1), FIL__, __LINE__, -1, 
-					    MSG_E_SUBGEN,
-					    errorExplain(err_num, expbuf, sizeof(expbuf)), 
-					    _("xfer_try_report_int: makeKey"));
-#endif
-			  *initialized = S_TRUE;
-			  noise();
-			}
-		    }
-		  if (foo_Sc != NULL)
-		    SH_FREE(foo_Sc);
-		}
-	    }
-	  if (foo_A != NULL)
-	    SH_FREE(foo_A);
-	  sh_srp_exit();
-	}
-    }
-
-  if (*initialized == S_FALSE)
-    {
-      xfer_timeout_val *= 2;
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NOAUTH);
-      memset(answer, 0, 512);
-      MUNLOCK(answer, 512);
-      SH_FREE(answer);
-      return -1;
-    }
-  else
-    {
-      if (flag_err_info == S_TRUE)
-	sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_AUTH);
-    }
-  return 0;
-}
-#endif
-
-int xfer_check_server_cmd(char * answer, char * buffer)
-{
-  int    flag_err;
-  size_t pos;
-  char   sigbuf[KEYBUF_SIZE];
-
-  /* --- SERVER CMD --- */
-  if (answer[KEY_LEN] != '\0' && 
-      sl_strlen(answer) > (2*KEY_LEN))
-    {
-      pos = sl_strlen(answer) - (2*KEY_LEN);
-      /*
-       * buffer is  >= 256
-       * answer has <= 255 bytes
-       */
-      (void) sl_strlcpy(buffer, &answer[KEY_LEN], 
-			pos+1);
-      flag_err = 
-	sl_ts_strncmp(&answer[KEY_LEN+pos],
-		   sh_util_siggen(skey->session, 
-				  buffer,
-				  pos,
-				  sigbuf, sizeof(sigbuf)),
-		   KEY_LEN);
-      
-      TPT((0, FIL__, __LINE__, 
-	   _("CONF RECV <%d> <%s>\n"),
-	   flag_err, &answer[KEY_LEN]));
-      
-      if (flag_err != 0) {
-	sh_error_handle((-1), FIL__, __LINE__, 
-			flag_err,
-			MSG_TCP_NOCONF);
-      } 
-#ifdef SH_WITH_CLIENT
-      else {
-	sh_socket_server_cmd(buffer);
-      }
-#endif
-      flag_err = 0;
-      
-    } else {
-    
-    TPT((0, FIL__, __LINE__, 
-	 _("CONF RECV <0> <[null]>\n")));
-    
-  }
-  /* --- SERVER CMD END --- */
-  return 0;
-}
-
-
-
-int xfer_send_message(char * errmsg, int sockfd, char * answer)
-{
-  char   hash[KEY_LEN+1];
-  size_t len;
-  char * buffer;
-  char   nsrv[KEY_LEN+1];
-  char   sigbuf[KEYBUF_SIZE];
-  char   head_u[5];
-  int    flag_err;
-
-  SL_REQUIRE((sockfd > 2), _("sockfd > 2"));
-
-  /* --- Save the challenge. ---  
-   */
-  (void) sl_strlcpy(nsrv, answer, KEY_LEN + 1);
-  
-  /* --- Hash(msg,challenge,sessionkey). ---  
-   */
-  len    = sl_strlen(errmsg) + sl_strlen(answer) 
-    + KEY_LEN + 1;
-  len = (size_t)((len < 256) ? 256 : len);
-  buffer = SH_ALLOC(len);
-  MLOCK(buffer, len);
-  (void) sl_strlcpy(buffer, errmsg, len);
-  (void) sl_strlcat(buffer, answer, len);
-  (void) sl_strlcpy(hash, 
-		    sh_util_siggen (skey->session, 
-				    buffer, 
-				    sl_strlen(buffer),
-				    sigbuf, sizeof(sigbuf)), 
-		    KEY_LEN+1);
-  TPT((0, FIL__, __LINE__, _("msg=<sign %s.>\n"),
-       sh_util_siggen(skey->session, buffer, 
-		      sl_strlen(buffer), sigbuf, sizeof(sigbuf))));
-  
-  (void) sl_strlcpy(buffer, errmsg, len);
-  (void) sl_strlcat(buffer, hash,   len);
-  
-  flag_err = 
-    xfer_send_crypt (sockfd, 
-#ifdef SH_ENCRYPT
-		     (char)(SH_PROTO_MSG|SH_PROTO_ENC),
-#else
-		     (char)(SH_PROTO_MSG),
-#endif
-		     _("MESG"),
-		     buffer, 
-		     (unsigned long)(sl_strlen(buffer)+1));
-  TPT(( 0, FIL__, __LINE__, 
-	_("msg=<Sent %s, status %d.>\n"), 
-	answer, flag_err));
-
-  /* --- Get confirmation. ---
-   */
-  if (flag_err == 0)
-    {
-      flag_err = (int)
-	xfer_receive_crypt (sockfd, 
-#ifdef SH_ENCRYPT
-			    (char)(SH_PROTO_MSG|SH_PROTO_ENC|SH_PROTO_END),
-#else
-			    (char)(SH_PROTO_MSG|SH_PROTO_END),
-#endif
-			    head_u, 
-			    answer, 255);   
-      TPT(( 0, FIL__, __LINE__, 
-	    _("msg=<Rcvt %s, u %s, status %d.>\n"), 
-	    answer, hu_trans(head_u), flag_err));
-      flag_err = (flag_err < 0) ? flag_err : 0;
-    }
-
-  
-  /* --- Check confirmation. ---
-   */
-  if (flag_err == 0)
-    {
-      /*   CLIENT CONF RECV
-       * 
-       *   first KEY_LEN bytes must be
-       *   sig(skey->session (errmsg nsrv))
-       *
-       */
-      (void) sl_strlcpy(buffer, errmsg, len);
-      (void) sl_strlcat(buffer, nsrv,   len);
-      flag_err = sl_ts_strncmp(answer,
-			       sh_util_siggen(skey->session, 
-					      buffer,
-					      sl_strlen(buffer),
-					      sigbuf, sizeof(sigbuf)),
-			       KEY_LEN);
-      TPT((0, FIL__, __LINE__, _("msg=<sign %s.>\n"),
-	   sh_util_siggen(skey->session, buffer, 
-			  sl_strlen(buffer), sigbuf, sizeof(sigbuf))));
-      
-      if (flag_err != 0)
-	{
-#ifdef ENOMSG
-	  flag_err = ENOMSG;
-#else
-	  flag_err = EIO;
-#endif
-	  sh_error_handle((-1), FIL__, __LINE__, flag_err,
-			  MSG_TCP_NOCONF);
-	}
-      else
-	{
-#ifdef SH_ENCRYPT
-	  flag_err = xfer_check_server_cmd(answer, buffer);
-#endif
-	  if (flag_err_debug == S_TRUE)
-	    sh_error_handle((-1), FIL__, __LINE__, 0,
-			    MSG_TCP_CONF);
-	}
-    }
-  
-  memset(buffer, 0, len);
-  MUNLOCK(buffer, len);
-  SH_FREE(buffer);
-
-  if (flag_err != 0)
-    return -1;
-  return 0;
-}
-
-
-static SL_TICKET xfer_get_file(int sockfd, char * answer, 
-			       char * nclt, char * foo_M1, const int theProto)
-{
-  /* --- Open a temporary file. ---
-   */
-  int flag_err = 0;
-  SL_TICKET sfd;
-
-  SL_REQUIRE((sockfd > 2), _("sockfd > 2"));
-
-  if ( (sfd = open_tmp ()) < 0)
-    {
-      flag_err = (-1);
-      sh_error_handle((-1), FIL__, __LINE__, flag_err, MSG_TCP_EFIL);
-    }
-  else
-    {
-      /* --- Read from socket into tmp file. ---
-       */
-      int    transfercount = 0;
-      char   head_u[5];
-
-      do {
-	flag_err = (int)
-	  xfer_receive_crypt (sockfd, 
-#ifdef SH_ENCRYPT
-			      (char)(SH_PROTO_BIG|SH_PROTO_ENC),
-#else
-			      (char)(SH_PROTO_BIG),
-#endif
-			      head_u, 
-			      answer, 
-			      TRANS_BYTES + 255);
-	
-	TPT(( 0, FIL__, __LINE__, 
-	      _("msg=<Received: %d bytes, marked %s.>\n"),
-	      flag_err, hu_trans(head_u)));
-
-	if (flag_err > 0 && 0 == check_request_nerr(head_u, _("FILE")))
-	  {
-	    if (0 == hash_check (foo_M1, answer, flag_err))
-	      {
-		(void) sl_write(sfd, &answer[KEY_LEN], 
-				flag_err-KEY_LEN);
-		++transfercount;
-
-		/*  Delay for throughput throttling
-		 */
-		if (sh_throttle_delay > 0)
-		  retry_msleep(sh_throttle_delay/1000, sh_throttle_delay % 1000);
-
-		flag_err = xfer_send_crypt (sockfd, theProto, 
-					    _("RECV"),
-					    nclt, 
-					    (unsigned long)sl_strlen(nclt));
-		
-	      }
-	    else
-	      {
-		TPT(( 0, FIL__, __LINE__, 
-		      _("msg=<File transfer: Hash check failed.>\n")));
-		break;
-	      }
-	  }
-	else
-	  {
-	    TPT(( 0, FIL__, __LINE__, 
-		  _("msg=<File transfer: No more data.>\n")));
-	    break;
-	  }
-      } while (transfercount < 32000); /* 64 Mbyte */
-      
-      if (0 == check_request_nerr(head_u, _("EEOT")) &&
-	  0 <  flag_err                              &&
-	  0 == hash_check (foo_M1, answer, (int)sl_strlen(answer)))
-	{
-	  flag_err = xfer_send_crypt (sockfd, theProto, 
-				      _("EOTE"),
-				      nclt, 
-				      (unsigned int) sl_strlen(nclt));
-	  
-	  (void) rewind_tmp (sfd);
-	  (void) sl_sync(sfd);
-	  if (flag_err_info == S_TRUE)
-	    sh_error_handle((-1), FIL__, __LINE__, flag_err, MSG_TCP_FOK);
-	}
-      else
-	{
-	  (void) sl_close (sfd);
-	  sfd = (-1);
-	}
-    }
-
-  return sfd;
-}
-
-static  long xfer_try_report_int (char * errmsg, const int what)
-{
-  static int           initialized = S_FALSE;
-  static int           conn_state  = S_TRUE;
-  int                  sockfd;
-  int                  flag_err = 0;
-  char               * answer;
-  int                  theProto = 0;
-
-  UINT32 ticks;
-  char   head_u[5];
-  char * buffer;
-  char   nsrv[KEY_LEN+1];
-  char   nclt[KEY_LEN+1];
-  char   foo_M1[KEY_LEN+1];
-
-  char hashbuf[KEYBUF_SIZE];
-  char sigbuf[KEYBUF_SIZE];
-
-  SL_ENTER(_("xfer_try_report_int"));
-
-  /* --- No message to transmit. ---
-   */
-  if (errmsg == NULL && initialized == S_TRUE)
-    SL_RETURN( 0, _("xfer_try_report_int"));
-  
-  /* --- Connection in bad state. ---
-   */
-  if (xfer_conn_state(initialized, conn_state) < 0)
-    SL_RETURN( (-1), _("xfer_try_report_int"));
-
-  /* --- Try to connect to log server. ---
-   */
-  sockfd = xfer_connect(&conn_state);
-  if (sockfd < 0)
-    SL_RETURN( (-1), _("xfer_try_report_int"));
-  
-
-  /*************************
-   *
-   *  initialization
-   * 
-   */
-  flag_err = 0;
-  answer   = SH_ALLOC(512);
-  MLOCK(answer, 512);
-
-  if (initialized == S_FALSE)
-    {
-      if (xfer_auth(S_FALSE, &initialized, sockfd, answer) < 0)
-	SL_RETURN( (-1), _("xfer_try_report_int"));
-    }
-
- retry_send:
-
-  /* no message, just session key negotiated
-   */
-  if (errmsg == NULL)
-    {
-      xfer_timeout_val = 1;
-      memset(answer, 0, 512);
-      MUNLOCK(answer, 512);
-      SH_FREE(answer);
-      TPT(( 0, FIL__, __LINE__, _("msg=<No message.>\n")));
-      SL_RETURN( (0), _("xfer_try_report_int"));
-    }
-  else if (what == SH_PROTO_BIG)
-    {
-      MUNLOCK(answer, 512);
-      SH_FREE (answer);
-      answer   = SH_ALLOC(TRANS_BYTES + 256);
-      MLOCK(answer, TRANS_BYTES + 256);
-      TPT(( 0, FIL__, __LINE__, _("msg=<File transfer.>\n")));
-    }
-
-  sl_strlcpy (answer, 
-	      sh_util_siggen(skey->session,
-			     sh.host.name,
-			     sl_strlen(sh.host.name),
-			     sigbuf, sizeof(sigbuf)), 
-	      KEY_LEN+1);
-
-  TPT((0, FIL__, __LINE__, _("msg=<host %s>\n"), sh.host.name));
-  TPT((0, FIL__, __LINE__, _("msg=<ckey %s>\n"), skey->session));
-  TPT((0, FIL__, __LINE__, _("msg=<sign %s>\n"), answer));
-    
-  sl_strlcat (answer, sh.host.name, 512);
-
-  TPT((0, FIL__, __LINE__, _("msg=<mesg %s>\n"), answer));
-
-  /***********************************************
-   *
-   * send the message
-   *
-   */
-
-  if (what == SH_PROTO_MSG)
-    theProto = SH_PROTO_MSG;
-  else if (what == SH_PROTO_BIG)
-    theProto = SH_PROTO_BIG;
-
-  /* --- Say HELO  ---       
-   */
-  flag_err = xfer_send    (sockfd, theProto, _("HELO"),
-			   answer, sl_strlen(answer));
-  TPT(( 0, FIL__, __LINE__, _("msg=<Sent %s, status %d.>\n"), 
-	answer, flag_err));
-
-  if (flag_err == 0)
-    { 
-      /* --- Get NSRV. ---  
-       */
-      flag_err = (int) xfer_receive (sockfd, theProto, head_u, answer, 255);
-      TPT(( 0, FIL__, __LINE__, _("msg=<Rcvt %s, u %s, status %d.>\n"), 
-	    answer, hu_trans(head_u), flag_err));
-      flag_err = (flag_err < 0) ? flag_err : 0;
-    }   
-
-  if (what == SH_PROTO_MSG)
-    {
-      if (flag_err == 0)
-	{
-	  /* --- Re-negotiate key. ---
-	   */
-	  if (0 == check_request_nerr(head_u, _("INIT")))
-	    {
-	      flag_err    = 0;
-	      initialized = S_FALSE;
-	      if (xfer_auth(S_TRUE, &initialized, sockfd, answer) == 0)
-		goto retry_send;
-	    }
-	  
-	  else if (0 == check_request(head_u, _("TALK")))
-	    {
-	      flag_err = xfer_send_message(errmsg, sockfd, answer);
-	    }
-
-	  else
-	    {
-	      /* --- Unexpected reply from server. ---
-	       */
-	      sh_error_handle((-1), FIL__, __LINE__, 0,
-			      MSG_TCP_UNEXP);
-	      flag_err = (-1);
-		
-	    }
-	}
-    }
-
-
-  else if (what == SH_PROTO_BIG)
-    {
-      if (flag_err == 0)
-	{
-	  
-	  /* --- Re-negotiate key. ---
-	   */
-	  if (0 == check_request_nerr(head_u, _("INIT")))
-	    {
-	      flag_err    = 0;
-	      initialized = BAD;
-	      if (xfer_auth(S_TRUE, &initialized, sockfd, answer) == 0)
-		goto retry_send;
-	    }
-	  
- 
-	  else if (0 == check_request(head_u, _("NSRV")))
-	    {
-	      size_t buffersize;
-#ifdef SH_ENCRYPT
-	      /* --- Set encryption flag. ---
-	       */
-	      theProto = (SH_PROTO_BIG|SH_PROTO_ENC);
-#endif
-
-	      (void) sl_strlcpy(nsrv, answer, KEY_LEN+1);
-	      
-	      /* --- Generate a nonce. ---
-	       */
-	      ticks = (UINT32) taus_get ();
-              
-	      (void) sl_strlcpy(nclt, 
-				sh_tiger_hash((char *) &ticks, 
-					      TIGER_DATA, 
-					      (unsigned long)sizeof(UINT32), 
-					      hashbuf, sizeof(hashbuf)),
-				KEY_LEN+1);
-
-	      /* --- Compute H(nsrv, nclt, skey). ---
-	       */
-	      buffer = sh_util_strconcat (nsrv, nclt, 
-					  skey->session, NULL);
-	      (void)sl_strlcpy(foo_M1, 
-			       sh_tiger_hash(buffer, TIGER_DATA,
-					     (unsigned long)sl_strlen(buffer), 
-					     hashbuf, sizeof(hashbuf)),
-			       KEY_LEN+1);
-	      memset (buffer, 0, sl_strlen(buffer));
-	      SH_FREE(buffer);
-
-	      /* --- Send (nclt, msg) ---
-	       */
-	      if (S_TRUE == sl_ok_adds(strlen(errmsg), strlen(nclt)+2+KEY_LEN))
-		{
-		  buffersize = strlen(nclt)+strlen(errmsg)+2;
-		  
-#if !defined(SH_ENCRYPT)
-		  buffersize += KEY_LEN;
-#endif
-		  buffer = SH_ALLOC(buffersize);
-
-		  sl_strlcpy(buffer, nclt,   buffersize);
-		  sl_strlcat(buffer, errmsg, buffersize);
-		  
-#if !defined(SH_ENCRYPT)
-		  if (4 == sl_strlen(errmsg)) {  /* backward compatibility   */
-		    buffersize = sl_strlen(buffer);
-		    buffer[buffersize]   = theProto; /* nctl//DATA//theProto */
-		    buffer[buffersize+1] = '\0';
-		  }
-		  sh_tools_hash_add(foo_M1, buffer, buffersize+1);
-#endif
-	      
-		  flag_err = 
-		    xfer_send_crypt (sockfd, (char) theProto, _("NCLT"),
-				     buffer, 
-				     (unsigned long) sl_strlen(buffer));
-		  
-		  TPT(( 0, FIL__, __LINE__, _("msg=<Sent %s, status %d.>\n"), 
-			buffer, flag_err));
-		  SH_FREE (buffer);
-		}
-	      else {
-		flag_err = -1;
-	      }
-	    } 
-	}
-
-      if (flag_err == 0)
-	{
-	  /* --- Receive the file. ---
-	   */
-	  SL_TICKET sfd = xfer_get_file(sockfd, answer, nclt, foo_M1, theProto);
-	  if (!SL_ISERROR(sfd))
-	    {
-	      (void) sl_close_fd (FIL__, __LINE__, sockfd);
-	      memset(answer, 0, TRANS_BYTES + 256);
-	      MUNLOCK(answer, TRANS_BYTES + 256);
-	      SH_FREE(answer);
-	      xfer_timeout_val = 1;
-
-	      SL_RETURN( (sfd), _("xfer_try_report_int"));
-	    }
-	}
-
-      (void) sl_close_fd (FIL__, __LINE__, sockfd);
-      memset(answer, 0, TRANS_BYTES + 256);
-      MUNLOCK(answer, TRANS_BYTES + 256);
-      SH_FREE(answer);
-      xfer_timeout_val *= 2;
-
-      SL_RETURN( (-1), _("xfer_try_report_int"));
-    }
- 
-  (void) sl_close_fd (FIL__, __LINE__, sockfd);
-  memset(answer, 0, 512);
-  MUNLOCK(answer, 512);
-  SH_FREE(answer);
-
-#ifndef EIO
-#define EIO 5
-#endif
-  
-
-#ifdef SH_ERROR_H  
-  if (flag_err != 0)
-    {
-      char errbuf[SH_ERRBUF_SIZE];
-      conn_state = S_FALSE;
-      xfer_timeout_val *= 2;
-      if (flag_err < 0 || NULL == sh_error_message(flag_err, errbuf, sizeof(errbuf)))
-	flag_err = EIO;
-      sh_error_handle((-1), FIL__, __LINE__, flag_err, MSG_TCP_ECONN,
-		      sh_error_message(flag_err, errbuf, sizeof(errbuf)));
-      SL_RETURN( (-1), _("xfer_try_report_int"));
-    }
-#endif
-  xfer_timeout_val = 1;
-
-  SL_RETURN( (0), _("xfer_try_report_int"));
-}
-
-/* #ifdef SH_WITH_CLIENT */
-#endif
-
-
-
-
Index: trunk/src/sh_xfer_server.c
===================================================================
--- trunk/src/sh_xfer_server.c	(revision 591)
+++ 	(revision )
@@ -1,3876 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 1999, 2000 Rainer Wichmann                                */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-#include "config_xor.h"
-
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <ctype.h>
-
-/* Must be early on FreeBSD
- */
-#include <sys/types.h>
-
-/* must be .le. than (1020 * 64)
- * (see sh_tools.c -- put_header)
- *
- * also: must be  (N * 16), otherwise
- * binary files cannot be transferred encrypted
- *
- * 65280 = (1020*64)
- * #define TRANS_BYTES 8000  V0.8
- */
-#ifdef  SH_ENCRYPT
-#define TRANS_BYTES 65120
-#else
-#define TRANS_BYTES 65280
-#endif
-
-/* timeout for session key
- */
-#define TIMEOUT_KEY 7200
-
-/* max time between connection attempts
- */
-#define TIMEOUT_CON 2048 
-
-/* #undef  SRP_DEBUG */
-/* #define SRP_DEBUG */
-
-#ifdef HAVE_MEMORY_H
-#include <memory.h>
-#endif
-
-#if HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
-#include <time.h>
-
-#ifdef HAVE_SYS_SELECT_H
-#include <sys/select.h>
-#endif
-
-#ifdef  HAVE_UNISTD_H
-#include <errno.h>
-#include <signal.h>
-#include <setjmp.h>
-#include <pwd.h>
-#include <grp.h>
-#include <sys/stat.h>
-#include <sys/resource.h>
-#include <fcntl.h>
-#include <sys/wait.h>
-#include <unistd.h>
-#endif
-
-#ifndef FD_SET
-#define NFDBITS         32
-#define FD_SET(n, p)    ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
-#define FD_CLR(n, p)    ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
-#define FD_ISSET(n, p)  ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
-#endif /* !FD_SET */
-#ifndef FD_SETSIZE
-#define FD_SETSIZE      32
-#endif
-#ifndef FD_ZERO
-#define FD_ZERO(p)      memset((char *)(p), 0, sizeof(*(p)))
-#endif
-
-#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
-#include <sys/mman.h>
-#endif
-
-
-#include <netdb.h> 
-#include <sys/types.h> 
-#include <netinet/in.h> 
-#include <sys/socket.h> 
-#ifndef S_SPLINT_S
-#include <arpa/inet.h>
-#endif
-
-#include "sh_ipvx.h"
-#include "samhain.h"
-#include "sh_tiger.h"
-#include "sh_utils.h"
-#include "sh_unix.h"
-#include "sh_xfer.h"
-#include "sh_srp.h"
-#include "sh_fifo.h"
-#include "sh_tools.h"
-#include "sh_entropy.h"
-#include "sh_html.h"
-#include "sh_nmail.h"
-#include "sh_socket.h"
-#define SH_NEED_GETHOSTBYXXX
-#include "sh_static.h"
-#include "sh_guid.h"
-
-#ifdef SH_ENCRYPT
-#include "rijndael-api-fst.h"
-char * sh_tools_makePack (unsigned char * header, int flag,
-			  char * payload, unsigned long payload_size,
-			  keyInstance * keyInstE);
-char * sh_tools_revertPack (unsigned char * header, int flag, char * message,
-			    keyInstance * keyInstE, 
-			    unsigned long message_size);
-#endif
-
-/* define this if you want to debug the client/server communication */
-/* #define SH_DBG_PROT 1 */
-
-#ifdef  SH_DBG_PROT
-#define SH_SHOWPROT(c,d) sh_tools_show_header((c), (d))
-#else
-#define SH_SHOWPROT(c,d) 
-#endif
-
-/* the port client will be connecting to 
- */
-#ifndef SH_DEFAULT_PORT
-#define SH_DEFAULT_PORT 49777    
-#endif
-
-#ifndef SH_SELECT_REPEAT
-#define SH_SELECT_REPEAT 60
-#endif
-
-#ifndef SH_HEADER_SIZE
-#define SH_HEADER_SIZE 7
-#endif
-
-#ifndef SH_CHALLENGE_SIZE
-#define SH_CHALLENGE_SIZE 9
-#endif
-
-#undef  FIL__
-#define FIL__  _("sh_xfer_server.c")
-
-int     clt_class = (-1);
-
-extern int flag_err_debug;
-extern int flag_err_info;
-
-
-#if defined (SH_WITH_SERVER)
-
-#if defined(WITH_TRACE) || defined(WITH_TPT) 
-extern char * hu_trans(const char * ihu);
-#endif
-extern unsigned int ServerPort;
-#if !defined(USE_SRP_PROTOCOL)
-extern void sh_passwd (char * salt, char * password, char * nounce, char *hash);
-#endif
-
-static int StripDomain = S_TRUE;
-
-int sh_xfer_set_strip (const char * str)
-{
-  static int fromcl = 0;
-
-  if (fromcl == 1)
-    return 0;
-  else
-    return (sh_util_flagval(str, &StripDomain));
-}
-
-static char * sh_strip_domain (char *name)
-{
-  char * out = NULL;
-
-  SL_ENTER(_("sh_strip_domain"));
-
-  if (StripDomain == S_FALSE || strchr(name, '.') == NULL) 
-    {
-      out = sh_util_strdup(name);
-      SL_RETURN( out, _("sh_strip_domain"));
-    }
-  else
-    {
-      /* check whether it is in dotted number format
-       * --> last part must be kept
-       */
-      if (0 != sh_ipvx_is_numeric(name))
-	{
-	  out = sh_util_strdup(name);
-	  SL_RETURN( out, _("sh_strip_domain"));
-	}
-      else
-	{
-	  char * p;
-	  out = sh_util_strdup(name);
-	  p   = strchr(out, '.');
-	  if (p) *p = '\0';
-	  SL_RETURN( out, _("sh_strip_domain"));
-	}
-    }
-
-  SL_RETURN( out, _("sh_strip_domain"));
-}
-
-#ifndef USE_SRP_PROTOCOL
-
-int sh_xfer_make_client (const char * str)
-{
-  /* char *          safer; */
-  char            key[KEY_LEN+1];
-  unsigned char   in[PW_LEN+1];
-  int    i = 0, j, k, l = 0;
-  char hashbuf[KEYBUF_SIZE];
-  
-  if (sl_strlen(str) != (PW_LEN * 2)) 
-    {
-      fprintf(stderr, 
-	      _("Input must be a %d digit hexadecimal number"\
-		" (only 0-9, a-f, A-F allowed in input)\n"),
-	      (PW_LEN * 2));
-      _exit(EXIT_FAILURE);
-    }
-  
-  while (i < (PW_LEN * 2))
-    {
-      k = sh_util_hexchar(str[i]); j = sh_util_hexchar(str[i+1]); 
-      if (k != -1 && j != -1) 
-        {
-          in[l] = (k * 16 + j);
-          ++l; i+= 2;
-        }
-      else
-        {
-          fprintf(stderr, _("Invalid char %c\n"), str[i]);
-          _exit(EXIT_FAILURE);
-        }
-    }
-  in[PW_LEN] = '\0';
-
-  sl_strlcpy ((char *)key, 
-	      sh_tiger_hash ((char*)in, TIGER_DATA, PW_LEN, 
-			     hashbuf, sizeof(hashbuf)), 
-	      KEY_LEN+1);
-  key[KEY_LEN] = '\0';
-  
-  fprintf(stdout, _("Client entry: Client=HOSTNAME@00000000@%s\n"), 
-	  key);
-  fflush(stdout);
-
-  _exit(EXIT_SUCCESS);
-  return 0;
-}
-
-#else
-
-int sh_xfer_make_client (const char * str)
-{
-  char * foo_v;
-
-  char   salt[17];
-  char   key[KEY_LEN+1];
-  char   in[PW_LEN];
-  int    i = 0, j, k, l = 0;
-  char hashbuf[KEYBUF_SIZE];
-  
-  if (sl_strlen(str) != (PW_LEN*2)) 
-    {
-      fprintf(stderr, 
-	      _("Input must be a %d digit hexadecimal number"\
-		" (only 0-9, a-f, A-F allowed in input)\n"),
-	      (PW_LEN*2));
-      _exit(EXIT_FAILURE);
-    }
-
-    while (i < (PW_LEN*2))
-      {
-        k = sh_util_hexchar(str[i]); j = sh_util_hexchar(str[i+1]); 
-        if (k != -1 && j != -1) 
-          {
-            in[l] = (k * 16 + j);
-            ++l; i+= 2;
-          }
-        else
-          {
-            fprintf(stderr, _("Invalid char %c\n"), str[i]);
-            _exit(EXIT_FAILURE);
-          }
-      }
-    
-  
-    if (0 == sh_srp_init())
-      {
-	sh_util_keyinit(key, KEY_LEN);
-	sl_strlcpy(salt, sh_tiger_hash(key, TIGER_DATA, KEY_LEN, 
-				       hashbuf, sizeof(hashbuf)), 
-		   17); 
-	sh_srp_x (salt, in);
-	foo_v  = sh_srp_verifier ();
-	fprintf(stdout, _("Client=HOSTNAME@%s@%s\n"), 
-		salt, foo_v);
-	fflush(stdout);
-	SH_FREE(foo_v);
-	sh_srp_exit();
-	_exit(EXIT_SUCCESS);
-      }
-    fprintf(stdout, "%s",_("ERROR initializing BigNum library.\n"));
-    fflush (stdout);
-    _exit(EXIT_FAILURE);
-    return -1;
-}
-#endif
-
-
-int sh_xfer_create_password (const char * dummy)
-{
-  UINT32   val[2]; 
-  char     output[KEY_LEN+1];
-  char hashbuf[KEYBUF_SIZE];
-
-  val[0] = taus_get ();
-  val[1] = taus_get ();
-
-  sl_strlcpy (output, 
-	      sh_tiger_hash((char *)(&val[0]), TIGER_DATA, 2*sizeof(UINT32),
-			    hashbuf, sizeof(hashbuf)),
-	      KEY_LEN);
-
-  output[16] = '\0';
-
-  fprintf(stdout, _("%s\n"), output);
-  fflush (stdout);
-
-  if (dummy)
-    _exit(EXIT_SUCCESS);
-  else
-    _exit(EXIT_SUCCESS);  
-  return (0);  /* avoid compiler warning */
-}
-
-/* #if defined (SH_WITH_SERVER) */
-#endif
-
-/**************************************************
- *
- *
- *  S E R V E R   
- *
- *
- ***************************************************/
-
-#ifdef SH_WITH_SERVER
-
-#include "sh_readconf.h"
-
-
-#define CONN_FREE    0
-#define CONN_READING 1
-#define CONN_SENDING 2
-#define CONN_PAUSE   3
-#define CONN_BUSY    4
-
-char * clt_stat[] = {
-  N_("Inactive"),
-  N_("Started"),
-  N_("ILLEGAL"),
-  N_("FAILED"),
-  N_("Exited"),
-  N_("PANIC"),
-  N_("POLICY"),
-  N_("File_transfer"),
-  N_("Message"),
-  N_("TIMEOUT_EXCEEDED"),
-  N_("Suspended"),
-  N_("Filecheck"),
-};
-
-#include <time.h>
-
-/* in sh_html.h:
- *  typedef struct client_entry {
- *  } client_t;
- */
-
-typedef struct client_alias {
-  char                  * alias;
-  char                  * hostname;
-} alias_t;
-
-#include "zAVLTree.h"
-
-static char * sh_tolower (char * s)
-{
-  char * ret = s;
-  if (s)
-    {
-      for (; *s; ++s)
-	{ 
-	  *s = tolower((unsigned char) *s);
-	}
-    }
-  return ret;
-}
-
-/* Function to return the key for indexing
- * the argument (for the client list)
- */
-zAVLKey sh_avl_key (void const * arg)
-{
-  const client_t * sa = (const client_t *) arg;
-  return (zAVLKey) sa->hostname;
-}
-
-zAVLTree * all_clients = NULL;
-
-/* Function to return the key for indexing
- * the argument (for the aliases list)
- */
-zAVLKey sh_avl_alias (void const * arg)
-{
-  const alias_t * sa = (const alias_t *) arg;
-  return (zAVLKey) sa->alias;
-}
-
-zAVLTree * all_aliases = NULL;
-
-void sh_xfer_html_write()
-{
-  SL_ENTER(_("sh_xfer_html_write"));
-  sh_html_write(all_clients);
-  SL_RET0(_("sh_xfer_html_write"));
-}
-
-
-int sh_xfer_use_clt_class (const char * c)
-{
-  int i;
-  SL_ENTER(_("sh_xfer_use_clt_class"));
-  i = sh_util_flagval(c, &(sh.flag.client_class));
-  SL_RETURN(i, _("sh_xfer_use_clt_class"));
-}
-
-int sh_xfer_use_clt_sev (const char * c)
-{
-  int i;
-  SL_ENTER(_("sh_xfer_use_clt_sev"));
-  i = sh_util_flagval(c, &(sh.flag.client_severity));
-  SL_RETURN(i, _("sh_xfer_use_clt_sev"));  
-}
-
-
-/* the destructor (client list item)
- */
-void free_client(void * inptr)
-{
-  client_t * here;
-
-  SL_ENTER(_("free_client"));
-  if (inptr == NULL)
-    SL_RET0(_("free_client"));
-  else
-    here = (client_t *) inptr;
-
-  /* cppcheck-suppress uninitStructMember */
-  if (here->hostname != NULL)
-    SH_FREE(here->hostname);
-  if (here->salt != NULL)
-    SH_FREE(here->salt);
-  if (here->verifier != NULL)
-    SH_FREE(here->verifier);
-  SH_FREE(here);
-  SL_RET0(_("free_client"));
-}
-
-/* the destructor (alias list item)
- */
-void free_alias(void * inptr)
-{
-  alias_t * here;
-
-  SL_ENTER(_("free_alias"));
-  if (inptr == NULL)
-    SL_RET0(_("free_alias"));
-  else
-    here = (alias_t *) inptr;
-
-  /* cppcheck-suppress uninitStructMember */
-  if (here->alias != NULL)
-    SH_FREE(here->alias);
-  if (here->hostname != NULL)
-    SH_FREE(here->hostname);
-  SH_FREE(here);
-  SL_RET0(_("free_alias"));
-}
-
-int sh_xfer_register_alias (const char * str)
-{
-  alias_t    * newalias;
-  alias_t    * testalias;
-
-  const char * ptr;
-  int          sepnum  = 0;
-  int          sep     = 0;
-  register int i       = 0;
-  int          siz_str = 0;
-
-  SL_ENTER(_("sh_xfer_register_alias"));
-
-  ptr = str; 
-  while (*ptr) {
-    if (*ptr == '@' && sepnum < 1) 
-      { 
-	sep = i;
-	++sepnum;
-      } 
-    ++ptr; ++i; 
-  }
-
-  if (all_aliases == NULL)
-    {
-      all_aliases = zAVLAllocTree (sh_avl_alias, zAVL_KEY_STRING);
-      if (all_aliases == NULL) 
-	{
-	  (void) safe_logger (0, 0, NULL);
-	  aud__exit(FIL__, __LINE__, EXIT_FAILURE);
-	}
-    }
-
-  if ((sepnum == 1) && (sep > 0) && (i > (sep + 1)))
-    {
-      newalias                  = SH_ALLOC (sizeof(alias_t));
-      newalias->alias           = SH_ALLOC (sep+1);
-      newalias->hostname        = SH_ALLOC (sl_strlen(str)-sep);
-
-      /* truncate */
-      sl_strlcpy(newalias->alias,  &str[0],        sep+1);
-      sh_tolower(newalias->alias);
-
-      /* truncate */
-      sl_strlcpy(newalias->hostname,  &str[sep+1], sl_strlen(str)-sep);
-      sh_tolower(newalias->hostname);
-
-      testalias = (alias_t *) zAVLSearch (all_aliases, newalias->alias);
-
-      if (testalias != NULL)
-	{
-	  /* keep the alias but replace the hostname with the new one */
-	  SH_FREE(testalias->hostname);
-	  siz_str = strlen (newalias->hostname) + 1;
-	  testalias->hostname = SH_ALLOC (siz_str);
-	  sl_strlcpy(testalias->hostname, newalias->hostname, siz_str);
-	  
-	  free_alias(newalias);
-	  SL_RETURN( 0, _("sh_xfer_register_alias"));
-	}
-      else
-	{
-	  if (0 == zAVLInsert (all_aliases, newalias))
-	    {
-	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_AREG,
-			      newalias->alias, 
-			      newalias->hostname);
-	      SL_RETURN( 0, _("sh_xfer_register_alias"));
-	    }
-	}
-    }
-  SL_RETURN (-1, _("sh_xfer_register_alias"));
-}
-
-
-int sh_xfer_register_client (const char * str)
-{
-  client_t   * newclt;
-  client_t   * testclt;
-
-  const char * ptr;
-  int          sepnum = 0;
-  int          sep[2];
-  register int i = 0;
-  int          siz_str = 0;
-
-  SL_ENTER(_("sh_xfer_register_client"));
-
-  ptr = str; 
-  while (*ptr) {
-    if (*ptr == '@' && sepnum < 2 ) 
-      { 
-	sep[sepnum] = i;
-	++sepnum;
-      } 
-    ++ptr; ++i; 
-  }
-
-  if (all_clients == NULL)
-    {
-      all_clients = zAVLAllocTree (sh_avl_key, zAVL_KEY_STRING);
-      if (all_clients == NULL) 
-	{
-	  (void) safe_logger (0, 0, NULL);
-	  aud__exit(FIL__, __LINE__, EXIT_FAILURE);
-	}
-    }
-  
-  if ((sepnum == 2) && (sep[0] > 0) && (sep[1] > sep[0]))
-    {
-      newclt = SH_ALLOC (sizeof(client_t));
-      newclt->hostname = SH_ALLOC (sep[0]+1);
-      newclt->salt     = SH_ALLOC (sep[1]-sep[0]);
-      newclt->verifier = SH_ALLOC (sl_strlen(str)-sep[1]+1);
-      newclt->exit_flag         = 0;
-      newclt->dead_flag         = 0;
-#ifdef SH_ENCRYPT
-      newclt->encf_flag         = SH_PROTO_ENC;
-      newclt->ency_flag         = SH_PROTO_ENC;
-#else
-      newclt->encf_flag         = 0;
-      newclt->ency_flag         = 0;
-#endif
-      newclt->ivst_flag         = 0;
-      newclt->session_key[0]    = '\0';
-      newclt->last_connect      = (time_t) 0;
-      newclt->session_key_timer = (time_t) 0;
-      newclt->status_now        = CLT_INACTIVE;
-      for (i = 0; i < CLT_MAX; ++i) 
-	newclt->status_arr[i] = CLT_INACTIVE;
-      (void) sh_unix_time(0, newclt->timestamp[CLT_INACTIVE], TIM_MAX);
-
-      /* truncate */
-      sl_strlcpy(newclt->hostname,  &str[0],        sep[0]+1);
-      sh_tolower(newclt->hostname);
-
-      /* truncate */
-      sl_strlcpy(newclt->salt,      &str[sep[0]+1], sep[1]-sep[0]);
-      sl_strlcpy(newclt->verifier,  &str[sep[1]+1], sl_strlen(str)-sep[1]+1);
-
-      testclt = (client_t *) zAVLSearch (all_clients, newclt->hostname);
-
-      if (testclt != NULL)
-	{
-	  SH_FREE(testclt->verifier);
-	  siz_str = strlen (newclt->verifier) + 1;
-	  testclt->verifier = SH_ALLOC (siz_str);
-	  sl_strlcpy(testclt->verifier, newclt->verifier, siz_str);
-
-	  SH_FREE(testclt->salt);
-	  siz_str = strlen (newclt->salt) + 1;
-	  testclt->salt = SH_ALLOC (siz_str);
-	  sl_strlcpy(testclt->salt, newclt->salt, siz_str);
-
-	  testclt->dead_flag = 0;
-	      
-	  free_client(newclt);
-	  SL_RETURN( 0, _("sh_xfer_register_client"));
-	}
-      else
-	{
-	  if (0 == zAVLInsert (all_clients, newclt))
-	    {
-	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_CREG,
-			      newclt->hostname, 
-			      newclt->salt, newclt->verifier);
-	      SL_RETURN( 0, _("sh_xfer_register_client"));
-	    }
-	}
-    }
-  SL_RETURN (-1, _("sh_xfer_register_client"));
-}
-
-typedef struct {
-  int             state;
-  int             fd;
-  char          * buf;
-  unsigned char   head[SH_HEADER_SIZE];
-  char            challenge[SH_CHALLENGE_SIZE];
-  char            peer[SH_MINIBUF+1];
-  client_t      * client_entry;
-  char          * K;
-  char          * M1;
-  char          * A;
-  int             headcount;
-  unsigned long   bytecount;
-  unsigned long   bytes_to_send;
-  unsigned long   bytes_to_get;
-  int             pass;
-  unsigned long   timer;
-
-  char          * FileName;
-  unsigned long   FileLength;
-  unsigned long   FileSent;
-  char            FileType[5];
-
-  struct sh_sockaddr addr_peer;
-} sh_conn_t;
-
-
-static char zap_challenge[SH_CHALLENGE_SIZE] = { 0 };
- 
-void sh_xfer_do_free (sh_conn_t * conn)
-{
-  SL_ENTER(_("sh_xfer_do_free"));
-
-  if (conn->K != NULL) 
-    {
-      SH_FREE(conn->K);
-      conn->K           = NULL;
-    }
-  if (conn->A != NULL) 
-    {
-      SH_FREE(conn->A);
-      conn->A           = NULL;
-    }
-  if (conn->M1 != NULL) 
-    {
-      SH_FREE(conn->M1);
-      conn->M1           = NULL;
-    }
-  if (conn->buf != NULL) 
-    {
-      SH_FREE(conn->buf);
-      conn->buf          = NULL;
-    }
-  if (conn->fd != (-1))
-    {
-      sl_close_fd (FIL__, __LINE__, conn->fd);
-      conn->fd            = -1;
-    }
-  memcpy(conn->challenge, zap_challenge, SH_CHALLENGE_SIZE);
-  conn->state         = CONN_FREE;
-  conn->headcount     = 0;
-  conn->bytecount     = 0;
-  conn->bytes_to_send = 0;
-  conn->bytes_to_get  = 0;
-  conn->pass          = 0;
-  conn->timer         = 0;
-  conn->client_entry  = NULL;
-
-  if (conn->FileName != NULL) 
-    {
-      SH_FREE(conn->FileName);
-      conn->FileName     = NULL;
-    }
-  conn->FileLength     = 0;
-  conn->FileSent       = 0;
-  conn->FileType[0] = '\0';
-  conn->FileType[1] = '\0';
-  conn->FileType[2] = '\0';
-  conn->FileType[3] = '\0';
-  conn->FileType[4] = '\0';
-
-  --server_status.conn_open;
-  
-  SL_RET0(_("sh_xfer_do_free"));
-}
-
-/****************************************
- *
- *   -- Reconfiguration. --
- *
- *   (1) Mark all clients as 'dead'.
- *   (2) Reload configuration - clients
- *       in config are non-dead now.
- *   (3) Remove all clients still
- *       marked as 'dead'.
- */
-
-/* -- Mark all clients as dead.
- */
-void sh_xfer_mark_dead (void)
-{
-  zAVLCursor avlcursor;
-  client_t * item;
-
-  SL_ENTER(_("sh_xfer_mark_dead"));
-
-  for (item = (client_t *) zAVLFirst(&avlcursor, all_clients); item;
-       item = (client_t *) zAVLNext(&avlcursor))
-    {
-      item->dead_flag = 1;
-    }
-  SL_RET0(_("sh_xfer_mark_dead"));
-}
-
-
-/* -- Clean tree from dead clients.
- */
-void sh_xfer_clean_tree (void)
-{
-  zAVLCursor avlcursor;
-  client_t * item;
-
-  SL_ENTER(_("sh_xfer_clean_tree"));
-
- repeat_search:
-
-  for (item = (client_t *) zAVLFirst(&avlcursor, all_clients); item;
-       item = (client_t *) zAVLNext(&avlcursor))
-    {
-      if (item->dead_flag == 1)
-	{
-	  zAVLDelete (all_clients, item->hostname);
-	  free_client (item);
-	  goto repeat_search;
-	}
-    }
-  SL_RET0(_("sh_xfer_clean_tree"));
-}
-
-/*
- *
- **********************************************/
-
-
-
-/* -- SERVER SEND FUNCTION. --
- */
-void sh_xfer_prep_send_int (sh_conn_t * conn, 
-			    char * msg, unsigned long length,
-			    char * u, char protocol,
-			    int docrypt)
-{
-  /* register unsigned long i; */
-  unsigned long           length2;
-
-#if !defined(SH_ENCRYPT)
-  (void) docrypt;
-#endif
-
-  SL_ENTER(_("sh_xfer_prep_send_int"));
-
-  TPT((0, FIL__, __LINE__, _("msg=<%s>, docrypt=<%d>\n"), msg, docrypt ));
-
-  length2 = length;
-
-  conn->headcount     = 0;
-  conn->bytecount     = 0;
-  conn->bytes_to_send = 0;
-  conn->bytes_to_get  = 0;
-
-  if (conn->buf != NULL) 
-    {
-      SH_FREE(conn->buf);
-      conn->buf           = NULL;
-    }
-
-  put_header (conn->head, protocol, &length2, u);
-  SH_SHOWPROT(conn->head,'>');
-
-  TPT((0, FIL__, __LINE__, _("msg=<put_header done>\n") ));
-
-  if (msg == NULL) 
-    length2 = 0;
-  
-#ifdef SH_ENCRYPT
-  if      ((S_TRUE == docrypt) && ((protocol & SH_PROTO_ENC) != 0))
-    {
-      TPT((0, FIL__, __LINE__, _("encrypting (version 2)\n")));
-      
-      conn->buf = sh_tools_makePack (conn->head, conn->client_entry->ivst_flag,
-				     msg, length2,
-				     &(conn->client_entry->keyInstE));
-    }
-  else if (msg == NULL)
-    {
-      sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
-		      _("msg is NULL"), 
-		      _("sh_xfer_prep_send_int: cipherInit"));
-    }
-  else
-    {
-      if ((length2 + 1) < length2) --length2;
-      conn->buf       = SH_ALLOC(length2 + 1);
-
-      memcpy(conn->buf, msg, length2);
-      conn->buf[length2] = '\0';
-      TPT((0, FIL__, __LINE__, _("msg=<no encryption done>\n") ));
-    }
-#else
-  if ((length2 + 1) < length2) --length2;
-  conn->buf       = SH_ALLOC(length2 + 1);
-
-  memcpy(conn->buf, msg, length2);
-  conn->buf[length2] = '\0';
-  TPT((0, FIL__, __LINE__, _("msg=<no encryption done>\n") ));
-#endif
-
-  conn->state     = CONN_SENDING;
-  SL_RET0(_("sh_xfer_prep_send_int"));
-}
-
-/* -- Send/Receive. --
- */
-void sh_xfer_prep_send (sh_conn_t * conn, 
-			   char * msg, unsigned long length,
-			   char * u, char protocol)
-{
-  SL_ENTER(_("sh_xfer_prep_send"));
-  sh_xfer_prep_send_int (conn,  msg, length, u, protocol, S_FALSE);
-  SL_RET0(_("sh_xfer_prep_send"));
-}  
-
-void sh_xfer_send_crypt (sh_conn_t * conn, 
-				 char * msg, unsigned long length,
-				 char * u, char protocol)
-{
-  SL_ENTER(_("sh_xfer_send_crypt"));
-  sh_xfer_prep_send_int (conn,  msg, length, u, protocol, S_TRUE);
-  SL_RET0(_("sh_xfer_send_crypt"));
-}  
-
-/* #include <sys/times.h> */
-
-#if defined(WITH_EXTERNAL)
-#include "sh_extern.h"
-#endif
-
-/* -- Update the client status. --
- *
- * Update the status array for the client,
- * and eventually call external program.
- */
-static void status_update (client_t * conn, int status)
-{ 
-#if defined(WITH_EXTERNAL)
-  char msg[2 * SH_MINIBUF + TIM_MAX + 3];
-#endif
-
-  SL_ENTER(_("status_update"));
-
-  if (conn == NULL || 
-      status < 0   || status >= CLT_MAX)
-    SL_RET0(_("status_update"));
-
-  conn->status_now = status;
-  conn->status_arr[status] = status;
-  (void) sh_unix_time(0, conn->timestamp[status], TIM_MAX);
-
-#if defined(WITH_EXTERNAL)
-  sl_snprintf(msg, sizeof(msg), _("%s %s %s"),
-	      conn->hostname, conn->timestamp[status], _(clt_stat[status]));
-  sh_ext_execute('s', 'r', 'v', msg, 0);
-#endif
-
-  SL_RET0(_("status_update"));
-}
-
-static time_t time_client_limit = 86400;
-
-int sh_xfer_set_time_limit (const char * c)
-{
-  long val;
-
-  SL_ENTER(_("sh_xfer_set_time_limit"));
-
-  val = strtol (c, (char **)NULL, 10);
-  if (val <= 0)
-    SL_RETURN( (-1), _("sh_xfer_set_time_limit"));
-
-  time_client_limit = (time_t) val;
-  SL_RETURN( (0), _("sh_xfer_set_time_limit"));
-}
-
-
-/* -- Check for time limit exceeded. --
- */
-static int client_time_check(void)
-{
-  zAVLCursor avlcursor;
-  client_t * item;
-
-  SL_ENTER(_("client_time_check"));
-
-  if (time_client_limit == (time_t) 0)
-    SL_RETURN( 0, _("client_time_check"));
-
-  for (item = (client_t *) zAVLFirst(&avlcursor, all_clients); item;
-       item = (client_t *) zAVLNext(&avlcursor))
-    {
-      if (item->exit_flag == 0 && item->last_connect != (time_t) 0)
-	{
-	  if ( (time(NULL) - item->last_connect) > time_client_limit)
-	    {
-	      if (item->status_now != CLT_TOOLONG)
-		{
-		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_TIMEXC,
-				  item->hostname);
-		  status_update (item, CLT_TOOLONG);
-		}
-	    }
-	}
-    }
-  SL_RETURN( 0, _("client_time_check"));
-}
-
-static int lookup_err = SH_ERR_SEVERE;
-
-int sh_xfer_lookup_level (const char * c)
-{
-  int ci =  sh_error_convert_level (c);
-
-  SL_ENTER(_("sh_xfer_lookup_level"));
-
-  if (ci >= 0)
-    {
-      lookup_err = ci;
-      SL_RETURN( 0, _("sh_xfer_lookup_level"));
-    }
-  else
-    SL_RETURN( (-1), _("sh_xfer_lookup_level"));
-}
-
-#ifndef MAXHOSTNAMELEN
-#define MAXHOSTNAMELEN  127
-#endif
-
-int check_addr (const char * claim, struct sh_sockaddr * addr_peer)
-{
-  char               h_name[MAXHOSTNAMELEN + 1];
-  char               h_peer[MAXHOSTNAMELEN + 1];
-  char               h_peer_IP[SH_IP_BUF];
-  char               tmp_peer_IP[SH_IP_BUF];
-  char             * canonical;
-  char               numeric[SH_IP_BUF];
-
-  SL_ENTER(_("check_addr"));
-
-  if (claim == NULL)
-    {
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		      _("NULL input"), _("check_addr"));
-      SL_RETURN ((-1), _("check_addr"));
-    }
-
-  /* Make sure we have the canonical name for the client
-   */
-  canonical = sh_ipvx_canonical(claim, numeric, sizeof(numeric));
-
-  /* copy canonical name into h_name
-   */
-  if (canonical != NULL)
-    {
-      sl_strlcpy(h_name, canonical, MAXHOSTNAMELEN + 1);
-      SH_FREE(canonical);
-    }
-  else
-    {
-      sh_error_handle(lookup_err, FIL__, __LINE__, 0, MSG_TCP_RESCLT,
-		      claim);
-      SL_RETURN ((0), _("check_addr"));
-    }
-
-
-  /* get canonical name of socket peer
-   */
-  canonical = sh_ipvx_addrtoname(addr_peer);
-
-  if (canonical)
-    {
-      if (0 == sl_strcmp(canonical, _("localhost")))
-	sl_strlcpy(h_peer, sh.host.name, MAXHOSTNAMELEN + 1);
-      else
-	sl_strlcpy(h_peer, canonical, MAXHOSTNAMELEN + 1);
-      SH_FREE(canonical);
-    }
-  else
-    {
-      sh_ipvx_ntoa (tmp_peer_IP, sizeof(tmp_peer_IP), addr_peer);
-      sh_error_handle(lookup_err, FIL__, __LINE__, 0, MSG_TCP_RESPEER,
-		      claim, tmp_peer_IP);
-      SL_RETURN ((0), _("check_addr"));
-    }
-
-  sh_ipvx_ntoa (h_peer_IP, sizeof(h_peer_IP), addr_peer);
-
-  /* reverse lookup
-   */
-  if (0 == sh_ipvx_reverse_check_ok (h_peer, ServerPort, addr_peer))
-    {
-      sh_ipvx_ntoa (tmp_peer_IP, sizeof(tmp_peer_IP), addr_peer);
-
-      sh_error_handle(lookup_err, FIL__, __LINE__, 0, MSG_TCP_LOOKERS,
-		      claim, h_peer, tmp_peer_IP);
-      SL_RETURN ((0), _("check_addr"));
-    }
-
-  /* Check whether claim and peer are identical
-   */
-  sh_tolower(h_peer); /* Canonical name of what the peer is     */
-  sh_tolower(h_name); /* Canonical name of what the peer claims */
-
-  if ((0 == sl_strcmp(h_peer, h_name)) || (0 == sl_strcmp(h_peer_IP, h_name)))
-    {
-      SL_RETURN ((0), _("check_addr"));
-    }
-#if !defined(USE_IPVX)
-  else
-    {
-      struct hostent   * he = sh_gethostbyname(h_peer);
-      int                i = 0;
-      int                flag = 0;
-
-      while (he->h_aliases[i] != NULL)
-	{
-	  if (0 == sl_strcmp(sh_tolower(he->h_aliases[i]), h_name))
-	    {
-	      flag = 1;
-	      break;
-	    }
-	  ++i;
-	}
-      if (flag == 0) 
-	sh_error_handle(lookup_err, FIL__, __LINE__, 0, MSG_TCP_LOOKUP,
-			claim, h_peer);
-    }
-#endif
-
-  SL_RETURN ((0), _("check_addr"));
-}
-
-static int UseSocketPeer = S_FALSE;
-
-int set_socket_peer (const char * c)
-{
-  return sh_util_flagval(c, &UseSocketPeer);
-}
-
-
-/* -- Search register. --
- */
-client_t * search_register(sh_conn_t * conn, int pos)
-{
-  alias_t  * this_alias;
-  client_t * this_client;
-  char       peer_ip[SH_IP_BUF];
-  char       numerical[SH_IP_BUF];
-  char       peer_name[MAXHOSTNAMELEN+1];
-  char     * search_string;
-
-  struct sh_sockaddr peer_addr;
-  char             * canonical;
-
-  SL_ENTER(_("search_register"));
-
-  if (UseSocketPeer == S_TRUE)
-    {
-      memcpy(&peer_addr, &(conn->addr_peer), sizeof(struct sh_sockaddr));
-      sh_ipvx_ntoa (peer_ip, sizeof(peer_ip), &peer_addr);
-      peer_name[0] = '\0';
-
-      /* get canonical name of socket peer
-       */
-      canonical = sh_ipvx_canonical(peer_ip, numerical, sizeof(numerical));
-
-      if (canonical != NULL)
-	{
-	  if (0 == sl_strcmp(canonical, _("localhost")))
-	    sl_strlcpy(peer_name, sh.host.name, MAXHOSTNAMELEN + 1);
-	  else
-	    sl_strlcpy(peer_name, canonical,    MAXHOSTNAMELEN + 1);
-	  SH_FREE(canonical);
-	}
-
-      if (0 == sh_ipvx_reverse_check_ok (peer_name, ServerPort, &peer_addr))
-	{
-	  sl_strlcpy(peer_name, peer_ip, MAXHOSTNAMELEN + 1);
-	}
-      else
-	{
-	  this_alias = zAVLSearch(all_aliases, peer_name);
-	  if (this_alias)
-	    {
-	      sl_strlcpy(peer_name, this_alias->hostname, MAXHOSTNAMELEN + 1);
-	    }
-	}
-
-      search_string = peer_name;
-    }
-  else
-    {
-      search_string = &(conn->buf[pos]);
-
-      if (0 != check_addr (search_string, &(conn->addr_peer)))
-	{
-	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
-			  _("Reverse lookup failed"), search_string);
-	  sh_xfer_do_free (conn);
-	  SL_RETURN( NULL, _("search_register"));
-	} 
-    }
-
-  sh_tolower(search_string);
-
-  /* ----  search the register  -----
-   */
-  this_client = zAVLSearch(all_clients, search_string);
-
-  if (this_client == NULL)
-    {
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
-		      _("Not in client list"), search_string);
-      sh_xfer_do_free (conn);
-      SL_RETURN( NULL, _("search_register"));
-    } 
-  if (this_client->exit_flag == 1)
-    {
-      TPT((0, FIL__, __LINE__, _("msg=<this_client->exit_flag == 1>\n")));
-      this_client->session_key_timer = (time_t) 0;
-      this_client->session_key[0]    = '\0';
-      this_client->exit_flag         = 0;    
-    }
-  TPT((0, FIL__, __LINE__, _("msg=<search_register: client %s>\n"), 
-       this_client->hostname));
-  TPT((0, FIL__, __LINE__, _("msg=<search_register: key %s>\n"), 
-       this_client->session_key));
-  SL_RETURN( this_client, _("search_register"));
-}  
-
-client_t * do_check_client(sh_conn_t * conn, int * retval)
-{
-  client_t * this_client = NULL; 
-  char sigbuf[KEYBUF_SIZE];
-  
-  *retval = 0;
-
-  TPT(( 0, FIL__, __LINE__, _("msg=<Client connect - HELO (1).>\n")));
-	  
-  if (conn->buf == NULL || sl_strlen(conn->buf) <= KEY_LEN)
-    {
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NOCLT);
-      sh_xfer_do_free (conn);
-      return NULL;
-    } 
-	  
-  /* ----  search the register  -----
-   */
-  
-  this_client = search_register (conn, KEY_LEN);
-  if (this_client == NULL)
-    return NULL;
-  
-  /* ---- force authentication -----
-   */
-  
-  if (this_client->session_key[0] == '\0' ||
-      (time(NULL) - this_client->session_key_timer) 
-      > (time_t) TIMEOUT_KEY )
-    {
-      size_t len;
-
-      /* fake an auth request and jump there
-       */
-      conn->head[0]  = (conn->head[0] | SH_PROTO_SRP);
-      conn->head[3]  = 'S';
-      conn->head[4]  = 'A';
-      conn->head[5]  = 'L';
-      conn->head[6]  = 'T';
-      if (flag_err_info == S_TRUE)
-	sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FAUTH,
-			&(conn->buf[KEY_LEN]));
-      len = sl_strlen(&(conn->buf[KEY_LEN])) + 1;
-      /* may overlap, thus only memmove is correct */
-      memmove(conn->buf, &(conn->buf[KEY_LEN]), len); 
-      this_client->session_key[0]    = '\0';
-      this_client->session_key_timer = (time_t) 1;
-      *retval = -1;
-      return NULL;
-    }
-
-  /* --- check whether hostname is properly signed ---
-   */ 
-  if (conn->K != NULL) 
-    {
-      SH_FREE(conn->K);
-      conn->K = NULL;
-    }
-  
-  conn->K = SH_ALLOC(KEY_LEN+1);
-  
-  sl_strlcpy (conn->K, 
-	      sh_util_siggen(this_client->session_key,
-			     &(conn->buf[KEY_LEN]),
-			     sl_strlen(&(conn->buf[KEY_LEN])),
-			     sigbuf, sizeof(sigbuf)),
-	      KEY_LEN+1);
-  
-  if (0 != sl_ts_strncmp(conn->K, conn->buf, KEY_LEN))
-    {
-      TPT((0, FIL__, __LINE__, _("msg=<clt %s>\n"), conn->buf));
-      TPT((0, FIL__, __LINE__, _("msg=<srv %s>\n"), conn->K));
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
-		      _("Signature mismatch"), 
-		      &(conn->buf[KEY_LEN]));
-      
-      this_client->session_key_timer =
-	time(NULL) - (2*TIMEOUT_KEY);
-      
-      sh_xfer_do_free (conn);
-      return NULL;
-    }
-  SH_FREE(conn->K); 
-  conn->K = NULL;
-
-  return this_client;
-}
-
-/* ------------------------------------------------------
- *
- *  FILE TRANSFER
- *
- * ------------------------------------------------------ */
-
-static void do_file_send_data(sh_conn_t * conn)
-{
-  char     * read_buf = 0;
-  char     * send_buf;
-  int        bytes;
-  SL_TICKET  sfd = -1;
-
-  if (conn == NULL    || conn->FileName == NULL)
-    {
-      sh_error_handle((-1), FIL__, __LINE__, sfd, MSG_TCP_NFILE,
-                      conn->peer, 
-                      (conn->FileName == NULL) ? 
-                      _("(NULL)") : conn->FileName);
-      status_update (conn->client_entry, CLT_FAILED);
-      sh_xfer_do_free (conn);
-      return;
-    }
-
-  if (conn->FileSent == conn->FileLength)
-    {
-      send_buf = hash_me(conn->K, conn->peer, sl_strlen(conn->peer));
-#ifdef SH_ENCRYPT
-      sh_xfer_send_crypt (conn, send_buf, sl_strlen(conn->peer)+KEY_LEN, 
-				  _("EEOT"), SH_PROTO_BIG|conn->client_entry->encf_flag);
-#else
-      sh_xfer_send_crypt (conn, send_buf, sl_strlen(conn->peer)+KEY_LEN, 
-				  _("EEOT"), SH_PROTO_BIG);
-#endif
-      SH_FREE(send_buf);
-    }
-  else
-    {
-      bytes = -1;
-
-      sfd = sl_open_read(FIL__, __LINE__, conn->FileName, SL_YESPRIV);
-
-      if (!SL_ISERROR(sfd))
-	{
-	  read_buf = SH_ALLOC(TRANS_BYTES);
-	  if (conn->FileSent > 0)
-	    sl_seek (sfd, (off_t) conn->FileSent);
-	  bytes = sl_read (sfd, read_buf, TRANS_BYTES);
-	  sl_close(sfd);
-	}
-      else
-	{
-	  sh_error_handle((-1), FIL__, __LINE__, sfd, 
-			  MSG_E_ACCESS, (long) geteuid(), conn->FileName);
-	}
-
-      if (bytes >= 0)
-	{
-	  send_buf = hash_me(conn->K, read_buf,  bytes);
-#ifdef SH_ENCRYPT
-	  sh_xfer_send_crypt (conn, send_buf, bytes+KEY_LEN, _("FILE"),  
-				      SH_PROTO_BIG|conn->client_entry->encf_flag);
-#else
-	  sh_xfer_send_crypt (conn, send_buf, bytes+KEY_LEN, _("FILE"),  
-				      SH_PROTO_BIG);
-#endif
-	  conn->FileSent += bytes;
-	  if (send_buf) /* hash_me() *may* return NULL */
-	    SH_FREE(send_buf);
-	  SH_FREE(read_buf);
-	}
-      else
-	{
-	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NFILE, conn->peer,
-			  (conn->FileName == NULL) ? _("(NULL)") : conn->FileName);
-	  status_update (conn->client_entry, CLT_FAILED);
-	  sh_xfer_do_free (conn);
-	}
-    }
-  return;
-}
-
-static void do_file_initial(sh_conn_t * conn)
-{
-  char     * ptok;
-  char       hashbuf[KEYBUF_SIZE];
-
-  /* --- get client nonce and compute hash ---
-   *
-   *  K = H(NSRV, NCLT, session_key) 
-   */ 
-  if (conn->A != NULL)
-    {
-      SH_FREE(conn->A);
-      conn->A = NULL;
-    }
-  conn->A = SH_ALLOC(3*KEY_LEN+1);
-  sl_strlcpy (conn->A, conn->K, KEY_LEN+1); 
-  sl_strlcat(conn->A, conn->buf, /* truncate */
-	     2*KEY_LEN+1);
-  sl_strlcat(conn->A, conn->client_entry->session_key, 
-	     3*KEY_LEN+1);
-  sl_strlcpy (conn->K, sh_tiger_hash(conn->A,TIGER_DATA,3*KEY_LEN,
-				     hashbuf, sizeof(hashbuf)),
-	      KEY_LEN+1);
-  SH_FREE(conn->A); 
-  conn->A = NULL;
-  
-
-  /* Warn about encryption mismatch
-   */
-#ifdef SH_ENCRYPT
-  if ((conn->client_entry->encf_flag != 0) &&  /* server */
-      ((conn->head[0] & SH_PROTO_ENC) == 0))   /* client */
-    {
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_MISENC,
-		      _("file download"), _("version2"), _("none"));
-    }
-
-  else if ((conn->client_entry->encf_flag != 0) && /* server */
-	   ((conn->head[0] & SH_MASK_ENC) !=       /* client */
-	    conn->client_entry->encf_flag))
-    {
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_MISENC,
-		      _("file download"), _("version2"),
-		      ((conn->head[0] & SH_PROTO_ENC) == SH_PROTO_ENC) ? 
-		      _("version2") : _("invalid"));
-      conn->client_entry->encf_flag = (conn->head[0] & SH_MASK_ENC);
-    }
-#else
-  if ((conn->head[0] & SH_PROTO_ENC) != 0) 
-    {
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_MISENC,
-		      _("file download"), _("none"), 
-		      ((conn->head[0] & SH_PROTO_ENC) == SH_PROTO_ENC) ? 
-		      _("version2") : _("invalid"));
-    }
-#endif
-  
-  
-  if (conn->FileName != NULL)
-    {
-      SH_FREE(conn->FileName);
-      conn->FileName = NULL;
-    }
-
-  /* Determine what to send
-   */
-  if (0 == sl_strncmp (_("CONF"), &(conn->buf[KEY_LEN]), 4))
-    {
-      strcpy(conn->FileType, _("CONF"));     /* known to fit  */
-      conn->FileName = get_client_conf_file(conn->peer, &(conn->FileLength));
-      conn->FileSent = 0;
-    }
-  else  if (0 == sl_strncmp (_("DATA"), &(conn->buf[KEY_LEN]), 4))
-    {
-      strcpy(conn->FileType, _("DATA"));     /* known to fit  */
-      conn->FileName = get_client_data_file(conn->peer, &(conn->FileLength));
-      conn->FileSent = 0;
-    }
-  else  if (0 == sh_uuid_check(&(conn->buf[KEY_LEN])))
-    {
-      char * uuid = &(conn->buf[KEY_LEN]);
-      strcpy(conn->FileType, _("UUID"));     /* known to fit  */
-      conn->FileName = get_client_uuid_file(conn->peer, &(conn->FileLength), uuid);
-      conn->FileSent = 0;
-    }
-  else
-    {
-      ptok = sh_util_safe_name(&(conn->buf[KEY_LEN]));
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FFILE,
-		      conn->peer, 
-		      ptok);
-      SH_FREE(ptok);
-      status_update (conn->client_entry, CLT_FAILED);
-      sh_xfer_do_free (conn);
-    }
-
-  return;
-}
-
-
-static int do_file_transfer(sh_conn_t * conn, int state)
-{
-  client_t * this_client; 
-  UINT32     ticks;
-  char       hashbuf[KEYBUF_SIZE];
-
-  SL_ENTER(_("do_file_transfer"));
-
-  if (state == SH_DO_READ)        /* finished reading */
-    {
-      TPT(( 0, FIL__, __LINE__, _("msg=<File transfer - entry.>\n")));
-      
-      /* -- Client requests challenge. --
-       */
-      if (0 == check_request_nerr ((char *) &(conn->head[3]), _("HELO")))
-	{
-	  int client_state;
-
-	  this_client = do_check_client(conn, &client_state);
-	  if (!this_client)
-	    SL_RETURN(client_state, _("do_file_transfer"));
-
-	  /* --- create and send a nonce ---
-	   */
-	  
-	  conn->client_entry = this_client;
-	  sl_strlcpy (conn->peer, &(conn->buf[KEY_LEN]), SH_MINIBUF+1);
-	  
-	  ticks = (UINT32) taus_get ();
-	  
-	  if (conn->K != NULL) 
-	    {
-	      SH_FREE(conn->K);
-	      conn->K = NULL;
-	    }
-	  conn->K = SH_ALLOC(KEY_LEN+1);
-	  sl_strlcpy (conn->K, 
-		      sh_tiger_hash ((char *) &ticks, 
-				     TIGER_DATA, sizeof(UINT32), 
-				     hashbuf, sizeof(hashbuf)), 
-		      KEY_LEN+1);
-	  
-	  TPT((0, FIL__, __LINE__, _("msg=<send nonce>\n")));
-	  sh_xfer_prep_send (conn, conn->K, KEY_LEN+1, _("NSRV"), 
-				SH_PROTO_BIG);
-	}
-
-      /* --- Client has send a message. Check state and message. ---
-       */
-      else if (0 == check_request_nerr((char *)&(conn->head[3]), _("NCLT")) &&
-	       conn->client_entry != NULL                           &&
-	       sl_strlen(conn->buf) > KEY_LEN                       &&
-	       conn->K != NULL)
-	{
-	  
-	  TPT(( 0, FIL__, __LINE__, 
-		_("msg=<File transfer - NCLT (3).>\n")));
-	  
-          do_file_initial(conn);
-	  do_file_send_data(conn);
-	}
-      
-      else if (0 == check_request_nerr((char *)&(conn->head[3]), 
-				       _("RECV"))                    &&
-	       conn->client_entry != NULL                           &&
-	       conn->K != NULL                                      &&
-	       conn->FileName != NULL)
-	{
-	  
-	  TPT(( 0, FIL__, __LINE__, 
-		_("msg=<File transfer - RCVT (5+).>\n")));
-	  
-	  do_file_send_data(conn);
-	}
-      
-      
-      else if (0 == check_request_nerr((char *)&(conn->head[3]), 
-				       _("EOTE")) &&
-	       conn->client_entry != NULL)
-	{
-	  
-	  TPT(( 0, FIL__, __LINE__, 
-		_("msg=<File transfer - EOTE (7).>\n")));
-	  
-	  if (flag_err_info == S_TRUE)
-	    sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_OKFILE,
-			    conn->peer);
-	  
-	  if ((conn->client_entry->status_now != CLT_SUSPEND) &&
-	      (conn->client_entry->status_now != CLT_TOOLONG))
-	    { status_update (conn->client_entry, CLT_FILE); }
-	  else
-	    { conn->client_entry->session_key[0]    = '\0'; }
-	  conn->client_entry->last_connect = time (NULL);
-	  sh_xfer_do_free (conn);
-	}
-      
-      
-      /* client does something unexpected
-       */
-      else  /* ---- ??? ----- */
-	{
-	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FINV,
-			  1, conn->pass, conn->peer,  
-			  '\\', conn->head[3], '\\',conn->head[4],
-			  '\\', conn->head[5], '\\',conn->head[6]);
-	  status_update (conn->client_entry, CLT_FAILED);
-	  sh_xfer_do_free (conn);
-	}
-    }
-  
-  else if (state == SH_DO_WRITE)  /* finished writing */
-    {
-      TPT(( 0, FIL__, __LINE__, _("msg=<File transfer - (wait).>\n")));
-      
-      /* challenge is sent, now wait for message from client
-       */
-      conn->headcount     = 0;
-      conn->bytecount     = 0;
-      conn->bytes_to_send = 0;
-      conn->bytes_to_get  = 0;
-      if (conn->buf != NULL) 
-	{
-	  SH_FREE(conn->buf);
-	  conn->buf           = NULL;
-	}
-      conn->state     = CONN_READING;
-    }
-  SL_RETURN(0, _("do_file_transfer"));
-}
-
-/* ------------------------------------------------------
- *
- *  MESSAGE TRANSFER
- *
- * ------------------------------------------------------ */
-static int do_message_transfer(sh_conn_t * conn, int state)
-{
-  client_t * this_client; 
-  char     * cmd;
-  char       hash[SH_MAXMSGLEN + KEY_LEN + KEY_LEN + 1];
-  char     * buffer;
-  int        clt_sev;
-  char     * ptok;
-  UINT32     ticks;
-  size_t     len;
-  int        i;
-  char     * test;
-  char       sigbuf[KEYBUF_SIZE];
-
-  SL_ENTER(_("do_message_transfer"));
-
-  if (state == SH_DO_READ)        /* finished reading */
-    {
-      TPT(( 0, FIL__, __LINE__, _("msg=<File transfer - entry.>\n")));
-      
-      /* -- Client requests challenge. --
-       */
-      if (0 == check_request_nerr ((char *) &(conn->head[3]), _("HELO")))
-	{
-	  int client_state;
-	  
-	  this_client = do_check_client(conn, &client_state);
-	  if (!this_client)
-	    SL_RETURN(client_state, _("do_message_transfer"));
-	  
-	  
-	  /* -- create a nonce and send it --
-	   */
-	  conn->client_entry = this_client;
-	  sl_strlcpy (conn->peer, &(conn->buf[KEY_LEN]), SH_MINIBUF+1);
-	  
-	  ticks = (UINT32) taus_get ();
-	  
-	  test = (char *) &ticks;
-	  sh_util_cpylong (conn->challenge, test, 4);
-	  conn->challenge[4] = '\0';
-	  for (i = 0; i < 4; ++i)
-	    if (conn->challenge[i] == '\0')
-	      conn->challenge[i] = 0x01;
-      
-	  sh_xfer_prep_send (conn, conn->challenge, 5, _("TALK"), 
-				SH_PROTO_MSG);
-	  TPT(( 0, FIL__, __LINE__, _("msg=<Sent %s.>\n"), 
-		hu_trans(conn->challenge)));
-	}
-      
-      /* Client has send a message. Check whether we are in proper
-       * state, and verify message.
-       */
-      else if (0 == 
-	       check_request_nerr((char *)&(conn->head[3]), _("MESG")) &&
-	       conn->client_entry != NULL                           &&
-	       conn->client_entry->session_key[0] != '\0'           &&
-	       (len = sl_strlen(conn->buf) - KEY_LEN) > 0           &&
-	       sl_strlen(conn->challenge) == 4)
-	{
-	  TPT(( 0, FIL__, __LINE__, 
-		_("msg=<Message transfer - MESG (3).>\n")));
-	  
-#ifdef SH_ENCRYPT
-	  if (conn->client_entry->encf_flag == 0) {
-	    conn->client_entry->ency_flag = 0;
-	  }
-	  if ((conn->client_entry->ency_flag != 0) && 
-	      ((conn->head[0] & SH_PROTO_ENC) == 0)) 
-	    {
-	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_MISENC,
-			      _("message transfer"), 
-			      _("version2"),
-			      _("none"));
-	    }
-	  else if ((conn->client_entry->ency_flag != 0) &&
-		   ((conn->head[0] & SH_MASK_ENC) != 
-		    conn->client_entry->ency_flag))
-	    {
-	      sh_error_handle(SH_ERR_NOTICE, FIL__, __LINE__, 0, 
-			      MSG_TCP_MISENC,
-			      _("message transfer"), 
-			      _("version2"),
-			      ((conn->head[0] & SH_PROTO_ENC) == SH_PROTO_ENC) ? _("version2") : _("invalid"));
-	      conn->client_entry->ency_flag = 
-		(conn->head[0] & SH_MASK_ENC); 
-	    }
-#else
-	  if ((conn->head[0] & SH_PROTO_ENC) != 0) 
-	    {
-	      sh_error_handle((-1), FIL__, __LINE__, 0, 
-			      MSG_TCP_MISENC,
-			      _("message transfer"), 
-			      _("none"), 
-			      ((conn->head[0] & SH_PROTO_ENC) == SH_PROTO_ENC) ? _("version2") : _("invalid"));
-	    }
-#endif
-	  
-	  TPT(( 0, FIL__, __LINE__, _("msg=<Rcvt %s.>\n"), conn->buf));
-	  /* get hash from message end, truncate message
-	   */
-	  sl_strlcpy(hash, &(conn->buf[len]), KEY_LEN+1);
-	  conn->buf[len] = '\0';
-	  
-	  /* verify hash
-	   */
-	  buffer = sh_util_strconcat(conn->buf, conn->challenge, NULL);
-	  i =  sl_ts_strncmp(hash, 
-			     sh_util_siggen(conn->client_entry->session_key,
-					    buffer,
-					    sl_strlen(buffer),
-					    sigbuf, sizeof(sigbuf)),
-			     KEY_LEN);
-	  TPT((0, FIL__, __LINE__, _("msg=<sign %s.>\n"),
-	       sh_util_siggen(conn->client_entry->session_key,
-			      buffer,
-			      sl_strlen(buffer),
-			      sigbuf, sizeof(sigbuf))));
-	  
-	  
-	  if (0 != i)
-	    {
-	      TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
-	      status_update (conn->client_entry, CLT_FAILED);
-	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
-			      _("Msg signature mismatch"), conn->peer);
-	      conn->client_entry->session_key_timer = 
-		time(NULL) - (2*TIMEOUT_KEY);
-	      sh_xfer_do_free (conn);
-	      SL_RETURN(0, _("do_message_transfer"));
-	    }
-	  else
-	    {
-	      conn->client_entry->last_connect = time (NULL);
-	      
-	      if (NULL != sl_strstr(conn->buf,      _("EXIT")))
-		{
-		  TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
-		  conn->client_entry->exit_flag = 1;
-		  status_update (conn->client_entry, CLT_EXITED);
-		}
-	      else if (NULL != sl_strstr(conn->buf, _("PANIC")))
-		{
-		  TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
-		  status_update (conn->client_entry, CLT_PANIC);
-		}
-	      else if (NULL != sl_strstr(conn->buf, _("SUSPEND")))
-		{
-		  TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
-		  status_update (conn->client_entry, CLT_SUSPEND);
-		}
-	      else if (NULL != sl_strstr(conn->buf, _("POLICY")))
-		{
-		  TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
-		  status_update (conn->client_entry, CLT_POLICY);
-		}
-	      else if (NULL != sl_strstr(conn->buf, 
-					 _("File check completed")))
-		{
-		  TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
-		  status_update (conn->client_entry, CLT_CHECK);
-		}
-	      else if (NULL != sl_strstr(conn->buf, _("START")))
-		{
-		  TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
-		  sh_socket_add2reload (conn->client_entry->hostname);
-		  if (conn->client_entry->status_now == CLT_SUSPEND) {
-		    status_update (conn->client_entry, CLT_ILLEGAL);
-		    sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_ILL,
-				    conn->peer);
-		  }
-		  else
-		    status_update (conn->client_entry, CLT_STARTED);
-		}
-	      else
-		{
-		  TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
-		  if (NULL != sl_strstr(conn->buf, 
-					_("Runtime configuration reloaded")))
-		    {
-		      sh_socket_add2reload (conn->client_entry->hostname);
-		    }
-		  status_update (conn->client_entry, CLT_MSG);
-		}
-	      
-	      TPT((0, FIL__, __LINE__, _("msg=<status updated>\n")));
-	      clt_sev   = atoi(conn->buf);
-	      clt_class = (-1);
-	      ptok    = strchr(conn->buf, '?');
-	      if (ptok != NULL)
-		{
-		  ++ptok;
-		  if (ptok != NULL && sh.flag.client_class == S_TRUE) 
-		    clt_class = atoi(ptok);  /* is a global */
-		  ptok = strchr(ptok, '?');
-		  if (ptok != NULL) 
-		    ++ptok;
-		}
-	      if (sh.flag.client_severity == S_FALSE)
-		clt_sev = (-1);
-	      
-	      /* here we expect an xml formatted message, thus we don't
-		 escape xml special chars (flag == 0) */
-	      ptok = 
-		sh_tools_safe_name ((ptok!=NULL) ? ptok : conn->buf, 0);
-	      
-	      /* push client name to error routine
-	       */
-#if defined(SH_WITH_SERVER) && defined(HAVE_LIBPRELUDE)
-	      {
-		char peer_ip[SH_IP_BUF];
-		sh_ipvx_ntoa(peer_ip, sizeof(peer_ip), &(conn->addr_peer)); 
-		sh_error_set_peer_ip( peer_ip );
-	      }                        
-#endif
-	      {
-		char * pstrip = sh_strip_domain (conn->peer);
-		sh_error_set_peer(pstrip);
-		sh_error_handle(clt_sev, FIL__, __LINE__, 0, MSG_TCP_MSG,
-				pstrip, 
-				ptok);
-		SH_FREE(pstrip);
-		sh_error_set_peer(NULL);
-	      }
-#if defined(SH_WITH_SERVER) && defined(HAVE_LIBPRELUDE)
-	      sh_error_set_peer_ip(NULL);
-#endif
-	      
-	      TPT((0, FIL__, __LINE__, _("msg=<%s>\n"), ptok));
-	      SH_FREE(ptok);
-	      clt_class = (-1);
-	    }
-	  memset(buffer, 0, sl_strlen(buffer));
-	  SH_FREE(buffer);
-	  
-	  /* SERVER CONF SEND
-	   */
-	  buffer = sh_util_strconcat(conn->buf,
-				     conn->challenge,
-				     NULL);
-	  sl_strlcpy(hash, 
-		     sh_util_siggen ( conn->client_entry->session_key,
-				      buffer,
-				      sl_strlen(buffer),
-				      sigbuf, sizeof(sigbuf)),
-		     KEY_LEN+1);
-	  
-	  /* --- SERVER CMD --- */
-	  cmd = sh_socket_check (conn->peer);
-	  
-	  if (cmd != NULL)
-	    {
-	      /* max cmd size is SH_MAXMSGLEN bytes
-	       */
-	      sl_strlcpy(&hash[KEY_LEN], cmd, SH_MAXMSGLEN);
-	      sl_strlcat(&hash[KEY_LEN],
-			 sh_util_siggen ( conn->client_entry->session_key,
-					  &hash[KEY_LEN],
-					  sl_strlen(&hash[KEY_LEN]),
-					  sigbuf, sizeof(sigbuf)),
-			 SH_MAXMSGLEN+KEY_LEN+1);
-	      
-	      TPT((0, FIL__, __LINE__, _("CONF SEND <0> <%s>\n"), 
-		   &hash[KEY_LEN]));
-	      
-	    } else {
-	    
-	    TPT((0, FIL__, __LINE__, _("CONF SEND <0> <[NULL]>\n")));
-	    
-	  }
-	  /* --- SERVER CMD END --- */
-	  
-	  TPT((0, FIL__, __LINE__, _("msg=<sign %s.>\n"),
-	       sh_util_siggen(conn->client_entry->session_key,
-			      buffer,
-			      sl_strlen(buffer),
-			      sigbuf, sizeof(sigbuf))));
-	  
-#ifdef SH_ENCRYPT
-	  sh_xfer_send_crypt (conn, hash, 
-			      sl_strlen(hash) /* KEY_LEN */, 
-			      _("CONF"), 
-			      SH_PROTO_MSG|SH_PROTO_END|conn->client_entry->ency_flag);
-#else
-	  sh_xfer_send_crypt (conn, hash, 
-			      sl_strlen(hash) /* KEY_LEN */, 
-			      _("CONF"), 
-			      SH_PROTO_MSG|SH_PROTO_END);
-#endif
-	  
-	  memset(buffer, 0, sl_strlen(buffer));
-	  SH_FREE(buffer);
-	  
-	  /* sh_xfer_do_free (conn); */
-	}
-      
-      /* client does something unexpected
-       */
-      else  /* ---- ??? ----- */
-	{
-	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FINV,
-			  2, conn->pass, conn->peer,  
-			  '\\', conn->head[3], '\\',conn->head[4],
-			  '\\', conn->head[5], '\\',conn->head[6]);
-	  status_update (conn->client_entry, CLT_FAILED);
-	  conn->client_entry->session_key_timer = 
-	    time(NULL) - (2*TIMEOUT_KEY);
-	  sh_xfer_do_free (conn);
-	}
-    }
-
-  else if (state == SH_DO_WRITE)  /* finished writing */
-    {
-      if (0 != (conn->head[0] & SH_PROTO_END))
-	{
-	  if (flag_err_debug == S_TRUE)
-	    {
-	      char * pstrip = sh_strip_domain (conn->peer);
-	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_OKMSG,
-			      pstrip);
-	      SH_FREE(pstrip);
-	    }
-	  sh_xfer_do_free (conn);
-	  SL_RETURN(0, _("do_message_transfer"));
-	}
-      
-      TPT(( 0, FIL__, __LINE__, _("msg=<Msg transfer - (wait).>\n")));
-      
-      /* challenge is sent, now wait for message from client
-       */
-      conn->headcount     = 0;
-      conn->bytecount     = 0;
-      conn->bytes_to_send = 0;
-      conn->bytes_to_get  = 0;
-      if (conn->buf != NULL) 
-	{
-	  SH_FREE(conn->buf);
-	  conn->buf           = NULL;
-	}
-      conn->state     = CONN_READING;
-    }
-  TPT((0, FIL__, __LINE__, _("msg=<return>\n") ));
-  SL_RETURN(0, _("do_message_transfer"));
-}
-
-/* ------------------------------------------------------
- *
- *  AUTHENTICATION
- *
- * ------------------------------------------------------ */
-
-static void check_probe(sh_conn_t * conn)
-{
-  if (conn && conn->client_entry)
-    {
-      /* If client has sent probe, change ivst_flag and clear probe in head[0].
-       */
-      conn->head[0] = sh_tools_probe_store(conn->head[0], 
-					   &(conn->client_entry->ivst_flag));
-    }
-}
-
-client_t * do_auth_start(sh_conn_t * conn)
-{
-  client_t * this_client;
-
-  TPT((0, FIL__, __LINE__, 
-       _("msg=<Authentication - SALT (1).>\n")));
-  
-  if (conn->buf == NULL || sl_strlen(conn->buf) == 0)
-    {
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NOCLT);
-      sh_xfer_do_free (conn);
-      return NULL;
-    } 
-                  
-  /* search the register
-   */
-  
-  this_client = search_register (conn, 0);
-  if (NULL == this_client)
-    return NULL;
-    
-  conn->client_entry = this_client;
-  sl_strlcpy (conn->peer, conn->buf, SH_MINIBUF+1);
-  
-  if (0 != check_request_s((char *)&(conn->head[3]), 
-			   _("SALT"),conn->peer))
-    {
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
-		      _("No salt requested"), conn->peer);
-      status_update (conn->client_entry, CLT_FAILED);
-      conn->client_entry->session_key_timer = 
-	time(NULL) - (2*TIMEOUT_KEY);
-      sh_xfer_do_free (conn);
-      return NULL;
-    }
-
-  check_probe(conn);
-  return this_client;
-}
-
-#if !defined(USE_SRP_PROTOCOL)
-
-int do_auth(sh_conn_t * conn)
-{
-  client_t * this_client; 
-  UINT32     ticks;
-  char       u[5] = "OOOO";
-#ifdef SH_ENCRYPT
-  int        err_num;
-  char expbuf[SH_ERRBUF_SIZE];
-#endif
-  char       hash[SH_MAXMSGLEN + KEY_LEN + KEY_LEN + 1];
-  char hashbuf[KEYBUF_SIZE];
-
-  /* first pass -- client request salt  
-   */
-  if (conn->pass    == 1) 
-    {
-      this_client = do_auth_start(conn);
-
-      if (!this_client)
-	return -1;
-
-      /* -- create server nounce v --
-       */
-      ticks = (UINT32) taus_get ();
-      
-      if (conn->A != NULL)
-	{
-	  SH_FREE(conn->A);
-	  conn->A = NULL;
-	}
-      conn->A = SH_ALLOC(KEY_LEN+1);
-      
-      sl_strlcpy(conn->A, 
-		 sh_tiger_hash((char *) &ticks, 
-			       TIGER_DATA, sizeof(UINT32), 
-			       hashbuf, sizeof(hashbuf)),
-		 KEY_LEN+1);
-      u[0] = 'I'; u[1] = 'N'; u[2] = 'I'; u[3] = 'T'; u[4] = '\0';
-      
-      if (conn->M1 != NULL)
-	{
-	  SH_FREE(conn->M1);
-	  conn->M1 = NULL;
-	}
-      conn->M1 = SH_ALLOC(2*KEY_LEN+1);
-      
-      /* compute hash key H(v(server), P)v(server)
-       */
-      sh_passwd (conn->A, conn->client_entry->verifier, 
-		 NULL, conn->M1);
-      
-      sl_strlcat(conn->M1, conn->A, 2*KEY_LEN+1);
-      
-      
-      /* --- send H(v(server), P)v(server) ----
-       */
-      sh_xfer_prep_send (conn, 
-			    conn->M1, 
-			    sl_strlen(conn->M1), 
-			    u, 
-			    (conn->head[0]|SH_PROTO_SRP));
-      
-      SH_FREE(conn->M1); 
-      conn->M1 = NULL;
-    }
-  
-  /* client -- third pass
-   * Message is H(H(u,v),P)u
-   *
-   * A := v, verifier := H(password), 
-   */
-  else if (conn->pass    == 3                   && 
-	   conn->client_entry != NULL)
-    {
-      
-      TPT((0, FIL__, __LINE__, 
-	   _("msg=<Authentication - PASS (3).>\n")));
-      
-      if (0 != check_request_s((char *) &(conn->head[3]), _("PASS"), 
-			       conn->peer)                    ||
-	  sl_strlen(conn->buf) <= KEY_LEN                        ||
-	  conn->A == NULL)
-	{
-	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
-			  _("Invalid client request"), conn->peer);
-	  status_update (conn->client_entry, CLT_FAILED);
-	  conn->client_entry->session_key_timer = 
-	    time(NULL) - (2*TIMEOUT_KEY);
-	  sh_xfer_do_free (conn);
-	  return -1;
-	} 
-
-      check_probe(conn);
-
-      /* store random nonce u from client
-       */
-      if (conn->K != NULL)
-	{
-	  SH_FREE(conn->K);
-	  conn->K = NULL;
-	}
-      conn->K = SH_ALLOC(KEY_LEN+1);
-      sl_strlcpy(conn->K, &(conn->buf[KEY_LEN]), KEY_LEN+1);
-      
-      /* verify random nonce u from client
-       */
-      if (conn->M1 != NULL)
-	{
-	  SH_FREE(conn->M1);
-	  conn->M1 = NULL;
-	}
-      conn->M1 = sh_util_strconcat(conn->K, conn->A, NULL);
-      
-      TPT((0, FIL__, __LINE__, _("msg=<c/r: K = %s>\n"), conn->K));
-      TPT((0, FIL__, __LINE__, _("msg=<c/r: A = %s>\n"), conn->A));
-      TPT((0, FIL__, __LINE__, _("msg=<c/r: M = %s>\n"), conn->M1));
-      
-      sl_strlcpy(hash, sh_tiger_hash (conn->M1, 
-				      TIGER_DATA, 
-				      sl_strlen(conn->M1),
-				      hashbuf, sizeof(hashbuf)), 
-		 KEY_LEN+1); 
-      sh_passwd (hash, conn->client_entry->verifier, NULL, conn->M1);
-      
-      TPT((0, FIL__, __LINE__, _("msg=<c/r: H = %s>\n"), hash));
-      TPT((0, FIL__, __LINE__, _("msg=<c/r: P = %s>\n"), conn->M1));
-      
-      if ( 0 != sl_ts_strncmp(conn->M1, conn->buf, KEY_LEN))
-	{
-	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
-			  _("Session key mismatch"), conn->peer);
-	  status_update (conn->client_entry, CLT_FAILED);
-	  conn->client_entry->session_key_timer = 
-	    time(NULL) - (2*TIMEOUT_KEY);
-	  sh_xfer_do_free (conn);
-	  return -1;
-	} 
-      
-      /* ---- compute hash key H(v, P, u) ----
-       */
-      sh_passwd (conn->A, conn->client_entry->verifier, conn->K,
-		 conn->M1);
-      
-      sl_strlcpy(conn->client_entry->session_key, 
-		 conn->M1, KEY_LEN+1);
-      TPT((0, FIL__, __LINE__, _("msg=<c/r: Key = %s>\n"), 
-	   conn->client_entry->session_key));
-      
-#ifdef SH_ENCRYPT
-      err_num = rijndael_makeKey(&(conn->client_entry->keyInstE), 
-				 DIR_ENCRYPT, 192, 
-				 conn->client_entry->session_key);
-      if (err_num < 0)
-	sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
-			errorExplain(err_num, expbuf, sizeof(expbuf)), 
-			_("check_protocol: makeKey"));
-      err_num = rijndael_makeKey(&(conn->client_entry->keyInstD), 
-				 DIR_DECRYPT, 192, 
-				 conn->client_entry->session_key);
-      if (err_num < 0)
-	sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
-			errorExplain(err_num, expbuf, sizeof(expbuf)), 
-			_("check_protocol: makeKey"));
-#endif
-      
-      if (conn->K  != NULL) SH_FREE (conn->K);
-      conn->K  = NULL;
-      if (conn->A  != NULL) SH_FREE (conn->A);
-      conn->A  = NULL;
-      if (conn->M1 != NULL) SH_FREE (conn->M1);
-      conn->M1 = NULL;
-      
-      /* if (conn->client_entry->status_now == CLT_STARTED */
-      if (((conn->client_entry->status_now != CLT_INACTIVE) &&
-	   (conn->client_entry->status_now != CLT_EXITED)   &&
-	   (conn->client_entry->status_now != CLT_SUSPEND))
-	  && conn->client_entry->session_key_timer > (time_t) 1)
-	{
-	  status_update (conn->client_entry, CLT_ILLEGAL);
-	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_ILL,
-			  conn->peer);
-	}
-      else if (conn->client_entry->session_key_timer == (time_t) 0)
-	{
-	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NEW,
-			  conn->peer);
-	  if (conn->client_entry->status_now != CLT_SUSPEND)
-	    status_update (conn->client_entry, CLT_STARTED);
-	}
-      
-      conn->client_entry->session_key_timer = time (NULL);
-      conn->client_entry->last_connect = time (NULL);
-      
-      /* put in read state
-       */
-      sh_xfer_prep_send (conn, 
-			    _("AUTH"),
-			    5, 
-			    _("AUTH"), 
-			    (conn->head[0]|SH_PROTO_SRP));
-      
-    }
-  else
-    {
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FINV,
-		      3, conn->pass, conn->peer, 
-		      '\\', conn->head[3], '\\', conn->head[4],
-		      '\\', conn->head[5], '\\', conn->head[6]);
-      sh_xfer_do_free (conn);
-    }
-  return 0;
-}
-
-#else
-
-static void noise()
-{
-  UINT32 n = taus_get();
-  retry_msleep(0, (n & 0x0000007F));
-  return;
-}
-  
-/* use SRP */
-
-int do_auth(sh_conn_t * conn)
-{
-  client_t * this_client; 
-  UINT32     ticks;
-  char       u[5] = "OOOO";
-#ifdef SH_ENCRYPT
-  int        err_num;
-  char expbuf[SH_ERRBUF_SIZE];
-#endif
-  size_t     len;
-  char     * test;
-  char     * foo_B;
-  char     * foo_Ss;
-  char hashbuf[KEYBUF_SIZE];
-
-  /* use SRP                    
-   */
-  if (conn->pass == 1)
-    {
-      this_client = do_auth_start(conn);
-      if (!this_client)
-	return -1;
-
-      u[0] = 'I'; u[1] = 'N'; u[2] = 'I'; u[3] = 'T'; u[4] = '\0';
-      sh_xfer_prep_send (conn, 
-			    conn->client_entry->salt, 
-			    sl_strlen(conn->client_entry->salt), 
-			    u, 
-			    (conn->head[0]|SH_PROTO_SRP));
-    }
-
-  /* client has sent A -- third pass
-   */
-  else if (conn->pass == 3                    && 
-	   conn->client_entry != NULL)
-    {
-      
-      TPT((0, FIL__, __LINE__, 
-	   _("msg=<Authentication - PC01 (3).>\n")));
-      
-      if (0 != check_request_s((char *)&(conn->head[3]),_("PC01"),conn->peer)||
-	  conn->buf == NULL
-	  )
-	{
-	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
-			  _("Invalid client request"), conn->peer);
-	  status_update (conn->client_entry, CLT_FAILED);
-	  conn->client_entry->session_key_timer = 
-	    time(NULL) - (2*TIMEOUT_KEY);
-	  sh_xfer_do_free (conn);
-	  return -1;
-	} 
-      
-      check_probe(conn); noise();
-
-      if (0 != sh_srp_init())
-	{
-	  status_update (conn->client_entry, CLT_FAILED);
-	  sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0, 
-			  MSG_TCP_EBGN);
-	  sh_xfer_do_free (conn);
-	  return -1;
-	}
-      
-      
-      /* check A, only send B if correct 
-       */
-      if ( sl_strlen(conn->buf) < SH_BUFSIZE && 
-	   0 == sh_srp_check_zero (conn->buf) )
-	{
-	  len = sl_strlen(conn->buf)+1;
-	  
-	  if (conn->A != NULL)
-	    {
-	      SH_FREE(conn->A);
-	      conn->A = NULL;
-	    }
-	  conn->A = SH_ALLOC(len);
-	  sl_strlcpy (conn->A, conn->buf, len);
-	  
-	  /* 
-	   * compute B 
-	   */
-	  if (0 != sh_srp_make_a ())     /* b        random number */
-	    {
-	      status_update (conn->client_entry, CLT_FAILED);
-	      
-	      sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0, 
-			      MSG_TCP_EBGN);
-	      sh_srp_exit();
-	      sh_xfer_do_free (conn);
-	      return -1;
-	    }
-	  
-	  foo_B = sh_srp_B               /* B = v + g^b            */
-	    (conn->client_entry->verifier);
-	  
-	  if (foo_B == NULL)
-	    {
-	      status_update (conn->client_entry, CLT_FAILED);
-	      
-	      sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0, 
-			      MSG_TCP_EBGN);
-	      sh_srp_exit();
-	      sh_xfer_do_free (conn);
-	      return -1;
-	    }
-	  
-	  TPT((0, FIL__, __LINE__, _("msg=<srp: A = %s>\n"), conn->A));
-	  TPT((0, FIL__, __LINE__, _("msg=<srp: B = %s>\n"), foo_B));
-	  
-	  /* 
-	   * create nonce u 
-	   */
-	  ticks = (UINT32) taus_get ();
-	  
-	  test = (char *) &ticks;
-	  sh_util_cpylong (u, test, 4);  /* u        nounce        */
-	  u[4] = '\0';
-	  sl_strlcpy(conn->challenge, 
-		     sh_tiger_hash(u, TIGER_DATA, 4, hashbuf, sizeof(hashbuf)),
-		     SH_CHALLENGE_SIZE);
-	  
-	  TPT((0, FIL__, __LINE__, _("msg=<srp: u = %03o-%03o-%03o-%03o>\n"), u[0], u[1], u[2], u[3]));
-	  TPT((0, FIL__, __LINE__, _("msg=<srp: U = %s>\n"), 
-	       conn->challenge));
-	  
-	  /* 
-	   * compute the session key K and M1 = Hash(A,B,K)
-	   */
-	  foo_Ss = sh_srp_S_s (conn->challenge, 
-			       conn->A, 
-			       conn->client_entry->verifier);
-	  
-	  if (foo_Ss == NULL || 0 != sh_srp_check_zero (foo_Ss))
-	    {
-	      status_update (conn->client_entry, CLT_FAILED);
-	      
-	      sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0, 
-			      MSG_TCP_EBGN);
-	      sh_srp_exit();
-	      sh_xfer_do_free (conn);
-	      return -1;
-	    }
-	  
-	  if (conn->K != NULL)
-	    {
-	      SH_FREE(conn->K);
-	      conn->K = NULL;
-	    }
-	  conn->K = SH_ALLOC(KEY_LEN+1);
-	  sl_strlcpy(conn->K, 
-		     sh_tiger_hash(foo_Ss, TIGER_DATA, 
-				   sl_strlen(foo_Ss), 
-				   hashbuf, sizeof(hashbuf)),
-		     KEY_LEN+1);
-	  
-	  if (conn->M1 != NULL)
-	    {
-	      SH_FREE(conn->M1);
-	      conn->M1 = NULL;
-	    }
-	  conn->M1 = SH_ALLOC(KEY_LEN+1);
-	  sh_srp_M (conn->A, foo_B, conn->K, conn->M1, KEY_LEN+1);
-	  
-	  TPT((0, FIL__, __LINE__, _("msg=<srp:Ss = %s>\n"), foo_Ss));
-	  TPT((0, FIL__, __LINE__, _("msg=<srp: K = %s>\n"), conn->K));
-	  TPT((0, FIL__, __LINE__, _("msg=<srp:M1 = %s>\n"),conn->M1));
-	  
-	  /*
-	   * send B
-	   */
-	  sh_xfer_prep_send (conn, 
-				foo_B,
-				sl_strlen(foo_B)+1,
-				u,
-				(conn->head[0]|SH_PROTO_SRP));
-	  if (foo_Ss != NULL)
-	    {
-	      SH_FREE(foo_Ss);
-	      foo_Ss = NULL;
-	    }
-	  if (foo_B  != NULL)
-	    {
-	      SH_FREE(foo_B);
-	      foo_B = NULL;
-	    }
-	}
-      else
-	{
-	  status_update (conn->client_entry, CLT_FAILED);
-	  
-	  sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0, 
-			  MSG_TCP_EZERO);
-	  sh_xfer_do_free (conn);
-	}
-      
-      sh_srp_exit();
-    }
-  
-  /* client has sent M1 -- fifth pass
-   */
-  else if (conn->pass    == 5           && 
-	   conn->client_entry != NULL) 
-    {
-      TPT((0, FIL__, __LINE__, 
-	   _("msg=<Authentication - PC02 (5).>\n")));
-      
-      /* check that the state is valid
-       */
-      if (0 != check_request_s((char *)&(conn->head[3]), _("PC02"),
-			       conn->peer)                   ||
-	  conn->A == NULL || conn->K == NULL || conn->M1 == NULL)
-	{
-	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
-			  _("Invalid client request"), conn->peer);
-	  status_update (conn->client_entry, CLT_FAILED);
-	  conn->client_entry->session_key_timer = 
-	    time(NULL) - (2*TIMEOUT_KEY);
-	  sh_xfer_do_free (conn);
-	  return -1;
-	} 
-
-      check_probe(conn); noise();
-
-      /* ------ verify M1 = H(A,  B, K) -------
-       * -----    send M2 = H(A, M1, K) -------
-       */
-      if (conn->buf != NULL && 
-	  sl_ts_strncmp(conn->buf, conn->M1, KEY_LEN) == 0)
-	{
-	  /*
-	   * send M2
-	   */
-	  char M_buf[KEY_LEN+1];
-	  sh_xfer_prep_send (conn, 
-				sh_srp_M (conn->A, conn->M1, conn->K,
-					  M_buf, sizeof(M_buf)),
-				KEY_LEN+1,
-				_("PARP"),
-				(conn->head[0]|SH_PROTO_SRP));
-	  
-	  if (conn->A  != NULL)
-	    SH_FREE(conn->A);
-	  conn->A  = NULL;
-	  if (conn->M1 != NULL)
-	    SH_FREE(conn->M1);
-	  conn->M1 = NULL;
-	  sl_strlcpy(conn->client_entry->session_key, 
-		     conn->K, KEY_LEN+1);
-	  TPT((0, FIL__, __LINE__, _("msg=<key %s>\n"), 
-	       conn->client_entry->session_key));
-	  
-#ifdef SH_ENCRYPT
-	  err_num = rijndael_makeKey(&(conn->client_entry->keyInstE), 
-				     DIR_ENCRYPT, 192, 
-				     conn->client_entry->session_key);
-	  if (err_num < 0)
-	    sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
-			    errorExplain(err_num, expbuf, sizeof(expbuf)), 
-			    _("sh_xfer_prep_send_int: makeKey"));
-	  err_num = rijndael_makeKey(&(conn->client_entry->keyInstD), 
-				     DIR_DECRYPT, 192, 
-				     conn->client_entry->session_key);
-	  if (err_num < 0)
-	    sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
-			    errorExplain(err_num, expbuf, sizeof(expbuf)), 
-			    _("sh_xfer_prep_send_int: makeKey"));
-#endif
-	  
-	  if (conn->K  != NULL)
-	    SH_FREE(conn->K);
-	  conn->K  = NULL;
-	  
-	  conn->client_entry->last_connect = time (NULL);
-	  
-	  if (((conn->client_entry->status_now != CLT_INACTIVE) &&
-	       (conn->client_entry->status_now != CLT_EXITED)   &&
-	       (conn->client_entry->status_now != CLT_SUSPEND))
-	      && conn->client_entry->session_key_timer > (time_t) 1)
-	    {
-	      status_update (conn->client_entry, CLT_ILLEGAL);
-	      
-	      sh_error_handle((-1), FIL__, __LINE__, 0, 
-			      MSG_TCP_ILL,
-			      conn->peer);
-	    }
-	  else if (conn->client_entry->session_key_timer == (time_t) 0)
-	    {
-	      sh_error_handle((-1), FIL__, __LINE__, 0, 
-			      MSG_TCP_NEW,
-			      conn->peer);
-	      if (conn->client_entry->status_now != CLT_SUSPEND)
-		status_update (conn->client_entry, CLT_STARTED);
-	    }
-	  conn->client_entry->session_key_timer = time (NULL);
-	  
-	}
-      else
-	{
-	  status_update (conn->client_entry, CLT_FAILED);
-	  conn->client_entry->session_key_timer = 
-	    time(NULL) - (2*TIMEOUT_KEY);
-	  
-	  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
-			  _("Session key mismatch"), conn->peer);
-	  sh_xfer_do_free (conn);
-	} 
-    }
-  
-  else
-    {
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FINV,
-		      4, conn->pass, conn->peer, 
-		      '\\', conn->head[3], '\\', conn->head[4],
-		      '\\', conn->head[5], '\\', conn->head[6]);
-      sh_xfer_do_free (conn);
-    }
-  return 0;
-}
-#endif
-
-/************************************************************************
- *
- * Here we check the message received, and decide on the answer to send
- * (if any). The connection is in CONN_PAUSED state, thus we must:
- * (i)   define the proper reaction
- * (ii)  reset to CONN_READING or CONN_WRITING or CONN_FREE
- * (iii) eventually reset the connection entry
- *
- *************************************************************************/
-static
-void check_protocol(sh_conn_t * conn, int state)
-{
-  SL_ENTER(_("check_protocol"));
-
-  /* seed / re-seed the PRNG if required
-   */
-  (void) taus_seed();
-
-  /* protocols: 
-   * -- (iii)    file transfer
-   * -- (ii)     authenticated message transfer
-   * -- (i)      SRP key exchange
-   */
-
-  /* --------- FILE TRANSFER  -----------
-   */
-  if ( (conn->head[0] & SH_PROTO_SRP) == 0  &&
-       (conn->head[0] & SH_PROTO_BIG) != 0  /* is set */ )
-    {
-      /* nonzero means re-authentication is required
-       */
-      if (0 == do_file_transfer(conn, state))
-	SL_RET0(_("check_protocol"));
-    }
-  /* --------- END FILE TRANSFER ----------- */
-
-
-  /* ---------  message exchange  -----------
-   */
-  else if ((conn->head[0] & SH_PROTO_SRP) == 0  && 
-	   (conn->head[0] & SH_PROTO_MSG) != 0  /* is set */ )
-    {
-      /* nonzero means re-authentication is required
-       */
-      if (0 == do_message_transfer(conn, state))
-	SL_RET0(_("check_protocol"));
-    }
-  /* ---------  END MESSAGE TRANSFER ------ */
-
-  /* ---------  authentication  -----------
-   */
-  if ( (conn->head[0] & SH_PROTO_SRP) != 0   /* is set */ )
-    {
-      if (state == SH_DO_READ)        /* finished reading */
-	{
-	  do_auth(conn);
-	}
-
-      else if (state == SH_DO_WRITE)  /* finished writing */
-	{
-	  TPT((0, FIL__, __LINE__, _("msg=<Authentication -- (wait).>\n")));
-
-	  conn->headcount     = 0;
-	  conn->bytecount     = 0;
-	  conn->bytes_to_send = 0;
-	  conn->bytes_to_get  = 0;
-	  if (conn->buf != NULL) 
-	    {
-	      SH_FREE(conn->buf);
-	      conn->buf           = NULL;
-	    }
-	  conn->state     = CONN_READING;
-	}
-    }
-  SL_RET0(_("check_protocol"));
-}
-
-
-/***********************************************************
- *
- *    SERVER RECEIVE FUNCTION
- *
- ***********************************************************
- */
-int sh_xfer_do_read (sh_conn_t * conn)
-{
-  unsigned long   byteread;     /* bytes read         */
-
-  SL_ENTER(_("sh_xfer_do_read"));
-
-  if (conn->state == CONN_SENDING)
-    {
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_SYNC,
-		      conn->peer);
-      SL_RETURN( (-1), _("sh_xfer_do_read"));
-    }
-
-  if (conn->headcount < SH_HEADER_SIZE) 
-    {
-      conn->bytes_to_get = SH_HEADER_SIZE - conn->headcount;
-      byteread = read (conn->fd, &(conn->head[conn->headcount]),
-		       conn->bytes_to_get);
-      if (byteread > 0 || errno == EINTR) 
-	{
-	  if (byteread > 0) 
-	    conn->headcount += byteread;
-	  if (conn->headcount == SH_HEADER_SIZE)
-	    {
-	      conn->bytes_to_get = (256 * (unsigned int)conn->head[1] + 
-				    (unsigned int)conn->head[2]);
-	      SH_SHOWPROT(conn->head, '<');
-	      conn->bytecount = 0;
-	    }
-	}
-      else
-	goto conn_reset;
-      SL_RETURN( (0), _("sh_xfer_do_read"));
-    }
-
-  /* limit message size
-   */
-  conn->bytes_to_get = (conn->bytes_to_get > TRANS_BYTES) ? 
-    TRANS_BYTES : conn->bytes_to_get;
-
-  if (conn->headcount == SH_HEADER_SIZE && conn->bytes_to_get > 0)
-    {
-      if ( conn->bytecount == 0)
-	{
-	  if (conn->buf != NULL)
-	    SH_FREE (conn->buf);
-	  conn->buf = SH_ALLOC(conn->bytes_to_get + 1); /* <= TRANS_BYTES+1 */
-	}
-
-      byteread           = read (conn->fd, &(conn->buf[conn->bytecount]),
-				 conn->bytes_to_get - conn->bytecount);
-      if (byteread > 0 || errno == EINTR) 
-	{
-	  if (byteread > 0) 
-	    conn->bytecount    += byteread;
-	  if (conn->bytecount == conn->bytes_to_get) 
-	    {
-	      ++conn->pass;
-	      /* always terminate with NULL - we might use sl_strcmp() */
-	      conn->buf[conn->bytecount] = '\0';
-	      conn->state                = CONN_PAUSE;
-
-#ifdef SH_ENCRYPT
-	      if ((conn->head[0] & SH_PROTO_ENC) != 0) 
-		{
-		  conn->buf = sh_tools_revertPack (conn->head, 
-						   conn->client_entry->ivst_flag,
-						   conn->buf,
-						   &(conn->client_entry->keyInstD),
-						   conn->bytecount);
-		}
-#endif
-	      /* ------  HERE CALL check_protocol(conn) -------  */
-	      check_protocol(conn, SH_DO_READ);
-	    }
-	}
-      else
-	  goto conn_reset;
-    }
-
-  else if (conn->headcount == SH_HEADER_SIZE && conn->bytes_to_get == 0)
-    {
-      if (conn->buf != NULL)
-	SH_FREE (conn->buf);
-      conn->buf       = NULL;
-      conn->bytecount = 0;
-      ++conn->pass;
-      conn->state     = CONN_PAUSE;
-      /* ------  HERE CALL check_protocol(conn) -------  */
-      check_protocol(conn, SH_DO_READ);
-    }
-      
-  SL_RETURN( (0), _("sh_xfer_do_read"));
-
- conn_reset:
-  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_RESET,
-		  conn->peer);
-  sh_xfer_do_free ( conn );
-  SL_RETURN( (-1), _("sh_xfer_do_read"));
-}
-
-#if !defined(O_NONBLOCK)
-#if defined(O_NDELAY)
-#define O_NONBLOCK  O_NDELAY
-#else
-#define O_NONBLOCK  0
-#endif
-#endif
-
-/* send to the client
- */
-int sh_xfer_do_write (sh_conn_t * conn)
-{
-  int    flags;
-  long   arg = 0;
-  long   bytesent;     /* bytes read         */
-
-  SL_ENTER(_("sh_xfer_do_write"));
-
-  /* ---- consistency check ------
-   */
-  if (conn->state == CONN_READING)
-    {
-      sh_error_handle( (-1), FIL__, __LINE__, 0, MSG_TCP_SYNC,
-		      conn->peer);
-      SL_RETURN( (-1), _("sh_xfer_do_write"));
-    }
- 
-  flags = retry_fcntl (FIL__, __LINE__, conn->fd, F_GETFL, arg);
-  retry_fcntl (FIL__, __LINE__, conn->fd, F_SETFL,  flags|O_NONBLOCK);
-
-  /* ---- send the header ------
-   */
-  if (conn->headcount < SH_HEADER_SIZE) 
-    {
-      conn->bytes_to_send = SH_HEADER_SIZE - conn->headcount;
-      bytesent = write (conn->fd, &(conn->head[conn->headcount]), 
-			conn->bytes_to_send);
-      if (bytesent >= 0 || errno == EINTR || errno == EAGAIN) 
-	{
-	  if (bytesent > 0) 
-	    conn->headcount += bytesent;
-	  if (conn->headcount == SH_HEADER_SIZE) 
-	    conn->bytes_to_send = 
-	      (256 * (int)conn->head[1] + (int)conn->head[2]);
-	}
-      else 
-	goto conn_reset_w;
-
-      if (conn->fd >= 0)
-	retry_fcntl (FIL__, __LINE__, conn->fd, F_SETFL,  flags);
-      SL_RETURN( (0), _("sh_xfer_do_write"));
-    }
-
-  /* ---- send the body ------
-   */
-
-  if (conn->headcount == SH_HEADER_SIZE && conn->bytes_to_send > 0 &&
-      conn->buf != NULL)
-    {
-      bytesent = write (conn->fd, &(conn->buf[conn->bytecount]), 
-			conn->bytes_to_send - conn->bytecount);
-      if (bytesent >= 0 || errno == EINTR || errno == EAGAIN) 
-	{
-	  if (bytesent > 0) 
-	    conn->bytecount    += bytesent;
-	  if (conn->bytecount == conn->bytes_to_send) 
-	    {
-	      ++conn->pass;
-	      conn->state = CONN_PAUSE;
-	      /* ------  HERE CALL check_protocol(conn) -------  */
-	      check_protocol(conn, SH_DO_WRITE);
-	    }
-	}
-      else
-	goto conn_reset_w;
-    }
-      
-  else if (conn->headcount == SH_HEADER_SIZE && conn->bytes_to_send == 0)
-    {
-      ++conn->pass;
-      conn->state = CONN_PAUSE;
-      /* ------  HERE CALL check_protocol(conn) -------  */
-      check_protocol(conn, SH_DO_WRITE);
-    }
-
-  if (conn->fd >= 0)
-    retry_fcntl (FIL__, __LINE__, conn->fd, F_SETFL,  flags);
-  SL_RETURN( (0), _("sh_xfer_do_write"));
-
- conn_reset_w:
-  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_RESET,
-		  conn->peer);
-  sh_xfer_do_free ( conn );
-  SL_RETURN( (-1), _("sh_xfer_do_write"));
-}
-
-/* accept a connection from a client
- */ 
-#include <syslog.h>
-#ifdef SH_USE_LIBWRAP
-#include <tcpd.h>
-
-#ifndef ALLOW_SEVERITY 
-#define ALLOW_SEVERITY LOG_INFO
-#define DENY_SEVERITY  LOG_WARNING
-#endif
-
-int allow_severity;
-int deny_severity;
-#endif
-
-#ifdef SH_USE_LIBWRAP
-static int check_libwrap(int rc, sh_conn_t * newconn)
-{
-  struct request_info request;
-  char                errbuf[128];
-  char                daemon[128];
-
-  sl_strlcpy(daemon, SH_INSTALL_NAME, sizeof(daemon));
-  request_init(&request, RQ_DAEMON, daemon, RQ_FILE, rc, 0);
-  fromhost(&request);
-  if (!hosts_access(&request)) 
-    {
-      sl_strlcpy(errbuf, _("Refused connection from "), sizeof(errbuf));
-      sl_strlcat(errbuf,   eval_client(&request), sizeof(errbuf));
-      
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		      errbuf, _("libwrap"));
-      newconn->fd    = -1;
-      newconn->state = CONN_FREE;
-      sl_close_fd(FIL__, __LINE__, rc);
-      return -1;
-    }
-  return 0;
-}
-#endif
-
-int sh_xfer_accept (int sock, sh_conn_t * newconn)
-{
-  int                errflag;
-  int                rc;
-  struct sh_sockaddr addr;
-  
-  /* handle AIX (size_t addrlen) in wrapper
-   */
-  int                addrlen = sizeof(addr);
-
-  SL_ENTER(_("sh_xfer_accept"));
-
-  rc = retry_accept(FIL__, __LINE__, sock, &addr, &addrlen);
-
-  if (rc < 0)
-    {
-      char err_buf[SH_ERRBUF_SIZE];
-      errflag = errno;
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		      sh_error_message(errflag,err_buf, sizeof(err_buf)), _("accept"));
-      newconn->fd    = -1;
-      newconn->state = CONN_FREE;
-      SL_RETURN( (-1), _("sh_xfer_accept"));
-    }
-
-  if (addrlen == 0)
-    {
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
-		      _("Connecting entity unknown"), _("accept"));
-      newconn->fd    = -1;
-      newconn->state = CONN_FREE;
-      sl_close_fd(FIL__, __LINE__, rc);
-      SL_RETURN( (-1), _("sh_xfer_accept"));
-    }
-
-#ifdef SH_USE_LIBWRAP
-  if (check_libwrap(rc, newconn) < 0)
-    SL_RETURN( (-1), _("sh_xfer_accept"));
-#endif
-
-  memcpy (&(newconn->addr_peer), &addr, sizeof(struct sh_sockaddr));
-
-  /* prepare for usage of connection
-   */
-  (void) retry_fcntl( FIL__, __LINE__, rc, F_SETFD, 1 );
-  newconn->fd           = rc;
-  newconn->state        = CONN_READING;
-  newconn->timer        = (unsigned long) time (NULL);
-  
-  if (flag_err_info == S_TRUE)
-    sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_CNEW, newconn->fd);
-
-  SL_RETURN( (0), _("sh_xfer_accept"));
-}
-
-extern char sh_sig_msg[64];  /* defined in sh_unix.c */
-
-/* ------------  port and interface -------
- */
-static unsigned int server_port = SH_DEFAULT_PORT;
-
-int sh_xfer_set_port (const char * str)
-{
-  int retval = 0;
-  unsigned long   i;
-  char * endptr;
-  
-  SL_ENTER(_("sh_xfer_set_port"));
-  i = strtoul (str, &endptr, 0);
-  if (endptr == str) {
-    retval = -1;
-  } else if (i > 65535) {
-    retval = -1;
-  } else {
-    server_port = i;
-  }
-  SL_RETURN( (retval), _("sh_xfer_set_port"));
-}
-
-static struct sh_sockaddr server_interface;
-static int            use_server_interface = 0;
-
-int sh_xfer_set_interface (const char * str)
-{
-  if (0 == strcmp(str, _("INADDR_ANY")))
-    {
-      use_server_interface = 0;
-      return 0;
-    }
-
-  if (0 == sh_ipvx_aton(str, &server_interface)) 
-    {
-      use_server_interface = 0;
-      return -1;
-    }
-
-  use_server_interface = 1;
-  return 0;
-}
-
-/* ------------  print error --------------
- */
-struct sock_err_st {
-  char msg[128];
-  int  errnum;
-  int  port;
-  int  line;
-  int  euid;
-};
-
-static struct sock_err_st sock_err[2];
-
-void sh_xfer_printerr(char * str, int errnum, unsigned int port, int line)
-{
-  int slot = 0;
-
-  if (port != server_port)
-    slot = 1;
-  if (str == NULL)
-    sock_err[slot].msg[0] = '\0';
-  else
-    sl_strlcpy(sock_err[slot].msg, str, 128);
-  sock_err[slot].errnum = errnum;
-  sock_err[slot].port   = port;
-  sock_err[slot].line   = line;
-  sock_err[slot].euid   = (int) geteuid();
-}
-
-int sh_xfer_printerr_final(int slot)
-{
-  char errbuf[SH_ERRBUF_SIZE];
-
-  SL_ENTER(_("sh_xfer_printerr_final"));
-  if (sock_err[slot].msg[0] != '\0')
-    {
-      dlog(1, FIL__, __LINE__, 
-	   _("Could not set up the listening socket for the server because of the\nfollowing error: %s\nPossible reasons include:\n - insufficient privilege for UID %d, or\n - the port %d is already used by another program.\n"),
-	   sh_error_message(sock_err[slot].errnum, errbuf, sizeof(errbuf)), 
-	   sock_err[slot].euid, 
-	   sock_err[slot].port);
-      sh_error_handle((-1), FIL__, sock_err[slot].line, 
-		      sock_err[slot].errnum, MSG_EXIT_ABORTS,
-		      sh_error_message(sock_err[slot].errnum, errbuf, sizeof(errbuf)),
-		      sh.prg_name,
-		      sock_err[slot].msg);
-      SL_RETURN((-1), _("sh_xfer_printerr_final"));
-    }
-  SL_RETURN(0, _("sh_xfer_printerr_final"));
-}
-
-#define  TIME_OUT_DEF 900
-static   unsigned long  time_out_val = TIME_OUT_DEF;
-
-int sh_xfer_set_timeout (const char * c)
-{
-  long val;
-
-  SL_ENTER(_("sh_xfer_set_time_out"));
-
-  val = strtol (c, (char **)NULL, 10);
-
-  if (val == 0)
-    {
-      val = TIME_OUT_DEF;
-    }
-  else if (val < 0)
-    {
-      time_out_val = TIME_OUT_DEF;
-      SL_RETURN( (-1), _("sh_xfer_set_time_out"));
-    }
-
-  time_out_val = (unsigned long) val;
-  SL_RETURN( (0), _("sh_xfer_set_time_out"));
-}
-
-
-static   sh_conn_t        * conns = NULL;
-static   int  maxconn = 0;  /* maximum number of simultaneous connections */
-
-
-#ifdef INET_SYSLOG
-#define INET_SUSPEND_TIME 180		/* equal to 3 minutes */
-#define SH_MINSOCK_DEFAULT 3
-int sh_xfer_syslog_sock[SH_SOCKMAX] = { -1 };
-extern int sh_xfer_recv_syslog_socket   (int fd);
-int sh_xfer_syslog_sock_n = 0;
-#else
-#define SH_MINSOCK_DEFAULT 2
-#endif
-
-int SH_MINSOCK = SH_MINSOCK_DEFAULT;
-
-extern int pf_unix_fd;
-
-/* the tcp socket, and the function to establish it
- */
-static int sh_tcp_sock[SH_SOCKMAX] = { -1 };
-static int sh_tcp_sock_n = 0;
-
-static int do_socket(int domain, int type, int protocol,
-		     struct sockaddr * sa, int salen)
-{
-  int sock = -1;
-  int errnum = 0;
-  int flag   = 1; /* non-zero to enable an option */
-
-  /* create the socket, bind() it and listen()
-   */
-  if ((sock = socket(domain, type, protocol)) < 0 )
-    {
-      errnum = errno; 
-      sh_xfer_printerr (_("socket"), errnum, server_port, __LINE__);
-      return -1;
-    }
-  (void) retry_fcntl( FIL__, __LINE__, sock, F_SETFD, 1 );
- 
-  if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
-		  (void *) &flag, sizeof(flag)) < 0 )
-    {
-      errnum = errno;
-      sh_xfer_printerr (_("setsockopt"), errnum, server_port, __LINE__);
-      sl_close_fd (FIL__, __LINE__, sock);
-      return -1;
-    }
-  
-  if ( bind(sock, (struct sockaddr *) sa, salen) < 0) 
-    {
-      if (errno != EADDRINUSE)
-	{
-	  errnum = errno;
-	  sh_xfer_printerr (_("bind"), errnum, server_port, __LINE__);
-	  sl_close_fd (FIL__, __LINE__, sock);
-	  return -1;
-	}
-      else
-	{
-	  sl_close_fd (FIL__, __LINE__, sock);
-	  return -2;
-	}
-    }
-  
-  if ( retry_fcntl( FIL__, __LINE__, sock, F_SETFL, O_NONBLOCK ) < 0 )
-    {
-      errnum = errno;
-      sh_xfer_printerr (_("fcntl"), errnum, server_port, __LINE__);
-      sl_close_fd (FIL__, __LINE__, sock);
-      return -1;
-    }
-  
-  if ( listen(sock, 64) < 0)
-    {
-      errnum = errno;
-      sh_xfer_printerr (_("listen"), errnum, server_port, __LINE__);
-      sl_close_fd (FIL__, __LINE__, sock);
-      return -1;
-    }
-
-  return sock;
-}
-
-int sh_create_tcp_socket (void)
-{
-#if defined(USE_IPVX)
-  struct addrinfo *ai;
-  struct addrinfo *p;
-  struct addrinfo hints;
-  char            port[32];
-#else
-  struct sockaddr_in addr;
-  int addrlen      = sizeof(addr);
-#endif
-
-  int sock   = -1;
-
-  SL_ENTER(_("sh_create_tcp_socket"));
-
-  sh_xfer_printerr (NULL, 0, server_port, __LINE__);
-
-#if defined(USE_IPVX)
-  if (use_server_interface == 0) /* INADDR_ANY, listen on all interfaces */
-    {
-      memset (&hints, 0, sizeof (hints));
-      hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
-      hints.ai_socktype = SOCK_STREAM;
-      hints.ai_family   = AF_UNSPEC;
-      sl_snprintf(port, sizeof(port), "%d", server_port);
-
-      if (getaddrinfo (NULL, port, &hints, &ai) != 0)
-	{
-	  int errnum = errno;
-	  sh_xfer_printerr (_("getaddrinfo"), errnum, server_port, __LINE__);
-	  sl_close_fd (FIL__, __LINE__, sock);
-	  SL_RETURN((-1), _("sl_create_tcp_socket"));
-	}
-      
-      p = ai;
-      
-      while (p != NULL && sh_tcp_sock_n < SH_SOCKMAX)
-	{
-	  sock = do_socket(p->ai_family, p->ai_socktype, p->ai_protocol,
-			   p->ai_addr, p->ai_addrlen);
-	  
-	  if (sock >= 0) {
-	    if (sh_tcp_sock_n < SH_SOCKMAX) {
-	      sh_tcp_sock[sh_tcp_sock_n] = sock;
-	      ++sh_tcp_sock_n;
-	    }
-	    else {
-	      sl_close_fd (FIL__, __LINE__, sock);
-	    }    
-	  } else if (sock == -1) {
-	    freeaddrinfo (ai);
-	    goto end;
-	  }
-	  p = p->ai_next;
-	}
-      
-      freeaddrinfo (ai);
-    }
-  else
-    {
-      sh_ipvx_set_port(&server_interface, server_port);
-
-      sock = do_socket(server_interface.ss_family, SOCK_STREAM, 0, 
-		       sh_ipvx_sockaddr_cast(&server_interface), 
-		       SH_SS_LEN(server_interface));
-      
-      if (sock >= 0) {
-	sh_tcp_sock[0] = sock;
-	sh_tcp_sock_n  = 1;
-      }
-    }	       
-#else
-  if (use_server_interface == 0)
-    addr.sin_addr.s_addr = INADDR_ANY;
-  else
-    memcpy(&addr, sh_ipvx_sockaddr_cast(&server_interface), addrlen);
-  addr.sin_family      = AF_INET;
-  addr.sin_port        = htons(server_port);
-  
-  sock = do_socket(AF_INET, SOCK_STREAM, 0, (struct sockaddr *) &addr, addrlen);
-
-  if (sock >= 0) {
-      sh_tcp_sock[0] = sock;
-      sh_tcp_sock_n  = 1;
-  }
-
-#endif
-
-#if defined(USE_IPVX)
- end:
-#endif
-  if (sh_tcp_sock_n > 1)
-    SH_MINSOCK += (sh_tcp_sock_n - 1);
-
-  SL_RETURN((sh_tcp_sock_n), _("sl_create_tcp_socket"));
-}
-
-/*****************************************
- *
- * This is the server main loop.
- *
- * The server is set up for listening, and
- * and starts a select() loop.
- *
- *****************************************/
-
-void sh_xfer_start_server()
-{
-#ifdef SH_USE_XML
-  extern int  sh_log_file    (char * message, char * inet_peer);
-#endif
-
-  /* Use volatile to circumvent a gcc4 problem on RH/CentOS 4.8 (?) */
-  volatile int       sock = -1;
-  sh_conn_t        * cx;
-  fd_set             readset;
-  fd_set             writeset;
-  struct timeval     tv;
-  int                num_sel;
-  int                errnum;
-  int                nowconn;
-  int                status;
-  int                high_fd = -1;
-  register int       i;
-  long               dummy = 0;
-  unsigned long      time_now;
-  unsigned long      time_last = 0;
-  unsigned long      time_out = time_out_val;  
-  
-  time_t told;
-  time_t tcurrent;
-
-  unsigned long tchkold;
-
-  int setsize_fd;
-
-  int sock_tcp[2];
-  int sock_unix;
-#ifdef INET_SYSLOG
-  int sock_log[2];
-#endif
-  
-  SL_ENTER(_("sh_xfer_start_server"));
-
-  if ( sh_xfer_printerr_final(0) < 0)
-    {
-      aud_exit(FIL__, __LINE__, EXIT_FAILURE);
-    }
-
-  sock = sh_tcp_sock[0];
-
-  /* ****************************************************************
-   *
-   * This is a non-forking server. We use select() on the listen()
-   * socket to watch for new connections. For new connections, accept()
-   * will return a new socket that is put in the read/write filesets.
-   * Data about active connections are kept in the 'conns' table. 
-   *
-   ******************************************************************/
-  
-  /* The table to hold info on sockets.
-   * We reserve 6 file descriptors for misc. use.
-   * The POSIX lower limit on open files seems to be eight. 
-   */
-  maxconn    = get_open_max() - 6;
-
-  /* ugly fix for FreeBSD compiler warning; casting FD_SETSIZE in the
-   * conditional expression does not suppress the warning... */
-  setsize_fd = (int)FD_SETSIZE;
-  maxconn = (setsize_fd < maxconn) ? setsize_fd : maxconn;
-
-  if (maxconn < 0 || !sl_ok_muls(maxconn, sizeof(sh_conn_t)))
-    {
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_START_SRV,
-		      0, sock);
-      aud_exit (FIL__, __LINE__, EXIT_FAILURE);
-    }
-  conns   = SH_ALLOC (sizeof(sh_conn_t) * maxconn);
-
-  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_START_SRV,
-		  (maxconn-1), sock);
-
-  /* timer
-   */
-  tcurrent                   = (unsigned long) time (NULL);
-  told                       = tcurrent;
-
-  tchkold                    = tcurrent;
-  
-  for (i = SH_MINSOCK; i < maxconn; ++i)
-    {
-      conns[i].buf         = NULL;
-      conns[i].K           = NULL;
-      conns[i].A           = NULL;
-      conns[i].M1          = NULL;
-      conns[i].FileName    = NULL;
-      conns[i].fd          = -1;
-      sh_xfer_do_free ( &conns[i]);
-    }
-  
-  /* status init
-   */
-  server_status.conn_open  = 0;
-  server_status.conn_total = 0;
-  server_status.conn_max   = maxconn-1;
-  server_status.start      = time (NULL);
-  server_status.last       = (time_t) 0;
-
-  nowconn    = 1;
-  tv.tv_sec  = 5;
-  tv.tv_usec = 0;
-  
-  /* conns[0] is the listen() socket. Always in read mode.
-   */
-  sock = 0;
-
-  sock_tcp[0] = 0;
-  while (sock < sh_tcp_sock_n)
-    {
-      conns[sock].fd    = sh_tcp_sock[sock];
-      conns[sock].state = CONN_READING;
-      /* high_fd = (sh_tcp_sock[sock] > high_fd) ? sh_tcp_sock[sock] : high_fd; */
-      ++sock;
-    }
-  sock_tcp[1] = sock;
-  
-  conns[sock].fd    = pf_unix_fd;
-  conns[sock].state = CONN_READING;
-  /* high_fd = (pf_unix_fd > high_fd) ? pf_unix_fd : high_fd; */
-
-  sock_unix = sock;
-
-  ++sock;
-
-#ifdef INET_SYSLOG
-  conns[sock].fd = -1;
-
-  if ( sh_xfer_printerr_final(1) < 0)
-    {
-      SH_FREE(conns);
-      conns = NULL;
-      aud_exit(FIL__, __LINE__, EXIT_FAILURE);
-    }
-
-  sock_log[0] = sock;
-  sock_log[1] = sock;
-
-  if (sh_xfer_syslog_sock_n > 0)
-    {
-      int s2;
-      for (s2 = 0; s2 < sh_xfer_syslog_sock_n; ++s2)
-	{
-	  conns[sock].fd    = sh_xfer_syslog_sock[s2];
-	  conns[sock].state = CONN_READING;
-	  /* high_fd = (high_fd > conns[sock].fd) ? high_fd : conns[sock].fd; */
-	  ++sock;
-	}
-      sock_log[1] = sock;
-
-    }
-#endif
-  
-  sh_html_write(all_clients);
-  
-  /* This is the select() loop.
-   */
-  while (1 == 1)
-    {
-
-    if (sig_raised > 0)
-      {
-	TPT((0, FIL__, __LINE__, _("msg=<Process a signal.>\n")))
-
-	if (sig_termfast == 1)  /* SIGTERM */
-	  {
-	    TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
-	    strncpy (sh_sig_msg, _("SIGTERM"), 20);
-	    --sig_raised; --sig_urgent;
-	    aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
-	  }
-	  
-	if (sig_config_read_again == 1)
-	  {
-	    TPT((0, FIL__, __LINE__, _("msg=<Re-read configuration.>\n")));
-	    sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_RECONF);
-
-
-	    /* -- Delete the name server cache. --
-	     */
-
-	    delete_cache();
-#if defined(WITH_EXTERNAL)
-	    /* -- Delete list of external tasks. --
-	     */
-	    (void) sh_ext_cleanup();
-#endif
-#if defined(SH_WITH_MAIL)
-	      sh_nmail_free();
-#endif
-	    /* - mark all clients dead
-	     * - read configuration file
-	     * - remove clients still dead
-	     */
-	    sh_xfer_mark_dead ();
-
-	    /* free the aliases list */
-	    zAVLFreeTree (all_aliases, free_alias);
-	    all_aliases = NULL;
-
-	    reset_count_dev_console();
-	    reset_count_dev_time();
-	    sl_trust_purge_user();
-
-	    (void) sh_readconf_read ();
-
-	    for (i = SH_MINSOCK; i < maxconn; ++i)
-	      if (conns[i].state != CONN_FREE   && 
-		  conns[i].client_entry != NULL &&
-		  conns[i].client_entry->dead_flag == 1)
-		sh_xfer_do_free ( &conns[i]);
-	    sh_xfer_clean_tree ();
-
-	    sig_config_read_again = 0;
-	    --sig_raised;
-	  }
-
-	if (sig_fresh_trail == 1) /* SIGIOT */
-	  {
-	    /* Logfile access 
-	     */
-#ifdef SH_USE_XML
-	    sh_log_file (NULL, NULL);
-#endif
-	    TPT((0, FIL__, __LINE__, _("msg=<Logfile stop/restart.>\n")));
-	    sh_error_only_stderr (S_TRUE);
-	    sh_unix_rm_lock_file(sh.srvlog.name);
-	    retry_msleep(3, 0);
-	    sh.flag.log_start = S_TRUE;
-	    sh_error_only_stderr (S_FALSE);
-	    sig_fresh_trail       = 0;
-	    --sig_raised;
-	  }
-	
-	  
-	if (sig_terminate == 1 && nowconn < 2)  /* SIGQUIT */
-	  {
-	    TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
-	    strncpy (sh_sig_msg, _("SIGQUIT"), 20);
-	    --sig_raised; --sig_urgent;
-	    aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
-	  }
-	
-	  
-	if (sig_debug_switch == 1)  /* SIGUSR1 */
-	  {
-	    TPT((0, FIL__, __LINE__, _("msg=<Debug switch.>\n")));
-	    sh_error_dbg_switch();
-	    sig_debug_switch = 0;
-	    --sig_raised;
-	  }
-	
-	if (sig_suspend_switch > 0)  /* SIGUSR2 */
-	  {
-	    TPT((0, FIL__, __LINE__, _("msg=<Suspend switch.>\n")));
-	    if (sh_global_suspend_flag == 1) {
-	      sh_global_suspend_flag = 0;
-	    } else {
-	      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_SUSPEND, 
-			      sh.prg_name);
-	      sh_global_suspend_flag = 1;
-	    }
-	    --sig_suspend_switch;
-	    --sig_raised; --sig_urgent;
-	  }
-
-	sig_raised = (sig_raised < 0) ? 0 : sig_raised;
-	sig_urgent = (sig_urgent < 0) ? 0 : sig_urgent;
-	TPT((0, FIL__, __LINE__, _("msg=<End signal processing.>\n")));
-      }
-      
-      if (sh_global_suspend_flag == 1)
-	{
-	  (void) retry_msleep (1, 0);
-	  continue;
-	}
-
-      /* Recompute the descriptor set. select() modifies it,
-       * thus we update it using the info from the connection table.
-       * Also recompute the number of open connections.
-       */
-      FD_ZERO( &readset );
-      FD_ZERO( &writeset );
-      high_fd = conns[0].fd;
-
-      for (sock = sock_tcp[0]; sock < sock_tcp[1]; ++sock)
-	{
-	  FD_SET(conns[sock].fd, &readset );
-	  high_fd   = (high_fd > conns[sock].fd) ? high_fd : conns[sock].fd;
-	}
-
-      if (conns[sock_unix].fd > -1)
-	{
-	  FD_SET(conns[sock_unix].fd, &readset );
-	  high_fd   = (high_fd > conns[sock_unix].fd) ? high_fd : conns[sock_unix].fd;
-	}
-
-#ifdef INET_SYSLOG
-      for (sock = sock_log[0]; sock < sock_log[1]; ++sock)
-	{
-	  if (conns[sock].fd > -1)
-	    {
-	      FD_SET(conns[sock].fd, &readset );
-	      high_fd   = (high_fd > conns[sock].fd) ? high_fd : conns[sock].fd;
-	    }
-	}
-#endif
-
-      time_now  = (unsigned long) time (NULL);
-      nowconn   = 1;
-      
-      for (i = SH_MINSOCK; i < maxconn; ++i)
-	{
-	  /* eliminate timed out connections
-	   */
-	  if (conns[i].state != CONN_FREE) 
-	    {
-	      if (time_now-conns[i].timer > time_out)
-		{
-		  sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_TIMOUT,
-				  conns[i].peer);
-		  sh_xfer_do_free ( &conns[i]);
-		}
-	      else
-		++nowconn;
-	    }
-	  
-	  
-	  if       (conns[i].state   == CONN_READING)
-	    { 
-	      FD_SET(conns[i].fd, &readset);
-	      high_fd = (high_fd < conns[i].fd ? conns[i].fd : high_fd);
-	    }
-	  else if  (conns[i].state   == CONN_SENDING)
-	    {
-	      FD_SET(conns[i].fd, &writeset);
-	      high_fd = (high_fd < conns[i].fd ? conns[i].fd : high_fd);
-	    }
-	}
-
-      /* -- Exponentially reduce timeout limit if more than 1/2 full. --
-       */
-      /* Eliminate this, will cause problems when too much clients are
-       * starting up. */
-#if 0
-      if (nowconn > (maxconn/2))
-	time_out = ( (time_out/2) > 1) ? (time_out/2) : 1;
-      else
-	time_out = time_out_val;
-#endif
-      
-      
-      /* -- Do the select(). --
-       */
-      num_sel = select(high_fd+1, &readset, &writeset, NULL, &tv);
-      errnum  = errno;
-      
-      /* reset timeout - modified by select() on some systems
-       */
-      tv.tv_sec  = 5;
-      tv.tv_usec = 0;
-      
-
-      if ( (time_now - time_last) > 2L)
-	{
-	  time_last = time_now;
-	  if (sh_html_write(all_clients) < 0)
-	    sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_HTML);
-	}
-      
-      
-      /* Error handling.
-       */
-      if ( num_sel < 0 )        /* some error             */
-	{
-	  char errbuf[SH_ERRBUF_SIZE];
-
-	  if (sig_raised == 1)
-	    {
-	      sig_raised = 2;
-	      continue;
-	    }
-
-	  if ( errnum == EINTR)
-	    continue;	  /* try again              */
-
-	  if ( errnum == EBADF)
-	    {
-	      /* seek and destroy the bad fd
-	       */
-	      for (i = SH_MINSOCK; i < high_fd; ++i)
-		{
-		  if ((conns[i].state == CONN_READING) ||
-		      (conns[i].state == CONN_SENDING))
-		    {
-		      if (-1 == retry_fcntl(FIL__, __LINE__, 
-					    conns[i].fd, F_GETFL, dummy))
-			sh_xfer_do_free ( &conns[i]);
-		    }
-		}
-	      continue;
-	    }
-
-	  sh_error_handle((-1), FIL__, __LINE__, errnum, MSG_EXIT_ABORTS,
-			  sh_error_message(errnum, errbuf, sizeof(errbuf)), 
-			  sh.prg_name,
-			  _("select"));
-	  aud_exit(FIL__, __LINE__,  EXIT_FAILURE );
-	}
-      
-
-      /* log the timestamp
-       */
-      if ((tcurrent - told) > sh.looptime )
-	{
-	  told = tcurrent;
-#ifdef MEM_DEBUG
-	  sh_mem_check();
-	  sh_unix_count_mlock();
-#else
-	  sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_STAMP);
-#endif
-	}
-
-#if defined(SH_WITH_MAIL)
-      /* 
-       * flush the mail queue
-       */
-      if (tcurrent - sh.mailTime.alarm_last > sh.mailTime.alarm_interval) 
-	{
-	  TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
-	  (void) sh_nmail_flush ();
-	  sh.mailTime.alarm_last = tcurrent;
-	}
-#endif
-#ifdef MEM_DEBUG
-      sh_mem_dump();
-#endif
-
-      tcurrent = (unsigned long) time (NULL);
-
-      /* check for time limit exceeded
-       */
-      if ((tcurrent - tchkold) > (unsigned int) 3 )
-	{
-	  tchkold = tcurrent;
-	  client_time_check(/* all_clients */);
-	  /* reset cache */
-	  sh_userid_destroy();
-	}
-      
-      /* seed / re-seed the PRNG if required
-       */
-      (void) taus_seed();
-
-      /* select() timeout handling.
-       */
-      if ( num_sel == 0 )       /* timeout - no connection */ 
-	{
-	  if (sh_html_write(all_clients) < 0)
-	    sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_HTML);
-	  continue;
-	}
-
-      /* New connection.
-       */
-      for (sock = sock_tcp[0]; sock < sock_tcp[1]; ++sock)
-	{
-	  if ( FD_ISSET(conns[sock].fd , &readset )) /* a new connection   */
-	    {
-	      --num_sel;
-	      status = 0;
-	      if (nowconn < maxconn && sig_terminate == 0 && sig_termfast == 0)
-		{
-		  /* Find a free slot to accept the connection
-		   */
-		  i = SH_MINSOCK;
-		  while (i < maxconn)
-		    {
-		      if (conns[i].state == CONN_FREE)
-			{
-			  /* Here we run the accept() and copy the peer to
-			   * the free slot. 
-			   */
-			  status = sh_xfer_accept(conns[sock].fd, &conns[i]);
-			  
-			  if (status == 0)
-			    {
-			      high_fd = 
-				(high_fd > conns[i].fd ? high_fd : conns[i].fd);
-			      ++server_status.conn_open;
-			      ++server_status.conn_total;
-			      server_status.last = time (NULL);
-			    }
-			  break;
-			}
-		      ++i;
-		    }
-		}
-	      /* This re-runs select to accept data on the new
-	       * connection, rather than first dealing with old
-	       * connections.
-	       */
-	      if (status == 0) 
-		continue;
-	    }
-	}
-      
-      /* check for commands on the socket
-       */
-      if (conns[sock_unix].fd > (-1) && FD_ISSET(conns[sock_unix].fd , &readset ))
-	{
-	  sh_socket_poll();
-	}
-
-#ifdef INET_SYSLOG
-      for (sock = sock_log[0]; sock < sock_log[1]; ++sock)
-	{
-	  if (conns[sock].fd > (-1) && FD_ISSET(conns[sock].fd , &readset ))
-	    {
-	      sh_xfer_recv_syslog_socket (conns[sock].fd);
-	    }
-	}
-#endif
-
-      /* Check for pending read/write on the rest of the sockets.
-       */
-      for ( i = SH_MINSOCK; num_sel > 0 && i < maxconn; ++i )
-	{
-	  if (sig_termfast == 1)
-	    break;
-
-	  cx = &conns[i];
-	  if ( cx->state == CONN_READING &&
-	       FD_ISSET( cx->fd, &readset ) )
-	    {
-	      --num_sel;
-	      sh_xfer_do_read ( cx );
-	    }
-	  else if ( cx->state == CONN_SENDING &&
-		    FD_ISSET( cx->fd, &writeset ) )
-	    {
-	      --num_sel;
-	      sh_xfer_do_write ( cx );
-	    }
-	}
-      /* continue */
-    }
-  /* notreached */
-}
-
-void  free_client_tree (void)
-{
-  SL_ENTER(_("free_client_tree"));
-  zAVLFreeTree (all_clients, free_client);
-  SL_RET0(_("free_client_tree"));
-}
-
-void sh_xfer_free_all ()
-{
-  register int i;
-  
-  SL_ENTER(_("sh_xfer_free_all"));
-
-  if (conns != NULL)
-    for (i = SH_MINSOCK; i < maxconn; ++i)
-      {
-	sh_xfer_do_free ( &conns[i]);
-      }
-
-
-  free_client_tree ();
-
-  if (conns != NULL)
-    SH_FREE (conns);
-
-  SL_RET0(_("sh_xfer_free_all"));
-}
-
-
-
-/* #ifdef SH_WITH_SERVER */
-#endif
-
-
-  
-
-
-
Index: trunk/src/sh_xfer_syslog.c
===================================================================
--- trunk/src/sh_xfer_syslog.c	(revision 591)
+++ 	(revision )
@@ -1,475 +1,0 @@
-/* SAMHAIN file system integrity testing                                   */
-/* Copyright (C) 1999, 2000 Rainer Wichmann                                */
-/*                                                                         */
-/*  This program is free software; you can redistribute it                 */
-/*  and/or modify                                                          */
-/*  it under the terms of the GNU General Public License as                */
-/*  published by                                                           */
-/*  the Free Software Foundation; either version 2 of the License, or      */
-/*  (at your option) any later version.                                    */
-/*                                                                         */
-/*  This program is distributed in the hope that it will be useful,        */
-/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
-/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
-/*  GNU General Public License for more details.                           */
-/*                                                                         */
-/*  You should have received a copy of the GNU General Public License      */
-/*  along with this program; if not, write to the Free Software            */
-/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
-
-#include "config_xor.h"
-
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <syslog.h>
-
-/* Must be early on FreeBSD
- */
-#include <sys/types.h>
-#include <sys/socket.h> 
-#include <netdb.h>
-#include <netinet/in.h>
-
-#if HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
-#include <time.h>
-
-#include <unistd.h>
-#include <fcntl.h>
-
-#ifdef SH_WITH_SERVER
-
-#include "samhain.h"
-#include "sh_tools.h"
-#include "sh_utils.h"
-#include "sh_ipvx.h"
-
-#undef  FIL__
-#define FIL__  _("sh_xfer_syslog.c")
-
-#ifdef INET_SYSLOG
-
-extern void sh_xfer_printerr(char * str, int errnum, unsigned int port, int line);
-extern int sh_xfer_syslog_sock[SH_SOCKMAX];
-extern int sh_xfer_syslog_sock_n;
-extern int SH_MINSOCK;
-
-/* Unlike Linux / FreeBSD, most systems don't define the stuff below
- * in syslog.h
- */
-
-#ifndef LOG_FAC
-#define LOG_FAC(p)      (((p) & LOG_FACMASK) >> 3)
-#endif
-
-#ifndef LOG_PRI
-#define LOG_PRI(p)      ((p) & LOG_PRIMASK)
-#endif
-
-typedef struct sh_code {
-        char    *c_name;
-        int     c_val;
-} SH_CODE;
-
-SH_CODE sh_facilitynames[] =
-{
-#ifdef LOG_AUTH
-  { N_("auth"), LOG_AUTH },
-#endif
-#ifdef LOG_AUTHPRIV 
-  { N_("authpriv"), LOG_AUTHPRIV },
-#endif
-#ifdef LOG_CRON
-  { N_("cron"), LOG_CRON },
-#endif
-#ifdef LOG_DAEMON
-  { N_("daemon"), LOG_DAEMON },
-#endif
-#ifdef LOG_FTP
-  { N_("ftp"), LOG_FTP },
-#endif
-#ifdef LOG_KERN
-  { N_("kern"), LOG_KERN },
-#endif
-#ifdef LOG_LPR
-  { N_("lpr"), LOG_LPR },
-#endif
-#ifdef LOG_MAIL
-  { N_("mail"), LOG_MAIL },
-#endif
-#ifdef INTERNAL_MARK
-  { N_("mark"), INTERNAL_MARK },          /* INTERNAL */
-#endif
-#ifdef LOG_NEWS
-  { N_("news"), LOG_NEWS },
-#endif
-#ifdef LOG_AUTH
-  { N_("security"), LOG_AUTH },           /* DEPRECATED */
-#endif
-#ifdef LOG_SYSLOG
-  { N_("syslog"), LOG_SYSLOG },
-#endif
-#ifdef LOG_USER
-  { N_("user"), LOG_USER },
-#endif
-#ifdef LOG_UUCP
-  { N_("uucp"), LOG_UUCP },
-#endif
-#ifdef LOG_LOCAL0
-  { N_("local0"), LOG_LOCAL0 },
-#endif
-#ifdef LOG_LOCAL1
-  { N_("local1"), LOG_LOCAL1 },
-#endif
-#ifdef LOG_LOCAL2 
-  { N_("local2"), LOG_LOCAL2 },
-#endif
-#ifdef LOG_LOCAL3
-  { N_("local3"), LOG_LOCAL3 },
-#endif
-#ifdef LOG_LOCAL4
-  { N_("local4"), LOG_LOCAL4 },
-#endif
-#ifdef LOG_LOCAL5
-  { N_("local5"), LOG_LOCAL5 },
-#endif
-#ifdef LOG_LOCAL6
-  { N_("local6"), LOG_LOCAL6 },
-#endif
-#ifdef LOG_LOCAL7
-  { N_("local7"), LOG_LOCAL7 },
-#endif
-  { NULL, -1 }
-};
- 
-
-SH_CODE sh_prioritynames[] =
-{  
-#ifdef LOG_ALERT
-  { N_("alert"), LOG_ALERT },
-#endif
-#ifdef LOG_CRIT
-  { N_("crit"), LOG_CRIT },
-#endif
-#ifdef LOG_DEBUG
-  { N_("debug"), LOG_DEBUG },
-#endif
-#ifdef LOG_EMERG
-  { N_("emerg"), LOG_EMERG },
-#endif
-#ifdef LOG_ERR
-  { N_("err"), LOG_ERR },
-#endif
-#ifdef LOG_ERR
-  { N_("error"), LOG_ERR },               /* DEPRECATED */
-#endif
-#ifdef LOG_INFO
-  { N_("info"), LOG_INFO },
-#endif
-#ifdef INTERNAL_NOPRI
-  { N_("none"), INTERNAL_NOPRI },         /* INTERNAL */
-#endif
-#ifdef LOG_NOTICE
-  { N_("notice"), LOG_NOTICE },
-#endif
-#ifdef LOG_EMERG
-  { N_("panic"), LOG_EMERG },             /* DEPRECATED */
-#endif
-#ifdef LOG_WARNING
-  { N_("warn"), LOG_WARNING },            /* DEPRECATED */
-#endif
-#ifdef LOG_WARNING
-  { N_("warning"), LOG_WARNING },
-#endif
-  { NULL, -1 }
-};
-
-static int enable_syslog_socket = S_FALSE;
-
-int sh_xfer_recv_syslog_socket (int fd)
-{
-  static time_t      return_next = 0;
-  int                priority = 0;
-  int                fac, pri;
-  int                i;
-  char             * cfac = NULL;
-  char             * cpri = NULL;
-  int                res;
-  char             * tmp;
-  char             * bptr;
-  char             * ptr = NULL;
-  char               buf[1048];
-  struct sockaddr_in from;
-  char errbuf[SH_ERRBUF_SIZE];
-
-  struct sh_sockaddr ss;
-  struct sockaddr * sa = (struct sockaddr *) &from;
-  char   namebuf[SH_BUFSIZE];
-
-  /* The 6th argument in recvfrom is *socklen_t in Linux and *BSD, 
-   * but *int everywhere else. Because socklen_t is unsigned int, there
-   * should be no problem as long as  sizeof(struct sockaddr_in) < INT_MAX ...
-   */
-  unsigned int fromlen = sizeof(from);
-
-  if (enable_syslog_socket == S_FALSE)
-    return 0;
-
-  SL_ENTER(_("sh_xfer_recv_syslog_socket"));
-
-  if (return_next > 0)
-    {
-      if ( (time(NULL) - return_next) < 2)
-	SL_RETURN( 0, _("sh_xfer_recv_syslog_socket"));
-      else
-	return_next = 0;
-    }
-
-  res = recvfrom(fd,  buf,  1047, 0, (struct sockaddr *) &from, &fromlen);
-
-  sh_ipvx_save(&ss, sa->sa_family, (struct sockaddr *) &from);
-  sh_ipvx_ntoa(namebuf, sizeof(namebuf), &ss);
-
-  if (res > 0)
-    {
-      res = (res < 1047) ? res : 1047; 
-      buf[res] = '\0';
-      if (res > 1 && buf[res-1] == '\n')
-	buf[res-1] = '\0';
-
-      /* here we expect an xml formatted message, thus we don't
-	 escape xml special chars (flag == 0) */
-      /* commented out to not escape twice    */
-      /* bptr = sh_tools_safe_name(buf, 0);   */
-      bptr = buf;
-
-      if (!bptr || !(*bptr))
-	{
-	  res = errno;
-	  TPT(( 0, FIL__, __LINE__, _("msg=<UDP error: %d>\n"), res));
-	  sh_error_handle((-1), FIL__, __LINE__, res, MSG_ERR_SYSLOG,
-			  sh_error_message(res, errbuf, sizeof(errbuf)), 
-			  namebuf);
-	  SL_RETURN( (-1), _("sh_xfer_recv_syslog_socket"));
-	}      
-
-      TPT(( 0, FIL__, __LINE__, _("msg=<UDP message from %s>\n"), namebuf ));
-
-      ptr = bptr;
-      i = 0;
-      if (*ptr == '<') 
-	{
-	  ++ptr; ++i;
-	  while (i < res &&
-		 (unsigned char) *ptr > 47 && (unsigned char) *ptr < 58)
-	    {
-	      priority = 10 * priority + (*ptr - '0');
-	      ++ptr;
-	      ++i;
-	    }
-	  if (*ptr == '>')
-	    ++ptr;
-	}
-      fac = LOG_FAC(priority);
-      i = 0; 
-      while (sh_facilitynames[i].c_name != NULL)
-	{
-	  if (sh_facilitynames[i].c_val == (fac<<3))
-	    { cfac = sh_util_strdup(_(sh_facilitynames[i].c_name)); break; }
-	  ++i;
-	}
-      pri = LOG_PRI(priority);
-      i = 0; 
-      while (sh_prioritynames[i].c_name != NULL)
-	{
-	  if (sh_prioritynames[i].c_val == pri)
-	    { cpri = sh_util_strdup(_(sh_prioritynames[i].c_name)); break; }
-	  ++i;
-	}
-
-      /* here we do not expect an xml formatted message, thus we escape
-	 xml special chars (flag == 1) */
-      tmp = sh_tools_safe_name (ptr, 1);
-      sh_error_handle((-1), FIL__, __LINE__, 0, MSG_INET_SYSLOG,
-		      namebuf, 
-		      (cfac == NULL) ? _("none") : cfac, 
-		      (cpri == NULL) ? _("none") : cpri, 
-		      (tmp  == NULL) ? _("none") : tmp);
-      if (cfac != NULL)
-	SH_FREE(cfac);
-      if (cpri != NULL)
-	SH_FREE(cpri);
-      SH_FREE(tmp);
-      /* SH_FREE(bptr); */
-    }
-
-  else if (res < 0 && errno != EINTR)
-    {
-      res = errno;
-      TPT(( 0, FIL__, __LINE__, _("msg=<UDP error: %d>\n"), res));
-      sh_error_handle((-1), FIL__, __LINE__, res, MSG_ERR_SYSLOG,
-		      sh_error_message(res, errbuf, sizeof(errbuf)), 
-		      namebuf);
-
-      /* don't accept anything the next 2 seconds
-       */
-      return_next = time(NULL);
-      SL_RETURN( (-1), _("sh_xfer_recv_syslog_socket"));
-    }      
-  SL_RETURN( (0), _("sh_xfer_recv_syslog_socket"));
-}
-
-int set_syslog_active(const char * c)
-{
-  return sh_util_flagval(c, &enable_syslog_socket);
-}
-
-static int do_syslog_socket(int domain, int type, int protocol,
-			    struct sockaddr * sa, int salen)
-{
-  int                flag = 1;  /* non-zero to enable an option */
-  int sock;
-  int errnum;
-  int res;
-
-  /* create the socket, bind() it and listen()
-   */
-  sock = socket(domain, type, protocol);
-
-  if (sock < 0)
-    {
-      errnum = errno; 
-      sh_xfer_printerr (_("syslog socket"), errnum, 514, __LINE__);
-      return -1;
-    }
-  (void) retry_fcntl( FIL__, __LINE__, sock, F_SETFD, 1 );
-  
-  if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
-		  (void *) &flag, sizeof(flag)) < 0 )
-    {
-      errnum = errno;
-      sh_xfer_printerr (_("syslog setsockopt SO_REUSEADDR"), 
-			   errnum, 514, __LINE__);
-      return -1;
-    }
-
-#if defined(SO_BSDCOMPAT)
-  if ( setsockopt(sock, SOL_SOCKET, SO_BSDCOMPAT,
-		  (void *) &flag, sizeof(flag)) < 0 )
-    {
-      errnum = errno;
-      sh_xfer_printerr (_("syslog setsockopt SO_BSDCOMPAT"), 
-			   errnum, 514, __LINE__);
-      return -1;
-    }
-#endif
-  
-  res = bind(sock, sa, salen);
-
-  if ( res < 0) 
-    {
-      errnum = errno;
-      sh_xfer_printerr (_("syslog bind"), errnum, 514, __LINE__);
-      sl_close_fd(FIL__, __LINE__, sock);
-      return -1;
-    }
-  return sock;
-}
-
-/* callerFlag == S_TRUE means override the enable_syslog_socket flag
- */
-int sh_xfer_create_syslog_socket (int callerFlag)
-{
-  int sock;
-
-#if defined(USE_IPVX)
-  struct addrinfo *ai;
-  struct addrinfo *p;
-  struct addrinfo hints;
-#else
-  struct sockaddr_in addr;
-  int addrlen      = sizeof(addr);
-#endif
-
-  SL_ENTER(_("sh_xfer_create_syslog_socket"));
-
-  if (callerFlag == S_FALSE)
-    {
-      if (enable_syslog_socket == S_FALSE && sh_xfer_syslog_sock_n > 0)
-	{
-	  /* user does not wish to use this facility
-	   */
-	  TPT(( 0, FIL__, __LINE__, _("msg=<close syslog socket>\n")));
-	  for (sock = 0; sock < sh_xfer_syslog_sock_n; ++sock)
-	    {
-	      sl_close_fd(FIL__, __LINE__, sh_xfer_syslog_sock[sock]);
-	      sh_xfer_syslog_sock[0] = -1;
-	    }
-	}
-      SL_RETURN((-1), _("sh_xfer_create_syslog_socket"));
-    }
-
-  sh_xfer_printerr (NULL, 0, 514, __LINE__);
-
-#if !defined(USE_IPVX)
-
-  memset(&addr, 0, sizeof(addr));
-  addr.sin_family      = AF_INET;
-  addr.sin_port        = htons(514);
-  
-  sock = do_syslog_socket(AF_INET, SOCK_DGRAM, 0, 
-			  (struct sockaddr *) &addr, addrlen);
-
-  if (sock >= 0) {
-    sh_xfer_syslog_sock[0] = sock;
-    sh_xfer_syslog_sock_n  = 1;
-  }
-
-#else
-  memset (&hints, 0, sizeof (hints));
-  hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
-  hints.ai_socktype = SOCK_DGRAM;
-  if (getaddrinfo (NULL, "syslog", &hints, &ai) != 0)
-    {
-      int errnum = errno;
-      sh_xfer_printerr (_("getaddrinfo"), errnum, 514, __LINE__);
-      SL_RETURN((-1), _("sh_xfer_create_syslog_socket"));
-    }
-  
-  p = ai;
-
-  while (p != NULL && sh_xfer_syslog_sock_n < SH_SOCKMAX)
-    {
-      sock = do_syslog_socket(p->ai_family, p->ai_socktype, p->ai_protocol,
-			      p->ai_addr, p->ai_addrlen);
-      
-      if (sock >= 0) {
-	if (sh_xfer_syslog_sock_n < SH_SOCKMAX) {
-	  sh_xfer_syslog_sock[sh_xfer_syslog_sock_n] = sock;
-	  ++sh_xfer_syslog_sock_n;
-	}
-	else {
-	  sl_close_fd (FIL__, __LINE__, sock);
-	}    
-      } else if (sock == -1) {
-	freeaddrinfo (ai);
-	goto end;
-      }
-      p = p->ai_next;
-    }
-  freeaddrinfo (ai);
-
- end:
-#endif
-  if (sh_xfer_syslog_sock_n > 1)
-    SH_MINSOCK += (sh_xfer_syslog_sock_n - 1);
-
-  SL_RETURN((sh_xfer_syslog_sock_n), _("sh_xfer_create_syslog_socket"));
-}
-/* #ifdef INET_SYSLOG */
-#endif
-
-/* #ifdef SH_WITH_SERVER */
-#endif
Index: trunk/src/slib.c
===================================================================
--- trunk/src/slib.c	(revision 591)
+++ trunk/src/slib.c	(revision 1)
@@ -1,17 +1,4 @@
 #include "config_xor.h"
 
-#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_MINCORE)
-
-#if defined(__sun) || defined(__sun__) || defined(sun)
-#define _XOPEN_SOURCE 500
-#else
-#define _XOPEN_SOURCE 600
-#endif
-
-#if defined(__GNUC__)
-#define _DEFAULT_SOURCE
-#endif
-#define _BSD_SOURCE
-#endif
 
 #include <stdio.h>
@@ -19,24 +6,22 @@
 #include <stdarg.h>
 #include <string.h>
-#include <limits.h>
-#ifdef HAVE_STDINT_H
-/* for SIZE_MAX */
-#include <stdint.h>
-#endif
-
+
+
+#include <unistd.h>
+#include <sys/stat.h>
 #include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
 #include <fcntl.h>
 #include <signal.h>
 
-#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_MINCORE)
-#include <sys/mman.h>
-#endif
-
+#if TIME_WITH_SYS_TIME
+#include <sys/time.h>
+#include <time.h>
+#else
 #if HAVE_SYS_TIME_H
 #include <sys/time.h>
-#endif
+#else
 #include <time.h>
+#endif
+#endif
 
 #ifdef HAVE_MEMORY_H
@@ -60,12 +45,8 @@
 #endif
 
-#define SH_REAL_SET
-
 #include "slib.h"
 #include "sh_calls.h"
 #define SH_NEED_PWD_GRP 1
 #include "sh_static.h"
-#include "sh_pthread.h"
-#include "sh_string.h"
 
 #undef  FIL__
@@ -117,12 +98,14 @@
 static FILE * trace_fp     = NULL;
 
-int  sl_trace_use (const char * dummy)
-{
-  (void) dummy;
-  slib_do_trace = 1;
+int  sl_trace_use (char * dummy)
+{
+  if (dummy)
+    slib_do_trace = 1;
+  else
+    slib_do_trace = 1;
   return 0;
 }
 
-int  sl_trace_file (const char * str)
+int  sl_trace_file (char * str)
 {
   if (!str)
@@ -134,5 +117,5 @@
 }
 
-FILE * sl_tracefile_open(const char * file, const char * mode)
+FILE * sl_tracefile_open(char * file, char * mode)
 {
   FILE * xp = NULL;
@@ -143,5 +126,5 @@
 }
 
-void sl_trace_in(const char * str, const char * file, int line)
+void sl_trace_in(char * str, char * file, int line)
 {
   int    i;
@@ -165,5 +148,4 @@
 	  fprintf(trace_fp, "[%2d] %s \t - File %c%s%c at line %d\n", 
 		 trace_level, str, 0x22, file, 0x22, line);
-	  fflush(trace_fp);
 	}
       else
@@ -176,5 +158,5 @@
 }
 
-void sl_trace_out(const char * str, const char * file, int line)
+void sl_trace_out(char * str, char * file, int line)
 {
   int    i;
@@ -201,5 +183,4 @@
 	  fprintf(trace_fp, _("[%2d] %s \t - File %c%s%c at line %d\n"), 
 		 trace_level, str, 0x22, file, 0x22, line);
-	  fflush(trace_fp);
 	}
       else
@@ -211,5 +192,5 @@
 }
 
-extern int sh_log_console (const char * msg);
+extern int sh_log_console (char * msg);
 
 static int dlogActive = 0;
@@ -226,5 +207,5 @@
  *      = 3 backtrace
  */
-int dlog (int flag, const char * file, int line,  const char *fmt, ...)
+int dlog (int flag, char * file, int line,  const char *fmt, ...)
 {
   va_list     ap;
@@ -250,8 +231,8 @@
   if (flag == 1)
     {
-      sl_snprintf    (val, 81, _("\n---------  %10s "), file);
-      sl_strlcpy     (msg,    val,   sizeof(msg));
-      sl_snprintf    (val, 81, _(" --- %6d ---------\n"), line);
-      sl_strlcat     (msg,     val,   sizeof(msg));
+      sprintf        (val, _("\n---------  %10s "), file);
+      sl_strlcpy     (msg,    val,   80);
+      sprintf        (val, _(" --- %6d ---------\n"), line);
+      sl_strlcat     (msg,     val,   80);
       sh_log_console (msg);
     }
@@ -262,5 +243,5 @@
   else
     sl_strlcpy(tmp, fmt, 256);
-  retval = strlen(tmp);
+  retval = sl_strlen(tmp);
   if (retval > 0 && tmp[retval-1] == '\n')
     tmp[retval-1] = '\0';
@@ -277,5 +258,5 @@
       sprintf      (val, _("[%2d] "), trace_level);
       sl_strlcat   (msg,     val,   256);
-      sl_vsnprintf (&msg[strlen(msg)], 255, tmp, ap);
+      sl_vsnprintf (&msg[sl_strlen(msg)], 255, tmp, ap);
       sl_snprintf  (tmp, 255, _(" \t - File %c%s%c at line %d"), 
 		    0x22, file, 0x22, line);
@@ -581,38 +562,25 @@
   
 /*
- * Have memset in a different translation unit (i.e. this) to prevent 
- * it to get optimized away ...not safe with link-time optimisation...
- */
-void * sl_memset(void *s, int c, size_t n)
-{
-  /* See:
-   * https://www.usenix.org/sites/default/files/conference/protected-files/usenixsecurity17_slides_zhaomo_yang.pdf
-   */
-#if defined(HAVE_EXPLICIT_MEMSET)
-  return explicit_memset(s, c, n);
-#elif defined(HAVE_EXPLICIT_BZERO)
-  if (c == 0) {
-    explicit_bzero(s, n);
+ * A memset that does not get optimized away
+ */
+void *sl_memset(void *s, int c, size_t n)
+{
+  size_t i;
+  volatile char *p = s;
+
+  if (s == NULL || n <= 0)
     return s;
-  } else {
-    return memset(s, c, n);
-  }
-#elif defined(__GNUC__)
-  memset(s, c, n);
-  __asm__  __volatile__ ("" ::"r"(s): "memory"); /* compiler barrier */
+  
+  for (i = 0; i < n; ++i)
+    p[i] = (char) c;
   return s;
-#else
-  if (c == 0) {
-    size_t i;
-    volatile unsigned char * t_s = (volatile unsigned char *)s;
-    for (i=0; i<n; ++i)
-      t_s[i] = 0;
-    return s;
-  } else {
-    return memset(s, c, n);
-  }
-#endif  
-}
-
+}
+
+#if !defined(HOST_IS_I86SOLARIS)
+#if !defined (_GNU_SOURCE)
+extern int vsnprintf ( char *str, size_t n,
+		       const char *format, va_list ap );
+#endif
+#endif
 
 #if !defined (VA_COPY)
@@ -810,13 +778,12 @@
 
 #if defined(HAVE_VSNPRINTF) && !defined(HAVE_BROKEN_VSNPRINTF)
-  len = vsnprintf (str, n, format, vl);                /* flawfinder: ignore */
+  len = vsnprintf (str, n, format, vl);
   str[n-1] = '\0';
 #else
-  VA_COPY (vl2, vl);                     /* save the argument list           */
+  VA_COPY (vl2, vl);                   /* save the argument list           */
   total = sl_printf_count (format, vl);
-  len = (int) total;
+  len   = (int) total;
   if (total < n) 
     {
-      /* flawfinder: ignore */
       vsprintf (str, format, vl2);       /* program has checked that it fits */
       str[n-1] = '\0';
@@ -838,5 +805,5 @@
  * ENULL:  src || format == NULL
  * ERANGE: n out of range
- * ETRUNC: truncated (unimplemented)
+ * ETRUNC: truncated
  */
 int sl_snprintf(char *str, size_t n,
@@ -855,5 +822,4 @@
   va_start (vl, format);
 #if defined(HAVE_VSNPRINTF) && !defined(HAVE_BROKEN_VSNPRINTF)
-  /* flawfinder: ignore */
   vsnprintf (str, n, format, vl);
   str[n-1] = '\0';
@@ -863,5 +829,4 @@
   if (total < n) 
     {
-      /* flawfinder: ignore */
       vsprintf (str, format, vl2);     /* program has checked that it fits */
       str[n-1] = '\0';
@@ -898,33 +863,33 @@
   register const char * q;
 
-  if (!(dst == NULL || src == NULL || *src == '\0'))
-    {
-      if (siz > 0) 
-	{
-
-	  /* How much free space do we have ?
-	   */
-	  dst_end  = strlen(dst);
-	  dst_free = siz - dst_end - 1;
-	  
-	  p = &dst[dst_end];
-	  q = src;
-	  
-	  while (dst_free > 0 && *q != '\0')
-	    {
-	      *p++ = *q++;
-	      --dst_free;
-	    }
-	
-	  /* NULL terminate dst.
-	   */
-	  *p = '\0';
-	
-	  if (*q == '\0')
-	    return SL_ENONE;
-	  else
-	    return SL_ETRUNC;
-	}
-    }
+  if (dst == NULL)
+    return SL_ENONE;
+  if (src == NULL || src == "") 
+    return SL_ENONE;
+
+  if (siz > 0) {
+
+    /* How much free space do we have ?
+     */
+    dst_end  = strlen(dst);
+    dst_free = siz - dst_end - 1;
+
+    p = &dst[dst_end];
+    q = src;
+
+    while (dst_free > 0 && *q != '\0')
+      {
+	*p++ = *q++;
+	--dst_free;
+      }
+
+    /* NULL terminate dst.
+     */
+    *p = '\0';
+
+    if (*q != '\0') 
+      return SL_ETRUNC;
+  }
+
   return SL_ENONE;
 }
@@ -945,27 +910,24 @@
   /* SL_ENTER(_("sl_strlcpy")); */
 
-  if (!((dst == NULL) || (src == NULL))) 
-    {
-      if (siz > 0) {
-	/* copy siz-1 characters 
-	 */
-	(void) strncpy(dst, src, siz-1);
-
-	/* NULL terminate
-	 */
-	dst[siz-1] = '\0';
-      }
-      return SL_ENONE;
-    }
-  else if (src == NULL)
-    {
-      if (dst && siz > 0) 
+  if (dst == NULL)
+    return SL_ENULL;
+  if (src == NULL)
+    { 
+      if (siz > 0) 
 	dst[0] = '\0';
       return SL_ENONE;
     }
-  else
-    {
-      return SL_ENULL;
-    } 
+
+
+  if (siz > 0) {
+    /* copy siz-1 characters 
+     */
+    (void) strncpy(dst, src, siz-1);
+
+    /* NULL terminate
+     */
+    dst[siz-1] = '\0';
+  }
+  return SL_ENONE;
 }
 
@@ -1044,41 +1006,4 @@
 }
 
-#include <ctype.h>
-int sl_strcasecmp(const char * one, const char * two)
-{
-#ifdef SL_FAIL_ON_ERROR
-  SL_REQUIRE (one != NULL, _("one != NULL"));
-  SL_REQUIRE (two != NULL, _("two != NULL"));
-#endif
-
-  if (one && two)
-    {
-      do {
-	if (*one && *two)
-	  {
-	    if (tolower((int) *one) == tolower((int) *two))
-	      {
-		++one; ++two;
-	      }
-	    else if (tolower((int) *one) < tolower((int) *two))
-	      return -1;
-	    else
-	      return 1;
-	  }
-	else if (*one == '\0' && *two == '\0')
-	  return 0;
-	else if (*one == '\0')
-	  return -1;
-	else
-	  return 1;
-      } while (1 == 1);
-    }
-  else if (one == NULL && two != NULL)
-    return -1;
-  else if (one != NULL && two == NULL)
-    return 1;
-  else
-    return -7; /* default to not equal */
-}
 
 int sl_strcmp(const char * a, const char * b)
@@ -1096,39 +1021,5 @@
     return (1);
   else
-    return (-7); /* default to not equal */
-}
-
-/* Does not report sign. */
-int sl_ts_strncmp(const char * a, const char * b, size_t n)
-{
-#ifdef SL_FAIL_ON_ERROR
-  SL_REQUIRE (a != NULL, _("a != NULL"));
-  SL_REQUIRE (b != NULL, _("b != NULL"));
-  SL_REQUIRE (n > 0, _("n > 0"));
-#endif
-
-  if (a != NULL && b != NULL)
-    {
-      const unsigned char *a1 = (const unsigned char *)a;
-      const unsigned char *b1 = (const unsigned char *)b;
-      size_t i;
-      int  retval=0;
-      /* The simple index based access is optimized best by the
-       * compiler (tested with gcc 7.3.0). */
-      for (i = 0; i < n; ++i)
-	{
-	  if (a1[i] == '\0' || b1[i] == '\0')
-	    break;
-	  retval |= (a1[i] ^ b1[i]);
-	}
-      /* if (retval == 0) --> false (0) */ 
-      return (retval != 0);
-    }
-  else if (a == NULL && b != NULL)
-    return (-1);
-  else if (a != NULL && b == NULL)
-    return (1);
-  else
-    return (-7); /* default to not equal */
+    return (-7); /* arbitrary */
 }
 
@@ -1148,23 +1039,5 @@
     return (1);
   else
-    return (-7); /* default to not equal */
-}
-
-int sl_strncasecmp(const char * a, const char * b, size_t n)
-{
-#ifdef SL_FAIL_ON_ERROR
-  SL_REQUIRE (a != NULL, _("a != NULL"));
-  SL_REQUIRE (b != NULL, _("b != NULL"));
-  SL_REQUIRE (n > 0, _("n > 0"));
-#endif
-
-  if (a != NULL && b != NULL)
-    return (strncasecmp(a, b, n));
-  else if (a == NULL && b != NULL)
-    return (-1);
-  else if (a != NULL && b == NULL)
-    return (1);
-  else
-    return (-7); /* default to not equal */
+    return (-7); /* arbitrary */
 }
 
@@ -1175,5 +1048,5 @@
 {
 #ifndef HAVE_STRSTR
-  unsigned int    i;
+  int             i;
   size_t          needle_len;
   size_t          haystack_len;
@@ -1214,6 +1087,6 @@
 static   gid_t   rgid_orig;
 
-static   int     uids_are_stored = S_FALSE;
-static   int     suid_is_set     = S_TRUE;
+static   int     uids_are_stored = SL_FALSE;
+static   int     suid_is_set     = SL_TRUE;
 
 #ifdef HAVE_SETRESUID
@@ -1229,5 +1102,5 @@
 int sl_is_suid()
 {
-  if (uids_are_stored == S_FALSE)
+  if (uids_are_stored == SL_FALSE)
     {
       if (getuid() == geteuid() && getgid() == getegid())
@@ -1252,6 +1125,6 @@
 {
   SL_ENTER(_("sl_get_euid"));
-  /* SL_REQUIRE(uids_are_stored == S_TRUE, _("uids_are_stored == S_TRUE"));*/
-  if (uids_are_stored == S_TRUE)
+  /* SL_REQUIRE(uids_are_stored == SL_TRUE, _("uids_are_stored == SL_TRUE"));*/
+  if (uids_are_stored == SL_TRUE)
     *ret = euid;
   else
@@ -1262,6 +1135,6 @@
 uid_t sl_ret_euid()
 {
-  /* SL_REQUIRE(uids_are_stored == S_TRUE, _("uids_are_stored == S_TRUE"));*/
-  if (uids_are_stored == S_TRUE)
+  /* SL_REQUIRE(uids_are_stored == SL_TRUE, _("uids_are_stored == SL_TRUE"));*/
+  if (uids_are_stored == SL_TRUE)
     return (euid);
   else
@@ -1276,6 +1149,6 @@
 {
   SL_ENTER(_("sl_get_egid"));
-  /* SL_REQUIRE(uids_are_stored == S_TRUE, _("uids_are_stored == S_TRUE"));*/
-  if (uids_are_stored == S_TRUE)
+  /* SL_REQUIRE(uids_are_stored == SL_TRUE, _("uids_are_stored == SL_TRUE"));*/
+  if (uids_are_stored == SL_TRUE)
     *ret = egid;
   else
@@ -1291,6 +1164,6 @@
 {
   SL_ENTER(_("sl_get_ruid"));
-  /* SL_REQUIRE(uids_are_stored == S_TRUE, _("uids_are_stored == S_TRUE"));*/
-  if (uids_are_stored == S_TRUE)
+  /* SL_REQUIRE(uids_are_stored == SL_TRUE, _("uids_are_stored == SL_TRUE"));*/
+  if (uids_are_stored == SL_TRUE)
     *ret = ruid;
   else
@@ -1306,6 +1179,6 @@
 {
   SL_ENTER(_("sl_get_rgid"));
-  /* SL_REQUIRE(uids_are_stored == S_TRUE, _("uids_are_stored == S_TRUE"));*/
-  if (uids_are_stored == S_TRUE)
+  /* SL_REQUIRE(uids_are_stored == SL_TRUE, _("uids_are_stored == SL_TRUE"));*/
+  if (uids_are_stored == SL_TRUE)
     *ret = rgid;
   else
@@ -1321,6 +1194,6 @@
 {
   SL_ENTER(_("sl_get_ruid_orig"));
-  /* SL_REQUIRE(uids_are_stored == S_TRUE, _("uids_are_stored == S_TRUE"));*/
-  if (uids_are_stored == S_TRUE)
+  /* SL_REQUIRE(uids_are_stored == SL_TRUE, _("uids_are_stored == SL_TRUE"));*/
+  if (uids_are_stored == SL_TRUE)
     *ret = ruid_orig;
   else
@@ -1336,6 +1209,6 @@
 {
   SL_ENTER(_("sl_get_rgid_orig"));
-  /* SL_REQUIRE(uids_are_stored == S_TRUE, _("uids_are_stored == S_TRUE"));*/
-  if (uids_are_stored == S_TRUE)
+  /* SL_REQUIRE(uids_are_stored == SL_TRUE, _("uids_are_stored == SL_TRUE"));*/
+  if (uids_are_stored == SL_TRUE)
     *ret = rgid_orig;
   else
@@ -1362,17 +1235,17 @@
   SL_ENTER(_("sl_set_suid"));
 
-  if (uids_are_stored == S_FALSE)
+  if (uids_are_stored == SL_FALSE)
     {
       SL_IRETURN(SL_ENONE, _("sl_set_suid"));
     }
 
-  SL_REQUIRE(uids_are_stored == S_TRUE, _("uids_are_stored == S_TRUE"));  
+  SL_REQUIRE(uids_are_stored == SL_TRUE, _("uids_are_stored == SL_TRUE"));  
 
   if (ruid == euid && rgid == egid) 
     {
-      suid_is_set = S_TRUE;
+      suid_is_set = SL_TRUE;
       SL_IRETURN(SL_ENONE, _("sl_set_suid"));
     }  
-  SL_REQUIRE(suid_is_set     == S_FALSE, _("suid_is_set == S_FALSE"));  
+  SL_REQUIRE(suid_is_set     == SL_FALSE, _("suid_is_set == SL_FALSE"));  
 
 #if defined(HAVE_SETRESUID)
@@ -1403,5 +1276,5 @@
 
   SL_REQUIRE(retval == 0, _("retval == 0"));
-  suid_is_set = S_TRUE;
+  suid_is_set = SL_TRUE;
   SL_IRETURN(SL_ENONE, _("sl_set_suid"));
 }
@@ -1417,17 +1290,17 @@
   SL_ENTER(_("sl_unset_suid"));
 
-  if (uids_are_stored == S_FALSE)
+  if (uids_are_stored == SL_FALSE)
     {
       SL_IRETURN(SL_ENONE, _("sl_unset_suid"));
     }
 
-  SL_REQUIRE(uids_are_stored == S_TRUE, _("uids_are_stored == S_TRUE"));
+  SL_REQUIRE(uids_are_stored == SL_TRUE, _("uids_are_stored == SL_TRUE"));
 
   if (ruid == euid && rgid == egid)
     {
-      suid_is_set = S_FALSE;
+      suid_is_set = SL_FALSE;
       SL_IRETURN(SL_ENONE, _("sl_unset_suid"));
     }  
-  SL_REQUIRE(suid_is_set     == S_TRUE, _("suid_is_set == S_TRUE"));  
+  SL_REQUIRE(suid_is_set     == SL_TRUE, _("suid_is_set == SL_TRUE"));  
 
 #if defined(HAVE_SETRESUID)
@@ -1457,5 +1330,5 @@
 
   SL_REQUIRE(retval == 0, _("retval == 0"));
-  suid_is_set = S_FALSE;
+  suid_is_set = SL_FALSE;
   SL_IRETURN(SL_ENONE, _("sl_unset_suid"));
 }
@@ -1468,5 +1341,5 @@
 {
   SL_ENTER(_("sl_save_uids"));
-  if (uids_are_stored == S_TRUE) 
+  if (uids_are_stored == SL_TRUE) 
     SL_IRETURN(SL_EREPEAT, _("sl_save_uids"));
 
@@ -1477,5 +1350,5 @@
   ruid = ruid_orig;
   rgid = rgid_orig;
-  uids_are_stored = S_TRUE;
+  uids_are_stored = SL_TRUE;
 
   SL_IRETURN(SL_ENONE, _("sl_save_uids"));
@@ -1490,5 +1363,5 @@
 {
   SL_ENTER(_("sl_drop_privileges"));
-  SL_REQUIRE(uids_are_stored == S_TRUE, _("uids_are_stored == S_TRUE"));
+  SL_REQUIRE(uids_are_stored == SL_TRUE, _("uids_are_stored == SL_TRUE"));
 
   SL_REQUIRE(setgid(rgid_orig) == 0, _("setgid(rgid_orig) == 0"));
@@ -1515,5 +1388,5 @@
 {
   SL_ENTER(_("sl_policy_get_root"));
-  SL_REQUIRE(uids_are_stored == S_FALSE, _("uids_are_stored == S_FALSE"));
+  SL_REQUIRE(uids_are_stored == SL_FALSE, _("uids_are_stored == SL_FALSE"));
 
   SL_REQUIRE (sl_save_uids() == SL_ENONE, _("sl_save_uids() == SL_ENONE"));
@@ -1528,5 +1401,5 @@
       rgid = egid;
     }
-  suid_is_set = S_TRUE;
+  suid_is_set = SL_TRUE;
   if (euid == 0)
     {
@@ -1546,20 +1419,13 @@
 int sl_policy_get_real(char * user)
 {
+  struct passwd * tempres;
+
   SL_ENTER(_("sl_policy_get_real"));
-  SL_REQUIRE(uids_are_stored == S_FALSE, _("uids_are_stored == S_FALSE"));
+  SL_REQUIRE(uids_are_stored == SL_FALSE, _("uids_are_stored == SL_FALSE"));
   SL_REQUIRE (sl_save_uids() == SL_ENONE, _("sl_save_uids() == SL_ENONE"));
 
   if (euid == 0 || ruid == 0)
     {
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-      struct passwd    pwd;
-      char          *  buffer;
-      struct passwd *  tempres;
-      buffer = calloc(1,SH_PWBUF_SIZE);
-      SL_REQUIRE (buffer != NULL, _("buffer != NULL"));
-      sh_getpwnam_r(user, &pwd, buffer, SH_PWBUF_SIZE, &tempres);
-#else
-      struct passwd * tempres = sh_getpwnam(user);
-#endif
+      tempres = sh_getpwnam(user);
 
       SL_REQUIRE (NULL != tempres, _("tempres != NULL"));
@@ -1567,7 +1433,4 @@
       rgid_orig = tempres->pw_gid;
       ruid_orig = tempres->pw_uid;
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-      free(buffer);
-#endif
     }
   else
@@ -1580,5 +1443,5 @@
 	      _("sl_drop_privileges() == SL_ENONE"));
 
-  suid_is_set = S_TRUE;
+  suid_is_set = SL_TRUE;
   SL_IRETURN(SL_ENONE, _("sl_policy_get_real"));
 }
@@ -1590,35 +1453,29 @@
  * Do nothing if not SUID.
  */
-int sl_policy_get_user(const char * user)
-{
+int sl_policy_get_user(char * user)
+{
+  struct passwd * tempres;
+
   SL_ENTER(_("sl_policy_get_user"));
 
   SL_REQUIRE(user != NULL, _("user != NULL"));
-  SL_REQUIRE(uids_are_stored == S_FALSE, _("uids_are_stored == S_FALSE"));
+  SL_REQUIRE(uids_are_stored == SL_FALSE, _("uids_are_stored == SL_FALSE"));
   SL_REQUIRE (sl_save_uids() == SL_ENONE, _("sl_save_uids() == SL_ENONE"));
 
-#ifndef SH_ALLOW_SUID
   if (euid != ruid || egid != rgid)
     {
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-      struct passwd    pwd;
-      char          *  buffer;
-      struct passwd *  tempres;
-      buffer = calloc(1,SH_PWBUF_SIZE);
-      SL_REQUIRE (buffer != NULL, _("buffer != NULL"));
-      sh_getpwnam_r(user, &pwd, buffer, SH_PWBUF_SIZE, &tempres);
-#else
-      struct passwd * tempres = sh_getpwnam(user);
-#endif
+      tempres = sh_getpwnam(user);
 
       SL_REQUIRE (NULL != tempres, _("tempres != NULL"));
 
+#if 0
+      rgid = tempres->pw_gid;
+      ruid = tempres->pw_uid;
+      SL_REQUIRE(sl_unset_suid() == SL_ENONE, 
+		 _("sl_unset_suid() == SL_ENONE"));
+#endif
       SL_REQUIRE (sl_drop_privileges() == SL_ENONE,
 		  _("sl_drop_privileges() == SL_ENONE"));
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-      free(buffer);
-#endif
-    }
-#endif
+    }
   SL_IRETURN(SL_ENONE, _("sl_policy_get_user"));
 }
@@ -1640,77 +1497,13 @@
 
 typedef struct openfiles {
-  SL_TICKET ticket;          /* The unique  ID.      */ 
-  int fd;                    /* The file descriptor. */
-  FILE * stream;             /* The file descriptor. */
-  char * path;               /* The file path.       */
-  int flush;                 /* Whether we want to flush the cache */
-  char ofile[SL_OFILE_SIZE]; /* origin file */
-  int  oline;                /* origin line */
-  sh_string * content;       /* The file content     */
+  SL_TICKET ticket;     /* The unique  ID.      */ 
+  int fd;               /* The file descriptor. */
+  char * path;          /* The file path.       */
 } SL_OFILE; 
 
 static SL_OFILE * ofiles[MAXFD]; 
 
-static char stale_orig_file[64] = { '\0' };
-static int  stale_orig_line = -1;
-static char stale_orig_mesg[128];
-
-static char badfd_orig_file[64] = { '\0' };
-static int  badfd_orig_line = -1;
-static char badfd_orig_mesg[128];
-
-
-char * sl_check_stale()
-{
-  if (stale_orig_line == -1)
-    return NULL;
-  sl_snprintf(stale_orig_mesg, sizeof(stale_orig_mesg), 
-	      _("stale handle, %s, %d"), stale_orig_file, stale_orig_line);
-  stale_orig_file[0] = '\0';
-  stale_orig_line    = -1;
-  return stale_orig_mesg;
-}
-
-char * sl_check_badfd()
-{
-  if (badfd_orig_line == -1)
-    return NULL;
-  sl_snprintf(badfd_orig_mesg, sizeof(badfd_orig_mesg), 
-	      _("close on file descriptor with allocated handle, %s, %d"), 
-	      badfd_orig_file, badfd_orig_line);
-  badfd_orig_file[0] = '\0';
-  badfd_orig_line    = -1;
-  return badfd_orig_mesg;
-}
-
-typedef struct { volatile unsigned int atom; } atomic_t;
-static atomic_t nonce_counter = { TOFFSET };
-
-#if defined(__GNUC__) && (defined(__i486__) || defined(__x86_64__))
-/* from linux/include/asm-i386/atomic.h */
-static unsigned int atomic_add ( unsigned int i, atomic_t *var)
-{
-  unsigned int j = i;
-
-  __asm__ __volatile__ ("lock; xaddl %0, %1"
-			: "+r" (i), "+m" (var->atom)
-			: : "memory");
-  return j+i; 
-}
-#else
-SH_MUTEX_STATIC(mutex_ticket, PTHREAD_MUTEX_INITIALIZER);
-
-static unsigned int atomic_add ( unsigned int i, atomic_t *var)
-{
-  volatile unsigned int j;
-
-  SH_MUTEX_LOCK_UNSAFE(mutex_ticket);
-  var->atom += i;
-  j = var->atom;
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_ticket);
-
-  return j;
-}
-#endif
+
+static unsigned int nonce_counter = TOFFSET;
 
 static
@@ -1719,14 +1512,9 @@
   unsigned int high; /* index */ 
   unsigned int low;  /* nonce */
-  SL_TICKET    retval = SL_EINTERNAL;
-  unsigned int nonce;/* nonce */
 
   SL_ENTER(_("sl_create_ticket"));
 
-  if (myindex >= MAXFD)
-    {
-      retval = SL_EINTERNAL01;
-      goto out_ticket;
-    }
+  if (myindex >= MAXFD) 
+    SL_IRETURN (SL_EINTERNAL, _("sl_create_ticket")); 
 
   /* mask out the high bit and check that it is not used
@@ -1735,35 +1523,21 @@
   high = (myindex + TOFFSET) & 0x7fff; 
 
-  if (high != myindex + TOFFSET)
-    {
-      retval = SL_EINTERNAL02;
-      goto out_ticket;
-    }
-
-  nonce = atomic_add(1, &nonce_counter);
-
+  if (high != myindex + TOFFSET) 
+    SL_IRETURN (SL_EINTERNAL, _("sl_create_ticket")); 
+
+  low = nonce_counter & 0xffff;
+
+  /* Overflow -> nonce too big.
+   */
+  if ((low != nonce_counter++) || low == 0)
+    SL_IRETURN (SL_EINTERNAL, _("sl_create_ticket"));
+ 
   /* Wrap around the nonce counter.
    * This is a dirty trick.
    */
-  if (nonce > 0x7fff)
-    {
-      nonce_counter.atom = TOFFSET;
-      nonce = atomic_add(1, &nonce_counter);
-    }
-
-  low = nonce & 0xffff;
-
-  /* Overflow -> nonce too big.
-   */
-  if ((low != nonce) || low == 0)
-    {
-      retval = SL_EINTERNAL03;
-      goto out_ticket;
-    }
-
-  retval = (SL_TICKET) ((high << 16) | low);
-
- out_ticket:
-  SL_RETURN (retval, _("sl_create_ticket")); 
+  if (nonce_counter > 0x7fff)
+    nonce_counter = TOFFSET;
+
+  SL_RETURN ((SL_TICKET) ((high << 16) | low), _("sl_create_ticket")); 
 }
 
@@ -1785,21 +1559,18 @@
 
   if ((of = ofiles[myindex])->fd < 0 || of->fd >= MAXFD )
-    return (SL_EINTERNAL04);
+    return (SL_EINTERNAL);
 
   if (((of->ticket) & 0xffff) == 0)
-    return (SL_EINTERNAL05); 
+    return (SL_EINTERNAL); 
 
   return (myindex); 
 }
 
-SL_TICKET sl_make_ticket (const char * ofile, int oline,
-			  int fd, const char * filename, FILE * stream)
-{
-  size_t    len;
+SL_TICKET sl_make_ticket (int fd, char * filename)
+{
   SL_TICKET ticket;
   SL_ENTER(_("sl_make_ticket"));
   /* Make entry.
    */
-  /* cppcheck-suppress arrayIndexOutOfBoundsCond */
   if (fd >= MAXFD || fd < 0)
      {
@@ -1807,29 +1578,18 @@
      }
 
-  if (ofiles[fd] != NULL) /* stale entry */
-    {
-      /* SL_IRETURN(SL_EINTERNAL06, _("sl_make_ticket")); */
-      sl_strlcpy(stale_orig_file, ofiles[fd]->ofile, sizeof(stale_orig_file));
-      stale_orig_line = ofiles[fd]->oline;
-
-      if (ofiles[fd]->content)
-	sh_string_destroy(&(ofiles[fd]->content));
-      (void) free (ofiles[fd]->path);
-      (void) free (ofiles[fd]);
+   if (ofiles[fd] != NULL)
+    {
+      SL_IRETURN(SL_EINTERNAL, _("sl_make_ticket"));
+    }
+
+  if ( (ofiles[fd] = (SL_OFILE *) malloc(sizeof(SL_OFILE))) == NULL)
+    {
+      SL_IRETURN(SL_EMEM, _("sl_make_ticket"));
+    }
+
+   if ( (ofiles[fd]->path = (char *) malloc( strlen(filename)+1) ) == NULL)
+    {
+      free(ofiles[fd]);
       ofiles[fd] = NULL;
-    }
-
-  if ( (ofiles[fd] = calloc(1,sizeof(SL_OFILE))) == NULL)
-    {
-      SL_IRETURN(SL_EMEM, _("sl_make_ticket"));
-    }
-
-  len = sl_strlen(filename)+1;
-
-  if ( (ofiles[fd]->path = calloc(1,len) ) == NULL)
-    {
-      free (ofiles[fd]);
-      ofiles[fd] = NULL;
-      /* cppcheck-suppress memleak */
       SL_IRETURN(SL_EMEM, _("sl_make_ticket"));
     }
@@ -1843,20 +1603,11 @@
       (void) free (ofiles[fd]->path);
       (void) free (ofiles[fd]);
-      ofiles[fd] = NULL;
-      /* cppcheck-suppress memleak */
       SL_IRETURN(ticket, _("sl_make_ticket"));
     }
 
-  sl_strlcpy (ofiles[fd]->path, filename, len);
-  ofiles[fd]->ticket  = ticket;
-  ofiles[fd]->fd      = fd;
-  ofiles[fd]->content = NULL;
-  ofiles[fd]->stream  = stream;
-  ofiles[fd]->flush   = S_FALSE;
-
-  sl_strlcpy(ofiles[fd]->ofile, ofile, SL_OFILE_SIZE);
-  ofiles[fd]->oline = oline;
-
-  /* cppcheck-suppress memleak */
+  strcpy (ofiles[fd]->path, filename);                    /* Known to fit  */
+  ofiles[fd]->ticket = ticket;
+  ofiles[fd]->fd     = fd;
+
   SL_IRETURN(ticket, _("sl_make_ticket"));
 }
@@ -1873,5 +1624,5 @@
 
 #if !defined(O_NOATIME)
-#if defined(__linux__) && (defined(__i386__) || defined(__x86_64__) || defined(__PPC__))
+#if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
 #define O_NOATIME 01000000
 #else
@@ -1888,15 +1639,12 @@
 
 static
-int sl_open_file (const char * ofile, int oline,
-		  const char *filename, int mode, int priv)
+int sl_open_file (char *filename, int mode, int priv)
 {
   struct stat   lbuf;
   struct stat   buf;
-  int           errval = 0;
   int           lstat_return;
   int           stat_return;
   int           fd;
   int           sflags;
-  size_t        len;
   SL_TICKET     ticket;
  
@@ -1914,10 +1662,10 @@
     SL_IRETURN(SL_ENULL, _("sl_open_file"));
   if (mode < SL_OPEN_MIN || mode > SL_OPEN_MAX)
-    SL_IRETURN(SL_EINTERNAL07, _("sl_open_file"));
+    SL_IRETURN(SL_EINTERNAL, _("sl_open_file"));
     
   /* "This system call always succeeds and the previous value of
    * the mask is returned." 
    */
-  (void) umask (0);
+  (void) umask (0); 
 
   if (mode == SL_OPEN_FOR_FASTREAD)
@@ -1925,10 +1673,8 @@
       fd = aud_open_noatime (FIL__, __LINE__, priv, filename, 
 			     O_RDONLY|O_NONBLOCK, 0, &o_noatime);
-      /*
       if (fd >= 0) {
 	sflags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
 	retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags & ~O_NONBLOCK);
       }
-      */
       if (fd < 0)
 	SL_IRETURN(SL_EBADFILE, _("sl_open_file"));
@@ -1944,5 +1690,4 @@
   else
     lstat_return = retry_lstat(FIL__, __LINE__, filename, &lbuf);
-  errval = errno;
 #ifdef USE_SUID
   if (priv == SL_YESPRIV)
@@ -1954,10 +1699,9 @@
       lstat_return = ENOENT;
       if ( (mode == SL_OPEN_FOR_READ && lstat_return == ENOENT) ||
-	   (errval != ENOENT))
+	   (errno != ENOENT))
 	{
 	  TPT(( 0, FIL__, __LINE__, _("msg=<lstat: %s> errno=<%d>\n"), 
-	    filename, errval));
-	  errno = errval;
-	  SL_IRETURN(SL_ESTAT, _("sl_open_file"));
+	    filename, errno));
+	  SL_IRETURN(SL_EBADFILE, _("sl_open_file"));
 	}
     }
@@ -1966,9 +1710,6 @@
        ( S_ISDIR(lbuf.st_mode) || (S_IWOTH & lbuf.st_mode) ) 
       )
-    {
-      int retval = S_ISDIR(lbuf.st_mode) ? SL_EISDIR : SL_EBADOTH;
-      errno = 0;
-      SL_IRETURN(retval, _("sl_open_file"));
-    }
+    SL_IRETURN(SL_EBADFILE, _("sl_open_file"));
+
     
   /* O_NOATIME has an effect for read(). But write() ?.
@@ -1979,5 +1720,4 @@
       fd = aud_open_noatime (FIL__, __LINE__, priv, filename, 
 			     O_RDONLY|O_NONBLOCK, 0, &o_noatime);
-      errval = errno;
       if (fd >= 0) {
 	sflags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
@@ -1992,27 +1732,19 @@
 	fd = aud_open (FIL__, __LINE__, priv, filename, 
 		       O_WRONLY,                   open_mode);
-      errval = errno;
       break;
     case SL_OPEN_SAFE_RDWR:
       if (lstat_return == ENOENT)
-	{
-	  fd = aud_open (FIL__, __LINE__, priv, filename, 
-			 O_RDWR|O_CREAT|O_EXCL,      open_mode);
-	  errval = errno;
-	}
+      	fd = aud_open (FIL__, __LINE__, priv, filename, 
+		       O_RDWR|O_CREAT|O_EXCL,      open_mode);
       else
-	{
-	  errno = errval;
-	  SL_IRETURN(SL_EBADFILE, _("sl_open_file"));
-	}
+	SL_IRETURN(SL_EBADFILE, _("sl_open_file"));
       break;
     case SL_OPEN_FOR_RDWR:
       if (lstat_return == ENOENT)
-	fd = aud_open (FIL__, __LINE__, priv, filename, 
-			 O_RDWR|O_CREAT|O_EXCL,      open_mode);
+      	fd = aud_open (FIL__, __LINE__, priv, filename, 
+		       O_RDWR|O_CREAT|O_EXCL,      open_mode);
       else
 	fd = aud_open (FIL__, __LINE__, priv, filename, 
 		       O_RDWR,                     open_mode);
-      errval = errno;
       break;
     case SL_OPEN_FOR_WTRUNC:
@@ -2023,5 +1755,4 @@
 	fd = aud_open (FIL__, __LINE__, priv, filename, 
 		       O_WRONLY|O_TRUNC,           open_mode);
-      errval = errno;
       break;
     case SL_OPEN_FOR_RWTRUNC:
@@ -2032,9 +1763,7 @@
 	fd = aud_open (FIL__, __LINE__, priv, filename, 
 		       O_RDWR|O_TRUNC,             open_mode);
-      errval = errno;
       break;
     default:
-      errno = 0;
-      SL_IRETURN(SL_EINTERNAL08, _("sl_open_file"));
+      SL_IRETURN(SL_EINTERNAL, _("sl_open_file"));
     }
 
@@ -2042,6 +1771,5 @@
     {
       TPT(( 0, FIL__, __LINE__, _("msg=<Error opening: %s> errno=<%d>\n"), 
-	    filename, errval));
-      errno = errval;
+	    filename, errno));
       SL_IRETURN(SL_EBADFILE, _("sl_open_file"));
     }
@@ -2052,5 +1780,4 @@
 #endif
   stat_return = retry_fstat(FIL__, __LINE__, fd, &buf);
-  errval = errno;
 #ifdef USE_SUID
   if (priv == SL_YESPRIV)
@@ -2060,14 +1787,11 @@
   if (stat_return < 0)
     {
-      sl_close_fd (FIL__, __LINE__, fd);
-      errno = errval;
-      SL_IRETURN(SL_EFSTAT, _("sl_open_file"));
-    }
-
-  errno = 0;
+      close (fd);
+      SL_IRETURN(SL_EBADFILE, _("sl_open_file"));
+    }
 
   if (lstat_return != ENOENT && buf.st_ino != lbuf.st_ino)
     {
-      sl_close_fd (FIL__, __LINE__, fd);
+      close (fd);
       SL_IRETURN(SL_EBOGUS, _("sl_open_file"));
     }
@@ -2077,40 +1801,27 @@
   /* Make entry.
    */
-  /* cppcheck-suppress arrayIndexOutOfBoundsCond */
   if (fd >= MAXFD)
      {
-	sl_close_fd(FIL__, __LINE__, fd);
+	close(fd);
 	SL_IRETURN(SL_TOOMANY, _("sl_open_file"));
      }
 
-  if (ofiles[fd] != NULL) /* stale entry */
-    {
-      /*
-      sl_close_fd(FIL__, __LINE__, fd);
-      SL_IRETURN(SL_EINTERNAL09, _("sl_open_file"));
-      */
-      sl_strlcpy(stale_orig_file, ofiles[fd]->ofile, sizeof(stale_orig_file));
-      stale_orig_line = ofiles[fd]->oline;
-
-      if (ofiles[fd]->content)
-	sh_string_destroy(&(ofiles[fd]->content));
-      (void) free (ofiles[fd]->path);
-      (void) free (ofiles[fd]);
+   if (ofiles[fd] != NULL)
+    {
+      close(fd);
+      SL_IRETURN(SL_EINTERNAL, _("sl_open_file"));
+    }
+
+  if ( (ofiles[fd] = (SL_OFILE *) malloc(sizeof(SL_OFILE))) == NULL)
+    {
+      close(fd);
+      SL_IRETURN(SL_EMEM, _("sl_open_file"));
+    }
+
+   if ( (ofiles[fd]->path = (char *) malloc( strlen(filename)+1) ) == NULL)
+    {
+      free(ofiles[fd]);
       ofiles[fd] = NULL;
-    }
-
-  if ( (ofiles[fd] = calloc(1,sizeof(SL_OFILE))) == NULL)
-    {
-      sl_close_fd(FIL__, __LINE__, fd);
-      SL_IRETURN(SL_EMEM, _("sl_open_file"));
-    }
-
-  len = sl_strlen(filename)+1;
-
-  if ( (ofiles[fd]->path = calloc(1,len) ) == NULL)
-    {
-      free (ofiles[fd]);
-      ofiles[fd] = NULL;
-      sl_close_fd(FIL__, __LINE__, fd);
+      close(fd);
       SL_IRETURN(SL_EMEM, _("sl_open_file"));
     }
@@ -2124,55 +1835,17 @@
       (void) free (ofiles[fd]->path);
       (void) free (ofiles[fd]);
-      ofiles[fd] = NULL;
-      sl_close_fd(FIL__, __LINE__, fd);
+      close(fd);
       SL_IRETURN(ticket, _("sl_open_file"));
     }
 
-  sl_strlcpy (ofiles[fd]->path, filename, len);
-  ofiles[fd]->ticket  = ticket;
-  ofiles[fd]->fd      = fd;
-  ofiles[fd]->content = NULL;
-  ofiles[fd]->stream  = NULL;
-  ofiles[fd]->flush   = S_FALSE;
-
-  sl_strlcpy(ofiles[fd]->ofile, ofile, SL_OFILE_SIZE);
-  ofiles[fd]->oline = oline;
+  strcpy (ofiles[fd]->path, filename);                    /* Known to fit  */
+  ofiles[fd]->ticket = ticket;
+  ofiles[fd]->fd     = fd;
 
   SL_IRETURN(ticket, _("sl_open_file"));
 }
 
-FILE * sl_stream (SL_TICKET ticket, char * mode)
-{
-  int    fd;
-
-  if (SL_ISERROR(fd = sl_read_ticket(ticket)))
-    return (NULL);
-
-  if (ofiles[fd] == NULL || fd != ofiles[fd]->fd || 
-      ticket != ofiles[fd]->ticket || fd < 0)
-    return (NULL);
-
-  if (!ofiles[fd]->stream)
-    ofiles[fd]->stream = fdopen(fd, mode);
-
-  return ofiles[fd]->stream;
-}
-
-int get_the_fd (SL_TICKET ticket)
-{
-  int fd;
-
-  if (SL_ISERROR(fd = sl_read_ticket(ticket)))
-    return (fd);
-
-  if (ofiles[fd] == NULL || fd != ofiles[fd]->fd || 
-      ticket != ofiles[fd]->ticket || fd < 0)
-    return (SL_EINTERNAL10);
-
-  return (fd);
-}
-
 static
-int check_fname_priv (const char * fname, int priv)
+int check_fname_priv (char * fname, int priv)
 {
   SL_ENTER(_("check_fname_priv"));
@@ -2180,10 +1853,9 @@
     SL_IRETURN(SL_ENULL, _("check_fname_priv"));
   if (priv != SL_YESPRIV && priv != SL_NOPRIV)
-    SL_IRETURN(SL_EINTERNAL11, _("check_fname_priv"));
+    SL_IRETURN(SL_EINTERNAL, _("check_fname_priv"));
   SL_IRETURN(SL_ENONE, _("check_fname_priv"));
 }
   
-SL_TICKET sl_open_write (const char * ofile, int oline,
-			 const char * fname, int priv)
+SL_TICKET sl_open_write (char * fname, int priv)
 {
   long status;
@@ -2193,10 +1865,9 @@
     SL_IRETURN(status, _("sl_open_write"));
 
-  status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_WRITE, priv);
+  status = sl_open_file(fname, SL_OPEN_FOR_WRITE, priv);
   SL_IRETURN(status, _("sl_open_write"));
 }
 
-SL_TICKET sl_open_read (const char * ofile, int oline,
-			const char * fname, int priv)
+SL_TICKET sl_open_read (char * fname, int priv)
 {
   long status;
@@ -2211,61 +1882,9 @@
     }
 
-  status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_READ, priv);
+  status = sl_open_file(fname, SL_OPEN_FOR_READ, priv);
   SL_IRETURN(status, _("sl_open_read"));
 }
 
-#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_MINCORE) && defined(POSIX_FADV_DONTNEED)
-static int sl_check_mincore(int fd)
-{
-  /* Idea from Tobias Oetiker (http://insights.oetiker.ch/linux/fadvise.html)
-   */
-  struct stat fbuf;
-  int retval = -1;
-
-  if (0 == fstat(fd, &fbuf))
-    {
-      void *f_map;
-      
-      f_map = mmap((void *)0, fbuf.st_size, PROT_NONE, MAP_SHARED, fd, 0);
-      if (MAP_FAILED != f_map)
-	{
-	  extern int sh_unix_pagesize(void);
-	  size_t i;
-	  size_t page_size    = sh_unix_pagesize();
-	  size_t vec_size     = (fbuf.st_size+page_size-1)/page_size;
-	  unsigned char * vec = calloc(1, vec_size);
-
-	  if (vec)
-	    {
-	      mincore(f_map, fbuf.st_size, vec);
-	      /* imax = fbuf.st_size/page_size; */
-	      for (i = 0; i <= vec_size; ++i)
-		{
-		  if (vec[i]&1)
-		    {
-		      goto incore;
-		    }
-		}
-	      retval = 0;
-	    incore:
-	      free(vec);
-	    }
-	  munmap(f_map, fbuf.st_size);
-	}
-    }
-  return retval;
-}
-#endif
-
-static int sl_drop_cache = S_FALSE;
-
-int sl_set_drop_cache(const char * str)
-{
-  extern int sh_util_flagval(const char * c, int * fval);
-  return sh_util_flagval(str, &sl_drop_cache);
-}
-
-SL_TICKET sl_open_fastread (const char * ofile, int oline,
-			    const char * fname, int priv)
+SL_TICKET sl_open_fastread (char * fname, int priv)
 {
   long status;
@@ -2275,25 +1894,9 @@
     SL_IRETURN(status, _("sl_open_read"));
 
-  status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_FASTREAD, priv);
-
-#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_MINCORE) && defined(POSIX_FADV_DONTNEED)
-
-  if (S_FALSE != sl_drop_cache && !SL_ISERROR(status))
-    {
-      int fd = get_the_fd(status);
-      if (fd >= 0)
-	{
-	  if (0 == sl_check_mincore(fd))
-	    ofiles[fd]->flush = S_TRUE;
-	}
-    }
-
-#endif
-
+  status = sl_open_file(fname, SL_OPEN_FOR_FASTREAD, priv);
   SL_IRETURN(status, _("sl_open_fastread"));
 }
 
-SL_TICKET sl_open_rdwr (const char * ofile, int oline,
-			const char * fname, int priv)
+SL_TICKET sl_open_rdwr (char * fname, int priv)
 {
   long status;
@@ -2303,10 +1906,9 @@
     SL_IRETURN(status, _("sl_open_rdwr"));
 
-  status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_RDWR, priv);
+  status = sl_open_file(fname, SL_OPEN_FOR_RDWR, priv);
   SL_IRETURN(status, _("sl_open_rdwr"));
 }
 
-SL_TICKET sl_open_safe_rdwr (const char * ofile, int oline,
-			     const char * fname, int priv)
+SL_TICKET sl_open_safe_rdwr (char * fname, int priv)
 {
   long status;
@@ -2316,10 +1918,9 @@
     SL_IRETURN(status, _("sl_open_safe_rdwr"));
 
-  status = sl_open_file(ofile, oline, fname, SL_OPEN_SAFE_RDWR, priv);
+  status = sl_open_file(fname, SL_OPEN_SAFE_RDWR, priv);
   SL_IRETURN(status, _("sl_open_safe_rdwr"));
 }
 
-SL_TICKET sl_open_write_trunc (const char * ofile, int oline,
-			       const char * fname, int priv)
+SL_TICKET sl_open_write_trunc (char * fname, int priv)
 {
   long status;
@@ -2329,10 +1930,9 @@
     SL_IRETURN(status, _("sl_open_write_trunc"));
 
-  status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_WTRUNC, priv);
+  status = sl_open_file(fname, SL_OPEN_FOR_WTRUNC, priv);
   SL_IRETURN(status, _("sl_open_write_trunc"));
 }
 
-SL_TICKET sl_open_rdwr_trunc (const char * ofile, int oline,
-			      const char * fname, int priv)
+SL_TICKET sl_open_rdwr_trunc (char * fname, int priv)
 {
   long status;
@@ -2342,10 +1942,10 @@
     SL_IRETURN(status, _("sl_open_rdwr_trunc"));
 
-  status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_RWTRUNC, priv);
+  status = sl_open_file(fname, SL_OPEN_FOR_RWTRUNC, priv);
   SL_IRETURN(status, _("sl_open_rdwr_trunc"));
 }
 
 
-int sl_init_content (SL_TICKET ticket, size_t size)
+int get_the_fd (SL_TICKET ticket)
 {
   int fd;
@@ -2354,62 +1954,12 @@
     return (fd);
 
-  if (ofiles[fd] == NULL || fd != ofiles[fd]->fd || 
-      ticket != ofiles[fd]->ticket || fd < 0)
-    return (SL_EINTERNAL12);
-
-  if (ofiles[fd]->content)
-    sh_string_destroy(&(ofiles[fd]->content));
-  ofiles[fd]->content = sh_string_new(size);
-
-  return SL_ENONE;
-}
-
-sh_string * sl_get_content (SL_TICKET ticket)
-{
-  int fd;
-
-  if (SL_ISERROR(fd = sl_read_ticket(ticket)))
-    return (NULL);
-
-  if (ofiles[fd] == NULL || fd != ofiles[fd]->fd || 
-      ticket != ofiles[fd]->ticket || fd < 0)
-    return (NULL);
-
-  return (ofiles[fd]->content);
-}
-
-int sl_lock (SL_TICKET ticket)
-{
-  int fd;
-  struct flock lock;
-  int retval;
- 
-  SL_ENTER(_("sl_lock"));
-
-  if (SL_ISERROR(fd = get_the_fd (ticket)))
-    SL_IRETURN(fd, _("sl_lock"));
-
-  lock.l_type   = F_WRLCK;
-  lock.l_whence = SEEK_SET;
-  lock.l_start  = 0;
-  lock.l_len    = 0;
-
-  /* F_SETLK returns if the lock cannot be obtained */
-  do {
-    retval = fcntl(fd, F_SETLK, &lock);
-  } while (retval < 0 && errno == EINTR);
-
-  if (retval < 0 && errno == EBADF)
-    SL_IRETURN(SL_ETICKET, _("sl_lock"));
-  else if (retval < 0)
-    SL_IRETURN(SL_EBADFILE, _("sl_lock"));
-  else
-    SL_IRETURN(SL_ENONE, _("sl_lock"));
- }
- 
+  if (ofiles[fd] == NULL || fd != ofiles[fd]->fd || fd < 0)
+    return (SL_EINTERNAL);
+  return (fd);
+}
+
 int sl_close (SL_TICKET ticket) 
 {
   register int fd;
-  FILE * fp = NULL;
 
   SL_ENTER(_("sl_close"));
@@ -2418,81 +1968,21 @@
     SL_IRETURN(fd, _("sl_close"));
 
-  if (ofiles[fd] != NULL)
-    {
-#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_MINCORE) && defined(POSIX_FADV_DONTNEED)
-      if (ofiles[fd]->flush == S_TRUE)
-	{
-	  posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
-	}
-#endif
-      if (ofiles[fd]->content)
-	sh_string_destroy(&(ofiles[fd]->content));
-      (void) free (ofiles[fd]->path);
-      fp = ofiles[fd]->stream;
-      (void) free (ofiles[fd]);
-      ofiles[fd] = NULL;
-    }
-
   /* This may fail, but what to do then ?
    */
-  if (fp)
-    {
-      if (0 != fclose (fp)) /* within sl_close */
-	{
-	  TPT((0, FIL__, __LINE__, 
-	       _("msg=<Error fclosing file.>, fd=<%d>, err=<%s>\n"), 
-	       fd, strerror(errno)));
-	  SL_IRETURN(SL_ECLOSE, _("sl_close")); 
-	}
-    }
-  else
-    {
-      if (0 != close(fd)) /* within sl_close */
-	{
-	  TPT((0, FIL__, __LINE__, 
-	       _("msg=<Error closing file.>, fd=<%d>, err=<%s>\n"), 
-	       fd, strerror(errno)));
-	  SL_IRETURN(SL_ECLOSE, _("sl_close")); 
-	}
+  if (0 != close(fd) && ofiles[fd] != NULL)
+    {
+      TPT((0, FIL__, __LINE__, 
+	   _("msg=<Error closing file.>, path=<%s>, fd=<%d>\n"), 
+	   ofiles[fd]->path, fd));
+    }
+
+  if (ofiles[fd] != NULL)
+    {
+      (void) free(ofiles[fd]->path);
+      (void) free(ofiles[fd]);
+      ofiles[fd] = NULL;
     }
 
   SL_IRETURN(SL_ENONE, _("sl_close")); 
-}
-
-int sl_close_fd (const char * file, int line, int fd)
-{
-  int ret = -1;
-
-  SL_ENTER(_("sl_close_fd"));
-
-  if (fd >= 0 && fd < MAXFD && ofiles[fd] != NULL) /* stale ofiles[fd] handle */
-    {
-      sl_strlcpy(badfd_orig_file, file, sizeof(badfd_orig_file));
-      badfd_orig_line = line;
-    }
-
-  ret = close(fd); /* within sl_close_fd wrapper */
-
-  SL_IRETURN(ret, _("sl_close_fd")); 
-}
-
-int sl_fclose (const char * file, int line, FILE * fp)
-{
-  int ret = -1;
-  int fd;
-
-  SL_ENTER(_("sl_fclose"));
-
-  fd = fileno(fp);
-
-  if (fd >= 0 && fd < MAXFD && ofiles[fd] != NULL) /* stale ofiles[fd] handle */
-    {
-      sl_strlcpy(badfd_orig_file, file, sizeof(badfd_orig_file));
-      badfd_orig_line = line;
-    }
-
-  ret = fclose(fp); /* within sl_fclose wrapper */
-
-  SL_IRETURN(ret, _("sl_fclose")); 
 }
 
@@ -2503,9 +1993,7 @@
       if (ofiles[fd] != NULL && fd != except)
 	{
-	  if (ofiles[fd]->content)
-	    sh_string_destroy(&(ofiles[fd]->content));
 	  if (ofiles[fd]->path != NULL)
-	    (void) free (ofiles[fd]->path);
-	  (void) free (ofiles[fd]);
+	    (void) free(ofiles[fd]->path);
+	  (void) free(ofiles[fd]);
 	  ofiles[fd] = NULL;
 	}
@@ -2515,17 +2003,4 @@
 }
 
-int sl_dropall_dirty(int fd, int except)
-{
-  while (fd < MAXFD)
-    {
-      if (ofiles[fd] != NULL && fd != except)
-	{
-	  ofiles[fd] = NULL;
-	}
-      ++fd;
-    }
-  return 0;
-}
-
 
 int sl_unlink (SL_TICKET ticket) 
@@ -2606,36 +2081,14 @@
 }
 
-int sl_read_timeout_prep (SL_TICKET ticket)
-{
-  int fd;
-  int sflags;
-
-  SL_ENTER(_("sl_read_timeout_prep"));
-
-  if (SL_ISERROR(fd = get_the_fd(ticket)))
-    {
-      TPT(( 0, FIL__, __LINE__, _("msg=<ticket error> errno=<%d>"), fd));
-      SL_IRETURN(fd, _("sl_read_timeout_prep"));
-    }
-
-  /* set to non-blocking mode 
-   */
-  sflags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
-  retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags | O_NONBLOCK);
-
-  SL_IRETURN(SL_ENONE, _("sl_read_timeout_prep"));
-}
-
-
-static int sl_read_timeout_fd_int (int fd, void * buf_in, size_t count, 
-				   int timeout, int is_nonblocking, int once)
-{
-  int sflags = 0;
+
+int sl_read_timeout (SL_TICKET ticket, void * buf_in, size_t count, 
+		     int timeout)
+{
   fd_set readfds;
   struct timeval tv;
-  /* int sflags; */
+  int sflags;
   int retval;
-  int error;
-
+
+  int    fd;
   int    byteread = 0;
   int    bytes    = 0;
@@ -2647,13 +2100,27 @@
   extern volatile int sig_termfast;
  
-  if (is_nonblocking == S_FALSE)
-    {
-      /* set to non-blocking mode 
-       */
-      sflags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
-      retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags | O_NONBLOCK);
+  if (count < 1)
+    {
+      TPT(( 0, FIL__, __LINE__, _("msg=<range error>")));
+      return(SL_ERANGE);
+    }
+  if (buf_in == NULL)
+    {
+      TPT(( 0, FIL__, __LINE__, _("msg=<null buffer>")));
+      return (SL_ENULL);
+    }
+
+  if (SL_ISERROR(fd = get_the_fd(ticket)))
+    {
+      TPT(( 0, FIL__, __LINE__, _("msg=<ticket error> errno=<%d>"), fd));
+      return (fd);
     }
 
   buf = (char *) buf_in;
+
+  /* set to non-blocking mode 
+   */
+  sflags = retry_fcntl(FIL__, __LINE__, fd, F_GETFL, 0);
+  retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags | O_NONBLOCK);
 
   tstart = time(NULL);
@@ -2662,6 +2129,26 @@
   while (count > 0)
     {
+
+      if (sig_termfast == 1) 
+	{
+	  retry_fcntl(FIL__, __LINE__, fd, F_SETFL, 
+		      sflags & ~O_NONBLOCK);
+	  return (SL_EREAD);
+	}
+	  
       FD_ZERO(&readfds);
       FD_SET(fd, &readfds);
+
+      if (tdiff >= timeout)
+	{
+	  retry_fcntl(FIL__, __LINE__, fd, F_SETFL, 
+		      sflags & ~O_NONBLOCK);
+	  return (SL_TIMEOUT);
+	}
+
+      /*
+      tnow  = time(NULL);
+      tdiff = tnow - tstart;
+      */
 
       tv.tv_sec  = timeout - tdiff;
@@ -2673,15 +2160,13 @@
 	{
 	  byteread = read (fd, buf, count);
-
 	  if (byteread > 0) 
 	    {
 	      bytes += byteread; count -= byteread;
 	      buf += byteread;
-	      if (count == 0 || once == S_TRUE)
+	      if (count == 0)
 		break;
 	    }  
 	  else if (byteread == 0)
 	    {
-	      /* zero indicates end of file */
 	      break;
 	    }
@@ -2697,9 +2182,6 @@
 	      else
 		{
-		  error = errno;
-		  if (is_nonblocking == S_FALSE)
-		      retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
-		  TPT(( 0, FIL__, __LINE__, _("msg=<read error>")));
-		  errno = error;
+		  retry_fcntl(FIL__, __LINE__, fd, F_SETFL, 
+			      sflags & ~O_NONBLOCK);
 		  return (SL_EREAD);
 		}
@@ -2715,86 +2197,20 @@
       else if (retval == 0)
 	{
-	  if (is_nonblocking == S_FALSE)
-	      retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
-	  TPT(( 0, FIL__, __LINE__, _("msg=<timeout>")));
-	  errno = 0;
-	  if (bytes > 0)
-	    return ((int) bytes); 
+	  retry_fcntl(FIL__, __LINE__, fd, F_SETFL, 
+		      sflags & ~O_NONBLOCK);
 	  return (SL_TIMEOUT);
 	}
       else
 	{
-	  error = errno;
-	  if (is_nonblocking == S_FALSE)
-	      retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
-	  TPT(( 0, FIL__, __LINE__, _("msg=<timeout>")));
-	  errno = error;
+	  retry_fcntl(FIL__, __LINE__, fd, F_SETFL, 
+		      sflags & ~O_NONBLOCK);
 	  return (SL_EREAD);
 	}
-
-      if (sig_termfast == 1) 
-	{
-	  if (is_nonblocking == S_FALSE)
-	      retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
-	  TPT(( 0, FIL__, __LINE__, _("msg=<terminated>")));
-	  errno = 0;
-	  return (SL_EREAD);
-	}
-	  
       tnow  = time(NULL);
       tdiff = tnow - tstart;
-
-      if (tdiff > timeout)
-	{
-	  if (is_nonblocking == S_FALSE)
-	      retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
-	  TPT(( 0, FIL__, __LINE__, _("msg=<timeout>")));
-	  errno = 0;
-	  if (bytes > 0)
-	    return ((int) bytes);
-	  return (SL_TIMEOUT);
-	}
-    }
-
-  if (is_nonblocking == S_FALSE)
-    retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags);
+    }
+
+  retry_fcntl(FIL__, __LINE__, fd, F_SETFL, sflags & ~O_NONBLOCK);
   return ((int) bytes);
-}
-
-int sl_read_timeout_fd (int fd, void * buf_in, size_t count, 
-			int timeout, int is_nonblocking)
-{
-  return sl_read_timeout_fd_int (fd, buf_in, count, timeout, is_nonblocking, S_FALSE);
-}
-
-int sl_read_timeout_fd_once (int fd, void * buf_in, size_t count, 
-			int timeout, int is_nonblocking)
-{
-  return sl_read_timeout_fd_int (fd, buf_in, count, timeout, is_nonblocking, S_TRUE);
-}
-
-int sl_read_timeout (SL_TICKET ticket, void * buf_in, size_t count, 
-		     int timeout, int is_nonblocking)
-{
-  int    fd, retval;
- 
-  SL_ENTER(_("sl_read_timeout"));
-
-  if (buf_in == NULL || SL_ISERROR(fd = get_the_fd(ticket)))
-    {
-      if (buf_in == NULL)
-	{
-	  TPT(( 0, FIL__, __LINE__, _("msg=<null buffer>")));
-	  SL_IRETURN((SL_ENULL), _("sl_read_timeout"));
-	}
-      if (SL_ISERROR(fd = get_the_fd(ticket)))
-	{
-	  TPT(( 0, FIL__, __LINE__, _("msg=<ticket error> errno=<%d>"), fd));
-	  SL_IRETURN((fd),  _("sl_read_timeout"));
-	}
-    }
-
-  retval = sl_read_timeout_fd (fd, buf_in, count, timeout, is_nonblocking);
-  SL_IRETURN((retval), _("sl_read_timeout"));
 }
 
@@ -2808,15 +2224,13 @@
   char * buf;
 
-  SL_ENTER(_("sl_read"));
-
   if (count < 1)
     {
       TPT(( 0, FIL__, __LINE__, _("msg=<range error>")));
-      SL_IRETURN((SL_ERANGE), _("sl_read"));
+      return(SL_ERANGE);
     }
   if (buf_in == NULL)
     {
       TPT(( 0, FIL__, __LINE__, _("msg=<null buffer>")));
-      SL_IRETURN((SL_ENULL), _("sl_read"));
+      return (SL_ENULL);
     }
 
@@ -2824,5 +2238,5 @@
     {
       TPT(( 0, FIL__, __LINE__, _("msg=<ticket error> errno=<%d>"), fd));
-      SL_IRETURN((fd), _("sl_read"));
+      return (fd);
     }
 
@@ -2838,5 +2252,5 @@
 	}  
     } while ( byteread > 0 || 
-	      ( byteread == -1 && (errno == EINTR || errno == EAGAIN)) 
+	      ( byteread < 0 && (errno == EINTR || errno == EAGAIN)) 
 	      );
 
@@ -2845,7 +2259,7 @@
     {
       TPT(( 0, FIL__, __LINE__, _("msg=<read error> errno=<%d>\n"), errno));
-      SL_IRETURN((SL_EREAD), _("sl_read"));
-    }
-  SL_IRETURN((bytes), _("sl_read"));
+      return (SL_EREAD);
+    }
+  return (bytes);
 }
 
@@ -2857,15 +2271,13 @@
   char * buf;
 
-  SL_ENTER(_("sl_read_fast"));
-
   if (count < 1)
     {
       TPT(( 0, FIL__, __LINE__, _("msg=<range error>")));
-      SL_IRETURN((SL_ERANGE), _("sl_read_fast"));
+      return(SL_ERANGE);
     }
   if (buf_in == NULL)
     {
       TPT(( 0, FIL__, __LINE__, _("msg=<null buffer>")));
-      SL_IRETURN((SL_ENULL), _("sl_read_fast"));
+      return (SL_ENULL);
     }
 
@@ -2873,5 +2285,5 @@
     {
       TPT(( 0, FIL__, __LINE__, _("msg=<ticket error> errno=<%d>"), fd));
-      SL_IRETURN((fd), _("sl_read_fast"));
+      return (fd);
     }
 
@@ -2883,7 +2295,7 @@
       if (byteread >= 0) 
 	{
-	  SL_IRETURN((byteread), _("sl_read_fast"));
+	  return (byteread);
 	}  
-    } while ( byteread == -1 && (errno == EINTR || errno == EAGAIN));
+    } while ( byteread < 0 && (errno == EINTR || errno == EAGAIN));
 
  
@@ -2891,11 +2303,11 @@
     {
       TPT(( 0, FIL__, __LINE__, _("msg=<read error> errno=<%d>\n"), errno));
-      SL_IRETURN((SL_EREAD), _("sl_read_fast"));
-    }
-  SL_IRETURN((0), _("sl_read_fast"));
-}
-
-
-int sl_write (SL_TICKET ticket, const void * msg_in, long nbytes)
+      return (SL_EREAD);
+    }
+  return (0);
+}
+
+
+int sl_write (SL_TICKET ticket, void * msg_in, long nbytes)
 {
   long bytewritten;
@@ -2903,5 +2315,5 @@
   int  fd;
 
-  const char * msg; 
+  char * msg; 
 
   SL_ENTER(_("sl_write"));
@@ -2914,15 +2326,13 @@
     SL_IRETURN(fd, _("sl_write"));
 
-  msg = (const char *) msg_in;
+  msg = (char *) msg_in;
 
   /* write
    */
   bytecount    = 0;
-
+  bytewritten  = 0;
   while (bytecount < nbytes) 
     {    
-      bytewritten = write (fd, msg, nbytes-bytecount);
-
-      if (bytewritten > 0) 
+      if ((bytewritten = write (fd, msg, nbytes-bytecount)) > 0) 
 	{
 	  bytecount += bytewritten;
@@ -2940,5 +2350,5 @@
 }
 
-int sl_write_line (SL_TICKET ticket, const void * msg, long nbytes)
+int sl_write_line (SL_TICKET ticket, void * msg, long nbytes)
 {
   int  status;
@@ -2951,21 +2361,4 @@
 
   SL_IRETURN(status, _("sl_write_line"));
-}
-
-int sl_write_line_fast (SL_TICKET ticket, void * msg, long nbytes)
-{
-  int  status;
-  char * p = (char *) msg;
-
-  SL_ENTER(_("sl_write_line_fast"));
-
-  /* Here nbytes is strlen(msg), so p[nbytes] is the terminating '\0'
-   * Overwrite the terminator, write out, then write back the terminator.
-   */
-  p[nbytes] = '\n';
-  status = sl_write(ticket,  msg, nbytes+1);
-  p[nbytes] = '\0';
-
-  SL_IRETURN(status, _("sl_write_line_fast"));
 }
 
@@ -2978,93 +2371,65 @@
 
 extern uid_t rootonly[];
-extern unsigned int   EUIDSLOT;
-extern unsigned int   ORIG_EUIDSLOT;
+extern int   EUIDSLOT;
+extern int   ORIG_EUIDSLOT;
 
 extern char  tf_path[MAXFILENAME];	/* Error path for trust function. */
 extern uid_t tf_euid;	                /* Space for EUID of process.     */
 
+
 char * sl_error_string(int errorcode)
 {
-
   switch (errorcode)
     {
     case SL_EBOGUS: 
-      return _("Bogus file, modified during access");
+      return _("Bogus file. Modified during access.");
     case SL_EWRITE: 
-      return _("Write error");
+      return _("Write error.");
     case SL_EREAD: 
-      return _("Read error");
+      return _("Read error.");
     case SL_ESYNC: 
-      return _("Error in fsync()");
+      return _("Error in fsync().");
     case SL_EFORWARD: 
-      return _("Error in lseek()");
+      return _("Error in lseek().");
     case SL_EREWIND: 
-      return _("Error in lseek()");
+      return _("Error in lseek().");
     case SL_EUNLINK: 
-      return _("Error in unlink()");
+      return _("Error in unlink().");
     case SL_EMEM: 
-      return _("Out of memory");
+      return _("Out of memory.");
     case SL_EINTERNAL: 
-      return _("Internal error");
-    case SL_EINTERNAL01: 
-      return _("Internal error 01");
-    case SL_EINTERNAL02: 
-      return _("Internal error 02");
-    case SL_EINTERNAL03: 
-      return _("Internal error 03");
-    case SL_EINTERNAL04: 
-      return _("Internal error 04");
-    case SL_EINTERNAL05: 
-      return _("Internal error 05");
-    case SL_EINTERNAL06: 
-      return _("Internal error 06");
-    case SL_EINTERNAL07: 
-      return _("Internal error 07");
-    case SL_EINTERNAL08: 
-      return _("Internal error 08");
-    case SL_EINTERNAL09: 
-      return _("Internal error 09");
-    case SL_EINTERNAL10: 
-      return _("Internal error 10");
-    case SL_EINTERNAL11: 
-      return _("Internal error 11");
-    case SL_EINTERNAL12: 
-      return _("Internal error 12");
+      return _("Internal error.");
     case SL_ETICKET:
-      return _("Bad ticket");
+      return _("Bad ticket.");
     case SL_EREPEAT: 
-      return _("Illegal repeated use of function");
+      return _("Illegal repeated use of function.");
     case SL_ERANGE: 
-      return _("Argument out of range");
+      return _("Argument out of range.");
     case SL_ENULL: 
-      return _("Dereferenced NULL pointer");
+      return _("Dereferenced NULL pointer.");
 
     case SL_EBADUID: 
-      return _("Owner not trustworthy");
+      return _("Owner not trustworthy.");
     case SL_EBADGID:
-      return _("Group writeable and member not trustworthy");
+      return _("Group writeable and member not trustworthy.");
     case SL_EBADOTH:
-      return _("World writeable");
-    case SL_EISDIR:
-      return _("Is a directory");
+      return _("World writeable.");
     case SL_EBADFILE:
-      return _("File access error");
+      return _("File access error.");
     case SL_EBADNAME:
-      return _("Invalid filename (prob. too long or null)");
+      return _("Invalid filename (prob. too long or null).");
 
     case SL_ETRUNC:
-      return _("Truncation occured");
+      return _("Truncation occured.");
     case SL_ESTAT:
-      return _("stat() failed");
-    case SL_EFSTAT:
-      return _("fstat() failed");
+      return _("stat() failed.");
     default:
-      return _("Unknown error");
-    }
-}
-
-
-
-char * sl_trust_errfile(void)
+      return _("Unknown error.");
+    }
+}
+
+
+
+char * sl_trust_errfile()
 {
   return &tf_path[0];
@@ -3072,5 +2437,5 @@
 
 extern uid_t tf_baduid;
-uid_t   sl_trust_baduid(void)
+uid_t   sl_trust_baduid()
 {
   return tf_baduid;
@@ -3078,5 +2443,5 @@
 
 extern gid_t tf_badgid;
-gid_t   sl_trust_badgid(void)
+gid_t   sl_trust_badgid()
 {
   return tf_badgid;
@@ -3086,7 +2451,7 @@
 static int trust_count = 0;
 
-int  sl_trust_purge_user (void)
-{
-  unsigned int i;
+int  sl_trust_purge_user ()
+{
+  int i;
 
   EUIDSLOT = ORIG_EUIDSLOT;
@@ -3112,59 +2477,7 @@
 }
 
-#include "sh_mem.h"
-extern char * sh_util_strdup (const char * str);
-
-struct sl_trustfile_store {
-  char * filename;
-  uid_t  teuid;
-  struct sl_trustfile_store * next;
-};
-
-static struct sl_trustfile_store * sl_trusted_files = NULL;
-
-static void sl_add_trusted_file(const char * filename, uid_t teuid)
-{
-  struct sl_trustfile_store *new = SH_ALLOC(sizeof(struct sl_trustfile_store));
-
-  new->filename = sh_util_strdup (filename);
-  new->teuid    = teuid;
-  new->next     = sl_trusted_files;
-
-  sl_trusted_files = new;
-  return;
-}
-
-static const char * sl_check_trusted_file(const char * filename, uid_t teuid)
-{
-  struct sl_trustfile_store *new = sl_trusted_files;
-
-  while (new)
-    {
-      if ((new->teuid == teuid) && (0 == strcmp(new->filename, filename)))
-	return filename;
-      new = new->next;
-    }
-
-  return NULL;
-}
-
-static void sl_clear_trusted_file(struct sl_trustfile_store * file)
-{
-  if (file)
-    {
-      if (file->next != NULL)
-	sl_clear_trusted_file(file->next);
-      SH_FREE(file->filename);
-      SH_FREE(file);
-    }
-  return;
-}
-
-int sl_trustfile_euid(const char * filename, uid_t teuid)
-{
-  long          status;
-  static size_t old = 0;
-  static size_t now;
-
+int sl_trustfile_euid(char * filename, uid_t teuid)
+{
+  long status;
   SL_ENTER(_("sl_trustfile_euid"));
 
@@ -3173,103 +2486,8 @@
     SL_IRETURN(SL_EBADNAME, _("sl_trustfile_euid"));
 
-  now = time(NULL);
-
-  if (now < (old + 300))
-    {
-      if (NULL != sl_check_trusted_file(filename, teuid))
-	{
-	  sl_strlcpy(tf_path, filename, sizeof(tf_path));
-	  SL_IRETURN(SL_ENONE, _("sl_trustfile_euid"));
-	}
-    }
-  else
-    {
-      sl_clear_trusted_file(sl_trusted_files);
-      sl_trusted_files = NULL;
-      old = now;
-    }
-
   tf_euid = teuid;
   status = sl_trustfile(filename, NULL, NULL);
-  if (status == SL_ENONE)
-    sl_add_trusted_file(filename, teuid);
   SL_IRETURN(status, _("sl_trustfile_euid"));
 }
 
-/* ---------------------------------------------------------------- 
- *
- *    Overflow tests
- *
- * ---------------------------------------------------------------- */
-
-#ifndef SIZE_MAX
-#define SIZE_MAX              (4294967295U)
-#endif
-
-int sl_ok_muli (int a, int b) /* a*b */
-{
-  if ((b == 0) || (a >= (INT_MIN / b) && a <= (INT_MAX / b)))
-    return S_TRUE; /* no overflow */
-  return S_FALSE;
-}
-
-int sl_ok_muls (size_t a, size_t b) /* a*b */
-{
-  if ((b == 0) || (a <= (SIZE_MAX / b)))
-    return S_TRUE; /* no overflow */
-  return S_FALSE;
-}
-
-int sl_ok_divi (int a, int b) /* a/b */
-{
-  (void) a;
-  if (b != 0)
-    return S_TRUE; /* no overflow */
-  return S_FALSE;
-}
-
-int sl_ok_addi (int a, int b) /* a+b */
-{
-  if (a >= 0 && b >= 0)
-    {
-      if (a <= (INT_MAX - b))
-	return S_TRUE; /* no overflow */
-      else
-	return S_FALSE;
-    }
-  else if (a < 0 && b < 0)
-    {
-      if (a >= (INT_MIN - b))
-	return S_TRUE; /* no overflow */
-      else
-	return S_FALSE;
-    }
-  return S_TRUE;
-}
-
-int sl_ok_adds (size_t a, size_t b) /* a+b */
-{
-  if (a <= (SIZE_MAX - b))
-    return S_TRUE; /* no overflow */
-  else
-    return S_FALSE;
-}
-
-int sl_ok_subi (int a, int b) /* a-b */
-{
-  if (a >= 0 && b < 0)
-    {
-      if (a <= (INT_MAX + b))
-	return S_TRUE; /* no overflow */
-      else
-	return S_FALSE;
-    }
-  else if (a < 0 && b >= 0)
-    {
-      if (a >= (INT_MIN + b))
-	return S_TRUE; /* no overflow */
-      else
-	return S_FALSE;
-    }
-  return S_TRUE;
-}
+
Index: trunk/src/sstrip.c
===================================================================
--- trunk/src/sstrip.c	(revision 1)
+++ trunk/src/sstrip.c	(revision 1)
@@ -0,0 +1,331 @@
+/* sstrip, version 2.0: Copyright (C) 1999-2001 by Brian Raiter, under the
+ * GNU General Public License. No warranty. See LICENSE for details.
+ */
+
+/* #include "config_xor.h" */ 
+
+#include	<stdio.h>
+#include	<stdlib.h>
+#include	<string.h>
+#include	<errno.h>
+#include	<unistd.h>
+#include	<fcntl.h>
+
+#if !defined(__ia64)  && !defined(__ia64__)  && !defined(__itanium__) &&  \
+    !defined(__alpha) && !defined(__alpha__) && \
+    (defined(__linux__) || defined(__FreeBSD__)) && \
+    (defined(__i386__)  || defined(__i386) || defined(i386))
+
+/* || defined(__sun) || defined(__sun__) || defined(sun) */
+
+#if defined(__linux__)
+#include        <linux/elf.h>
+#else
+#include        <elf.h>
+#endif
+
+#ifndef TRUE
+#define	TRUE		1
+#define	FALSE		0
+#endif
+
+#if ELF_CLASS == ELFCLASS32
+#define Elf_Ehdr        Elf32_Ehdr
+#define Elf_Phdr        Elf32_Phdr
+#else
+#define Elf_Ehdr        Elf64_Ehdr
+#define Elf_Phdr        Elf64_Phdr
+#endif
+
+/* The name of the program.
+ */
+static char const      *progname;
+
+/* The name of the current file.
+ */
+static char const      *filename;
+
+
+/* A simple error-handling function. FALSE is always returned for the
+ * convenience of the caller.
+ */
+static int err(char const *errmsg)
+{
+    fprintf(stderr, "%s: %s: %s\n", progname, filename, errmsg);
+    return FALSE;
+}
+
+/* A macro for I/O errors: The given error message is used only when
+ * errno is not set.
+ */
+#define	ferr(msg)	(err(errno ? strerror(errno) : (msg)))
+
+/* readelfheader() reads the ELF header into our global variable, and
+ * checks to make sure that this is in fact a file that we should be
+ * munging.
+ */
+static int readelfheader(int fd, Elf_Ehdr *ehdr)
+{
+    errno = 0;
+    if (read(fd, ehdr, sizeof *ehdr) != sizeof *ehdr)
+	return ferr("missing or incomplete ELF header.");
+
+    /* Check the ELF signature.
+     */
+    if (!(ehdr->e_ident[EI_MAG0] == ELFMAG0 &&
+	  ehdr->e_ident[EI_MAG1] == ELFMAG1 &&
+	  ehdr->e_ident[EI_MAG2] == ELFMAG2 &&
+	  ehdr->e_ident[EI_MAG3] == ELFMAG3))
+	return err("missing ELF signature.");
+
+    /* Compare the file's class and endianness with the program's.
+     */
+    if (ehdr->e_ident[EI_DATA] != ELF_DATA)
+	return err("ELF file has different endianness.");
+    if (ehdr->e_ident[EI_CLASS] != ELF_CLASS)
+	return err("ELF file has different word size.");
+
+    /* Check the target architecture.
+     */
+    if (ehdr->e_machine != ELF_ARCH)
+	return err("ELF file created for different architecture.");
+
+    /* Verify the sizes of the ELF header and the program segment
+     * header table entries.
+     */
+    if (ehdr->e_ehsize != sizeof(Elf_Ehdr))
+	return err("unrecognized ELF header size.");
+    if (ehdr->e_phentsize != sizeof(Elf_Phdr))
+	return err("unrecognized program segment header size.");
+
+    /* Finally, check the file type.
+     */
+    if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN)
+	return err("not an executable or shared-object library.");
+
+    return TRUE;
+}
+
+/* readphdrtable() loads the program segment header table into memory.
+ */
+static int readphdrtable(int fd, Elf_Ehdr const *ehdr, Elf_Phdr **phdrs)
+{
+    size_t	size;
+
+    if (!ehdr->e_phoff || !ehdr->e_phnum)
+	return err("ELF file has no program header table.");
+
+    size = ehdr->e_phnum * sizeof **phdrs;
+    if (!(*phdrs = malloc(size)))
+	return err("Out of memory!");
+
+    errno = 0;
+    if (read(fd, *phdrs, size) != (ssize_t)size)
+	return ferr("missing or incomplete program segment header table.");
+
+    return TRUE;
+}
+
+/* getmemorysize() determines the offset of the last byte of the file
+ * that is referenced by an entry in the program segment header table.
+ * (Anything in the file after that point is not used when the program
+ * is executing, and thus can be safely discarded.)
+ */
+static int getmemorysize(Elf_Ehdr const *ehdr, Elf_Phdr const *phdrs,
+			 unsigned long *newsize)
+{
+    Elf_Phdr   const   *phdr;
+    unsigned long	size, n;
+    int			i;
+
+    /* Start by setting the size to include the ELF header and the
+     * complete program segment header table.
+     */
+    size = ehdr->e_phoff + ehdr->e_phnum * sizeof *phdrs;
+    if (size < sizeof *ehdr)
+	size = sizeof *ehdr;
+
+    /* Then keep extending the size to include whatever data the
+     * program segment header table references.
+     */
+    for (i = 0, phdr = phdrs ; i < ehdr->e_phnum ; ++i, ++phdr) {
+	if (phdr->p_type != PT_NULL) {
+	    n = phdr->p_offset + phdr->p_filesz;
+	    if (n > size)
+		size = n;
+	}
+    }
+
+    *newsize = size;
+    return TRUE;
+}
+
+/* truncatezeros() examines the bytes at the end of the file's
+ * size-to-be, and reduces the size to exclude any trailing zero
+ * bytes.
+ */
+static int truncatezeros(int fd, unsigned long *newsize)
+{
+    unsigned char	contents[1024];
+    unsigned long	size, n;
+
+    size = *newsize;
+    do {
+	n = sizeof contents;
+	if (n > size)
+	    n = size;
+	if (lseek(fd, size - n, SEEK_SET) == (off_t)-1)
+	    return ferr("cannot seek in file.");
+	if (read(fd, contents, n) != (ssize_t)n)
+	    return ferr("cannot read file contents");
+	while (n && !contents[--n])
+	    --size;
+    } while (size && !n);
+
+    /* Sanity check.
+     */
+    if (!size)
+	return err("ELF file is completely blank!");
+
+    *newsize = size;
+    return TRUE;
+}
+
+/* modifyheaders() removes references to the section header table if
+ * it was stripped, and reduces program header table entries that
+ * included truncated bytes at the end of the file.
+ */
+static int modifyheaders(Elf_Ehdr *ehdr, Elf_Phdr *phdrs,
+			 unsigned long newsize)
+{
+    Elf_Phdr   *phdr;
+    int		i;
+
+    /* If the section header table is gone, then remove all references
+     * to it in the ELF header.
+     */
+    if (ehdr->e_shoff >= newsize) {
+	ehdr->e_shoff = 0;
+	ehdr->e_shnum = 0;
+	ehdr->e_shentsize = 0;
+	ehdr->e_shstrndx = 0;
+    }
+
+    /* The program adjusts the file size of any segment that was
+     * truncated. The case of a segment being completely stripped out
+     * is handled separately.
+     */
+    for (i = 0, phdr = phdrs ; i < ehdr->e_phnum ; ++i, ++phdr) {
+	if (phdr->p_offset >= newsize) {
+	    phdr->p_offset = newsize;
+	    phdr->p_filesz = 0;
+	} else if (phdr->p_offset + phdr->p_filesz > newsize) {
+	    phdr->p_filesz = newsize - phdr->p_offset;
+	}
+    }
+
+    return TRUE;
+}
+
+/* commitchanges() writes the new headers back to the original file
+ * and sets the file to its new size.
+ */
+static int commitchanges(int fd, Elf_Ehdr const *ehdr, Elf_Phdr *phdrs,
+			 unsigned long newsize)
+{
+    size_t	n;
+
+    /* Save the changes to the ELF header, if any.
+     */
+    if (lseek(fd, 0, SEEK_SET))
+	return ferr("could not rewind file");
+    errno = 0;
+    if (write(fd, ehdr, sizeof *ehdr) != sizeof *ehdr)
+	return err("could not modify file");
+
+    /* Save the changes to the program segment header table, if any.
+     */
+    if (lseek(fd, ehdr->e_phoff, SEEK_SET) == (off_t)-1) {
+	err("could not seek in file.");
+	goto warning;
+    }
+    n = ehdr->e_phnum * sizeof *phdrs;
+    if (write(fd, phdrs, n) != (ssize_t)n) {
+	err("could not write to file");
+	goto warning;
+    }
+
+    /* Eleventh-hour sanity check: don't truncate before the end of
+     * the program segment header table.
+     */
+    if (newsize < ehdr->e_phoff + n)
+	newsize = ehdr->e_phoff + n;
+
+    /* Chop off the end of the file.
+     */
+    if (ftruncate(fd, newsize)) {
+	err("could not resize file");
+	goto warning;
+    }
+
+    return TRUE;
+
+  warning:
+    return err("ELF file may have been corrupted!");
+}
+
+/* main() loops over the cmdline arguments, leaving all the real work
+ * to the other functions.
+ */
+int main(int argc, char *argv[])
+{
+    int			fd;
+    Elf_Ehdr		ehdr;
+    Elf_Phdr	       *phdrs = NULL;
+    unsigned long	newsize;
+    char	      **arg;
+    int			failures = 0;
+
+    if (argc < 2 || argv[1][0] == '-') {
+	printf("Usage: sstrip FILE...\n"
+	       "sstrip discards all nonessential bytes from an executable.\n\n"
+	       "Version 2.0 Copyright (C) 2000,2001 Brian Raiter.\n"
+	       "This program is free software, licensed under the GNU\n"
+	       "General Public License. There is absolutely no warranty.\n");
+	return EXIT_SUCCESS;
+    }
+
+    progname = argv[0];
+
+    for (arg = argv + 1 ; *arg != NULL ; ++arg) {
+	filename = *arg;
+
+	fd = open(*arg, O_RDWR);
+	if (fd < 0) {
+	    ferr("can't open");
+	    ++failures;
+	    continue;
+	}
+
+	if (!(readelfheader(fd, &ehdr)			&&
+	      readphdrtable(fd, &ehdr, &phdrs)		&&
+	      getmemorysize(&ehdr, phdrs, &newsize)	&&
+	      truncatezeros(fd, &newsize)		&&
+	      modifyheaders(&ehdr, phdrs, newsize)	&&
+	      commitchanges(fd, &ehdr, phdrs, newsize)))
+	    ++failures;
+
+	close(fd);
+    }
+
+    return failures ? EXIT_FAILURE : EXIT_SUCCESS;
+}
+
+#else
+
+int main()
+{
+  return (EXIT_SUCCESS);
+}
+
+#endif
Index: trunk/src/t-test0.c
===================================================================
--- trunk/src/t-test0.c	(revision 591)
+++ 	(revision )
@@ -1,482 +1,0 @@
-/*
-* Copyright (c) 1996-1999, 2001-2004 Wolfram Gloger
-
-Permission to use, copy, modify, distribute, and sell this software
-and its documentation for any purpose is hereby granted without fee,
-provided that (i) the above copyright notices and this permission
-notice appear in all copies of the software and related documentation,
-and (ii) the name of Wolfram Gloger may not be used in any advertising
-or publicity relating to the software.
-
-THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
-EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
-WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
-
-IN NO EVENT SHALL WOLFRAM GLOGER BE LIABLE FOR ANY SPECIAL,
-INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY
-DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
-WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY
-OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-PERFORMANCE OF THIS SOFTWARE.
-*/
-
-/*
- * $Id: t-test1.c,v 1.2 2004/11/04 14:58:45 wg Exp $
- * by Wolfram Gloger 1996-1999, 2001, 2004
- * A multi-thread test for malloc performance, maintaining one pool of
- * allocated bins per thread.
- */
-/*
-  t-test[12] <n-total> <n-parallel> <n-allocs> <size-max> <bins>
-
-    n-total = total number of threads executed (default 10)
-    n-parallel = number of threads running in parallel (2)
-    n-allocs = number of malloc()'s / free()'s per thread (10000)
-    size-max = max. size requested with malloc() in bytes (10000)
-    bins = number of bins to maintain
-*/
-
-#if defined(HAVE_CONFIG_H)
-#include "config.h"
-#endif
-
-#if (defined __STDC__ && __STDC__) || defined __cplusplus
-# include <stdlib.h>
-#endif
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/time.h>
-#include <sys/resource.h>
-#include <sys/wait.h>
-#include <sys/mman.h>
-
-/*
-#if !USE_MALLOC
-#include <malloc.h>
-#else
-#include "malloc.h"
-#endif
-*/
-
-#ifdef USE_SYSTEM_MALLOC
-extern void *memalign(size_t boundary, size_t size);
-/* #define memalign(a,b)  malloc(b) */
-#else
-extern void *memalign(size_t boundary, size_t size);
-#endif
-
-static int verbose = 1;
-
-/* dummy for the samhain safe_fatal logger 
- */
-void safe_fatal(const char * details, 
-				const char * file, int line)
-{
-  (void) file;
-  (void) line;
-  fputs("assert failed: ", stderr);
-  puts(details);
-  _exit(EXIT_FAILURE);
-}
-
-/* lran2.h
- * by Wolfram Gloger 1996.
- *
- * A small, portable pseudo-random number generator.
- */
-
-#ifndef _LRAN2_H
-#define _LRAN2_H
-
-#define LRAN2_MAX 714025l /* constants for portable */
-#define IA	  1366l	  /* random number generator */
-#define IC	  150889l /* (see e.g. `Numerical Recipes') */
-
-struct lran2_st {
-    long x, y, v[97];
-};
-
-static void
-lran2_init(struct lran2_st* d, long seed)
-{
-    long x;
-    int j;
-
-    x = (IC - seed) % LRAN2_MAX;
-    if(x < 0) x = -x;
-    for(j=0; j<97; j++) {
-	x = (IA*x + IC) % LRAN2_MAX;
-	d->v[j] = x;
-    }
-    d->x = (IA*x + IC) % LRAN2_MAX;
-    d->y = d->x;
-}
-
-#ifdef __GNUC__
-__inline__
-#endif
-static long
-lran2(struct lran2_st* d)
-{
-    int j = (d->y % 97);
-
-    d->y = d->v[j];
-    d->x = (IA*d->x + IC) % LRAN2_MAX;
-    d->v[j] = d->x;
-    return d->y;
-}
-
-#undef IA
-#undef IC
-
-#endif
-
-/*
- * $Id: t-test.h,v 1.1 2004/11/04 14:32:21 wg Exp $
- * by Wolfram Gloger 1996.
- * Common data structures and functions for testing malloc performance.
- */
-
-/* Testing level */
-#ifndef TEST
-#define TEST 99
-#endif
-
-/* For large allocation sizes, the time required by copying in
-   realloc() can dwarf all other execution times.  Avoid this with a
-   size threshold. */
-#ifndef REALLOC_MAX
-#define REALLOC_MAX	2000
-#endif
-
-struct bin {
-	unsigned char *ptr;
-	unsigned long size;
-};
-
-#if TEST > 0
-
-static void
-mem_init(unsigned char *ptr, unsigned long size)
-{
-	unsigned long i, j;
-
-	if(size == 0) return;
-#if TEST > 3
-	memset(ptr, 0, size);
-#endif
-	for(i=0; i<size; i+=2047) {
-		j = (unsigned long)ptr ^ i;
-		ptr[i] = ((j ^ (j>>8)) & 0xFF);
-	}
-	j = (unsigned long)ptr ^ (size-1);
-	ptr[size-1] = ((j ^ (j>>8)) & 0xFF);
-}
-
-static int
-mem_check(unsigned char *ptr, unsigned long size)
-{
-	unsigned long i, j;
-
-	if(size == 0) return 0;
-	for(i=0; i<size; i+=2047) {
-		j = (unsigned long)ptr ^ i;
-		if(ptr[i] != ((j ^ (j>>8)) & 0xFF)) return 1;
-	}
-	j = (unsigned long)ptr ^ (size-1);
-	if(ptr[size-1] != ((j ^ (j>>8)) & 0xFF)) return 2;
-	return 0;
-}
-
-static int
-zero_check(unsigned* ptr, unsigned long size)
-{
-	unsigned char* ptr2;
-
-	while(size >= sizeof(*ptr)) {
-		if(*ptr++ != 0)
-			return -1;
-		size -= sizeof(*ptr);
-	}
-	ptr2 = (unsigned char*)ptr;
-	while(size > 0) {
-		if(*ptr2++ != 0)
-			return -1;
-		--size;
-	}
-	return 0;
-}
-
-#endif /* TEST > 0 */
-
-/* Allocate a bin with malloc(), realloc() or memalign().  r must be a
-   random number >= 1024. */
-int n_malloc=0, n_memalign=0, n_realloc=0, n_calloc=0;
-
-static void
-bin_alloc(struct bin *m, unsigned long size, int r)
-{
-#if TEST > 0
-	if(mem_check(m->ptr, m->size)) {
-	  fprintf(stderr, "memory corrupt!\n");
-	  exit(1);
-	}
-#endif
-	r %= 1024;
-	/*printf("%d ", r);*/
-	if(r < 4) { /* memalign */
-		if(m->size > 0) free(m->ptr);
-		m->ptr = (unsigned char *)memalign(sizeof(int) << r, size);
-		++n_memalign;
-	} else if(r < 20) { /* calloc */
-		if(m->size > 0) free(m->ptr);
-		m->ptr = (unsigned char *)calloc(size, 1);
-#if TEST > 0
-		if(zero_check((unsigned*)m->ptr, size)) {
-			unsigned long i;
-			for(i=0; i<size; i++)
-				if(m->ptr[i] != 0)
-					break;
-			fprintf(stderr, "calloc'ed memory non-zero (ptr=%p, i=%ld)!\n", m->ptr, i);
-			exit(1);
-		}
-#endif
-		++n_calloc;
-	} else if(r < 100 && m->size < REALLOC_MAX) { /* realloc */
-		if(m->size == 0) m->ptr = NULL;
-		m->ptr = realloc(m->ptr, size);
-		++n_realloc;
-	} else { /* plain malloc */
-		if(m->size > 0) free(m->ptr);
-		m->ptr = (unsigned char *)malloc(size);
-		++n_malloc;
-	}
-	if(!m->ptr) {
-	  fprintf(stderr, "out of memory (r=%d, size=%ld)!\n", r, (long)size);
-	  exit(1);
-	}
-	m->size = size;
-#if TEST > 0
-	mem_init(m->ptr, m->size);
-#endif
-}
-
-/* Free a bin. */
-
-static void
-bin_free(struct bin *m)
-{
-	if(m->size == 0) return;
-#if TEST > 0
-	if(mem_check(m->ptr, m->size)) {
-	  fprintf(stderr, "memory corrupt!\n");
-	  exit(1);
-	}
-#endif
-	free(m->ptr);
-	m->size = 0;
-}
-
-/*
- * Local variables:
- * tab-width: 4
- * End:
- */
-
-
-struct user_data {
-	int bins, max;
-	unsigned long size;
-	long seed;
-};
-
-/*
- * $Id: thread-st.h$
- * pthread version
- * by Wolfram Gloger 2004
- */
-
-#include <pthread.h>
-#include <stdio.h>
-
-pthread_cond_t finish_cond = PTHREAD_COND_INITIALIZER;
-pthread_mutex_t finish_mutex = PTHREAD_MUTEX_INITIALIZER;
-
-#ifndef USE_PTHREADS_STACKS
-#define USE_PTHREADS_STACKS 0
-#endif
-
-#ifndef STACKSIZE
-#define STACKSIZE	32768
-#endif
-
-
-
-
-/*
- * Local variables:
- * tab-width: 4
- * End:
- */
-
-
-#define N_TOTAL		10
-#ifndef N_THREADS
-#define N_THREADS	2
-#endif
-#ifndef N_TOTAL_PRINT
-#define N_TOTAL_PRINT 50
-#endif
-#ifndef MEMORY
-#define MEMORY		8000000l
-#endif
-#define SIZE		10000
-#define I_MAX		10000
-#define ACTIONS_MAX	30
-#ifndef TEST_FORK
-#define TEST_FORK 0
-#endif
-
-#define RANDOM(d,s)	(lran2(d) % (s))
-
-struct bin_info {
-	struct bin *m;
-	unsigned long size, bins;
-};
-
-#if TEST > 0
-
-void
-bin_test(struct bin_info *p)
-{
-	unsigned int b;
-
-	for(b=0; b<p->bins; b++) {
-		if(mem_check(p->m[b].ptr, p->m[b].size)) {
-		  fprintf(stderr, "memory corrupt!\n");
-		  abort();
-		}
-	}
-}
-
-#endif
-
-void
-malloc_test(unsigned long size, int bins, int max)
-{
-    unsigned int b;
-	int i, j, actions;
-	struct bin_info p;
-	struct lran2_st ld; /* data for random number generator */
-
-	lran2_init(&ld, ((long)max*size + getpid()) ^ bins);
-
-	p.m = (struct bin *)malloc(bins*sizeof(*p.m));
-	p.bins = bins;
-	p.size = size;
-
-	for(b=0; b<p.bins; b++) {
-		p.m[b].size = 0;
-		p.m[b].ptr = NULL;
-		if(RANDOM(&ld, 2) == 0)
-			bin_alloc(&p.m[b], RANDOM(&ld, p.size) + 1, lran2(&ld));
-	}
-	for(i=0; i<=max;) {
-#if TEST > 1
-		bin_test(&p);
-#endif
-		actions = RANDOM(&ld, ACTIONS_MAX);
-#if USE_MALLOC && MALLOC_DEBUG
-		if(actions < 2) { mallinfo2(); }
-#endif
-		for(j=0; j<actions; j++) {
-			b = RANDOM(&ld, p.bins);
-			bin_free(&p.m[b]);
-		}
-		i += actions;
-		actions = RANDOM(&ld, ACTIONS_MAX);
-		for(j=0; j<actions; j++) {
-			b = RANDOM(&ld, p.bins);
-			bin_alloc(&p.m[b], RANDOM(&ld, p.size) + 1, lran2(&ld));
-#if TEST > 2
-			bin_test(&p);
-#endif
-		}
-
-		i += actions;
-	}
-	for(b=0; b<p.bins; b++)
-		bin_free(&p.m[b]);
-	free(p.m);
-	return;
-}
-
-int n_total=0, n_total_max=N_TOTAL, n_running;
-
-int
-main(int argc, char *argv[])
-{
-	int bins;
-	int n_thr=N_THREADS;
-	int i_max=I_MAX;
-	unsigned long size=SIZE;
-
-#if USE_MALLOC && USE_STARTER==2
-	ptmalloc_init();
-	printf("ptmalloc_init\n");
-#endif
-
-	if((argc > 1) && (0 == strcmp(argv[1], "-h") || 0 == strcmp(argv[1], "--help")))
-	  {
-		printf("%s <n-total> <n-parallel> <n-allocs> <size-max> <bins>\n\n", argv[0]);
-		printf(" n-total = total number of threads executed (default 10)\n");
-		printf(" UNUSED n-parallel = number of threads running in parallel (2)\n");
-		printf(" n-allocs = number of malloc()'s / free()'s per thread (10000)\n");
-		printf(" size-max = max. size requested with malloc() in bytes (10000)\n");
-		printf(" bins = number of bins to maintain\n");
-		return 0;
-	  }
-
-	if(argc > 1) n_total_max = atoi(argv[1]);
-	if(n_total_max < 1) n_thr = 1;
-	if(argc > 2) n_thr = atoi(argv[2]);
-	if(n_thr < 1) n_thr = 1;
-	if(n_thr > 100) n_thr = 100;
-	if(argc > 3) i_max = atoi(argv[3]);
-
-	if(argc > 4) size = atol(argv[4]);
-	if(size < 2) size = 2;
-
-	bins = MEMORY/(size*n_thr);
-	if(argc > 5) bins = atoi(argv[5]);
-	if(bins < 4) bins = 4;
-
-	printf("[total=%d threads=%d] i_max=%d size=%ld bins=%d\n",
-		   n_total_max, n_thr, i_max, size, bins);
-
-	do {
-	  n_total++;
-	  malloc_test(size, bins, i_max);
-	  if (verbose)
-		if(n_total%N_TOTAL_PRINT == 0)
-		  printf("n_total = %8d - malloc %12d / memalign %12d / realloc %12d / calloc %12d\n", 
-				 n_total, 
-				 n_malloc, n_memalign, n_realloc, n_calloc);
-	} while (n_total < n_total_max);
-
-
-#if USE_MALLOC
-	malloc_stats();
-#endif
-	if (verbose)
-	  printf("Done.\n");
-	return 0;
-}
-
-/*
- * Local variables:
- * tab-width: 4
- * End:
- */
Index: trunk/src/t-test1.c
===================================================================
--- trunk/src/t-test1.c	(revision 591)
+++ 	(revision )
@@ -1,684 +1,0 @@
-/*
-* Copyright (c) 1996-1999, 2001-2004 Wolfram Gloger
-
-Permission to use, copy, modify, distribute, and sell this software
-and its documentation for any purpose is hereby granted without fee,
-provided that (i) the above copyright notices and this permission
-notice appear in all copies of the software and related documentation,
-and (ii) the name of Wolfram Gloger may not be used in any advertising
-or publicity relating to the software.
-
-THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
-EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
-WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
-
-IN NO EVENT SHALL WOLFRAM GLOGER BE LIABLE FOR ANY SPECIAL,
-INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY
-DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
-WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY
-OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-PERFORMANCE OF THIS SOFTWARE.
-*/
-
-/*
- * $Id: t-test1.c,v 1.2 2004/11/04 14:58:45 wg Exp $
- * by Wolfram Gloger 1996-1999, 2001, 2004
- * A multi-thread test for malloc performance, maintaining one pool of
- * allocated bins per thread.
- */
-/*
-  t-test[12] <n-total> <n-parallel> <n-allocs> <size-max> <bins>
-
-    n-total = total number of threads executed (default 10)
-    n-parallel = number of threads running in parallel (2)
-    n-allocs = number of malloc()'s / free()'s per thread (10000)
-    size-max = max. size requested with malloc() in bytes (10000)
-    bins = number of bins to maintain
-*/
-
-#if defined(HAVE_CONFIG_H)
-#include "config.h"
-#endif
-
-#if (defined __STDC__ && __STDC__) || defined __cplusplus
-# include <stdlib.h>
-#endif
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/time.h>
-#include <sys/resource.h>
-#include <sys/wait.h>
-#include <sys/mman.h>
-
-/*
-#if !USE_MALLOC
-#include <malloc.h>
-#else
-#include "malloc.h"
-#endif
-*/
-
-#ifdef USE_SYSTEM_MALLOC
-extern void *memalign(size_t boundary, size_t size);
-/* #define memalign(a,b)  malloc(b) */
-#else
-extern void *memalign(size_t boundary, size_t size);
-#endif
-
-static int verbose = 1;
-
-/* dummy for the samhain safe_fatal logger 
- */
-void safe_fatal(const char * details, 
-				const char * file, int line)
-{
-  (void) file;
-  (void) line;
-  fputs("assert failed: ", stderr);
-  puts(details);
-  _exit(EXIT_FAILURE);
-}
-
-/* lran2.h
- * by Wolfram Gloger 1996.
- *
- * A small, portable pseudo-random number generator.
- */
-
-#ifndef _LRAN2_H
-#define _LRAN2_H
-
-#define LRAN2_MAX 714025l /* constants for portable */
-#define IA	  1366l	  /* random number generator */
-#define IC	  150889l /* (see e.g. `Numerical Recipes') */
-
-struct lran2_st {
-    long x, y, v[97];
-};
-
-static void
-lran2_init(struct lran2_st* d, long seed)
-{
-    long x;
-    int j;
-
-    x = (IC - seed) % LRAN2_MAX;
-    if(x < 0) x = -x;
-    for(j=0; j<97; j++) {
-	x = (IA*x + IC) % LRAN2_MAX;
-	d->v[j] = x;
-    }
-    d->x = (IA*x + IC) % LRAN2_MAX;
-    d->y = d->x;
-}
-
-#ifdef __GNUC__
-__inline__
-#endif
-static long
-lran2(struct lran2_st* d)
-{
-    int j = (d->y % 97);
-
-    d->y = d->v[j];
-    d->x = (IA*d->x + IC) % LRAN2_MAX;
-    d->v[j] = d->x;
-    return d->y;
-}
-
-#undef IA
-#undef IC
-
-#endif
-
-/*
- * $Id: t-test.h,v 1.1 2004/11/04 14:32:21 wg Exp $
- * by Wolfram Gloger 1996.
- * Common data structures and functions for testing malloc performance.
- */
-
-/* Testing level */
-#ifndef TEST
-#define TEST 99
-#endif
-
-/* For large allocation sizes, the time required by copying in
-   realloc() can dwarf all other execution times.  Avoid this with a
-   size threshold. */
-#ifndef REALLOC_MAX
-#define REALLOC_MAX	2000
-#endif
-
-struct bin {
-	unsigned char *ptr;
-	unsigned long size;
-};
-
-#if TEST > 0
-
-static void
-mem_init(unsigned char *ptr, unsigned long size)
-{
-	unsigned long i, j;
-
-	if(size == 0) return;
-#if TEST > 3
-	memset(ptr, '\0', size);
-#endif
-	for(i=0; i<size; i+=2047) {
-		j = (unsigned long)ptr ^ i;
-		ptr[i] = ((j ^ (j>>8)) & 0xFF);
-	}
-	j = (unsigned long)ptr ^ (size-1);
-	ptr[size-1] = ((j ^ (j>>8)) & 0xFF);
-}
-
-static int
-mem_check(unsigned char *ptr, unsigned long size)
-{
-	unsigned long i, j;
-
-	if(size == 0) return 0;
-	for(i=0; i<size; i+=2047) {
-		j = (unsigned long)ptr ^ i;
-		if(ptr[i] != ((j ^ (j>>8)) & 0xFF)) return 1;
-	}
-	j = (unsigned long)ptr ^ (size-1);
-	if(ptr[size-1] != ((j ^ (j>>8)) & 0xFF)) return 2;
-	return 0;
-}
-
-static int
-zero_check(unsigned* ptr, unsigned long size)
-{
-	unsigned char* ptr2;
-
-	while(size >= sizeof(*ptr)) {
-		if(*ptr++ != 0)
-			return -1;
-		size -= sizeof(*ptr);
-	}
-	ptr2 = (unsigned char*)ptr;
-	while(size > 0) {
-		if(*ptr2++ != 0)
-			return -1;
-		--size;
-	}
-	return 0;
-}
-
-#endif /* TEST > 0 */
-
-/* Allocate a bin with malloc(), realloc() or memalign().  r must be a
-   random number >= 1024. */
-int n_malloc=0, n_memalign=0, n_realloc=0, n_calloc=0;
-
-static void
-bin_alloc(struct bin *m, unsigned long size, int r)
-{
-#if TEST > 0
-	if(mem_check(m->ptr, m->size)) {
-	  fprintf(stderr, "memory corrupt!\n");
-	  exit(1);
-	}
-#endif
-	r %= 1024;
-	/*printf("%d ", r);*/
-	if(r < 4) { /* memalign */
-		if(m->size > 0) free(m->ptr);
-		m->ptr = (unsigned char *)memalign(sizeof(int) << r, size);
-		++n_memalign;
-	} else if(r < 20) { /* calloc */
-		if(m->size > 0) free(m->ptr);
-		m->ptr = (unsigned char *)calloc(size, 1);
-#if TEST > 0
-		if(zero_check((unsigned*)m->ptr, size)) {
-			unsigned long i;
-			for(i=0; i<size; i++)
-				if(m->ptr[i] != 0)
-					break;
-			fprintf(stderr, "calloc'ed memory non-zero (ptr=%p, i=%ld)!\n", m->ptr, i);
-			exit(1);
-		}
-#endif
-		++n_calloc;
-	} else if(r < 100 && m->size < REALLOC_MAX) { /* realloc */
-		if(m->size == 0) m->ptr = NULL;
-		m->ptr = realloc(m->ptr, size);
-		++n_realloc;
-	} else { /* plain malloc */
-		if(m->size > 0) free(m->ptr);
-		m->ptr = (unsigned char *)malloc(size);
-		++n_malloc;
-	}
-	if(!m->ptr) {
-	  fprintf(stderr, "out of memory (r=%d, size=%ld)!\n", r, (long)size);
-	  exit(1);
-	}
-	m->size = size;
-#if TEST > 0
-	mem_init(m->ptr, m->size);
-#endif
-}
-
-/* Free a bin. */
-
-static void
-bin_free(struct bin *m)
-{
-	if(m->size == 0) return;
-#if TEST > 0
-	if(mem_check(m->ptr, m->size)) {
-	  fprintf(stderr, "memory corrupt!\n");
-	  exit(1);
-	}
-#endif
-	free(m->ptr);
-	m->size = 0;
-}
-
-/*
- * Local variables:
- * tab-width: 4
- * End:
- */
-
-
-struct user_data {
-	int bins, max;
-	unsigned long size;
-	long seed;
-};
-
-/*
- * $Id: thread-st.h$
- * pthread version
- * by Wolfram Gloger 2004
- */
-
-#include <pthread.h>
-#include <stdio.h>
-
-pthread_cond_t finish_cond = PTHREAD_COND_INITIALIZER;
-pthread_mutex_t finish_mutex = PTHREAD_MUTEX_INITIALIZER;
-
-#ifndef USE_PTHREADS_STACKS
-#define USE_PTHREADS_STACKS 0
-#endif
-
-#ifndef STACKSIZE
-#define STACKSIZE	32768
-#endif
-
-struct thread_st {
-	char *sp;							/* stack pointer, can be 0 */
-	void (*func)(struct thread_st* st);	/* must be set by user */
-	pthread_t id;
-	int flags;
-	struct user_data u;
-};
-
-static void
-mthread_init(void)
-{
-#if !defined(USE_SYSTEM_MALLOC) && defined(USE_MALLOC_LOCK)
-  extern int dnmalloc_pthread_init(void);
-  dnmalloc_pthread_init();
-#endif
-
-  if (verbose)
-	printf("Using posix threads.\n");
-  pthread_cond_init(&finish_cond, NULL);
-  pthread_mutex_init(&finish_mutex, NULL);
-}
-
-static void *
-mthread_wrapper(void *ptr)
-{
-	struct thread_st *st = (struct thread_st*)ptr;
-
-	/*printf("begin %p\n", st->sp);*/
-	st->func(st);
-	pthread_mutex_lock(&finish_mutex);
-	st->flags = 1;
-	pthread_mutex_unlock(&finish_mutex);
-	pthread_cond_signal(&finish_cond);
-	/*printf("end %p\n", st->sp);*/
-	return NULL;
-}
-
-/* Create a thread. */
-static int
-mthread_create(struct thread_st *st)
-{
-	st->flags = 0;
-	{
-		pthread_attr_t* attr_p = 0;
-#if USE_PTHREADS_STACKS
-		pthread_attr_t attr;
-
-		pthread_attr_init (&attr);
-		if(!st->sp)
-			st->sp = malloc(STACKSIZE+16);
-		if(!st->sp)
-			return -1;
-		if(pthread_attr_setstacksize(&attr, STACKSIZE))
-			fprintf(stderr, "error setting stacksize");
-		else
-			pthread_attr_setstackaddr(&attr, st->sp + STACKSIZE);
-		/*printf("create %p\n", st->sp);*/
-		attr_p = &attr;
-#endif
-		return pthread_create(&st->id, attr_p, mthread_wrapper, st);
-	}
-	return 0;
-}
-
-/* Wait for one of several subthreads to finish. */
-static void
-wait_for_thread(struct thread_st st[], int n_thr,
-				int (*end_thr)(struct thread_st*))
-{
-	int i;
-
-	pthread_mutex_lock(&finish_mutex);
-	for(;;) {
-		int term = 0;
-		for(i=0; i<n_thr; i++)
-			if(st[i].flags) {
-				/*printf("joining %p\n", st[i].sp);*/
-				if(pthread_join(st[i].id, NULL) == 0) {
-					st[i].flags = 0;
-					if(end_thr)
-						end_thr(&st[i]);
-				} else
-					fprintf(stderr, "can't join\n");
-				++term;
-			}
-		if(term > 0)
-			break;
-		pthread_cond_wait(&finish_cond, &finish_mutex);
-	}
-	pthread_mutex_unlock(&finish_mutex);
-}
-
-/*
- * Local variables:
- * tab-width: 4
- * End:
- */
-
-
-#define N_TOTAL		10
-#ifndef N_THREADS
-#define N_THREADS	2
-#endif
-#ifndef N_TOTAL_PRINT
-#define N_TOTAL_PRINT 50
-#endif
-#ifndef MEMORY
-#define MEMORY		8000000l
-#endif
-#define SIZE		10000
-#define I_MAX		10000
-#define ACTIONS_MAX	30
-#ifndef TEST_FORK
-#define TEST_FORK 0
-#endif
-
-#define RANDOM(d,s)	(lran2(d) % (s))
-
-struct bin_info {
-	struct bin *m;
-	unsigned long size, bins;
-};
-
-#if TEST > 0
-
-void
-bin_test(struct bin_info *p)
-{
-	unsigned int b;
-
-	for(b=0; b<p->bins; b++) {
-		if(mem_check(p->m[b].ptr, p->m[b].size)) {
-		  fprintf(stderr, "memory corrupt!\n");
-		  abort();
-		}
-	}
-}
-
-#endif
-
-void
-malloc_test(struct thread_st *st)
-{
-    unsigned int b;
-	int i, j, actions, pid = 1;
-	struct bin_info p;
-	struct lran2_st ld; /* data for random number generator */
-
-	lran2_init(&ld, st->u.seed);
-#if TEST_FORK>0
-	if(RANDOM(&ld, TEST_FORK) == 0) {
-		int status;
-
-#if !USE_THR
-		pid = fork();
-#else
-		pid = fork1();
-#endif
-		if(pid > 0) {
-		    /*printf("forked, waiting for %d...\n", pid);*/
-			waitpid(pid, &status, 0);
-			printf("done with %d...\n", pid);
-			if(!WIFEXITED(status)) {
-				printf("child term with signal %d\n", WTERMSIG(status));
-				exit(1);
-			}
-			return;
-		}
-		exit(0);
-	}
-#endif
-	p.m = (struct bin *)malloc(st->u.bins*sizeof(*p.m));
-	p.bins = st->u.bins;
-	p.size = st->u.size;
-	for(b=0; b<p.bins; b++) {
-		p.m[b].size = 0;
-		p.m[b].ptr = NULL;
-		if(RANDOM(&ld, 2) == 0)
-			bin_alloc(&p.m[b], RANDOM(&ld, p.size) + 1, lran2(&ld));
-	}
-	for(i=0; i<=st->u.max;) {
-#if TEST > 1
-		bin_test(&p);
-#endif
-		actions = RANDOM(&ld, ACTIONS_MAX);
-#if USE_MALLOC && MALLOC_DEBUG
-		if(actions < 2) { mallinfo2(); }
-#endif
-		for(j=0; j<actions; j++) {
-			b = RANDOM(&ld, p.bins);
-			bin_free(&p.m[b]);
-		}
-		i += actions;
-		actions = RANDOM(&ld, ACTIONS_MAX);
-		for(j=0; j<actions; j++) {
-			b = RANDOM(&ld, p.bins);
-			bin_alloc(&p.m[b], RANDOM(&ld, p.size) + 1, lran2(&ld));
-#if TEST > 2
-			bin_test(&p);
-#endif
-		}
-#if 0 /* Test illegal free()s while setting MALLOC_CHECK_ */
-		for(j=0; j<8; j++) {
-			b = RANDOM(&ld, p.bins);
-			if(p.m[b].ptr) {
-			  int offset = (RANDOM(&ld, 11) - 5)*8;
-			  char *rogue = (char*)(p.m[b].ptr) + offset;
-			  /*printf("p=%p rogue=%p\n", p.m[b].ptr, rogue);*/
-			  free(rogue);
-			}
-		}
-#endif
-		i += actions;
-	}
-	for(b=0; b<p.bins; b++)
-		bin_free(&p.m[b]);
-	free(p.m);
-	if(pid == 0)
-		exit(0);
-}
-
-int n_total=0, n_total_max=N_TOTAL, n_running;
-
-int
-my_end_thread(struct thread_st *st)
-{
-	/* Thread st has finished.  Start a new one. */
-#if 0
-	printf("Thread %lx terminated.\n", (long)st->id);
-#endif
-	if(n_total >= n_total_max) {
-		n_running--;
-	} else if(st->u.seed++, mthread_create(st)) {
-		printf("Creating thread #%d failed.\n", n_total);
-		exit(1);
-	} else {
-		n_total++;
-		if (verbose)
-		  if(n_total%N_TOTAL_PRINT == 0)
-			printf("n_total = %8d - malloc %12d / memalign %12d / realloc %12d / calloc %12d\n", 
-				   n_total, 
-				   n_malloc, n_memalign, n_realloc, n_calloc);
-		
-	}
-	return 0;
-}
-
-#if 0
-/* Protect address space for allocation of n threads by LinuxThreads.  */
-static void
-protect_stack(int n)
-{
-	char buf[2048*1024];
-	char* guard;
-	size_t guard_size = 2*2048*1024UL*(n+2);
-
-	buf[0] = '\0';
-	guard = (char*)(((unsigned long)buf - 4096)& ~4095UL) - guard_size;
-	printf("Setting up stack guard at %p\n", guard);
-	if(mmap(guard, guard_size, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED,
-			-1, 0)
-	   != guard)
-		printf("failed!\n");
-}
-#endif
-
-int
-main(int argc, char *argv[])
-{
-	int i, bins;
-	int n_thr=N_THREADS;
-	int i_max=I_MAX;
-	unsigned long size=SIZE;
-	struct thread_st *st;
-
-#if USE_MALLOC && USE_STARTER==2
-	ptmalloc_init();
-	printf("ptmalloc_init\n");
-#endif
-
-	if((argc > 1) && (0 == strcmp(argv[1], "-h") || 0 == strcmp(argv[1], "--help")))
-	  {
-		printf("%s <n-total> <n-parallel> <n-allocs> <size-max> <bins>\n\n", argv[0]);
-		printf(" n-total = total number of threads executed (default 10)\n");
-		printf(" n-parallel = number of threads running in parallel (2)\n");
-		printf(" n-allocs = number of malloc()'s / free()'s per thread (10000)\n");
-		printf(" size-max = max. size requested with malloc() in bytes (10000)\n");
-		printf(" bins = number of bins to maintain\n");
-		return 0;
-	  }
-
-	if(argc > 1) n_total_max = atoi(argv[1]);
-	if(n_total_max < 1) n_thr = 1;
-	if(argc > 2) n_thr = atoi(argv[2]);
-	if(n_thr < 1) n_thr = 1;
-	if(n_thr > 100) n_thr = 100;
-	if(argc > 3) i_max = atoi(argv[3]);
-
-	if(argc > 4) size = atol(argv[4]);
-	if(size < 2) size = 2;
-
-	bins = MEMORY/(size*n_thr);
-	if(argc > 5) bins = atoi(argv[5]);
-	if(bins < 4) bins = 4;
-
-	/*protect_stack(n_thr);*/
-
-	mthread_init();
-	printf("total=%d threads=%d i_max=%d size=%ld bins=%d\n",
-		   n_total_max, n_thr, i_max, size, bins);
-
-	st = (struct thread_st *)malloc(n_thr*sizeof(*st));
-	if(!st) exit(-1);
-
-#if !defined NO_THREADS && (defined __sun__ || defined sun)
-	/* I know of no other way to achieve proper concurrency with Solaris. */
-	thr_setconcurrency(n_thr);
-#endif
-
-	/* Start all n_thr threads. */
-	for(i=0; i<n_thr; i++) {
-		st[i].u.bins = bins;
-		st[i].u.max = i_max;
-		st[i].u.size = size;
-		st[i].u.seed = ((long)i_max*size + i) ^ bins;
-		st[i].sp = 0;
-		st[i].func = malloc_test;
-		if(mthread_create(&st[i])) {
-		  fprintf(stderr, "Creating thread #%d failed.\n", i);
-		  n_thr = i;
-		  exit(1);
-		}
-		if (verbose)
-		  printf("Created thread %lx.\n", (long)st[i].id);
-	}
-
-	/* Start an extra thread so we don't run out of stacks. */
-	if(0) {
-		struct thread_st lst;
-		lst.u.bins = 10; lst.u.max = 20; lst.u.size = 8000; lst.u.seed = 8999;
-		lst.sp = 0;
-		lst.func = malloc_test;
-		if(mthread_create(&lst)) {
-		  fprintf(stderr, "Creating thread #%d failed.\n", i);
-		  exit(1);
-		} else {
-			wait_for_thread(&lst, 1, NULL);
-		}
-	}
-
-	for(n_running=n_total=n_thr; n_running>0;) {
-		wait_for_thread(st, n_thr, my_end_thread);
-	}
-	for(i=0; i<n_thr; i++) {
-		free(st[i].sp);
-	}
-	free(st);
-#if USE_MALLOC
-	malloc_stats();
-#endif
-	if (verbose)
-	  printf("Done.\n");
-	return 0;
-}
-
-/*
- * Local variables:
- * tab-width: 4
- * End:
- */
Index: trunk/src/trustfile.c
===================================================================
--- trunk/src/trustfile.c	(revision 591)
+++ trunk/src/trustfile.c	(revision 1)
@@ -86,7 +86,4 @@
 #else
 #define UID_CAST long
-#define HAVE_GETPWENT
-#define SH_MUTEX_LOCK(a)   ((void)0)
-#define SH_MUTEX_UNLOCK(a) ((void)0)
 #endif
 
@@ -99,28 +96,22 @@
 #include <string.h>
 #include <unistd.h>
-#include <errno.h>
-
-
-#if !defined(TRUST_MAIN)
-
+
+
+#ifndef TRUST_MAIN
 #include "slib.h"
 #define SH_NEED_PWD_GRP 1
 #include "sh_static.h"
-#include "sh_pthread.h"
 
 #else
 
 #define sh_getgrgid   getgrgid
-#define sh_getgrgid_r getgrgid_r
+#define sh_getpwent   getpwent
 #define sh_getpwnam   getpwnam
-#define sh_getpwnam_r getpwnam_r
 #define sh_getpwuid   getpwuid
-#define sh_getpwuid_r getpwuid_r
-#define sh_getpwent   getpwent
 #define sh_endpwent   endpwent
 
 #define TRUST_DEBUG
-#define S_FALSE 0
-#define S_TRUE  1
+#define SL_FALSE 0
+#define SL_TRUE  1
 #define SL_ENTER(string)
 #define SL_IRETURN(a, b)  return a
@@ -136,5 +127,4 @@
 #define SL_EINTERNAL -1028     /* Internal error.                      */
 #define SL_EBADFILE  -1030     /* File access error. Check errno.      */
-#define SL_EMEM      -1032     /* Out of memory.                       */
 #define SL_EBADNAME  -1040     /* Invalid name.                        */
 #define SL_ESTAT     -1041     /* stat of file failed. Check errno.    */
@@ -211,5 +201,5 @@
 #define SL_ALWAYS_TRUSTED  0
 #endif
-uid_t test_rootonly[] = { SL_ALWAYS_TRUSTED };
+static uid_t test_rootonly[] = { SL_ALWAYS_TRUSTED };
 
 #define tf_uid_neg ((uid_t)-1)
@@ -222,6 +212,6 @@
 
 uid_t tf_euid = tf_uid_neg;
-unsigned int EUIDSLOT = sizeof(test_rootonly)/sizeof(uid_t);
-unsigned int ORIG_EUIDSLOT = sizeof(test_rootonly)/sizeof(uid_t);
+int EUIDSLOT = sizeof(test_rootonly)/sizeof(uid_t);
+int ORIG_EUIDSLOT = sizeof(test_rootonly)/sizeof(uid_t);
 
 char  tf_path[MAXFILENAME];		/* error path for trust function */
@@ -324,5 +314,5 @@
 
 /* not static to circumvent stupid gcc 4 bug */ 
-int getfname(const char *fname, char *rbuf, int rsz)
+int getfname(char *fname, char *rbuf, int rsz)
 {
 #ifndef TRUST_MAIN
@@ -367,8 +357,6 @@
 	SL_IRETURN(status, _("getfname"));
 #else
-      strncat(rbuf, "/",   rsz-strlen(rbuf)-1);
-      rbuf[rsz-1] = '\0';
-      strncat(rbuf, fname, rsz-strlen(rbuf)-1);
-      rbuf[rsz-1] = '\0';
+      strncat(rbuf, "/",   rsz-strlen(rbuf));
+      strncat(rbuf, fname, rsz-strlen(rbuf));
 #endif
     }
@@ -381,5 +369,5 @@
   SL_ENTER(_("isin"));
   if (list == NULL)
-    SL_IRETURN(S_FALSE, _("isin"));
+    SL_IRETURN(SL_FALSE, _("isin"));
 
   while(*list != tf_uid_neg && *list != n)
@@ -400,5 +388,5 @@
 	       (UID_CAST) n);
 #endif 
-      SL_IRETURN(S_FALSE, _("isin"));
+      SL_IRETURN(SL_FALSE, _("isin"));
     }
 
@@ -408,5 +396,5 @@
 	   (UID_CAST)n, (UID_CAST)*list);
 #endif 
-  SL_IRETURN(S_TRUE, _("isin"));
+  SL_IRETURN(SL_TRUE, _("isin"));
 }
 
@@ -415,65 +403,36 @@
  */
 /* not static to circumvent stupid gcc 4 bug */ 
-int isingrp(gid_t grp, uid_t *ulist, int * errval)
+int isingrp(gid_t grp, uid_t *ulist)
 {
-  struct passwd *w;	        /* info about group member */
+  register struct passwd *w;	/* info about group member */
   register uid_t *u;		/* points to current ulist member */
   register char **p;		/* points to current group member */
-  struct group *g;	        /* pointer to group information */
-
-  int status;
+  register struct group *g;	/* pointer to group information */
   
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
-  struct group    gr;
-  char          * buffer = NULL;
-  struct passwd   pwd;
-  char          * pbuffer = NULL;
-#endif
-
   SL_ENTER(_("isingrp"));
 
-  *errval = 0;
-
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
-  buffer = calloc(1,SH_GRBUF_SIZE);
-  status = sh_getgrgid_r(grp, &gr, buffer, SH_GRBUF_SIZE, &g);
+  if ((g = sh_getgrgid(grp)) == NULL)
+    {
+      SL_IRETURN(SL_FALSE, _("isingrp") );
+    }
+  /*
+  if(g->gr_mem == NULL || g->gr_mem[0] == NULL )
+    SL_IRETURN(SL_FALSE, _("isingrp") );
+  */
+
+  /* this will return at the first match
+   */
+  for(p = g->gr_mem; *p != NULL; p++)
+    {
+      for(u = ulist; *u != tf_uid_neg; u++)
+	{
+	  /* map user name to UID and compare */
+#ifdef TRUST_MAIN
+	  if ((w = getpwnam(*p)) != NULL && *u == (uid_t)(w->pw_uid) )
+	    SL_IRETURN(SL_TRUE, _("isingrp"));
 #else
-  errno = 0;
-  g = sh_getgrgid(grp);
-  status = errno;
-#endif
-
-  if (g == NULL)
-    {
-      if (status == ERANGE)
-	*errval = status;
-
-      goto end_false;
-    }
-
-  /* this will return at the first match
-   */
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
-  pbuffer = calloc(1,SH_PWBUF_SIZE);
-#endif
-
-  for(p = g->gr_mem; *p != NULL; p++)
-    {
-      for(u = ulist; *u != tf_uid_neg; u++)
-	{
-	  /* map user name to UID and compare */
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-	  sh_getpwnam_r(*p, &pwd, pbuffer, SH_PWBUF_SIZE, &w);
-#else
-	  w = sh_getpwnam(*p);
-#endif
-
-#ifdef TRUST_MAIN
-	  if (w != NULL && *u == (uid_t)(w->pw_uid) )
-	    goto end_true;
-#else
-	  if (w != NULL && *u == (uid_t)(w->pw_uid) )
-	    {
-	      goto end_true;
+	  if ((w = sh_getpwnam(*p)) != NULL && *u == (uid_t)(w->pw_uid) )
+	    {
+	      SL_IRETURN(SL_TRUE, _("isingrp"));
 	    }
 #endif
@@ -485,33 +444,16 @@
   for(u = ulist; *u != tf_uid_neg; u++)
     {
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
-      sh_getpwuid_r(*u, &pwd, pbuffer, SH_PWBUF_SIZE, &w);
+#ifdef TRUST_MAIN
+      if ((w = getpwuid(*u)) != NULL && grp == (gid_t)(w->pw_gid) )
+	SL_IRETURN(SL_TRUE, _("isingrp"));
 #else
-      w = sh_getpwuid(*u);
-#endif
-#ifdef TRUST_MAIN
-      if (w != NULL && grp == (gid_t)(w->pw_gid) )
-	goto end_true;
-#else
-      if (w != NULL && grp == (gid_t)(w->pw_gid) )
-	{
-	  goto end_true;
-	}
-#endif
-    }
-
- end_false:
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
-  if (buffer)  free(buffer);
-  if (pbuffer) free(pbuffer);
-#endif
-  SL_IRETURN(S_FALSE, _("isingrp"));
-
- end_true:
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
-  if (buffer)  free(buffer);
-  if (pbuffer) free(pbuffer);
-#endif
-  SL_IRETURN(S_TRUE, _("isingrp"));
+      if ((w = sh_getpwuid(*u)) != NULL && grp == (gid_t)(w->pw_gid) )
+	{
+	  SL_IRETURN(SL_TRUE, _("isingrp"));
+	}
+#endif
+    }
+
+  SL_IRETURN(SL_FALSE, _("isingrp"));
 }
 
@@ -520,26 +462,13 @@
  */
 /* not static to circumvent stupid gcc 4 bug */ 
-int onlytrustedingrp(gid_t grp, uid_t *ulist, int * errval)
+int onlytrustedingrp(gid_t grp, uid_t *ulist)
 {
-  struct passwd *w;	        /* info about group member */
+  register struct passwd *w;	/* info about group member */
   register uid_t *u;		/* points to current ulist member */
   register char **p;		/* points to current group member */
-  struct group *g;	        /* pointer to group information */
+  register struct group *g;	/* pointer to group information */
   register int flag = -1;       /* group member found */
 
-  int status;
-
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
-  struct group    gr;
-  char          * buffer  = NULL;
-  struct passwd   pw;
-  char          * pbuffer = NULL;
-#endif
-
-  int retval = S_FALSE;
-
   SL_ENTER(_("onlytrustedingrp"));
-
-  *errval = 0;
 
 #ifdef TRUST_DEBUG
@@ -548,18 +477,6 @@
 #endif
 
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
-  buffer = calloc(1,SH_GRBUF_SIZE);
-  status = sh_getgrgid_r(grp, &gr, buffer, SH_GRBUF_SIZE, &g);
-#else
-  errno = 0;
-  g = sh_getgrgid(grp);
-  status = errno;
-#endif
-
-  if (g == NULL)
-    {
-      if (status == ERANGE)
-	*errval = status;
-
+  if ((g = sh_getgrgid(grp)) == NULL)
+    {
 #ifdef TRUST_DEBUG
       fprintf(stderr, 
@@ -567,6 +484,5 @@
 	      (UID_CAST)grp); 
 #endif
-      retval = S_FALSE;
-      goto end_retval;
+      SL_IRETURN(SL_FALSE, _("onlytrustedingrp") );
     }
 
@@ -574,13 +490,9 @@
    
   if(g->gr_mem == NULL || g->gr_mem[0] == NULL )
-    SL_IRETURN(S_TRUE, _("onlytrustedingrp") );
+    SL_IRETURN(SL_TRUE, _("onlytrustedingrp") );
   */
 
   /* check for untrusted members of the group
    */
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-  pbuffer = calloc(1,SH_PWBUF_SIZE);
-#endif
-
   for(p = g->gr_mem; *p != NULL; p++)
     {
@@ -591,10 +503,9 @@
       /* map user name to UID and compare 
        */
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
-      sh_getpwnam_r(*p, &pw, pbuffer, SH_PWBUF_SIZE, &w);
-#else
       w = sh_getpwnam(*p);
-#endif
-
+#ifndef TRUST_MAIN
+      if (!w)
+	w = sh_getpwnam(*p);
+#endif
       if (w == NULL)    /* not a valid user, ignore    */
 	{
@@ -640,15 +551,7 @@
 #endif 
 	  tf_baduid = w->pw_uid;
-	  retval = S_FALSE;
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
-	  if (pbuffer) free(pbuffer);
-#endif
-	  goto end_retval;
-	}
-    }
-
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
-  if (pbuffer) free(pbuffer);
-#endif
+	  SL_IRETURN(SL_FALSE, _("onlytrustedingrp"));
+	}
+    }
 
 #ifndef TEST_ONLY	
@@ -656,6 +559,4 @@
   /* now check ALL users for their GID !!!
    */
-  SH_MUTEX_LOCK(mutex_pwent);
-
   while (NULL != (w = sh_getpwent())) 
     {
@@ -676,5 +577,5 @@
 		  fprintf (stderr, 
 			   "trustfile: uid=%ld, trusted_uid=%ld, match found --> OK\n", 
-			   (UID_CAST)(w->pw_uid), (UID_CAST)(*u));
+			   (UID_CAST)(w->pw_uid), *u);
 #endif 
 		  flag = 0;
@@ -698,59 +599,39 @@
 #endif
 	      tf_baduid = w->pw_uid;
-	      retval = S_FALSE;
-	      goto out;
-	      /* SL_IRETURN(S_FALSE, _("onlytrustedingrp")); */
-	    }
-	}
-    }
-  retval = S_TRUE;
-
- out:
+	      SL_IRETURN(SL_FALSE, _("onlytrustedingrp"));
+	    }
+	}
+    }
 
 #ifdef HAVE_ENDPWENT
   sh_endpwent();
 #endif
-
-  SH_MUTEX_UNLOCK(mutex_pwent);
-
   /* TEST_ONLY */
 #endif
+
   /* #ifdef HAVE_GETPWENT */
 #endif
 
 #ifdef TRUST_DEBUG
-  if (retval == S_TRUE)
-    fprintf(stderr,
-	    "trustfile: group %ld:  all members are trusted users --> OK\n", 
-	    (UID_CAST)grp);
+  fprintf(stderr,
+	  "trustfile: group %ld:  all members are trusted users --> OK\n", 
+	  (UID_CAST)grp);
 #endif
   /* all found
    */
- end_retval:
-#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
-  if (buffer)  free(buffer);
-#endif
-  SL_IRETURN(retval, _("onlytrustedingrp"));
+  SL_IRETURN(SL_TRUE, _("onlytrustedingrp"));
 }
 
-int sl_trustfile(const char *fname, uid_t *okusers, uid_t *badusers)
+int sl_trustfile(char *fname, uid_t *okusers, uid_t *badusers)
 {
-  char * fexp = NULL;	        /* file name fully expanded        */
-  register char *p;             /* used to hold name to be checked */
+  char fexp[MAXFILENAME];	/* file name fully expanded        */
+  register char *p = fexp;      /* used to hold name to be checked */
   struct stat stbuf;	        /* used to check file permissions  */
   char c;			/* used to hold temp char          */
   
-  int errgrp = 0;
-
   SL_ENTER(_("sl_trustfile"));
   if (fname == NULL)
     SL_IRETURN(SL_EBADFILE, _("sl_trustfile"));
 
-  fexp = calloc(1, MAXFILENAME );
-  if (!fexp)
-    SL_IRETURN(SL_EMEM, _("sl_trustfile"));
-
-  p = fexp;
-
   /*
    * next expand to the full file name
@@ -760,14 +641,8 @@
   sl_errno = getfname(fname, fexp, MAXFILENAME);
   if (sl_errno != 0)
-    {
-      free(fexp);
-      return sl_errno;
-    }
+    return sl_errno;
 #else
   if (SL_ISERROR(getfname(fname, fexp, MAXFILENAME)))
-    {
-      free(fexp);
       SL_IRETURN(sl_errno, _("sl_trustfile"));
-    }
 #endif
 
@@ -812,13 +687,11 @@
       if (retry_lstat(FIL__, __LINE__, fexp, &stbuf) < 0)
 	{
-	  (void) strncpy(tf_path, fexp, sizeof(tf_path));
-	  tf_path[sizeof(tf_path)-1] = '\0';
+	  (void) strcpy(tf_path, fexp);                  /* known to fit  */
 #ifdef TRUST_MAIN
 	  fprintf(stderr, "---------------------------------------------\n");
-	  fprintf(stderr, "trustfile: ESTAT: stat(%s) failed,\n", fexp);
-	  fprintf(stderr, "maybe the file does not exist\n");
+	      fprintf(stderr, "trustfile: ESTAT: stat(%s) failed, maybe the file does not exist\n",
+		      fexp);
 	  fprintf(stderr, "---------------------------------------------\n");
 #endif
-	  free(fexp);
 	  SL_IRETURN(SL_ESTAT, _("sl_trustfile"));
 	}
@@ -847,12 +720,9 @@
 	   * got it?
 	   */
-	  char * csym;	                /* contents of symlink file  */
-	  char * full;	                /* "full" name of symlink    */
-	  char *b;                      /* used to copy stuff around */
-	  const char *t;	        /* used to copy stuff around */
-	  int lsym;	                /* num chars in symlink ref  */
-	  int i;		        /* trustworthy or not?       */
-	  const char * t_const;
-	  char *end;
+	  char csym[MAXFILENAME];	/* contents of symlink file  */
+	  char full[MAXFILENAME];	/* "full" name of symlink    */
+	  register char *b, *t;	        /* used to copy stuff around */
+	  register int lsym;	        /* num chars in symlink ref  */
+	  register int i;		/* trustworthy or not?       */
 
 	  /*
@@ -864,11 +734,4 @@
 	   * R.W. Tue May 29 22:05:16 CEST 2001
 	   */
-	  csym = calloc(1, MAXFILENAME );
-	  if (!csym)
-	    {
-	      free(fexp);
-	      SL_IRETURN(SL_EMEM, _("sl_trustfile"));
-	    }
-
 	  lsym = readlink(fexp, csym, MAXFILENAME-1);
 	  if (lsym >= 0) 
@@ -882,15 +745,5 @@
 	      fprintf(stderr, "---------------------------------------------\n");
 #endif
-	      free(csym);
-	      free(fexp);
 	      SL_IRETURN(SL_EBADNAME, _("sl_trustfile"));
-	    }
-
-	  full = calloc(1, MAXFILENAME );
-	  if (!full)
-	    {
-	      free(csym);
-	      free(fexp);
-	      SL_IRETURN(SL_EMEM, _("sl_trustfile"));
 	    }
 
@@ -900,8 +753,4 @@
 	  if (csym[0] != '/')
 	    {
-	      /* pointer to one above last element
-	       */
-	      end = &full[MAXFILENAME-1]; ++end;
-
 	      /* initialize pointers 
 	       */
@@ -911,11 +760,11 @@
 	       */
 	      t = fexp;
-	      while(*t && b < end)
+	      while(*t && b < &full[MAXFILENAME])
 		*b++ = *t++;
 
 	      /* smack on the /../ 
 	       */
-	      t_const = "/../"; t = (const char *)t_const;
-	      while(*t && b < end)
+	      t = "/../";
+	      while(*t && b < &full[MAXFILENAME])
 		*b++ = *t++;
 
@@ -923,15 +772,14 @@
 	       */
 	      t = csym;
-	      while(*t && b < end)
+	      while(*t && b < &full[MAXFILENAME])
 		*b++ = *t++;
 
 	      /* see if we're too big 
 	       */
-	      if (*t || b == end)
+	      if (*t || b == &full[MAXFILENAME])
 		{
 		  /* yes -- error 
 		   */
-		  (void) strncpy(tf_path, fexp, sizeof(tf_path));
-		  tf_path[sizeof(tf_path)-1] = '\0';
+		  (void) strcpy(tf_path, fexp);          /* known to fit  */
 #ifdef TRUST_MAIN
 		  fprintf(stderr, "---------------------------------------------\n");
@@ -941,7 +789,4 @@
 		  fprintf(stderr, "---------------------------------------------\n");
 #endif
-		  free(full);
-		  free(csym);
-		  free(fexp);
 		  SL_IRETURN(SL_ETRUNC, _("sl_trustfile"));
 		}
@@ -959,10 +804,5 @@
 	   */
 	  if ((i = sl_trustfile(full, okusers, badusers)) != SL_ENONE)
-	    {
-	      free(full);
-	      free(csym);
-	      free(fexp);
-	      SL_IRETURN(i, _("sl_trustfile"));
-	    }
+	    SL_IRETURN(i, _("sl_trustfile"));
 
 	  /*
@@ -983,6 +823,4 @@
 		p++;
 	    }
-	  free(full);
-	  free(csym);
 	  continue;
 	}
@@ -998,6 +836,6 @@
        * file regardless of permissions, so bomb
        */
-      if (((okusers != NULL && S_FALSE == isin((uid_t)stbuf.st_uid,okusers))||
-	   (badusers != NULL && S_TRUE == isin((uid_t)stbuf.st_uid,badusers))))
+      if (((okusers != NULL && SL_FALSE == isin((uid_t)stbuf.st_uid,okusers))||
+	   (badusers != NULL && SL_TRUE == isin((uid_t)stbuf.st_uid,badusers))))
 	{
 #ifdef TRUST_DEBUG
@@ -1012,9 +850,6 @@
 	  fprintf(stderr, "---------------------------------------------\n");
 #endif 
-	  (void) strncpy(tf_path, fexp, sizeof(tf_path));
-	  tf_path[sizeof(tf_path)-1] = '\0';
-
+	  (void) strcpy(tf_path, fexp);                  /* known to fit  */
 	  tf_baduid = (uid_t) stbuf.st_uid;
-	  free(fexp);
 	  SL_IRETURN(SL_EBADUID, _("sl_trustfile"));
 	}
@@ -1032,6 +867,6 @@
        */
       if (((stbuf.st_mode & S_IWGRP) == S_IWGRP) &&
-	  ((okusers != NULL && !onlytrustedingrp((gid_t)stbuf.st_gid,okusers,&errgrp))||
-	   (badusers != NULL && isingrp((gid_t)stbuf.st_gid, badusers,&errgrp)))
+	  ((okusers != NULL && !onlytrustedingrp((gid_t)stbuf.st_gid,okusers))||
+	   (badusers != NULL && isingrp((gid_t)stbuf.st_gid, badusers)))
 #ifdef STICKY
 	  && ((stbuf.st_mode&S_IFDIR) != S_IFDIR ||
@@ -1052,10 +887,7 @@
 	  fprintf(stderr, "---------------------------------------------\n");
 #endif 
-	  (void) strncpy(tf_path, fexp, sizeof(tf_path));
-	  tf_path[sizeof(tf_path)-1] = '\0';
-
+	  (void) strcpy(tf_path, fexp);                  /* known to fit  */
 	  tf_badgid = (gid_t) stbuf.st_gid;
-	  free(fexp);
-	  SL_IRETURN((errgrp == ERANGE) ? SL_ERANGE : SL_EBADGID, _("sl_trustfile"));
+	  SL_IRETURN(SL_EBADGID, _("sl_trustfile"));
 	}
       /*
@@ -1077,8 +909,5 @@
 	  fprintf(stderr, "---------------------------------------------\n");
 #endif 
-	  (void) strncpy(tf_path, fexp, sizeof(tf_path));
-	  tf_path[sizeof(tf_path)-1] = '\0';
-
-	  free(fexp);
+	  (void) strcpy(tf_path, fexp);                  /* known to fit  */
 	  SL_IRETURN(SL_EBADOTH, _("sl_trustfile"));
 	}
@@ -1103,19 +932,9 @@
    * yes, it can be trusted
    */
-  (void) strncpy(tf_path, fexp, sizeof(tf_path));
-  tf_path[sizeof(tf_path)-1] = '\0';
-
-  free(fexp);
+  (void) strcpy(tf_path, fexp);                      /* known to fit  */
   SL_IRETURN(SL_ENONE, _("sl_trustfile"));
 }
 
 #ifdef TRUST_MAIN
-
-#if defined(HOST_IS_CYGWIN) || defined(__cygwin__) || defined(__CYGWIN32__) || defined(__CYGWIN__)
-int main()
-{
-  return 0;
-}
-#else
 int main (int argc, char * argv[])
 {
@@ -1132,5 +951,5 @@
   tf_path[0] = '\0';
 #if defined(SH_WITH_SERVER)
-  pass = sh_getpwnam(SH_IDENT);  /* TESTONLY */
+  pass = getpwnam(SH_IDENT);
   if (pass != NULL)
     tf_euid = pass->pw_uid;
@@ -1155,6 +974,5 @@
 }
 #endif
-#endif
-
-
-
+
+
+
Index: trunk/src/yulectl.c
===================================================================
--- trunk/src/yulectl.c	(revision 591)
+++ trunk/src/yulectl.c	(revision 1)
@@ -24,5 +24,4 @@
 #include <stdlib.h>
 #include <string.h> 
-#include <ctype.h>
 #include <errno.h>
 
@@ -42,14 +41,9 @@
 #define SH_MAXMSG 209
 
-#if !defined(HAVE_GETPEEREID) && !defined(SO_PEERCRED) && \
-  !defined(HAVE_STRUCT_CMSGCRED) && !defined(HAVE_STRUCT_FCRED) && \
-  !(defined(HAVE_STRUCT_SOCKCRED) && defined(LOCAL_CREDS))
-#define SH_REQ_PASSWORD 1
-#endif
-
-#define SH_PW_SIZE 15
-
 static int    sock     = -1;
-static char   password[SH_PW_SIZE] = "";
+static char * sockname = NULL;
+
+static char   password[15] = "";
+
 static int    verbose = 0;
 
@@ -92,8 +86,15 @@
 
 
-static int 
-create_unix_socket ()
+int 
+make_named_socket (char * sockname)
 {
   int sock;
+
+#if 0
+  struct sockaddr_un name;
+  size_t size;
+#else
+  (void) sockname;
+#endif
 
   /* Create the socket. */
@@ -106,8 +107,27 @@
     }
 
+#if 0
+  /* Bind a name to the socket. */
+  name.sun_family = AF_FILE;
+  strcpy (name.sun_path, sockname);
+
+  /* The size of the address is
+     the offset of the start of the filename,
+     plus its length,
+     plus one for the terminating null byte. */
+  size = (offsetof (struct sockaddr_un, sun_path)
+          + strlen (name.sun_path) + 1);
+
+  if (bind (sock, (struct sockaddr *) &name, size) < 0)
+    {
+      perror (_("ERROR: bind"));
+      return -1;
+    }
+#endif
+
   return sock;
 }
 
-static void
+void
 termination_handler (int signum)
 {
@@ -118,23 +138,17 @@
 	fprintf(stdout, _("# Terminated on signal %d\n"), signum);
     }
-  if (sock   >= 0 ) 
-    close  (sock);
+#if 0
+  if (sockname != NULL) unlink (sockname);
+#endif
+  if (sock   >= 0 ) close  (sock);
+
   return;
 }
 
-static char * safe_copy(char * to, const char * from, size_t size)
-{
-  if (to && from && (size > 0))
-    {
-      strncpy (to, from, size-1);
-      to[size-1] = '\0';
-    }
-  return to;
-}
- 
-
-static int send_to_server (char * serversock, char * message)
+
+int send_to_server (char * serversock, char * message)
 {
   struct sockaddr_un name;
+  /* size_t size; */
   int size;
   int nbytes;
@@ -143,11 +157,5 @@
    */
   name.sun_family = AF_UNIX;
-  memcpy(name.sun_path, serversock, sizeof(name.sun_path));
-  name.sun_path[sizeof(name.sun_path)-1] = '\0';
-  if (strlen(serversock) > strlen(name.sun_path))
-    {
-      perror (_("ERROR: socket path too long"));
-      return -1;
-    }
+  strncpy (name.sun_path, serversock, sizeof(name.sun_path) - 1);
   size = (offsetof (struct sockaddr_un, sun_path)
           + strlen (name.sun_path) + 1);
@@ -160,7 +168,10 @@
     }
 
-  /* Send the data. 
+  /* Send the datagram. 
+  nbytes = sendto (sock, message, strlen (message) + 1, 0,
+                   (struct sockaddr *) & name, size);
    */
   nbytes = send (sock, message, strlen (message) + 1, 0);
+
   if (nbytes < 0)
     {
@@ -194,5 +205,5 @@
 }
 
-static int recv_from_server (char * message)
+int recv_from_server (char * message)
 {
   int nbytes = 0;
@@ -200,20 +211,48 @@
   int  num = 0;
   int  good = -1;
+  int  islist = 0;
   char * p;
 
   if (password[0] == '\0')
-    p = message;
-  else
-    p = &message[strlen(password)+1];
-
-  if (0 == strncmp(p, _("PROBE"), 5) ||
-      0 == strncmp(p, _("LIST"),  4))
+    {
+      if (message[0] == 'L' && message[1] == 'I' &&
+	  message[2] == 'S' && message[3] == 'T')
+	{
+	  islist = 1;
+	}
+      if (message[0] == 'P' && message[1] == 'R' &&
+	  message[2] == 'O' && message[3] == 'B' && message[4] == 'E' )
+	{
+	  islist = 1;
+	}
+    }
+  else
+    {
+      p = &message[strlen(password)+1];
+      if (p[0] == 'L' && p[1] == 'I' &&
+	  p[2] == 'S' && p[3] == 'T')
+	{
+	  islist = 1;
+	}
+      if (p[0] == 'P' && p[1] == 'R' &&
+	  p[2] == 'O' && p[3] == 'B' && p[4] == 'E' )
+	{
+	  islist = 1;
+	}
+    }
+
+  if (islist == 1)
     {
       do {
+	/*
+	nbytes = recvfrom (sock, recvmsg, SH_MAXMSG, 0, NULL, 0);
+	*/
 	nbytes = getline_from_server (sock, recvmsg, SH_MAXMSG);
 	if (nbytes < 0)
 	  {
 	    if (errno == EAGAIN)
-	      return 0;
+	      {
+		return 0;
+	      }
 	    else
 	      {
@@ -223,10 +262,11 @@
 	  }
 	else if (nbytes == 0)
-	  return 0;
-
+	  {
+	    return 0;
+	  }
 	if (recvmsg[0] == 'E' && recvmsg[1] == 'N' && recvmsg[2] == 'D')
 	  {
 	    if (verbose && (num == 0))
-	      fprintf (stdout, "%s", _("# There are no pending commands.\n"));
+	      fprintf (stdout, _("# There are no pending commands.\n"));
 	    return 0;
 	  }
@@ -237,4 +277,7 @@
   else
     {
+      /*
+      nbytes = recvfrom (sock, recvmsg, SH_MAXMSG, 0, NULL, 0);
+      */
       nbytes = recv (sock, recvmsg, SH_MAXMSG, 0);
       if (nbytes < 0)
@@ -247,19 +290,15 @@
   /* Print a diagnostic message. */
   if (password[0] == '\0')
-    good = strcmp (message, recvmsg);
-  else
-    good = strcmp (&message[strlen(password)+1], recvmsg);
+    {
+      good = strcmp (message, recvmsg);
+    }
+  else
+    {
+      good = strcmp (&message[strlen(password)+1], recvmsg);
+    }
 
   if (0 != good)
     {
-      if (0 == strncmp(recvmsg, _("!E:"), 3))
-	{ 
-	  fputs(recvmsg, stderr); 
-	  fputc('\n', stderr); 
-	}
-      else
-	{
-	  fputs (_("ERROR: Bounced message != original message.\n"), stderr);
-	}
+      fprintf (stderr, _("ERROR: Bounced message != original message.\n"));
       return -1;
     }
@@ -267,5 +306,5 @@
     {
       if (verbose)
-	fprintf (stdout, "%s", _("# Message received by server.\n"));
+	fprintf (stdout, _("# Message received by server.\n"));
     }
 
@@ -273,105 +312,31 @@
 }
 
-static int check_uuid(const char * in)
-{
-  int 		i;
-  const char	*cp;
-
-  if (!in || strlen(in) != 36)
-    return -1;
-  for (i=0, cp = in; i <= 36; i++,cp++) {
-    if ((i == 8) || (i == 13) || (i == 18) ||
-	(i == 23)) {
-      if (*cp == '-')
-	continue;
-      else
-	return -1;
-    }
-    if (i== 36)
-      if (*cp == 0)
-	continue;
-    if (!isxdigit(*cp))
-      return -1;
-  }
-  return 0;
-}
-
-static int check_command(const char * str)
-{
-  unsigned int i = 0;
-  char * commands[] = { N_("DELTA:"), N_("RELOAD"),  N_("STOP"), N_("SCAN"),
-			N_("CANCEL"), N_("LISTALL"), N_("LIST"), N_("PROBE"), NULL };
-
-  while (commands[i])
-    {
-      size_t len = strlen(_(commands[i]));
-
-      if (0 == strncmp(_(commands[i]), str, len))
-	{
-	  if (i == 0)
-	    {
-	      char * p = strchr(str, ':'); ++p;
-	      if ( 0 == check_uuid(p) )
-		return 0;
-	    }
-	  else
-	    {
-	      if (len == strlen(str))
-		return 0;
-	    }
-	}
-      ++i;
-    }
-
-  fprintf (stderr, _("ERROR: invalid command <%s>\n\n"), str);
-  return -1;
-}
-
-static void print_usage_and_exit(char * name, int exit_status)
+void usage(char * name)
 {
   printf(_("\nUsage : %s [-v][-s server_socket] -c command <client_hostname>\n\n"), 
 	 name);
 
-  printf("%s", _("Purpose : send commands to the server via a socket,\n"));
-  printf("%s", _("          in particular commands that the server would\n"));
-  printf("%s", _("          transfer to the client <client_hostname> when\n"));
-  printf("%s", _("          this client connects to deliver a message.\n\n"));
-  printf("%s", _("          If password is required, it is read from\n"));
-  printf("%s", _("          $HOME/.yulectl_cred or taken from the environment\n"));
-  printf("%s", _("          variable YULECTL_PASSWORD (not recommended).\n\n"));
-
-  printf("%s", _("Commands: RELOAD         reload configuration\n"));
-  printf("%s", _("          DELTA:<uuid>   load delta database with given uuid\n"));
-  printf("%s", _("          STOP           terminate\n"));
-  printf("%s", _("          SCAN           initiate file system check\n"));
-  printf("%s", _("          CANCEL         cancel pending command(s)\n"));
-  printf("%s", _("          LIST           list queued commands\n"));
-  printf("%s", _("          LISTALL        list queued and last sent commands\n"));
-  printf("%s", _("          PROBE          probe all clients for necessity of reload\n"));
-  exit(exit_status);
-}
-
-char * rtrim(char * str)
-{
-  size_t len;
-
-  if (!str) return str;
-
-  len = strlen(str);
-  while (len > 0)
-    {
-      --len;
-      if (str[len] == '\n' || str[len] == '\r')
-	str[len] = '\0';
-      else
-	break;
-    }
-  return str;
-}
-
-static int get_home(char * home, size_t size)
-{
+  printf(_("Purpose : send commands to the server via a socket,\n"));
+  printf(_("          in particular commands that the server would\n"));
+  printf(_("          transfer to the client <client_hostname> when\n"));
+  printf(_("          this client connects to deliver a message.\n\n"));
+
+  printf(_("Commands: RELOAD    <reload configuration>\n"));
+  printf(_("          STOP      <terminate>\n"));
+  printf(_("          CANCEL    <cancel previous command>\n"));
+  printf(_("          LIST      <list queued commands>\n"));
+  printf(_("          LISTALL   <list queued and last sent commands>\n"));
+  printf(_("          PROBE     <probe all clients for necessity of reload>\n"));
+  return;
+}
+
+void fixup_message (char * message)
+{
+  char message2[SH_MAXMSG];
+  char home[4096];
+  FILE * fp;
   struct passwd * pwent;
 
+  
   pwent = getpwuid(geteuid());
   if ((pwent == 0) || (pwent->pw_dir == NULL))
@@ -382,165 +347,99 @@
       if (NULL != getenv(_("HOME")))
 	{
-	  safe_copy(home, getenv(_("HOME")), size);
+	  strncpy(home, getenv(_("HOME")), 4096);
+	  home[4095] = '\0';
 	}
       else
 	{
 	  fprintf (stderr, _("ERROR: no home directory for euid %ld (tried $HOME and password database).\n"), (long) geteuid());
-	  return -1;
-	}
-    }
-  else
-    {
-      safe_copy(home, pwent->pw_dir, size);
-    }
-  return 0;
-}
-
-static int get_passwd(char * message2, size_t size)
-{
-  char home[4096];
-  FILE * fp;
-  char * pw;
-
-  /* 1) Password from environment
-   */
-  pw = getenv(_("YULECTL_PASSWORD"));
-  if (pw && strlen(pw) < SH_PW_SIZE)
-    {
-      strcpy(password, pw);
-      strcpy(message2, password);
-      return 0;
-    }
-
-  /* 2) Password from $HOME/.yule_cred
-   */
-  if (get_home(home, sizeof(home)) < 0)
-    return -1;
-
-  if ( (strlen(home) + strlen(_("/.yulectl_cred")) + 1) > sizeof(home))
-    {
-      fprintf (stderr, "%s", _("ERROR: path for $HOME is too long.\n"));
-      return -1;
+	  exit(EXIT_FAILURE);
+	}
+    }
+  else
+    {
+      strncpy(home, pwent->pw_dir, 4096);
+      home[4095] = '\0';
+    }
+
+  if ( (strlen(home) + strlen(_("/.yulectl_cred")) + 1) > 4096)
+    {
+      fprintf (stderr, _("ERROR: path for $HOME is too long.\n"));
+      exit(EXIT_FAILURE);
     }
   strcat(home, _("/.yulectl_cred"));
   fp = fopen(home, "r");
-
-#if defined(SH_REQ_PASSWORD)
   if (fp == NULL)
     {
-      if (errno == ENOENT) {
-	fprintf (stderr, 
-		 _("ERROR No password file (%s) exists\n"),
+      if (verbose && (errno == ENOENT))
+	fprintf (stdout, 
+		 _("# Password file (%s) missing\n"),
 		 home);
-      }
-      else {
-	fprintf (stderr, 
-		 _("ERROR: Password file (%s) not accessible for euid %ld uid %ld\n"),
+      else if (verbose)
+	fprintf (stdout, 
+		 _("# Password file (%s) not accessible for euid %ld uid %ld\n"),
 		 home, (long)geteuid(), (long)getuid());
-      }
-      return -1;
-    }
-#else
-  if (fp == NULL)
-    return 0;
-#endif
-
-  if (NULL == fgets(message2, size, fp))
-    {
-      fprintf (stderr,
+      return;
+    }
+  if (NULL == fgets(message2, SH_MAXMSG, fp))
+    {
+      fprintf (stderr, 
 	       _("ERROR: empty or unreadable password file (%s).\n"),
 	       home);
-      return -1;
-    }
-
-  (void) rtrim(message2);
-
-  if (strlen(message2) > (SH_PW_SIZE -1))
-    {
-      fprintf (stderr, "%s", 
+      exit(EXIT_FAILURE);
+    }
+  if (strlen(message2) > 15)
+    {
+      fprintf (stderr, 
 	       _("ERROR: Password too long (max. 14 characters).\n"));
-      return -1;
+      exit(EXIT_FAILURE);
+    }
+
+  if ( (0 != strlen(message2)) && ('\n' == message2[strlen(message2)-1]))
+    {
+      message2[strlen(message2)-1] = '\0';
     }
   strcpy(password, message2);
+  strcat(message2, "@");
   fclose(fp);
 
-  return 0;
-}
-
-static int fixup_message (char * message)
-{
-  char message_fixed[SH_MAXMSG] = { 0 };
-
-  if (get_passwd(message_fixed, sizeof(message_fixed)) < 0)
-    return -1;
-
-  if (strlen(message_fixed) > 0)
-    {
-      strcat(message_fixed, "@");
-
-      strncat(message_fixed, message, SH_MAXMSG - strlen(message_fixed) -1);
-      message_fixed[SH_MAXMSG-1] = '\0';
-      strcpy(message, message_fixed);
-    }
-  return 0;
-}
-
-static int fill_serversock(char * serversock, size_t size)
-{
-  int status;
-
+  strncat(message2, message, SH_MAXMSG - strlen(message2) -1);
+  message2[SH_MAXMSG-1] = '\0';
+  strcpy(message, message2);
+  return;
+}
+
+
+int
+main (int argc, char * argv[])
+{
+
+  char   message[SH_MAXMSG] = "";
+  char   clientcd[1024];
+  char   serversock[256];
+  int    status, size;
+  int    num = 1;
+  int    flag = 0;
+  
 #ifdef HAVE_VSNPRINTF
-  status = snprintf(serversock, size, _("%s/%s.sock"), 
+  status = snprintf(serversock, 256, _("%s/%s.sock"), 
 		    DEFAULT_PIDDIR, SH_INSTALL_NAME);
 #else
-  if ((strlen(DEFAULT_PIDDIR) + strlen(SH_INSTALL_NAME) + 1 + 6) > size)
-    status = -1;
-  else
-    status = sprintf (serversock, _("%s/%s.sock"), 
-		      DEFAULT_PIDDIR, SH_INSTALL_NAME);
-#endif
-
-  if ((status < 0) || (status > (int)(size-1)))
-    {
-      fprintf(stderr, _("ERROR: Path too long (maximum %d): %s/%s.sock\n"), 
-	      (int) (size-1), DEFAULT_PIDDIR, SH_INSTALL_NAME);
-      return -1;
-    }
-  return 0;
-}
-
-static void checklen(char * command, char * str, size_t maxlen)
-{
-  if (strlen(str) > maxlen) 
-    {
-      fprintf(stderr, _("ERROR: String too long (max %d): %s\n\n"), 
-	      (int) maxlen, str);
-      print_usage_and_exit (command, EXIT_FAILURE);
-    }
-  return;
-}
-
-static void checknull(char * command, char * str)
-{
-  if (str == NULL || str[0] == '\0') {
-    fprintf(stderr, "%s", _("ERROR: option with missing argument\n\n"));
-    print_usage_and_exit(command, EXIT_FAILURE);
-  }
-  return;
-}
-
-int
-main (int argc, char * argv[])
-{
-
-  char   message[SH_MAXMSG] = "";
-  char   serversock[256];
-  int    status;
-  int    num = 1;
-  int    flag = 0;
-
-  if (fill_serversock(serversock, sizeof(serversock)) < 0)
-    return (EXIT_FAILURE);
-
+  if ((strlen(DEFAULT_PIDDIR) + strlen(SH_INSTALL_NAME) + 1 + 6) > 256)
+    {
+      status = -1;
+    }
+  else
+    {
+      status = sprintf (serversock, _("%s/%s.sock"), 
+			DEFAULT_PIDDIR, SH_INSTALL_NAME);
+    }
+#endif
+
+  if ((status < 0) || (status > 255))
+    {
+      fprintf(stderr, _("ERROR: Path too long (maximum 255): %s/%s.sock\n"), 
+	      DEFAULT_PIDDIR, SH_INSTALL_NAME);
+      return (EXIT_FAILURE);
+    }
 
   while (argc > 1 && argv[num][0] == '-')
@@ -549,30 +448,53 @@
 	{
 	  case 'h':
-	    print_usage_and_exit(argv[0], EXIT_SUCCESS);
-	    break;
+	    usage(argv[0]);
+	    return (EXIT_SUCCESS);
+
 	  case 'v':
 	    ++verbose;
 	    break;
+
 	  case 's':
 	    --argc; ++num;
-	    checknull(argv[0], argv[num]);
-	    checklen(argv[0], argv[num], sizeof(serversock)-1);
-	    safe_copy (serversock, argv[num], sizeof(serversock));
+	    if (argv[num] == NULL || argv[num][0] == '\0') {
+	      usage(argv[0]);
+	      fprintf(stderr, _("ERROR: -s: argument missing\n"));
+	      return (EXIT_FAILURE);
+	    } else {
+	      if (strlen(argv[num]) > 255) 
+		{
+		  fprintf(stderr, _("ERROR: Path too long: %s\n"), argv[num]);
+		  return (EXIT_FAILURE);
+		}
+	      strncpy (serversock, argv[num], 256);
+	      serversock[255] = '\0';
+	    }
 	    break;
+
 	  case 'c':
 	    --argc; ++num;
-	    checknull(argv[0], argv[num]);
-	    checklen(argv[0], argv[num], SH_MAXMSG-1);
-	    if (0 != check_command(argv[num]))
-	      print_usage_and_exit(argv[0], EXIT_FAILURE);
-	    safe_copy(message, argv[num], SH_MAXMSG);
-	    strncat(message, ":", SH_MAXMSG-strlen(message)-1);
-	    message[SH_MAXMSG-1] = '\0';
-	    flag = 1;
+	    if (argv[num] == NULL || argv[num][0] == '\0') {
+	      usage(argv[0]);
+	      fprintf(stderr, _("ERROR: -c: argument missing\n"));
+	      return (EXIT_FAILURE);
+	    } else {
+	      if (strlen(argv[num]) >= SH_MAXMSG) 
+		{
+		  fprintf(stderr, _("ERROR: Command too long: %s\n"), 
+			  argv[num]);
+		  return (EXIT_FAILURE);
+		}
+	      strncpy (message, argv[num], SH_MAXMSG);
+	      message[SH_MAXMSG-1] = '\0';
+	      strncat(message, ":", SH_MAXMSG-strlen(message)-1);
+	      message[SH_MAXMSG-1] = '\0';
+	      flag = 1;
+	    }
 	    break;
+
 	  default:
-	    fprintf(stderr, _("ERROR: unknown option -%c\n\n"), argv[num][1]);
-	    print_usage_and_exit(argv[0], EXIT_FAILURE);
-	    break;
+	    usage(argv[0]);
+	    fprintf(stderr, _("ERROR: unknown option -%c\n"), argv[num][1]);
+	    return (EXIT_FAILURE);
 	}
       --argc; ++num;
@@ -580,35 +502,67 @@
 
   if (flag == 0) /* no command given */
-    print_usage_and_exit(argv[0], EXIT_FAILURE);
+    {
+      usage(argv[0]);
+      return (EXIT_FAILURE);
+    }
 
   if (argc > 1)
     {
-      checklen(argv[0], argv[num], SH_MAXMSG - strlen(message) - 1);
-      strncat (message, argv[num], SH_MAXMSG - strlen(message) - 1);
+      if (strlen(argv[num]) > (SH_MAXMSG - strlen(message) -1)) 
+	{
+	  fprintf(stderr, _("ERROR: Hostname too long: %s\n"), argv[num]);
+	  return (EXIT_FAILURE);
+	}
+      strncat (message, argv[num], SH_MAXMSG -strlen(message) - 1);
       message[SH_MAXMSG-1] = '\0';
     }
   else
     {
-      if (0 == strncmp(message, _("PROBE"), 5) ||
-	  0 == strncmp(message, _("LIST"),  4))
+      if (message[0] == 'P' && message[1] == 'R' &&
+	  message[2] == 'O' && message[3] == 'B' && message[4] == 'E' )
 	{
 	  strncat (message, _("dummy"), SH_MAXMSG -strlen(message) - 1);
 	  message[SH_MAXMSG-1] = '\0';
 	}
+      else if (message[0] == 'L' && message[1] == 'I' &&
+	       message[2] == 'S' && message[3] == 'T')
+	{
+	  strncat (message, _("dummy"), SH_MAXMSG -strlen(message) - 1);
+	  message[SH_MAXMSG-1] = '\0';
+	}
       else
 	{
-	  fprintf(stderr, "%s", _("ERROR: this command requires a hostname\n"));
-	  print_usage_and_exit(argv[0], EXIT_FAILURE);
-	}
-    }
-
-  if (fixup_message(message) < 0)
-    return (EXIT_FAILURE);
+	  fprintf(stderr, "ERROR: this command requires a hostname\n");
+	  usage(argv[0]);
+	  return (EXIT_FAILURE);
+	}
+    }
+
+  fixup_message(message);
+
+  /* OpenBSD wants >= 1024
+   */
+  if (NULL == getcwd(clientcd, 1024))
+    {
+      perror(_("ERROR: getcwd"));
+      return (EXIT_FAILURE);
+    }
+  size = strlen(clientcd) + 1 + strlen(CLIENT) + 6;
+  sockname = malloc (size);
+  if (!sockname)
+    {
+      perror(_("ERROR: main: malloc"));
+      return (EXIT_FAILURE);
+    }
+  sprintf(sockname, _("%s/%s.sock"), clientcd, CLIENT);
+
 
   /* Make the socket.
    */
-  sock = create_unix_socket ();
+  sock = make_named_socket (sockname);
   if (sock < 0)
-    return (EXIT_FAILURE);
+    {
+      return (EXIT_FAILURE);
+    }
 
   /* Set up termination handler.
@@ -624,5 +578,5 @@
   if (status < 0)
     {
-      fprintf(stderr, "%s", _("ERROR: sending command to server failed\n"));
+      fprintf(stderr, _("ERROR: sending command to server failed\n"));
       (void) termination_handler(0);
       return (EXIT_FAILURE);
@@ -631,17 +585,20 @@
   /* Wait for a reply. 
    */
-  if (verbose)
-    {
-      if (0 == strncmp(message, "LIST", 4))
-	fprintf(stdout, "%s", _("# Waiting for listing.\n"));
-      else
-	fprintf(stdout, "%s", _("# Waiting for confirmation.\n"));
-    }
-
+  if (message[0] == 'L' && message[1] == 'I' &&
+      message[2] == 'S' && message[3] == 'T')
+    {
+      if (verbose)
+	fprintf(stdout, _("# Waiting for listing.\n"));
+    }
+  else
+    {
+      if (verbose)
+	fprintf(stdout, _("# Waiting for confirmation.\n"));
+    }
   status = recv_from_server (message);
 
   if (status < 0)
     {
-      fputs(_("ERROR: unexpected or no reply from server.\n"), stderr);
+      fprintf(stderr, _("ERROR: receiving data from server failed.\n"));
       (void) termination_handler(0);
       return (EXIT_FAILURE);
Index: trunk/src/zAVLTree.c
===================================================================
--- trunk/src/zAVLTree.c	(revision 591)
+++ trunk/src/zAVLTree.c	(revision 1)
@@ -32,88 +32,4 @@
 #include <string.h>
 #include "zAVLTree.h"
-
-/* Interface for handling "string only" items rw 2014-06-26
- */
-static zAVLKey zstring_key (void const * arg)
-{
-  return (zAVLKey) arg;
-}
-
-char * dummy_zfree_string;
-#ifdef __clang__
-char * dummy_zfree_str;
-#endif
-
-static void zfree_string (void * inptr)
-{
-#ifdef __clang__
-  dummy_zfree_str = (char *) inptr;
-#else
-  char * str = (char *) inptr;
-#endif
-
-  /* Take the address to circumvent gcc 4.9 optimizer bug */
-  dummy_zfree_string = (char *) &inptr;
-
-#ifdef __clang__
-  dummy_zfree_str[0] = '\0';
-  free (dummy_zfree_str);
-#else
-  str[0] = '\0';
-  free (inptr);
-#endif
-  return;
-}
-void zAVL_string_reset (zAVLTree * tree)
-{
-  if (tree)
-    zAVLFreeTree (tree, zfree_string);
-  return;
-}
-int zAVL_string_set (zAVLTree ** tree, const char * key)
-{
-  if (tree && key)
-    {
-      zAVLTree * itree = (*tree);
-      if (!itree)
-	{
-	  itree = zAVLAllocTree (zstring_key, zAVL_KEY_STRING);
-	  if (!itree) 
-	    {
-	      return -1;
-	    }
-	}
-      *tree = itree;
-      return zAVLInsert (itree, strdup(key));
-    }
-  return -1;
-}
-char * zAVL_string_get (zAVLTree * tree, const char * key)
-{
-  /* zAVLSearch() checks for NULL tree 
-   */
-  if (key)
-    {
-      return ((char *) zAVLSearch (tree, key));
-    }
-  return NULL;
-}
-void zAVL_string_del (zAVLTree * tree, const char * key)
-{
-  /* zAVLSearch() checks for NULL tree 
-   */
-  if (key)
-    {
-      char * item = ((char *) zAVLSearch (tree, key));
-      if (item)
-	{
-	  zAVLDelete(tree, key);
-	  zfree_string(item);
-	}
-    }
-  return;
-}
-
-
 
 /* Wed Nov 23 17:57:42 CET 2005 rw: introduce third argument in
@@ -137,24 +53,4 @@
 #define ZAVL_NO 0
 
-/* The comparison function. Was a macro, but this allows for more
- * flexibility (non-string keys). The key is a (void *) now, and
- * the type is stored in the zAVLTree struct. Oct 21, 2011, rw
- */
-static int zAVLKey_cmp(const zAVLTree * tree, zAVLKey a, zAVLKey b)
-{
-  if (tree->keytype == zAVL_KEY_STRING)
-    {
-      return (strcmp((const char*)a, (const char *)b));
-    }
-  else /* zAVL_KEY_INT */
-    {
-      const int x = *((const int *)a);
-      const int y = *((const int *)b);
-
-      if      (x > y) return  1;
-      else if (x < y) return -1;
-      else return 0;
-    }
-}
 
 /*
@@ -166,9 +62,9 @@
  * was a malloc failure, then NULL is returned.
  */
-zAVLTree *zAVLAllocTree (zAVLKey (*getkey)(void const *item), int keytype)
+zAVLTree *zAVLAllocTree (zAVLKey (*getkey)(void const *item))
 {
   zAVLTree *rc;
 
-  rc = calloc(1, sizeof(zAVLTree));
+  rc = malloc(sizeof(zAVLTree));
   if (rc == NULL)
     return NULL;
@@ -177,5 +73,4 @@
   rc->count = 0;
   rc->getkey = getkey;
-  rc->keytype = keytype;
   return rc;
 }
@@ -214,5 +109,5 @@
   int       ok;
 
-  newnode = calloc(1, sizeof(zAVLNode));
+  newnode = malloc(sizeof(zAVLNode));
   if (newnode == NULL)
     return -1;
@@ -228,5 +123,5 @@
     node = zAVLCloseSearchNode(avltree, newnode->key, &ok);
 
-    if (ok == ZAVL_OK) { /* exists already */
+    if (!zAVLKey_cmp(avltree, node->key, newnode->key)) {
       free(newnode);
       return 3;
@@ -296,5 +191,5 @@
 
   avlnode = zAVLCloseSearchNode(avltree, key, &ok);
-  if (avlnode == NULL || ok == ZAVL_NO) /* does not exist */
+  if (avlnode == NULL || zAVLKey_cmp(avltree, avlnode->key, key))
     return -1;
 
@@ -450,5 +345,5 @@
   depthdiff = R_DEPTH(avlnode) - L_DEPTH(avlnode);
 
-  if (depthdiff <= -2 && avlnode->left) {
+  if (depthdiff <= -2) {
     child = avlnode->left;
 
@@ -468,28 +363,25 @@
     else {
       gchild = child->right;
-      if (gchild)
-	{
-	  avlnode->left = gchild->right;
-	  if (avlnode->left != NULL)
-	    avlnode->left->parent = avlnode;
-	  avlnode->depth = CALC_DEPTH(avlnode);
-	  child->right = gchild->left;
-	  if (child->right != NULL)
-	    child->right->parent = child;
-	  child->depth = CALC_DEPTH(child);
-	  gchild->right = avlnode;
-	  if (gchild->right != NULL)
-	    gchild->right->parent = gchild;
-	  gchild->left = child;
-	  if (gchild->left != NULL)
-	    gchild->left->parent = gchild;
-	  gchild->depth = CALC_DEPTH(gchild);
-	  *superparent = gchild;
-	  gchild->parent = origparent;
-	}
-    }
-  }
-
-  else if (depthdiff >= 2 && avlnode->right) {
+      avlnode->left = gchild->right;
+      if (avlnode->left != NULL)
+        avlnode->left->parent = avlnode;
+      avlnode->depth = CALC_DEPTH(avlnode);
+      child->right = gchild->left;
+      if (child->right != NULL)
+        child->right->parent = child;
+      child->depth = CALC_DEPTH(child);
+      gchild->right = avlnode;
+      if (gchild->right != NULL)
+        gchild->right->parent = gchild;
+      gchild->left = child;
+      if (gchild->left != NULL)
+        gchild->left->parent = gchild;
+      gchild->depth = CALC_DEPTH(gchild);
+      *superparent = gchild;
+      gchild->parent = origparent;
+    }
+  }
+
+  else if (depthdiff >= 2) {
     child = avlnode->right;
 
@@ -509,24 +401,21 @@
     else {
       gchild = child->left;
-      if (gchild)
-	{
-	  avlnode->right = gchild->left;
-	  if (avlnode->right != NULL)
-	    avlnode->right->parent = avlnode;
-	  avlnode->depth = CALC_DEPTH(avlnode);
-	  child->left = gchild->right;
-	  if (child->left != NULL)
-	    child->left->parent = child;
-	  child->depth = CALC_DEPTH(child);
-	  gchild->left = avlnode;
-	  if (gchild->left != NULL)
-	    gchild->left->parent = gchild;
-	  gchild->right = child;
-	  if (gchild->right != NULL)
-	    gchild->right->parent = gchild;
-	  gchild->depth = CALC_DEPTH(gchild);
-	  *superparent = gchild;
-	  gchild->parent = origparent;
-	}
+      avlnode->right = gchild->left;
+      if (avlnode->right != NULL)
+        avlnode->right->parent = avlnode;
+      avlnode->depth = CALC_DEPTH(avlnode);
+      child->left = gchild->right;
+      if (child->left != NULL)
+        child->left->parent = child;
+      child->depth = CALC_DEPTH(child);
+      gchild->left = avlnode;
+      if (gchild->left != NULL)
+        gchild->left->parent = gchild;
+      gchild->right = child;
+      if (gchild->right != NULL)
+        gchild->right->parent = gchild;
+      gchild->depth = CALC_DEPTH(gchild);
+      *superparent = gchild;
+      gchild->parent = origparent;
     }
   }
@@ -551,8 +440,6 @@
   if (avlnode->right)
     zAVLFreeBranch(avlnode->right, freeitem);
-  if (freeitem) {
+  if (freeitem)
     freeitem(avlnode->item);
-    avlnode->item = NULL;
-  }
   free(avlnode);
 }
