]>
Commit | Line | Data |
---|---|---|
e3aced8e MM |
1 | # vi: encoding=utf-8 ts=8 sts=4 sw=4 et |
2 | ||
459e7d48 MM |
3 | import os |
4 | import time | |
88d4cb67 MM |
5 | import config |
6 | import common | |
7 | import md5 | |
08b49edd | 8 | import ftptree |
459e7d48 | 9 | |
459e7d48 | 10 | |
e3aced8e MM |
11 | def 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 |
38 | |
39 | def lock(con, arg, hard): | |
40 | if arg not in locks: | |
41 | locks[arg]=hard | |
42 | con.sock.send("OK") | |
43 | elif locks[arg]: | |
44 | con.sock.send("HARD") # Hard lock - you can go get a cup of tea | |
45 | else: | |
46 | con.sock.send("SOFT") # Soft lock - try in a second or two | |
47 | ||
48 | ||
b55905f2 | 49 | def cmd_unlock(con, arg): |
e3aced8e MM |
50 | if arg in locks: |
51 | del locks[arg] | |
52 | ||
b55905f2 | 53 | def cmd_lock_soft(con, arg): |
e3aced8e MM |
54 | lock(con, arg, False) |
55 | ||
b55905f2 | 56 | def cmd_lock_hard(con, arg): |
e3aced8e MM |
57 | lock(con, arg, True) |
58 | ||
459e7d48 | 59 | |
b55905f2 MM |
60 | def cmd_log(con, msg): |
61 | logfile.write('%s [%s] -- %s\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), | |
62 | con.name, msg)) | |
459e7d48 | 63 | logfile.flush() |
e3aced8e | 64 | |
b55905f2 MM |
65 | def cmd_name(con, name): |
66 | con.name=name | |
67 | ||
88d4cb67 MM |
68 | |
69 | def load_creds(): | |
70 | global users, cookies | |
71 | users={} | |
72 | cookies={} | |
73 | if not common.fileexists(common.ftpadmdir+'var/passwd'): | |
74 | return | |
75 | else: | |
76 | f=open(common.ftpadmdir+'var/passwd', 'r') | |
77 | for line in f.xreadlines(): | |
78 | x=line.strip().split(':') | |
79 | if len(x)>=2: | |
80 | users[x[0]]=x[1] | |
81 | f.close() | |
82 | if not common.fileexists(common.ftpadmdir+'var/cookies'): | |
83 | return | |
84 | else: | |
85 | f=open(common.ftpadmdir+'var/cookies', 'r') | |
86 | for line in f.xreadlines(): | |
87 | x=line.strip().split(':') | |
88 | if len(x)>=2: | |
89 | users[x[0]]=x[1] | |
90 | f.close() | |
91 | ||
92 | def write_cookies(): | |
93 | f=open(common.ftpadmdir+'var/cookies', 'w') | |
94 | for key in cookies.keys(): | |
95 | f.write('%s:%s\n' % (key, cookies[key])) | |
96 | f.close() | |
97 | ||
6992b18d MM |
98 | def cmd_login_passwd(con, data): |
99 | tmp=data.split('\n') | |
100 | if len(tmp)!=2: | |
101 | raise BailOut | |
102 | login=tmp[0] | |
103 | passwd=tmp[1] | |
38492fee MM |
104 | md5pass=md5.new(passwd).hexdigest() |
105 | if login in users and users[login]==md5pass: | |
106 | cookie=`time.time()`.split('.')[0]+'_'+md5.new(md5pass+salt).hexdigest() | |
88d4cb67 MM |
107 | cookies[cookie]=login |
108 | write_cookies() | |
08b49edd | 109 | con.username=login |
88d4cb67 MM |
110 | con.authorized=True |
111 | con.sock.send('OK '+cookie) | |
6992b18d MM |
112 | else: |
113 | # TODO: log this | |
114 | con.sock.send('FAIL') | |
115 | raise BailOut | |
116 | ||
117 | def cmd_login_cookie(con, cookie): | |
88d4cb67 MM |
118 | if cookie in cookies: |
119 | con.cookie=cookie | |
120 | con.authorized=True | |
08b49edd | 121 | con.username=cookies[cookie] |
88d4cb67 | 122 | con.sock.send('OK '+cookies[cookie]) |
6992b18d MM |
123 | else: |
124 | # TODO: log this (or not) | |
125 | con.sock.send('FAIL') | |
126 | ||
127 | def cmd_logout(con): | |
88d4cb67 MM |
128 | if con.cookie in cookies: |
129 | del cookies[con.cookie] | |
130 | write_cookies() | |
08b49edd MM |
131 | |
132 | def reloadftptree(): | |
133 | global srctree, pkglist | |
134 | srctree=ftptree.FtpTree(config.value['default_to'], loadall=True) | |
135 | pkglist=srctree.keys() | |
136 | pkglist.sort() | |
137 | ||
138 | def cmd_gettree(con): | |
139 | buf='' | |
140 | for pkg in pkglist: | |
141 | # TODO: show only user's own pkgs | |
142 | # TODO: show if already marked | |
143 | buf=buf+'\n'+pkg | |
144 | if buf: | |
145 | con.sock.send('%.6d' % (len(buf)-1)) | |
146 | con.sock.send(buf[1:]) | |
147 | else: | |
148 | con.sock.send('000000') | |
149 | ||
6992b18d MM |
150 | |
151 | cmdlist_args={'lcks':cmd_lock_soft, 'lckh':cmd_lock_hard, 'ulck':cmd_unlock, | |
152 | 'log1':cmd_log, 'name':cmd_name, 'linp':cmd_login_passwd, | |
153 | 'linc':cmd_login_cookie} | |
154 | ||
08b49edd | 155 | cmdlist_noargs={'lout':cmd_logout, 'gett':cmd_gettree} |
6992b18d | 156 | |
88d4cb67 MM |
157 | # Global stuff and initializations |
158 | ||
159 | BailOut="BailOut" | |
160 | locks={} | |
161 | logfile=open(common.ftpadmdir+'var/log', 'a') | |
162 | load_creds() | |
08b49edd | 163 | reloadftptree() |
38492fee | 164 | salt=md5.new(`time.time()`).hexdigest() |
e3aced8e | 165 |