]> TLD Linux GIT Repositories - tld-ftp-admin.git/blobdiff - modules/cmds.py
- merged PLD changes
[tld-ftp-admin.git] / modules / cmds.py
index 192dbf672ea73af9acc1c3d692405e28223c7375..9fa2b195c89ece9c06d438ec0f244393ff9aba3f 100644 (file)
@@ -4,7 +4,7 @@ import os
 import time
 import config
 import common
-import md5
+import hashlib
 import ftptree
 
 
@@ -17,7 +17,7 @@ def parse(con):
         con.data=con.data[len(cmd)+1:]
         cmdname=cmd[:4]
         if not con.authorized and cmdname not in ('linp', 'linc', 'name'):
-            raise BailOut
+            raise BailOut()
             # TODO: log unauthorized access
         if cmdname in cmdlist_noargs:
             if len(cmd)==4:
@@ -32,24 +32,24 @@ def parse(con):
                 pass
                 # TODO: log malicious msg
         else:
-            raise BailOut
+            raise BailOut()
             # TODO: log this
 
 def lock(con, arg, hard):
     if arg not in locks:
         locks[arg]={'hard': hard, 'name': con.name, 'time': int(time.time())}
-        con.sock.send("OK")
+        con.sock.send(bytearray("OK", encoding='utf-8'))
     elif locks[arg]['hard']:
-        con.sock.send("HARD") # Hard lock - you can go get a cup of tea
+        con.sock.send(bytearray("HARD", encoding='utf-8')) # Hard lock - you can go get a cup of tea
     else:
-        con.sock.send("SOFT") # Soft lock - try in a second or two
+        con.sock.send(bytearray("SOFT", encoding='utf-8')) # Soft lock - try in a second or two
 
 def cmd_unlock(con, arg):
     if arg in locks:
         del locks[arg]
-        con.sock.send("OK")
+        con.sock.send(bytearray("OK", encoding='utf-8'))
     else:
-        con.sock.send("FAIL")
+        con.sock.send(bytearray("FAIL", encoding='utf-8'))
 
 def cmd_lock_soft(con, arg):
     lock(con, arg, False)
@@ -61,17 +61,17 @@ def cmd_show_locks(con):
     cmd_log(con, "Dumping locks data:");
     if len(locks):
         res = ""
-        for lockdata in locks.iteritems():
+        for lockdata in locks.items():
             tree, data = lockdata
             msg = "Tree: %s, Conn name: %s, Hard Lock: %s, Time: %s" % (
                     tree, data['name'], data['hard'], time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data['time'])))
             cmd_log(con, msg)
             res = res + msg
-#        con.sock.send("BLOB:%d" % len(res))
-        con.sock.send(res)
+#        con.sock.send(bytearray("BLOB:%d" % len(res), encoding='utf-8')))
+        con.sock.send(bytearray(res, encoding='utf-8'))
     else:
-        cmd_log(con, "No locks found.");
-        con.sock.send("NLCK");
+        cmd_log(con, "No locks found.")
+        con.sock.send(bytearray("NLCK", encoding='utf-8'))
 
 def cmd_log(con, msg):
     logfile.write('%s [%s] -- %s\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), con.name, msg))
@@ -88,7 +88,7 @@ def load_creds():
         return
     else:
         f=open(common.ftpadmdir+'/var/passwd', 'r')
-        for line in f.xreadlines():
+        for line in f:
             x=line.strip().split(':')
             if len(x)>=2:
                 users[x[0]]=x[1]
@@ -97,7 +97,7 @@ def load_creds():
         return
     else:
         f=open(common.ftpadmdir+'/var/cookies', 'r')
-        for line in f.xreadlines():
+        for line in f:
             x=line.strip().split(':')
             if len(x)>=2:
                 users[x[0]]=x[1]
@@ -112,31 +112,32 @@ def write_cookies():
 def cmd_login_passwd(con, data):
     tmp=data.split('\n')
     if len(tmp)!=2:
-        raise BailOut
+        raise BailOut()
     login=tmp[0]
     passwd=tmp[1]
-    md5pass=md5.new(passwd).hexdigest()
-    if login in users and users[login]==md5pass:
-        cookie=`time.time()`.split('.')[0]+'_'+md5.new(md5pass+salt).hexdigest()
+    md5pass=hashlib.md5(passwd.encode('utf-8')).hexdigest()
+    if login in users and users[login] == md5pass:
+        fullpass = md5pass+salt
+        cookie=repr(time.time()).split('.')[0]+'_'+hashlib.md5(fullpass.encode('utf-8')).hexdigest()
         cookies[cookie]=login
         write_cookies()
         con.username=login
         con.authorized=True
-        con.sock.send('OK '+cookie)
+        con.sock.send(bytearray('OK '+cookie, encoding='utf-8'))
     else:
         # TODO: log this
-        con.sock.send('FAIL')
-        raise BailOut
+        con.sock.send(bytearray('FAIL', encoding='utf-8'))
+        raise BailOut()
 
 def cmd_login_cookie(con, cookie):
     if cookie in cookies:
         con.cookie=cookie
         con.authorized=True
         con.username=cookies[cookie]
-        con.sock.send('OK '+cookies[cookie])
+        con.sock.send(bytearray('OK '+cookies[cookie], encoding='utf-8'))
     else:
         # TODO: log this (or not)
-        con.sock.send('FAIL')
+        con.sock.send(bytearray('FAIL'))
 
 def cmd_logout(con):
     if con.cookie in cookies:
@@ -146,8 +147,7 @@ def cmd_logout(con):
 def reloadftptree():
     global srctree, pkglist
     srctree=ftptree.FtpTree(config.value['default_to'], loadall=True)
-    pkglist=srctree.keys()
-    pkglist.sort()
+    pkglist=sorted(srctree.keys())
 
 def cmd_gettree(con):
     buf=''
@@ -165,10 +165,10 @@ def cmd_gettree(con):
             line=line+'\n0'
         buf=buf+'\0'+line
     if buf:
-        con.sock.send('%.6d' % (len(buf)-1))
-        con.sock.send(buf[1:])
+        con.sock.send(bytearray('%.6d' % (len(buf)-1), encoding='utf-8'))
+        con.sock.send(bytearray(buf[1:], encoding='utf-8'))
     else:
-        con.sock.send('000000')
+        con.sock.send(bytearray('000000', encoding='utf-8'))
 
 
 cmdlist_args={'lcks':cmd_lock_soft, 'lckh':cmd_lock_hard, 'ulck':cmd_unlock,
@@ -184,5 +184,5 @@ locks={}
 logfile=open(common.ftpadmdir+'/var/log', 'a')
 load_creds()
 reloadftptree()
-salt=md5.new(`time.time()`).hexdigest()
+salt=hashlib.md5(repr(time.time()).encode('utf-8')).hexdigest()