summaryrefslogtreecommitdiffstats
path: root/misc-utils/mcookie.c
blob: 33ae7630e1c052d55173022b39b2f1c1c956bac0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* mcookie.c -- Generates random numbers for xauth
 * Created: Fri Feb  3 10:42:48 1995 by faith@cs.unc.edu
 * Revised: Mon Sep 25 23:44:43 1995 by r.faith@ieee.org
 * Public Domain 1995 Rickard E. Faith (faith@cs.unc.edu)
 * This program comes with ABSOLUTELY NO WARRANTY.
 * 
 * $Id: mcookie.c,v 1.5 1997/07/06 00:13:06 aebr Exp $
 *
 * This program gathers some random bits of data and used the MD5
 * message-digest algorithm to generate a 128-bit hexadecimal number for
 * use with xauth(1).
 *
 * NOTE: Unless /dev/random is available, this program does not actually
 * gather 128 bits of random information, so the magic cookie generated
 * will be considerably easier to guess than one might expect.
 */

#ifdef __linux__
#define HAVE_GETTIMEOFDAY 1
#endif

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include "md5.h"
#if HAVE_GETTIMEOFDAY
#include <sys/time.h>
#include <unistd.h>
#endif

#define MAXBUFFERSIZE 512

struct rngs {
   const char *path;
   int        length;
} rngs[] = {
   { "/dev/random",              16 },
   { "/dev/urandom",            128 },
   { "/proc/stat",    MAXBUFFERSIZE },
   { "/proc/loadavg", MAXBUFFERSIZE },
   { "/dev/audio",    MAXBUFFERSIZE },
};
#define RNGS (sizeof(rngs)/sizeof(struct rngs))

int Verbose = 0;

int main( int argc, char **argv )
{
   int               i;
   struct MD5Context ctx;
   unsigned char     digest[16];
   unsigned char     buf[MAXBUFFERSIZE];
   int               fd;
   int               c;
   pid_t             pid;
   char              *file = NULL;
   int               r;
#if HAVE_GETTIMEOFDAY
   struct timeval    tv;
   struct timezone   tz;
#else
   long int          t;
#endif

   while ((c = getopt( argc, argv, "vf:" )) != EOF)
      switch (c) {
      case 'v': ++Verbose;     break;
      case 'f': file = optarg; break;
      }

   MD5Init( &ctx );
   
#if HAVE_GETTIMEOFDAY
   gettimeofday( &tv, &tz );
   MD5Update( &ctx, (unsigned char *)&tv, sizeof( tv ) );
#else
   time( &t );
   MD5Update( &ctx, (unsigned char *)&t, sizeof( t ) );
#endif
   pid = getppid();
   MD5Update( &ctx, (unsigned char *)&pid, sizeof( pid ));
   pid = getpid();
   MD5Update( &ctx, (unsigned char *)&pid, sizeof( pid ));

   if (file) {
      int count = 0;
      
      if (file[0] == '-' && !file[1]) fd = fileno(stdin);
      else if ((fd = open( file, O_RDONLY )) <0) {
	 fprintf( stderr, "Could not open %s\n", file );
      }

      while ((r = read( fd, buf, sizeof( buf ) )) > 0) {
	 MD5Update( &ctx, buf, r );
	 count += r;
      }
      if (Verbose)
	 fprintf( stderr, "Got %d bytes from %s\n", count, file );
      
      if (file[0] != '-' || file[1]) close( fd );
   }

   for (i = 0; i < RNGS; i++) {
      if ((fd = open( rngs[i].path, O_RDONLY|O_NONBLOCK )) >= 0) {
	 r = read( fd, buf, sizeof( buf ) );
	 if (r > 0)
	    MD5Update( &ctx, buf, r );
	 else
	    r = 0;
	 close( fd );
	 if (Verbose)
	    fprintf( stderr, "Got %d bytes from %s\n", r, rngs[i].path );
	 if (r >= rngs[i].length) break;
      } else if (Verbose)
	 fprintf( stderr, "Could not open %s\n", rngs[i].path );
   }

   MD5Final( digest, &ctx );
   for (i = 0; i < 16; i++) printf( "%02x", digest[i] );
   putchar ( '\n' );
   
   return 0;
}