[Nym3-commit] r83 - trunk

nym3-devel@lists.noreply.org nym3-devel@lists.noreply.org
Thu, 05 Aug 2004 09:22:12 +0200


Author: jr
Date: 2004-08-05 09:22:10 +0200 (Thu, 05 Aug 2004)
New Revision: 83

Modified:
   trunk/Main.py
   trunk/User.py
Log:
- rearrange signature check (Main.py)
- add checkChallenge and checkMessageSign(User.py)
- add User.abort (destruction on a non initialized account)(User.py)
(Laurent : some feedback please)
- add an alternative t the creation of User for create=1(User.py)
- use User.User as it should be used(Main.py)
- correct some bugs in processMessage. We now detect all the
cases in which the account creation fails(Main.py)


Modified: trunk/Main.py
===================================================================
--- trunk/Main.py	2004-08-05 06:02:14 UTC (rev 82)
+++ trunk/Main.py	2004-08-05 07:22:10 UTC (rev 83)
@@ -30,110 +30,114 @@
     - verifies signature
     - parse the message into header + sequence on control commands
     - run appropriate actions"""
+    def MyException(Exception): pass
+    
     sr = Message.StrReader(msg)
     try:
 	h = sr.readHeader()
-	if(h.sig != Crypto.pk_check_signature(msg[Message.sigLength:],nymuser.idKey)):
-	    print "Invalid signature"
-	    print e
-	    sys.exit(2) #TODO is it the smart error code
-	comList = sr.readCommandCToSList()
+        if(h.nym == ""):
+            comList = sr.readCommandCToSList()
+            try:
+                if(len(comList) != 3):
+                    raise MyException()
+	        #this may be an account setup message
+	        #we suppose there is a exactly 1 Create Command in the message, 1 Newpk, and 1 surb
+	        #more will raise an error
+
+                nymUser = None
+	        #phase 1 we look for the command create
+                for idx, com in enumerate(comList):
+                    if(com.ct() == 0):
+		        #the Create command
+                        for pnym in self.list:
+                            try:
+                                nymUser=User.User(pnym,1)
+                            except User.AlreadySuchUser:
+                                pass
+                            if(nymUser != None):
+                                break
+                        if(nymUser == None):
+                            #TODO send an Error message to the client when surbs become available?
+                            #for the time being just ignore
+                            print "All nyms proposed in the list were already attribuated"
+                            raise MyException()
+                        del(comList[idx])
+                        break
+                if(nymUser == None):
+                    #TODO send an Error message to the client when surbs become available?
+                    #for the time being just ignore
+                    print "No Create Command"
+                    raise MyException()
+                        
+	        #phase 2 we look for the command surb
+                for idx, com in enumerate(comList):
+                    if(com.ct()==2):
+                        nymUser.addSurbs(com.surbs)
+                        del(comList[idx])
+                        break
+                if(len(comList) != 1):
+                    nymUser.abort()
+                    raise MyException()
+	        #phase 3 the last command should be a Newpk
+                com = comList[0]
+                if(com.ct() != 3):
+                    nymUser.abort()
+                    raise MyException()
+                nymUser.setKeys(com.kid,com.kenc)
+                if(not nymUser.checkMessageSign(msg[Message.sigLength:],h.sig)):
+                   nymUser.abort()
+                   raise MyException()
+            except MyException:
+                   #if you come here something went wrong during the account
+                   #initialization
+                   print "Bad formed account creation message"
+                   sys.exit(2) #TODO smart error code
+        else:
+            try:
+                nymUser = User.user(h.nym)
+            except User.NoSuchUser:
+                print "No such user"
+                sys.exit(73) #TODO is it the smart error code
+            if(not nymUser.checkMessageSign(msg[Message.sigLength:],h.sig)):
+               return
+            comList = sr.readCommandCToSList()
+            for com in comList:
+                if (com.ct() == 0):
+		    #we ignore the Create command if it comes from the nymholder of an account, should we rise an error?
+                    pass
+                #if the account is not initialized, or if it is already up we ignore Create2 messages
+                elif (com.ct() == 1):
+                    if (nymUser.isInitialized() and (not nymUser.isUp())):
+                        if(nymUser.checkChallenge(com.cr)):
+                            nymUser.setUp()
+		#other commands are only taken into account if the account is up
+                elif (nymUser.isUp()):
+                    if (com.ct() == 2):
+                        if( len(com.surbs) == 0):
+                            nymUser.delSurbs()
+                        else:
+                            nymUser.addSurbs(com.surbs)
+                    elif (com.ct() == 3):
+                        nymUser.setKeys(com.kid,com.kenc)
+                    elif (com.ct() == 4):
+                        pass
+                    elif (com.ct() == 5):
+                        pass
+                    elif (com.ct() == 6):
+                        sendList=nymUser.sendList(com.num,com.after)
+                        #TODO send what is in sendList (list of BF,enc syn)
+                        #TODO modify the state (['sent']='synopsis'
+                    elif (com.ct() == 7):
+                        for mid in com.l: pass
+                    elif (com.ct() == 8):
+                        if(com.opt in Common.userPolicy):
+                            nymUser.data[com.opt] = com.val
+                    else:
+                        pass
     except Message.ParseError, inst:
 	print inst
-	sys.exit(2) #TODO error code
-    if(h.nym == ""):
-	try:
-	    if(len(comList) != 3):
-		raise Exception("Bad formed account message")
-	    #this may be an account setup message
-	    #we suppose there is a exactly 1 Create Command in the message, 1 Newpk, and 1 surb
-	    #more will raise an error
-
-	    nymUser = None
-	    #phase 1 we look for the command create
-	    for idx, com in enumerate(comList):
-		if(com.ct()==0):
-		    #the Create command
-		    for i, pnym in enumerate(self.list):
-			if(User.add(pnym)):
-			    nymUser=User.User(pnym)
-			    break
-		    if(nymUser == None):
-			#TODO send an Error message to the client when surbs become available?
-			print "All nyms proposed in the list were already attribuated"
-			break
-		    del(comList[idx])
-		    break
-	    #phase 2 we look for the command surb
-	    for idx, com in enumerate(comList):
-		if(com.ct()==2):
-		    if(nymUser == None):
-			#TODO send Error message?
-			sys.exit(2) #TODO change 2
-		    else:
-			nymUser.addSurbs(com.surbs)
-			del(comList[idx])
-			break
-	    #phase 3 the last command should be a Newpk
-	    com = comList[0]
-	    if(com.ct() != 3):
-		raise Exception("Bad formed account message")
-	    nymUser.setKeys(com.kid,com.kenc)
-	except Exception, inst: #do we keep Exception or do change it?
-	    #SyntaxError could be better
-	    print inst
-	    sys.exit(2) #TODO smart error code
-    else:
-	try:
-	    nymUser = User.user(h.nym)
-	except User.NoSuchUser:
-	    print "No such user"
-	    sys.exit(73) #TODO is it the smart error code
-	
-	for com in comList:
-	    if (com.ct() == 0):
-		#we ignore the Create command if it comes from the nymholder of an account, should we rise an error?
-		pass
-	    elif (com.ct() == 1):
-		if (nymUser.isInitialized() and (not nymUser.isUp())):
-		    if(nymUser['cr'] == com.cr):
-			nymUser.setUp()
-		#if the account is not initialized, or if it is already up we ignore Create2 messages
-	    elif (nymUser.isUp()):
-		if (com.ct() == 2):
-		    if( len(com.surbs) == 0):
-			nymUser.delSurbs()
-		    else:
-			nymUser.addSurbs(com.surbs)
-		elif (com.ct() == 3):
-		    nymUser.setKeys(com.kid,com.kenc)
-		elif (com.ct() == 4):
-		    pass
-		elif (com.ct() == 5):
-		    pass
-                elif (com.ct() == 6):
-                    sendList=nymUser.sendList(com.num,com.after)
-                    #TODO send what is in sendList (list of BF,enc syn)
-                    #TODO modify the state (['sent']='synopsis'
-                elif (com.ct() == 7):
-		    nymUser.lock()
-                    nymUser.load_mbox()
-                    nymUser.load_index()
-                    nymUser.load_synbox()
-
-                    for mid in com.l: pass
-                        
-
-                    #nymUser.save_mbox()
-                    #nymUser.save_index()
-                    #nymUser.save_synbox()
-                    #nymUser.release()
-		elif (com.ct() == 8):
-		    if(com.opt in Common.userPolicy):
-			nymUser.data[com.opt] = com.val
-		else:
-		    pass
-		    
+	sys.exit(2) #TODO error code                
+	      
     
 if __name__ == '__main__':
     optlist, pholder = getopt.getopt(sys.argv[1:], 'd:')

Modified: trunk/User.py
===================================================================
--- trunk/User.py	2004-08-05 06:02:14 UTC (rev 82)
+++ trunk/User.py	2004-08-05 07:22:10 UTC (rev 83)
@@ -14,22 +14,31 @@
 
 class NoSuchUser(Exception): pass
 
+class AlreadySuchUser(Exception): pass
+
 def timecmp(a, b):
     return cmp(self.index[a][time], self.index[b][time])
         
-
 class User:
     """Hold user data"""
     def __init__(self, username, create = 0):
-	self.datafile = Config.path + os.sep + username + '.dat'
+        """0 : load user data throw an error if the user doesn't exist
+        1 : create user data throw an error if the user exists
+        _ : load a user data, if it doesn't exist create a new user silently
+	"""
+        self.datafile = Config.path + os.sep + username + '.dat'
 	self.username = username
 	self.index = None
 	self.mbox = None
 	self.syn = None
 	self.data = None
+        self._abort = False
 	self._lock()
 	try:
 	    f = open(self.datafile, 'r')
+            if(create == 1):
+                f.close()
+                raise AlreadySuchUser
 	    self.data = pickle.load(f)
 	    f.close()
 	except IOError:
@@ -44,12 +53,13 @@
 	    #f.close()
     
     def __del__(self):
-	self._save_index()
-	self._save_synbox()
-	self._save_mbox()
-	self._save_data()
-	self._release()
-
+        if(not self._abort):
+            self._save_index()
+            self._save_synbox()
+            self._save_mbox()
+            self._save_data()
+        self._release()
+    
     def __getitem__(self, key):
 	return self.data[key]
 
@@ -65,6 +75,17 @@
     def _release(self):
 	self.lock.release()
 
+    def abort(self):
+        """permit to destroy a User object without saving its data
+        Can only be used if the object is being created for the
+        first time. If username.dat exist, it is ignored
+        """
+        try:
+            f = open(self.datafile, 'r')
+            f.close()
+        except IOError:
+            self._abort = True
+
     def quota(self):
 	return self.data['quota']
 
@@ -277,11 +298,17 @@
 	return (self.data['nSurbs'] > 2) and (self.data['idkey'] != None)
 
     def isUp(self):
-	return self.data['up']
+	return self['up']
 
     def setUp(self):
-	self.data['up'] = True
+	self['up'] = True
 
+    def checkMessageSign(self,m,s):
+        return (s == Crypto.pk_check_signature(m,self.idKey()))
+	    
+    def checkChallenge(self,cr):
+        return (self['cr'] == cr)
+
     def encryptSyn(self,i,j):
         """Replace in the synbox the syn between i and j-1
         by an encrypted blob. Initially the syn have to be in clear