summaryrefslogtreecommitdiff
path: root/localadduser
blob: 292608b2b76ae55f5053665759df66e9181c83bc (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: Implement --help option
  12. # TODO: Support overriding options in /etc/local file
  13. #
  14. set -e
  15. PRG=$(basename "$0")
  16. TEMP="`getopt -s sh -o vqin -l verbose,quiet,interactive,noninteractive,dry-run -n "$PRG" -- "$@"`"
  17. if [ $? != 0 ] ; then echo >&2 "ERROR: Internal getopt error." ; exit 1 ; fi
  18. eval set -- "$TEMP"
  19. verbose=1
  20. interactive=0
  21. while true ; do
  22. case "$1" in
  23. -v|--verbose) verbose=1; shift ;;
  24. -q|--quiet) verbose=0; shift ;;
  25. -i|--interactive) interactive=1; shift ;;
  26. -n|--noninteractive) interactive=0; shift ;;
  27. --dry-run) simulate=true; shift ;;
  28. --) shift ; break ;;
  29. *) echo >&2 "ERROR: Internal error resolving options." ; exit 1 ;;
  30. esac
  31. done
  32. # echo something, but only if in verbose mode
  33. vecho() {
  34. test -n "$verbose" && echo "$@" >&2
  35. }
  36. exit1() {
  37. response="${1:+Error: }${1:-Internal error!}"
  38. echo "$response"
  39. exit 1
  40. }
  41. u=$1
  42. shift
  43. for chunk in $@; do
  44. case $chunk in
  45. @*)
  46. groupchunks="${groupchunks:+$groupchunks }$chunk"
  47. ;;
  48. %*)
  49. officechunks="${officechunks:+$officechunks }$chunk"
  50. ;;
  51. *@*)
  52. other="${other:+$other }$chunk"
  53. ;;
  54. +*)
  55. phone_area="$chunk"
  56. ;;
  57. 0*|1*|2*|3*|4*|5*|6*|7*|8*|9*)
  58. if [ -z "$phone_area" ]; then
  59. exit1 "Phone number provided without leading area code!"
  60. fi
  61. if [ -n "$home_phone" ]; then
  62. exit1 "More than 2 phone numbers provided!"
  63. elif [ -n "$office_phone" ]; then
  64. office_phone="$phone_area $chunk"
  65. else
  66. home_phone="$phone_area $chunk"
  67. fi
  68. phone_area=""
  69. ;;
  70. *)
  71. fullname="${fullname:+$fullname }$chunk"
  72. ;;
  73. esac
  74. done
  75. if [ -n "$phone_area" ]; then
  76. exit1 "Area code provided without trailing phonenumber!"
  77. fi
  78. for groupchunk in $groupchunks; do
  79. group="$(echo "$groupchunk" | perl -pe 's/^@//;')"
  80. if echo "$group" | perl -ne '/^[a-z][a-z0-9_-]*$/ and exit 1;'; then
  81. exit1 "Group \"$group\" contains illegal characters!"
  82. fi
  83. if ! members="$(getent group "$group")"; then
  84. exit1 "Group \"$group\" does not exist!"
  85. fi
  86. if echo "$members" | perl -pe 's/.*://; s/,/\n/g' | grep -Fxq "$u"; then
  87. exit1 "Group \"$group\" already contains user \"$u\"!"
  88. fi
  89. groups="${groups:+$groups }$group"
  90. done
  91. for officechunk in $officechunks; do
  92. office="${office:+$office }$(echo "$officechunk" | perl -pe 's/^%//;')"
  93. done
  94. if [ ! "$interactive" -gt 0 ]; then
  95. quiet="--quiet"
  96. fi
  97. if [ -n "$fullname$office$office_phone$home_phone$other" ]; then
  98. eval $simulate sudo "/usr/sbin/adduser $quiet --disabled-login --gecos \"$fullname,$office,$office_phone,$home_phone,$other\" \"$u\""
  99. else
  100. if [ ! "$interactive" -gt 0 ]; then
  101. exit1 "Not enough info provided to create account for \"$u\"!"
  102. fi
  103. eval $simulate sudo "/usr/sbin/adduser --disabled-login \"$u\""
  104. fi
  105. for group in $groups; do
  106. eval $simulate sudo "/usr/sbin/adduser $quiet \"$u\" \"$group\""
  107. done
  108. eval $simulate localresetpasswd "$u"
  109. #vecho "Account \"$u\" created succesfully! Password is $pass"