summaryrefslogtreecommitdiff
path: root/localadduser
blob: 433315c4cedb73bcceeda26d86b4b03dfbe18a1d (plain)
  1. #!/bin/sh
  2. #
  3. # /usr/local/bin/localadduser
  4. # Copyright 2003 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: localadduser,v 1.3 2006-08-25 04:36:44 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. pass_len=11
  16. pass_expire=30
  17. pass_warn=14
  18. verbose=1
  19. #simulate=true
  20. interactive=0
  21. # echo something, but only if in verbose mode
  22. vecho() {
  23. test -n "$verbose" && echo "$@" >&2
  24. }
  25. randompass() {
  26. gpw 1 $pass_len 2>&- ||
  27. pwgen $pass_len 1 2>&- ||
  28. tr -d '[\000-\057][\072-\100][\133-\140][\173-\377]' < /dev/urandom |
  29. dd bs=$pass_len count=1 2>&-
  30. }
  31. u=$1
  32. shift
  33. for chunk in $@; do
  34. case $chunk in
  35. *@*)
  36. other="${other:+$other }$chunk"
  37. ;;
  38. +*)
  39. phone_area="$chunk"
  40. ;;
  41. 0*|1*|2*|3*|4*|5*|6*|7*|8*|9*)
  42. if [ -z "$phone_area" ]; then
  43. echo "E: Phone number provided without leading area code" >&2
  44. exit 1
  45. fi
  46. if [ -n "$office_phone" ]; then
  47. echo "E: More than 2 phone numbers provided" >&2
  48. exit 1
  49. elif [ -n "$home_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. echo "E: Area code provided without trailing phonenumber" >&2
  63. exit 1
  64. fi
  65. if [ ! "$interactive" -gt 0 ]; then
  66. quiet="--quiet"
  67. fi
  68. if [ -n "$fullname$office_phone$home_phone$other" ]; then
  69. eval $simulate sudo "/usr/sbin/adduser $quiet --disabled-login --gecos \"$fullname,,$office_phone,$home_phone,$other\" \"$u\""
  70. else
  71. if [ ! "$interactive" -gt 0 ]; then
  72. echo "E: Not enough info provided to create account for \"$u\"" >&2
  73. exit 1
  74. fi
  75. eval $simulate sudo "/usr/sbin/adduser --disabled-login \"$u\""
  76. fi
  77. vecho -n "generating password for $u..."
  78. pass=$(randompass)
  79. echo "$u:$pass" | eval $simulate sudo "/usr/sbin/chpasswd"
  80. vecho -n "."
  81. #eval $simulate sudo "/usr/bin/chage -M $pass_expire -d 2003-01-01 \"$u\""
  82. eval $simulate sudo "/usr/bin/chage -M$pass_expire -W$pass_warn -d 2003-01-01 \"$u\""
  83. vecho -n "."
  84. vecho "$pass"