Guest User

Untitled

a guest
Jun 2nd, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # This script creates a git group and user account under Mac OS X
  4. # Assumes git was installed via macports (i.e. /opt/local)
  5.  
  6. USERNAME="git"
  7. GROUPNAME="$USERNAME"
  8. REALNAME="git version control"
  9. PASSWORD="*"
  10.  
  11. if [[ $UID -ne 0 ]]; then echo "Please run $0 as root." && exit 1; fi
  12.  
  13. # make sure the user name is unique
  14. unset -v new_user
  15. new_user="$(dscl . -search /Users name "$USERNAME")"
  16. if [[ -z "$new_user" ]]; then
  17. new_user="$USERNAME"
  18. else
  19. echo "User name already exists: $USERNAME"
  20. exit 1
  21. fi
  22.  
  23. # make sure the user's primary group name is unique
  24. unset -v new_group
  25. new_group="$(dscl . -search /Groups name "$GROUPNAME")"
  26. if [[ -z "$new_group" ]]; then
  27. new_group="$GROUPNAME"
  28. else
  29. echo "Group name already exists: $GROUPNAME"
  30. exit 1
  31. fi
  32.  
  33. # make sure there is no (file or) home directory of the same name already
  34. if [[ -e "/Users/$USERNAME" ]]; then
  35. echo "\nUser $USERNAME already exists at /Users/$USERNAME"
  36. exit 1
  37. fi
  38.  
  39. # get unique id numbers (uid, gid) that are greater than 500
  40. unset -v i new_uid new_gid idvar
  41. declare -i new_uid=0 new_gid=0 i=500 idvar=0
  42.  
  43. while [[ $idvar -eq 0 ]]; do
  44. i=$[i+1]
  45. if [[ -z "$(dscl . -search /Users uid $i)" ]] && [[ -z "$(dscl . -search /Groups gid $i)" ]]; then
  46. new_uid=$i
  47. new_gid=$i
  48. idvar=1
  49. #break
  50. fi
  51. done
  52.  
  53. if [[ $new_uid -eq 0 ]] || [[ $new_gid -eq 0 ]]; then echo 'Getting unique id numbers (uid, gid) failed!'; exit 1; fi
  54.  
  55. # Create the group
  56. echo "creating group $new_gid: $GROUPNAME"
  57. dscl . -create /Groups/$GROUPNAME
  58. dscl . -append /Groups/$GROUPNAME gid "$new_gid"
  59.  
  60. # Create the user account
  61. echo "creating user $new_uid: $USERNAME ($REALNAME)"
  62. dscl . -create /Users/$USERNAME
  63. dscl . -append /Users/$USERNAME UserShell /bin/bash
  64. dscl . -append /Users/$USERNAME RealName "$REALNAME"
  65. dscl . -append /Users/$USERNAME UniqueID "$new_uid"
  66. dscl . -append /Users/$USERNAME PrimaryGroupID "$new_gid"
  67. dscl . -append /Users/$USERNAME NFSHomeDirectory /Users/$USERNAME
  68. dscl . -passwd /Users/$USERNAME "$PASSWORD"
  69.  
  70. # Create the home directory
  71. createhomedir -c
  72.  
  73. # Create .bashrc
  74. cat > /Users/$USERNAME/.bashrc << EOF
  75.  
  76. if [ -x /usr/libexec/path_helper ]; then
  77. eval `/usr/libexec/path_helper -s`
  78. fi
  79.  
  80. export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
  81. export PATH=$(git --exec-path):$PATH
  82.  
  83. EOF
  84.  
  85. # Grant the user ssh access
  86. echo "granting $USERNAME ssh access"
  87. dscl . -append /Groups/com.apple.access_ssh GroupMembership $USERNAME
  88.  
  89. # Add _www to git group so web server can access git user's content
  90. dscl . -append /Groups/git GroupMembership _www
  91.  
  92. echo "User: $USERNAME and Group: $GROUPNAME created."
Add Comment
Please, Sign In to add comment