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