~ubuntu-branches/debian/sid/samhain/sid

« back to all changes in this revision

Viewing changes to src/samhain_setpwd.c

  • Committer: Bazaar Package Importer
  • Author(s): Javier Fernandez-Sanguino Pen~a
  • Date: 2006-08-19 10:38:36 UTC
  • mfrom: (1.2.1 upstream) (3.1.2 edgy)
  • Revision ID: james.westby@ubuntu.com-20060819103836-3mhhosrrqe1h33zv
Tags: 2.2.3-2
Disable GCC_STACK_PROTECT from the autoconf definition as this
introduces a FTBFS currently. Should be reenabled once this
issue is fixed (Closes: #382617)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include "config_xor.h"
2
2
 
3
 
#ifdef HAVE_BROKEN_INCLUDES
4
 
#define _ANSI_C_SOURCE
5
 
#define _POSIX_SOURCE
6
 
#endif
7
 
 
8
3
#include <stdio.h>
9
4
#include <stdlib.h>
10
5
#include <string.h>
12
7
 
13
8
#include <unistd.h>
14
9
#include <sys/types.h>
 
10
#include <sys/wait.h>
15
11
#include <sys/stat.h>
16
12
#include <fcntl.h>
 
13
#include <errno.h>
 
14
#include <sys/time.h>
17
15
#include <time.h>
18
16
 
 
17
#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_YIELD)
 
18
#include <sched.h>
 
19
#endif
 
20
 
 
21
#if defined(HAVE_INT_32)
 
22
typedef unsigned int UINT32;
 
23
#elif defined(HAVE_LONG_32)
 
24
typedef unsigned long UINT32;
 
25
#elif defined(HAVE_SHORT_32)
 
26
typedef unsigned short UINT32;
 
27
#endif
 
28
 
 
29
#define TAUS_MAX 4294967295UL
 
30
 
 
31
static UINT32 taus_state[3];
 
32
 
 
33
static UINT32 taus_get ()
 
34
{
 
35
 
 
36
#define TAUSWORTHE(s,a,b,c,d) ((s &c) <<d) ^ (((s <<a) ^s) >>b)
 
37
  taus_state[0] = TAUSWORTHE (taus_state[0], 13, 19, 4294967294UL, 12);
 
38
  taus_state[1] = TAUSWORTHE (taus_state[1],  2, 25, 4294967288UL,  4);
 
39
  taus_state[2] = TAUSWORTHE (taus_state[2],  3, 11, 4294967280UL, 17);
 
40
  return (taus_state[0] ^ taus_state[1] ^ taus_state[2]);
 
41
}
 
42
 
 
43
static void taus_seed ()
 
44
{
 
45
  unsigned char buf[12];
 
46
  unsigned char buf2[12];
 
47
  unsigned char buf3[12];
 
48
  ssize_t count;
 
49
  size_t nbytes = sizeof(buf);
 
50
  size_t where  = 0;
 
51
 
 
52
  struct timeval t1, t2;
 
53
  UINT32 delta, k[3];
 
54
  int i, j;
 
55
 
 
56
  int fd = open ("/dev/urandom", O_RDONLY);
 
57
 
 
58
  if (fd == -1)
 
59
    {
 
60
      gettimeofday(&t1, NULL);
 
61
      delta = t1.tv_usec;
 
62
      memcpy(&buf[0], &delta, 4);
 
63
      gettimeofday(&t1, NULL);
 
64
      delta = t1.tv_usec;
 
65
      memcpy(&buf[4], &delta, 4);
 
66
      gettimeofday(&t1, NULL);
 
67
      delta = t1.tv_usec;
 
68
      memcpy(&buf[8], &delta, 4);
 
69
      goto second;
 
70
    }
 
71
 
 
72
  while (nbytes) {
 
73
    count = read(fd, &buf[where], nbytes);
 
74
    if (count == -1 && errno == EINTR)
 
75
      continue;
 
76
    where  += count;
 
77
    nbytes -= count;
 
78
  } while (count == -1 && errno == EINTR);
 
79
 
 
80
  close(fd);
 
81
 
 
82
 second:
 
83
  for (i = 0; i < 12; ++i)
 
84
    {
 
85
      gettimeofday(&t1, NULL);
 
86
      if (0 == fork())
 
87
        _exit(EXIT_SUCCESS);
 
88
      wait(NULL);
 
89
      gettimeofday(&t2, NULL);
 
90
      delta = t2.tv_usec - t1.tv_usec;
 
91
      buf2[i] = (unsigned char) delta;
 
92
    }
 
93
 
 
94
  for (i = 0; i < 12; ++i)
 
95
    {
 
96
      gettimeofday(&t1, NULL);
 
97
      for (j = 0; j < 32768; ++j)
 
98
        {
 
99
          if (0 == kill (j,0))
 
100
            k[i % 3] ^= j;
 
101
        }
 
102
      gettimeofday(&t2, NULL);
 
103
      delta = t2.tv_usec - t1.tv_usec;
 
104
      buf3[i] ^= (unsigned char) delta;
 
105
    }
 
106
 
 
107
  memcpy(&taus_state[0], &buf3[0], 4);
 
108
  memcpy(&taus_state[1], &buf3[4], 4);
 
109
  memcpy(&taus_state[2], &buf3[8], 4);
 
110
 
 
111
  taus_state[0] ^= k[0];
 
112
  taus_state[1] ^= k[1];
 
113
  taus_state[2] ^= k[2];
 
114
  
 
115
  memcpy(&k[0], &buf2[0], 4);
 
116
  memcpy(&k[1], &buf2[4], 4);
 
117
  memcpy(&k[2], &buf2[8], 4);
 
118
 
 
119
  taus_state[0] ^= k[0];
 
120
  taus_state[1] ^= k[1];
 
121
  taus_state[2] ^= k[2];
 
122
  
 
123
  memcpy(&k[0], &buf[0], 4);
 
124
  memcpy(&k[1], &buf[4], 4);
 
125
  memcpy(&k[2], &buf[8], 4);
 
126
 
 
127
  taus_state[0] ^= k[0];
 
128
  taus_state[1] ^= k[1];
 
129
  taus_state[2] ^= k[2];
 
130
 
 
131
  taus_state[0] |= (UINT32) 0x03;
 
132
  taus_state[1] |= (UINT32) 0x09;
 
133
  taus_state[2] |= (UINT32) 0x17;
 
134
}
19
135
 
20
136
#ifdef SH_STEALTH
21
 
char * globber(char * string);
 
137
char * globber(const char * string);
22
138
#define _(string) globber(string) 
23
139
#define N_(string) string
24
140
#else
30
146
#ifndef SH_MAX_GLOBS
31
147
#define SH_MAX_GLOBS 32
32
148
#endif
33
 
char * globber(char * str)
 
149
char * globber(const char * str)
34
150
{
35
151
  register int i, j;
36
152
  static int  count = -1;
111
227
  int    badcnt = 0;
112
228
 
113
229
  char * newn;
 
230
  size_t nlen;
114
231
  int    oldf;
115
232
  int    newf;
116
233
 
154
271
      fprintf (stderr, _("   ('A' = 41 hex, 'B' = 42 hex, ...) "\
155
272
               "and outputs the result\n"));
156
273
      fprintf (stderr, _("   to 'samhain.new'.\n"));
157
 
      return -1;
 
274
      return  EXIT_FAILURE;
158
275
    }
159
276
 
160
277
  if (strlen(argv[3]) != 16)
162
279
      fprintf (stdout, _("ERROR <new_password> %s has not exactly 16 chars\n"),
163
280
               argv[0]);
164
281
      fflush(stdout);
165
 
      return -1;
 
282
      return  EXIT_FAILURE;
166
283
    }
167
284
 
168
285
 
184
301
            {
185
302
              fprintf(stdout, _("ERROR Invalid char %c\n"), str[i]);
186
303
              fflush(stdout);
187
 
              _exit(EXIT_FAILURE);
 
304
              return EXIT_FAILURE;
188
305
            }
189
306
        }
190
307
      ++i;
195
312
   */
196
313
  (void) umask (0);
197
314
 
198
 
  srand(time(NULL) ^ getpid());
 
315
  taus_seed();
199
316
 
200
317
  bytecount = 0;
201
318
 
205
322
  
206
323
  oldf = open(argv[1], O_RDONLY);
207
324
 
208
 
  newn = (char *) malloc (strlen(argv[1])+strlen(argv[2])+2);
209
 
  strcpy(newn, argv[1]);
210
 
  strcat(newn, ".");
211
 
  strcat(newn, argv[2]);
 
325
  nlen = strlen(argv[1])+strlen(argv[2])+2;
 
326
  newn = (char *) malloc (nlen);
 
327
  strncpy(newn, argv[1], nlen); newn[nlen-1] = '\0';
 
328
  strncat(newn, ".", nlen);     newn[nlen-1] = '\0';
 
329
  strncat(newn, argv[2], nlen); newn[nlen-1] = '\0';
212
330
  newf = open(newn, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
213
331
 
214
332
  if (oldf < 0)
215
333
    {
216
334
      fprintf(stdout, _("ERROR Cannot open input file %s.\n"), argv[1]);
217
335
      fflush(stdout);
218
 
      _exit(EXIT_FAILURE);
 
336
      return EXIT_FAILURE;
219
337
    }
220
338
  if (newf < 0)
221
339
    {
222
340
      fprintf(stdout, _("ERROR Cannot open output file %s.\n"), newn);
223
341
      fflush(stdout);
224
 
      _exit(EXIT_FAILURE);
 
342
      return EXIT_FAILURE;
225
343
    }
226
344
      
227
345
  /* ---- scan file -----
264
382
              sprintf(&oldpwd[i*2], _("%02x"), 
265
383
                      (unsigned char) *found_it);
266
384
 
267
 
              ccd = (unsigned char) (256.0 * rand()/(RAND_MAX+1.0));
 
385
              ccd = (unsigned char) (256.0 * (taus_get()/(TAUS_MAX+1.0)));
268
386
              sprintf(&newpwd[i*2], _("%02x"), 
269
387
                      (unsigned char) ccd);
270
388
              *found_it = ccd;
339
457
              sprintf(&oldpwd[i*2], _("%02x"), 
340
458
                      (unsigned char) *found_it);
341
459
 
342
 
              ccd = (unsigned char) (256.0 * rand()/(RAND_MAX+1.0));
 
460
              ccd = (unsigned char) (256.0 * taus_get()/(TAUS_MAX+1.0));
343
461
              sprintf(&newpwd[i*2], _("%02x"), 
344
462
                      (unsigned char) ccd);
345
463
              *found_it = ccd;
359
477
  if (suc == 1 && badcnt == 7)
360
478
    {
361
479
      fprintf (stdout, _("INFO   finished\n"));
 
480
      fflush(stdout);
 
481
      return 0;
362
482
    }
363
 
  else if (suc == 0 || badcnt < 7)
 
483
 
 
484
  if (suc == 0 || badcnt < 7)
364
485
    {
365
486
      fprintf (stdout, _("ERROR incomplete replacement\n"));
366
487
    }
369
490
      fprintf (stdout, _("ERROR bad replacement\n"));
370
491
    }
371
492
  fflush(stdout);
372
 
  return 0;
 
493
  return EXIT_FAILURE;
373
494
}