summaryrefslogtreecommitdiff
path: root/localadduser
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2006-08-25 02:34:29 +0000
committerJonas Smedegaard <dr@jones.dk>2006-08-25 02:34:29 +0000
commitecf07f49b24276116aec8b8f1f6a12aedcd23b36 (patch)
tree409a916b47d7f247cec49af4434dfac586a8eee6 /localadduser
parent05dbeac2a51c997f4e8837ce5597829d46e59dff (diff)
Extend to handle noninteractive use.
Diffstat (limited to 'localadduser')
-rwxr-xr-xlocaladduser88
1 files changed, 85 insertions, 3 deletions
diff --git a/localadduser b/localadduser
index bf93954..774c24c 100755
--- a/localadduser
+++ b/localadduser
@@ -3,11 +3,93 @@
# /usr/local/bin/localadduser
# Copyright 2003 Jonas Smedegaard <dr@jones.dk>
#
-# $Id: localadduser,v 1.1 2003-12-17 14:53:28 jonas Exp $
+# $Id: localadduser,v 1.2 2006-08-25 02:34:29 jonas Exp $
#
-# Execute adduser through sudo
+# Execute adduser noninteractively through sudo
#
# TODO: Check for bad arguments
+# TODO: Use getopts to offer help
+# TODO: Support overriding options in /etc/local file
#
-exec sudo /usr/sbin/adduser $1
+set -e
+set -x
+
+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"