deluser.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # This is a script to easily add a user to system with scom
  5. #
  6. import os
  7. import pwd
  8. import sys
  9. import dbus
  10. import time
  11. from optparse import OptionParser
  12. user = {"uid": None,
  13. "deletefiles": False
  14. }
  15. def fail(_message):
  16. print(_message)
  17. sys.exit(1)
  18. def connectToDBus():
  19. global bus
  20. bus = None
  21. try:
  22. bus = dbus.SystemBus()
  23. except dbus.exceptions.DBusException:
  24. return False
  25. if bus:
  26. return True
  27. def delUser():
  28. obj = bus.get_object("tr.org.sulin.scom", "/package/baselayout")
  29. try:
  30. obj.deleteUser(user["uid"], user["deletefiles"],
  31. dbus_interface="tr.org.sulin.scom.User.Manager")
  32. except dbus.exceptions.DBusException as e:
  33. fail("Error: %s." % e)
  34. if __name__ == "__main__":
  35. usage = "usage: %prog [options] username"
  36. parser = OptionParser(usage=usage)
  37. parser.add_option("-r", "--remove-home", dest="removehome", action="store_true",
  38. help="Also remove user home directory")
  39. (opts, args) = parser.parse_args()
  40. if len(args) != 1:
  41. fail("please give one username to delete")
  42. try:
  43. user["uid"] = pwd.getpwnam(args[0]).pw_uid
  44. except KeyError:
  45. fail("Error: No such user '%s'" % args[0])
  46. user["deletefiles"] = opts.removehome
  47. if os.getuid() != 0:
  48. fail("you must have root permissions to delete a user")
  49. if not connectToDBus():
  50. fail("Could not connect to DBUS, please check your system settings")
  51. delUser()