summaryrefslogtreecommitdiff
path: root/localadduser
blob: 4989242cd1e01383f3d1e16c1a22f7a0750d4339 (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. u=$1
  23. shift
  24. for chunk in $@; do
  25. case $chunk in
  26. *@*)
  27. other="${other:+$other }$chunk"
  28. ;;
  29. +*)
  30. phone_area="$chunk"
  31. ;;
  32. 0*|1*|2*|3*|4*|5*|6*|7*|8*|9*)
  33. if [ -z "$phone_area" ]; then
  34. echo "E: Phone number provided without leading area code" >&2
  35. exit 1
  36. fi
  37. if [ -n "$office_phone" ]; then
  38. echo "E: More than 2 phone numbers provided" >&2
  39. exit 1
  40. elif [ -n "$home_phone" ]; then
  41. office_phone="$phone_area $chunk"
  42. else
  43. home_phone="$phone_area $chunk"
  44. fi
  45. phone_area=""
  46. ;;
  47. *)
  48. fullname="${fullname:+$fullname }$chunk"
  49. ;;
  50. esac
  51. done
  52. if [ -n "$phone_area" ]; then
  53. echo "E: Area code provided without trailing phonenumber" >&2
  54. exit 1
  55. fi
  56. if [ ! "$interactive" -gt 0 ]; then
  57. quiet="--quiet"
  58. fi
  59. if [ -n "$fullname$office_phone$home_phone$other" ]; then
  60. eval $simulate sudo "/usr/sbin/adduser $quiet --disabled-login --gecos \"$fullname,,$office_phone,$home_phone,$other\" \"$u\""
  61. else
  62. if [ ! "$interactive" -gt 0 ]; then
  63. echo "E: Not enough info provided to create account for \"$u\"" >&2
  64. exit 1
  65. fi
  66. eval $simulate sudo "/usr/sbin/adduser --disabled-login \"$u\""
  67. fi
  68. eval $simulate localresetpasswd "$u"
  69. #vecho "Account \"$u\" created succesfully! Password is $pass"