1 | # -*- coding: utf-8 -*- |
---|
2 | |
---|
3 | import os.path |
---|
4 | import base64 |
---|
5 | import os |
---|
6 | import pwd |
---|
7 | import multiprocessing |
---|
8 | import socket |
---|
9 | import ssl |
---|
10 | import shutil |
---|
11 | |
---|
12 | from xmlrpclib import * |
---|
13 | |
---|
14 | |
---|
15 | NET="/net/server-sync/" |
---|
16 | |
---|
17 | NET_FOLDER="/net/server-sync/home/.lliurex-harvester/" |
---|
18 | ORIG_NET_FOLDER=NET_FOLDER |
---|
19 | |
---|
20 | NEW_NET_FOLDER="/net/server-sync/home/students/%%STUDENT%%/.harvester/" |
---|
21 | |
---|
22 | if not os.path.exists(NET): |
---|
23 | |
---|
24 | NET_FOLDER="/run/%%USER%%/home/.lliurex-harvester/" |
---|
25 | NEW_NET_FOLDER="/run/%%USER%%/home/students/%%STUDENT%%/.harvester/" |
---|
26 | |
---|
27 | |
---|
28 | |
---|
29 | class TeacherShare: |
---|
30 | |
---|
31 | |
---|
32 | def send_to_teacher(self,from_user,to_user,file_name,file_in_b64): |
---|
33 | |
---|
34 | file_name=file_name.encode("utf-8") |
---|
35 | server = ServerProxy ("https://server:9779") |
---|
36 | try: |
---|
37 | self.paths=server.get_paths("","TeacherShareManager") |
---|
38 | except Exception as e: |
---|
39 | print e |
---|
40 | return False |
---|
41 | |
---|
42 | |
---|
43 | if to_user in self.paths: |
---|
44 | try: |
---|
45 | path,ip,name=self.paths[to_user] |
---|
46 | fname=self.generate_file_name(from_user,file_name,path) |
---|
47 | f=open(fname,'w') |
---|
48 | f.write(base64.b64decode(file_in_b64)) |
---|
49 | f.close() |
---|
50 | os.chown(fname,pwd.getpwnam(to_user)[2],pwd.getpwnam(to_user)[3]) |
---|
51 | return True |
---|
52 | |
---|
53 | except Exception as e: |
---|
54 | print e |
---|
55 | return False |
---|
56 | |
---|
57 | else: |
---|
58 | return False |
---|
59 | |
---|
60 | |
---|
61 | |
---|
62 | #def send_to_users |
---|
63 | |
---|
64 | def send_to_teacher_socket_p(self,from_user,to_user,file_path,queue): |
---|
65 | |
---|
66 | ret_value=False |
---|
67 | file_path=file_path.encode("utf-8") |
---|
68 | server = ServerProxy ("https://server:9779") |
---|
69 | |
---|
70 | try: |
---|
71 | paths=server.get_paths("","TeacherShareManager") |
---|
72 | except Exception as e: |
---|
73 | print e |
---|
74 | |
---|
75 | if to_user in paths: |
---|
76 | |
---|
77 | try: |
---|
78 | path,name,ip,port=paths[to_user] |
---|
79 | student_uid=pwd.getpwnam(from_user)[2] |
---|
80 | student_gid=pwd.getpwnam(from_user)[3] |
---|
81 | os.setgid(student_gid) |
---|
82 | os.setuid(student_uid) |
---|
83 | f=open(file_path,'rb') |
---|
84 | tmp=file_path.split("/") |
---|
85 | file_name=tmp[len(tmp)-1] |
---|
86 | |
---|
87 | s=socket.socket() |
---|
88 | ssl_socket=ssl.wrap_socket(s) |
---|
89 | ssl_socket.connect((ip,port)) |
---|
90 | ssl_socket.sendall("[N4D-HEADER-BEGIN]user="+from_user+";file="+file_name+"[N4D-HEADER-END]"+f.read()) |
---|
91 | ret_value=True |
---|
92 | ssl_socket.close() |
---|
93 | |
---|
94 | except Exception as e: |
---|
95 | print e |
---|
96 | |
---|
97 | |
---|
98 | queue.put(ret_value) |
---|
99 | |
---|
100 | #def send_to_teacher_socket_mp |
---|
101 | |
---|
102 | def send_to_teacher_net(self,from_user,to_user,file_path): |
---|
103 | |
---|
104 | file_path=file_path.encode("utf8") |
---|
105 | |
---|
106 | student_uid=pwd.getpwnam(from_user)[2] |
---|
107 | student_gid=pwd.getpwnam(from_user)[3] |
---|
108 | |
---|
109 | teacher_uid=pwd.getpwnam(to_user)[2] |
---|
110 | teacher_gid=pwd.getpwnam(to_user)[3] |
---|
111 | |
---|
112 | |
---|
113 | server = ServerProxy ("https://server:9779") |
---|
114 | |
---|
115 | try: |
---|
116 | paths=server.get_paths("","TeacherShareManager") |
---|
117 | |
---|
118 | except Exception as e: |
---|
119 | print e |
---|
120 | return False |
---|
121 | |
---|
122 | if to_user in paths: |
---|
123 | path,name,ip,port=paths[to_user] |
---|
124 | path=path.encode("utf8") |
---|
125 | name=name.encode("utf8") |
---|
126 | try: |
---|
127 | #STEP 1 --- TO /NET |
---|
128 | src=file_path |
---|
129 | tmp=file_path.split("/") |
---|
130 | file_name=tmp[len(tmp)-1] |
---|
131 | tmp_net_folder=NEW_NET_FOLDER.replace("%%USER%%",from_user) |
---|
132 | tmp_net_folder=tmp_net_folder.replace("%%STUDENT%%",from_user) |
---|
133 | dst=tmp_net_folder+"/["+from_user+"]_"+file_name |
---|
134 | queue=multiprocessing.Queue() |
---|
135 | p=multiprocessing.Process(target=self.copy_file_as_user,args=(src,dst,student_uid,student_gid,False,queue)) |
---|
136 | p.start() |
---|
137 | p.join() |
---|
138 | |
---|
139 | if not queue.get(): |
---|
140 | return False |
---|
141 | |
---|
142 | #STEP 2 -- TEACHER GRABS IT |
---|
143 | server=ServerProxy("https://"+ip+":9779") |
---|
144 | return server.grab_file("","TeacherShare",to_user,"["+from_user+"]_"+file_name,from_user) |
---|
145 | |
---|
146 | |
---|
147 | except Exception as e: |
---|
148 | |
---|
149 | print e |
---|
150 | return False |
---|
151 | |
---|
152 | |
---|
153 | #def send_to_teacher_net |
---|
154 | |
---|
155 | def grab_file(self,teacher,file_name,student): |
---|
156 | |
---|
157 | file_name=file_name.encode("utf8") |
---|
158 | try: |
---|
159 | server=ServerProxy("https://server:9779") |
---|
160 | paths=server.get_paths("","TeacherShareManager") |
---|
161 | if teacher in paths: |
---|
162 | path,name,ip,port=paths[teacher] |
---|
163 | path=path.encode("utf8") |
---|
164 | name=name.encode("utf8") |
---|
165 | tmp_net_folder=NEW_NET_FOLDER.replace("%%STUDENT%%",student) |
---|
166 | tmp_net_folder=tmp_net_folder.replace("%%USER%%",teacher) |
---|
167 | src=tmp_net_folder+file_name |
---|
168 | dst=path+file_name |
---|
169 | teacher_uid=pwd.getpwnam(teacher)[2] |
---|
170 | teacher_gid=pwd.getpwnam(teacher)[3] |
---|
171 | queue=multiprocessing.Queue() |
---|
172 | p=multiprocessing.Process(target=self.copy_file_as_user,args=(src,dst,teacher_uid,teacher_gid,True,queue)) |
---|
173 | p.start() |
---|
174 | p.join() |
---|
175 | return queue.get() |
---|
176 | |
---|
177 | else: |
---|
178 | print "[TeacherShare] Teacher " + teacher + " not sharing [!]" |
---|
179 | return False |
---|
180 | |
---|
181 | |
---|
182 | |
---|
183 | except Exception as e: |
---|
184 | print e |
---|
185 | return False |
---|
186 | |
---|
187 | #def grab_file |
---|
188 | |
---|
189 | def copy_file_as_user(self,src,dst,uid,gid,delete,queue): |
---|
190 | |
---|
191 | try: |
---|
192 | os.setgid(gid) |
---|
193 | os.setuid(uid) |
---|
194 | |
---|
195 | tmp=dst.split("/") |
---|
196 | folder=""+"/".join(tmp[0:len(tmp)-2]) |
---|
197 | prevmask = os.umask(0) |
---|
198 | |
---|
199 | if not os.path.exists(folder): |
---|
200 | os.makedirs(folder,02770) |
---|
201 | os.chmod(folder,02770) |
---|
202 | |
---|
203 | shutil.copyfile(src,dst) |
---|
204 | os.chmod(dst,0660) |
---|
205 | os.umask(prevmask) |
---|
206 | |
---|
207 | if delete: |
---|
208 | os.remove(src) |
---|
209 | queue.put(True) |
---|
210 | except Exception as e: |
---|
211 | print e |
---|
212 | queue.put(False) |
---|
213 | |
---|
214 | #def copy_file_as_user |
---|
215 | |
---|
216 | |
---|
217 | def send_to_teacher_socket(self,from_user,to_user,file_name): |
---|
218 | |
---|
219 | queue=multiprocessing.Queue() |
---|
220 | p=multiprocessing.Process(target=self.send_to_teacher_socket_p,args=(from_user,to_user, file_name,queue,)) |
---|
221 | p.start() |
---|
222 | p.join() |
---|
223 | return queue.get() |
---|
224 | |
---|
225 | #def send_to_teacher_socket |
---|
226 | |
---|
227 | def generate_file_name(self,from_user,file_name,path): |
---|
228 | |
---|
229 | |
---|
230 | |
---|
231 | done=False |
---|
232 | counter=1 |
---|
233 | |
---|
234 | original_path=path |
---|
235 | original_file_name=file_name |
---|
236 | file_name="["+from_user+"]_"+file_name |
---|
237 | |
---|
238 | |
---|
239 | while not done: |
---|
240 | |
---|
241 | if os.path.exists(path+file_name): |
---|
242 | tmp_list=original_file_name.split(".") |
---|
243 | tmp_list[0]=tmp_list[0]+"_("+str(counter)+")" |
---|
244 | file_name=".".join(tmp_list) |
---|
245 | file_name="["+from_user+"]_"+file_name |
---|
246 | counter+=1 |
---|
247 | else: |
---|
248 | done=True |
---|
249 | |
---|
250 | return path+file_name |
---|
251 | |
---|
252 | |
---|
253 | |
---|
254 | |
---|
255 | |
---|
256 | #def generate_file_name |
---|
257 | |
---|
258 | |
---|
259 | #class TeacherFileManager |
---|
260 | |
---|
261 | if __name__=="__main__": |
---|
262 | |
---|
263 | tfm=TeacherShare() |
---|
264 | tfm.send_to_teacher_socket(1,2,3) |
---|
265 | #print tfm.generate_file_name("hector","hola.txt","/tmp/test/") |
---|