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..." "$0"
35 f_include $BSDCFG_SHARE/dialog.subr
36 f_include $BSDCFG_SHARE/mustberoot.subr
37 f_include $BSDCFG_SHARE/usermgmt/group_input.subr
39 BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="070.usermgmt"
40 f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
42 ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
43 [ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
45 ############################################################ CONFIGURATION
47 # set some reasonable defaults if /etc/adduser.conf does not exist.
48 [ -f /etc/adduser.conf ] && f_include /etc/adduser.conf
49 : ${passwdtype:="yes"}
51 ############################################################ FUNCTIONS
55 # Save any/all settings (actions performed depend on $mode value).
59 local err retval=$SUCCESS
63 err=$( pw groupdel "$group_name" 2>&1 )
65 if [ $retval -ne $SUCCESS ]; then
66 f_dialog_msgbox "$msg_error $err\n"
69 f_show_msg "$msg_group_deleted"
72 local cmd="pw groupadd -n '$group_name'"
73 [ "$group_gid" ] && cmd="$cmd -g '$group_gid'"
74 [ "$group_members" != "$cur_group_members" ] &&
75 cmd="$cmd -M '$group_members'"
76 if [ "$pw_group_password_disable" ]; then
78 elif [ "$group_password" ]; then
79 cmd="echo \"\$group_password\" | $cmd -h 0"
81 f_dprintf "cmd=%s" "$cmd"
82 err=$( eval $cmd 2>&1 )
84 if [ $retval -ne $SUCCESS ]; then
85 f_dialog_msgbox "$msg_error $err\n"
88 f_show_msg "$msg_group_added"
91 local cmd="pw groupmod -n '$group_name'"
92 [ "$group_gid" ] && cmd="$cmd -g '$group_gid'"
93 [ "$group_members" != "$cur_group_members" ] &&
94 cmd="$cmd -M '$group_members'"
95 if [ "$pw_group_password_disable" ]; then
97 elif [ "$group_password" ]; then
98 cmd="echo \"\$group_password\" | $cmd -h 0"
100 f_dprintf "cmd=%s" "$cmd"
101 err=$( eval $cmd 2>&1 )
103 if [ $retval -ne $SUCCESS ]; then
104 f_dialog_msgbox "$msg_error $err\n"
107 f_show_msg "$msg_group_updated"
115 # dialog_title_update $mode
117 # Set the title based on the given $mode.
119 dialog_title_update()
123 Add) f_dialog_title "$msg_add $msg_group" ;;
124 Edit/View) f_dialog_title "$msg_edit_view $msg_group: $group" ;;
125 Delete) f_dialog_title "$msg_delete $msg_group: $group" ;;
129 ############################################################ MAIN
131 # Incorporate rc-file if it exists
132 [ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
135 # Process command-line arguments
137 while [ $# -gt 0 ]; do
140 f_dprintf "key=[%s] value=[%s]" "$key" "$value"
142 mode) mode="$value" ;;
143 group) group="$value" ;;
147 f_dprintf "mode=[%s] group=[%s]" "$mode" "$group"
152 dialog_title_update "$mode"
153 f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
157 hline="$hline_arrows_tab_enter"
159 if [ "$mode" = "Add" ]; then
160 f_dialog_input_group_name || exit 0
162 f_dialog_noyes "$msg_use_default_values_for_all_account_details"
165 if [ $retval -eq 255 ]; then # User pressed ESC
167 elif [ $retval -ne $SUCCESS ]; then
169 # Ask a series of questions to pre-fill the editor screen.
171 # The defaults used in each dialog should allow the user to
172 # simply hit ENTER to proceed, because cancelling a single
173 # dialog will cause them to be returned to the main groupmenu.
176 [ "$passwdtype" = "yes" ] &&
177 { f_dialog_input_group_password || exit 0; }
178 f_dialog_input_group_gid || exit 0
179 f_dialog_input_group_members || exit 0
183 if [ "$mode" = "Edit/View" -o "$mode" = "Delete" ]; then
184 f_input_group "$group" || f_die 1 "$msg_group_not_found"
187 cur_group_name="$group_name"
188 cur_group_password="$group_password"
189 cur_group_gid="$group_gid"
190 cur_group_members="$group_members"
192 [ "$mode" = "Delete" ] && save_flag=1
195 # Loop until the user decides to Exit, Cancel, or presses ESC
198 dialog_title_update "$mode"
201 menu_exit="$msg_exit"
202 if [ "$save_flag" ]; then
203 if [ "$mode" = "Delete" ]; then
204 menu_exit="$msg_delete/$msg_exit"
205 menu_text="$msg_delete_exit_or_cancel"
207 menu_exit="$msg_save/$msg_exit"
208 menu_text="$msg_save_exit_or_cancel"
216 '1' '$msg_group: $group_name'
217 '-' '$msg_password: -----'
218 '-' '$msg_group_id: $group_gid'
219 '-' '$msg_group_members: $group_members'
225 '1' '$msg_group: $group_name'
226 '2' '$msg_password: -----'
227 '3' '$msg_group_id: $group_gid'
228 '4' '$msg_group_members: $group_members'
232 eval f_dialog_menu_size height width rows \
234 \"\$DIALOG_BACKTITLE\" \
239 f_dialog_default_fetch defaultitem
240 mtag=$( eval $DIALOG \
241 --title \"\$DIALOG_TITLE\" \
242 --backtitle \"\$DIALOG_BACKTITLE\" \
243 --hline \"\$hline\" \
244 --ok-label \"\$msg_ok\" \
245 --cancel-label \"\$msg_cancel\" \
246 --default-item \"\$defaultitem\" \
247 --menu \"\$menu_text\" \
248 $height $width $rows \
250 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
253 f_dialog_data_sanitize mtag
254 f_dialog_default_store "$mtag"
255 f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
257 # Exit if user has either pressed ESC or chosen Cancel/No
258 [ $retval -eq $SUCCESS ] || f_die
262 if [ "$save_flag" ]; then
263 save_changes || continue
269 Add) f_dialog_input_group_name "$group_name" ;;
271 f_dialog_menu_group_list "$group_name"
273 f_dialog_menutag_fetch mtag
274 f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
276 # Loop if user has either pressed ESC or chosen Cancel/No
277 [ $retval -eq $SUCCESS ] || continue
279 [ "$mtag" = "X $msg_exit" ] && continue
282 f_input_group "$group" || f_die 1 "$msg_group_not_found"
283 cur_group_name="$group_name"
284 cur_group_password="$group_password"
285 cur_group_gid="$group_gid"
286 cur_group_members="$group_members"
287 [ "$mode" != "Delete" ] && save_flag=
291 f_dialog_input_group_password
294 f_dialog_input_group_gid "$group_gid"
297 f_dialog_input_group_members "$group_members"
305 ################################################################################
307 ################################################################################