Friday, November 1, 2013

Samba Server Scripts

Samba Server Scripts

In this page there are some scripts that are going to create new user, new share drive, add user to share drive, remove user to share 
============== newuser.sh ==============
#!/bin/bash
# Create a new user
if (! test $1)
then
  echo "Usage: $0 username"
  exit 1
fi
echo "Adding user to group"
useradd $1 -g users -s /bin/false -d /shares/homes/$1
echo "Creating home folder"
mkdir /shares/homes/$1
chown -R $1 /shares/homes/$1
chmod -R 700 /shares/homes/$1
echo "Setting password"
smbpasswd -a $1
echo "User with name \"$1\" created."
=========== newshare.sh ================
#!/bin/bash
# Create a new share by creating directory, group and adding samba configuration
config=/etc/samba/smb.conf
config_backup=$config.`date +%Y%m%d_%H%M%S`.bak
config_temp=$config.tmp
if (! test $1)
then
  echo "Usage: $0 share"
  exit 1
fi

if (test -e /shares/$1)
then
  echo "Share exists already"
  exit 1
fi

echo "Adding directory with name \"$1\""
mkdir /shares/$1

echo "Adding group with name \"group_$1\""
groupadd group_$1

echo "Adding \"user-backup\" to group"
usermod user-backup -a -G group_$1

echo "Adjusting rights of new directory"
chgrp -R group_$1 /shares/$1
chmod -R 770 /shares/$1

echo "Adding samba configuration"
echo "
[$1]
   path = /shares/$1
   inherit permissions = yes
   force group = group_$1
   valid users = @group_$1
   guest ok = no
   read only = no
;   read list =" > $config_temp
cp $config $config_backup
cat $config_temp >> $config

service smb restart

echo "Share created. Samba restarted.
Add users to share group with relevant script."
============== adduserstoshare.sh =============
#!/bin/bash
# Add existing users to a share
if (! test $# -eq 2)
then
  echo "Usage: $0 share \"user1 user2 ...\""
  exit 1
fi

if (! test -e /shares/$1)
then
  echo "Share does not exist"
  exit 1
fi

group=group_$1
for user in $2
do
  echo "Adding $user to $group"
  usermod $user -a -G $group
done
service smb restart
echo "Users added to share. Samba restarted."
============= removeuserfromshare.sh ============
#!/bin/bash
# Remove existing users from a share

if (! test $# -eq 2)
then
  echo "Usage: $0 share \"user1 user2 ...\""
  exit 1
fi

if (! test -e /shares/$1)
then
  echo "Share does not exist"
  exit 1
fi

group=group_$1

for user in $2
do
  echo "Removing $user from $group"
  gpasswd -d $user $group
done
service smb restart
echo "Users removed from share. Samba restarted."
========== changepassword.sh ============
#!/bin/bash
# Change a samba user's password
if (! test $1)
then
  echo "Usage: $0 username"
  exit 1
fi
echo "Changing password of user \"$1\"..."
smbpasswd $1
============= disableuser.sh ==============
#!/bin/bash
# Disable a samba user
if (! test $1)
then
  echo "Usage: $0 username"
  exit 1
fi
echo "Disabling user \"$1\"..."
smbpasswd -d $1
============== enableuser.sh =============
#!/bin/bash
# Enable a samba user
if (! test $1)
then
  echo "Usage: $0 username"
  exit 1
fi
echo "Enabling user \"$1\"..."
smbpasswd -e $1
============== deleteuser.sh =============
#!/bin/bash
# Delete a samba user
if (! test $1)
then
  echo "Usage: $0 username"
  exit 1
fi
echo "Deleting user \"$1\"..."
smbpasswd -x $1
============= Create other script for other purpose =============

No comments:

Post a Comment