daily pastebin goal
34%
SHARE
TWEET

Untitled

a guest Dec 2nd, 2016 231 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env bash
  2. #===============================================================================
  3. #          FILE: samba.sh
  4. #
  5. #         USAGE: ./samba.sh
  6. #
  7. #   DESCRIPTION: Entrypoint for samba docker container
  8. #
  9. #       OPTIONS: ---
  10. #  REQUIREMENTS: ---
  11. #          BUGS: ---
  12. #         NOTES: ---
  13. #        AUTHOR: David Personette (dperson@gmail.com),
  14. #  ORGANIZATION:
  15. #       CREATED: 09/28/2014 12:11
  16. #      REVISION: 1.0
  17. #===============================================================================
  18.  
  19. set -o nounset                              # Treat unset variables as an error
  20.  
  21. ### import: import a smbpasswd file
  22. # Arguments:
  23. #   file) file to import
  24. # Return: user(s) added to container
  25. import() { local name id file="${1}"
  26.     while read name id; do
  27.         useradd "$name" -M -u "$id"
  28.     done < <(cut -d: -f1,2 --output-delimiter=' ' $file)
  29.     pdbedit -i smbpasswd:$file
  30. }
  31.  
  32. ### share: Add share
  33. # Arguments:
  34. #   share) share name
  35. #   path) path to share
  36. #   browsable) 'yes' or 'no'
  37. #   readonly) 'yes' or 'no'
  38. #   guest) 'yes' or 'no'
  39. #   users) list of allowed users
  40. #   admins) list of admin users
  41. # Return: result
  42. share() { local share="$1" path="$2" browsable=${3:-yes} ro=${4:-yes} \
  43.                 guest=${5:-yes} users=${6:-""} admins=${7:-""} \
  44.                 file=/etc/samba/smb.conf
  45.     sed -i "/\\[$share\\]/,/^\$/d" $file
  46.     echo "[$share]" >>$file
  47.     echo "   path = $path" >>$file
  48.     echo "   browsable = $browsable" >>$file
  49.     echo "   read only = $ro" >>$file
  50.     echo "   guest ok = $guest" >>$file
  51.     [[ ${users:-""} && ! ${users:-""} =~ all ]] &&
  52.         echo "   valid users = $(tr ',' ' ' <<< $users)" >>$file
  53.     [[ ${admins:-""} && ! ${admins:-""} =~ none ]] &&
  54.         echo "   admin users = $(tr ',' ' ' <<< $admins)" >>$file
  55.     echo -e "" >>$file
  56. }
  57.  
  58. ### timezone: Set the timezone for the container
  59. # Arguments:
  60. #   timezone) for example EST5EDT
  61. # Return: the correct zoneinfo file will be symlinked into place
  62. timezone() { local timezone="${1:-EST5EDT}"
  63.     [[ -e /usr/share/zoneinfo/$timezone ]] || {
  64.         echo "ERROR: invalid timezone specified: $timezone" >&2
  65.         return
  66.     }
  67.  
  68.     if [[ -w /etc/timezone && $(cat /etc/timezone) != $timezone ]]; then
  69.         echo "$timezone" >/etc/timezone
  70.         ln -sf /usr/share/zoneinfo/$timezone /etc/localtime
  71.         dpkg-reconfigure -f noninteractive tzdata >/dev/null 2>&1
  72.     fi
  73. }
  74.  
  75. ### user: add a user
  76. # Arguments:
  77. #   name) for user
  78. #   password) for user
  79. # Return: user added to container
  80. user() { local name="${1}" passwd="${2}"
  81.     useradd "$name" -M
  82.     echo "$passwd" | tee - | smbpasswd -s -a "$name"
  83. }
  84.  
  85. ### workgroup: set the workgroup
  86. # Arguments:
  87. #   workgroup) the name to set
  88. # Return: configure the correct workgroup
  89. workgroup() { local workgroup="${1}" file=/etc/samba/smb.conf
  90.     sed -i 's/^\( *workgroup = \).*/\1'"$workgroup"'/' $file
  91. }
  92.  
  93. ### usage: Help
  94. # Arguments:
  95. #   none)
  96. # Return: Help text
  97. usage() { local RC=${1:-0}
  98.     echo "Usage: ${0##*/} [-opt] [command]
  99. Options (fields in '[]' are optional, '<>' are required):
  100.    -h          This help
  101.    -i \"<path>\" Import smbpassword
  102.                required arg: \"<path>\" - full file path in container
  103.    -n          Start the 'nmbd' daemon to advertise the shares
  104.    -s \"<name;/path>[;browsable;readonly;guest;users]\" Configure a share
  105.                required arg: \"<name>;<comment>;</path>\"
  106.                <name> is how it's called for clients
  107.                <path> path to share
  108.                NOTE: for the default value, just leave blank
  109.                [browsable] default:'yes' or 'no'
  110.                [readonly] default:'yes' or 'no'
  111.                [guest] allowed default:'yes' or 'no'
  112.                [users] allowed default:'all' or list of allowed users
  113.                [admins] allowed default:'none' or list of admin users
  114.    -t \"\"       Configure timezone
  115.                possible arg: \"[timezone]\" - zoneinfo timezone for container
  116.    -u \"<username;password>\"       Add a user
  117.                required arg: \"<username>;<passwd>\"
  118.                <username> for user
  119.                <password> for user
  120.    -w \"<workgroup>\"       Configure the workgroup (domain) samba should use
  121.                required arg: \"<workgroup>\"
  122.                <workgroup> for samba
  123.  
  124. The 'command' (if provided and valid) will be run instead of samba
  125. " >&2
  126.     exit $RC
  127. }
  128.  
  129. while getopts ":hi:ns:t:u:w:" opt; do
  130.     case "$opt" in
  131.         h) usage ;;
  132.         i) import "$OPTARG" ;;
  133.         n) NMBD="true" ;;
  134.         s) eval share $(sed 's/^\|$/"/g; s/;/" "/g' <<< $OPTARG) ;;
  135.         t) timezone "$OPTARG" ;;
  136.         u) eval user $(sed 's/;/ /g' <<< $OPTARG) ;;
  137.         w) workgroup "$OPTARG" ;;
  138.         "?") echo "Unknown option: -$OPTARG"; usage 1 ;;
  139.         ":") echo "No argument value for option: -$OPTARG"; usage 2 ;;
  140.     esac
  141. done
  142. shift $(( OPTIND - 1 ))
  143.  
  144. [[ "${TZ:-""}" ]] && timezone "$TZ"
  145. [[ "${WORKGROUP:-""}" ]] && workgroup "$WORKGROUP"
  146.  
  147. if [[ $# -ge 1 && -x $(which $1 2>&-) ]]; then
  148.     exec "$@"
  149. elif [[ $# -ge 1 ]]; then
  150.     echo "ERROR: command not found: $1"
  151.     exit 13
  152. elif ps -ef | egrep -v grep | grep -q smbd; then
  153.     echo "Service already running, please restart container to apply changes"
  154. else
  155.     [[ ${NMBD:-""} ]] && ionice -c 3 nmbd -D
  156.     exec ionice -c 3 smbd -FS </dev/null
RAW Paste Data
Top