]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - usr.sbin/bsdconfig/usermgmt/userinput
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / usr.sbin / bsdconfig / usermgmt / userinput
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/user_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 : ${homeprefix:="/home"}
51 : ${defaultshell:="/bin/sh"}
52 : ${udotdir:="/usr/share/skel"}
53
54 ############################################################ FUNCTIONS
55
56 # copy_dotfiles
57 #
58 # Copy `skel' dot-files to a new home directory.
59 #
60 copy_dotfiles()
61 {
62         ( # Operate within sub-shell to protect CWD/glob of parent
63                 cd "$udotdir" || exit $?
64                 set +f # glob
65                 for file in dot.*; do
66                         cp -n "$file" "$pw_home_dir/${file#dot}" || exit $?
67                 done
68         )
69 }
70
71 # save_changes
72 #
73 # Save any/all settings (actions performed depend on $mode value).
74 #
75 save_changes()
76 {
77         local err retval=$SUCCESS
78
79         case "$mode" in
80         Delete)
81                 err=$( pw userdel -u "$pw_uid" 2>&1 )
82                 retval=$?
83                 if [ $retval -ne $SUCCESS ]; then
84                         f_dialog_msgbox "$msg_error $err\n"
85                         return $retval
86                 fi
87                 f_show_msg "$msg_login_deleted"
88
89                 if [ "$pw_group_delete" = "$msg_yes" ] &&
90                    f_quietly pw groupshow -g "$pw_gid"
91                 then
92                         err=$( pw groupdel -g "$pw_gid" 2>&1 ) ||
93                                 f_dialog_msgbox "$msg_warning $err\n"
94                 fi
95
96                 if [ "$pw_home_delete" = "$msg_yes" ]; then
97                         f_dialog_info "$msg_deleting_home_directory"
98                         err=$( rm -Rf "$pw_home_dir" 2>&1 ) ||
99                                 f_dialog_msgbox "$msg_warning $err\n"
100                 fi
101                 ;;
102         Add)
103                 local cmd="pw useradd -n '$pw_name'"
104                 [ "$pw_member_groups"   ] && cmd="$cmd -G '$pw_member_groups'"
105                 [ "$pw_class"           ] && cmd="$cmd -L '$pw_class'"
106                 [ "$pw_gecos"           ] && cmd="$cmd -c '$pw_gecos'"
107                 [ "$pw_home_dir"        ] && cmd="$cmd -d '$pw_home_dir'"
108                 [ "$pw_account_expire"  ] && cmd="$cmd -e '$pw_account_expire'"
109                 [ "$pw_gid"             ] && cmd="$cmd -g '$pw_gid'"
110                 [ "$pw_password_expire" ] && cmd="$cmd -p '$pw_password_expire'"
111                 [ "$pw_shell"           ] && cmd="$cmd -s '$pw_shell'"
112                 [ "$pw_uid"             ] && cmd="$cmd -u '$pw_uid'"
113                 if [ "$pw_password_disable" ]; then
114                         cmd="$cmd -h -"
115                 elif [ "$pw_password" ]; then
116                         cmd="echo \"\$pw_password\" | $cmd -h 0"
117                 fi
118                 f_dprintf "cmd=%s" "$cmd"
119                 err=$( eval $cmd 2>&1 )
120                 retval=$?
121                 if [ $retval -ne $SUCCESS ]; then
122                         f_dialog_msgbox "$msg_error $err\n"
123                         return $retval
124                 fi
125                 f_show_msg "$msg_login_added"
126
127                 if [ "$pw_home_create" = "$msg_yes" ]; then
128                         err=$( mkdir -p "$pw_home_dir" 2>&1 )
129                         if [ $? -ne $SUCCESS ]; then
130                                 f_dialog_msgbox "$msg_warning $err\n"
131                         elif [ -e "$pw_home_dir" ]; then
132                                 err=$( chown -R "$pw_uid:$pw_gid" \
133                                                 "$pw_home_dir" 2>&1 )
134                                 [ $? -eq $SUCCESS ] ||
135                                         f_dialog_msgbox "$msg_warning $err\n"
136                         fi
137                 fi
138
139                 if [ "$pw_dotfiles_create" = "$msg_yes" ]; then
140                         err=$( copy_dotfiles 2>&1 ) ||
141                                 f_dialog_msgbox "$msg_warning $err\n"
142                 fi
143
144                 user="$pw_name"
145                 f_quietly pw usershow -n "$pw_name" &&
146                         mode="Edit/View" # Change mode
147                 ;;
148         Edit/View)
149                 local cmd="pw usermod -n '$pw_name'"
150                 [ "$pw_member_groups"   ] && cmd="$cmd -G '$pw_member_groups'"
151                 [ "$pw_class"           ] && cmd="$cmd -L '$pw_class'"
152                 [ "$pw_gecos"           ] && cmd="$cmd -c '$pw_gecos'"
153                 [ "$pw_home_dir"        ] && cmd="$cmd -d '$pw_home_dir'"
154                 [ "$pw_account_expire"  ] && cmd="$cmd -e '$pw_account_expire'"
155                 [ "$pw_gid"             ] && cmd="$cmd -g '$pw_gid'"
156                 [ "$pw_password_expire" ] && cmd="$cmd -p '$pw_password_expire'"
157                 [ "$pw_shell"           ] && cmd="$cmd -s '$pw_shell'"
158                 [ "$pw_uid"             ] && cmd="$cmd -u '$pw_uid'"
159                 if [ "$pw_password_disable" ]; then
160                         cmd="$cmd -h -"
161                 elif [ "$pw_password" ]; then
162                         cmd="echo \"\$pw_password\" | $cmd -h 0"
163                 fi
164                 f_dprintf "cmd=%s" "$cmd"
165                 err=$( eval $cmd 2>&1 )
166                 retval=$?
167                 if [ $retval -ne $SUCCESS ]; then
168                         f_dialog_msgbox "$msg_error $err\n"
169                         return $retval
170                 fi
171                 f_show_msg "$msg_login_updated"
172
173                 if [ "$pw_home_create" = "$msg_yes" ]; then
174                         err=$( mkdir -p "$pw_home_dir" )
175                         if [ $? -ne $SUCCESS ]; then
176                                 f_dialog_msgbox "$msg_warning $err\n"
177                         elif [ -e "$pw_home_dir" ]; then
178                                 err=$( chown -R "$pw_uid:$pw_gid" \
179                                                 "$pw_home_dir" 2>&1 )
180                                 [ $? -eq $SUCCESS ] ||
181                                         f_dialog_msgbox "$msg_warning $err\n"
182                         fi
183                 fi
184
185                 if [ "$pw_dotfiles_create" = "$msg_yes" ]; then
186                         err=$( copy_dotfiles 2>&1 ) ||
187                                 f_dialog_msgbox "$msg_warning $err\n"
188                 fi
189                 ;;
190         esac
191
192         save_flag=
193         return $SUCCESS
194 }
195
196 # dialog_title_update $mode
197 #
198 # Set the title based on the given $mode.
199 #
200 dialog_title_update()
201 {
202         local mode="$1"
203         case "$mode" in
204         Add)       f_dialog_title "$msg_add $msg_user" ;;
205         Edit/View) f_dialog_title "$msg_edit_view $msg_user: $user" ;;
206         Delete)    f_dialog_title "$msg_delete $msg_user: $user" ;;
207         esac
208 }
209
210 ############################################################ MAIN
211
212 # Incorporate rc-file if it exists
213 [ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
214
215 #
216 # Process command-line arguments
217 #
218 while [ $# -gt 0 ]; do
219         key="${1%%=*}"
220         value="${1#*=}"
221         f_dprintf "key=[%s] value=[%s]" "$key" "$value"
222         case "$key" in
223         mode) mode="$value" ;;
224         user) user="$value" ;;
225         esac
226         shift
227 done
228 f_dprintf "mode=[%s] user=[%s]" "$mode" "$user"
229
230 #
231 # Initialize
232 #
233 dialog_title_update "$mode"
234 f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
235 f_mustberoot_init
236 menu_text=
237 save_flag=
238 hline="$hline_arrows_tab_enter"
239
240 if [ "$mode" = "Add" ]; then
241         f_dialog_input_name || exit 0
242
243         #
244         # Set some sensible defaults for account attributes
245         #
246         pw_gecos="${pw_gecos-$pw_name}"
247         pw_home_dir="${pw_home_dir:-$homeprefix/$pw_name}"
248         if [ -d "$pw_home_dir" ]; then
249                 pw_home_create="${pw_home_create:-$msg_no}"
250                 pw_dotfiles_create="${pw_dotfiles_create:-$msg_no}"
251         else
252                 pw_home_create="${pw_home_create:-$msg_yes}"
253                 pw_dotfiles_create="${pw_dotfiles_create:-$msg_yes}"
254         fi
255         pw_shell="${pw_shell:-$defaultshell}"
256
257         f_dialog_noyes "$msg_use_default_values_for_all_account_details"
258         retval=$?
259
260         if [ $retval -eq $DIALOG_ESC ]; then
261                 exit $SUCCESS
262         elif [ $retval -ne $DIALOG_OK ]; then
263                 #
264                 # Ask a series of questions to pre-fill the editor screen.
265                 #
266                 # The defaults used in each dialog should allow the user to
267                 # simply hit ENTER to proceed, because cancelling a single
268                 # dialog will cause them to be returned to the main usermenu.
269                 #
270
271                 f_dialog_input_gecos "$pw_gecos" || exit 0
272                 [ "$passwdtype" = "yes" ] &&
273                         { f_dialog_input_password || exit 0; }
274                 f_dialog_input_uid || exit 0
275                 f_dialog_input_gid || exit 0
276                 f_dialog_input_member_groups || exit 0
277                 f_dialog_input_class || exit 0
278                 f_dialog_input_expire_password || exit 0
279                 f_dialog_input_expire_account || exit 0
280                 f_dialog_input_home_dir "$pw_home_dir" || exit 0
281                 if [ ! -d "$pw_home_dir" ]; then
282                         f_dialog_input_home_create || exit 0
283                         [ "$pw_home_create" = "$msg_yes" ] &&
284                                 { f_dialog_input_dotfiles_create || exit 0; }
285                 fi
286                 f_dialog_input_shell "$pw_shell" || exit 0
287         fi
288 fi
289
290 if [ "$mode" = "Edit/View" -o "$mode" = "Delete" ]; then
291         f_input_user "$user" || f_die 1 "$msg_login_not_found"
292 fi
293
294 if [ "$mode" = "Edit/View" ]; then
295         [ -d "$pw_home_dir" ] || pw_home_create="$msg_no"
296         pw_dotfiles_create="$msg_no"
297 fi
298
299 if [ "$mode" = "Delete" ]; then
300         f_dialog_input_group_delete || exit 0
301         pw_home_delete="$msg_no"
302         [ -d "$pw_home_dir" ] &&
303                 { f_dialog_input_home_delete || exit 0; }
304 fi
305
306 cur_pw_name="$pw_name"
307 cur_pw_password="$pw_password"
308 cur_pw_uid="$pw_uid"
309 cur_pw_gid="$pw_gid"
310 cur_pw_member_groups="$pw_member_groups"
311 cur_pw_class="$pw_class"
312 cur_pw_password_expire="$pw_password_expire"
313 cur_pw_account_expire="$pw_account_expire"
314 cur_pw_gecos="$pw_gecos"
315 cur_pw_home_dir="$pw_home_dir"
316 cur_pw_shell="$pw_shell"
317 cur_pw_group_delete="$pw_group_delete"
318 cur_pw_home_create="$pw_home_create"
319 cur_pw_home_delete="$pw_home_delete"
320 cur_pw_dotfiles_create="$pw_dotfiles_create"
321
322 [ "$mode" = "Delete" ] && save_flag=1
323
324 #
325 # Loop until the user decides to Exit, Cancel, or presses ESC
326 #
327 while :; do
328         dialog_title_update "$mode"
329
330         menu_text=
331         menu_exit="$msg_exit"
332         if [ "$save_flag" ]; then
333                 if [ "$mode" = "Delete" ]; then
334                         menu_exit="$msg_delete/$msg_exit"
335                         menu_text="$msg_delete_exit_or_cancel"
336                 else
337                         menu_exit="$msg_save/$msg_exit"
338                         menu_text="$msg_save_exit_or_cancel"
339                 fi
340         fi
341
342         pw_password_expires_on="$pw_password_expire"
343         f_isinteger "$pw_password_expire" && [ $pw_password_expire -ne 0 ] &&
344                 pw_password_expires_on=$(
345                         date -r "$pw_password_expire" "+%F %T %Z"
346                 )
347         pw_account_expires_on="$pw_account_expire"
348         f_isinteger "$pw_account_expire" && [ "$pw_account_expire" -ne 0 ] &&
349                 pw_account_expires_on=$(
350                         date -r "$pw_account_expire" "+%F %T %Z"
351                 )
352
353         case "$mode" in
354         Delete)
355                 menu_items="
356                         'X' '$menu_exit'
357                         '1' '$msg_login: $pw_name'
358                         '-' '$msg_full_name: $pw_gecos'
359                         '-' '$msg_password: -----'
360                         '-' '$msg_user_id: $pw_uid'
361                         '-' '$msg_group_id: $pw_gid'
362                         '-' '$msg_member_of_groups: $pw_member_groups'
363                         '-' '$msg_login_class: $pw_class'
364                         '-' '$msg_password_expires_on: $pw_password_expires_on'
365                         '-' '$msg_account_expires_on: $pw_account_expires_on'
366                         '-' '$msg_home_directory: $pw_home_dir'
367                         '-' '$msg_shell: $pw_shell'
368                 " # END-QUOTE
369                 ;;
370         *)
371                 menu_items="
372                         'X' '$menu_exit'
373                         '1' '$msg_login: $pw_name'
374                         '2' '$msg_full_name: $pw_gecos'
375                         '3' '$msg_password: -----'
376                         '4' '$msg_user_id: $pw_uid'
377                         '5' '$msg_group_id: $pw_gid'
378                         '6' '$msg_member_of_groups: $pw_member_groups'
379                         '7' '$msg_login_class: $pw_class'
380                         '8' '$msg_password_expires_on: $pw_password_expires_on'
381                         '9' '$msg_account_expires_on: $pw_account_expires_on'
382                         'A' '$msg_home_directory: $pw_home_dir'
383                         'B' '$msg_shell: $pw_shell'
384                 " # END-QUOTE
385         esac
386
387         case "$mode" in
388         Add|Edit/View)
389                 if [ -d "$pw_home_dir" ]; then menu_items="$menu_items
390                         '-' '$msg_create_home_directory: $msg_n_a'
391                         'D' '$msg_create_dotfiles: $pw_dotfiles_create'
392                 "; else menu_items="$menu_items
393                         'C' '$msg_create_home_directory: $pw_home_create'
394                         'D' '$msg_create_dotfiles: $pw_dotfiles_create'
395                 "; fi
396                 ;;
397         Delete)
398                 if [ -d "$pw_home_dir" ]; then menu_items="$menu_items
399                         'C' '$msg_delete_primary_group: $pw_group_delete'
400                         'D' '$msg_delete_home_directory: $pw_home_delete'
401                 "; else menu_items="$menu_items
402                         'C' '$msg_delete_primary_group: $pw_group_delete'
403                         '-' '$msg_delete_home_directory: $msg_n_a'
404                 "; fi
405                 ;;
406         esac
407
408         eval f_dialog_menu_size height width rows \
409                                 \"\$DIALOG_TITLE\"     \
410                                 \"\$DIALOG_BACKTITLE\" \
411                                 \"\$menu_text\"        \
412                                 \"\$hline\"            \
413                                 $menu_items
414
415         f_dialog_default_fetch defaultitem
416         mtag=$( eval $DIALOG \
417                 --title \"\$DIALOG_TITLE\"         \
418                 --backtitle \"\$DIALOG_BACKTITLE\" \
419                 --hline \"\$hline\"                \
420                 --ok-label \"\$msg_ok\"            \
421                 --cancel-label \"\$msg_cancel\"    \
422                 --default-item \"\$defaultitem\"   \
423                 --menu \"\$menu_text\"             \
424                 $height $width $rows               \
425                 $menu_items                        \
426                 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
427         )
428         retval=$?
429         f_dialog_data_sanitize mtag
430         f_dialog_default_store "$mtag"
431         f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
432
433         # Exit if user has either pressed ESC or chosen Cancel/No
434         [ $retval -eq $DIALOG_OK ] || f_die
435
436         case "$mtag" in
437         X) # Exit
438            if [ "$save_flag" ]; then
439                 save_changes || continue
440            fi
441            break
442            ;;
443         1) # Login
444            case "$mode" in
445            Add) f_dialog_input_name "$pw_name" ;;
446            Edit/View|Delete)
447                 f_dialog_menu_user_list "$pw_name"
448                 retval=$?
449                 f_dialog_menutag_fetch mtag
450                 f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
451
452                 # Loop if user has either pressed ESC or chosen Cancel/No
453                 [ $retval -eq $DIALOG_OK ] || continue
454
455                 [ "$mtag" = "X $msg_exit" ] && continue
456
457                 user="$mtag"
458                 f_input_user "$user" || f_die 1 "$msg_login_not_found"
459                 cur_pw_name="$pw_name"
460                 cur_pw_password="$pw_password"
461                 cur_pw_uid="$pw_uid"
462                 cur_pw_gid="$pw_gid"
463                 cur_pw_member_groups="$pw_member_groups"
464                 cur_pw_class="$pw_class"
465                 cur_pw_password_expire="$pw_password_expire"
466                 cur_pw_account_expire="$pw_account_expire"
467                 cur_pw_gecos="$pw_gecos"
468                 cur_pw_home_dir="$pw_home_dir"
469                 cur_pw_shell="$pw_shell"
470                 cur_pw_group_delete="$pw_group_delete"
471                 cur_pw_home_create="$pw_home_create"
472                 cur_pw_home_delete="$pw_home_delete"
473                 cur_pw_dotfiles_create="$pw_dotfiles_create"
474                 [ "$mode" != "Delete" ] && save_flag=
475            esac
476            ;;
477         2) # Full Name
478            f_dialog_input_gecos "$pw_gecos" ;;
479         3) # Password
480            f_dialog_input_password ;;
481         4) # UID
482            f_dialog_input_uid "$pw_uid" ;;
483         5) # Default Group
484            f_dialog_input_gid "$pw_gid" ;;
485         6) # Member of Groups
486            f_dialog_input_member_groups "$pw_member_groups" ;;
487         7) # Login Class
488            f_dialog_input_class "$pw_class" ;;
489         8) # Password Expire on
490            f_dialog_input_expire_password "$pw_password_expire" ;;
491         9) # Account Expire on
492            f_dialog_input_expire_account "$pw_account_expire" ;;
493         A) # Home Directory
494            f_dialog_input_home_dir "$pw_home_dir" ;;
495         B) # Shell
496            f_dialog_input_shell "$pw_shell" ;;
497         esac
498
499         case "$mode" in
500         Delete)
501                 case "$mtag" in
502                 C) # Delete Primary Group
503                    f_dialog_input_group_delete ;;
504                 D) # Delete Home Directory
505                    f_dialog_input_home_delete ;;
506                 esac
507                 ;;
508         Add|Edit/View)
509                 case "$mtag" in
510                 C) # Create Home Directory
511                    f_dialog_input_home_create
512                    [ "$pw_home_create" = "$msg_no" ] &&
513                         pw_dotfiles_create="$msg_no"
514                    ;;
515                 D) # Create Dotfiles
516                    f_dialog_input_dotfiles_create
517                    [ "$pw_dotfiles_create" = "$msg_yes" ] &&
518                         pw_home_create="$msg_yes"
519                    ;;
520                 esac
521                 ;;
522         esac
523 done
524
525 exit $SUCCESS
526
527 ################################################################################
528 # END
529 ################################################################################