[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[minion-cvs] Bugfixes for armoring, refactor to use readFile/WriteFi...



Update of /home/minion/cvsroot/src/minion/lib/mixminion/server
In directory moria.mit.edu:/tmp/cvs-serv15318/lib/mixminion/server

Modified Files:
	EventStats.py HashLog.py Modules.py ServerKeys.py 
	ServerMain.py ServerQueue.py 
Log Message:
Bugfixes for armoring, refactor to use readFile/WriteFile wrappers.

Common:
- Add "tryUnlink" function to perform common unlink-this-file-if-you-can
  operation.
- Debug armorText and unarmorText.
- Document writeFile and readFile

Packet:
- Remove dead comment.

test: 
- Remove redundant readFile and writeFile
- Add tests for armorText and unarmorText.

ClientMain, Common, ServerInbox, ServerList, EventStats, HashLogs, Modules,
   ServerKeys, ServerMain, ServerQueue:
- Use writeFile, readFile, tryUnlink where possible.
- Avoid "except" without "raise" or sys.exc_info()
- Avoid "except OSError" without checking errno or displaying error.


Index: EventStats.py
===================================================================
RCS file: /home/minion/cvsroot/src/minion/lib/mixminion/server/EventStats.py,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- EventStats.py	17 May 2003 00:08:44 -0000	1.4
+++ EventStats.py	5 Jun 2003 05:24:23 -0000	1.5
@@ -12,7 +12,7 @@
 from time import time
 
 from mixminion.Common import formatTime, LOG, previousMidnight, floorDiv, \
-     createPrivateDir, MixError, readPickled, writePickled
+     createPrivateDir, MixError, readPickled, tryUnlink, writePickled
 
 # _EVENTS: a list of all recognized event types.
 _EVENTS = [ 'ReceivedPacket',
@@ -161,10 +161,7 @@
         LOG.debug("Syncing statistics to disk")
         if not now: now = time()
         tmpfile = self.filename + "_tmp"
-        try:
-            os.unlink(tmpfile)
-        except:
-            pass
+        tryUnlink(tmpfile)
         self.accumulatedTime += int(now-self.lastSave)
         self.lastSave = now
         writePickled(self.filename, { 'count' : self.count,

Index: HashLog.py
===================================================================
RCS file: /home/minion/cvsroot/src/minion/lib/mixminion/server/HashLog.py,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -d -r1.12 -r1.13
--- HashLog.py	30 May 2003 14:02:05 -0000	1.12
+++ HashLog.py	5 Jun 2003 05:24:23 -0000	1.13
@@ -7,11 +7,13 @@
    PacketHandler to prevent replay attacks."""
 
 import binascii
+import errno
 import os
 import stat
 import anydbm, dumbdbm
 import threading
-from mixminion.Common import MixFatalError, LOG, createPrivateDir
+from mixminion.Common import MixFatalError, LOG, createPrivateDir, readFile, \
+     tryUnlink
 from mixminion.Packet import DIGEST_LEN
 
 __all__ = [ 'HashLog' ]
@@ -87,11 +89,14 @@
         # Catch empty logfiles: these can be created if we exit before
         # syncing the log for the first time.
         try:
-            if os.stat(filename)[stat.ST_SIZE] == 0:
-                LOG.warn("Half-created database %s found; cleaning up.")
-                os.unlink(filename)
-        except os.error:
-            pass
+            st = os.stat(filename)
+        except OSError, e:
+            if e.errno != errno.ENOENT:
+                raise
+            st = None
+        if st and st[stat.ST_SIZE] == 0:
+            LOG.warn("Half-created database %s found; cleaning up.")
+            tryUnlink(filename)
 
         LOG.debug("Opening database %s for packet digests", filename)
         self.log = anydbm.open(filename, 'c')
@@ -108,11 +113,9 @@
         self.journalFileName = filename+"_jrnl"
         self.journal = {}
         if os.path.exists(self.journalFileName):
-            f = open(self.journalFileName, 'r')
-            j = f.read()
+            j = readFile(self.journalFileName, 1)
             for i in xrange(0, len(j), DIGEST_LEN):
                 self.journal[j[i:i+DIGEST_LEN]] = 1
-            f.close()
 
         self.journalFile = os.open(self.journalFileName,
                     _JOURNAL_OPEN_FLAGS|os.O_APPEND, 0600)

Index: Modules.py
===================================================================
RCS file: /home/minion/cvsroot/src/minion/lib/mixminion/server/Modules.py,v
retrieving revision 1.39
retrieving revision 1.40
diff -u -d -r1.39 -r1.40
--- Modules.py	28 May 2003 06:37:43 -0000	1.39
+++ Modules.py	5 Jun 2003 05:24:23 -0000	1.40
@@ -34,7 +34,7 @@
 from mixminion.Config import ConfigError, _parseBoolean, _parseCommand, \
      _parseIntervalList
 from mixminion.Common import LOG, createPrivateDir, MixError, isSMTPMailbox, \
-     isPrintingAscii, waitForChildren
+     isPrintingAscii, readFile, waitForChildren
 from mixminion.Packet import ParseError, CompressedDataTooLong
 
 # Return values for processMessage
@@ -174,28 +174,28 @@
     def _deliverMessages(self, msgList):
         for handle, packet in msgList:
             try:
-                EventStats.log.attemptedDelivery() #XXXX
+                EventStats.log.attemptedDelivery() #FFFF
                 result = self.module.processMessage(packet)
                 if result == DELIVER_OK:
                     LOG.debug("Successfully delivered message MOD:%s", handle)
                     self.deliverySucceeded(handle)
-                    EventStats.log.successfulDelivery() #XXXX
+                    EventStats.log.successfulDelivery() #FFFF
                 elif result == DELIVER_FAIL_RETRY:
                     LOG.debug("Unable to deliver message MOD:%s; will retry",
                               handle)
                     self.deliveryFailed(handle, 1)
-                    EventStats.log.failedDelivery() #XXXX
+                    EventStats.log.failedDelivery() #FFFF
                 else:
                     assert result == DELIVER_FAIL_NORETRY
                     LOG.error("Unable to deliver message MOD:%s; giving up",
                               handle)
                     self.deliveryFailed(handle, 0)
-                    EventStats.log.unretriableDelivery() #XXXX
+                    EventStats.log.unretriableDelivery() #FFFF
             except:
                 LOG.error_exc(sys.exc_info(),
                                    "Exception delivering message")
                 self.deliveryFailed(handle, 0)
-                EventStats.log.unretriableDelivery() #XXXX
+                EventStats.log.unretriableDelivery() #FFFF
 
 class DeliveryThread(threading.Thread):
     """A thread object used by ModuleManager to send messages in the
@@ -503,9 +503,7 @@
     def __init__(self, fname=None, string=None, includeStr="deny"):
         """Read the address set from a file or a string."""
         if string is None:
-            f = open(fname, 'r')
-            string = f.read()
-            f.close()
+            string = readFile(fname)
 
         self.addresses = {}
         self.domains = {}
@@ -680,26 +678,26 @@
         # Parse the address file.
         self.addresses = {}
         f = open(self.addressFile)
-        address_line_re = re.compile(r'\s*([^\s:=]+)\s*[:=]\s*(\S+)')
         try:
-            lineno = 0
-            while 1:
-                line = f.readline()
-                if not line:
-                    break
-                line = line.strip()
-                lineno += 1
-                if line == '' or line[0] == '#':
-                    continue
-                m = address_line_re.match(line)
-                if not m:
-                    raise ConfigError("Bad address on line %s of %s"%(
-                        lineno,self.addressFile))
-                self.addresses[m.group(1)] = m.group(2)
-                LOG.trace("Mapping MBOX address %s -> %s", m.group(1),
-                               m.group(2))
+            lines = f.readlines()
         finally:
             f.close()
+        
+        address_line_re = re.compile(r'([^\s:=]+)\s*[:=]\s*(\S+)')
+
+        lineno = 0
+        for line in lines:
+            line = line.strip()
+            lineno += 1
+            if line == '' or line[0] == '#':
+                continue
+            m = address_line_re.match(line)
+            if not m:
+                raise ConfigError("Bad address on line %s of %s"%(
+                    lineno,self.addressFile))
+            self.addresses[m.group(1)] = m.group(2)
+            LOG.trace("Mapping MBOX address %s -> %s", m.group(1),
+                           m.group(2))
 
         moduleManager.enableModule(self)
 

Index: ServerKeys.py
===================================================================
RCS file: /home/minion/cvsroot/src/minion/lib/mixminion/server/ServerKeys.py,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -d -r1.37 -r1.38
--- ServerKeys.py	31 May 2003 12:59:12 -0000	1.37
+++ ServerKeys.py	5 Jun 2003 05:24:23 -0000	1.38
@@ -11,6 +11,7 @@
             "generateCertChain" ]
 
 import os
+import errno
 import socket
 import re
 import sys
@@ -28,11 +29,10 @@
 
 from mixminion.ServerInfo import ServerInfo, PACKET_KEY_BYTES, MMTP_KEY_BYTES,\
      signServerInfo
-
 from mixminion.Common import AtomicFile, LOG, MixError, MixFatalError, \
-     ceilDiv, createPrivateDir, \
-     checkPrivateFile, formatBase64, formatDate, formatTime, previousMidnight,\
-     secureDelete, writeFile
+     ceilDiv, createPrivateDir, checkPrivateFile, formatBase64, formatDate, \
+     formatTime, previousMidnight, readFile, secureDelete, tryUnlink, \
+     writeFile
 
 #----------------------------------------------------------------------
 
@@ -594,27 +594,18 @@
         return self.published
     def markAsPublished(self):
         """DOCDOC"""
-        f = open(self.publishedFile, 'w')
-        try:
-            f.write(formatTime(time.time(), 1))
-            f.write("\n")
-        finally:
-            f.close()
+        contents = "%s\n"%formatTime(time.time(),1)
+        writeFile(self.publishedFile, contents, mode=0600)
         self.published = 1
     def markAsUnpublished(self):
-        try:
-            os.unlink(self.publishedFile)
-        except OSError:
-            pass
+        """DOCDOC"""
+        tryUnlink(self.publishedFile)
         self.published = 0
     def regenerateServerDescriptor(self, config, identityKey):
         """DOCDOC"""
         self.load()
+        self.markAsUnpublished()
         validAt,validUntil = self.getLiveness()
-        try:
-            os.unlink(self.publishedFile)
-        except OSError:
-            pass
         LOG.info("Regenerating descriptor for keyset %s (%s--%s)",
                  self.keyname, formatTime(validAt,1),
                  formatTime(validUntil,1))
@@ -623,7 +614,6 @@
                          validAt=validAt, validUntil=validUntil,
                          useServerKeys=1)
         self.serverinfo = self.validAfter = self.validUntil = None
-        self.markAsUnpublished()
 
     def checkConsistency(self, config, log=1):
         """DOCDOC"""
@@ -635,11 +625,7 @@
     def publish(self, url):
         """DOCDOC Returns 'accept', 'reject', 'error'. """
         fname = self.getDescriptorFileName()
-        f = open(fname, 'r')
-        try:
-            descriptor = f.read()
-        finally:
-            f.close()
+        descriptor = readFile(fname)
         fields = urllib.urlencode({"desc" : descriptor})
         try:
             try:
@@ -957,13 +943,7 @@
     info = signServerInfo(info, identityKey)
 
     # Write the desciptor
-    f = AtomicFile(serverKeys.getDescriptorFileName(), 'w')
-    try:
-        f.write(info)
-        f.close()
-    except:
-        f.discard()
-        raise
+    writeFile(serverKeys.getDescriptorFileName(), info, mode=0644)
 
     # This is for debugging: we try to parse and validate the descriptor
     #   we just made.
@@ -1059,24 +1039,14 @@
                                    "%s<MMTP>" %nickname,
                                    nickname,
                                    certStarts, certEnds)
-    try:
-        f = open(fname)
-        certText = f.read()
-    finally:
-        f.close()
+    certText = readFile(fname)
     os.unlink(fname)
     mixminion.Crypto.generate_cert(fname,
                                    identityKey, identityKey,
                                    nickname, nickname,
                                    certStarts, certEnds)
-    try:
-        f = open(fname)
-        identityCertText = f.read()
-        f.close()
-        os.unlink(fname)
-        f = open(filename, 'w')
-        f.write(certText)
-        f.write(identityCertText)
-    finally:
-        f.close()
+
+    identityCertText = readFile(fname)
+    os.unlink(fname)
+    writeFile(filename, certText+identityCertText, 0600)
 

Index: ServerMain.py
===================================================================
RCS file: /home/minion/cvsroot/src/minion/lib/mixminion/server/ServerMain.py,v
retrieving revision 1.69
retrieving revision 1.70
diff -u -d -r1.69 -r1.70
--- ServerMain.py	5 Jun 2003 02:27:30 -0000	1.69
+++ ServerMain.py	5 Jun 2003 05:24:23 -0000	1.70
@@ -66,8 +66,8 @@
 from bisect import insort
 from mixminion.Common import LOG, LogStream, MixError, MixFatalError,\
      UIError, ceilDiv, createPrivateDir, formatBase64, formatTime, \
-     installSIGCHLDHandler, Lockfile, readFile, secureDelete, waitForChildren,\
-     writeFile
+     installSIGCHLDHandler, Lockfile, readFile, secureDelete, tryUnlink, \
+     waitForChildren, writeFile
 
 #DOCDOC
 # For backward-incompatible changes only.
@@ -758,9 +758,7 @@
     def run(self):
         """Run the server; don't return unless we hit an exception."""
         global GOT_HUP
-        f = open(self.pidFile, 'wt')
-        f.write("%s\n" % os.getpid())
-        f.close()
+        writeFile(self.pidFile, "%s\n"%os.getpid(), mode=0644)
 
         self.cleanQueues()
 
@@ -900,9 +898,8 @@
 
         try:
             self.lockFile.release()
-            os.unlink(self.pidFile)
-        except OSError:
-            pass
+        finally:
+            tryUnlink(self.pidFile)
 
 #----------------------------------------------------------------------
 def daemonize():

Index: ServerQueue.py
===================================================================
RCS file: /home/minion/cvsroot/src/minion/lib/mixminion/server/ServerQueue.py,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -d -r1.18 -r1.19
--- ServerQueue.py	31 May 2003 12:52:55 -0000	1.18
+++ ServerQueue.py	5 Jun 2003 05:24:23 -0000	1.19
@@ -15,7 +15,7 @@
 import types
 
 from mixminion.Common import MixError, MixFatalError, secureDelete, LOG, \
-     createPrivateDir, readPickled, writePickled, formatTime
+     createPrivateDir, readPickled, writePickled, formatTime, readFile
 from mixminion.Crypto import getCommonPRNG
 
 __all__ = [ 'Queue', 'DeliveryQueue', 'TimedMixPool', 'CottrellMixPool',
@@ -195,10 +195,7 @@
            message."""
         try:
             self._lock.acquire()
-            f = open(os.path.join(self.dir, "msg_"+handle), 'rb')
-            s = f.read()
-            f.close()
-            return s
+            return readFile(os.path.join(self.dir, "msg_"+handle), 1)
         finally:
             self._lock.release()