Commit 893acedc authored by Imran Hussain's avatar Imran Hussain
Browse files

Start work on the core delete function.

Currently does:
* kills users
* takes a backup of their ldap entry to /home/deleted/$user/ldap-ldif
* deletes them from ldap
* moves their homedir to /home/deleted/$user/homedir
* moves their mbox to /home/deleted/$user/mbox
* deletes them from the printer credit db

Doesn't do:
* sends an email saying their account is/has been deleted
* unsubscribe from mailing lists
* delete from DB
* add entery to old members table
* anything else i've forgotten
parent ee9a2a62
Loading
Loading
Loading
Loading
+62 −1
Original line number Diff line number Diff line
@@ -168,6 +168,56 @@ def listUsers():
	for hon in hons:
		print(str(hon[0]) + " (" + str(hon[1]) + ")")

def deleteUser(peopleList):

	#get fs ready
	directory = "/home/deleted"
	subprocess.call(['sudo', 'mkdir', "-m", "700", directory])

	for person in peopleList:
		#get all their info from the db
		cur = DBconn.cursor()
		cur.execute("SELECT * from members WHERE username=%(user)s",{"user" : person[0]})
		userDBinfo = cur.fetchall()
		cur.close()
		
		#get all their info from the ldap
		userLDAPinfo = ldapconn.search_s(ldap_base,ldap.SCOPE_SUBTREE,"uid="+str(person[0]))

		#make the dir to store their stuff
		subprocess.call(["sudo", "mkdir", directory+"/"+str(person[0])])

		#kill all their procs
		subprocess.call(["sudo", "pkill", "-u", str(person[0])])

		#backup their ldap entry
		#sudo ldapsearch -x -D "cn=Manager,dc=sucs,dc=org" -y /etc/ldap.secret  "(uid=imranh)"
		ldapbackupfile = open("./ldap-ldif", "w")
		subprocess.call(["sudo", "ldapsearch", "-x", "-D", ldap_manager, "-y", ldap_manager_pass, "-L", "(uid="+str(person[0])+")"], stdout=ldapbackupfile)
		ldapbackupfile.close()
		subprocess.call(['sudo', 'mv', "-f", "./ldap-ldif", directory+"/"+str(person[0])+"ldap-ldif"])

		#delete them from ldap
		#ldapconn.delete_s(ldap_base,ldap.SCOPE_SUBTREE,"uid="+str(person[0]))
		#sudo ldapdelete -D "cn=Manager,dc=sucs,dc=org" -y /etc/ldap.secret "uid=imran,ou=people,dc=sucs,dc=org"
		subprocess.call(["sudo", "ldapdelete", "-D", ldap_manager, "-y", ldap_manager_pass, userLDAPinfo[0][0]])

		#move their homedir to the deleted folder
		subprocess.call(['sudo', 'mv', "-f", userLDAPinfo[0][1]["homeDirectory"][0], directory+"/"+str(person[0])+"/homedir"])

		#move their mail to the deleted folder
		subprocess.call(['sudo', 'mv', "-f", "/var/mail", directory+"/"+str(person[0])+"/mbox"])

		#remove them from the printer
		#pkusers --delete imranh		
		subprocess.call(['sudo', 'pkusers', "--delete", str(person[0])])

		#remove from mailing lists
		#/usr/lib/mailman/bin/remove_members imranh@sucs.org members
		#/usr/lib/mailman/bin/remove_members imranh@sucs.org users
		        


def reminderMenu():
	print("")
	print("Double check https://sucs.org/Admin/SU that everyone that has paid to renew has been marked as renwed. This tool doesn't do that job yet!")
@@ -280,7 +330,18 @@ except NameError:
try:
	DBconn = psycopg2.connect(database="sucs")
except:
	print("Can't connect to the SUCS DB, suicidng!")
	print("Can't connect to the SUCS DB, suiciding!")
	sys.exit(2)

#try and connect to ldap
try:
	ldapconn = ldap.initialize("ldap://sucs.org")
	ldapconn.simple_bind_s("","")
	ldap_base = "ou=People,dc=sucs,dc=org"
	ldap_manager = "cn=Manager,dc=sucs,dc=org"
	ldap_manager_pass = "/etc/ldap.secret"
except:
	print("Can't connect to the SUCS LDAP, suiciding")
	sys.exit(2)

#store some data from to db to operate on