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