Index: trunk/src/samhain_setpwd.c
===================================================================
--- trunk/src/samhain_setpwd.c	(revision 581)
+++ 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);
