#! /bin/sh #---------------------------------------------------------------------------- # /var/install/bin/modify-user - modify user details # # Copyright (c) 2001-2005 The Eisfair Team, c/o Frank Meyer, frank(at)eisfair(dot)org # # Creation: 09.03.2005 jed # Last Update: $Id$ # # usage: modify-user # or: modify-user -c login-name comment # or: modify-user -d login-name home-directory [-m|-r] # or: modify-user -e login-name YYYY-MM-DD # or: modify-user -f login-name number-of-days # or: modify-user -g login-name group-name # or: modify-user -s login-name user-shell # # or: change-group # # 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>./modify-user-trace-$$.log #set -x #------------------------------------------------------------------------------ # check if numeric value # input : $1 - value # return: 0 - numeric # 1 - no numeric #------------------------------------------------------------------------------ function is_numeric () { echo "$1"|grep -q '^[0-9]*$' } #---------------------------------------------------------------------------- # check user # input : $1 - username # return: 0 - exists # 1 - doesn't exist #---------------------------------------------------------------------------- function check_user() { u_name=$1 ret=1 grep -q "^$u_name:" /etc/passwd if [ $? -eq 0 ] then ret=0 else mecho -error "User '$u_name' doesn't exist!" fi return $ret } #---------------------------------------------------------------------------- # check group # input : $1 - groupname # return: 0 - exists # 1 - doesn't exist #---------------------------------------------------------------------------- function check_group() { g_name=$1 ret=1 grep -q "^$g_name:" /etc/group if [ $? -eq 0 ] then ret=0 else mecho -error "Group '$g_name' doesn't exist!" fi return $ret } #---------------------------------------------------------------------------- # check shell # input : $1 - shell name # return: 0 - exists # 1 - doesn't exist #---------------------------------------------------------------------------- function check_shell () { shell_name="$1" ret=1 { cat /etc/shells echo /bin/true echo /bin/false } | grep -q $shell_name if [ $? -eq 0 -a -f $shell_name ] then ret=0 else mecho -error "Shell '$shell_name' doesn't exist!" fi return $ret } #---------------------------------------------------------------------------------------- # check last day of month - YYYY-MM-DD # input: $1 - date # return: 0 - valid # 1 - invalid #---------------------------------------------------------------------------------------- function check_last_day_of_month() { d_str="$1" ret=1 year=`echo $d_str|cut -d- -f1` month=`echo $d_str|cut -d- -f2|sed 's/^0*//g'` day=`echo $d_str|cut -d- -f3|sed 's/^0*//g'` lastday=`cal $month $year | awk 'NF != 0{ last = $0 }; END{ print last }' | awk '{ print $NF }'` if [ $day -le $lastday ] then ret=0 fi return $ret } #---------------------------------------------------------------------------- # check date - YYYY-MM-DD # input : $1 - date # return: 0 - ok # 1 - wrong #---------------------------------------------------------------------------- function check_date () { date_str="$1" ret=1 _ifs="$IFS" IFS='-' set -- $date_str IFS="$_ifs" year="$1" month="$2" day="$3" if is_numeric "$year" && [ "$year" -gt 2000 -a "$year" -lt 2500 ] then if is_numeric "$month" && [ "$month" -ge 1 -a "$month" -le 12 ] then if is_numeric "$day" && check_last_day_of_month "${year}-${month}-${day}" && [ "$day" -ge 1 -a "$day" -le 31 ] then ret=0 else mecho -error "Invalid day '$day' given!" fi else mecho -error "Invalid month '$month' given!" fi else mecho -error "Invalid year '$year' given!" fi return $ret } #---------------------------------------------------------------------------- # check days - days or -1 # input : $1 - days # return: 0 - ok # 1 - wrong #---------------------------------------------------------------------------- function check_days () { days_str="$1" ret=1 if is_numeric "$days_str" then ret=0 else mecho -error "Invalid days value '$days_str' given!" fi return $ret } #---------------------------------------------------------------------------- # get group # input: $1 - username # return: 0 - ok # 1 - not found #---------------------------------------------------------------------------- function get_group () { u_name=$1 ret=1 line=`grep "^$u_name:" /etc/passwd` if [ "$line" != "" ] then _ifs="$IFS" IFS=: set -- $line gid_search=$4 not_found=1 while read line do set -- $line group_tmp="$1" gid_tmp="$3" if [ $gid_search -eq $gid_tmp ] then echo $group_tmp not_found=0 break fi done 2000 and < 65534 (nobody) are listed #---------------------------------------------------------------------------- create_userlist () { while read line do _ifs="$IFS" IFS=: set -- $line if [ $3 -ge 2000 -a $3 -lt 65534 ] then echo "$1:$5:" fi IFS="$_ifs" done < /etc/passwd | sort > $tmpfile } #---------------------------------------------------------------------------- # print list of users #---------------------------------------------------------------------------- print_userlist () { # print header clrhome mecho -info "List users" mecho techo begin 2 15 15 15 techo row "" -info User -info Group -info Name tty=`tty` row=4 while read line do _ifs="$IFS" IFS=: set -- $line user="$1" name="$2" group=`get_group "$user"` # output data techo row "" $user $group $name IFS="$_ifs" row=`expr $row + 1` if [ "$act_pmode" = 'tty' ] then if [ $row -eq 21 ] then mecho mecho anykey <$tty # print header clrhome mecho -info "List users" <$tty mecho techo row "" -info User -info Group -info Name row=4 fi fi done < $tmpfile techo end mecho } #---------------------------------------------------------------------------- # print list of groups #---------------------------------------------------------------------------- print_grouplist () { # print header clrhome mecho -info "List groups" mecho techo begin 2 15 10 techo row "" -info Group -info Gid tty=`tty` row=4 cat /etc/group|sort|while read line do _ifs="$IFS" IFS=: set -- $line group=$1 gid=$3 # output data techo row "" $group $gid IFS="$_ifs" row=`expr $row + 1` if [ "$act_pmode" = 'tty' ] then if [ $row -eq 21 ] then mecho mecho anykey <$tty # print header clrhome mecho -info "List groups" <$tty mecho techo row "" -info Group -info Gid row=4 fi fi done techo end mecho } #---------------------------------------------------------------------------- # print parameters #---------------------------------------------------------------------------- print_parameter () { line=`grep "^$user:" /etc/passwd` _ifs="$IFS" IFS=: set -- $line IFS="$_ifs" mecho "0-user:$1:" mecho "1-comment:$5:" mecho "2-home:$6:" mecho "3-shell:$7:" mecho "4-group:`get_group $user`:" mecho "5-in.time:`get_inactive_time $user`:" mecho "6-ex.date:`get_expire_date $user`:" } #---------------------------------------------------------------------------- # print header # input : $1 - username #---------------------------------------------------------------------------- print_header () { u_name=$1 clrhome mecho -info "Modify user" mecho mecho -n "Username: " mecho -warn "$u_name" mecho } #---------------------------------------------------------------------------- # print list of functions #---------------------------------------------------------------------------- print_functions () { mecho "Functions:" mecho techo begin 1 2 2 25 2 2 25 techo row "" 1 - "change comment" 4 - "change group" techo row "" 2 - "change home directory" 5 - "set inactive time" techo row "" 3 - "change user shell" 6 - "set expire date" techo end mecho } #---------------------------------------------------------------------------- # print error message and usage message and exit #---------------------------------------------------------------------------- too_few_args () { mecho -error "Too few arguments" $0 -? exit 1 } #============================================================================ # main #============================================================================ act_pmode=`get_printmode` pgmname=`basename $0` if [ $# -eq 0 ] then base=`basename $0` tmpfile="/tmp/$base-$$" create_userlist interactive='true' clrhome mecho -info "Modify user" mecho uname='' u_exit=0 until [ $u_exit -eq 1 ] do if [ "$uname" = "" -o "$uname" = "l" ] then # read uname /var/install/bin/ask "Please enter username to modify (user), (l)ist or (q)uit:" '' '+' > /tmp/ask.$$ rc=$? uname=`cat /tmp/ask.$$` rm -f /tmp/ask.$$ if [ $rc = 255 ]; then rm $tmpfile exit 1 fi fi case $uname in l ) # list users print_userlist ;; q ) # quit u_exit=1 ;; * ) # process user grep -q "^$uname:" $tmpfile if [ $? -eq 0 ] then if [ "$pgmname" != "change-group" ] then print_header $uname print_functions fi f_exit=0 until [ $f_exit -eq 1 ] do if [ "$pgmname" = "change-group" ] then func=4 else # read func /var/install/bin/ask "Please select function number (1-6), (q)uit:" '' '+' > /tmp/ask.$$ rc=$? func=`cat /tmp/ask.$$` rm -f /tmp/ask.$$ if [ $rc = 255 ] then rm $tmpfile exit 1 fi fi case $func in 1 ) # change comment old_comment=`grep "^$uname:" /etc/passwd|cut -d: -f5` print_header $uname mecho "Current comment: $old_comment" mecho /var/install/bin/ask "Please enter new comment or (q)uit:" '' '*' > /tmp/ask.$$ rc=$? comment=`cat /tmp/ask.$$` rm -f /tmp/ask.$$ if [ $rc = 255 ] then rm $tmpfile exit 1 fi if [ "$comment" != "q" ] then print_header $uname mecho "Current comment: $old_comment" mecho "New comment : $comment" mecho /var/install/bin/ask "Change comment" 'n' > /tmp/ask.$$ rc=$? yesno=`cat /tmp/ask.$$` rm -f /tmp/ask.$$ if [ $rc = 255 ] then rm $tmpfile exit 1 fi if [ "$yesno" = "yes" ] then $0 -c $uname "$comment" fi f_exit=1 u_exit=1 else # quit - return to main menue f_exit=1 fi ;; 2 ) # change home directory old_home=`grep "^$uname:" /etc/passwd|cut -d: -f6` print_header $uname mecho "Current home directory: $old_home" mecho /var/install/bin/ask "Please enter new home directory or (q)uit:" '' '+' > /tmp/ask.$$ rc=$? home=`cat /tmp/ask.$$` rm -f /tmp/ask.$$ if [ $rc = 255 ] then rm $tmpfile exit 1 fi if [ "$home" != "q" -a "$home" != "" ] then print_header $uname mecho "Current home directory: $old_home" mecho "New home directory : $home" mecho /var/install/bin/ask "Change home directory" 'n' > /tmp/ask.$$ rc=$? yesno=`cat /tmp/ask.$$` rm -f /tmp/ask.$$ if [ $rc = 255 ] then rm $tmpfile exit 1 fi if [ "$yesno" = "yes" ] then /var/install/bin/ask "Move old directory content to new location" 'y' > /tmp/ask.$$ rc=$? yesno=`cat /tmp/ask.$$` rm -f /tmp/ask.$$ if [ $rc = 255 ] then rm $tmpfile exit 1 fi opts='' if [ "$yesno" = "yes" ] then opts='-m' else /var/install/bin/ask "Delete old directory content" 'n' > /tmp/ask.$$ rc=$? yesno=`cat /tmp/ask.$$` rm -f /tmp/ask.$$ if [ $rc = 255 ] then rm $tmpfile exit 1 fi if [ "$yesno" = "yes" ] then opts="-r" fi fi $0 -d $uname "$home" $opts fi f_exit=1 u_exit=1 else # quit - return to main menue f_exit=1 fi ;; 3 ) # change user shell old_shell=`grep "^$uname:" /etc/passwd|cut -d: -f7` print_header $uname mecho "Current shell: $old_shell" mecho /var/install/bin/ask "Please enter new shell or (q)uit:" '' '+' > /tmp/ask.$$ rc=$? shell=`cat /tmp/ask.$$` rm -f /tmp/ask.$$ if [ $rc = 255 ]; then rm $tmpfile exit 1 fi if [ "$shell" != "q" -a "$shell" != "" ] then print_header $uname mecho "Current shell: $old_shell" mecho "New shell : $shell" mecho /var/install/bin/ask "Change shell" 'n' > /tmp/ask.$$ rc=$? yesno=`cat /tmp/ask.$$` rm -f /tmp/ask.$$ if [ $rc = 255 ] then rm $tmpfile exit 1 fi if [ "$yesno" = "yes" ] then $0 -s $uname "$shell" fi f_exit=1 u_exit=1 else # quit - return to main menue f_exit=1 fi ;; 4 ) # change group old_gname=`get_group $uname` if [ $? -eq 0 ] then print_header $uname mecho "Current group: $old_gname" mecho gname='' g_exit=0 until [ $g_exit -eq 1 ] do if [ "$gname" = "" -o "$gname" = "l" ] then # read new group /var/install/bin/ask "Please enter new (group), (l)ist or (q)uit:" '' '+' > /tmp/ask.$$ rc=$? gname=`cat /tmp/ask.$$` rm -f /tmp/ask.$$ if [ $rc = 255 ] then rm $tmpfile exit 1 fi fi case $gname in l ) # list users print_grouplist ;; q ) # quit g_exit=1 f_exit=1 if [ "$pgmname" = "change-group" ] then u_exit=1 fi ;; * ) # process group grep -q "^$gname:" /etc/group if [ $? -eq 0 ] then print_header $uname mecho "Current group: $old_gname" mecho "New group : $gname" mecho /var/install/bin/ask "Change group" 'n' > /tmp/ask.$$ rc=$? yesno=`cat /tmp/ask.$$` rm -f /tmp/ask.$$ if [ $rc = 255 ] then rm $tmpfile exit 1 fi if [ "$yesno" = "yes" ] then $0 -g $uname "$gname" fi g_exit=1 f_exit=1 u_exit=1 fi esac done else mecho -error "Group with '$gname' doesn't exist!" fi ;; 5 ) # set inactive time old_itime=`get_inactive_time $uname` print_header $uname mecho "Current inactive time: $old_itime" mecho /var/install/bin/ask "Please enter new inactive time (number), (never) or (q)uit:" '' '+' > /tmp/ask.$$ rc=$? itime=`cat /tmp/ask.$$` rm -f /tmp/ask.$$ if [ $rc = 255 ] then rm $tmpfile exit 1 fi if [ "$itime" != "q" -a "$itime" != "" ] then print_header $uname mecho "Current inactive time: $old_itime" mecho "New inactive time : $itime" mecho /var/install/bin/ask "Change inactive time" 'n' > /tmp/ask.$$ rc=$? yesno=`cat /tmp/ask.$$` rm -f /tmp/ask.$$ if [ $rc = 255 ] then rm $tmpfile exit 1 fi if [ "$yesno" = "yes" ] then $0 -f $uname "$itime" fi f_exit=1 u_exit=1 else # quit - return to main menue f_exit=1 fi ;; 6 ) # set expire date old_exdate=`get_expire_date $uname` print_header $uname mecho "Current expire date: $old_exdate" mecho /var/install/bin/ask "Please enter new expire date (YYYY-MM-DD), (never) or (q)uit:" '' '+' > /tmp/ask.$$ rc=$? exdate=`cat /tmp/ask.$$` rm -f /tmp/ask.$$ if [ $rc = 255 ]; then rm $tmpfile exit 1 fi if [ "$exdate" != "q" -a "$exdate" != "" ] then print_header $uname mecho "Current expire date: $old_exdate" mecho "New expire date : $exdate" mecho /var/install/bin/ask "Change expire date" 'n' > /tmp/ask.$$ rc=$? yesno=`cat /tmp/ask.$$` rm -f /tmp/ask.$$ if [ $rc = 255 ] then rm $tmpfile exit 1 fi if [ "$yesno" = "yes" ] then $0 -e $uname "$exdate" fi f_exit=1 u_exit=1 else # quit - return to main menue f_exit=1 fi ;; q ) # quit f_exit=1 u_exit=1 ;; *) # wrong input mecho -warn "Wrong input: $func" ;; esac done else mecho -warn "User '$uname' doesn't exist!" uname='' anykey fi ;; esac done rm -f $tmpfile else interactive='false' fi case $1 in '-c' ) # change comment user="$2" name="$3" if check_user $user then /usr/sbin/usermod -c "$name" $user fi ;; '-d' ) # change home directory user="$2" home="$3" option="$4" [ "$home" = '' ] && too_few_args if check_user $user then # get old home directory oldhome=`grep "^$user:" /etc/passwd|cut -d: -f6` if [ "$oldhome" != "$home" ] then case "$option" in -m ) # move directory content # directory will be created automatically! /usr/sbin/usermod -m -d "$home" $user ;; -r ) # remove old home directory create_homedir "$home" $user /usr/sbin/usermod -d "$home" $user rm -f -R $oldhome ;; '' ) # normal processing create_homedir "$home" $user /usr/sbin/usermod -d "$home" $user ;; esac fi fi ;; '-e' ) # set expire date - format: YYYY-MM-DD or -1-disable feature user="$2" date="$3" [ "$date" = '' ] && too_few_args date=`echo $date|tr 'A-Z' 'a-z'` if [ "$date" = "never" ] then # reset expiry function /usr/sbin/usermod -e '-1' $user elif check_user $user && check_date "$date" then # set date /usr/sbin/usermod -e $date $user fi ;; '-f' ) # set inactive time - 0-disable account, -1-disable feature, 1+-number of days user="$2" days="$3" [ "$days" = '' ] && too_few_args days=`echo $days|tr 'A-Z' 'a-z'` if [ "$days" = "never" ] then # reset inactivity function /usr/sbin/usermod -f '-1' $user elif check_user $user && check_days $days then /usr/sbin/usermod -f $days $user fi ;; '-g' ) # change group user="$2" group="$3" [ "$group" = '' ] && too_few_args if check_user $user && check_group $group then new_gid=`get_gid $group` home=`grep "^$user:" /etc/passwd|cut -d: -f6` if [ $? -eq 0 ] then /usr/sbin/usermod -g $new_gid $user chgrp -R $group $home fi fi ;; '-l' ) # list current values user="$2" [ "$user" = '' ] && too_few_args if check_user $user then print_parameter fi ;; '-s' ) # change user shell user="$2" shell="$3" [ "$shell" = '' ] && too_few_args if check_user $user && check_shell "$shell" then /usr/sbin/usermod -s "$shell" "$user" fi ;; '-?'|'--help' ) # show help mecho "usage: $pgmname" >&2 mecho " or: $pgmname -c login-name comment" >&2 mecho " or: $pgmname -d login-name home-directory [-m|-r]" >&2 mecho " or: $pgmname -e login-name YYYY-MM-DD" >&2 mecho " or: $pgmname -f login-name number-of-days" >&2 mecho " or: $pgmname -g login-name group-name" >&2 mecho " or: $pgmname -s login-name user-shell" >&2 exit 1 ;; esac if [ "$interactive" = "true" ] then mecho anykey fi