Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import sh
  4. import re
  5.  
  6. HUMANS = ['jkane', 'djames', 'mkirk']
  7.  
  8.  
  9. def clean_passwd(h, humans):
  10. clean = []
  11. for row in h:
  12. user = row.split(':')
  13. if user[0] not in humans:
  14. clean.append(row.strip())
  15. else:
  16. print('Removing %r' % user[0])
  17.  
  18. return "\n".join(clean) + "\n"
  19.  
  20. def clean_group(h, humans):
  21. clean = []
  22. for row in h:
  23. group = row.split(':')
  24. users = group[-1].split(',')
  25.  
  26. new_users = []
  27. for user in users:
  28. user = user.strip()
  29. if user not in humans:
  30. new_users.append(user)
  31. else:
  32. print('Removing %r from group %r' % (user, group[0]))
  33.  
  34. group[-1] = ",".join(new_users)
  35. if group[0] not in humans:
  36. clean.append(':'.join(group))
  37. else:
  38. print('Removing group %r' % group[0])
  39.  
  40.  
  41. return "\n".join(clean) + "\n"
  42.  
  43. def clean_shadow(h, humans):
  44. """format of /etc/shadow is close-enough to password for our purposes"""
  45. return clean_passwd(h, humans)
  46.  
  47.  
  48. for human in HUMANS:
  49. try:
  50. uid, gid, groups = sh.id(human).split()
  51. except sh.ErrorReturnCode_1:
  52. print('Human %r not found.' % human)
  53. continue
  54.  
  55. old_uid = int(re.search("uid=(\d*)\(.*", uid).group(1))
  56. old_gid = int(re.search("gid=(\d*)\(.*", gid).group(1))
  57.  
  58. print("Human %r was uid %r / gid %r" % (human, old_uid, old_gid))
  59.  
  60. with open('/etc/passwd', 'r') as h:
  61. clean = clean_passwd(h, [human])
  62. with open('/etc/passwd', 'w') as h:
  63. h.write(clean)
  64.  
  65. with open('/etc/group', 'r') as h:
  66. clean = clean_group(h, [human])
  67. with open('/etc/group', 'w') as h:
  68. h.write(clean)
  69.  
  70. with open('/etc/shadow', 'r') as h:
  71. clean = clean_shadow(h, [human])
  72. with open('/etc/shadow', 'w') as h:
  73. h.write(clean)
  74.  
  75. try:
  76. uid, gid, groups = sh.id(human).split()
  77. except sh.ErrorReturnCode_1:
  78. print('Human %r not found.' % human)
  79. continue
  80.  
  81. new_uid = int(re.search("uid=(\d*)\(.*", uid).group(1))
  82. new_gid = int(re.search("gid=(\d*)\(.*", gid).group(1))
  83.  
  84. print("Human %r is now uid %r / gid %r" % (human, new_uid, new_gid))
  85.  
  86. if new_uid != old_uid:
  87. # change every file owned by old_uid to be owned by this human
  88. sh.find("/", "-uid", old_uid, "-exec", "chown", human, "{}", "+")
  89.  
  90. if new_gid != old_gid:
  91. # change every file owned by old_gid to be owned by the new group
  92. # the -h is to avoid reaching through symlinks and instead change the sym itself
  93. sh.find("/", "-gid", old_gid, "-exec", "chgrp", "-h", new_gid, "{}", "+")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement