Guest User

Jailkit Script

a guest
Mar 9th, 2014
305
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. JAILPREFIX=/home
  4. HOMEDIR=/home
  5.  
  6. USERNAME=$1
  7.  
  8. [ "$1" == "" ] && {
  9.         echo "Syntax: ${0} <username>"
  10.         exit 1
  11. }
  12.  
  13. echo -e "\nCreating jail in ${JAILPREFIX}/${USERNAME}"
  14. jk_init -v -j ${JAILPREFIX}/${USERNAME} sftp scp jk_lsh
  15.  
  16. echo -e "\nCreating home directory in ${JAILPREFIX}/${USERNAME}${HOMEDIR}"
  17. mkdir -p ${JAILPREFIX}/${USERNAME}${HOMEDIR}
  18. rsync -rtv /etc/skel/ ${JAILPREFIX}/${USERNAME}${HOMEDIR}/
  19.  
  20. echo -e "\nAdding user/group ${USERNAME}"
  21. useradd --home-dir ${JAILPREFIX}/${USERNAME}/.${HOMEDIR} --user-group --shell /usr/sbin/jk_chrootsh ${USERNAME}
  22. chown -R ${USERNAME}.${USERNAME} ${JAILPREFIX}/${USERNAME}${HOMEDIR}
  23.  
  24. echo -e "\nGenerating secure password"
  25. PASSWORD=`pwgen -sB 12 1`
  26. echo ${USERNAME}:${PASSWORD} | chpasswd
  27.  
  28. USERID=`id -u ${USERNAME}`
  29. GROUPID=`id -g ${USERNAME}`
  30.  
  31. echo -e "\nUpdating /etc/password inside jail"
  32. echo "${USERNAME}:x:${USERID}:${GROUPID}::${HOMEDIR}:/usr/sbin/jk_lsh" >> ${JAILPREFIX}/${USERNAME}/etc/passwd
  33.  
  34. echo -e "\nUpdating /etc/group inside jail"
  35. echo "${USERNAME}:x:${GROUPID}:" >> ${JAILPREFIX}/${USERNAME}/etc/group
  36.  
  37. echo -e "\nUpdating jk_lsh.ini to allow scp and sftp"
  38. cat >> ${JAILPREFIX}/${USERNAME}/etc/jailkit/jk_lsh.ini << EOL
  39. [${USERNAME}]
  40. paths= /usr/bin, /usr/lib
  41. executables= /usr/bin/scp, /usr/lib/openssh/sftp-server
  42. EOL
  43.  
  44. echo -e "\nSummary"
  45. echo -e "-----------------------------------------------------"
  46. echo -e "Jail: ${JAILPREFIX}/${USERNAME}"
  47. echo -e "Home: ${JAILPREFIX}/${USERNAME}${HOMEDIR}"
  48. echo -e "-------------------------------------------------------"
  49. echo -e "Username: ${USERNAME} (UID: ${USERID})"
  50. echo -e "Group:    ${USERNAME} (GID: ${GROUPID})"
  51. echo -e "Password: ${PASSWORD}"
  52. echo -e "-------------------------------------------------------"
  53. echo -e "\n"
RAW Paste Data