FreeNAS Code
This project has moved to github - see https://github.com/freenas
Brought to you by:
cochard,
mattolander
#!/bin/sh # Copyright (c) 2007-2008 Volker Theile (votdev@gmx.de) # All rights reserved. # PROVIDE: lighttpd # REQUIRE: htpasswd DAEMON # KEYWORD: shutdown . /etc/rc.subr . /etc/configxml.subr name="lighttpd" rcvar=`set_rcvar` # Custom commands start_precmd="create_conf" stop_postcmd=stop_postcmd restart_precmd="checkconfig" reload_precmd=reload_precmd reload_postcmd=reload_postcmd extra_commands="reload check" # Defaults lighttpd_enable=${lighttpd_enable:-"NO"} lighttpd_conf=${lighttpd_conf:-"/var/etc/lighttpd.conf"} lighttpd_certpem=${lighttpd_certpem:-"/var/etc/cert.pem"} lighttpd_docroot=${lighttpd_docroot:-"/usr/local/www"} command=/usr/local/sbin/lighttpd command_args="-f ${lighttpd_conf} -m /usr/local/lib/lighttpd" pidfile=/var/run/lighttpd.pid sig_reload="-INT" check_cmd="checkconfig" # Create symlink for lighttpd file upload if it doesn't exist. if [ ! -h "/var/tmp" ]; then ln -s /ftmp /var/tmp fi create_conf() { local _protocol _certificate _privatekey _tmpfile # Create lighttpd.conf file cat <<EOF > ${lighttpd_conf} server.modules = ( "mod_access", "mod_auth", "mod_cgi" ) server.document-root = "${lighttpd_docroot}" server.errorlog-use-syslog = "enable" server.event-handler = "freebsd-kqueue" index-file.names = ( "index.php" ) mimetype.assign = ( ".pdf" => "application/pdf", ".sig" => "application/pgp-signature", ".spl" => "application/futuresplash", ".class" => "application/octet-stream", ".ps" => "application/postscript", ".torrent" => "application/x-bittorrent", ".dvi" => "application/x-dvi", ".gz" => "application/x-gzip", ".pac" => "application/x-ns-proxy-autoconfig", ".swf" => "application/x-shockwave-flash", ".tar.gz" => "application/x-tgz", ".tgz" => "application/x-tgz", ".tar" => "application/x-tar", ".zip" => "application/zip", ".mp3" => "audio/mpeg", ".m3u" => "audio/x-mpegurl", ".wma" => "audio/x-ms-wma", ".wax" => "audio/x-ms-wax", ".ogg" => "application/ogg", ".wav" => "audio/x-wav", ".gif" => "image/gif", ".jpg" => "image/jpeg", ".jpeg" => "image/jpeg", ".png" => "image/png", ".xbm" => "image/x-xbitmap", ".xpm" => "image/x-xpixmap", ".xwd" => "image/x-xwindowdump", ".css" => "text/css", ".html" => "text/html", ".htm" => "text/html", ".js" => "text/javascript", ".asc" => "text/plain", ".c" => "text/plain", ".cpp" => "text/plain", ".log" => "text/plain", ".conf" => "text/plain", ".text" => "text/plain", ".txt" => "text/plain", ".dtd" => "text/xml", ".xml" => "text/xml", ".mpeg" => "video/mpeg", ".mpg" => "video/mpeg", ".mov" => "video/quicktime", ".qt" => "video/quicktime", ".avi" => "video/x-msvideo", ".asf" => "video/x-ms-asf", ".asx" => "video/x-ms-asf", ".wmv" => "video/x-ms-wmv", ".bz2" => "application/x-bzip", ".tbz" => "application/x-bzip-compressed-tar", ".tar.bz2" => "application/x-bzip-compressed-tar" ) url.access-deny = ( "~", ".inc" ) static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" ) server.pid-file = "${pidfile}" auth.backend = "htpasswd" auth.backend.htpasswd.userfile = "${lighttpd_docroot}/.htpasswd" cgi.assign = (".php" => "/usr/local/bin/php") EOF /usr/local/bin/xml sel -t \ -o "auth.require = ( "/" => (" -n \ -o " "method" => "basic"," -n \ -v "concat(' "realm" => "',//system/hostname,'",')" -n \ -o " "require" => "valid-user"" -n \ -o " )," -n \ -o ")" -n \ -i "string-length(//system/webgui/port) > 0" -v "concat('server.port = ',//system/webgui/port)" -n -b \ -i "//system/webgui/protocol[. = 'https']" \ -o "ssl.engine = "enable"" -n \ -o "ssl.pemfile = "${lighttpd_certpem}"" -n \ -b \ ${configxml_file} | /usr/local/bin/xml unesc >> ${lighttpd_conf} _protocol=`configxml_get "//system/webgui/protocol"` _certificate=`configxml_get "//system/webgui/certificate"` _privatekey=`configxml_get "//system/webgui/privatekey"` if [ "${_protocol}" = "https" ]; then # Create /var/etc/cert.pem file if [ -n "${_certificate}" -a -n "${_privatekey}" ]; then _tmpfile=/tmp/lighttpd$$.tmp echo "${_certificate}" > ${_tmpfile} /usr/bin/uudecode -m -p -r ${_tmpfile} > ${lighttpd_certpem} echo "" >> ${lighttpd_certpem} echo "${_privatekey}" > ${_tmpfile} /usr/bin/uudecode -m -p -r ${_tmpfile} >> ${lighttpd_certpem} /bin/rm -f ${_tmpfile} fi [ -e ${lighttpd_certpem} ] && /bin/chmod 0600 ${lighttpd_certpem} 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"