#! /bin/sh #---------------------------------------------------------------------------- # /var/install/bin/add-user - add user # # Copyright (c) 2001-2005 The Eisfair Team, c/o Frank Meyer, frank(at)eisfair(dot)org # # Creation: 04.11.2001 fm # Last Update: $Id$ # # usage: add-user # or: add-user [-d|-l] user encrypted-password uid gid name home shell # # option -d : disable password # option -l : lock password # # if password is empty, user will be prompted # if uid or gid is empty, values will be evaluated # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #---------------------------------------------------------------------------- # include eislib . /var/install/include/eislib #exec 2>./add-user-trace-$$.log #set -x ### ------------------------------------------------------------------------- ### get optional flags ### ------------------------------------------------------------------------- flags='' noadd=0 while [ 1 ] do case "$1" in -d) if [ $noadd -eq 0 ] then flags="$flags -d" noadd=1 else mecho -warn "Redundant option '-d' will be ignored!" fi shift ;; -l) if [ $noadd -eq 0 ] then flags="$flags -l" noadd=1 else mecho -warn "Redundant option '-l' will be ignored!" fi shift ;; *) break ;; esac done ### ------------------------------------------------------------------------- ### get parameter ### ------------------------------------------------------------------------- case $# in 0) interactive='true' user="" password="" uid="" gid="" name="" home="" shell="" ;; 7) interactive='false' user="$1" password="$2" uid="$3" gid="$4" name="$5" home="$6" shell="$7" ;; *) echo "usage: `basename $0`" >&2 echo " or: `basename $0` user encrypted-password uid gid name home shell" >&2 exit 1 ;; esac ### ------------------------------------------------------------------------- ### clear screen ### ------------------------------------------------------------------------- if [ "$interactive" = "true" ] then clrhome mecho -info "Add user" echo fi ### ------------------------------------------------------------------------- ### create uid ### ------------------------------------------------------------------------- if [ "$uid" = "" ] then oldifs="$IFS" IFS=':' uid=2000 while read line do set -- $line if [ $3 -gt $uid -a $3 -lt 3000 ] then uid=$3 fi done /tmp/ask.$$ rc=$? user=`cat /tmp/ask.$$` rm -f /tmp/ask.$$ if [ $rc = 255 ] then exit 1 fi fi ### ------------------------------------------------------------------------- ### get password and append user to files ### ------------------------------------------------------------------------- echo case "$user" in "") mecho -warn "no user added" echo anykey exit 1 ;; *) grep "^$user:" /etc/passwd >/dev/null if [ $? = 0 ] then mecho -error "user $user already exists" echo anykey exit 1 else if [ "$name" = "" ] then /var/install/bin/ask "Name of user (comment field):" '' '*' > /tmp/ask.$$ rc=$? name=`cat /tmp/ask.$$` rm -f /tmp/ask.$$ if [ $rc = 255 ] then exit 1 fi fi grep -q "^users:" /etc/group if [ $? != 0 ] then /usr/sbin/groupadd -g 100 users fi if [ "$gid" = "" ] then gid=100 else gidexists=false for a in `cat /etc/group | cut -d":" -f3` do if [ "$gid" = "$a" ] then gidexists=true fi done if [ "$gidexists" = "false" ] then mecho -error "gid $gid doesn't exists" echo anykey exit 1 fi fi homeswitch="-m" case "$home" in "") home="/home/$user" ;; "/dev/null") home="/home/__dummyhome__" homeswitch="-M" ;; esac if [ "$shell" = "" ] then shell=/bin/bash fi /usr/sbin/useradd -u "$uid" -g "$gid" -c "$name" -s "$shell" -d "$home" "$homeswitch" $user case $flags in *-d) # disable password passwd -d $user >/dev/null ;; *-l) # lock password passwd -l $user >/dev/null ;; *) # get / set password if [ -z "$password" ] then idx=1 while [ $idx -le 3 ] do passwd $user if [ $? = 0 ] then break fi idx=`/usr/bin/expr $idx + 1` if [ $idx -eq 4 ] then # disable password passwd -d $user >/dev/null echo mecho -error "Failed to get a valid password. Login for $user disabled." mecho -std "Please use 'passwd $user' to change password manually." anykey fi done fi ;; esac # set permissions of home if [ "$home" != "/home/__dummyhome__" ] then chown $user $home chgrp $gid $home chmod 700 $home fi fi ;; esac if [ "$interactive" = "true" ] then echo anykey fi