xref: /freebsd/usr.sbin/bsdconfig/usermgmt/usermgmt (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1641a6cfbSDevin Teske#!/bin/sh
2641a6cfbSDevin Teske#-
3641a6cfbSDevin Teske# Copyright (c) 2012 Ron McDowell
4e14ddd1fSDevin Teske# Copyright (c) 2012-2013 Devin Teske
5641a6cfbSDevin Teske# All rights reserved.
6641a6cfbSDevin Teske#
7641a6cfbSDevin Teske# Redistribution and use in source and binary forms, with or without
8641a6cfbSDevin Teske# modification, are permitted provided that the following conditions
9641a6cfbSDevin Teske# are met:
10641a6cfbSDevin Teske# 1. Redistributions of source code must retain the above copyright
11641a6cfbSDevin Teske#    notice, this list of conditions and the following disclaimer.
12641a6cfbSDevin Teske# 2. Redistributions in binary form must reproduce the above copyright
13641a6cfbSDevin Teske#    notice, this list of conditions and the following disclaimer in the
14641a6cfbSDevin Teske#    documentation and/or other materials provided with the distribution.
15641a6cfbSDevin Teske#
16641a6cfbSDevin Teske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17641a6cfbSDevin Teske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18641a6cfbSDevin Teske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19641a6cfbSDevin Teske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20641a6cfbSDevin Teske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21641a6cfbSDevin Teske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22641a6cfbSDevin Teske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23641a6cfbSDevin Teske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24641a6cfbSDevin Teske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25641a6cfbSDevin Teske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26641a6cfbSDevin Teske# SUCH DAMAGE.
27641a6cfbSDevin Teske#
28641a6cfbSDevin Teske#
29641a6cfbSDevin Teske############################################################ INCLUDES
30641a6cfbSDevin Teske
31ab2043b8SDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
32ab2043b8SDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
3356961fd7SDevin Teskef_dprintf "%s: loading includes..." "$0"
34ab2043b8SDevin Teskef_include $BSDCFG_SHARE/dialog.subr
35ab2043b8SDevin Teskef_include $BSDCFG_SHARE/mustberoot.subr
36641a6cfbSDevin Teske
37ab2043b8SDevin TeskeBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="070.usermgmt"
38641a6cfbSDevin Teskef_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
39641a6cfbSDevin Teske
40724b4bfdSDevin TeskeUSERMGMT_HELPFILE=$BSDCFG_LIBE/$APP_DIR/include/usermgmt.hlp
41724b4bfdSDevin Teske
42*d4ae33f0SDevin Teskef_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" ipgm &&
43*d4ae33f0SDevin Teske	pgm="${ipgm:-$pgm}"
44641a6cfbSDevin Teske
45641a6cfbSDevin Teske############################################################ FUNCTIONS
46641a6cfbSDevin Teske
47641a6cfbSDevin Teske# dialog_menu_main
48641a6cfbSDevin Teske#
49641a6cfbSDevin Teske# Display the dialog(1)-based application main menu.
50641a6cfbSDevin Teske#
51641a6cfbSDevin Teskedialog_menu_main()
52641a6cfbSDevin Teske{
53052f8969SDevin Teske	local prompt=
54052f8969SDevin Teske	local menu_list="
55641a6cfbSDevin Teske		'X' '$msg_exit'
56641a6cfbSDevin Teske		'1' '$msg_add_login'
57641a6cfbSDevin Teske		'2' '$msg_edit_login'
58641a6cfbSDevin Teske		'3' '$msg_delete_login'
59641a6cfbSDevin Teske		'-' '-'
60641a6cfbSDevin Teske		'4' '$msg_add_group'
61641a6cfbSDevin Teske		'5' '$msg_edit_group'
62641a6cfbSDevin Teske		'6' '$msg_delete_group'
63641a6cfbSDevin Teske	" # END-QUOTE
64052f8969SDevin Teske	local defaultitem= # Calculated below
65052f8969SDevin Teske	local hline="$hline_arrows_tab_enter"
66641a6cfbSDevin Teske
6774036c4dSDevin Teske	local height width rows
6874036c4dSDevin Teske	eval f_dialog_menu_size height width rows \
69641a6cfbSDevin Teske	                        \"\$DIALOG_TITLE\"     \
70641a6cfbSDevin Teske	                        \"\$DIALOG_BACKTITLE\" \
71052f8969SDevin Teske	                        \"\$prompt\"           \
72641a6cfbSDevin Teske	                        \"\$hline\"            \
7374036c4dSDevin Teske	                        $menu_list
74641a6cfbSDevin Teske
7574036c4dSDevin Teske	# When using Xdialog(1) we need to bump the width for the buttons
7674036c4dSDevin Teske	[ "$USE_XDIALOG" ] && width=40
77724b4bfdSDevin Teske
7877599778SDevin Teske	# Obtain default-item from previously stored selection
7977599778SDevin Teske	f_dialog_default_fetch defaultitem
8077599778SDevin Teske
81fd962ac6SDevin Teske	local menu_choice
82fd962ac6SDevin Teske	menu_choice=$( eval $DIALOG \
83d3a0f918SDevin Teske		--title \"\$DIALOG_TITLE\"  \
84641a6cfbSDevin Teske		--backtitle \"\$DIALOG_BACKTITLE\" \
85641a6cfbSDevin Teske		--hline \"\$hline\"                \
86641a6cfbSDevin Teske		--ok-label \"\$msg_ok\"            \
87641a6cfbSDevin Teske		--cancel-label \"\$msg_cancel\"    \
88724b4bfdSDevin Teske		--help-button                      \
893c91c65aSDevin Teske		--help-label \"\$msg_help\"        \
90724b4bfdSDevin Teske		${USE_XDIALOG:+--help \"\"}        \
9177599778SDevin Teske		--default-item \"\$defaultitem\"   \
92052f8969SDevin Teske		--menu \"\$prompt\"                \
9374036c4dSDevin Teske		$height $width $rows               \
9474036c4dSDevin Teske		$menu_list                         \
9589498fdfSDevin Teske		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
9689498fdfSDevin Teske	)
9789498fdfSDevin Teske	local retval=$?
98fd962ac6SDevin Teske	f_dialog_data_sanitize menu_choice
99fd962ac6SDevin Teske	f_dialog_menutag_store "$menu_choice"
100d3a0f918SDevin Teske
101d3a0f918SDevin Teske	# Only update default-item on success
102f677a9e2SDevin Teske	[ $retval -eq $DIALOG_OK ] && f_dialog_default_store "$menu_choice"
103d3a0f918SDevin Teske
10489498fdfSDevin Teske	return $retval
105641a6cfbSDevin Teske}
106641a6cfbSDevin Teske
107641a6cfbSDevin Teske############################################################ MAIN
108641a6cfbSDevin Teske
109641a6cfbSDevin Teske# Incorporate rc-file if it exists
110641a6cfbSDevin Teske[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
111641a6cfbSDevin Teske
112641a6cfbSDevin Teske#
113641a6cfbSDevin Teske# Process command-line arguments
114641a6cfbSDevin Teske#
115c3755aa3SDevin Teskewhile getopts h$GETOPTS_STDARGS flag; do
116641a6cfbSDevin Teske	case "$flag" in
117641a6cfbSDevin Teske	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm" ;;
118641a6cfbSDevin Teske	esac
119641a6cfbSDevin Teskedone
120641a6cfbSDevin Teskeshift $(( $OPTIND - 1 ))
121641a6cfbSDevin Teske
122641a6cfbSDevin Teske#
123641a6cfbSDevin Teske# Initialize
124641a6cfbSDevin Teske#
125641a6cfbSDevin Teskef_dialog_title "$msg_login_management"
126641a6cfbSDevin Teskef_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
127641a6cfbSDevin Teskef_mustberoot_init
128641a6cfbSDevin Teske
129641a6cfbSDevin Teske#
130641a6cfbSDevin Teske# Launch application main menu
131641a6cfbSDevin Teske#
132641a6cfbSDevin Teskewhile :; do
133641a6cfbSDevin Teske	dialog_menu_main
134641a6cfbSDevin Teske	retval=$?
135fd962ac6SDevin Teske	f_dialog_menutag_fetch mtag
136526e1dc1SDevin Teske	f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
137641a6cfbSDevin Teske
138f677a9e2SDevin Teske	if [ $retval -eq $DIALOG_HELP ]; then
139724b4bfdSDevin Teske		f_show_help "$USERMGMT_HELPFILE"
140724b4bfdSDevin Teske		continue
141f677a9e2SDevin Teske	elif [ $retval -ne $DIALOG_OK ]; then
142724b4bfdSDevin Teske		f_die
143724b4bfdSDevin Teske	fi
144641a6cfbSDevin Teske
145df750a6cSDevin Teske	command=
146641a6cfbSDevin Teske	case "$mtag" in
147df750a6cSDevin Teske	X) break ;;
148df750a6cSDevin Teske	1) command=useradd   ;; # Add User
149df750a6cSDevin Teske	2) command=useredit  ;; # Edit/View User
150df750a6cSDevin Teske	3) command=userdel   ;; # Delete User
151df750a6cSDevin Teske	4) command=groupadd  ;; # Add Group
152df750a6cSDevin Teske	5) command=groupedit ;; # Edit/View Group
153df750a6cSDevin Teske	6) command=groupdel  ;; # Delete Group
154641a6cfbSDevin Teske	esac
155df750a6cSDevin Teske
156df750a6cSDevin Teske	if [ "$command" ]; then
157df750a6cSDevin Teske		$BSDCFG_LIBE/$APP_DIR/$command ${USE_XDIALOG:+-X}
158df750a6cSDevin Teske	else
159df750a6cSDevin Teske		f_die 1 "$msg_unknown_user_management_menu_selection"
160df750a6cSDevin Teske	fi
161641a6cfbSDevin Teskedone
162641a6cfbSDevin Teske
163df750a6cSDevin Teskeexit $SUCCESS
164df750a6cSDevin Teske
165641a6cfbSDevin Teske################################################################################
166641a6cfbSDevin Teske# END
167641a6cfbSDevin Teske################################################################################
168