[Nym3-commit] r52 - trunk

nym3-devel@lists.noreply.org nym3-devel@lists.noreply.org
Mon, 02 Aug 2004 15:08:06 +0200


Author: jr
Date: 2004-08-02 15:08:03 +0200 (Mon, 02 Aug 2004)
New Revision: 52

Modified:
   trunk/Config.py
   trunk/Main.py
   trunk/Message.py
   trunk/TODO
   trunk/User.py
Log:
the last "multi-purpose", inappropriate commit:
- skeleton of processMessage (Main.py)
- processing of anonymous control message for the account creation(Main.py)
- add the User data : nSurs, up(Config.py)
- add Message.ct(Message.py)
- add User.isInitialized, User.idKey, User.encKey(User.py)


Modified: trunk/Config.py
===================================================================
--- trunk/Config.py	2004-08-01 22:26:00 UTC (rev 51)
+++ trunk/Config.py	2004-08-02 13:08:03 UTC (rev 52)
@@ -6,4 +6,8 @@
 		     'idKey' : None, 'encKey' : None, 'policy' : None,
 		     'maxMailLat' : 24, 'maxSynLat' : 12, 
 		     'maxSURBSperDay' : 10, 'maxClearSyn' : '24',
-		     'hold' : 'Always', 'shutdownHash' : None}
+		     'hold' : 'Always', 'shutdownHash' : None,
+                     'nSurbs' : 0, 'up' : False, 'cr' = ""}
+
+#up : the user account has been initialized and the answer to the challenge is correct
+#cr : response to the challenge

Modified: trunk/Main.py
===================================================================
--- trunk/Main.py	2004-08-01 22:26:00 UTC (rev 51)
+++ trunk/Main.py	2004-08-02 13:08:03 UTC (rev 52)
@@ -5,6 +5,8 @@
 import getopt
 import User
 import Config
+import Message
+import Crypto
 
 def processIncoming(localpart, msg):
     try:
@@ -19,6 +21,95 @@
     nymuser.store(msg)
     sys.exit(0)
 
+def processMessage(msg):
+    """process incoming control message
+    - verifies signature
+    - parse the message into header + sequence on control commands
+    - run appropriate actions"""
+    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()
+    except 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):
+                pass
+            elif (com.ct() == 2):
+                pass
+            elif (com.ct() == 3):
+                pass
+            elif (com.ct() == 4):
+                pass
+            elif (com.ct() == 5):
+                pass
+            elif (com.ct() == 6):
+                pass
+            elif (com.ct() == 7):
+                pass
+            elif (com.ct() == 8):
+                pass
+            else:
+                pass
+                    
+    
 if __name__ == '__main__':
     optlist, pholder = getopt.getopt(sys.argv[1:], 'd:')
     for o, a in optlist:

Modified: trunk/Message.py
===================================================================
--- trunk/Message.py	2004-08-01 22:26:00 UTC (rev 51)
+++ trunk/Message.py	2004-08-02 13:08:03 UTC (rev 52)
@@ -101,6 +101,11 @@
 			return a
 		except IndexError: #is it really necessary? is it not considered in the various fromStrReader ?
 			raise ParseError("Bad Formed Command")
+	def readCommandCToSList(self):
+		l=[]
+		while (not self.isEnd()):
+			l.append(self.readCommandCToS)
+		return l
 	def readCommandSToC(self):
 		"""Read next CommandSToC from string"""
 		try:
@@ -247,6 +252,9 @@
 	def __init__(self):
 		"""Build a Create empty object"""
 		pass
+	def ct(self):
+		"""return a int for the code of the command"""
+		return 0
 	def fromData(self,l,pw=""):
 		"""Fill a Create Object from a list of nyms and a proof of work"""
 		if(len(l)>255):
@@ -287,6 +295,9 @@
 	def __init__(self):
 		"""Build a Create2 empty object"""
 		pass
+	def ct(self):
+		"""return a int for the code of the command"""
+		return 1
 	def fromData(self,cr):
 		"""Fill Create2 from a string containing the response to a challenge"""
 		self.cr=cr
@@ -311,6 +322,9 @@
 	def __init__(self):
 		"""Build a Surb empty object"""
 		pass
+	def ct(self):
+		"""return a int for the code of the command"""
+		return 2
 	def fromData(self,s):
 		"""Fill a Surb Object from a string containing Surbs"""
 		self.surbs=s
@@ -339,6 +353,9 @@
 	def __init__(self):
 		"""Build a Newpk empty object"""
 		pass
+	def ct(self):
+		"""return a int for the code of the command"""
+		return 3
 	def fromData(self,sid,senc):
 		"""Fill a Newpk Object from 2 strings containing an ASN-1 encoded RSA key"""
 		id_l=len(sid)
@@ -379,6 +396,9 @@
 	def __init__(self):
 		"""Build a Relay empty object"""
 		pass
+	def ct(self):
+		"""return a int for the code of the command"""
+		return 4
 	def fromData(self,rt,ri,body):
 		"""Fill a Relay Object from a 3 strings : routing type(2 octets) routing info and body"""
 		if(len(rt) !=2):
@@ -414,6 +434,9 @@
 	def __init__(self):
 		"""Build a Get empty object"""
 		pass
+	def ct(self):
+		"""return a int for the code of the command"""
+		return 5
 	def fromData(self,li):
 		"""Fill a Get Object from a list of message id (=string of len 20)"""
 		#check that each element of the list has 20 bytes
@@ -446,6 +469,9 @@
 	def __init__(self):
 		"""Build a Summarize empty object"""
 		pass
+	def ct(self):
+		"""return a int for the code of the command"""
+		return 6
 	def fromData(self,n,m):
 		"""Fill a Summarize Object from the max number of synopsis to retrieve (int)  and the message id of a message older than the messages we want the synopsis from (string of len 20)
 		the int is considered modulo 256^2"""
@@ -478,6 +504,9 @@
 	def __init__(self):
 		"""Build a Delete empty object"""
 		pass
+	def ct(self):
+		"""return a int for the code of the command"""
+		return 7
 	def fromData(self,li):
 		"""Fill a Delete Object from a list of message ID (string of length midLength"""
 		#check that each element of the list has 20 bytes
@@ -510,6 +539,9 @@
 	def __init__(self):
 		"""Build a Policy empty object"""
 		pass
+	def ct(self):
+		"""return a int for the code of the command"""
+		return 8
 	def fromData(self,opt,val):
 		"""Fill a Policy Object from 2 str : the option name and its value
 		It is the caller work to convert adequatly into a string the value he wants to be sent"""

Modified: trunk/TODO
===================================================================
--- trunk/TODO	2004-08-01 22:26:00 UTC (rev 51)
+++ trunk/TODO	2004-08-02 13:08:03 UTC (rev 52)
@@ -28,4 +28,5 @@
  - implement encrypt a mail, a set of synopses
  - implement sign a control message
  - figure what the routing type and info (cf Command relay) are
+ - check the size of an ASN.1 encoded key 
  - get a life :)

Modified: trunk/User.py
===================================================================
--- trunk/User.py	2004-08-01 22:26:00 UTC (rev 51)
+++ trunk/User.py	2004-08-02 13:08:03 UTC (rev 52)
@@ -30,6 +30,12 @@
     def usage(self):
 	return self.data['usage']
 
+    def idKey(self):
+        return self.data['idKey']
+
+    def encKey(self):
+        return self.data['encKey']
+    
     def save(self):
 	f = open(self.datafile, 'w') # TODO : Locks?
 	pickle.dump(self.data, f)
@@ -105,7 +111,16 @@
 	f = open(synbox, 'w') # TODO : Locks?
 	pickle.dump(self.syn, f)
 	f.close()
-	
+
+    def isInitialized(self):
+        """True if the first 3 commands :
+        - a Create command
+        - a Newpk command
+        - a Surb command containing at least 3 surbs
+        have arrived"""
+        return (self.data['nSurbs'] > 2) and (self.data['idkey'] != None)
+
+    
 if __name__ == '__main__':
     a = User('laurent')
     ec = a.relay("NYM3 TEST : " + repr(time.time()))