1 | # -*- coding: utf-8 -*- |
---|
2 | import imp |
---|
3 | import ldap |
---|
4 | import sys |
---|
5 | import subprocess |
---|
6 | import grp |
---|
7 | import shutil |
---|
8 | import threading |
---|
9 | import magic |
---|
10 | import pyinotify |
---|
11 | import time |
---|
12 | from pyinotify import WatchManager, Notifier, ThreadedNotifier, EventsCodes, ProcessEvent |
---|
13 | |
---|
14 | |
---|
15 | class Golem: |
---|
16 | |
---|
17 | |
---|
18 | PLUGIN_PATH="/usr/share/n4d/python-plugins/" |
---|
19 | LDAP_LOG="/var/lib/ldap/" |
---|
20 | |
---|
21 | |
---|
22 | def startup(self,options): |
---|
23 | |
---|
24 | |
---|
25 | try: |
---|
26 | self.mime=magic.open(magic.MAGIC_MIME) |
---|
27 | self.mime.load() |
---|
28 | self.obj=imp.load_source("LdapManager",Golem.PLUGIN_PATH + "LdapManager.py") |
---|
29 | obj3=imp.load_source("NetFilesManager",Golem.PLUGIN_PATH + "NetFilesManager.py") |
---|
30 | obj4=imp.load_source("PasswordManager",Golem.PLUGIN_PATH + "PasswordManager.py") |
---|
31 | obj5=imp.load_source("GesItaManager",Golem.PLUGIN_PATH + "GesItaManager.py") |
---|
32 | obj6=imp.load_source("FileOperations",Golem.PLUGIN_PATH + "FileOperations.py") |
---|
33 | obj7=imp.load_source("PeterPan",Golem.PLUGIN_PATH + "PeterPan.py") |
---|
34 | self.ldap=self.obj.LdapManager(llxvars) |
---|
35 | self.netfiles=obj3.NetFilesManager(llxvars) |
---|
36 | self.pw=obj4.PasswordManager() |
---|
37 | self.itaca=obj5.GesItaManager(llxvars,self,'llxgesc.xml') |
---|
38 | self.file_operations=obj6.FileOperations() |
---|
39 | self.peter_pan=obj7.PeterPan() |
---|
40 | self.try_count=0 |
---|
41 | self.sharefunctions = {} |
---|
42 | |
---|
43 | |
---|
44 | |
---|
45 | if objects["VariablesManager"].get_variable("MASTER_SERVER_IP")!=None: |
---|
46 | p=subprocess.Popen(["gluster volume info"],shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()[1] |
---|
47 | if 'No volumes present' in p: |
---|
48 | #Light version. Does not chown existing net files |
---|
49 | self.regenerate_net_files(1) |
---|
50 | self.start_inotify() |
---|
51 | |
---|
52 | except Exception as e: |
---|
53 | print e |
---|
54 | |
---|
55 | #def __init__ |
---|
56 | |
---|
57 | |
---|
58 | def start_inotify(self): |
---|
59 | |
---|
60 | t=threading.Thread(target=self._inotify) |
---|
61 | t.daemon=True |
---|
62 | t.start() |
---|
63 | |
---|
64 | #def start_inotify |
---|
65 | |
---|
66 | def _inotify(self): |
---|
67 | |
---|
68 | wm=WatchManager() |
---|
69 | mask=pyinotify.ALL_EVENTS |
---|
70 | |
---|
71 | class Process_handler(ProcessEvent): |
---|
72 | |
---|
73 | def __init__(self,main): |
---|
74 | |
---|
75 | self.main=main |
---|
76 | self.count=0 |
---|
77 | self.in_modify=False |
---|
78 | |
---|
79 | def process_IN_MODIFY(self,event): |
---|
80 | if not self.in_modify: |
---|
81 | self.in_modify=True |
---|
82 | time.sleep(2) |
---|
83 | # light version. Does not chown existing net files |
---|
84 | self.main.regenerate_net_files(1) |
---|
85 | time.sleep(2) |
---|
86 | self.in_modify=False |
---|
87 | |
---|
88 | |
---|
89 | |
---|
90 | notifier=Notifier(wm,Process_handler(self)) |
---|
91 | wdd=wm.add_watch(Golem.LDAP_LOG,mask,rec=True) |
---|
92 | |
---|
93 | while True: |
---|
94 | try: |
---|
95 | |
---|
96 | notifier.process_events() |
---|
97 | if notifier.check_events(): |
---|
98 | notifier.read_events() |
---|
99 | |
---|
100 | except Exception as e: |
---|
101 | print e |
---|
102 | notifier.stop() |
---|
103 | |
---|
104 | return False |
---|
105 | |
---|
106 | #def _inotify |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | def _restore_groups_folders(self): |
---|
111 | |
---|
112 | t=threading.Thread(target=self.restore_groups_folders) |
---|
113 | t.daemon=True |
---|
114 | t.start() |
---|
115 | |
---|
116 | #def |
---|
117 | |
---|
118 | def add_user(self,plantille,properties,generic_mode=False): |
---|
119 | |
---|
120 | generated_user=None |
---|
121 | properties["uid"]=properties["uid"].encode("utf8") |
---|
122 | properties["cn"]=properties["cn"].encode("utf8") |
---|
123 | properties["sn"]=properties["sn"].encode("utf8") |
---|
124 | |
---|
125 | if properties.has_key("userPassword"): |
---|
126 | properties["userPassword"]=properties["userPassword"].encode("utf8") |
---|
127 | |
---|
128 | if type(generic_mode)==type(True) and generic_mode: |
---|
129 | generated_user=self.ldap.add_user(generic_mode,plantille,properties) |
---|
130 | else: |
---|
131 | generated_user=self.ldap.add_user(False,plantille,properties) |
---|
132 | |
---|
133 | if type(generated_user) is dict: |
---|
134 | |
---|
135 | homepath = self.netfiles.exist_home_or_create(generated_user) |
---|
136 | if plantille=="Teachers" or plantille=="Others": |
---|
137 | self.pw.add_password(generated_user["uid"],generated_user["cn"],generated_user["sn"],generated_user["userPassword"]) |
---|
138 | properties["group_type"]=plantille |
---|
139 | self.peter_pan.execute_python_dir('/usr/share/n4d/hooks/golem','add_user',properties) |
---|
140 | self.peter_pan.execute_python_dir('/usr/share/n4d/hooks/openmeetings','add_user',[properties]) |
---|
141 | return "true: " + generated_user["uid"] |
---|
142 | else: |
---|
143 | return generated_user |
---|
144 | |
---|
145 | #def add_user |
---|
146 | |
---|
147 | |
---|
148 | def add_generic_users(self,plantille,group_type,number,generic_name,pwd_generation_type,pwd=None): |
---|
149 | |
---|
150 | generated_list=self.ldap.add_generic_users(plantille,group_type,number,generic_name,pwd_generation_type,pwd) |
---|
151 | for item in generated_list: |
---|
152 | # |
---|
153 | # Item {uid:name,userPassword:password} |
---|
154 | # |
---|
155 | |
---|
156 | homepath = self.netfiles.exist_home_or_create(item) |
---|
157 | |
---|
158 | #print "password saving..." |
---|
159 | if plantille=="Teachers" or plantille=="Others": |
---|
160 | self.pw.add_password(item["uid"],item["cn"],item["sn"],item["userPassword"]) |
---|
161 | self.peter_pan.execute_python_dir('/usr/share/n4d/hooks/golem',('add_generic_users'),{'group':group_type,'user':item}) |
---|
162 | |
---|
163 | properties = {} |
---|
164 | properties['group_type'] = plantille |
---|
165 | properties['uid'] = item['uid'] |
---|
166 | properties['cn'] = item['uid'] |
---|
167 | properties['sn'] = item['uid'] |
---|
168 | self.peter_pan.execute_python_dir('/usr/share/n4d/hooks/openmeetings','add_user',[properties]) |
---|
169 | return generated_list |
---|
170 | |
---|
171 | |
---|
172 | #def add_generic_users |
---|
173 | |
---|
174 | def add_admin(self,user_name): |
---|
175 | # existing system user |
---|
176 | |
---|
177 | try: |
---|
178 | uid=pwd.getpwnam(user_name).pw_uid |
---|
179 | |
---|
180 | properties={} |
---|
181 | properties["uid"]=user_name |
---|
182 | properties["cn"]=user_name |
---|
183 | properties["sn"]=user_name |
---|
184 | properties["userPassword"]=uid |
---|
185 | properties["uidNumber"]=os.environ["SUDO_UID"] |
---|
186 | |
---|
187 | self.ldap.add_user(False,"Admin",properties) |
---|
188 | return True |
---|
189 | |
---|
190 | except Exception as e: |
---|
191 | |
---|
192 | return [False,e.message] |
---|
193 | |
---|
194 | |
---|
195 | |
---|
196 | #def add_admin |
---|
197 | |
---|
198 | def login(self,user_info): |
---|
199 | |
---|
200 | uid,password=user_info |
---|
201 | |
---|
202 | dic={} |
---|
203 | p = subprocess.Popen(["groups",uid],stdout = subprocess.PIPE,stderr = subprocess.PIPE) |
---|
204 | output = p.communicate()[0] |
---|
205 | output=output.replace("\n","") |
---|
206 | |
---|
207 | dic["groups"]=output |
---|
208 | |
---|
209 | students="ou=Students,ou=People," + llxvars("LDAP_BASE_DN") |
---|
210 | teachers="ou=Teachers,ou=People," + llxvars("LDAP_BASE_DN") |
---|
211 | admins="ou=Admins,ou=People," + llxvars("LDAP_BASE_DN") |
---|
212 | |
---|
213 | group_type="None" |
---|
214 | |
---|
215 | if output.find("students")!=-1: |
---|
216 | dic["path"]="uid=" + uid + "," + students |
---|
217 | group_type="students" |
---|
218 | |
---|
219 | if output.find("teachers")!=-1: |
---|
220 | dic["path"]="uid=" + uid + "," + teachers |
---|
221 | group_type="teachers" |
---|
222 | |
---|
223 | if output.find("admins")!=-1 and output.find("teachers")!=-1: |
---|
224 | dic["path"]="uid=" + uid + "," + teachers |
---|
225 | group_type="promoted-teacher" |
---|
226 | |
---|
227 | if output.find("adm")!=-1: |
---|
228 | dic["path"]="uid=" + uid + "," + admins |
---|
229 | group_type="admin" |
---|
230 | #return "true " + group_type |
---|
231 | |
---|
232 | if "NTicketsManager" in objects: |
---|
233 | if objects["NTicketsManager"].validate_user(uid,password): |
---|
234 | return "true " + group_type |
---|
235 | |
---|
236 | if validate_user(uid,password)[0]: |
---|
237 | return "true "+ group_type |
---|
238 | else: |
---|
239 | return "false" |
---|
240 | |
---|
241 | |
---|
242 | #def login |
---|
243 | |
---|
244 | |
---|
245 | |
---|
246 | def change_own_password(self,user_info,new_password): |
---|
247 | |
---|
248 | uid,password=user_info |
---|
249 | dic={} |
---|
250 | p = subprocess.Popen(["groups",uid],stdout = subprocess.PIPE,stderr = subprocess.PIPE) |
---|
251 | output = p.communicate()[0] |
---|
252 | output=output.replace("\n","") |
---|
253 | |
---|
254 | dic["groups"]=output |
---|
255 | |
---|
256 | students="ou=Students,ou=People," + llxvars("LDAP_BASE_DN") |
---|
257 | teachers="ou=Teachers,ou=People," + llxvars("LDAP_BASE_DN") |
---|
258 | admin="ou=Admins,ou=People," + llxvars("LDAP_BASE_DN") |
---|
259 | others="ou=Other,ou=People," + llxvars("LDAP_BASE_DN") |
---|
260 | |
---|
261 | if output.find("students")!=-1: |
---|
262 | path="uid=" + uid + "," + students |
---|
263 | elif output.find("teachers")!=-1: |
---|
264 | path="uid=" + uid + "," + teachers |
---|
265 | elif output.find("others")!=-1: |
---|
266 | path="uid=" + uid + "," + others |
---|
267 | elif output.find("admin")!=-1: |
---|
268 | path="uid=" + uid + "," + admin |
---|
269 | else: |
---|
270 | return "false" |
---|
271 | |
---|
272 | dic["path"]=path |
---|
273 | |
---|
274 | #dic["llxvars"]=llxvars |
---|
275 | |
---|
276 | try: |
---|
277 | |
---|
278 | tmp_ldap=ldap.initialize(llxvars("CLIENT_LDAP_URI")) |
---|
279 | dic["a"]="initialize" |
---|
280 | tmp_ldap.set_option(ldap.VERSION,ldap.VERSION3) |
---|
281 | dic["b"]="set_option" |
---|
282 | tmp_ldap.bind_s(path,password) |
---|
283 | dic["c"]="bind" |
---|
284 | self.ldap.change_password(path,new_password) |
---|
285 | dic["d"]="ldap password" |
---|
286 | |
---|
287 | if "Teachers" in path: |
---|
288 | self.pw.set_externally_modified(uid) |
---|
289 | |
---|
290 | return "true" |
---|
291 | |
---|
292 | except Exception as inst: |
---|
293 | |
---|
294 | |
---|
295 | |
---|
296 | dic["exception"]=inst |
---|
297 | |
---|
298 | return "false" |
---|
299 | |
---|
300 | #def change_own_password |
---|
301 | |
---|
302 | |
---|
303 | |
---|
304 | |
---|
305 | def delete_student(self,uid,delete_data=True): |
---|
306 | |
---|
307 | user_info={} |
---|
308 | user_info["uid"]=uid |
---|
309 | user_info["profile"]="students" |
---|
310 | |
---|
311 | #self.unfreeze_user(uid) |
---|
312 | |
---|
313 | |
---|
314 | if delete_data==True: |
---|
315 | homepath = self.netfiles.delete_home(user_info) |
---|
316 | |
---|
317 | os.system("rm -rf /home/%s"%uid) |
---|
318 | |
---|
319 | ret=self.ldap.delete_student(uid) |
---|
320 | |
---|
321 | self.peter_pan.execute_python_dir('/usr/share/n4d/hooks/golem','delete_student') |
---|
322 | properties = {} |
---|
323 | properties['uid'] = uid |
---|
324 | properties['group_type'] = 'Students' |
---|
325 | self.peter_pan.execute_python_dir('/usr/share/n4d/hooks/openmeetings','delete_user',[properties]) |
---|
326 | return ret |
---|
327 | |
---|
328 | #def delete_student |
---|
329 | |
---|
330 | |
---|
331 | |
---|
332 | def delete_teacher(self,uid,delete_data=True): |
---|
333 | |
---|
334 | user_info={} |
---|
335 | user_info["uid"]=uid |
---|
336 | user_info["profile"]="teachers" |
---|
337 | |
---|
338 | #self.unfreeze_user(uid) |
---|
339 | |
---|
340 | if delete_data==True: |
---|
341 | homepath = self.netfiles.delete_home(user_info) |
---|
342 | |
---|
343 | os.system("rm -rf /home/%s"%uid) |
---|
344 | |
---|
345 | self.pw.remove_password(uid) |
---|
346 | ret=self.ldap.delete_teacher(uid) |
---|
347 | |
---|
348 | self.peter_pan.execute_python_dir('/usr/share/n4d/hooks/golem','delete_teacher') |
---|
349 | properties = {} |
---|
350 | properties['uid'] = uid |
---|
351 | properties['group_type'] = 'Teachers' |
---|
352 | self.peter_pan.execute_python_dir('/usr/share/n4d/hooks/openmeetings','delete_user',[properties]) |
---|
353 | |
---|
354 | return ret |
---|
355 | #def delete_teacher |
---|
356 | |
---|
357 | def delete_other(self,uid,delete_data=True): |
---|
358 | |
---|
359 | user_info={} |
---|
360 | user_info["uid"]=uid |
---|
361 | user_info["profile"]="others" |
---|
362 | |
---|
363 | self.unfreeze_user(uid) |
---|
364 | |
---|
365 | |
---|
366 | if delete_data==True: |
---|
367 | homepath = self.netfiles.delete_home(user_info) |
---|
368 | |
---|
369 | |
---|
370 | self.pw.remove_password(uid) |
---|
371 | |
---|
372 | ret=self.ldap.delete_other(uid) |
---|
373 | self.peter_pan.execute_python_dir('/usr/share/n4d/hooks/golem','delete_other') |
---|
374 | properties = {} |
---|
375 | properties['uid'] = uid |
---|
376 | properties['group_type'] = 'Others' |
---|
377 | self.peter_pan.execute_python_dir('/usr/share/n4d/hooks/openmeetings','delete_user',[properties]) |
---|
378 | |
---|
379 | return ret |
---|
380 | |
---|
381 | #def delete_other |
---|
382 | |
---|
383 | def delete_students(self,delete_data=True): |
---|
384 | |
---|
385 | list=self.ldap.search_user("*") |
---|
386 | |
---|
387 | ret_list=[] |
---|
388 | |
---|
389 | for item in list: |
---|
390 | if item.properties["path"].find("ou=Students")!=-1: |
---|
391 | ret=self.delete_student(item.properties["uid"],delete_data) |
---|
392 | ret_list.append(item.properties["uid"] +":"+ret) |
---|
393 | |
---|
394 | return ret_list |
---|
395 | |
---|
396 | |
---|
397 | |
---|
398 | #def delete_students |
---|
399 | |
---|
400 | def delete_teachers(self,delete_data=True): |
---|
401 | |
---|
402 | list=self.ldap.search_user("*") |
---|
403 | |
---|
404 | ret_list=[] |
---|
405 | |
---|
406 | for item in list: |
---|
407 | if item.properties["path"].find("ou=Teachers")!=-1: |
---|
408 | ret=self.delete_teacher(item.properties["uid"],delete_data) |
---|
409 | ret_list.append(item.properties["uid"] +":"+ret) |
---|
410 | |
---|
411 | return ret_list |
---|
412 | |
---|
413 | |
---|
414 | #def delete_students |
---|
415 | |
---|
416 | |
---|
417 | def delete_all(self,delete_data=True): |
---|
418 | |
---|
419 | list=self.ldap.search_user("*") |
---|
420 | |
---|
421 | ok=True |
---|
422 | |
---|
423 | ret_list=[] |
---|
424 | |
---|
425 | for item in list: |
---|
426 | if item.properties["path"].find("ou=Teachers")!=-1: |
---|
427 | ret=self.delete_teacher(item.properties["uid"],delete_data) |
---|
428 | ret_list.append(item.properties["uid"] +":"+ret) |
---|
429 | |
---|
430 | if item.properties["path"].find("ou=Students")!=-1: |
---|
431 | ret=self.delete_student(item.properties["uid"],delete_data) |
---|
432 | ret_list.append(item.properties["uid"] +":"+ret) |
---|
433 | |
---|
434 | if item.properties["path"].find("ou=Other")!=-1: |
---|
435 | ret=self.delete_other(item.properties["uid"],delete_data) |
---|
436 | ret_list.append(item.properties["uid"] +":"+ret) |
---|
437 | |
---|
438 | return ret_list |
---|
439 | |
---|
440 | #def delete_students |
---|
441 | |
---|
442 | def get_students_function_list(self): |
---|
443 | return students_func_list |
---|
444 | |
---|
445 | def get_teachers_function_list(self): |
---|
446 | return teachers_func_list |
---|
447 | |
---|
448 | def get_admin_function_list(self): |
---|
449 | return admin_func_list |
---|
450 | |
---|
451 | def get_others_function_list(self): |
---|
452 | return others_func_list |
---|
453 | |
---|
454 | def get_student_list(self): |
---|
455 | |
---|
456 | list=self.ldap.search_students("*") |
---|
457 | |
---|
458 | return_list=[] |
---|
459 | |
---|
460 | for item in list: |
---|
461 | return_list.append(item.properties) |
---|
462 | |
---|
463 | return return_list |
---|
464 | |
---|
465 | def get_teacher_list(self): |
---|
466 | |
---|
467 | list=self.ldap.search_teachers("*") |
---|
468 | |
---|
469 | return_list=[] |
---|
470 | |
---|
471 | for item in list: |
---|
472 | return_list.append(item.properties) |
---|
473 | |
---|
474 | return return_list |
---|
475 | |
---|
476 | def get_user_list(self,filter): |
---|
477 | |
---|
478 | list=self.ldap.search_user(filter) |
---|
479 | |
---|
480 | |
---|
481 | #return self.ldap.light_search(filter) |
---|
482 | |
---|
483 | return_list=[] |
---|
484 | for item in list: |
---|
485 | return_list.append(item.properties) |
---|
486 | |
---|
487 | return return_list |
---|
488 | |
---|
489 | |
---|
490 | #def get_user_list |
---|
491 | |
---|
492 | def light_get_user_list(self): |
---|
493 | |
---|
494 | list=self.ldap.light_search() |
---|
495 | |
---|
496 | |
---|
497 | return list |
---|
498 | |
---|
499 | #def light_get_user_list |
---|
500 | |
---|
501 | def get_available_groups(self): |
---|
502 | |
---|
503 | return self.ldap.get_available_groups() |
---|
504 | |
---|
505 | #def get_available_groups |
---|
506 | |
---|
507 | |
---|
508 | def add_to_group(self,uid,group): |
---|
509 | |
---|
510 | result=self.ldap.add_to_group_type(group,uid) |
---|
511 | |
---|
512 | user_info={} |
---|
513 | user_info["uid"]=uid |
---|
514 | |
---|
515 | path=self.ldap.get_dn(uid) |
---|
516 | |
---|
517 | if path.find("ou=Students")!=-1: |
---|
518 | user_info["profile"]="students" |
---|
519 | if path.find("ou=Teachers")!=-1: |
---|
520 | user_info["profile"]="teachers" |
---|
521 | if path.find("ou=Other")!=-1: |
---|
522 | user_info["profile"]="others" |
---|
523 | |
---|
524 | |
---|
525 | |
---|
526 | self.peter_pan.execute_python_dir('/usr/share/n4d/hooks/golem',('add_to_group'),{'group':{'cn':group},'user':user_info}) |
---|
527 | #return must be "true" (string) |
---|
528 | |
---|
529 | return result |
---|
530 | |
---|
531 | #def add_to_group |
---|
532 | |
---|
533 | def remove_from_group(self,uid,group): |
---|
534 | |
---|
535 | result=self.ldap.del_user_from_group(uid,group) |
---|
536 | user_info={} |
---|
537 | user_info["uid"]=uid |
---|
538 | |
---|
539 | |
---|
540 | |
---|
541 | #return must be "true" (string) |
---|
542 | |
---|
543 | self.peter_pan.execute_python_dir('/usr/share/n4d/hooks/golem',('remove_from_group'),{'group':{'cn':group},'user':user_info}) |
---|
544 | return result |
---|
545 | |
---|
546 | #def remove_from_group |
---|
547 | |
---|
548 | |
---|
549 | def change_student_personal_data(self,uid,name,surname): |
---|
550 | name=unicode(name).encode("utf8") |
---|
551 | surname=unicode(surname).encode("utf8") |
---|
552 | result=self.ldap.change_student_name(uid,name) |
---|
553 | #print result |
---|
554 | result2=self.ldap.change_student_surname(uid,surname) |
---|
555 | #print result2 |
---|
556 | if result==result2 and result=="true": |
---|
557 | |
---|
558 | # TODO |
---|
559 | # Execute hook to moodle |
---|
560 | |
---|
561 | |
---|
562 | properties = {} |
---|
563 | properties['group_type'] = 'Students' |
---|
564 | properties['uid'] = uid |
---|
565 | properties['cn'] = name |
---|
566 | properties['sn'] = surname |
---|
567 | self.peter_pan.execute_python_dir('/usr/share/n4d/hooks/openmeetings','update_user',[properties]) |
---|
568 | |
---|
569 | return result |
---|
570 | else: |
---|
571 | return result + "," + result2 |
---|
572 | |
---|
573 | #def change_personal_data |
---|
574 | |
---|
575 | def change_password(self,path,password,uid="",cn="",sn="",auto=False): |
---|
576 | |
---|
577 | password=unicode(password).encode("utf8") |
---|
578 | result=self.ldap.change_password(path,password) |
---|
579 | |
---|
580 | #trying to obtain user uid |
---|
581 | list=path.split(",") |
---|
582 | uid=list[0].split("=")[1] |
---|
583 | |
---|
584 | |
---|
585 | #return=="true" |
---|
586 | |
---|
587 | if uid!="" and cn!="" and sn!="": |
---|
588 | self.pw.add_password(uid,cn,sn,password) |
---|
589 | |
---|
590 | if not auto: |
---|
591 | if "Teachers" in path: |
---|
592 | self.pw.set_externally_modified(uid) |
---|
593 | |
---|
594 | return result |
---|
595 | |
---|
596 | #def change_student_password |
---|
597 | |
---|
598 | def change_student_password(self,uid,password): |
---|
599 | |
---|
600 | result=self.ldap.change_user_password(uid,password) |
---|
601 | |
---|
602 | #return=="true" |
---|
603 | |
---|
604 | |
---|
605 | return result |
---|
606 | |
---|
607 | #def change_student_password |
---|
608 | def freeze_user(self,uid_list): |
---|
609 | self.ldap.freeze_user(uid_list) |
---|
610 | return 0 |
---|
611 | #def freeze_user |
---|
612 | |
---|
613 | def freeze_group(self,cn): |
---|
614 | self.ldap.freeze_group(cn) |
---|
615 | return 0 |
---|
616 | #def freeze_group |
---|
617 | |
---|
618 | def unfreeze_user(self,uid_list): |
---|
619 | self.ldap.unfreeze_user(uid_list) |
---|
620 | return 0 |
---|
621 | #def unfreeze_user |
---|
622 | |
---|
623 | def unfreeze_group(self,cn): |
---|
624 | self.ldap.unfreeze_group(cn) |
---|
625 | return 0 |
---|
626 | #def unfreeze_group |
---|
627 | |
---|
628 | def add_teacher_to_admins(self,uid): |
---|
629 | |
---|
630 | result=self.ldap.add_teacher_to_admins(uid) |
---|
631 | |
---|
632 | return result |
---|
633 | |
---|
634 | #def add_teacher_to_admins |
---|
635 | |
---|
636 | def del_teacher_from_admins(self,uid): |
---|
637 | |
---|
638 | result=self.ldap.del_teacher_from_admins(uid) |
---|
639 | return result |
---|
640 | |
---|
641 | #def de_teacher_from_admins |
---|
642 | |
---|
643 | |
---|
644 | def change_group_description(self,gid,description): |
---|
645 | |
---|
646 | description=unicode(description).encode("utf8") |
---|
647 | result=self.ldap.change_group_description(gid,description) |
---|
648 | |
---|
649 | return result |
---|
650 | |
---|
651 | #def change_group_description |
---|
652 | |
---|
653 | def delete_group(self,group_name): |
---|
654 | |
---|
655 | #self.unfreeze_group(gid) |
---|
656 | result=self.ldap.delete_group(group_name) |
---|
657 | try: |
---|
658 | self.netfiles.remove_group_folder(group_name) |
---|
659 | except Exception as e: |
---|
660 | print(e) |
---|
661 | |
---|
662 | self.peter_pan.execute_python_dir('/usr/share/n4d/hooks/golem',('delete_group'),{'group':{'cn':group_name}}) |
---|
663 | return result |
---|
664 | |
---|
665 | #def delete_group |
---|
666 | |
---|
667 | |
---|
668 | def add_group(self,properties): |
---|
669 | properties["description"]=unicode(properties["description"]).encode("utf8") |
---|
670 | result=self.ldap.add_group(properties) |
---|
671 | |
---|
672 | try: |
---|
673 | self.create_group_folder(properties["cn"]) |
---|
674 | except Exception as e: |
---|
675 | print(e) |
---|
676 | |
---|
677 | try: |
---|
678 | self.peter_pan.execute_python_dir('/usr/share/n4d/hooks/golem',('add_group'),{'group':properties}) |
---|
679 | except Exception as e: |
---|
680 | print(e) |
---|
681 | return result |
---|
682 | |
---|
683 | #def add_group |
---|
684 | |
---|
685 | def get_students_passwords(self): |
---|
686 | |
---|
687 | list = self.ldap.get_students_passwords() |
---|
688 | |
---|
689 | return self.quicksort(list) |
---|
690 | |
---|
691 | #def get_students_passwords |
---|
692 | |
---|
693 | def get_teachers_passwords(self): |
---|
694 | |
---|
695 | ret=self.light_get_user_list() |
---|
696 | |
---|
697 | tmp_teachers={} |
---|
698 | |
---|
699 | for item in ret: |
---|
700 | |
---|
701 | if item[-1]=="teachers": |
---|
702 | teacher={} |
---|
703 | teacher["uid"]=item[1] |
---|
704 | teacher["cn"]=item[3] |
---|
705 | teacher["sn"]=item[4] |
---|
706 | teacher["passwd"]=" #! UNKNOWN PASSWORD !# " |
---|
707 | tmp_teachers[item[1]]=teacher |
---|
708 | |
---|
709 | |
---|
710 | ret2=self.quicksort(self.pw.get_passwords()) |
---|
711 | |
---|
712 | final_ret=[] |
---|
713 | |
---|
714 | for item in ret2: |
---|
715 | if item["uid"] in tmp_teachers: |
---|
716 | tmp_teachers[item["uid"]]["passwd"]=item["passwd"] |
---|
717 | |
---|
718 | |
---|
719 | for item in tmp_teachers: |
---|
720 | final_ret.append(tmp_teachers[item]) |
---|
721 | |
---|
722 | |
---|
723 | return final_ret |
---|
724 | |
---|
725 | #def get_teachers_passwords |
---|
726 | |
---|
727 | def get_all_passwords(self,force_teachers=False): |
---|
728 | list=self.ldap.get_students_passwords() |
---|
729 | if not force_teachers: |
---|
730 | list2=self.pw.get_passwords() |
---|
731 | else: |
---|
732 | list2=self.ldap.get_teachers_passwords() |
---|
733 | for item in list2: |
---|
734 | list.append(item) |
---|
735 | return self.quicksort(list) |
---|
736 | |
---|
737 | #def get_all_passwords |
---|
738 | |
---|
739 | def quicksort (self,lista): |
---|
740 | self.sort_quicksort(lista,0,len(lista)-1) |
---|
741 | return lista |
---|
742 | |
---|
743 | #def quicksort |
---|
744 | |
---|
745 | def sort_quicksort (self,lista,izdo,dcho) : |
---|
746 | if izdo<dcho : |
---|
747 | pivote=lista[(izdo+dcho)/2] |
---|
748 | i,d=izdo,dcho |
---|
749 | while i<=d : |
---|
750 | while lista[i]['sn'].lower()<pivote['sn'].lower() : i+=1 |
---|
751 | while lista[d]['sn'].lower()>pivote['sn'].lower() : d-=1 |
---|
752 | if i<=d : |
---|
753 | lista[i],lista[d]=lista[d],lista[i] |
---|
754 | i+=1 |
---|
755 | d-=1 |
---|
756 | if izdo<d : self.sort_quicksort(lista,izdo,d) |
---|
757 | if i<dcho : self.sort_quicksort(lista,i,dcho) |
---|
758 | return lista |
---|
759 | #def sort_quicksort |
---|
760 | |
---|
761 | |
---|
762 | def generic_student_to_itaca(self,uid,nia): |
---|
763 | |
---|
764 | return self.ldap.generic_student_to_itaca(uid,nia) |
---|
765 | |
---|
766 | #def generic_student_to_itaca |
---|
767 | |
---|
768 | def generic_teacher_to_itaca(self,uid,nif): |
---|
769 | |
---|
770 | return self.ldap.generic_teacher_to_itaca(uid,nif) |
---|
771 | |
---|
772 | #def generic_teachers_to_itaca |
---|
773 | |
---|
774 | def send_xml_to_server(self,data64,file_name,passwd=""): |
---|
775 | server_path="/tmp/" + file_name |
---|
776 | try: |
---|
777 | ret=self.file_operations.send_file_to_server(data64,server_path) |
---|
778 | if self.mime.file(server_path).split(";")[0]=="application/xml": |
---|
779 | pass |
---|
780 | elif self.mime.file(server_path).split(";")[0]=="application/octet-stream": |
---|
781 | if passwd=="": |
---|
782 | return "false:xml_encrypted" |
---|
783 | else: |
---|
784 | p=subprocess.Popen(["openssl","enc","-des","-d","-k",passwd,"-in",server_path,"-out",server_path+".xml"],stderr=subprocess.PIPE) |
---|
785 | output=p.communicate()[1] |
---|
786 | if output!=None: |
---|
787 | if "bad decrypt" in output: |
---|
788 | return "false:xml_bad_password" |
---|
789 | |
---|
790 | server_path=server_path+".xml" |
---|
791 | if self.mime.file(server_path).split(";")[0]=="application/xml": |
---|
792 | pass |
---|
793 | else: |
---|
794 | return "false:invalid_xml" |
---|
795 | |
---|
796 | |
---|
797 | except Exception as e: |
---|
798 | print e |
---|
799 | return "false:send_error" |
---|
800 | if ret==1: |
---|
801 | try: |
---|
802 | ret=self.gescen_set_path(server_path) |
---|
803 | if ret==True: |
---|
804 | return "true" |
---|
805 | elif ret==False: |
---|
806 | return "false:xml_error" |
---|
807 | elif ret=="false:xml_encrypted": |
---|
808 | return ret |
---|
809 | else: |
---|
810 | return "false:unknown_error" |
---|
811 | except: |
---|
812 | return "false:xml_error" |
---|
813 | else: |
---|
814 | return "false:send_error" |
---|
815 | |
---|
816 | |
---|
817 | #def send_xml_to_server |
---|
818 | |
---|
819 | |
---|
820 | def gescen_info(self): |
---|
821 | return self.itaca.get_info() |
---|
822 | #def gescen_info |
---|
823 | |
---|
824 | def gescen_set_path(self,path): |
---|
825 | return self.itaca.set_path(path) |
---|
826 | #def gescen_info |
---|
827 | |
---|
828 | def gescen_load(self): |
---|
829 | return self.itaca.load_file() |
---|
830 | #def gescen_info |
---|
831 | |
---|
832 | def gescen_groups(self): |
---|
833 | return self.itaca.get_groups() |
---|
834 | #def gescen_group |
---|
835 | |
---|
836 | def gescen_partial(self,group_list): |
---|
837 | #print "partial" |
---|
838 | #print group_list |
---|
839 | self.sharefunctions['generate_uid'] = generate_uid |
---|
840 | users_added = self.itaca.partial_import(group_list) |
---|
841 | self.peter_pan.execute_python_dir('/usr/share/n4d/hooks/golem',('gescen_partial'),{}) |
---|
842 | self.peter_pan.execute_python_dir('/usr/share/n4d/hooks/openmeetings','add_user',users_added) |
---|
843 | return 'true' |
---|
844 | |
---|
845 | #def gescen_partial |
---|
846 | |
---|
847 | def gescen_full(self): |
---|
848 | |
---|
849 | try: |
---|
850 | self.sharefunctions['generate_uid'] = generate_uid |
---|
851 | except Exception as e: |
---|
852 | print e |
---|
853 | raise e |
---|
854 | |
---|
855 | ret,users_added=self.itaca.full_import() |
---|
856 | self.peter_pan.execute_python_dir('/usr/share/n4d/hooks/golem',('gescen_full'),{}) |
---|
857 | self.peter_pan.execute_python_dir('/usr/share/n4d/hooks/openmeetings','add_user',users_added) |
---|
858 | return ret |
---|
859 | #def gescen_full |
---|
860 | |
---|
861 | def empty_students(self,generic=None): |
---|
862 | list=self.ldap.search_user("*") |
---|
863 | |
---|
864 | ret_list=[] |
---|
865 | |
---|
866 | for item in list: |
---|
867 | if item.properties["path"].find("ou=Students")!=-1: |
---|
868 | ret=self.empty_home(item.properties) |
---|
869 | ret_list.append(item.properties["uid"] +":"+ret) |
---|
870 | return ret_list |
---|
871 | #def empty_students |
---|
872 | |
---|
873 | def empty_teachers(self,generic=None): |
---|
874 | list=self.ldap.search_user("*") |
---|
875 | |
---|
876 | ret_list=[] |
---|
877 | |
---|
878 | for item in list: |
---|
879 | if item.properties["path"].find("ou=Teachers")!=-1: |
---|
880 | ret=self.empty_home(item.properties) |
---|
881 | ret_list.append(item.properties["uid"] +":"+ret) |
---|
882 | return ret_list |
---|
883 | #def empty_teachers |
---|
884 | |
---|
885 | def empty_others(self,generic=None): |
---|
886 | list=self.ldap.search_user("*") |
---|
887 | |
---|
888 | ret_list=[] |
---|
889 | |
---|
890 | for item in list: |
---|
891 | if item.properties["path"].find("ou=Other")!=-1: |
---|
892 | ret=self.empty_home(item.properties) |
---|
893 | ret_list.append(item.properties["uid"] +":"+ret) |
---|
894 | return ret_list |
---|
895 | #def empty_others |
---|
896 | |
---|
897 | def empty_all(self): |
---|
898 | |
---|
899 | ret_list=[] |
---|
900 | ret_list.extend(self.empty_students(True)) |
---|
901 | ret_list.extend(self.empty_teachers(True)) |
---|
902 | ret_list.extend(self.empty_others(True)) |
---|
903 | return ret_list |
---|
904 | |
---|
905 | #def empty_all |
---|
906 | |
---|
907 | |
---|
908 | |
---|
909 | def empty_home(self,user_info): |
---|
910 | try: |
---|
911 | self.netfiles.delete_home(user_info) |
---|
912 | self.netfiles.create_home(user_info) |
---|
913 | return "true" |
---|
914 | except: |
---|
915 | return "false" |
---|
916 | #def empty_home |
---|
917 | |
---|
918 | def get_frozen_users(self): |
---|
919 | return self.ldap.get_frozen_users() |
---|
920 | #def get_frozen_users |
---|
921 | |
---|
922 | def get_frozen_groups(self): |
---|
923 | return self.ldap.get_frozen_groups() |
---|
924 | #def get_frozen_groups |
---|
925 | |
---|
926 | def is_frozen_user(self,user): |
---|
927 | return self.ldap.is_frozen_user(user) |
---|
928 | |
---|
929 | def exist_home_or_create(self,user): |
---|
930 | return self.netfiles.exist_home_or_create(user) |
---|
931 | |
---|
932 | |
---|
933 | def create_group_folder(self,group_name): |
---|
934 | |
---|
935 | return self.netfiles.create_group_folder(group_name) |
---|
936 | |
---|
937 | |
---|
938 | #def create_group_folder |
---|
939 | |
---|
940 | def restore_groups_folders(self): |
---|
941 | ret=[] |
---|
942 | try: |
---|
943 | |
---|
944 | for item in self.get_available_groups(): |
---|
945 | try: |
---|
946 | id=self.create_group_folder(item["cn"][0]) |
---|
947 | ret.append((item["cn"][0],id)) |
---|
948 | except Exception as ex: |
---|
949 | ret.append((item["cn"][0],str(ex))) |
---|
950 | |
---|
951 | except Exception as e: |
---|
952 | ret.append(str(e)) |
---|
953 | |
---|
954 | return ret |
---|
955 | |
---|
956 | #def restore_group_folders |
---|
957 | |
---|
958 | def full_search(self): |
---|
959 | |
---|
960 | return self.ldap.full_search("*") |
---|
961 | |
---|
962 | #def full_search |
---|
963 | |
---|
964 | |
---|
965 | def export_llum_info(self): |
---|
966 | |
---|
967 | try: |
---|
968 | |
---|
969 | user_list=self.get_user_list("*") |
---|
970 | pwd_list=self.get_all_passwords(True) |
---|
971 | groups=self.get_available_groups() |
---|
972 | |
---|
973 | exported_groups={} |
---|
974 | exported_users={} |
---|
975 | |
---|
976 | for item in groups: |
---|
977 | |
---|
978 | exported_groups[item["cn"][0]]={} |
---|
979 | if item.has_key("memberUid"): |
---|
980 | exported_groups[item["cn"][0]]["members"]=item["memberUid"] |
---|
981 | else: |
---|
982 | exported_groups[item["cn"][0]]["members"]=[] |
---|
983 | exported_groups[item["cn"][0]]["description"]=item["description"][0] |
---|
984 | |
---|
985 | for item in user_list: |
---|
986 | |
---|
987 | if item["profile"]=="teachers": |
---|
988 | profile="Teachers" |
---|
989 | elif item["profile"]=="students": |
---|
990 | profile="Students" |
---|
991 | else: |
---|
992 | continue |
---|
993 | |
---|
994 | exported_users[item["uid"]]={} |
---|
995 | exported_users[item["uid"]]["profile"]=profile |
---|
996 | exported_users[item["uid"]]["cn"]=item["cn"] |
---|
997 | exported_users[item["uid"]]["sn"]=item["sn"] |
---|
998 | exported_users[item["uid"]]["groups"]=item["groups"] |
---|
999 | exported_users[item["uid"]]["is_admin"]=item["is_admin"] |
---|
1000 | exported_users[item["uid"]]["uidNumber"]=item["uidNumber"] |
---|
1001 | |
---|
1002 | |
---|
1003 | for item in pwd_list: |
---|
1004 | if item["uid"] in exported_users: |
---|
1005 | exported_users[item["uid"]]["userPassword"]=item["passwd"] |
---|
1006 | exported_users[item["uid"]]["sambaNTPassword"]=item["sambaNTPassword"] |
---|
1007 | exported_users[item["uid"]]["sambaLMPassword"]=item["sambaLMPassword"] |
---|
1008 | |
---|
1009 | exported={} |
---|
1010 | exported["groups"]=exported_groups |
---|
1011 | exported["users"]=exported_users |
---|
1012 | |
---|
1013 | return [True,exported] |
---|
1014 | |
---|
1015 | except Exception as e: |
---|
1016 | |
---|
1017 | return [False,str(e)] |
---|
1018 | |
---|
1019 | |
---|
1020 | |
---|
1021 | #def export_llum_info |
---|
1022 | |
---|
1023 | def import_llum_info(self,exported_info): |
---|
1024 | |
---|
1025 | try: |
---|
1026 | for group in exported_info["groups"]: |
---|
1027 | |
---|
1028 | properties={} |
---|
1029 | properties["description"]=exported_info["groups"][group]["description"] |
---|
1030 | properties["cn"]=group |
---|
1031 | self.add_group(properties) |
---|
1032 | |
---|
1033 | for user in sorted(exported_info["users"]): |
---|
1034 | |
---|
1035 | uids={} |
---|
1036 | for key in self.ldap.xid_counters: |
---|
1037 | uids[key]=int(self.ldap.xid_counters[key]) |
---|
1038 | |
---|
1039 | |
---|
1040 | properties={} |
---|
1041 | properties["uid"]=user |
---|
1042 | properties["cn"]=exported_info["users"][user]["cn"] |
---|
1043 | properties["sn"]=exported_info["users"][user]["sn"] |
---|
1044 | properties["userPassword"]=exported_info["users"][user]["userPassword"] |
---|
1045 | properties["sambaLMPassword"]=exported_info["users"][user]["sambaLMPassword"] |
---|
1046 | properties["sambaNTPassword"]=exported_info["users"][user]["sambaNTPassword"] |
---|
1047 | |
---|
1048 | if "uidNumber" in exported_info["users"][user]: |
---|
1049 | properties["uidNumber"]=exported_info["users"][user]["uidNumber"] |
---|
1050 | profile=exported_info["users"][user]["profile"] |
---|
1051 | |
---|
1052 | |
---|
1053 | ret=self.add_user(profile,properties) |
---|
1054 | |
---|
1055 | if "true" in str(ret): |
---|
1056 | if "uidNumber" in properties: |
---|
1057 | if uids[profile] < int(properties["uidNumber"]): |
---|
1058 | uids[profile]=int(properties["uidNumber"]) |
---|
1059 | |
---|
1060 | for group in exported_info["groups"]: |
---|
1061 | |
---|
1062 | for user in exported_info["groups"][group]["members"]: |
---|
1063 | self.add_to_group(user,group) |
---|
1064 | |
---|
1065 | for user in exported_info["users"]: |
---|
1066 | |
---|
1067 | if exported_info["users"][user]["is_admin"]: |
---|
1068 | self.add_teacher_to_admins(user) |
---|
1069 | |
---|
1070 | |
---|
1071 | for profile in uids: |
---|
1072 | if uids[profile] != int(self.ldap.xid_counters[profile]): |
---|
1073 | self.ldap.set_xid(profile,uids[profile]) |
---|
1074 | self.ldap.xid_counters[profile]=str(uids[profile]) |
---|
1075 | |
---|
1076 | |
---|
1077 | return[True,] |
---|
1078 | except Exception as e: |
---|
1079 | |
---|
1080 | return [False,str(e)] |
---|
1081 | |
---|
1082 | #def import_llum_info |
---|
1083 | |
---|
1084 | def regenerate_net_files(self,mode=0): |
---|
1085 | |
---|
1086 | try: |
---|
1087 | ret=self.light_get_user_list() |
---|
1088 | users=[] |
---|
1089 | for item in ret: |
---|
1090 | user={} |
---|
1091 | user["profile"]=item[5] |
---|
1092 | user["uid"]=item[1] |
---|
1093 | user["uidNumber"]=item[2] |
---|
1094 | users.append(user) |
---|
1095 | |
---|
1096 | for user in users: |
---|
1097 | try: |
---|
1098 | self.netfiles.exist_home_or_create(user,mode) |
---|
1099 | except: |
---|
1100 | pass |
---|
1101 | |
---|
1102 | try: |
---|
1103 | |
---|
1104 | self.restore_groups_folders(self) |
---|
1105 | except: |
---|
1106 | pass |
---|
1107 | |
---|
1108 | except: |
---|
1109 | pass |
---|
1110 | |
---|
1111 | #def regenerate_net_files |
---|
1112 | |
---|
1113 | def is_roadmin_available(self): |
---|
1114 | |
---|
1115 | try: |
---|
1116 | return self.ldap.custom_search("cn=roadmin,"+llxvars("LDAP_BASE_DN"))["status"] |
---|
1117 | except: |
---|
1118 | return False |
---|
1119 | |
---|
1120 | #def is_roadmin_avaiable |
---|
1121 | |
---|
1122 | |
---|
1123 | #class Golem |
---|
1124 | |
---|
1125 | |
---|
1126 | if __name__=="__main__": |
---|
1127 | |
---|
1128 | golem=Golem() |
---|