adduser.sh cz. 2

Marek Obuchowicz elephant w shadow.eu.org
Pią, 25 Wrz 1998, 21:35:47 CEST


Zguba idzie :)

+--------------------------------------------------------+
|       Marek Obuchowicz <elephant w shadow.eu.org>        |
|      Magazyn Wirtu w l - http://wirtual.to.jest.to       |
| oRIGIN: cAN aNYBODY tELL mE hOW tO tURN oFF cAPS lOCK? |
+--------------------------------------------------------+
-------------- następna część ---------
#!/bin/bash
#
#  adduser			Interactive user adding program.
#
#  Copyright (C) 1996		Petri Mattila, Prihateam Networks
#				petri w prihateam.fi
#	
#  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, or (at your option)
#  any later version.
#
# Changes:
#	220496	v0.01	Initial version
#	230496	v0.02	More checks, embolden summary
#	240496		Even more checks
#	250496 		Help with ?
#	040596	v0.03	Cleanups
#	050596	v0.04	Bug fixes, expire date checks
#	070596	v0.05	Iso-latin-1 names
#	230998	v0.06p	Polish Linux Distribution release
#			 by Marek Obuchowicz <elephant w shadow.eu.org>
#			 some thinks (like setting password-aging or
#			 help with ?) removed, a lot of modifications...
#


login_regex='^[a-zA-Z0-9-_]*$'
home_regex='^/[a-zA-Z0-9/-_]*$'

## Functions

check_root() {
	if test "$EUID" -ne 0
	then
		echo "Aby dodać użytkownika musisz być rootem."
		exit 1
	fi
}

check_user() {
	local usr pwd uid gid name home sh

	cat /etc/passwd | (
		while IFS=":" read usr pwd uid gid name home sh
		do
			if test "$1" = "${usr}"
			then
				return 1
			fi
		done
		return 0
	)
}

check_group() {
	local read grp pwd gid members

	cat /etc/group | (
		while IFS=":" read grp pwd gid members
		do
			if test "$1" = "${grp}"
			then
				return 1
			fi
		done
		return 0
	)
}

check_other_groups() {
	local grp check IFS

	check="$1"
	IFS=","

	set ${check}
	for grp
	do
		if check_group "${grp}"
		then
			echo "Grupa ${grp} nie istnieje."
			return 1
		fi
	done
	return 0
}		

check_uid() {
	local usr pwd uid gid name home sh
	
	cat /etc/passwd | (
		while IFS=":" read usr pwd uid gid name home sh
		do
			if test "$1" = "${uid}"
			then
				return 1
			fi
		done
		return 0
	)
}

read_yn() {
	local ans ynd
	
	ynd="$1"
	
	while :
	do
		read ans	
		case "${ans}" in
		      "") return ${ynd} ;;
		    [nN]) return 1 ;;
		    [yYtT]) return 0 ;;
		       *) echo -n "[T]ak lub [N]ie ? " ;;
		esac
	done
}

read_login() {
	while :
	do
		echo -n "login [${def_login:+${def_login}}]: "
		read login
		
		if test -z "${login}" -a -n "${def_login}"
		then
			login="${def_login}"
			return
		fi

		if [ "`echo ${login} | grep ${login_regex}`" = "" ]; then
			echo "Login może zawierać litery, cyfry oraz znaki -_"
			continue
		fi
		
		if test "${#login}" -gt 8
		then
			echo "Login może mieć maksymalnie 8 znaków"
			continue
		fi
		
		if test "${#login}" -lt 2
		then
			echo "Login musi mieć conajmniej 2 znaki"
			continue
		fi
		
		if ! check_user "${login}"
		then
			echo "Użytkownik ${login} już istnieje"
			continue
		fi
		
		def_login="${login}"
		return
	done
}

read_name () {
	while :
	do
		echo -n "Prawdziwe imię [${def_name:+${def_name}}]: "
		read name
		
		if test -z "${name}" -a -n "${def_name}"
		then
			name="${def_name}"
		fi

		if test "${#name}" -gt 32
		then
			echo "Imię może mieć maksymalnie 32 znaki"
			continue
		fi

		def_name="${name}"
		return
	done
}

read_home() {
	local x
	
	while :
	do
		echo -n "Katalog domowy [${def_home_dir}/${login}]: "
		read home
		
		if test -z "${home}"
		then
			home="${def_home_dir}/${login}"
		fi

		if [ "`echo ${home} | grep ${home_regex}`" = "" ]; then
			echo "Katalog domowy musi się rozpoczynać od / oraz może zawierać"
			echo "litery, liczby oraz znaki _-/"
			continue
		fi
		
		x="$(basename ${home})"
		if test "${x}" != "${login}"
		then
			echo "Uwaga: login jest inny od nazwy katalogu domowego."
		fi
		
		x="$(dirname ${home})"
		if ! test -d "${x}"
		then
			echo "Katalog ${x} nie istnieje."
			echo "Jeżeli nadal chcesz go użyć, utwórz go ręcznie."
			continue
		fi
		
		def_home_dir="${x}"
		return
	done
}

read_shell () {
	local x

	while :
	do
		echo -n "Shell [${def_shell}]: "
		read shell
		
		if test -z "${shell}"
		then
			shell="${def_shell}"
		fi
		
		for x in $(cat /etc/shells)
		do
			if test "${x}" = "${shell}"
			then
				def_shell="${shell}"
				return
			fi
		done

		echo "Dostępne shelle:"
		cat /etc/shells
	done
}

read_group () {
	while :
	do
		echo -n "Grupa [${def_group}]: "
		read group
		
		if test -z "${group}"
		then
			group="${def_group}"
		fi
		
		if check_group "${group}"
		then
			echo "Grupa ${group} nie istnieje."
			continue
		fi
		
		def_group="${group}"
		return
	done
}

read_other_groups () {
	while :
	do
		echo -n "Dodatkowe grupy [${def_og}]: "
		read other_groups
		
		if test -z "${other_groups}"
		then
			if test -n "${def_og}"
			then
				other_groups="${def_og}"
			else	
				return
			fi
		fi
		
		
		if ! check_other_groups "${other_groups}"
		then
			continue
		fi
		
		def_og="${other_groups}"
		return
	done
}

read_uid () {
	while :
	do
		echo -n "UID [pierwszy wolny]: "
		read uid
			
		if test -z "${uid}"
		then
			return
		fi
		
		if test "${uid}" -lt "${uid_low}"
		then
			echo "UID musi być większe od ${uid_low}"
			continue
		fi
		if test "${uid}" -gt "${uid_high}"
		then
			echo "UID musi być mniejsze od ${uid_high}"
			continue
		fi
		if ! check_uid "${uid}"
		then
			echo "UID ${uid} jest już zajęty."
			continue
		fi
		
		return
	done
}

read_passwd_yn() {
	echo -en "\nCzy chcesz ustawić hasło [T/n] ? "
	if read_yn 0
	then
		set_pwd="YES"
	else
		set_pwd=""
	fi
}


print_values() {

cat << _EOF_

Login:            ${login}
Grupa:            ${group}
Pozostałe grupy:  ${other_groups}
Prawdziwe imię:   ${name}
UID:              ${uid:-[pierwszy wolny]}
Katalog domowy:   ${home}
Shell:            ${shell}
${set_pwd:+Ustaw hasło użytkownikowi.}

_EOF_
}

set_user() {
	if ! useradd \
		-m \
		-c "${name}" \
		-d "${home}" \
		-g "${group}" \
		-s "${shell}" \
		${expire:+-e ${expire}} \
		${uid:+-u ${uid}} \
		${other_groups:+-G ${other_groups}} \
		${login}
	then
		echo "Błąd ($?) w useradd..."
		exit 1
	fi
}

set_password() {
	if test -n "${set_pwd}"
	then
		echo
		passwd ${login}
		echo
	fi
}	

set_system() {
	## Add your own stuff here:
	chmod ${def_mode} ${home}	
}


read_values() {
	echo "Proszę podać dane:"
	while :
	do
		read_login
		read_name
		read_group
		read_other_groups
		read_home
		read_shell
		read_uid
		read_passwd_yn

		print_values
		
		echo -n "Jesteś pewien [t/N] ? "
		read_yn 1 && return
	done
}

read_defaults() {
  if [ -f /etc/default/adduser ]; then
    . /etc/default/adduser
  else
    echo "Brak pliku /etc/default/adduser!"
    exit 1
  fi
}


main() {
	check_root
	read_defaults
	read_values
	set_user
	set_system
	set_password
}


# Uruchomienie - nareszcie ;)
def_login=$1
main


Więcej informacji o liście dyskusyjnej pld-devel-pl