#!/usr/bin/python # # sysadmin-console # Administration console for AIESEC Karlsruhe Office Server # written by Henning Jacobs, henning@jacobs1.de # http://www.srcco.de import readline import os import sys import pexpect import getpass class SysadminConsole: VERSION = "0.1-20071215" def __init__(self): self.menu_actions = { "1": self.listusers, "2": self.adduser, "3": self.passwd, "4": self.deluser, "q": self.exit, "x": self.exit } self.users_gid = "100"; self.min_passwd_len = 5 def is_normal_user(self, username): fd = open("/etc/passwd", "rb") for line in fd.readlines(): cols = line.split(":") if cols[0] == username and cols[3] == self.users_gid: return True return False def user_exists(self, username): fd = open("/etc/passwd", "rb") for line in fd.readlines(): cols = line.split(":") if cols[0] == username: return True return False def main(self): print ("=" * 70) print " sysadmin-console v.%s" % (self.VERSION,) print " Administration Console for AIESEC Karlsruhe" print ("=" * 70) self.mainloop() def listusers(self): print "List users." prefix = raw_input("Show usernames starting with: ") users = [] fd = open("/etc/passwd", "rb") for line in fd.readlines(): cols = line.split(":") # list only users in the "users" group (gid 100) if cols[0].startswith(prefix) and cols[3] == self.users_gid: users.append(cols[0] + ": " + cols[4]) users.sort() n = 1 for user in users: print "%d: %s" % (n, user) n += 1 def adduser(self): print "Add user." username = raw_input("Username: ") if self.user_exists(username): print "User already exists." return realname = raw_input("Realname: ") if len(realname) < 4: print "You must enter the user's full name." return password = getpass.getpass("Password: ") if len(password) < self.min_passwd_len: print "Password must have at least %d chars." % (self.min_passwd_len,) return print "Adding user to system.." child = pexpect.spawn("sudo /usr/sbin/adduser --quiet --ingroup users --gecos \"%s\" %s" % (realname, username)) child.expect(".* UNIX password: ") child.sendline(password) child.expect(".* UNIX password: ") child.sendline(password) child.read() child.close() print "Adding user to samba.." child = pexpect.spawn("sudo /usr/bin/smbpasswd -a %s" % (username,)) child.expect(".* password:\s*") child.sendline(password) child.expect(".* password:\s*") child.sendline(password) child.read() child.close() print "User %s added." % (username,) def passwd(self): print "Change user password." username = raw_input("Username: ") if not self.user_exists(username): print "User does not exist." return if not self.is_normal_user(username): print "Changing this user's password is forbidden." return password = getpass.getpass("New Password: ") if len(password) < self.min_passwd_len: print "Password must have at least %d chars." % (self.min_passwd_len,) return print "Changing samba password.." child = pexpect.spawn("sudo /usr/bin/smbpasswd %s" % (username,)) child.expect(".* password:\s*") child.sendline(password) child.expect(".* password:\s*") child.sendline(password) child.read() child.close() print "Changing system password.." child = pexpect.spawn("sudo /usr/bin/passwd %s" % (username,)) child.expect(".* UNIX password: ") child.sendline(password) child.expect(".* UNIX password: ") child.sendline(password) child.read() child.close() print "Password for user %s changed." % (username,) def deluser(self): print "Delete user." username = raw_input("Username: ") if not self.user_exists(username): print "User does not exist." return if not self.is_normal_user(username): print "Deleting this user is forbidden." return confirm = raw_input("Do you really want to remove %s? (Type YES or NO): " % (username,)) if confirm.lower() <> "yes": return print "Deleting user from samba.." os.system("sudo /usr/bin/smbpasswd -x %s" % (username,)) print "Deleting user from system.." os.system("sudo /usr/sbin/deluser %s" % (username,)) print "User %s deleted." % (username,) def exit(self): print "Exiting.." self.done = True def mainloop(self): self.done = False sel = None while not self.done: self.mainmenu() try: sel = raw_input(">>> ").lower() except KeyboardInterrupt: self.exit() if sel in self.menu_actions: self.menu_actions[sel]() else: print "Error: Please select a valid menu item" def mainmenu(self): # print ("-" * 70) print "Main Menu:" print " [1] -- List users" print " [2] -- Add user" print " [3] -- Change user's password" print " [4] -- Delete user" print " [Q] -- Exit" if __name__ == "__main__": sac = SysadminConsole() sac.main()