adduser.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env python3
  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 sys
  8. import dbus
  9. import time
  10. from optparse import OptionParser
  11. # uid = -1 means next available uid
  12. # first group in the groups list is the main group
  13. user = {"uid": -1,
  14. "username": "Fox",
  15. "realname": "Inari",
  16. "home": "/home/inari",
  17. "shell": "/bin/bash",
  18. "password": "inari",
  19. "defaultgroup": "users",
  20. "groups": [],
  21. "admingroups": ["wheel"],
  22. "grants": [],
  23. "blocks": []
  24. }
  25. defaultGroups = "users,cdrom,plugdev,floppy,disk,audio,video,power,dialout,lp,lpadmin"
  26. def fail(_message):
  27. print(_message)
  28. sys.exit(1)
  29. def connectToDBus():
  30. global bus
  31. bus = None
  32. try:
  33. bus = dbus.SystemBus()
  34. except dbus.exceptions.DBusException:
  35. return False
  36. if bus:
  37. return True
  38. def addUser():
  39. obj = bus.get_object("tr.org.sulin.scom", "/package/baselayout")
  40. try:
  41. obj.addUser(user["uid"], user["username"], user["realname"],
  42. user["home"], user["shell"], user["password"],
  43. user["groups"], user["grants"], user["blocks"],
  44. dbus_interface="tr.org.sulin.scom.User.Manager")
  45. except dbus.exceptions.DBusException as e:
  46. fail("Error: %s" % e)
  47. if __name__ == "__main__":
  48. usage = "usage: %prog [options] username"
  49. parser = OptionParser(usage=usage)
  50. parser.add_option("-c", "--comment", dest="realname", type="string", default="Pisi",
  51. help="comment for the user, usually used for Full Name")
  52. parser.add_option("-u", "--uid", dest="uid", type="int", default=-1,
  53. help="user id number, default is -1 meaning next available")
  54. parser.add_option("-g", "--gid", dest="defaultgroup", type="string", default=user["defaultgroup"],
  55. help="default group name for user")
  56. parser.add_option("-G", "--groups", dest="groups", type="string", default=defaultGroups,
  57. help="groups that user is member of")
  58. parser.add_option("-p", "--password", dest="password", type="string", default="pisi",
  59. help="password")
  60. parser.add_option("-d", "--home", dest="home", type="string", default="",
  61. help="password")
  62. parser.add_option("-s", "--shell", dest="shell", type="string", default="/bin/bash",
  63. help="default login shell")
  64. parser.add_option("--admin", action="store_true", dest="isadmin", default=False,
  65. help="give user admin rights, adding to wheel group")
  66. parser.add_option("--dry-run", action="store_true", dest="dryrun", default=False,
  67. help="do not add user, only show what will be done")
  68. (opts, args) = parser.parse_args()
  69. if len(args) != 1:
  70. fail("Please provide a username.")
  71. user["username"] = args[0]
  72. groups = opts.groups.split(",")
  73. if opts.home == "":
  74. user["home"] = "/home/%s" % user["username"]
  75. else:
  76. user["home"] = opts.home
  77. if opts.defaultgroup in groups:
  78. groups.remove(user["defaultgroup"])
  79. user["defaultgroup"] = opts.defaultgroup
  80. user["groups"].append(user["defaultgroup"])
  81. if opts.isadmin:
  82. for i in user["admingroups"]:
  83. if i not in groups:
  84. groups.append(i)
  85. user["groups"].extend(groups)
  86. user["shell"] = opts.shell
  87. user["realname"] = opts.realname
  88. user["password"] = opts.password
  89. user["uid"] = opts.uid
  90. if opts.dryrun:
  91. for i in list(user.keys()):
  92. print("%s\t%s" % (i, user[i]))
  93. else:
  94. if os.getuid() != 0:
  95. fail("You must have root permissions to add a user")
  96. if not connectToDBus():
  97. fail("Could not connect to D-Bus, please check your system settings")
  98. addUser()