[SUCS Devel] [Git][sucs/accountmanager][master] 2 commits: Add some colour :)

Imran Hussain imranh at sucs.org
Sat Mar 9 20:36:30 GMT 2019


Imran Hussain pushed to branch master at sucs / accountmanager


Commits:
ee9a2a62 by Imran Hussain at 2019-03-09T19:03:35Z
Add some colour :)

- - - - -
893acedc by Imran Hussain at 2019-03-09T20:32:09Z
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

- - - - -


1 changed file:

- sucsam.py


Changes:

=====================================
sucsam.py
=====================================
@@ -10,6 +10,7 @@ import codecs
 import smtplib
 from email.mime.multipart import MIMEMultipart
 from email.mime.text import MIMEText
+import ldap
 
 def correctPaidUntilStr():
 	#same logic as suapi stuff
@@ -167,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 at sucs.org members
+		#/usr/lib/mailman/bin/remove_members imranh at 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!")
@@ -220,8 +271,12 @@ def deleteMenu(deleteArray):
 			deleteArray.remove(user)
 			print(str(user) + " won't be deleted.")
 		else:
-			deleteArray.append(user)
-			print(str(user) + " will be deleted.")
+			try:
+				pwd.getpwnam(user)
+				deleteArray.append(user)
+				print(str(user) + " will be deleted.")
+			except KeyError:
+				print("\x1b[1;31mInvalid user.\x1b[0m")
 		deleteMenu(deleteArray)
 	elif (option == "3"):
 		for student in studentsBad:
@@ -256,7 +311,7 @@ def deleteAccounts():
 ### MAIN ###
 
 if (os.geteuid() == 0):
-	print("Don't run this as root!")
+	print("\x1b[1;31mDon't run this as root!\x1b[0m")
 	sys.exit(1)
 
 # get some date/time info ready
@@ -275,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
@@ -297,5 +363,5 @@ hons = cur.fetchall()
 
 cur.close()
 
-print("Welcome to the SUCS Account Manager!")	
+print("\x1b[33mWelcome to the SUCS Account Manager!\x1b[0m")
 mainMenu()
\ No newline at end of file



View it on GitLab: https://projects.sucs.org/sucs/accountmanager/compare/34b23bc48a728ffdfcfe927868d16b3bc625f597...893acedc5cbee051b7d3e4b570dbf8364b09efd5

-- 
View it on GitLab: https://projects.sucs.org/sucs/accountmanager/compare/34b23bc48a728ffdfcfe927868d16b3bc625f597...893acedc5cbee051b7d3e4b570dbf8364b09efd5
You're receiving this email because of your account on projects.sucs.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sucs.org/pipermail/devel/attachments/20190309/99ea6eac/attachment-0001.html>


More information about the Devel mailing list