genrand.c: Improved filename based random seed generation.
authorJeremy Allison <jra@samba.org>
Mon, 20 Apr 1998 23:57:29 +0000 (23:57 +0000)
committerJeremy Allison <jra@samba.org>
Mon, 20 Apr 1998 23:57:29 +0000 (23:57 +0000)
lib/rpc/server/srv_netlog.c: Changed to use generate_random_buffer().
Jeremy.

source/lib/genrand.c
source/rpc_server/srv_netlog.c

index b26269f091e03ddd7b9cc6a7934cdc75701648bd..5808206f6b590888d3100b4717e671d1252059f0 100644 (file)
 #include "includes.h"
 
 extern int DEBUGLEVEL;
+static uint32 counter = 0;
+
+/****************************************************************
+ Try and get a seed by looking at the atimes of files in a given
+ directory. XOR them into the buf array.
+*****************************************************************/
+
+static void do_dirrand(char *name, unsigned char *buf, int buf_len)
+{
+  void *dp = sys_opendir(name);
+  pstring fullname;
+  int len_left;
+  int fullname_len;
+  char *pos;
+
+  pstrcpy(fullname, name);
+  fullname_len = strlen(fullname);
+
+  if(fullname_len + 2 > sizeof(pstring))
+    return;
+
+  if(fullname[fullname_len] != '/') {
+    fullname[fullname_len] = '/';
+    fullname[fullname_len+1] = '\0';
+    fullname_len = strlen(fullname);
+  }
+
+  len_left = sizeof(pstring) - fullname_len - 1;
+  pos = &fullname[fullname_len];
+
+  if(dp != NULL) {
+    char *p;
+
+    while ((p = readdirname(dp))) {           
+      struct stat st;
+
+      if(strlen(p) <= len_left)
+        strcpy(pos, p);
+
+      if(sys_stat(fullname,&st) == 0) {
+        SIVAL(buf, ((counter * 4)%(buf_len-4)), 
+              IVAL(buf,((counter * 4)%(buf_len-4))) ^ st.st_atime);
+        counter++;
+        DEBUG(10,("do_dirrand: value from file %s.\n", fullname));
+      }
+    }
+    closedir(dp); 
+  }
+}
 
 /**************************************************************
  Try and get a good random number seed. Try a number of
@@ -36,13 +85,13 @@ extern int DEBUGLEVEL;
 
 static uint32 do_reseed(void)
 {
-  static int counter = 0;
   unsigned char md4_outbuf[16];
   unsigned char md4_inbuf[40];
   BOOL got_random = False;
   uint32 v1, v2, ret;
   int fd;
   struct timeval tval;
+  pid_t mypid;
 
   memset(md4_inbuf, '\0', sizeof(md4_inbuf));
 
@@ -62,29 +111,17 @@ static uint32 do_reseed(void)
     /*
      * /dev/random failed - try /tmp/ for timestamps.
      */
-    void *dp = sys_opendir("/tmp");
-
-    if(dp != NULL) {
-      char *p;
-
-      while ((p = readdirname(dp))) {           
-        struct stat st;
-        if(sys_stat(p,&st) != 0)
-          SIVAL(md4_inbuf, ((counter%sizeof(md4_inbuf))/4), 
-                IVAL(md4_inbuf,((counter%sizeof(md4_inbuf))/4)) ^ st.st_atime);
-          counter++;
-          DEBUG(10,("do_reseed: value from file %s.\n", p));
-        }
-      }
-    closedir(dp); 
+    do_dirrand("/tmp", md4_inbuf, sizeof(md4_inbuf));
+    do_dirrand("/dev", md4_inbuf, sizeof(md4_inbuf));
   }
 
   /*
    * Finally add the counter, time of day, and pid.
    */
   GetTimeOfDay(&tval);
-  v1 = (counter++) + getpid() + tval.tv_sec;
-  v2 = (counter++) * getpid() + tval.tv_usec;
+  mypid = getpid();
+  v1 = (counter++) + mypid + tval.tv_sec;
+  v2 = (counter++) * mypid + tval.tv_usec;
 
   SIVAL(md4_inbuf, 32, v1 ^ IVAL(md4_inbuf, 32));
   SIVAL(md4_inbuf, 36, v1 ^ IVAL(md4_inbuf, 36));
index c8386d47244844d1fef7086dc62fa67b37944ea2..cbe35e5202b02405ce835457858f49cbe365bfc3 100644 (file)
@@ -289,10 +289,9 @@ static void api_net_req_chal( int uid,
                memcpy(vuser->dc.clnt_cred.challenge.data, q_r.clnt_chal.data, sizeof(q_r.clnt_chal.data));
 
                /* create a server challenge for the client */
-               /* PAXX: set these to random values. */
-               /* lkcl: paul, you mentioned that it doesn't really matter much */
-               SIVAL(vuser->dc.srv_chal.data, 0, 0x11111111);
-               SIVAL(vuser->dc.srv_chal.data, 4, 0x11111111);
+               /* Set these to random values. */
+                generate_random_buffer(vuser->dc.srv_chal.data, 8, False);
+
                memcpy(vuser->dc.srv_cred.challenge.data, vuser->dc.srv_chal.data, 8);
 
                bzero(vuser->dc.sess_key, sizeof(vuser->dc.sess_key));