#!/bin/sh
#
# This file is part of OpenMediaVault.
#
# @license   http://www.gnu.org/licenses/gpl.html GPL Version 3
# @author    Volker Theile <volker.theile@openmediavault.org>
# @copyright Copyright (c) 2009-2012 Volker Theile
#
# OpenMediaVault is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# OpenMediaVault is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OpenMediaVault. If not, see <http://www.gnu.org/licenses/>.

# Documentation/Howto:
# http://www.linux-user.de/ausgabe/2006/04/090-rsync/
# http://ubuntuforums.org/showthread.php?p=7865055
# http://everythinglinux.org/rsync/
# http://www.fredshack.com/docs/rsync.html

set -e

. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions

OMV_RSYNCD_DEFAULT=${OMV_RSYNCD_DEFAULT:-"/etc/default/rsync"}
OMV_RSYNCD_CONFIG=${OMV_RSYNCD_CONFIG:-"/etc/rsyncd.conf"}
OMV_RSYNCD_USECHROOT=${OMV_RSYNCD_USECHROOT:-"yes"}
OMV_RSYNCD_SECRETSFILE_DIR=${OMV_RSYNCD_SECRETSFILE_DIR:-"/var/lib/openmediavault"}

# Create '/etc/default/rsync' file
xmlstarlet sel -t -m "//services/rsync/server" \
  -o "# defaults file for rsync daemon mode" -n \
  -i "enable[. = '1']" -o "RSYNC_ENABLE=true" -n -b \
  -i "enable[. = '0']" -o "RSYNC_ENABLE=false" -n -b \
  -o "RSYNC_OPTS=&quot;" \
  -o "&quot;" -n \
  ${OMV_CONFIG_FILE} | xmlstarlet unesc > ${OMV_RSYNCD_DEFAULT}

# Create '/etc/rsyncd.conf' file
xmlstarlet sel -t -m "//services/rsync/server" \
  -v "concat('port = ',port)" -n \
  -o "use chroot = ${OMV_RSYNCD_USECHROOT}" -n \
  -i "string-length(extraoptions) > 0" -v extraoptions -n -b \
  ${OMV_CONFIG_FILE} | xmlstarlet unesc > ${OMV_RSYNCD_CONFIG}

# Remove all secrets files
rm -f ${OMV_RSYNCD_SECRETSFILE_DIR}/rsyncd-*.secrets

index=$(omv_config_get_count "//services/rsync/server/modules/module")
while [ ${index} -gt 0 ]
do
	name=$(omv_config_get "//services/rsync/server/modules/module[position()=${index}]/name")
	authusers=$(omv_config_get "//services/rsync/server/modules/module[position()=${index}]/authusers")
	secretsfile="${OMV_RSYNCD_SECRETSFILE_DIR}/rsyncd-${name}.secrets"

	# Get the shared folder reference and path
	sfref=$(omv_config_get "//services/rsync/server/modules/module[position()=${index}]/sharedfolderref")
	sfpath=$(omv_get_sharedfolder_path "${sfref}")
	# Specify a dot-dir in the module's path to indicate the point where the chroot should occur
	sfpath="${sfpath%/*}/./${sfpath##*/}"

	# Create module configuration
	xmlstarlet sel -t -m "//services/rsync/server/modules/module[position()=${index}]" \
	  -v "concat('[',name,']')" -n \
	  -i "string-length(comment) > 0" -v "concat('comment = ',comment)" -n -b \
	  -o "path = ${sfpath}" -n \
	  -v "concat('uid = ',uid)" -n \
	  -v "concat('gid = ',gid)" -n \
	  -i "list[. = '0']" -o "list = no" -n -b \
	  -i "list[. = '1']" -o "list = yes" -n -b \
	  -i "readonly[. = '0']" -o "read only = no" -n -b \
	  -i "readonly[. = '1']" -o "read only = yes" -n -b \
	  -i "writeonly[. = '0']" -o "write only = no" -n -b \
	  -i "writeonly[. = '1']" -o "write only = yes" -n -b \
	  -i "maxconnections[. != '0']" -v "concat('max connections = ',maxconnections)" -n -b \
	  -i "string-length(hostsallow) > 0" -v "concat('hosts allow = ',hostsallow)" -n -b \
	  -i "string-length(hostsdeny) > 0" -v "concat('hosts deny = ',hostsdeny)" -n -b \
	  -i "authusers[. = '1']" \
		-o "auth users = " \
		-m "users/user" \
		  -v "name" \
		  -i "position() != last()" -o "," -b \
		-b \
		-n \
		-o "secrets file = ${secretsfile}" -n \
	  -b \
	  -i "string-length(extraoptions) > 0" -v extraoptions -n -b \
	  ${OMV_CONFIG_FILE} | xmlstarlet unesc >> ${OMV_RSYNCD_CONFIG}

	# Create the secrets file containing the usernames and passwords
	if omv_checkyesno ${authusers}; then
		xmlstarlet sel -t -m "//services/rsync/server/modules/module[position()=${index}]/users/user" \
		  -v "concat(name,':',password)" -n \
		  ${OMV_CONFIG_FILE} | xmlstarlet unesc > ${secretsfile}
		chmod 600 ${secretsfile}
	fi

	index=$(( ${index} - 1 ))
done
