Guest User

Untitled

a guest
Apr 23rd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3.  
  4. '''
  5. Author: Calum Hunter
  6. Date: April 16 2018
  7. Version: 1.0
  8.  
  9. This script will get the current UserShell
  10. value set in the users record in /Local/Default
  11. if this UserShell varies from '/bin/bash'
  12. we will change it to ensure it is set to '/bin/bash'
  13.  
  14. Note: In order to perform the change of the users shell
  15. dscl must be run with root or sudo.
  16.  
  17. This script should be run as a login script with outset
  18. or a similar tool - noting that it must be run 'as root'
  19. '''
  20.  
  21. import sys
  22. import subprocess
  23. from SystemConfiguration import SCDynamicStoreCopyConsoleUser
  24.  
  25. # Set to 'True' to enable debug for extra output
  26. debug = True
  27.  
  28. # Define the 'Correct' UserShell.
  29. # This is the UserShell we _SHOULD_ be setting
  30. correctUserShell = '/bin/bash'
  31.  
  32. # Start by getting the details of the currently logged in console user
  33. # use python and the SysConfig moduleto avoid incorrect information
  34. # being returned from other tools like whoami or other posix tools
  35. loggedInUser = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])
  36. loggedInUser = [loggedInUser,""][loggedInUser in [u"loginwindow", None, u""]]
  37.  
  38. # Assign the elements from loggedInUser dict to variables
  39. loggedInUser_shortname = loggedInUser[0]
  40. loggedInUser_uid = loggedInUser[1]
  41. loggedInUser_gid = loggedInUser[2]
  42.  
  43. # Prep the command to return the UserShell
  44. getShellCmd = [
  45. '/usr/bin/dscl', '/Local/Default', '-read',
  46. '/Users/'+loggedInUser_shortname, 'UserShell'
  47. ]
  48.  
  49. # Run the command above, split the result to obtain the 2nd element which
  50. # is actually the UserShell, then we strip off the \n new line at the end
  51. currentUserShell = subprocess.check_output(getShellCmd).split(': ')[1].strip()
  52.  
  53. # Print out some information here, if we have debug set to True
  54. if debug:
  55. print ('Current Logged in user details:')
  56. print (' - Shortname: %s') % loggedInUser_shortname
  57. print (' - UID: %s') % loggedInUser_uid
  58. print (' - GID: %s') % loggedInUser_gid
  59. print (' - Current UserShell: %s') % currentUserShell
  60. print ('')
  61.  
  62. # If our UserShell is _NOT_ what our correctUserShell is, then we should use
  63. # dscl to change the UserShell to $correctUserShell
  64. if currentUserShell != correctUserShell:
  65. # prep the change command
  66. changeShellCmd = [
  67. '/usr/bin/dscl', '/Local/Default', '-change',
  68. '/Users/'+loggedInUser_shortname, 'UserShell',
  69. currentUserShell, correctUserShell
  70. ]
  71. # Notify that the incorrect shell was detected and we
  72. # are going to change it if debug enabled
  73. if debug:
  74. print ('[ERROR] Current UserShell does NOT match what we expect!')
  75. print ('- Current UserShell: %s') % currentUserShell
  76. print ('- Correct UserShell: %s') % correctUserShell
  77. print ('')
  78. print (' - Attempting to change UserShell to: %s ...') % correctUserShell
  79. # Try to perform the change of Usershell
  80. try:
  81. changeUserShell = subprocess.check_output(changeShellCmd, stderr=subprocess.STDOUT)
  82. if debug:
  83. print ('[OK] - UserShell changed successfully!')
  84. # If we get a non zero return code from dscl, then trap it and raise an exception
  85. except subprocess.CalledProcessError as changeShellError:
  86. print ('[ERROR] There was an error trying to change the UserShell with dscl')
  87. print (' - Ensure you are running this script with root privilegs.')
  88. print ('')
  89. print (' - dscl exit code: %s') % changeShellError.returncode
  90. print (' - dscl error output: %s') % changeShellError.output
  91. else:
  92. if debug:
  93. print ('- Current UserShell matches what we expect, no changes required.')
  94. sys.exit()
Add Comment
Please, Sign In to add comment