]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - usr.sbin/bsdconfig/usermgmt/groupinput
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.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 ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
43 [ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
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 err retval=$SUCCESS
60
61         case "$mode" in
62         Delete)
63                 err=$( pw groupdel "$group_name" 2>&1 )
64                 retval=$?
65                 if [ $retval -ne $SUCCESS ]; then
66                         f_dialog_msgbox "$msg_error $err\n"
67                         return $retval
68                 fi
69                 f_show_msg "$msg_group_deleted"
70                 ;;
71         Add)
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
77                         cmd="$cmd -h -"
78                 elif [ "$group_password" ]; then
79                         cmd="echo \"\$group_password\" | $cmd -h 0"
80                 fi
81                 f_dprintf "cmd=%s" "$cmd"
82                 err=$( eval $cmd 2>&1 )
83                 retval=$?
84                 if [ $retval -ne $SUCCESS ]; then
85                         f_dialog_msgbox "$msg_error $err\n"
86                         return $retval
87                 fi
88                 f_show_msg "$msg_group_added"
89                 ;;
90         Edit/View)
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
96                         cmd="$cmd -h -"
97                 elif [ "$group_password" ]; then
98                         cmd="echo \"\$group_password\" | $cmd -h 0"
99                 fi
100                 f_dprintf "cmd=%s" "$cmd"
101                 err=$( eval $cmd 2>&1 )
102                 retval=$?
103                 if [ $retval -ne $SUCCESS ]; then
104                         f_dialog_msgbox "$msg_error $err\n"
105                         return $retval
106                 fi
107                 f_show_msg "$msg_group_updated"
108                 ;;
109         esac
110
111         save_flag=
112         return $SUCCESS
113 }
114
115 # dialog_title_update $mode
116 #
117 # Set the title based on the given $mode.
118
119 dialog_title_update()
120 {
121         local mode="$1"
122         case "$mode" in
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" ;;
126         esac
127 }
128
129 ############################################################ MAIN
130
131 # Incorporate rc-file if it exists
132 [ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
133
134 #
135 # Process command-line arguments
136 #
137 while [ $# -gt 0 ]; do
138         key="${1%%=*}"
139         value="${1#*=}"
140         f_dprintf "key=[%s] value=[%s]" "$key" "$value"
141         case "$key" in
142         mode) mode="$value" ;;
143         group) group="$value" ;;
144         esac
145         shift
146 done
147 f_dprintf "mode=[%s] group=[%s]" "$mode" "$group"
148
149 #
150 # Initialize
151 #
152 dialog_title_update "$mode"
153 f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
154 f_mustberoot_init
155 menu_text=
156 save_flag=
157 hline="$hline_arrows_tab_enter"
158
159 if [ "$mode" = "Add" ]; then
160         f_dialog_input_group_name || exit 0
161
162         f_dialog_noyes "$msg_use_default_values_for_all_account_details"
163         retval=$?
164
165         if [ $retval -eq $DIALOG_ESC ]; then
166                 exit $SUCCESS
167         elif [ $retval -ne $DIALOG_OK ]; then
168                 #
169                 # Ask a series of questions to pre-fill the editor screen.
170                 #
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.
174                 #
175         
176                 [ "$passwdtype" = "yes" ] &&
177                         { f_dialog_input_group_password || exit $SUCCESS; }
178                 f_dialog_input_group_gid  || exit $SUCCESS
179                 f_dialog_input_group_members || exit $SUCCESS
180         fi
181 fi
182
183 if [ "$mode" = "Edit/View" -o "$mode" = "Delete" ]; then
184         f_input_group "$group" || f_die 1 "$msg_group_not_found"
185 fi
186
187 cur_group_name="$group_name"
188 cur_group_password="$group_password"
189 cur_group_gid="$group_gid"
190 cur_group_members="$group_members"
191
192 [ "$mode" = "Delete" ] && save_flag=1
193
194 #
195 # Loop until the user decides to Exit, Cancel, or presses ESC
196 #
197 while :; do
198         dialog_title_update "$mode"
199
200         menu_text=
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"
206                 else
207                         menu_exit="$msg_save/$msg_exit"
208                         menu_text="$msg_save_exit_or_cancel"
209                 fi
210         fi
211
212         case "$mode" in
213         Delete)
214                 menu_items="
215                         'X' '$menu_exit'
216                         '1' '$msg_group: $group_name'
217                         '-' '$msg_password: -----'
218                         '-' '$msg_group_id: $group_gid'
219                         '-' '$msg_group_members: $group_members'
220                 " # END-QUOTE
221                 ;;
222         *)
223                 menu_items="
224                         'X' '$menu_exit'
225                         '1' '$msg_group: $group_name'
226                         '2' '$msg_password: -----'
227                         '3' '$msg_group_id: $group_gid'
228                         '4' '$msg_group_members: $group_members'
229                 " # END-QUOTE
230         esac
231
232         eval f_dialog_menu_size height width rows \
233                                 \"\$DIALOG_TITLE\"     \
234                                 \"\$DIALOG_BACKTITLE\" \
235                                 \"\$menu_text\"        \
236                                 \"\$hline\"            \
237                                 $menu_items
238
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               \
249                 $menu_items                        \
250                 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
251         )
252         retval=$?
253         f_dialog_data_sanitize mtag
254         f_dialog_default_store "$mtag"
255         f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
256
257         # Exit if user has either pressed ESC or chosen Cancel/No
258         [ $retval -eq $DIALOG_OK ] || f_die
259
260         case "$mtag" in
261         X) # Exit
262            [ "$save_flag" ] && { save_changes || continue; }
263            break
264            ;;
265         1) # Group Name
266            case "$mode" in
267            Add) f_dialog_input_group_name "$group_name" ;;
268            Edit/View|Delete)
269                 f_dialog_menu_group_list "$group_name"
270                 retval=$?
271                 f_dialog_menutag_fetch mtag
272                 f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
273
274                 # Loop if user has either pressed ESC or chosen Cancel/No
275                 [ $retval -eq $DIALOG_OK ] || continue
276
277                 [ "$mtag" = "X $msg_exit" ] && continue
278
279                 group="$mtag"
280                 f_input_group "$group" || f_die 1 "$msg_group_not_found"
281                 cur_group_name="$group_name"
282                 cur_group_password="$group_password"
283                 cur_group_gid="$group_gid"
284                 cur_group_members="$group_members"
285                 [ "$mode" != "Delete" ] && save_flag=
286            esac
287            ;;
288         2) # Password
289            f_dialog_input_group_password
290            ;;
291         3) # GID
292            f_dialog_input_group_gid "$group_gid"
293            ;;
294         4) # Users in Group
295            f_dialog_input_group_members "$group_members"
296            ;;
297         esac
298
299 done
300
301 exit $SUCCESS
302
303 ################################################################################
304 # END
305 ################################################################################