summaryrefslogtreecommitdiff
path: root/mass_useradd
blob: cf0fe2c3b8b0864006341701f8feaea5e9015adf (plain)
  1. #!/bin/sh
  2. ##
  3. ## Mass Useradd For Linux
  4. ## This script that extracts new user data from a delimited text file,
  5. ## and automatically generates new user accounts. It generates a random
  6. ## password for each login, and exports the new logins and passwords to
  7. ## a text file. Passwords automatically expire at first login. The
  8. ## input file format is "username: full name" (no quotes) for each
  9. ## line.
  10. ##
  11. ## Mass Useradd creates a "User Personal Group."
  12. ## The UID and the GID are the same. User's home directories
  13. ## are created with restrictive permissions, chmod 700.
  14. ## Mass Useradd uses standard Shadow Suite utilities.
  15. ## Values and behaviors are easily modifiable, according
  16. ## to the individual utility being called. It calls a companion
  17. ## script, mass_passwd, to set each user password. You should
  18. ## have received mass_passwd from the same source as mass_useradd.
  19. ##
  20. ## This script was created by Aaron Malone, and modified by
  21. ## Meredydd Luff, Peter Samuelson, and Kathryn Hogg.
  22. ## Many thanks!
  23. ## Carla Schroder wrote the documentation and pestered
  24. ## the aforementioned persons to modify the original script.
  25. ## Copyright (C) 2003 Carla Schroder
  26. ## carla at bratgrrl dot com
  27. ## This program is free software; you can redistribute it and/or
  28. ## modify it under the terms of the GNU General Public License
  29. ## as published by the Free Software Foundation; either version 2
  30. ## of the License, or (at your option) any later version.
  31. ##
  32. ## This program is distributed in the hope that it will be useful,
  33. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  34. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35. ## GNU General Public License for more details.
  36. ## http://www.fsf.org/licenses/gpl.html
  37. ##
  38. ## Usage:
  39. ## # sh mass_useradd < inputfile >> new-passwords.txt
  40. ##
  41. PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:$PATH
  42. # Read a line of input.
  43. # the format of the input file must be like this:
  44. # userlogin : FirstName LastName
  45. # to use a comma-delimited file, change IFS=":$IFS" to IFS=",$IFS"
  46. while IFS=":$IFS" read username realname; do
  47. # First, weed out blank lines and #comments
  48. case "$username" in
  49. '' | \#*) continue ;;
  50. esac
  51. # this part reads /etc/passwd and /etc/group, and calculates
  52. # the next available UID and GID.
  53. # it starts at {id=1000}, change this to suit
  54. id=$({ getent passwd; getent group; } | cut -f3 -d: | sort -un |
  55. awk 'BEGIN { id=1000 }
  56. $1 == id { id++ }
  57. $1 > id { print id; exit }')
  58. # Now users are added to /etc/group, /etc/passwd,
  59. # and home directories with chmod 700 are created
  60. # Any of the groupadd, useradd, and chmod options
  61. # can be changed to suit
  62. groupadd -g $id $username
  63. useradd -m -c "$realname" -g $username -u $id $username
  64. chmod 700 /home/$username
  65. # Set the password. This calls another script from this toolkit,
  66. # mass_passwd, which can be used independently. mass_passwd outputs
  67. # the username, password and userid.
  68. $(dirname $0)/mass_passwd -M $username
  69. done