# -*- coding: utf-8 -*- import os.path import base64 import os import pwd import multiprocessing import socket import ssl import shutil from xmlrpclib import * NET="/net/server-sync/" NET_FOLDER="/net/server-sync/home/.lliurex-harvester/" ORIG_NET_FOLDER=NET_FOLDER NEW_NET_FOLDER="/net/server-sync/home/students/%%STUDENT%%/.harvester/" if not os.path.exists(NET): NET_FOLDER="/run/%%USER%%/home/.lliurex-harvester/" NEW_NET_FOLDER="/run/%%USER%%/home/students/%%STUDENT%%/.harvester/" class TeacherShare: def send_to_teacher(self,from_user,to_user,file_name,file_in_b64): file_name=file_name.encode("utf-8") server = ServerProxy ("https://server:9779") try: self.paths=server.get_paths("","TeacherShareManager") except Exception as e: print e return False if to_user in self.paths: try: path,ip,name=self.paths[to_user] fname=self.generate_file_name(from_user,file_name,path) f=open(fname,'w') f.write(base64.b64decode(file_in_b64)) f.close() os.chown(fname,pwd.getpwnam(to_user)[2],pwd.getpwnam(to_user)[3]) return True except Exception as e: print e return False else: return False #def send_to_users def send_to_teacher_socket_p(self,from_user,to_user,file_path,queue): ret_value=False file_path=file_path.encode("utf-8") server = ServerProxy ("https://server:9779") try: paths=server.get_paths("","TeacherShareManager") except Exception as e: print e if to_user in paths: try: path,name,ip,port=paths[to_user] student_uid=pwd.getpwnam(from_user)[2] student_gid=pwd.getpwnam(from_user)[3] os.setgid(student_gid) os.setuid(student_uid) f=open(file_path,'rb') tmp=file_path.split("/") file_name=tmp[len(tmp)-1] s=socket.socket() ssl_socket=ssl.wrap_socket(s) ssl_socket.connect((ip,port)) ssl_socket.sendall("[N4D-HEADER-BEGIN]user="+from_user+";file="+file_name+"[N4D-HEADER-END]"+f.read()) ret_value=True ssl_socket.close() except Exception as e: print e queue.put(ret_value) #def send_to_teacher_socket_mp def send_to_teacher_net(self,from_user,to_user,file_path): file_path=file_path.encode("utf8") student_uid=pwd.getpwnam(from_user)[2] student_gid=pwd.getpwnam(from_user)[3] teacher_uid=pwd.getpwnam(to_user)[2] teacher_gid=pwd.getpwnam(to_user)[3] server = ServerProxy ("https://server:9779") try: paths=server.get_paths("","TeacherShareManager") except Exception as e: print e return False if to_user in paths: path,name,ip,port=paths[to_user] path=path.encode("utf8") name=name.encode("utf8") try: #STEP 1 --- TO /NET src=file_path tmp=file_path.split("/") file_name=tmp[len(tmp)-1] tmp_net_folder=NEW_NET_FOLDER.replace("%%USER%%",from_user) tmp_net_folder=tmp_net_folder.replace("%%STUDENT%%",from_user) dst=tmp_net_folder+"/["+from_user+"]_"+file_name queue=multiprocessing.Queue() p=multiprocessing.Process(target=self.copy_file_as_user,args=(src,dst,student_uid,student_gid,False,queue)) p.start() p.join() if not queue.get(): return False #STEP 2 -- TEACHER GRABS IT server=ServerProxy("https://"+ip+":9779") return server.grab_file("","TeacherShare",to_user,"["+from_user+"]_"+file_name,from_user) except Exception as e: print e return False #def send_to_teacher_net def grab_file(self,teacher,file_name,student): file_name=file_name.encode("utf8") try: server=ServerProxy("https://server:9779") paths=server.get_paths("","TeacherShareManager") if teacher in paths: path,name,ip,port=paths[teacher] path=path.encode("utf8") name=name.encode("utf8") tmp_net_folder=NEW_NET_FOLDER.replace("%%STUDENT%%",student) tmp_net_folder=tmp_net_folder.replace("%%USER%%",teacher) src=tmp_net_folder+file_name dst=path+file_name teacher_uid=pwd.getpwnam(teacher)[2] teacher_gid=pwd.getpwnam(teacher)[3] queue=multiprocessing.Queue() p=multiprocessing.Process(target=self.copy_file_as_user,args=(src,dst,teacher_uid,teacher_gid,True,queue)) p.start() p.join() return queue.get() else: print "[TeacherShare] Teacher " + teacher + " not sharing [!]" return False except Exception as e: print e return False #def grab_file def copy_file_as_user(self,src,dst,uid,gid,delete,queue): try: os.setgid(gid) os.setuid(uid) tmp=dst.split("/") folder=""+"/".join(tmp[0:len(tmp)-2]) prevmask = os.umask(0) if not os.path.exists(folder): os.makedirs(folder,02770) os.chmod(folder,02770) shutil.copyfile(src,dst) os.chmod(dst,0660) os.umask(prevmask) if delete: os.remove(src) queue.put(True) except Exception as e: print e queue.put(False) #def copy_file_as_user def send_to_teacher_socket(self,from_user,to_user,file_name): queue=multiprocessing.Queue() p=multiprocessing.Process(target=self.send_to_teacher_socket_p,args=(from_user,to_user, file_name,queue,)) p.start() p.join() return queue.get() #def send_to_teacher_socket def generate_file_name(self,from_user,file_name,path): done=False counter=1 original_path=path original_file_name=file_name file_name="["+from_user+"]_"+file_name while not done: if os.path.exists(path+file_name): tmp_list=original_file_name.split(".") tmp_list[0]=tmp_list[0]+"_("+str(counter)+")" file_name=".".join(tmp_list) file_name="["+from_user+"]_"+file_name counter+=1 else: done=True return path+file_name #def generate_file_name #class TeacherFileManager if __name__=="__main__": tfm=TeacherShare() tfm.send_to_teacher_socket(1,2,3) #print tfm.generate_file_name("hector","hola.txt","/tmp/test/")