summaryrefslogtreecommitdiff
path: root/localadduser
blob: 398e0804c1f63c236f5d030461b4680a2f21ea01 (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. groupchunks="${groupchunks:+$groupchunks }$chunk"
  33. ;;
  34. *@*)
  35. other="${other:+$other }$chunk"
  36. ;;
  37. +*)
  38. phone_area="$chunk"
  39. ;;
  40. 0*|1*|2*|3*|4*|5*|6*|7*|8*|9*)
  41. if [ -z "$phone_area" ]; then
  42. exit1 "Phone number provided without leading area code!"
  43. fi
  44. if [ -n "$home_phone" ]; then
  45. exit1 "More than 2 phone numbers provided!"
  46. elif [ -n "$office_phone" ]; then
  47. office_phone="$phone_area $chunk"
  48. else
  49. home_phone="$phone_area $chunk"
  50. fi
  51. phone_area=""
  52. ;;
  53. *)
  54. fullname="${fullname:+$fullname }$chunk"
  55. ;;
  56. esac
  57. done
  58. if [ -n "$phone_area" ]; then
  59. exit1 "Area code provided without trailing phonenumber!"
  60. fi
  61. for groupchunk in $groupchunks; do
  62. group="$(echo "$groupchunk" | perl -pe 's/^@//;')"
  63. if echo "$group" | perl -ne '/^[a-z][a-z0-9_-]*$/ and exit 1;'; then
  64. exit1 "Group \"$group\" contains illegal characters!"
  65. fi
  66. if ! members="$(getent group "$group")"; then
  67. exit1 "Group \"$group\" does not exist!"
  68. fi
  69. if echo "$members" | perl -pe 's/.*://; s/,/\n/g' | grep -Fxq "$u"; then
  70. exit1 "Group \"$group\" already contains user \"$u\"!"
  71. fi
  72. groups="${groups:+$groups }$group"
  73. done
  74. if [ ! "$interactive" -gt 0 ]; then
  75. quiet="--quiet"
  76. fi
  77. if [ -n "$fullname$office_phone$home_phone$other" ]; then
  78. eval $simulate sudo "/usr/sbin/adduser $quiet --disabled-login --gecos \"$fullname,,$office_phone,$home_phone,$other\" \"$u\""
  79. else
  80. if [ ! "$interactive" -gt 0 ]; then
  81. exit1 "Not enough info provided to create account for \"$u\"!"
  82. fi
  83. eval $simulate sudo "/usr/sbin/adduser --disabled-login \"$u\""
  84. fi
  85. for group in $groups; do
  86. eval $simulate sudo "/usr/sbin/adduser $quiet \"$u\" \"$group\""
  87. done
  88. eval $simulate localresetpasswd "$u"
  89. #vecho "Account \"$u\" created succesfully! Password is $pass"