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/user_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"}
50 : ${homeprefix:="/home"}
51 : ${defaultshell:="/bin/sh"}
52 : ${udotdir:="/usr/share/skel"}
54 ############################################################ FUNCTIONS
58 # Copy `skel' dot-files to a new home directory.
62 ( # Operate within sub-shell to protect CWD/glob of parent
63 cd "$udotdir" || exit $?
66 cp -n "$file" "$pw_home_dir/${file#dot}" || exit $?
73 # Save any/all settings (actions performed depend on $mode value).
77 local err retval=$SUCCESS
81 err=$( pw userdel -u "$pw_uid" 2>&1 )
83 if [ $retval -ne $SUCCESS ]; then
84 f_dialog_msgbox "$msg_error $err\n"
87 f_show_msg "$msg_login_deleted"
89 if [ "$pw_group_delete" = "$msg_yes" ] &&
90 f_quietly pw groupshow -g "$pw_gid"
92 err=$( pw groupdel -g "$pw_gid" 2>&1 ) ||
93 f_dialog_msgbox "$msg_warning $err\n"
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"
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
115 elif [ "$pw_password" ]; then
116 cmd="echo \"\$pw_password\" | $cmd -h 0"
118 f_dprintf "cmd=%s" "$cmd"
119 err=$( eval $cmd 2>&1 )
121 if [ $retval -ne $SUCCESS ]; then
122 f_dialog_msgbox "$msg_error $err\n"
125 f_show_msg "$msg_login_added"
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"
139 if [ "$pw_dotfiles_create" = "$msg_yes" ]; then
140 err=$( copy_dotfiles 2>&1 ) ||
141 f_dialog_msgbox "$msg_warning $err\n"
145 f_quietly pw usershow -n "$pw_name" &&
146 mode="Edit/View" # Change mode
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
161 elif [ "$pw_password" ]; then
162 cmd="echo \"\$pw_password\" | $cmd -h 0"
164 f_dprintf "cmd=%s" "$cmd"
165 err=$( eval $cmd 2>&1 )
167 if [ $retval -ne $SUCCESS ]; then
168 f_dialog_msgbox "$msg_error $err\n"
171 f_show_msg "$msg_login_updated"
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"
185 if [ "$pw_dotfiles_create" = "$msg_yes" ]; then
186 err=$( copy_dotfiles 2>&1 ) ||
187 f_dialog_msgbox "$msg_warning $err\n"
196 # dialog_title_update $mode
198 # Set the title based on the given $mode.
200 dialog_title_update()
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" ;;
210 ############################################################ MAIN
212 # Incorporate rc-file if it exists
213 [ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
216 # Process command-line arguments
218 while [ $# -gt 0 ]; do
221 f_dprintf "key=[%s] value=[%s]" "$key" "$value"
223 mode) mode="$value" ;;
224 user) user="$value" ;;
228 f_dprintf "mode=[%s] user=[%s]" "$mode" "$user"
233 dialog_title_update "$mode"
234 f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
238 hline="$hline_arrows_tab_enter"
240 if [ "$mode" = "Add" ]; then
241 f_dialog_input_name || exit 0
244 # Set some sensible defaults for account attributes
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}"
252 pw_home_create="${pw_home_create:-$msg_yes}"
253 pw_dotfiles_create="${pw_dotfiles_create:-$msg_yes}"
255 pw_shell="${pw_shell:-$defaultshell}"
257 f_dialog_noyes "$msg_use_default_values_for_all_account_details"
260 if [ $retval -eq $DIALOG_ESC ]; then
262 elif [ $retval -ne $DIALOG_OK ]; then
264 # Ask a series of questions to pre-fill the editor screen.
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.
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; }
286 f_dialog_input_shell "$pw_shell" || exit 0
290 if [ "$mode" = "Edit/View" -o "$mode" = "Delete" ]; then
291 f_input_user "$user" || f_die 1 "$msg_login_not_found"
294 if [ "$mode" = "Edit/View" ]; then
295 [ -d "$pw_home_dir" ] || pw_home_create="$msg_no"
296 pw_dotfiles_create="$msg_no"
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; }
306 cur_pw_name="$pw_name"
307 cur_pw_password="$pw_password"
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"
322 [ "$mode" = "Delete" ] && save_flag=1
325 # Loop until the user decides to Exit, Cancel, or presses ESC
328 dialog_title_update "$mode"
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"
337 menu_exit="$msg_save/$msg_exit"
338 menu_text="$msg_save_exit_or_cancel"
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"
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"
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'
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'
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'
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'
408 eval f_dialog_menu_size height width rows \
410 \"\$DIALOG_BACKTITLE\" \
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 \
426 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
429 f_dialog_data_sanitize mtag
430 f_dialog_default_store "$mtag"
431 f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
433 # Exit if user has either pressed ESC or chosen Cancel/No
434 [ $retval -eq $DIALOG_OK ] || f_die
438 if [ "$save_flag" ]; then
439 save_changes || continue
445 Add) f_dialog_input_name "$pw_name" ;;
447 f_dialog_menu_user_list "$pw_name"
449 f_dialog_menutag_fetch mtag
450 f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
452 # Loop if user has either pressed ESC or chosen Cancel/No
453 [ $retval -eq $DIALOG_OK ] || continue
455 [ "$mtag" = "X $msg_exit" ] && continue
458 f_input_user "$user" || f_die 1 "$msg_login_not_found"
459 cur_pw_name="$pw_name"
460 cur_pw_password="$pw_password"
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=
478 f_dialog_input_gecos "$pw_gecos" ;;
480 f_dialog_input_password ;;
482 f_dialog_input_uid "$pw_uid" ;;
484 f_dialog_input_gid "$pw_gid" ;;
485 6) # Member of Groups
486 f_dialog_input_member_groups "$pw_member_groups" ;;
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" ;;
494 f_dialog_input_home_dir "$pw_home_dir" ;;
496 f_dialog_input_shell "$pw_shell" ;;
502 C) # Delete Primary Group
503 f_dialog_input_group_delete ;;
504 D) # Delete Home Directory
505 f_dialog_input_home_delete ;;
510 C) # Create Home Directory
511 f_dialog_input_home_create
512 [ "$pw_home_create" = "$msg_no" ] &&
513 pw_dotfiles_create="$msg_no"
516 f_dialog_input_dotfiles_create
517 [ "$pw_dotfiles_create" = "$msg_yes" ] &&
518 pw_home_create="$msg_yes"
527 ################################################################################
529 ################################################################################