Create ~/.rnd with mode 0600 instead of 0666
[openssl.git] / crypto / rand / randfile.c
index c4e616077923a3fca2f6e5a881972365b8f0ae8b..7b4c7dc319e01467787ebc24ed62c389f41adb76 100644 (file)
  * [including the GNU Public Licence.]
  */
 
+#include "e_os.h"
+
 /* We need to define this to get macros like S_IFBLK and S_IFCHR */
+#if !defined(OPENSSL_SYS_VXWORKS)
 #define _XOPEN_SOURCE 500
+#endif
 
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
-#include "e_os.h"
 #include <openssl/crypto.h>
 #include <openssl/rand.h>
 #include <openssl/buffer.h>
 #endif
 #ifndef OPENSSL_NO_POSIX_IO
 # include <sys/stat.h>
+# include <fcntl.h>
+#endif
+
+#ifdef _WIN32
+#define stat   _stat
+#define chmod  _chmod
+#define open   _open
+#define fdopen _fdopen
 #endif
 
 #undef BUFSIZE
@@ -111,6 +122,14 @@ int RAND_load_file(const char *file, long bytes)
        if (file == NULL) return(0);
 
 #ifndef OPENSSL_NO_POSIX_IO
+#ifdef PURIFY
+       /* struct stat can have padding and unused fields that may not be
+        * initialized in the call to stat(). We need to clear the entire
+        * structure before calling RAND_add() to avoid complaints from
+        * applications such as Valgrind.
+        */
+       memset(&sb, 0, sizeof(sb));
+#endif
        if (stat(file,&sb) < 0) return(0);
        RAND_add(&sb,sizeof(sb),0.0);
 #endif
@@ -122,14 +141,16 @@ int RAND_load_file(const char *file, long bytes)
        in=fopen(file,"rb");
 #endif
        if (in == NULL) goto err;
-#if defined(S_IFBLK) && defined(S_IFCHR) && !defined(OPNESSL_NO_POSIX_IO)
+#if defined(S_IFBLK) && defined(S_IFCHR) && !defined(OPENSSL_NO_POSIX_IO)
        if (sb.st_mode & (S_IFBLK | S_IFCHR)) {
          /* this file is a device. we don't want read an infinite number
           * of bytes from a random device, nor do we want to use buffered
           * I/O because we will waste system entropy. 
           */
          bytes = (bytes == -1) ? 2048 : bytes; /* ok, is 2048 enough? */
+#ifndef OPENSSL_NO_SETVBUF_IONBF
          setvbuf(in, NULL, _IONBF, 0); /* don't do buffered reads */
+#endif /* ndef OPENSSL_NO_SETVBUF_IONBF */
        }
 #endif
        for (;;)
@@ -170,8 +191,8 @@ int RAND_write_file(const char *file)
        
        i=stat(file,&sb);
        if (i != -1) { 
-#if defined(S_IFBLK) && defined(S_IFCHR)
-         if (sb.st_mode & (S_IFBLK | S_IFCHR)) {
+#if defined(S_ISBLK) && defined(S_ISCHR)
+         if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
            /* this file is a device. we don't write back to it. 
             * we "succeed" on the assumption this is some sort 
             * of random device. Otherwise attempting to write to 
@@ -254,7 +275,6 @@ err:
 const char *RAND_file_name(char *buf, size_t size)
        {
        char *s=NULL;
-       int ok = 0;
 #ifdef __OpenBSD__
        struct stat sb;
 #endif
@@ -283,7 +303,6 @@ const char *RAND_file_name(char *buf, size_t size)
                        BUF_strlcat(buf,"/",size);
 #endif
                        BUF_strlcat(buf,RFILE,size);
-                       ok = 1;
                        }
                else
                        buf[0] = '\0'; /* no file name */
@@ -297,7 +316,7 @@ const char *RAND_file_name(char *buf, size_t size)
         * to something hopefully decent if that isn't available. 
         */
 
-       if (!ok)
+       if (!buf[0])
                if (BUF_strlcpy(buf,"/dev/arandom",size) >= size) {
                        return(NULL);
                }