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