#!/usr/bin/env python3 import xmlrpc.client import ssl import sys import os import grp import pwd ACTIONS_LIST=["enable","disable","status","help","-h","--help"] class Guest: def __init__(self,action): self.actions={} self.actions["enable"]=self.enable_guest_user self.actions["disable"]=self.disable_guest_user self.actions["status"]=self.get_guest_status server="localhost" if action not in ["enable","disable","status"]: sys.exit(1) context=ssl._create_unverified_context() self.n4d_client=xmlrpc.client.ServerProxy("https://%s:9779"%server,allow_none=True,context=context) if not self.get_user_info(): print("User not allowed to run this program.") sys.exit(1) try: self.actions[action]() except Exception as e: self.dprint(e) sys.exit(1) #def __init__ def get_user_info(self): #self.user=os.environ["USER"] if "PKEXEC_UID" in os.environ: self.user=pwd.getpwuid(int(os.environ["PKEXEC_UID"])).pw_name else: self.user=os.environ["USER"] #old groups method #groups = [g.gr_name for g in grp.getgrall() if self.user in g.gr_mem] gid = pwd.getpwnam(self.user).pw_gid groups_gids = os.getgrouplist(self.user, gid) groups = [ grp.getgrgid(x).gr_name for x in groups_gids ] if "sudo" not in groups and "admins" not in groups and "teachers" not in groups: return False if not self.get_n4d_ticket(): return False self.user_info=(self.user,self.ticket) return True #def get_user_info def get_n4d_ticket(self): try: self.n4d_client.create_ticket(self.user) except Exception as e: print(str(e)) t="/run/n4d/tickets/%s"%self.user print(t) if not os.path.exists(t): return False f=open(t) self.ticket=f.readline() f.close() return True def get_n4d_key(self): try: f=open("/etc/n4d/key") key=f.readline().strip() f.close() return key except Exception as e: self.dprint("Failed to read n4d key. [ %s ]"%e) sys.exit(1) #def get_n4d_key def dprint(self,msg): print("[llx-guest-manager] %s"%msg) #def dprint def enable_guest_user(self): #self.dprint("Enabling guest user...") #key=self.get_n4d_key() ret=self.n4d_client.enable_guest_user(self.user_info,"GuestAccountManager").get('return',None) if ret["status"]: self.dprint("guest user enabled") sys.exit(0) else: self.dprint("Failed to enable guest user. [ %s ]"%ret["msg"]) sys.exit(1) #def enable_guest_user def disable_guest_user(self): #self.dprint("Disabling guest user...") #key=self.get_n4d_key() ret=self.n4d_client.disable_guest_user(self.user_info,"GuestAccountManager").get('return',None) if ret["status"]: self.dprint("guest user disabled") sys.exit(0) else: self.dprint("Failed to disable guest user. [ %s ]"%ret["msg"]) sys.exit(1) #def disable_guest_user def get_guest_status(self): self.dprint("Getting guest user status...") ret=self.n4d_client.get_guest_status("","GuestAccountManager").get('return',None) if ret["status"]==True: self.dprint("guest-user is enabled") sys.exit(0) else: self.dprint("guest-user is disabled") sys.exit(1) #def get_guest_status #class Guest def print_usage(): print("Usage: llx-guest-manager OPTION") print("") print("Options:") print("\tenable\t\t\tenables guest user account") print("\tdisable\t\t\tdisables guest user account") print("\tstatus\t\t\tchecks if guest user account is created") print("\thelp\t\t\tprints this message") print("\t-h\t\t\tprints this message") print("\t--help\t\t\tprints this message") def parse_args(arg_list): if len(arg_list)<2: print("[!] llx-guest-manager needs an argument [!]") print_usage() sys.exit(1) action=arg_list[1] if action not in ACTIONS_LIST: print("Unknown argument") print_usage() sys.exit(1) if action in ["-h","--help","help"]: print_usage() sys.exit(0) return action #def parse_args if __name__=="__main__": action=parse_args(sys.argv) guest=Guest(action) sys.exit(0)