]> git.pld-linux.org Git - projects/pld-ftp-admin.git/blame - modules/cmds.py
- removed outdated ignores
[projects/pld-ftp-admin.git] / modules / cmds.py
CommitLineData
e3aced8e
MM
1# vi: encoding=utf-8 ts=8 sts=4 sw=4 et
2
459e7d48
MM
3import os
4import time
88d4cb67
MM
5import config
6import common
7import md5
08b49edd 8import ftptree
459e7d48 9
459e7d48 10
e3aced8e
MM
11def parse(con):
12 if '\0' not in con.data:
13 return
14 cmds=con.data.split('\0')[:-1]
15
16 for cmd in cmds:
17 con.data=con.data[len(cmd)+1:]
18 cmdname=cmd[:4]
08b49edd 19 if not con.authorized and cmdname not in ('linp', 'linc', 'name'):
5fcf3f9a
MM
20 raise BailOut
21 # TODO: log unauthorized access
6992b18d
MM
22 if cmdname in cmdlist_noargs:
23 if len(cmd)==4:
24 cmdlist_noargs[cmdname](con)
25 else:
26 pass
27 # TODO: log malicious msg
28 elif cmdname in cmdlist_args:
29 if len(cmd)>5:
30 cmdlist_args[cmdname](con, cmd[5:])
31 else:
32 pass
33 # TODO: log malicious msg
e3aced8e 34 else:
5fcf3f9a 35 raise BailOut
e3aced8e
MM
36 # TODO: log this
37
e3aced8e
MM
38def lock(con, arg, hard):
39 if arg not in locks:
135e7889 40 locks[arg]={'hard': hard, 'name': con.name, 'time': int(time.time())}
e3aced8e 41 con.sock.send("OK")
135e7889 42 elif locks[arg]['hard']:
e3aced8e
MM
43 con.sock.send("HARD") # Hard lock - you can go get a cup of tea
44 else:
45 con.sock.send("SOFT") # Soft lock - try in a second or two
461bc5ab 46
b55905f2 47def cmd_unlock(con, arg):
e3aced8e
MM
48 if arg in locks:
49 del locks[arg]
6b4b841a
AM
50 con.sock.send("OK")
51 else:
52 con.sock.send("FAIL")
e3aced8e 53
b55905f2 54def cmd_lock_soft(con, arg):
e3aced8e
MM
55 lock(con, arg, False)
56
b55905f2 57def cmd_lock_hard(con, arg):
e3aced8e
MM
58 lock(con, arg, True)
59
6b4b841a
AM
60def cmd_show_locks(con):
61 cmd_log(con, "Dumping locks data:");
62 if len(locks):
461bc5ab 63 res = ""
6b4b841a
AM
64 for lockdata in locks.iteritems():
65 tree, data = lockdata
461bc5ab
ER
66 msg = "Tree: %s, Conn name: %s, Hard Lock: %s, Time: %s" % (
67 tree, data['name'], data['hard'], time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data['time'])))
68 cmd_log(con, msg)
69 res = res + msg
70# con.sock.send("BLOB:%d" % len(res))
71 con.sock.send(res)
6b4b841a
AM
72 else:
73 cmd_log(con, "No locks found.");
461bc5ab 74 con.sock.send("NLCK");
459e7d48 75
b55905f2 76def cmd_log(con, msg):
461bc5ab 77 logfile.write('%s [%s] -- %s\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), con.name, msg))
459e7d48 78 logfile.flush()
e3aced8e 79
b55905f2
MM
80def cmd_name(con, name):
81 con.name=name
82
88d4cb67
MM
83def load_creds():
84 global users, cookies
85 users={}
86 cookies={}
304fcc79 87 if not common.fileexists(common.ftpadmdir+'/var/passwd'):
88d4cb67
MM
88 return
89 else:
304fcc79 90 f=open(common.ftpadmdir+'/var/passwd', 'r')
88d4cb67
MM
91 for line in f.xreadlines():
92 x=line.strip().split(':')
93 if len(x)>=2:
94 users[x[0]]=x[1]
95 f.close()
304fcc79 96 if not common.fileexists(common.ftpadmdir+'/var/cookies'):
88d4cb67
MM
97 return
98 else:
304fcc79 99 f=open(common.ftpadmdir+'/var/cookies', 'r')
88d4cb67
MM
100 for line in f.xreadlines():
101 x=line.strip().split(':')
102 if len(x)>=2:
103 users[x[0]]=x[1]
104 f.close()
105
106def write_cookies():
304fcc79 107 f=open(common.ftpadmdir+'/var/cookies', 'w')
88d4cb67
MM
108 for key in cookies.keys():
109 f.write('%s:%s\n' % (key, cookies[key]))
110 f.close()
111
6992b18d
MM
112def cmd_login_passwd(con, data):
113 tmp=data.split('\n')
114 if len(tmp)!=2:
115 raise BailOut
116 login=tmp[0]
117 passwd=tmp[1]
38492fee
MM
118 md5pass=md5.new(passwd).hexdigest()
119 if login in users and users[login]==md5pass:
120 cookie=`time.time()`.split('.')[0]+'_'+md5.new(md5pass+salt).hexdigest()
88d4cb67
MM
121 cookies[cookie]=login
122 write_cookies()
08b49edd 123 con.username=login
88d4cb67
MM
124 con.authorized=True
125 con.sock.send('OK '+cookie)
6992b18d
MM
126 else:
127 # TODO: log this
128 con.sock.send('FAIL')
129 raise BailOut
130
131def cmd_login_cookie(con, cookie):
88d4cb67
MM
132 if cookie in cookies:
133 con.cookie=cookie
134 con.authorized=True
08b49edd 135 con.username=cookies[cookie]
88d4cb67 136 con.sock.send('OK '+cookies[cookie])
6992b18d
MM
137 else:
138 # TODO: log this (or not)
139 con.sock.send('FAIL')
140
141def cmd_logout(con):
88d4cb67
MM
142 if con.cookie in cookies:
143 del cookies[con.cookie]
144 write_cookies()
08b49edd
MM
145
146def reloadftptree():
147 global srctree, pkglist
148 srctree=ftptree.FtpTree(config.value['default_to'], loadall=True)
149 pkglist=srctree.keys()
150 pkglist.sort()
151
152def cmd_gettree(con):
153 buf=''
85f3481a 154 for pkgnvr in pkglist:
08b49edd 155 # TODO: show only user's own pkgs
85f3481a
MM
156 pkg=srctree[pkgnvr]
157 line=pkgnvr
5d490a9c
MM
158 if pkg.marked4moving:
159 line=line+'\n1'
160 else:
161 line=line+'\n0'
162 if pkg.marked4removal:
163 line=line+'\n1'
164 else:
165 line=line+'\n0'
166 buf=buf+'\0'+line
08b49edd
MM
167 if buf:
168 con.sock.send('%.6d' % (len(buf)-1))
169 con.sock.send(buf[1:])
170 else:
171 con.sock.send('000000')
172
6992b18d
MM
173
174cmdlist_args={'lcks':cmd_lock_soft, 'lckh':cmd_lock_hard, 'ulck':cmd_unlock,
175 'log1':cmd_log, 'name':cmd_name, 'linp':cmd_login_passwd,
176 'linc':cmd_login_cookie}
177
6b4b841a 178cmdlist_noargs={'lout':cmd_logout, 'gett':cmd_gettree, 'slck':cmd_show_locks}
6992b18d 179
88d4cb67
MM
180# Global stuff and initializations
181
182BailOut="BailOut"
183locks={}
304fcc79 184logfile=open(common.ftpadmdir+'/var/log', 'a')
88d4cb67 185load_creds()
08b49edd 186reloadftptree()
38492fee 187salt=md5.new(`time.time()`).hexdigest()
e3aced8e 188
This page took 0.268344 seconds and 4 git commands to generate.