1 if [ ! "$_USERMGMT_GROUP_INPUT_SUBR" ]; then _USERMGMT_GROUP_INPUT_SUBR=1
3 # Copyright (c) 2012 Ron McDowell
4 # Copyright (c) 2012-2013 Devin Teske
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
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.
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
30 ############################################################ INCLUDES
32 BSDCFG_SHARE="/usr/share/bsdconfig"
33 . $BSDCFG_SHARE/common.subr || exit 1
34 f_dprintf "%s: loading includes..." usermgmt/group_input.subr
35 f_include $BSDCFG_SHARE/dialog.subr
36 f_include $BSDCFG_SHARE/strings.subr
38 BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="070.usermgmt"
39 f_include_lang $BSDCFG_LIBE/include/messages.subr
40 f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
42 ############################################################ FUNCTIONS
44 # f_input_group $group
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).
51 eval $( pw groupshow "$1" | awk -F: '
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
61 # f_dialog_menu_group_list [$default]
63 # Allows the user to select a group from a list. Optionally, if present and
64 # non-NULL, initially highlight $default group.
66 f_dialog_menu_group_list()
72 local defaultitem="$1"
73 local hline="$hline_alnum_punc_tab_enter"
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
82 local height width rows
83 eval f_dialog_menu_size height width rows \
85 \"\$DIALOG_BACKTITLE\" \
91 menu_choice=$( eval $DIALOG \
92 --title \"\$DIALOG_TITLE\" \
93 --backtitle \"\$DIALOG_BACKTITLE\" \
95 --ok-label \"\$msg_ok\" \
96 --cancel-label \"\$msg_cancel\" \
97 --default-item \"\$defaultitem\" \
99 $height $width $rows \
101 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
104 f_dialog_menutag_store -s "$menu_choice"
108 # f_dialog_input_group_name [$group_name]
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.
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).
118 f_dialog_input_group_name()
121 # Loop until the user provides taint-free/valid input
123 local _name="$1" _input="$1"
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
130 # Check for no-change
131 [ "$_input" = "$_name" ] && return $SUCCESS
133 # Check for reversion
134 if [ "$_input" = "$cur_group_name" ]; then
135 group_name="$cur_group_name"
139 # Check for NULL entry
140 if [ ! "$_input" ]; then
141 f_show_msg "$msg_group_is_empty"
145 # Check for invalid entry
146 if ! echo "$_input" | grep -q "^[[:alpha:]]"; then
147 f_show_msg "$msg_group_must_start_with_letter"
151 # Check for duplicate entry
152 if f_quietly pw groupshow -n "$_input"; then
153 f_show_msg "$msg_group_already_used" "$_input"
162 f_dprintf "group_name: [%s]->[%s]" "$cur_group_name" "$group_name"
167 # f_dialog_input_group_password
169 # Prompt the user to enter a password (twice).
171 f_dialog_input_group_password()
173 local prompt1="$msg_group_password"
174 local prompt2="$msg_reenter_group_password"
175 local hline="$hline_alnum_punc_tab_enter"
178 f_dialog_inputbox_size height1 width1 \
180 "$DIALOG_BACKTITLE" \
186 f_dialog_inputbox_size height2 width2 \
188 "$DIALOG_BACKTITLE" \
194 # Loop until the user provides taint-free/valid input
196 local retval _password1 _password2
198 _password1=$( $DIALOG \
199 --title "$DIALOG_TITLE" \
200 --backtitle "$DIALOG_BACKTITLE" \
202 --ok-label "$msg_ok" \
203 --cancel-label "$msg_cancel" \
205 --passwordbox "$prompt1" \
207 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
210 debug= f_dialog_line_sanitize _password1
212 # Return if user has either pressed ESC or chosen Cancel/No
213 [ $retval -eq $SUCCESS ] || return $retval
215 _password2=$( $DIALOG \
216 --title "$DIALOG_TITLE" \
217 --backtitle "$DIALOG_BACKTITLE" \
219 --ok-label "$msg_ok" \
220 --cancel-label "$msg_cancel" \
222 --passwordbox "$prompt2" \
224 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
227 debug= f_dialog_line_sanitize _password2
229 # Return if user has either pressed ESC or chosen Cancel/No
230 [ $retval -eq $SUCCESS ] || return $retval
232 # Check for password mismatch
233 if [ "$_password1" != "$_password2" ]; then
234 f_show_msg "$msg_group_passwords_do_not_match"
238 # Check for NULL entry
239 if [ ! "$_password1" ]; then
240 f_dialog_yesno "$msg_disable_password_auth_for_group"
242 if [ $retval -eq 255 ]; then # ESC was pressed
244 elif [ $retval -eq $SUCCESS ]; then
245 pw_group_password_disable=1
247 continue # back to password prompt
250 pw_group_password_disable=
253 group_password="$_password1"
258 f_dprintf "group_password: [%s]->[%s]" \
259 "$cur_group_password" "$group_password"
264 # f_dialog_input_group_gid [$group_gid]
266 # Allow the user to enter a new GID for a given group. If the user does not
267 # cancel or press ESC, the $group_gid variable will hold the newly-configured
270 f_dialog_input_group_gid()
274 # Return if user has either pressed ESC or chosen Cancel/No
275 f_dialog_input _input "$msg_group_id_leave_empty_for_default" \
276 "$_input" "$hline_num_tab_enter" || return
281 f_dprintf "group_gid: [%s]->[%s]" "$cur_group_gid" "$group_gid"
286 # f_dialog_input_group_members [$group_members]
288 # Allow the user to modify a list of members for a given group. If the user
289 # does not cancel or press ESC, the $group_members variable will hold the
290 # newly-configured value upon return.
292 f_dialog_input_group_members()
295 local prompt="$msg_group_members:"
298 '1' '$msg_select_group_members_from_list'
299 '2' '$msg_enter_group_members_manually'
302 local hline="$hline_num_arrows_tab_enter"
304 local mheight mwidth mrows
305 eval f_dialog_menu_size mheight mwidth mrows \
307 \"\$DIALOG_BACKTITLE\" \
312 local menu_choice retval
314 menu_choice=$( eval $DIALOG \
315 --title \"\$DIALOG_TITLE\" \
316 --backtitle \"\$DIALOG_BACKTITLE\" \
317 --hline \"\$hline\" \
318 --ok-label \"\$msg_ok\" \
319 --cancel-label \"\$msg_cancel\" \
320 --default-item \"\$defaultitem\" \
321 --menu \"\$prompt\" \
322 $mheight $mwidth $mrows \
324 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
327 f_dialog_data_sanitize menu_choice
328 defaultitem="$menu_choice"
329 f_dprintf "retval=%u menu_choice=[%s]" $retval "$menu_choice"
331 # Return if user has either pressed ESC or chosen Cancel/No
332 [ $retval -eq $SUCCESS ] || return $retval
335 case "$menu_choice" in
338 1) # Select Group Members from a list
339 local user check_list=
340 for user in $( pw usershow -a |
341 awk -F: '!/^[[:space:]]*(#|$)/{print $1}'
343 # Format of a checklist entry: tag item status
344 if echo "$_input" | grep -q "\<$user\>"; then
345 check_list="$check_list $user '' on"
347 check_list="$check_list $user '' off"
351 local cheight cwidth crows
352 eval f_dialog_checklist_size cheight cwidth crows \
354 \"\$DIALOG_BACKTITLE\" \
358 _group_members=$( eval $DIALOG \
359 --title \"\$DIALOG_TITLE\" \
360 --backtitle \"\$DIALOG_BACKTITLE\" \
362 --hline \"\$hline\" \
363 --ok-label \"\$msg_ok\" \
364 --cancel-label \"\$msg_cancel\" \
365 --checklist \"\$prompt\" \
366 $cheight $cwidth $crows \
368 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
370 # Return to previous menu if user either
371 # pressed ESC or chose Cancel/No
372 f_dialog_data_sanitize _group_members
374 # Convert the newline separated list into a comma-
375 # separated one so that if the user switches over to
376 # manual editing, list reflects checklist selections
377 _group_members=$( echo "$_group_members" |
379 sed -e 's/[[:space:]]\{1,\}/,/g;s/^,//;s/,$//'
382 _input="$_group_members"
384 2) # Enter Group Members manually
385 local p="$msg_group_members ($msg_separated_by_commas)"
387 f_dialog_input _group_members "$p" "$_input" \
388 "$hline_num_tab_enter" || continue
389 # Return to previous menu if user either
390 # pressed ESC or chose Cancel/No
392 _input="$_group_members"
397 group_members="$_input"
399 f_dprintf "group_members: [%s]->[%s]" \
400 "$cur_group_members" "$group_members"
405 ############################################################ MAIN
407 f_dprintf "%s: Successfully loaded." usermgmt/group_input.subr
409 fi # ! $_USERMGMT_GROUP_INPUT_SUBR