3 # Copyright (c) 2012 Ron McDowell
4 # Copyright (c) 2012 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_show_msg "%s %s\n" "$msg_error" "$err"
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_show_msg "%s %s\n" "$msg_warning" "$err"
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_show_msg "%s %s\n" "$msg_warning" "$err"
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_show_msg "%s %s\n" "$msg_error" "$err"
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_show_msg "%s %s\n" "$msg_warning" "$err"
131 elif [ -e "$pw_home_dir" ]; then
132 err=$( chown -R "$pw_uid:$pw_gid" \
133 "$pw_home_dir" 2>&1 )
134 [ $? -eq $SUCCESS ] || f_show_msg \
135 "%s %s\n" "$msg_warning" "$err"
139 if [ "$pw_dotfiles_create" = "$msg_yes" ]; then
140 err=$( copy_dotfiles 2>&1 ) ||
141 f_show_msg "%s %s\n" "$msg_warning" "$err"
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_show_msg "%s %s\n" "$msg_error" "$err"
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_show_msg "%s %s\n" "$msg_warning" "$err"
177 elif [ -e "$pw_home_dir" ]; then
178 err=$( chown -R "$pw_uid:$pw_gid" \
179 "$pw_home_dir" 2>&1 )
180 [ $? -eq $SUCCESS ] || f_show_msg \
181 "%s %s\n" "$msg_warning" "$err"
185 if [ "$pw_dotfiles_create" = "$msg_yes" ]; then
186 err=$( copy_dotfiles 2>&1 ) ||
187 f_show_msg "%s %s\n" "$msg_warning" "$err"
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
242 # Ask a series of questions to pre-fill the editor screen.
244 # The defaults used in each dialog should allow the user to simply
245 # hit ENTER to proceed, because cancelling a single dialog will
246 # cause them to be returned to the main usermenu.
249 f_dialog_input_name || exit 0
250 f_dialog_input_gecos "$pw_name" || exit 0
251 [ "$passwdtype" = "yes" ] &&
252 { f_dialog_input_password || exit 0; }
253 f_dialog_input_uid || exit 0
254 f_dialog_input_gid || exit 0
255 f_dialog_input_member_groups || exit 0
256 f_dialog_input_class || exit 0
257 f_dialog_input_change || exit 0
258 f_dialog_input_expire || exit 0
259 f_dialog_input_home_dir "$homeprefix/$pw_name" || exit 0
260 pw_dotfiles_create="$msg_no"
261 if [ ! -d "$homeprefix/$pw_name" ]; then
262 f_dialog_input_home_create || exit 0
263 [ "$pw_home_create" = "$msg_yes" ] &&
264 { f_dialog_input_dotfiles_create || exit 0; }
266 f_dialog_input_shell "$defaultshell" || exit 0
269 if [ "$mode" = "Edit/View" -o "$mode" = "Delete" ]; then
270 f_input_user "$user" || f_die 1 "$msg_login_not_found"
273 if [ "$mode" = "Edit/View" ]; then
274 [ -d "$pw_home_dir" ] || pw_home_create="$msg_no"
275 pw_dotfiles_create="$msg_no"
278 if [ "$mode" = "Delete" ]; then
279 f_dialog_input_group_delete || exit 0
280 pw_home_delete="$msg_no"
281 [ -d "$pw_home_dir" ] &&
282 { f_dialog_input_home_delete || exit 0; }
285 cur_pw_name="$pw_name"
286 cur_pw_password="$pw_password"
289 cur_pw_member_groups="$pw_member_groups"
290 cur_pw_class="$pw_class"
291 cur_pw_password_expire="$pw_password_expire"
292 cur_pw_account_expire="$pw_account_expire"
293 cur_pw_gecos="$pw_gecos"
294 cur_pw_home_dir="$pw_home_dir"
295 cur_pw_shell="$pw_shell"
296 cur_pw_group_delete="$pw_group_delete"
297 cur_pw_home_create="$pw_home_create"
298 cur_pw_home_delete="$pw_home_delete"
299 cur_pw_dotfiles_create="$pw_dotfiles_create"
301 [ "$mode" = "Delete" ] && save_flag=1
304 # Loop until the user decides to Exit, Cancel, or presses ESC
307 dialog_title_update "$mode"
310 menu_exit="$msg_exit"
311 if [ "$save_flag" ]; then
312 if [ "$mode" = "Delete" ]; then
313 menu_exit="$msg_delete/$msg_exit"
314 menu_text="$msg_delete_exit_or_cancel"
316 menu_exit="$msg_save/$msg_exit"
317 menu_text="$msg_save_exit_or_cancel"
321 pw_password_expires_on="$pw_password_expire"
322 f_isinteger "$pw_password_expire" && [ $pw_password_expire -ne 0 ] &&
323 pw_password_expires_on=$(
324 date -r "$pw_password_expire" "+%F %T %Z"
326 pw_account_expires_on="$pw_account_expire"
327 f_isinteger "$pw_account_expire" && [ "$pw_account_expire" -ne 0 ] &&
328 pw_account_expires_on=$(
329 date -r "$pw_account_expire" "+%F %T %Z"
336 '1' '$msg_login: $pw_name'
337 '-' '$msg_full_name: $pw_gecos'
338 '-' '$msg_password: -----'
339 '-' '$msg_user_id: $pw_uid'
340 '-' '$msg_group_id: $pw_gid'
341 '-' '$msg_member_of_groups: $pw_member_groups'
342 '-' '$msg_login_class: $pw_class'
343 '-' '$msg_password_expires_on: $pw_password_expires_on'
344 '-' '$msg_account_expires_on: $pw_account_expires_on'
345 '-' '$msg_home_directory: $pw_home_dir'
346 '-' '$msg_shell: $pw_shell'
352 '1' '$msg_login: $pw_name'
353 '2' '$msg_full_name: $pw_gecos'
354 '3' '$msg_password: -----'
355 '4' '$msg_user_id: $pw_uid'
356 '5' '$msg_group_id: $pw_gid'
357 '6' '$msg_member_of_groups: $pw_member_groups'
358 '7' '$msg_login_class: $pw_class'
359 '8' '$msg_password_expires_on: $pw_password_expires_on'
360 '9' '$msg_account_expires_on: $pw_account_expires_on'
361 'A' '$msg_home_directory: $pw_home_dir'
362 'B' '$msg_shell: $pw_shell'
368 if [ -d "$pw_home_dir" ]; then menu_items="$menu_items
369 '-' '$msg_create_home_directory: $msg_n_a'
370 'D' '$msg_create_dotfiles: $pw_dotfiles_create'
371 "; else menu_items="$menu_items
372 'C' '$msg_create_home_directory: $pw_home_create'
373 'D' '$msg_create_dotfiles: $pw_dotfiles_create'
377 if [ -d "$pw_home_dir" ]; then menu_items="$menu_items
378 'C' '$msg_delete_primary_group: $pw_group_delete'
379 'D' '$msg_delete_home_directory: $pw_home_delete'
380 "; else menu_items="$menu_items
381 'C' '$msg_delete_primary_group: $pw_group_delete'
382 '-' '$msg_delete_home_directory: $msg_n_a'
387 size=$( eval f_dialog_menu_size \
389 \"\$DIALOG_BACKTITLE\" \
394 dialog_menu=$( eval $DIALOG \
395 --title \"\$DIALOG_TITLE\" \
396 --backtitle \"\$DIALOG_BACKTITLE\" \
397 --hline \"\$hline\" \
398 --ok-label \"\$msg_ok\" \
399 --cancel-label \"\$msg_cancel\" \
400 --menu \"\$menu_text\" $size \
402 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
406 setvar DIALOG_MENU_$$ "$dialog_menu"
407 mtag=$( f_dialog_menutag )
408 f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
410 # Exit if user has either pressed ESC or chosen Cancel/No
411 [ $retval -eq $SUCCESS ] || f_die
415 if [ "$save_flag" ]; then
416 save_changes || continue
422 Add) f_dialog_input_name "$pw_name" ;;
424 f_dialog_menu_user_list
426 mtag=$( f_dialog_menutag )
427 f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
429 # Loop if user has either pressed ESC or chosen Cancel/No
430 [ $retval -eq $SUCCESS ] || continue
432 [ "$mtag" = "X $msg_exit" ] && continue
435 f_input_user "$user" || f_die 1 "$msg_login_not_found"
436 cur_pw_name="$pw_name"
437 cur_pw_password="$pw_password"
440 cur_pw_member_groups="$pw_member_groups"
441 cur_pw_class="$pw_class"
442 cur_pw_password_expire="$pw_password_expire"
443 cur_pw_account_expire="$pw_account_expire"
444 cur_pw_gecos="$pw_gecos"
445 cur_pw_home_dir="$pw_home_dir"
446 cur_pw_shell="$pw_shell"
447 cur_pw_group_delete="$pw_group_delete"
448 cur_pw_home_create="$pw_home_create"
449 cur_pw_home_delete="$pw_home_delete"
450 cur_pw_dotfiles_create="$pw_dotfiles_create"
451 [ "$mode" != "Delete" ] && save_flag=
455 f_dialog_input_gecos "$pw_gecos"
458 f_dialog_input_password
461 f_dialog_input_uid "$pw_uid"
464 f_dialog_input_gid "$pw_gid"
466 6) # Member of Groups
467 f_dialog_input_member_groups "$pw_member_groups"
470 f_dialog_input_class "$pw_class"
472 8) # Password Expire on
473 f_dialog_input_change "$pw_password_expire"
475 9) # Account Expire on
476 f_dialog_input_expire "$pw_account_expire"
479 f_dialog_input_home_dir "$pw_home_dir"
482 f_dialog_input_shell "$pw_shell"
489 C) # Delete Primary Group
490 f_dialog_input_group_delete ;;
491 D) # Delete Home Directory
492 f_dialog_input_home_delete ;;
497 C) # Create Home Directory
498 f_dialog_input_home_create
499 [ "$pw_home_create" = "$msg_no" ] &&
500 pw_dotfiles_create="$msg_no"
503 f_dialog_input_dotfiles_create
504 [ "$pw_dotfiles_create" = "$msg_yes" ] &&
505 pw_home_create="$msg_yes"
515 ################################################################################
517 ################################################################################