~ubuntu-branches/ubuntu/lucid/openssl/lucid-proposed

« back to all changes in this revision

Viewing changes to crypto/rand/randfile.c

  • Committer: Bazaar Package Importer
  • Author(s): Kurt Roeckx
  • Date: 2009-06-13 18:15:46 UTC
  • mto: (11.1.5 squeeze)
  • mto: This revision was merged to the branch mainline in revision 34.
  • Revision ID: james.westby@ubuntu.com-20090613181546-vbfntai3b009dl1u
Tags: upstream-0.9.8k
ImportĀ upstreamĀ versionĀ 0.9.8k

Show diffs side-by-side

added added

removed removed

Lines of Context:
81
81
# include <sys/stat.h>
82
82
#endif
83
83
 
 
84
#ifdef _WIN32
 
85
#define stat    _stat
 
86
#define chmod   _chmod
 
87
#define open    _open
 
88
#define fdopen  _fdopen
 
89
#endif
 
90
 
84
91
#undef BUFSIZE
85
92
#define BUFSIZE 1024
86
93
#define RAND_DATA 1024
87
94
 
 
95
#ifdef OPENSSL_SYS_VMS
 
96
/* This declaration is a nasty hack to get around vms' extension to fopen
 
97
 * for passing in sharing options being disabled by our /STANDARD=ANSI89 */
 
98
static FILE *(*const vms_fopen)(const char *, const char *, ...) =
 
99
    (FILE *(*)(const char *, const char *, ...))fopen;
 
100
#define VMS_OPEN_ATTRS "shr=get,put,upd,del","ctx=bin,stm","rfm=stm","rat=none","mrs=0"
 
101
#endif
 
102
 
88
103
/* #define RFILE ".rnd" - defined in ../../e_os.h */
89
104
 
90
105
/* Note that these functions are intended for seed files only.
106
121
        RAND_add(&sb,sizeof(sb),0.0);
107
122
        if (bytes == 0) return(ret);
108
123
 
 
124
#ifdef OPENSSL_SYS_VMS
 
125
        in=vms_fopen(file,"rb",VMS_OPEN_ATTRS);
 
126
#else
109
127
        in=fopen(file,"rb");
 
128
#endif
110
129
        if (in == NULL) goto err;
111
130
#if defined(S_IFBLK) && defined(S_IFCHR)
112
131
        if (sb.st_mode & (S_IFBLK | S_IFCHR)) {
126
145
                        n = BUFSIZE;
127
146
                i=fread(buf,1,n,in);
128
147
                if (i <= 0) break;
 
148
#ifdef PURIFY
 
149
                RAND_add(buf,i,(double)i);
 
150
#else
129
151
                /* even if n != i, use the full array */
130
152
                RAND_add(buf,n,(double)i);
 
153
#endif
131
154
                ret+=i;
132
155
                if (bytes > 0)
133
156
                        {
163
186
#endif
164
187
        }
165
188
 
166
 
#if defined(O_CREAT) && !defined(OPENSSL_SYS_WIN32)
 
189
#if defined(O_CREAT) && !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_VMS)
167
190
        {
168
191
        /* For some reason Win32 can't write to files created this way */
169
192
        
174
197
                out = fdopen(fd, "wb");
175
198
        }
176
199
#endif
 
200
 
 
201
#ifdef OPENSSL_SYS_VMS
 
202
        /* VMS NOTE: Prior versions of this routine created a _new_
 
203
         * version of the rand file for each call into this routine, then
 
204
         * deleted all existing versions named ;-1, and finally renamed
 
205
         * the current version as ';1'. Under concurrent usage, this
 
206
         * resulted in an RMS race condition in rename() which could
 
207
         * orphan files (see vms message help for RMS$_REENT). With the
 
208
         * fopen() calls below, openssl/VMS now shares the top-level
 
209
         * version of the rand file. Note that there may still be
 
210
         * conditions where the top-level rand file is locked. If so, this
 
211
         * code will then create a new version of the rand file. Without
 
212
         * the delete and rename code, this can result in ascending file
 
213
         * versions that stop at version 32767, and this routine will then
 
214
         * return an error. The remedy for this is to recode the calling
 
215
         * application to avoid concurrent use of the rand file, or
 
216
         * synchronize usage at the application level. Also consider
 
217
         * whether or not you NEED a persistent rand file in a concurrent
 
218
         * use situation. 
 
219
         */
 
220
 
 
221
        out = vms_fopen(file,"rb+",VMS_OPEN_ATTRS);
 
222
        if (out == NULL)
 
223
                out = vms_fopen(file,"wb",VMS_OPEN_ATTRS);
 
224
#else
177
225
        if (out == NULL)
178
226
                out = fopen(file,"wb");
 
227
#endif
179
228
        if (out == NULL) goto err;
180
229
 
181
230
#ifndef NO_CHMOD
197
246
                ret+=i;
198
247
                if (n <= 0) break;
199
248
                }
200
 
#ifdef OPENSSL_SYS_VMS
201
 
        /* Try to delete older versions of the file, until there aren't
202
 
           any */
203
 
        {
204
 
        char *tmpf;
205
 
 
206
 
        tmpf = OPENSSL_malloc(strlen(file) + 4);  /* to add ";-1" and a nul */
207
 
        if (tmpf)
208
 
                {
209
 
                strcpy(tmpf, file);
210
 
                strcat(tmpf, ";-1");
211
 
                while(delete(tmpf) == 0)
212
 
                        ;
213
 
                rename(file,";1"); /* Make sure it's version 1, or we
214
 
                                      will reach the limit (32767) at
215
 
                                      some point... */
216
 
                }
217
 
        }
218
 
#endif /* OPENSSL_SYS_VMS */
219
249
 
220
250
        fclose(out);
221
251
        OPENSSL_cleanse(buf,BUFSIZE);