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 f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$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 funcname=save_changes
63 f_eval_catch $funcname pw 'pw groupdel "%s"' "$group_name" ||
65 f_show_msg "$msg_group_deleted"
68 local cmd="pw groupadd -n '$group_name'"
69 [ "$group_gid" ] && cmd="$cmd -g '$group_gid'"
70 [ "$group_members" != "$cur_group_members" ] &&
71 cmd="$cmd -M '$group_members'"
72 if [ "$pw_group_password_disable" ]; then
73 f_eval_catch $funcname pw '%s -h -' "$cmd"
74 elif [ "$group_password" ]; then
75 echo "$group_password" |
76 f_eval_catch $funcname pw '%s -h 0' "$cmd"
78 f_eval_catch $funcname pw '%s' "$cmd"
80 f_show_msg "$msg_group_added"
83 local cmd="pw groupmod -n '$group_name'"
84 [ "$group_gid" ] && cmd="$cmd -g '$group_gid'"
85 [ "$group_members" != "$cur_group_members" ] &&
86 cmd="$cmd -M '$group_members'"
87 if [ "$pw_group_password_disable" ]; then
88 f_eval_catch $funcname pw '%s -h -' "$cmd"
89 elif [ "$group_password" ]; then
90 echo "$group_password" |
91 f_eval_catch $funcname pw '%s -h 0' "$cmd"
93 f_eval_catch $funcname pw '%s' "$cmd"
95 f_show_msg "$msg_group_updated"
103 # dialog_title_update $mode
105 # Set the title based on the given $mode.
107 dialog_title_update()
111 Add) f_dialog_title "$msg_add $msg_group" ;;
112 Edit/View) f_dialog_title "$msg_edit_view $msg_group: $group" ;;
113 Delete) f_dialog_title "$msg_delete $msg_group: $group" ;;
117 ############################################################ MAIN
119 # Incorporate rc-file if it exists
120 [ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
123 # Process command-line arguments
125 while [ $# -gt 0 ]; do
128 f_dprintf "key=[%s] value=[%s]" "$key" "$value"
130 mode) mode="$value" ;;
131 group) group="$value" ;;
135 f_dprintf "mode=[%s] group=[%s]" "$mode" "$group"
140 dialog_title_update "$mode"
141 f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
145 hline="$hline_arrows_tab_enter"
147 if [ "$mode" = "Add" ]; then
148 f_dialog_input_group_name || exit 0
150 f_dialog_noyes "$msg_use_default_values_for_all_account_details"
153 if [ $retval -eq $DIALOG_ESC ]; then
155 elif [ $retval -ne $DIALOG_OK ]; then
157 # Ask a series of questions to pre-fill the editor screen.
159 # The defaults used in each dialog should allow the user to
160 # simply hit ENTER to proceed, because cancelling a single
161 # dialog will cause them to be returned to the main groupmenu.
164 [ "$passwdtype" = "yes" ] &&
165 { f_dialog_input_group_password || exit $SUCCESS; }
166 f_dialog_input_group_gid || exit $SUCCESS
167 f_dialog_input_group_members || exit $SUCCESS
171 if [ "$mode" = "Edit/View" -o "$mode" = "Delete" ]; then
172 f_input_group "$group" || f_die 1 "$msg_group_not_found"
175 cur_group_name="$group_name"
176 cur_group_password="$group_password"
177 cur_group_gid="$group_gid"
178 cur_group_members="$group_members"
180 [ "$mode" = "Delete" ] && save_flag=1
183 # Loop until the user decides to Exit, Cancel, or presses ESC
186 dialog_title_update "$mode"
189 menu_exit="$msg_exit"
190 if [ "$save_flag" ]; then
191 if [ "$mode" = "Delete" ]; then
192 menu_exit="$msg_delete/$msg_exit"
193 menu_text="$msg_delete_exit_or_cancel"
195 menu_exit="$msg_save/$msg_exit"
196 menu_text="$msg_save_exit_or_cancel"
204 '1' '$msg_group: $group_name'
205 '-' '$msg_password: -----'
206 '-' '$msg_group_id: $group_gid'
207 '-' '$msg_group_members: $group_members'
213 '1' '$msg_group: $group_name'
214 '2' '$msg_password: -----'
215 '3' '$msg_group_id: $group_gid'
216 '4' '$msg_group_members: $group_members'
220 eval f_dialog_menu_size height width rows \
222 \"\$DIALOG_BACKTITLE\" \
227 f_dialog_default_fetch defaultitem
228 mtag=$( eval $DIALOG \
229 --title \"\$DIALOG_TITLE\" \
230 --backtitle \"\$DIALOG_BACKTITLE\" \
231 --hline \"\$hline\" \
232 --ok-label \"\$msg_ok\" \
233 --cancel-label \"\$msg_cancel\" \
234 --default-item \"\$defaultitem\" \
235 --menu \"\$menu_text\" \
236 $height $width $rows \
238 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
241 f_dialog_data_sanitize mtag
242 f_dialog_default_store "$mtag"
243 f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
245 # Exit if user has either pressed ESC or chosen Cancel/No
246 [ $retval -eq $DIALOG_OK ] || f_die
250 [ "$save_flag" ] && { save_changes || continue; }
255 Add) f_dialog_input_group_name "$group_name" ;;
257 f_dialog_menu_group_list "$group_name"
259 f_dialog_menutag_fetch mtag
260 f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
262 # Loop if user has either pressed ESC or chosen Cancel/No
263 [ $retval -eq $DIALOG_OK ] || continue
265 [ "$mtag" = "X $msg_exit" ] && continue
268 f_input_group "$group" || f_die 1 "$msg_group_not_found"
269 cur_group_name="$group_name"
270 cur_group_password="$group_password"
271 cur_group_gid="$group_gid"
272 cur_group_members="$group_members"
273 [ "$mode" != "Delete" ] && save_flag=
277 f_dialog_input_group_password
280 f_dialog_input_group_gid "$group_gid"
283 f_dialog_input_group_members "$group_members"
291 ################################################################################
293 ################################################################################