]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - usr.sbin/bsdconfig/usermgmt/groupinput
MFC r259054:
[FreeBSD/stable/10.git] / usr.sbin / bsdconfig / usermgmt / groupinput
1 #!/bin/sh
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
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
38
39 BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="070.usermgmt"
40 f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
41
42 f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" ipgm &&
43         pgm="${ipgm:-$pgm}"
44
45 ############################################################ CONFIGURATION
46
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"}
50
51 ############################################################ FUNCTIONS
52
53 # save_changes
54 #
55 # Save any/all settings (actions performed depend on $mode value).
56 #
57 save_changes()
58 {
59         local funcname=save_changes
60
61         case "$mode" in
62         Delete)
63                 f_eval_catch $funcname pw 'pw groupdel "%s"' "$group_name" ||
64                         return $?
65                 f_show_msg "$msg_group_deleted"
66                 ;;
67         Add)
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"
77                 else
78                         f_eval_catch $funcname pw '%s' "$cmd"
79                 fi || return $?
80                 f_show_msg "$msg_group_added"
81                 ;;
82         Edit/View)
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"
92                 else
93                         f_eval_catch $funcname pw '%s' "$cmd"
94                 fi || return $?
95                 f_show_msg "$msg_group_updated"
96                 ;;
97         esac
98
99         save_flag=
100         return $SUCCESS
101 }
102
103 # dialog_title_update $mode
104 #
105 # Set the title based on the given $mode.
106
107 dialog_title_update()
108 {
109         local mode="$1"
110         case "$mode" in
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" ;;
114         esac
115 }
116
117 ############################################################ MAIN
118
119 # Incorporate rc-file if it exists
120 [ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
121
122 #
123 # Process command-line arguments
124 #
125 while [ $# -gt 0 ]; do
126         key="${1%%=*}"
127         value="${1#*=}"
128         f_dprintf "key=[%s] value=[%s]" "$key" "$value"
129         case "$key" in
130         mode) mode="$value" ;;
131         group) group="$value" ;;
132         esac
133         shift
134 done
135 f_dprintf "mode=[%s] group=[%s]" "$mode" "$group"
136
137 #
138 # Initialize
139 #
140 dialog_title_update "$mode"
141 f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
142 f_mustberoot_init
143 menu_text=
144 save_flag=
145 hline="$hline_arrows_tab_enter"
146
147 if [ "$mode" = "Add" ]; then
148         f_dialog_input_group_name || exit 0
149
150         f_dialog_noyes "$msg_use_default_values_for_all_account_details"
151         retval=$?
152
153         if [ $retval -eq $DIALOG_ESC ]; then
154                 exit $SUCCESS
155         elif [ $retval -ne $DIALOG_OK ]; then
156                 #
157                 # Ask a series of questions to pre-fill the editor screen.
158                 #
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.
162                 #
163         
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
168         fi
169 fi
170
171 if [ "$mode" = "Edit/View" -o "$mode" = "Delete" ]; then
172         f_input_group "$group" || f_die 1 "$msg_group_not_found"
173 fi
174
175 cur_group_name="$group_name"
176 cur_group_password="$group_password"
177 cur_group_gid="$group_gid"
178 cur_group_members="$group_members"
179
180 [ "$mode" = "Delete" ] && save_flag=1
181
182 #
183 # Loop until the user decides to Exit, Cancel, or presses ESC
184 #
185 while :; do
186         dialog_title_update "$mode"
187
188         menu_text=
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"
194                 else
195                         menu_exit="$msg_save/$msg_exit"
196                         menu_text="$msg_save_exit_or_cancel"
197                 fi
198         fi
199
200         case "$mode" in
201         Delete)
202                 menu_items="
203                         'X' '$menu_exit'
204                         '1' '$msg_group: $group_name'
205                         '-' '$msg_password: -----'
206                         '-' '$msg_group_id: $group_gid'
207                         '-' '$msg_group_members: $group_members'
208                 " # END-QUOTE
209                 ;;
210         *)
211                 menu_items="
212                         'X' '$menu_exit'
213                         '1' '$msg_group: $group_name'
214                         '2' '$msg_password: -----'
215                         '3' '$msg_group_id: $group_gid'
216                         '4' '$msg_group_members: $group_members'
217                 " # END-QUOTE
218         esac
219
220         eval f_dialog_menu_size height width rows \
221                                 \"\$DIALOG_TITLE\"     \
222                                 \"\$DIALOG_BACKTITLE\" \
223                                 \"\$menu_text\"        \
224                                 \"\$hline\"            \
225                                 $menu_items
226
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               \
237                 $menu_items                        \
238                 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
239         )
240         retval=$?
241         f_dialog_data_sanitize mtag
242         f_dialog_default_store "$mtag"
243         f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
244
245         # Exit if user has either pressed ESC or chosen Cancel/No
246         [ $retval -eq $DIALOG_OK ] || f_die
247
248         case "$mtag" in
249         X) # Exit
250            [ "$save_flag" ] && { save_changes || continue; }
251            break
252            ;;
253         1) # Group Name
254            case "$mode" in
255            Add) f_dialog_input_group_name "$group_name" ;;
256            Edit/View|Delete)
257                 f_dialog_menu_group_list "$group_name"
258                 retval=$?
259                 f_dialog_menutag_fetch mtag
260                 f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
261
262                 # Loop if user has either pressed ESC or chosen Cancel/No
263                 [ $retval -eq $DIALOG_OK ] || continue
264
265                 [ "$mtag" = "X $msg_exit" ] && continue
266
267                 group="$mtag"
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=
274            esac
275            ;;
276         2) # Password
277            f_dialog_input_group_password
278            ;;
279         3) # GID
280            f_dialog_input_group_gid "$group_gid"
281            ;;
282         4) # Users in Group
283            f_dialog_input_group_members "$group_members"
284            ;;
285         esac
286
287 done
288
289 exit $SUCCESS
290
291 ################################################################################
292 # END
293 ################################################################################