Skip to content

Instantly share code, notes, and snippets.

@jensb
Created August 31, 2023 19:39
Show Gist options
  • Save jensb/7e46d9ab1f1d76604420c3f7a0b75c06 to your computer and use it in GitHub Desktop.
Save jensb/7e46d9ab1f1d76604420c3f7a0b75c06 to your computer and use it in GitHub Desktop.
OpenWrt menu based user and (smb)passwd management
#!/bin/bash
# User Management script for OpenWRT Samba user managemen (passwd and smbpasswd files).
# Needs "whiptail" and "samba4-server" packages installed, can be set as startup script when logging in as root
# This will not care about user groups and assume all users have group "users" (gid 100) for simplicity!
W=/usr/bin/whiptail
UA=/usr/sbin/useradd
UD=/usr/sbin/userdel
S=/usr/bin/smbpasswd
SMALL="10 60"
FULL="25 78 5"
while true; do
CHOICE=$($W --title "Created by: Jens Benecke" --menu "OpenWRT User Management.
Please use the cursor keys to navigate (not the mouse).
After creating a user here, this username can be used to assign shares
in the Web Interface at https://openwrt/." $FULL \
"Add User" "Add a user to the system" \
"Delete User" "Delete an existing user" \
"Change Password" "Add or change user password" \
"Exit" "Exit the program" 3>&1 1>&2 2>&3)
case $CHOICE in
"Add User" )
USERNAME=$($W --inputbox "Please enter the short username (allowed: lowercase a-z, 0-9, _):" $SMALL 3>&1 1>&2 2>&3)
echo User = $USERNAME ; sleep 1
if id "$USERNAME" >/dev/null 2>&1; then
$W --msgbox "User already exists!" 10 60
else
$UA --no-user-group --no-create-home --password "*" "$USERNAME" && \
$S -a -n -d "$USERNAME" && \
$W --msgbox "User has been added, but has not yet received a password. Set one next." $SMALL
fi
if ! id "$USERNAME" >/dev/null 2>&1; then
$W --msgbox "User was not created. Please recheck username." 10 60
fi
;;
"Delete User" )
USERNAME=$($W --menu "Select a user for deletion. Only the account will be removed, no files." $SMALL \
$(cat /etc/passwd | grep ':100::' | cut -f1 -d: | while read X ; do echo "$X" "$X" ; done) 3>&1 1>&2 2>&3)
echo User = $USERNAME ; sleep 1
if id "$USERNAME" >/dev/null 2>&1 ; then
$W --yesno "Do you really want to delete $USERNAME?" $SMALL && \
$S -x "$USERNAME" && \
$UD "$USERNAME" && \
$W --msgbox "User $USERNAME has been deleted." $SMALL
fi
if id "$USERNAME" >/dev/null 2>&1; then
$W --msgbox "User $USERNAME was not deleted. Please recheck username and try again." $SMALL
fi
;;
"Change Password")
USERNAME=$($W --menu "Select a user for password change." $FULL \
$(cat /etc/passwd | grep ':100::' | cut -f1 -d: | while read X ; do echo "$X" "$X" ; done) 3>&1 1>&2 2>&3)
echo User = $USERNAME ; sleep 1
if id "$USERNAME" >/dev/null 2>&1; then
$S "$USERNAME"
$S -e "$USERNAME"
$W --msgbox "Password changed successfully and user $USERNAME enabled." $SMALL
else
$W --msgbox "No user selected or user not found." $SMALL
fi
;;
"Exit")
echo "You can now close this terminal window or type 'exit' to logout."
exit
;;
*) # Invalid choice
$W --msgbox "Invalid choice. Please try again." $SMALL
esac
echo "Back to menu ..."
#sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment