blob: 433315c4cedb73bcceeda26d86b4b03dfbe18a1d (
plain)
- #!/bin/sh
- #
- # /usr/local/bin/localadduser
- # Copyright 2003 Jonas Smedegaard <dr@jones.dk>
- #
- # $Id: localadduser,v 1.3 2006-08-25 04:36:44 jonas Exp $
- #
- # Execute adduser noninteractively through sudo
- #
- # TODO: Check for bad arguments
- # TODO: Use getopts to offer help
- # TODO: Support overriding options in /etc/local file
- #
- set -e
- pass_len=11
- pass_expire=30
- pass_warn=14
- verbose=1
- #simulate=true
- interactive=0
- # echo something, but only if in verbose mode
- vecho() {
- test -n "$verbose" && echo "$@" >&2
- }
- randompass() {
- gpw 1 $pass_len 2>&- ||
- pwgen $pass_len 1 2>&- ||
- tr -d '[\000-\057][\072-\100][\133-\140][\173-\377]' < /dev/urandom |
- dd bs=$pass_len count=1 2>&-
- }
- u=$1
- shift
- for chunk in $@; do
- case $chunk in
- *@*)
- other="${other:+$other }$chunk"
- ;;
- +*)
- phone_area="$chunk"
- ;;
- 0*|1*|2*|3*|4*|5*|6*|7*|8*|9*)
- if [ -z "$phone_area" ]; then
- echo "E: Phone number provided without leading area code" >&2
- exit 1
- fi
- if [ -n "$office_phone" ]; then
- echo "E: More than 2 phone numbers provided" >&2
- exit 1
- elif [ -n "$home_phone" ]; then
- office_phone="$phone_area $chunk"
- else
- home_phone="$phone_area $chunk"
- fi
- phone_area=""
- ;;
- *)
- fullname="${fullname:+$fullname }$chunk"
- ;;
- esac
- done
- if [ -n "$phone_area" ]; then
- echo "E: Area code provided without trailing phonenumber" >&2
- exit 1
- fi
- if [ ! "$interactive" -gt 0 ]; then
- quiet="--quiet"
- fi
- if [ -n "$fullname$office_phone$home_phone$other" ]; then
- eval $simulate sudo "/usr/sbin/adduser $quiet --disabled-login --gecos \"$fullname,,$office_phone,$home_phone,$other\" \"$u\""
- else
- if [ ! "$interactive" -gt 0 ]; then
- echo "E: Not enough info provided to create account for \"$u\"" >&2
- exit 1
- fi
- eval $simulate sudo "/usr/sbin/adduser --disabled-login \"$u\""
- fi
- vecho -n "generating password for $u..."
- pass=$(randompass)
- echo "$u:$pass" | eval $simulate sudo "/usr/sbin/chpasswd"
- vecho -n "."
- #eval $simulate sudo "/usr/bin/chage -M $pass_expire -d 2003-01-01 \"$u\""
- eval $simulate sudo "/usr/bin/chage -M$pass_expire -W$pass_warn -d 2003-01-01 \"$u\""
- vecho -n "."
- vecho "$pass"
|