dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles # fast
git clone https://src.adamsgaard.dk/dotfiles.git # slow
Log | Files | Refs | README | LICENSE Back to index

newuser (1713B)


      1 #!/bin/sh
      2 set -e
      3 
      4 version=0.1.0
      5 
      6 die() {
      7 	printf '%s\n' "$1"
      8 	exit 1
      9 }
     10 
     11 help() {
     12 	echo "usage: ${0##*/} [OPTIONS] USER ..."
     13 	echo "will create a new USER with 'nopass' and 'keepenv' rules."
     14 	echo
     15 	echo "Graphical commands can be started with:"
     16 	echo "  $ ssh -Y USER@localhost COMMAND"
     17 	echo "  $ ssh -X USER@localhost COMMAND"
     18 	echo "The -Y option has native performance but full access to the"
     19 	echo "current X session. The -X option has restricted access but"
     20 	echo "reduced performance."
     21 	echo 
     22 	echo "OPTIONS are one or more of the following:"
     23 	echo "  -h   show this message"
     24 	echo "  -v   show version and license information"
     25 	echo "  -V   show verbose information during execution"
     26 	echo "  --   do not consider any following args as options"
     27 }
     28 
     29 show_version() {
     30 	echo "${0##*/} version $version"
     31 	echo "Licensed under the ISC License"
     32 	echo "written by Anders Damsgaard, anders@adamsgaard.dk"
     33 	echo "https://src.adamsgaard.dk/dotfiles"
     34 }
     35 
     36 if [ $# -lt 1 ]; then
     37 	die 'error: no USER specified'
     38 	exit 1
     39 fi
     40 
     41 verbose=0
     42 while :; do
     43 	case "$1" in
     44 		-h)
     45 			help
     46 			exit 0
     47 			;;
     48 		-v)
     49 			show_version
     50 			exit 0
     51 			;;
     52 		-V)
     53 			verbose=1
     54 			;;
     55 		--)
     56 			shift
     57 			break
     58 			;;
     59 		-?*)
     60 			die 'error: unknown option specified'
     61 			;;
     62 		*)
     63 			break;
     64 	esac
     65 	shift
     66 done
     67 
     68 add_user() {
     69 	if [ "$verbose" = 1 ]; then
     70 		printf 'adding user %s\n' "$1"
     71 		printf 'adding entry to /etc/doas.conf\n'
     72 		printf 'adding ssh key to %s\n' "$1"
     73 		printf 'limiting access to /home/%s\n' "$1"
     74 	fi
     75 	doas sh -c "useradd -s /sbin/nologin -m $1 && \
     76 		echo 'permit nopass keepenv ad as $1' >> /etc/doas.conf && \
     77 		cat $HOME/.ssh/id_rsa.pub >> /home/$1/.ssh/authorized_keys && \
     78 		chmod go-rx /home/$1"
     79 }
     80 
     81 for u in "$@"; do
     82 	add_user "$u"
     83 done