View | Details | Raw Unified | Return to bug 53830
Collapse All | Expand All

(-)container/catalina/src/share/org/apache/catalina/session/ManagerBase.java (-42 / +59 lines)
Lines 67-74 Link Here
67
67
68
    // ----------------------------------------------------- Instance Variables
68
    // ----------------------------------------------------- Instance Variables
69
69
70
    private static final String devRandomSourceDefault;
71
    static {
72
        // - Use the default value only if it is a Unix-like system
73
        // - Check that it exists 
74
        File f = new File("/dev/urandom");
75
        if (f.isAbsolute() && f.exists()) {
76
            devRandomSourceDefault = f.getPath();
77
        } else {
78
            devRandomSourceDefault = null;
79
        }
80
    }
81
70
    protected DataInputStream randomIS=null;
82
    protected DataInputStream randomIS=null;
71
    protected String devRandomSource="/dev/urandom";
83
    protected String devRandomSource = devRandomSourceDefault;
72
84
73
    /**
85
    /**
74
     * The default message digest algorithm to use if we cannot use
86
     * The default message digest algorithm to use if we cannot use
Lines 220-251 Link Here
220
232
221
233
222
    private class PrivilegedSetRandomFile implements PrivilegedAction{
234
    private class PrivilegedSetRandomFile implements PrivilegedAction{
235
236
        private final String s;
237
223
        public PrivilegedSetRandomFile(String s) {
238
        public PrivilegedSetRandomFile(String s) {
224
            devRandomSource = s;
239
            this.s = s;
225
        }
240
        }
226
241
227
        public Object run(){
242
        public Object run(){
228
            try {
243
            doSetRandomFile(s);
229
                File f=new File( devRandomSource );
244
            return null;
230
                if( ! f.exists() ) return null;
231
                randomIS= new DataInputStream( new FileInputStream(f));
232
                randomIS.readLong();
233
                if( log.isDebugEnabled() )
234
                    log.debug( "Opening " + devRandomSource );
235
                return randomIS;
236
            } catch (IOException ex){
237
                log.warn("Error reading " + devRandomSource, ex);
238
                if (randomIS != null) {
239
                    try {
240
                        randomIS.close();
241
                    } catch (Exception e) {
242
                        log.warn("Failed to close randomIS.");
243
                    }
244
                }
245
                devRandomSource = null;
246
                randomIS=null;
247
                return null;
248
            }
249
        }
245
        }
250
    }
246
    }
251
247
Lines 524-552 Link Here
524
        // as a hack, you can use a static file - and generate the same
520
        // as a hack, you can use a static file - and generate the same
525
        // session ids ( good for strange debugging )
521
        // session ids ( good for strange debugging )
526
        if (System.getSecurityManager() != null){
522
        if (System.getSecurityManager() != null){
527
            randomIS = (DataInputStream) AccessController
523
            AccessController.doPrivileged(new PrivilegedSetRandomFile(s));
528
                    .doPrivileged(new PrivilegedSetRandomFile(s));
529
        } else {
524
        } else {
530
            try{
525
            doSetRandomFile(s);
531
                devRandomSource=s;
526
        }
532
                File f=new File( devRandomSource );
527
    }
533
                if( ! f.exists() ) return;
528
534
                randomIS= new DataInputStream( new FileInputStream(f));
529
    private void doSetRandomFile(String s) {
535
                randomIS.readLong();
530
        DataInputStream is = null;
536
                if( log.isDebugEnabled() )
531
        try {
537
                    log.debug( "Opening " + devRandomSource );
532
            if (s == null || s.length() == 0) {
538
            } catch( IOException ex ) {
533
                return;
539
                log.warn("Error reading " + devRandomSource, ex);
534
            }
540
                if (randomIS != null) {
535
            File f = new File(s);
541
                    try {
536
            if( ! f.exists() ) return;
542
                        randomIS.close();
537
            if( log.isDebugEnabled() ) {
543
                    } catch (Exception e) {
538
                log.debug( "Opening " + s );
544
                        log.warn("Failed to close randomIS.");
539
            }
545
                    }
540
            is = new DataInputStream( new FileInputStream(f));
541
            is.readLong();
542
        } catch( IOException ex ) {
543
            log.warn("Error reading " + s, ex);
544
            if (is != null) {
545
                try {
546
                    is.close();
547
                } catch (Exception ex2) {
548
                    log.warn("Failed to close " + s, ex2);
546
                }
549
                }
550
                is = null;
551
            }
552
        } finally {
553
            DataInputStream oldIS = randomIS;
554
            if (is != null) {
555
                devRandomSource = s;
556
            } else {
547
                devRandomSource = null;
557
                devRandomSource = null;
548
                randomIS=null;
549
            }
558
            }
559
            randomIS = is;
560
            if (oldIS != null) {
561
                try {
562
                    oldIS.close();
563
                } catch (Exception ex) {
564
                    log.warn("Failed to close RandomIS", ex);
565
                }
566
            }
550
        }
567
        }
551
    }
568
    }
552
569
(-)container/webapps/docs/config/manager.xml (+14 lines)
Lines 157-162 Link Here
157
        <code>java.security.SecureRandom</code>.</p>
157
        <code>java.security.SecureRandom</code>.</p>
158
      </attribute>
158
      </attribute>
159
159
160
      <attribute name="randomFile" required="false">
161
        <p>Name of a file that provides random data that are used to generate
162
        session ids. If not specified, the default value is
163
        <code>/dev/urandom</code> on Unix-like systems (those where such
164
        file path is absolute) and empty on others.</p>
165
      </attribute>
166
160
      <attribute name="sessionIdLength" required="false">
167
      <attribute name="sessionIdLength" required="false">
161
       <p>The length of session ids created by this Manager, excluding any
168
       <p>The length of session ids created by this Manager, excluding any
162
        JVM route information used for load balancing. 
169
        JVM route information used for load balancing. 
Lines 263-268 Link Here
263
        <code>java.security.SecureRandom</code>.</p>
270
        <code>java.security.SecureRandom</code>.</p>
264
      </attribute>
271
      </attribute>
265
272
273
      <attribute name="randomFile" required="false">
274
        <p>Name of a file that provides random data that are used to generate
275
        session ids. If not specified, the default value is
276
        <code>/dev/urandom</code> on Unix-like systems (those where such
277
        file path is absolute) and empty on others.</p>
278
      </attribute>
279
266
      <attribute name="saveOnRestart" required="false">
280
      <attribute name="saveOnRestart" required="false">
267
        <p>Should all sessions be persisted and reloaded when Tomcat is shut
281
        <p>Should all sessions be persisted and reloaded when Tomcat is shut
268
        down and restarted (or when this application is reloaded)?  By default,
282
        down and restarted (or when this application is reloaded)?  By default,

Return to bug 53830