adduser.sh

Marek Obuchowicz elephant w shadow.eu.org
Pon, 21 Wrz 1998, 00:36:11 CEST


Oto przerobiona na polski i działająca wersja adduser.sh:
#!/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
#	200998	v0.05p	Polish Linux Distribution release
#			 by Marek Obuchowicz <elephant w shadow.eu.org>
#			 some thinks (like setting password-aging or
#			 help with ?) removed
#



def_group="users"
def_other_groups=""

def_home_dir=/home/users

def_shell=/bin/bash

uid_low=1000
uid_high=64000

def_mode=711

login_regex='^[0-9a-zA-Z_-]*$'

space='                 '

## 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 "${space}login: ${def_login:+[${def_login}] }"
		read login
		
		if test -z "${login}" -a -n "${def_login}"
		then
			login="${def_login}"
			return
		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 "${space}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 "${space}Katalog domowy: [${def_home_dir}/${login}] "
		read home
		
		if test -z "${home}"
		then
			home="${def_home_dir}/${login}"
		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 "${space}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 "${space}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 "${space}Dodatkowe grupy: [${def_og:-brak}] "
		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 "${space}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 "\n${space}Czy chcesz ustawić hasło [T/n] ? "
	if read_yn 0
	then
		set_pwd="YES"
	else
		set_pwd=""
	fi
}


print_values() {

cat << _EOF_


${space}Login:           ${login}
${space}Grupa:           ${group}
${space}Pozostałe grupy: ${other_groups:-[none]}
${space}Prawdziwe imię:  ${name}
${space}uid:             ${uid:-[pierwszy wolny]}
${space}katalog domowy:  ${home}
${space}shell:           ${shell}
${space}${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:
        echo 
}


read_values() {
	echo
	echo "${space}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 "${space}Jesteś pewien [t/N] ? "
		read_yn 1 && return
	done
}


main() {
	check_root
	read_values
	set_user
	set_system
	set_password
}


## Run it 8-)
main

# End.

W sumie nie ma sensu przerabiać tego na C...

+--------------------------------------------------------+
|       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? |
+--------------------------------------------------------+



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