Menu

[r8528]: / experimental / webdav / webdav  Maximize  Restore  History

Download this file

158 lines (130 with data), 3.9 kB

#!/bin/sh
# Copyright (c) 2008 Volker Theile (votdev@gmx.de)
# All rights reserved.

# PROVIDE: webdav
# REQUIRE: DAEMON
# KEYWORD: shutdown
# XQUERY: -i "count(//webdav/enable) > 0" -o "0" -b
# RCVAR: webdav

. /etc/rc.subr
. /etc/configxml.subr
. /etc/util.subr

name="webdav"
rcvar=`set_rcvar`

# Custom commands
start_precmd="mkconf_cmd"
stop_postcmd=stop_postcmd
restart_precmd="checkconfig"
reload_precmd=reload_precmd
reload_postcmd=reload_postcmd
mkpasswd_cmd="mkpasswd_cmd"
mkconf_cmd="mkconf_cmd"
extra_commands="reload check mkconf mkpasswd"

# Defaults
webdav_enable=${webdav_enable:-"NO"}
webdav_conf=${webdav_conf:-"/var/etc/webdav.conf"}
webdav_certpem=${webdav_certpem:-"/var/etc/webdavcert.pem"}
webdav_htpasswdfile=${webdav_htpasswdfile:-".webdav_htpasswd"}
webdav_htpasswd=${webdav_htpasswd:-"/var/run/${webdav_htpasswdfile}"}
command=/usr/local/sbin/lighttpd
command_args="-f ${webdav_conf} -m /usr/local/lib/lighttpd"
pidfile=/var/run/webdav.pid
sig_reload="-INT"
check_cmd="checkconfig"

mkpasswd_cmd()
{
	echo "Generating ${webdav_htpasswdfile}."

	# Generate the htpasswd file used by websrv.
	/bin/cat /dev/null > ${webdav_htpasswd}

	# Add configured users. Parse /etc/master.passwd to get password.
	# Did not find a better solution. Using PHP crypt fails when calling via WebGUI.
	/usr/local/bin/xml sel -t -m "//access/user" \
		-v login \
		-i "position() != last()" -n -b \
		${configxml_file} | /usr/local/bin/xml unesc | \
		while read _login; do
			_password=`/bin/cat /etc/master.passwd | /usr/bin/grep "^${_login}:*" | /usr/bin/awk '{split($0,a,":"); print a[2]}'`
			echo "${_login}:${_password}" >> ${webdav_htpasswd}
		done

	/bin/chmod 0600 ${webdav_htpasswd}
}

mkconf_cmd()
{
	local _protocol _certificate _privatekey _documentroot

	# Get configuration data
	_protocol=`configxml_get "//webdav/protocol"`
	_certificate=`configxml_get "//webdav/certificate"`
	_privatekey=`configxml_get "//webdav/privatekey"`
	_documentroot=`configxml_get "//webdav/documentroot"`

	# Create .conf file
	/usr/local/bin/xml sel -t -m "//webdav" \
		-v "concat('server.port = ',port)" -n \
		${configxml_file} | /usr/local/bin/xml unesc > ${webdav_conf}

	cat <<EOF >> ${webdav_conf}
server.document-root = "${_documentroot}"
server.modules = ( "mod_webdav" )
server.errorlog-use-syslog = "enable"
server.event-handler = "freebsd-kqueue"
server.pid-file = "${pidfile}"

webdav.activate = "enable"
webdav.is-readonly = "enable"

\$HTTP["url"] =~ "^/dav(\$|/)" {
  webdav.activate = "enable"
  webdav.is-readonly = "enable"
  webdav.sqlite-db-name = "/var/run/lighttpd/lighttpd.webdav_lock.db"
}

EOF

	# Configure authentication.
	if configxml_isset //webdav/authentication/enable; then
	  mkpasswd_cmd

		# Add authentication configuration.
		cat <<EOF >> ${websrv_conf}
auth.backend = "htpasswd"
auth.backend.htpasswd.userfile = "${webdav_htpasswd}"
auth.require = ( "/" =>
	(
		"method"  => "basic",
		"realm"   => "Websrv",
		"require" => "valid-user"
	),
)
EOF
	fi

	if [ "${_protocol}" = "https" ]; then
		cat <<EOF >> ${websrv_conf}
ssl.engine = "enable"
ssl.pemfile = "${websrv_certpem}"
EOF

		# Create .pem file (required)
		if [ -n "${_certificate}" -a -n "${_privatekey}" ]; then
			_tmpfile=/tmp/websrv$$.tmp

			echo "${_certificate}" > ${_tmpfile}
			/usr/bin/uudecode -m -p -r ${_tmpfile} > ${websrv_certpem}
			echo "" >> ${websrv_certpem}
			echo "${_privatekey}" > ${_tmpfile}
			/usr/bin/uudecode -m -p -r ${_tmpfile} >> ${websrv_certpem}
			/bin/chmod 0600 ${websrv_certpem}

			/bin/rm -f ${_tmpfile}
		fi
	fi
}

checkconfig()
{
	echo "Performing sanity check on ${name} configuration:"
	eval "${command} ${command_args} -t"
}

stop_postcmd()
{
	rm -f ${pidfile}
}

reload_precmd()
{
	echo "Stopping ${name} and starting gracefully."
}

reload_postcmd()
{
	rm -f ${pidfile}
	run_rc_command start
}

load_rc_config ${name}
run_rc_command "$1"
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.