many systems don't have /etc/shadow but do have another system for
[jra/samba/.git] / source3 / lib / genrand.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4
5    Functions to create reasonable random numbers for crypto use.
6
7    Copyright (C) Jeremy Allison 1998
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 extern int DEBUGLEVEL;
27 static uint32 counter = 0;
28
29
30 /****************************************************************
31 get a 16 byte hash from the contents of a file
32 Note that the hash is not initialised.
33 *****************************************************************/
34 static void do_filehash(char *fname, unsigned char *hash)
35 {
36         unsigned char buf[1011]; /* deliberate weird size */
37         unsigned char tmp_md4[16];
38         int fd, n;
39
40         fd = open(fname,O_RDONLY);
41         if (fd == -1) return;
42
43         while ((n = read(fd, (char *)buf, sizeof(buf))) > 0) {
44                 mdfour(tmp_md4, buf, n);
45                 for (n=0;n<16;n++)
46                         hash[n] ^= tmp_md4[n];
47         }
48         close(fd);
49 }
50
51
52
53 /****************************************************************
54  Try and get a seed by looking at the atimes of files in a given
55  directory. XOR them into the buf array.
56 *****************************************************************/
57
58 static void do_dirrand(char *name, unsigned char *buf, int buf_len)
59 {
60   void *dp = sys_opendir(name);
61   pstring fullname;
62   int len_left;
63   int fullname_len;
64   char *pos;
65
66   pstrcpy(fullname, name);
67   fullname_len = strlen(fullname);
68
69   if(fullname_len + 2 > sizeof(pstring))
70     return;
71
72   if(fullname[fullname_len] != '/') {
73     fullname[fullname_len] = '/';
74     fullname[fullname_len+1] = '\0';
75     fullname_len = strlen(fullname);
76   }
77
78   len_left = sizeof(pstring) - fullname_len - 1;
79   pos = &fullname[fullname_len];
80
81   if(dp != NULL) {
82     char *p;
83
84     while ((p = readdirname(dp))) {           
85       struct stat st;
86
87       if(strlen(p) <= len_left)
88         strcpy(pos, p);
89
90       if(sys_stat(fullname,&st) == 0) {
91         SIVAL(buf, ((counter * 4)%(buf_len-4)), 
92               IVAL(buf,((counter * 4)%(buf_len-4))) ^ st.st_atime);
93         counter++;
94         DEBUG(10,("do_dirrand: value from file %s.\n", fullname));
95       }
96     }
97     closedir(dp); 
98   }
99 }
100
101 /**************************************************************
102  Try and get a good random number seed. Try a number of
103  different factors. Firstly, try /dev/random and try and
104  read from this. If this fails iterate through /tmp and
105  XOR all the file timestamps. If this fails then just use
106  a combination of pid and time of day (yes I know this
107  sucks :-). Finally md4 the result.
108
109  The result goes in a 16 byte buffer passed from the caller
110 **************************************************************/
111
112 static void do_reseed(unsigned char *md4_outbuf)
113 {
114   unsigned char md4_inbuf[40];
115   BOOL got_random = False;
116   uint32 v1, v2;
117   int fd;
118   struct timeval tval;
119   pid_t mypid;
120   struct passwd *pw;
121
122   memset(md4_inbuf, '\0', sizeof(md4_inbuf));
123
124   fd = open( "/dev/random", O_RDONLY);
125   if(fd >= 0) {
126     /* 
127      * We can use /dev/random !
128      */
129     if(read(fd, md4_inbuf, 40) == 40) {
130       got_random = True;
131       DEBUG(10,("do_reseed: got 40 bytes from /dev/random.\n"));
132     }
133     close(fd);
134   }
135
136   if(!got_random) {
137     /*
138      * /dev/random failed - try /tmp/ for timestamps.
139      */
140     do_dirrand("/tmp", md4_inbuf, sizeof(md4_inbuf));
141     do_dirrand("/dev", md4_inbuf, sizeof(md4_inbuf));
142   }
143
144   /* possibly add in some secret file contents */
145   do_filehash("/etc/shadow", &md4_inbuf[0]);
146   do_filehash(SMB_PASSWD_FILE, &md4_inbuf[16]);
147
148   /* add in the root encrypted password. On any system where security is taken
149      seriously this will be secret */
150   pw = getpwnam("root");
151   if (pw) {
152           int i;
153           unsigned char md4_tmp[16];
154           mdfour(md4_tmp, pw->pw_passwd, strlen(pw->pw_passwd));
155           for (i=0;i<16;i++)
156                   md4_inbuf[8+i] ^= md4_tmp[i];
157   }
158
159   /*
160    * Finally add the counter, time of day, and pid.
161    */
162   GetTimeOfDay(&tval);
163   mypid = getpid();
164   v1 = (counter++) + mypid + tval.tv_sec;
165   v2 = (counter++) * mypid + tval.tv_usec;
166
167   SIVAL(md4_inbuf, 32, v1 ^ IVAL(md4_inbuf, 32));
168   SIVAL(md4_inbuf, 36, v2 ^ IVAL(md4_inbuf, 36));
169
170   mdfour(md4_outbuf, md4_inbuf, sizeof(md4_inbuf));
171 }
172
173 /*******************************************************************
174  Interface to the (hopefully) good crypto random number generator.
175 ********************************************************************/
176
177 void generate_random_buffer( unsigned char *out, int len, BOOL re_seed)
178 {
179   static BOOL done_reseed = False;
180   unsigned char tmp_buf[16];
181   unsigned char md4_buf[16];
182   unsigned char *p;
183
184   if(!done_reseed || re_seed) {
185           do_reseed(md4_buf);
186           done_reseed = True;
187   }
188
189   /*
190    * Generate random numbers in chunks of 64 bytes,
191    * then md4 them & copy to the output buffer.
192    */
193
194   p = out;
195   while(len > 0) {
196     int copy_len = len > 16 ? 16 : len;
197     mdfour(tmp_buf, md4_buf, sizeof(md4_buf));
198     memcpy(md4_buf, tmp_buf, sizeof(md4_buf));
199     memcpy(p, tmp_buf, copy_len);
200     p += copy_len;
201     len -= copy_len;
202   }
203 }