group_input.subr revision 251264
1if [ ! "$_USERMGMT_GROUP_INPUT_SUBR" ]; then _USERMGMT_GROUP_INPUT_SUBR=1
2#
3# Copyright (c) 2012 Ron McDowell
4# Copyright (c) 2012-2013 Devin Teske
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28# $FreeBSD: head/usr.sbin/bsdconfig/usermgmt/share/group_input.subr 251264 2013-06-02 20:02:50Z dteske $
29#
30############################################################ INCLUDES
31
32BSDCFG_SHARE="/usr/share/bsdconfig"
33. $BSDCFG_SHARE/common.subr || exit 1
34f_dprintf "%s: loading includes..." usermgmt/group_input.subr
35f_include $BSDCFG_SHARE/dialog.subr
36f_include $BSDCFG_SHARE/strings.subr
37
38BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="070.usermgmt"
39f_include_lang $BSDCFG_LIBE/include/messages.subr
40f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
41
42############################################################ FUNCTIONS
43
44# f_input_group $group
45#
46# Given $group name or id, create the environment variables group_name,
47# group_gid, and group_members (and group_password is reset to NULL).
48#
49f_input_group()
50{
51	eval $( pw groupshow "$1" | awk -F: '
52	{
53		printf "group_name='\'%s\''\n", $1
54		printf "group_password=\n"
55		printf "group_gid='\'%s\''\n", $3
56		printf "group_members='\'%s\''\n", $4
57		exit
58	}' )
59}
60
61# f_dialog_menu_group_list [$default]
62#
63# Allows the user to select a group from a list. Optionally, if present and
64# non-NULL, initially highlight $default group.
65#
66f_dialog_menu_group_list()
67{
68	local prompt=
69	local menu_list="
70		'X $msg_exit' ''
71	" # END-QUOTE
72	local defaultitem="$1"
73	local hline="$hline_alnum_punc_tab_enter"
74
75	# Add groups from group(5)
76	menu_list="$menu_list $( pw groupshow -a | awk -F: '
77		!/^[[:space:]]*(#|$)/ {
78			printf "'\'%s\'\ \'%s\''\n", $1, $1
79		}'
80	)"
81
82	local height width rows
83	eval f_dialog_menu_size height width rows \
84	                        \"\$DIALOG_TITLE\"     \
85	                        \"\$DIALOG_BACKTITLE\" \
86	                        \"\$prompt\"           \
87	                        \"\$hline\"            \
88	                        $menu_list
89
90	local menu_choice
91	menu_choice=$( eval $DIALOG \
92		--title \"\$DIALOG_TITLE\"         \
93		--backtitle \"\$DIALOG_BACKTITLE\" \
94		--hline \"\$hline\"                \
95		--ok-label \"\$msg_ok\"            \
96		--cancel-label \"\$msg_cancel\"    \
97		--default-item \"\$defaultitem\"   \
98		--menu \"\$prompt\"                \
99		$height $width $rows               \
100		$menu_list                         \
101		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
102	)
103	local retval=$?
104	f_dialog_menutag_store -s "$menu_choice"
105	return $retval
106}
107
108# f_dialog_input_group_name [$group_name]
109#
110# Allows the user to enter a new groupname for a given group. If the user does
111# not cancel or press ESC, the $group_name variable will hold the
112# newly-configured value upon return.
113#
114# If $cur_group_name is defined, the user can enter that and by-pass error-
115# checking (allowing the user to "revert" to an old value without, for example,
116# being told that the groupname already exists).
117#
118f_dialog_input_group_name()
119{
120	#
121	# Loop until the user provides taint-free/valid input
122	#
123	local _name="$1" _input="$1"
124	while :; do
125
126		# Return if user has either pressed ESC or chosen Cancel/No
127		f_dialog_input _input "$msg_group" "$_input" \
128		               "$hline_alnum_tab_enter" || return
129
130		# Check for no-change
131		[ "$_input" = "$_name" ] && return $SUCCESS
132
133		# Check for reversion
134		if [ "$_input" = "$cur_group_name" ]; then
135			group_name="$cur_group_name"
136			return $SUCCESS
137		fi
138
139		# Check for NULL entry
140		if [ ! "$_input" ]; then
141			f_dialog_msgbox "$msg_group_is_empty"
142			continue
143		fi
144
145		# Check for invalid entry
146		if ! echo "$_input" | grep -q "^[[:alpha:]]"; then
147			f_dialog_msgbox "$msg_group_must_start_with_letter"
148			continue
149		fi
150
151		# Check for duplicate entry
152		if f_quietly pw groupshow -n "$_input"; then
153			f_show_msg "$msg_group_already_used" "$_input"
154			continue
155		fi
156
157		group_name="$_input"
158		break
159	done
160	save_flag=1
161
162	f_dprintf "group_name: [%s]->[%s]" "$cur_group_name" "$group_name"
163
164	return $SUCCESS
165}
166
167# f_dialog_input_group_password
168#
169# Prompt the user to enter a password (twice).
170#
171f_dialog_input_group_password()
172{
173	local prompt1="$msg_group_password"
174	local prompt2="$msg_reenter_group_password"
175	local hline="$hline_alnum_punc_tab_enter"
176
177	local height1 width1
178	f_dialog_inputbox_size height1 width1 \
179	        	"$DIALOG_TITLE"     \
180	        	"$DIALOG_BACKTITLE" \
181	        	"$prompt1"          \
182	        	""                  \
183	        	"$hline"
184
185	local height2 width2
186	f_dialog_inputbox_size height2 width2 \
187	        	"$DIALOG_TITLE"     \
188	        	"$DIALOG_BACKTITLE" \
189	        	"$prompt2"          \
190	        	""                  \
191	        	"$hline"
192
193	#
194	# Loop until the user provides taint-free/valid input
195	#
196	local retval _password1 _password2
197	while :; do
198		_password1=$( $DIALOG \
199			--title "$DIALOG_TITLE"         \
200			--backtitle "$DIALOG_BACKTITLE" \
201			--hline "$hline"                \
202			--ok-label "$msg_ok"            \
203			--cancel-label "$msg_cancel"    \
204			--insecure                      \
205			--passwordbox "$prompt1"        \
206			$height1 $width1                \
207			2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
208		)
209		retval=$?
210		debug= f_dialog_line_sanitize _password1
211
212		# Return if user has either pressed ESC or chosen Cancel/No
213		[ $retval -eq $SUCCESS ] || return $retval
214
215		_password2=$( $DIALOG \
216			--title "$DIALOG_TITLE"         \
217			--backtitle "$DIALOG_BACKTITLE" \
218			--hline "$hline"                \
219			--ok-label "$msg_ok"            \
220			--cancel-label "$msg_cancel"    \
221			--insecure                      \
222			--passwordbox "$prompt2"        \
223			$height2 $width2                \
224			2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
225		)
226		retval=$?
227		debug= f_dialog_line_sanitize _password2
228
229		# Return if user has either pressed ESC or chosen Cancel/No
230		[ $retval -eq $SUCCESS ] || return $retval
231
232		# Check for password mismatch
233		if [ "$_password1" != "$_password2" ]; then
234			f_dialog_msgbox "$msg_group_passwords_do_not_match"
235			continue
236		fi
237
238		# Check for NULL entry
239		if [ ! "$_password1" ]; then
240			f_dialog_yesno \
241				"$msg_disable_password_auth_for_group" ||
242				continue
243			pw_group_password_disable=1
244		else
245			pw_group_password_disable=
246		fi
247
248		group_password="$_password1"
249		break
250	done
251	save_flag=1
252
253	f_dprintf "group_password: [%s]->[%s]" \
254	          "$cur_group_password" "$group_password"
255
256	return $SUCCESS
257}
258
259# f_dialog_input_group_gid [$group_gid]
260#
261# Allow the user to enter a new GID for a given group. If the user does not
262# cancel or press ESC, the $group_gid variable will hold the newly-configured
263# value upon return.
264#
265f_dialog_input_group_gid()
266{
267	local _input="$1"
268
269	# Return if user has either pressed ESC or chosen Cancel/No
270	f_dialog_input _input "$msg_group_id_leave_empty_for_default" \
271	               "$_input" "$hline_num_tab_enter" || return
272
273	group_gid="$_input"
274	save_flag=1
275
276	f_dprintf "group_gid: [%s]->[%s]" "$cur_group_gid" "$group_gid"
277
278	return $SUCCESS
279}
280
281# f_dialog_input_group_members [$group_members]
282#
283# Allow the user to modify a list of members for a given group. If the user does
284# not cancel or press ESC, the $group_members variable will hold the newly-
285# configured value upon return.
286#
287f_dialog_input_group_members()
288{
289	local _input="$1"
290	local prompt="$msg_group_members:"
291	local menu_list="
292		'X' '$msg_continue'
293		'1' '$msg_select_group_members_from_list'
294		'2' '$msg_enter_group_members_manually'
295	" # END-QUOTE
296	local defaultitem=
297	local hline="$hline_num_arrows_tab_enter"
298
299	local mheight mwidth mrows
300	eval f_dialog_menu_size mheight mwidth mrows \
301	                        \"\$DIALOG_TITLE\"     \
302	                        \"\$DIALOG_BACKTITLE\" \
303	                        \"\$prompt\"           \
304	                        \"\$hline\"            \
305	                        $menu_list
306
307	local menu_choice retval
308	while :; do
309		menu_choice=$( eval $DIALOG \
310			--title \"\$DIALOG_TITLE\"         \
311			--backtitle \"\$DIALOG_BACKTITLE\" \
312			--hline \"\$hline\"                \
313			--ok-label \"\$msg_ok\"            \
314			--cancel-label \"\$msg_cancel\"    \
315			--default-item \"\$defaultitem\"   \
316			--menu \"\$prompt\"                \
317			$mheight $mwidth $mrows            \
318			$menu_list                         \
319			2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
320		)
321		retval=$?
322		f_dialog_data_sanitize menu_choice
323		defaultitem="$menu_choice"
324		f_dprintf "retval=%u menu_choice=[%s]" $retval "$menu_choice"
325
326		# Return if user has either pressed ESC or chosen Cancel/No
327		[ $retval -eq $SUCCESS ] || return $retval
328
329		local _group_members
330		case "$menu_choice" in
331		X) # Exit
332			break ;;
333		1) # Select Group Members from a list
334			local user check_list=
335			for user in $( pw usershow -a |
336				awk -F: '!/^[[:space:]]*(#|$)/{print $1}'
337			); do
338				# Format of a checklist entry: tag item status
339				if echo "$_input" | grep -q "\<$user\>"; then
340					check_list="$check_list $user '' on"
341				else
342					check_list="$check_list $user '' off"
343				fi
344			done
345
346			local cheight cwidth crows
347			eval f_dialog_checklist_size cheight cwidth crows \
348			                             \"\$DIALOG_TITLE\"     \
349			                             \"\$DIALOG_BACKTITLE\" \
350			                             \"\$prompt\"           \
351			                             \"\$hline\"            \
352			                             $check_list
353			_group_members=$( eval $DIALOG \
354				--title \"\$DIALOG_TITLE\"         \
355				--backtitle \"\$DIALOG_BACKTITLE\" \
356				--separate-output                  \
357				--hline \"\$hline\"                \
358				--ok-label \"\$msg_ok\"            \
359				--cancel-label \"\$msg_cancel\"    \
360				--checklist \"\$prompt\"           \
361				$cheight $cwidth $crows            \
362				$check_list                        \
363				2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
364			) || continue
365				# Return to previous menu if user either
366				# pressed ESC or chose Cancel/No
367			f_dialog_data_sanitize _group_members
368
369			# Convert the newline separated list into a comma-
370			# separated one so that if the user switches over to
371			# manual editing, list reflects checklist selections
372			_group_members=$( echo "$_group_members" |
373				tr '\n' ' ' |
374				sed -e 's/[[:space:]]\{1,\}/,/g;s/^,//;s/,$//'
375			)
376
377			_input="$_group_members"
378			;;
379		2) # Enter Group Members manually
380			local p="$msg_group_members ($msg_separated_by_commas)"
381
382			f_dialog_input _group_members "$p" "$_input" \
383			               "$hline_num_tab_enter" || continue
384				# Return to previous menu if user either
385				# pressed ESC or chose Cancel/No
386
387			_input="$_group_members"
388			;;
389		esac
390	done
391
392	group_members="$_input"
393	save_flag=1
394	f_dprintf "group_members: [%s]->[%s]" \
395	          "$cur_group_members" "$group_members"
396
397	return $SUCCESS
398}
399
400############################################################ MAIN
401
402f_dprintf "%s: Successfully loaded." usermgmt/group_input.subr
403
404fi # ! $_USERMGMT_GROUP_INPUT_SUBR
405