This quick guide will get you set up with a secured simple WebDAV server using Ruby and Rack. The idea is to make your Dropbox accessible through WebDAV. This enables secure saving of documents from the iPad iWork apps back into your Dropbox account.
The script was inspired by Billy He’s RackDAV Thin script and improved to add self-signed certificates for an encrypted connection.
This guide was written with Mac OS X in mind, but with a few changes should work on any *nix-like system. Ruby and RubyGems are assumed to be already installed.
sudo gem install rack rack_dav
Create self-signed certificates
cd ~/Dropbox mkdir .certs cd .certs/
openssl genrsa -des3 -out server.key 1024
openssl req -new -key server.key -out server.csr
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
Remove the unnecessary PEM password unless you’re really paranoid:
cp server.key server.key.org openssl rsa -in server.key.org -out server.key
The RackDAV script
Here’s the RackDAV script which you could save as rackdav.rb in your Dropbox folder. Replace all instances of USERNAME and PASSWORD as needed.
#!/usr/bin/env ruby require 'rubygems' require 'rack' require 'rack_dav' require 'webrick' require 'webrick/https' require 'openssl' pkey = OpenSSL::PKey::RSA.new(File.open("/Users/USERNAME/Dropbox/.certs/server.key").read) cert = OpenSSL::X509::Certificate.new(File.open("/Users/USERNAME/Dropbox/.certs/server.crt").read) app = Rack::Builder.new do use Rack::ShowExceptions use Rack::CommonLogger use Rack::Reloader use Rack::Lint use Rack::Auth::Basic do |username, password| [username, password] === ['USERNAME', 'PASSWORD'] end run RackDAV::Handler.new(:root => '/Users/USERNAME/Dropbox') end.to_app Rack::Handler::WEBrick.run(app, :Port => 1024, :SSLEnable => true, :SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE, :SSLCertificate => cert, :SSLPrivateKey => pkey, :SSLCertName => [["CN",WEBrick::Utils::getservername]])
Source: This script was inspired by the one found at http://surrealtime.tumblr.com/dropbox_webdav.
Make sure it works and runs:
chmod +x rackdav.rb
Tip: To terminate the webrick server, try ctrl+alt+c.
Starting on system boot using launchd
Edit and save this plist file in /Library/LaunchDaemons/org.YOURNAME.RackDAV.plist and don’t foreget to change ALL CAP items like USERNAME.
< ?xml version="1.0" encoding="UTF-8"?> < !DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>ExitTimeOut</key> <integer>150</integer> <key>HopefullyExitsFirst</key> <true /> <key>Label</key> <string>com.YOURNAME.RackDAV</string> <key>UserName</key> <string>USERNAME</string> <key>ProgramArguments</key> <array> <string>/usr/bin/ruby</string> <string>/Users/USERNAME/Dropbox/rackdav.rb</string> </array> <key>RunAtLoad</key> <true /> </dict> </plist>
You can load and unload this daemon without restarting using the following commands:
sudo launchctl load -w /Library/LaunchDaemons/org.YOURNAME.RackDAV.plist
sudo launchctl unload -w /Library/LaunchDaemons/org.YOURNAME.RackDAV.plist