randfile.c: .rnd can become orphaned on VMS.
[openssl.git] / crypto / rand / randfile.c
index fd903528485e1e76aede5ef9503a7db8b40499fe..c4e616077923a3fca2f6e5a881972365b8f0ae8b 100644 (file)
@@ -57,7 +57,7 @@
  */
 
 /* We need to define this to get macros like S_IFBLK and S_IFCHR */
-#define _XOPEN_SOURCE 1
+#define _XOPEN_SOURCE 500
 
 #include <errno.h>
 #include <stdio.h>
@@ -75,9 +75,7 @@
 #ifndef NO_SYS_TYPES_H
 # include <sys/types.h>
 #endif
-#ifdef MAC_OS_pre_X
-# include <stat.h>
-#else
+#ifndef OPENSSL_NO_POSIX_IO
 # include <sys/stat.h>
 #endif
 
 #define BUFSIZE        1024
 #define RAND_DATA 1024
 
+#ifdef OPENSSL_SYS_VMS
+/* This declaration is a nasty hack to get around vms' extension to fopen
+ * for passing in sharing options being disabled by our /STANDARD=ANSI89 */
+static FILE *(*const vms_fopen)(const char *, const char *, ...) =
+    (FILE *(*)(const char *, const char *, ...))fopen;
+#define VMS_OPEN_ATTRS "shr=get,put,upd,del","ctx=bin,stm","rfm=stm","rat=none","mrs=0"
+#endif
+
 /* #define RFILE ".rnd" - defined in ../../e_os.h */
 
 /* Note that these functions are intended for seed files only.
@@ -96,21 +102,27 @@ int RAND_load_file(const char *file, long bytes)
         * if bytes == -1, read complete file. */
 
        MS_STATIC unsigned char buf[BUFSIZE];
+#ifndef OPENSSL_NO_POSIX_IO
        struct stat sb;
+#endif
        int i,ret=0,n;
        FILE *in;
 
        if (file == NULL) return(0);
 
-       i=stat(file,&sb);
-       /* If the state fails, put some crap in anyway */
+#ifndef OPENSSL_NO_POSIX_IO
+       if (stat(file,&sb) < 0) return(0);
        RAND_add(&sb,sizeof(sb),0.0);
-       if (i < 0) return(0);
+#endif
        if (bytes == 0) return(ret);
 
+#ifdef OPENSSL_SYS_VMS
+       in=vms_fopen(file,"rb",VMS_OPEN_ATTRS);
+#else
        in=fopen(file,"rb");
+#endif
        if (in == NULL) goto err;
-#if defined(S_IFBLK) && defined(S_IFCHR)
+#if defined(S_IFBLK) && defined(S_IFCHR) && !defined(OPNESSL_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
@@ -128,8 +140,12 @@ int RAND_load_file(const char *file, long bytes)
                        n = BUFSIZE;
                i=fread(buf,1,n,in);
                if (i <= 0) break;
+#ifdef PURIFY
+               RAND_add(buf,i,(double)i);
+#else
                /* even if n != i, use the full array */
                RAND_add(buf,n,(double)i);
+#endif
                ret+=i;
                if (bytes > 0)
                        {
@@ -149,6 +165,7 @@ int RAND_write_file(const char *file)
        int i,ret=0,rand_err=0;
        FILE *out = NULL;
        int n;
+#ifndef OPENSSL_NO_POSIX_IO
        struct stat sb;
        
        i=stat(file,&sb);
@@ -164,18 +181,48 @@ int RAND_write_file(const char *file)
          }
 #endif
        }
+#endif
 
-#if defined(O_CREAT) && !defined(OPENSSL_SYS_WIN32)
-       /* For some reason Win32 can't write to files created this way */
-       
+#if defined(O_CREAT) && !defined(OPENSSL_NO_POSIX_IO) && !defined(OPENSSL_SYS_VMS)
+       {
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
        /* chmod(..., 0600) is too late to protect the file,
         * permissions should be restrictive from the start */
-       int fd = open(file, O_CREAT, 0600);
+       int fd = open(file, O_WRONLY|O_CREAT|O_BINARY, 0600);
        if (fd != -1)
                out = fdopen(fd, "wb");
+       }
 #endif
+
+#ifdef OPENSSL_SYS_VMS
+       /* VMS NOTE: Prior versions of this routine created a _new_
+        * version of the rand file for each call into this routine, then
+        * deleted all existing versions named ;-1, and finally renamed
+        * the current version as ';1'. Under concurrent usage, this
+        * resulted in an RMS race condition in rename() which could
+        * orphan files (see vms message help for RMS$_REENT). With the
+        * fopen() calls below, openssl/VMS now shares the top-level
+        * version of the rand file. Note that there may still be
+        * conditions where the top-level rand file is locked. If so, this
+        * code will then create a new version of the rand file. Without
+        * the delete and rename code, this can result in ascending file
+        * versions that stop at version 32767, and this routine will then
+        * return an error. The remedy for this is to recode the calling
+        * application to avoid concurrent use of the rand file, or
+        * synchronize usage at the application level. Also consider
+        * whether or not you NEED a persistent rand file in a concurrent
+        * use situation. 
+        */
+
+       out = vms_fopen(file,"rb+",VMS_OPEN_ATTRS);
+       if (out == NULL)
+               out = vms_fopen(file,"wb",VMS_OPEN_ATTRS);
+#else
        if (out == NULL)
                out = fopen(file,"wb");
+#endif
        if (out == NULL) goto err;
 
 #ifndef NO_CHMOD
@@ -197,25 +244,6 @@ int RAND_write_file(const char *file)
                ret+=i;
                if (n <= 0) break;
                 }
-#ifdef OPENSSL_SYS_VMS
-       /* Try to delete older versions of the file, until there aren't
-          any */
-       {
-       char *tmpf;
-
-       tmpf = OPENSSL_malloc(strlen(file) + 4);  /* to add ";-1" and a nul */
-       if (tmpf)
-               {
-               strcpy(tmpf, file);
-               strcat(tmpf, ";-1");
-               while(delete(tmpf) == 0)
-                       ;
-               rename(file,";1"); /* Make sure it's version 1, or we
-                                     will reach the limit (32767) at
-                                     some point... */
-               }
-       }
-#endif /* OPENSSL_SYS_VMS */
 
        fclose(out);
        OPENSSL_cleanse(buf,BUFSIZE);