summaryrefslogtreecommitdiff
path: root/localadduser
blob: 4d62ed459e02eb7f69ca7cc58e0b2f0a25ab99fd (plain)
  1. #!/bin/sh
  2. #
  3. # /usr/local/bin/localadduser
  4. # Copyright 2003-2006 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: localadduser,v 1.5 2006-08-31 22:51:54 jonas Exp $
  7. #
  8. # Execute adduser noninteractively through sudo
  9. #
  10. # TODO: Check for bad arguments
  11. # TODO: Use getopts to offer help
  12. # TODO: Support overriding options in /etc/local file
  13. #
  14. set -e
  15. verbose=1
  16. #simulate=true
  17. interactive=0
  18. # echo something, but only if in verbose mode
  19. vecho() {
  20. test -n "$verbose" && echo "$@" >&2
  21. }
  22. exit1() {
  23. response="${1:+Error: }${1:-Internal error!}"
  24. echo "$response"
  25. exit 1
  26. }
  27. u=$1
  28. shift
  29. for chunk in $@; do
  30. case $chunk in
  31. *@*)
  32. other="${other:+$other }$chunk"
  33. ;;
  34. +*)
  35. phone_area="$chunk"
  36. ;;
  37. 0*|1*|2*|3*|4*|5*|6*|7*|8*|9*)
  38. if [ -z "$phone_area" ]; then
  39. exit1 "Phone number provided without leading area code!"
  40. fi
  41. if [ -n "$home_phone" ]; then
  42. exit1 "More than 2 phone numbers provided!"
  43. elif [ -n "$office_phone" ]; then
  44. office_phone="$phone_area $chunk"
  45. else
  46. home_phone="$phone_area $chunk"
  47. fi
  48. phone_area=""
  49. ;;
  50. *)
  51. fullname="${fullname:+$fullname }$chunk"
  52. ;;
  53. esac
  54. done
  55. if [ -n "$phone_area" ]; then
  56. exit1 "Area code provided without trailing phonenumber!"
  57. fi
  58. if [ ! "$interactive" -gt 0 ]; then
  59. quiet="--quiet"
  60. fi
  61. if [ -n "$fullname$office_phone$home_phone$other" ]; then
  62. eval $simulate sudo "/usr/sbin/adduser $quiet --disabled-login --gecos \"$fullname,,$office_phone,$home_phone,$other\" \"$u\""
  63. else
  64. if [ ! "$interactive" -gt 0 ]; then
  65. exit1 "Not enough info provided to create account for \"$u\"!"
  66. fi
  67. eval $simulate sudo "/usr/sbin/adduser --disabled-login \"$u\""
  68. fi
  69. eval $simulate localresetpasswd "$u"
  70. #vecho "Account \"$u\" created succesfully! Password is $pass"