shosei

addutils

May 11th, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.53 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # Adduser script for Mac OS X:
  5. # Usage: adduser [-u UID] [-c COMM] [-d HOME] [-g GID] [-G GRP[,GRP...]]
  6. #                [-s SHELL] USER
  7. #
  8. # Small hack for adding users via command line
  9. #
  10. # COPYRIGHT 2009 CYNAMICS Johan E. < [email protected] >
  11. #
  12. invalid() {
  13.   echo "${0##*/}: $*  Try '${0##*/} --help'" >&2
  14.   exit 1
  15. }
  16.  
  17. while [ -n "$1" ]; do
  18.   case "$1" in
  19.     -u    ) uid="$2"  ;;
  20.     -[Cc] ) full="$2" ;;
  21.     -d    ) dir="$2"  ;;
  22.     -g    ) gid="$2"  ;; # I think this has to be a number
  23.     -G    ) grps="$2" ;;
  24.     -s    ) shell="$2";;
  25.     -h|--help ) sed -e '/^## /!d' -e 's///' "`which $0 || echo $0`"; exit 0 ;;
  26.     -*    ) invalid "unknown option '$1' (maybe you need to space the args?)" ;;
  27.   esac
  28.   name="$1"
  29.   shift; shift  # uh, "shift 2" is unsupported!
  30. done
  31.  
  32. if [ -z "$name" ]; then invalid "no name specified."; fi
  33.  
  34. if [ -z "$uid" ]; then
  35.   uid=`ls -ln /Users |awk '{printf $3"\n"}' |sort |uniq |tail -n1`
  36.   uid=`expr 1 + $uid  2>/dev/null`
  37.   if [ -z "$uid" ]; then
  38.     echo "Unable to guess UID.  Please enter one with the -u flag." >&2
  39.     exit 1
  40.   fi
  41. fi
  42.  
  43. fail() {
  44.   echo "${0##*/}: Previous command FAILED, Cyn Engine STOPS HERE."
  45.   break
  46. }
  47.  
  48. do_it() {
  49.   $runtest niutil -create     / /users/$name || fail
  50.   $runtest niutil -createprop / /users/$name shell ${shell:=/bin/bash} || fail
  51.   if [ -n "$full" ]; then
  52.     $runtest niutil -createprop / /users/$name realname "$full" || fail
  53.   fi
  54.   $runtest niutil -createprop / /users/$name uid $uid || fail
  55.   if [ -z "$gid" ]; then
  56.     $runtest niutil -create     / /groups/$name || fail
  57.     $runtest niutil -createprop / /groups/$name gid $uid || fail
  58.   fi
  59.   $runtest niutil -createprop / /users/$name gid ${gid:-$uid} || fail
  60.   for group in ${grps//,/ }; do
  61.     $runtest niutil -appendprop / /groups/$group users $name || fail
  62.   done
  63.   $runtest niutil -createprop / /users/$name home ${dir:=/Users/$name} || fail
  64.   $runtest niutil -createprop / /users/$name _shadow_passwd || fail
  65.   $runtest passwd $name || fail
  66. }
  67.  
  68. echo "REMINDER:  NOT AN APPLE SCRIPT. A Cyn Engine: Cynamics Script."
  69. echo "About to execute:"
  70. runtest=echo do_it |sed 's/^/  /'
  71.  
  72. if [ "`uname -s`" != Darwin ]; then
  73.   echo "This script is only supposed to be run on Mac OS ('Darwin').  Quitting."
  74.   exit 1
  75. fi
  76.  
  77. echo "Is this okay to run [yN]? "
  78. read yn
  79. if [ "${yn#[Yy]}" = "${yn}" ]
  80.   then echo "Didn't do anything.  Please try again and/or FIX ME."; exit 0
  81. fi
  82.  
  83. echo "Here it is."
  84. for i in 1; do do_it; done
  85.  
  86. echo "The output of finger and id:"
  87. finger $name
  88. id $name
Advertisement
Add Comment
Please, Sign In to add comment