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$ 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 defaultitem="$1" 69 local menu_list 70 local hline="$hline_alnum_punc_tab_enter" 71 72 menu_list=" 73 'X $msg_exit' '' 74 " # END-QUOTE 75 76 # Add groups from group(5) 77 menu_list="$menu_list $( pw groupshow -a | awk -F: ' 78 !/^[[:space:]]*(#|$)/ { 79 printf "'\'%s\'\ \'%s\''\n", $1, $1 80 }' 81 )" 82 83 local height width rows 84 eval f_dialog_menu_size height width rows \ 85 \"\$DIALOG_TITLE\" \ 86 \"\$DIALOG_BACKTITLE\" \ 87 \"\" \ 88 \"\$hline\" \ 89 $menu_list 90 91 local menu_choice 92 menu_choice=$( eval $DIALOG \ 93 --title \"\$DIALOG_TITLE\" \ 94 --backtitle \"\$DIALOG_BACKTITLE\" \ 95 --hline \"\$hline\" \ 96 --ok-label \"\$msg_ok\" \ 97 --cancel-label \"\$msg_cancel\" \ 98 --default-item \"\$defaultitem\" \ 99 --menu \"\" \ 100 $height $width $rows \ 101 $menu_list \ 102 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 103 ) 104 local retval=$? 105 f_dialog_menutag_store -s "$menu_choice" 106 return $retval 107} 108 109# f_dialog_input_group_name [$group_name] 110# 111# Allows the user to enter a new groupname for a given group. If the user does 112# not cancel or press ESC, the $group_name variable will hold the 113# newly-configured value upon return. 114# 115# If $cur_group_name is defined, the user can enter that and by-pass error- 116# checking (allowing the user to "revert" to an old value without, for example, 117# being told that the groupname already exists). 118# 119f_dialog_input_group_name() 120{ 121 # 122 # Loop until the user provides taint-free/valid input 123 # 124 local _name="$1" _input="$1" 125 while :; do 126 127 # Return if user has either pressed ESC or chosen Cancel/No 128 f_dialog_input _input "$msg_group" "$_input" \ 129 "$hline_alnum_tab_enter" || return 130 131 # Check for no-change 132 [ "$_input" = "$_name" ] && return $SUCCESS 133 134 # Check for reversion 135 if [ "$_input" = "$cur_group_name" ]; then 136 group_name="$cur_group_name" 137 return $SUCCESS 138 fi 139 140 # Check for NULL entry 141 if [ ! "$_input" ]; then 142 f_dialog_msgbox "$msg_group_is_empty" 143 continue 144 fi 145 146 # Check for invalid entry 147 if ! echo "$_input" | grep -q "^[[:alpha:]]"; then 148 f_dialog_msgbox "$msg_group_must_start_with_letter" 149 continue 150 fi 151 152 # Check for duplicate entry 153 if f_quietly pw groupshow -n "$_input"; then 154 f_show_msg "$msg_group_already_used" "$_input" 155 continue 156 fi 157 158 group_name="$_input" 159 break 160 done 161 save_flag=1 162 163 f_dprintf "group_name: [%s]->[%s]" "$cur_group_name" "$group_name" 164 165 return $SUCCESS 166} 167 168# f_dialog_input_group_password 169# 170# Prompt the user to enter a password (twice). 171# 172f_dialog_input_group_password() 173{ 174 local hline="$hline_alnum_punc_tab_enter" 175 local msg rmsg 176 177 msg=$( printf "$msg_group_password" ) 178 local height1 width1 179 f_dialog_inputbox_size height1 width1 \ 180 "$DIALOG_TITLE" \ 181 "$DIALOG_BACKTITLE" \ 182 "$msg" \ 183 "" \ 184 "$hline" 185 186 rmsg=$( printf "$msg_reenter_group_password" ) 187 local height2 width2 188 f_dialog_inputbox_size height2 width2 \ 189 "$DIALOG_TITLE" \ 190 "$DIALOG_BACKTITLE" \ 191 "$rmsg" \ 192 "" \ 193 "$hline" 194 195 # 196 # Loop until the user provides taint-free/valid input 197 # 198 local retval _password1 _password2 199 while :; do 200 _password1=$( $DIALOG \ 201 --title "$DIALOG_TITLE" \ 202 --backtitle "$DIALOG_BACKTITLE" \ 203 --hline "$hline" \ 204 --ok-label "$msg_ok" \ 205 --cancel-label "$msg_cancel" \ 206 --insecure \ 207 --passwordbox "$msg" \ 208 $height1 $width1 \ 209 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 210 ) 211 retval=$? 212 debug= f_dialog_line_sanitize _password1 213 214 # Return if user has either pressed ESC or chosen Cancel/No 215 [ $retval -eq $SUCCESS ] || return $retval 216 217 _password2=$( $DIALOG \ 218 --title "$DIALOG_TITLE" \ 219 --backtitle "$DIALOG_BACKTITLE" \ 220 --hline "$hline" \ 221 --ok-label "$msg_ok" \ 222 --cancel-label "$msg_cancel" \ 223 --insecure \ 224 --passwordbox "$rmsg" \ 225 $height2 $width2 \ 226 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 227 ) 228 retval=$? 229 debug= f_dialog_line_sanitize _password2 230 231 # Return if user has either pressed ESC or chosen Cancel/No 232 [ $retval -eq $SUCCESS ] || return $retval 233 234 # Check for password mismatch 235 if [ "$_password1" != "$_password2" ]; then 236 f_dialog_msgbox "$msg_group_passwords_do_not_match" 237 continue 238 fi 239 240 # Check for NULL entry 241 if [ ! "$_password1" ]; then 242 f_dialog_yesno \ 243 "$msg_disable_password_auth_for_group" || 244 continue 245 pw_group_password_disable=1 246 else 247 pw_group_password_disable= 248 fi 249 250 group_password="$_password1" 251 break 252 done 253 save_flag=1 254 255 f_dprintf "group_password: [%s]->[%s]" \ 256 "$cur_group_password" "$group_password" 257 258 return $SUCCESS 259} 260 261# f_dialog_input_group_gid [$group_gid] 262# 263# Allow the user to enter a new GID for a given group. If the user does not 264# cancel or press ESC, the $group_gid variable will hold the newly-configured 265# value upon return. 266# 267f_dialog_input_group_gid() 268{ 269 local _input="$1" 270 271 # Return if user has either pressed ESC or chosen Cancel/No 272 f_dialog_input _input "$msg_group_id_leave_empty_for_default" \ 273 "$_input" "$hline_num_tab_enter" || return 274 275 group_gid="$_input" 276 save_flag=1 277 278 f_dprintf "group_gid: [%s]->[%s]" "$cur_group_gid" "$group_gid" 279 280 return $SUCCESS 281} 282 283# f_dialog_input_group_members [$group_members] 284# 285# Allow the user to modify a list of members for a given group. If the user does 286# not cancel or press ESC, the $group_members variable will hold the newly- 287# configured value upon return. 288# 289f_dialog_input_group_members() 290{ 291 local menu_choice retval _input="$1" 292 local msg="$msg_group_members:" 293 local hline="$hline_num_arrows_tab_enter" 294 local user 295 local menu_list 296 local all_users_valid 297 local _group_members 298 local checklist_users 299 300 menu_list=" 301 'X' '$msg_continue' 302 '1' '$msg_select_group_members_from_list' 303 '2' '$msg_enter_group_members_manually' 304 " # END-QUOTE 305 306 local menu_choice defaultitem= 307 local mheight mwidth mrows 308 eval f_dialog_menu_size mheight mwidth mrows \ 309 \"\$DIALOG_TITLE\" \ 310 \"\$DIALOG_BACKTITLE\" \ 311 \"\$msg\" \ 312 \"\$hline\" \ 313 $menu_list 314 315 while :; do 316 menu_choice=$( eval $DIALOG \ 317 --title \"\$DIALOG_TITLE\" \ 318 --backtitle \"\$DIALOG_BACKTITLE\" \ 319 --hline \"\$hline\" \ 320 --ok-label \"\$msg_ok\" \ 321 --cancel-label \"\$msg_cancel\" \ 322 --default-item \"\$defaultitem\" \ 323 --menu \"\$msg\" \ 324 $mheight $mwidth $mrows \ 325 $menu_list \ 326 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 327 ) 328 retval=$? 329 f_dialog_data_sanitize menu_choice 330 defaultitem="$menu_choice" 331 f_dprintf "retval=%u menu_choice=[%s]" $retval "$menu_choice" 332 333 # Return if user has either pressed ESC or chosen Cancel/No 334 [ $retval -eq $SUCCESS ] || return $retval 335 336 case "$menu_choice" in 337 X) # Exit 338 break ;; 339 1) # Select Group Members from a list 340 user_list=$( pw usershow -a | awk -F: ' 341 !/^[[:space:]]*(#|$)/ { printf "%s\n", $1 }' ) 342 checklist_users= 343 for user in $user_list; do 344 checklist_users="$checklist_users $user \"\"" 345 if echo "$_input" | grep -q "\<$user\>"; then 346 checklist_users="$checklist_users on" 347 else 348 checklist_users="$checklist_users off" 349 fi 350 done 351 352 local cheight cwidth crows 353 eval f_dialog_checklist_size cheight cwidth crows \ 354 \"\$DIALOG_TITLE\" \ 355 \"\$DIALOG_BACKTITLE\" \ 356 \"\" \ 357 \"\$hline\" \ 358 $checklist_users 359 _group_members=$( eval $DIALOG \ 360 --title \"\$DIALOG_TITLE\" \ 361 --backtitle \"\$DIALOG_BACKTITLE\" \ 362 --separate-output \ 363 --hline \"\$hline\" \ 364 --ok-label \"\$msg_ok\" \ 365 --cancel-label \"\$msg_cancel\" \ 366 --checklist \"\$msg\" \ 367 $cheight $cwidth $crows \ 368 $checklist_users \ 369 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 370 ) || continue 371 # Return to previous menu if user either 372 # pressed ESC or chose Cancel/No 373 f_dialog_data_sanitize _group_members 374 375 # Convert the newline separated list into a comma- 376 # separated one so that if the user switches over to 377 # manual editing, list reflects checklist selections 378 _group_members=$( echo "$_group_members" | 379 tr '\n' ' ' | 380 sed -e 's/[[:space:]]\{1,\}/,/g;s/^,//;s/,$//' 381 ) 382 383 _input="$_group_members" 384 ;; 385 2) # Enter Group Members manually 386 msg="$msg_group_members ($msg_separated_by_commas)" 387 388 f_dialog_input _group_members "$msg" "$_input" \ 389 "$hline_num_tab_enter" || continue 390 # Return to previous menu if user either 391 # pressed ESC or chose Cancel/No 392 393 _input="$_group_members" 394 ;; 395 esac 396 done 397 398 group_members="$_input" 399 save_flag=1 400 f_dprintf "group_members: [%s]->[%s]" \ 401 "$cur_group_members" "$group_members" 402 403 return $SUCCESS 404} 405 406############################################################ MAIN 407 408f_dprintf "%s: Successfully loaded." usermgmt/group_input.subr 409 410fi # ! $_USERMGMT_GROUP_INPUT_SUBR 411