Home | Trees | Indices | Help |
---|
|
1 #!/usr/bin/python 2 # -*- coding: ascii -*- 3 # Copyright (C) 2008 Dwayne C. Litzenberger <dlitz@dlitz.net> 4 # Copyright (C) 2008 Open Systems Canada Limited 5 # 6 # This file is part of paramiko. 7 # 8 # Paramiko is free software; you can redistribute it and/or modify it under the 9 # terms of the GNU Lesser General Public License as published by the Free 10 # Software Foundation; either version 2.1 of the License, or (at your option) 11 # any later version. 12 # 13 # Paramiko is distrubuted in the hope that it will be useful, but WITHOUT ANY 14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 15 # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 # details. 17 # 18 # You should have received a copy of the GNU Lesser General Public License 19 # along with Paramiko; if not, write to the Free Software Foundation, Inc., 20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 22 import os 23 import stat 24 27 4042 """Open /dev/urandom and perform some sanity checks.""" 43 44 f = None 45 g = None 46 47 if device_path is None: 48 device_path = "/dev/urandom" 49 50 try: 51 # Try to open /dev/urandom now so that paramiko will be able to access 52 # it even if os.chroot() is invoked later. 53 try: 54 f = open(device_path, "rb", 0) 55 except EnvironmentError: 56 raise error("Unable to open /dev/urandom") 57 58 # Open a second file descriptor for sanity checking later. 59 try: 60 g = open(device_path, "rb", 0) 61 except EnvironmentError: 62 raise error("Unable to open /dev/urandom") 63 64 # Check that /dev/urandom is a character special device, not a regular file. 65 st = os.fstat(f.fileno()) # f 66 if stat.S_ISREG(st.st_mode) or not stat.S_ISCHR(st.st_mode): 67 raise error("/dev/urandom is not a character special device") 68 69 st = os.fstat(g.fileno()) # g 70 if stat.S_ISREG(st.st_mode) or not stat.S_ISCHR(st.st_mode): 71 raise error("/dev/urandom is not a character special device") 72 73 # Check that /dev/urandom always returns the number of bytes requested 74 x = f.read(20) 75 y = g.read(20) 76 if len(x) != 20 or len(y) != 20: 77 raise error("Error reading from /dev/urandom: input truncated") 78 79 # Check that different reads return different data 80 if x == y: 81 raise error("/dev/urandom is broken; returning identical data: %r == %r" % (x, y)) 82 83 # Close the duplicate file object 84 g.close() 85 86 # Return the first file object 87 return _RNG(f) 88 89 except error: 90 if f is not None: 91 f.close() 92 if g is not None: 93 g.close() 94 raise95 96 # vim:set ts=4 sw=4 sts=4 expandtab: 97
Home | Trees | Indices | Help |
---|
Generated by Epydoc 3.0.1 on Sun Nov 1 22:14:18 2009 | http://epydoc.sourceforge.net |