Recent Articles

Setting up a custom Weave 0.5 server

October 9th, 2009 by derek

Weave is a synchronization engine from Mozilla Labs for all your browser information (settings, history, bookmarks, etc, etc).  It is a free add-on for Firefox and you can freely use Mozilla’s servers  to store your information.  This guide, however, is for anyone interested in setting up their own secure Weave server.

There are instructions on the Mozilla Labs places, but they are a bit scattered and it took some digging before I could get things working. Hopefully this guide will give a simple single locations for anyone interested.

Assumptions

For this guide I will assume PHP 5.1+, a working Apache 2 server with SSL, and MySQL (SQLite can easily be substituted here, it should be obvious where.  As Mozilla notes, whatever web server you use, WebDAV can’t be enabled on that server.

Server Configuration

First things, grab the latest version of the Weave Server from Mozilla Labs at http://hg.mozilla.org/labs/weaveserver in whichever format you prefer.  Extract the files into a folder accessible by your web server.  The “server” folder will be the web root, so we can go ahead an setup a virtual server configuration similar to the following example (replace the paths accordingly):

<VirtualHost weave.my.domain:443>
 
ServerName weave.my.domain
DocumentRoot /var/www/weaveserver/server/
 
ErrorLog /var/log/apache2/weave-error.log
CustomLog /var/log/apache2/weave-access.log combined
 
SSLEngine on
SSLCertificateFile /path/to/server.cert.crt
SSLCertificateKeyFile /path/to/server.cert.key
 
<Directory "/var/www/weaveserver/server/">
 
Options Indexes FollowSymLinks
AllowOverride none
Order allow,deny
Allow from all
AuthType Basic
AuthName "Weave Server"
AuthUserFile /path/to/auth/file
require valid-user
 
</Directory>
 
Alias /0.5 /var/www/weaveserver/server/0.5/index.php
Alias /user/1 /var/www/weaveserver/server/user/1/index.php
 
</VirtualHost>

As you can see from this Apache config, there are some file that need to be generated. Specifically, an SSL Certificate and key, and a basic authentication file. I will run through these briefly below, if you are good with generating these, just skip down a bit.

Generating a Self-Signed SSL Certificate

First, generate a key:

openssl genrsa -des3 -out server.key 1024

Generate a certificate signing request (CSR):

openssl req -new -key server.key -out server.csr

Optional – At this point you can remove the password from the key if you wish… please be aware of the security risks before doing this:

cp server.key server.key.pass
openssl rsa -in server.key.pass -out server.key

Finally, generate the certificate:

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

Make sure the Apache Virtual Host configuration paths match the locations of the key and certificate files.

Creating a Password File

Generate an htpasswd file using the following command:

htpasswd -c weaveserver.pwd

You will be prompted to enter a password for the user you created.  Make sure to update the AuthUserFile path in the Apache Virtual Server config to point to this file.

Note: You need to have the same user name(s) and password(s) in the htpasswd file as you plan on having setup in the Weave user database table (covered later).

Database Setup

Create a new database and create the two tables as follows:

CREATE DATABASE WEAVE;
USE WEAVE;
 
CREATE TABLE `wbo` (
  `username` varbinary(32) NOT NULL default '',
  `collection` varbinary(64) NOT NULL default '',
  `id` varbinary(64) NOT NULL default '',
  `parentid` varbinary(64) default NULL,
  `predecessorid` varbinary(64) default NULL,
  `modified` decimal(12,2) default NULL,
  `sortindex` int(11) default NULL,
  `depth` tinyint(4) default NULL,
  `payload` longtext,
  `payload_size` int(11) default NULL,
  PRIMARY KEY  (`username`,`collection`,`id`),
  KEY `parentindex` (`username`,`collection`,`parentid`),
  KEY `modified` (`username`,`collection`,`modified`),
  KEY `weightindex` (`username`,`collection`,`sortindex`),
  KEY `predecessorindex` (`username`,`collection`,`predecessorid`),
  KEY `size_index` (`username`,`payload_size`)
) ENGINE=InnoDB;
 
CREATE TABLE `users`
(
 `username` varchar(32) PRIMARY KEY,
 `md5` varbinary(32),
 `email` varbinary(64),
 `status` tinyint,
 `location` text,
 `alert` text
) ENGINE=InnoDB;

That should be it for the database! Feel free to adjust the database name to your own liking, and be sure to grant select, select, insert, delete, and update permissions to whichever MySQL user you plan on accessing this with from weave. In case anyone forgot the grant sytax:

GRANT SELECT, INSERT, UPDATE, DELETE ON weave.* TO 'myuser'@'localhost';

If you are creating this user, be sure to set the password by adding ” IDENTIFIED BY ‘mypassword’ ” on the end of the above statement.

Weave Server Configuration

Navigate to the weaverserver/server/user/1 folder. Copy weave_user_constants.php.dist to weave_user_constants.php and set the following items:

define('WEAVE_STORAGE_ENGINE', 'mysql');
...
define('WEAVE_MYSQL_STORE_READ_HOST', '');
define('WEAVE_MYSQL_STORE_READ_DB', '');
define('WEAVE_MYSQL_STORE_READ_USER', '');
define('WEAVE_MYSQL_STORE_READ_PASS', '');
...
define('WEAVE_AUTH_ENGINE', 'mysql');
...
define('WEAVE_MYSQL_AUTH_HOST', '');
define('WEAVE_MYSQL_AUTH_DB', '');
define('WEAVE_MYSQL_AUTH_USER', '');
define('WEAVE_MYSQL_AUTH_PASS', '');
...
define('WEAVE_REGISTER_STORAGE_LOCATION', 'weave.my.domain')

These settings are pretty obvious once you read over the comments. The first group of settings is for the database that stores the synchronized data, and just sets the type and connection information. The second is basically the same thing, but points to the database that has the table of users to authenticate against. The final setting above only needs to be set if you are using the same database to house both the storage and authentication data.

Now do essentially the exact same steps for weaveserver/server/0.5/default_constants.php.dist. That is, copy it over to default_constants.php and then edit the similar sections to set the connection strings for the authentication and storage database(s). The only difference is that instead of specifying the ‘WEAVE_REGISTER_STORAGE_LOCATION’, which will not exist, set the following line if both the authentication and store tables are in the same database:

define('WEAVE_SHARE_DBH', '1');

If you are using different databases, just leave this one alone. Also, you can easily use Sqlite as well by setting the various ‘engine’ variables to ‘sqlite’ and configuring the path to the stores (this is pretty self explanatory once you read through the constants files).

CAPTCHA

If you want to use captcha when creating an account, you will need to grab a public and private key from http://recaptcha.net and make some changes to the Apache and Weaver Server configs. This is completely optional (just ignore the captcha field if you don’t set this up when creating a new user).
Back in weaveserver/server/user/1/weave_user_constants.php set the following to 1:

define('WEAVE_REGISTER_USE_CAPTCHA', 1);

Now copy weaveserver/server/misc/1/weave_misc_constants.php.dist to weave_misc_constants.php and add in your public and private keys. Finally, add an alias to your Apache config as follows:

Alias /misc/1/captcha_html /server/misc/1/captcha.php

Test it!

Now everything should be configured, so install the Weave add-on for Firefox if you haven’t already (http://labs.mozilla.com/weave/). At the time of writing this, the latest was 0.7.

Open a new tab in Firefox and type about:config and search for extensions.weave.clusterURL. Set this to your Weave Server URL (i.e. “https://weave.my.domain/”). Make sure to include the trailing slash.
Now open weave and go to the user creation screen. At the bottom of the form box there is an option that says “Create my account with:,” select “A custom Weave server” and input your server into the textbox below (i.e. – “https://weave.my.domain/”). Again here, make sure you include a trailing slash as the underlying code doesn’t check and append if needed.

If all goes well, you should be able to put in a username and have it tell you that it is available (if it says it is not available, and its not already in the database, check the log in the top right of the page under tools -> debug log to see what the problem is). If you didn’t setup captcha, just leave it blank and create your user once you filled in an email and password. Now enter your pass phrase and you should be set! Select what you would like to sync and how, then either wait or manually kick off a sync by going to “Signed in as ” -> “Sync now” in the upper right. If everything went correctly, your data should be sent over SSL and encrypted into your server’s database.

References

Weave Storage / Server Setup
Weave User Setup
Weave Page on Mozilla Labs
Creating a Self Signed SSL Certificate
htpasswd Apache Reference

Tags: , ,
Posted in Guides | 2 Comments »

Multiple Secure Subdomains with a Wildcard SSL Certificate

October 27th, 2008 by derek

This guide is a walk through for configuring multiple secure subdomains using a wildcard SSL certificate and Apache2 on Ubuntu 8.04.

For those not familiar with the problems for this setup, a little background is in order (if you want to get to the meat of things, just skip down). If you have your domain, mydomain.com, it is convenient to organize things into subdomains. For example, maybe you have code repositories at code.mydomain.com or a development environment for testing at test.mydomain.com. But what if you want both of these subdomains to be secure? A standard SSL certificate for mydomain.com does not validate for any of the subdomains since the addresses do not match. You could get separate certificates for each subdomain, but configuring those can be tricky. Wildcard SSL certificates allow a single certificate for any immediate subdomain of the base, i.e. *.mydomain.com.

So problem solved right? Well, not exactly… due to the SSL protocol you cannot use name-based virtual hosts in Apache with SSL. Since only IP-based virtual hosts can be used Apache requires a single Virtual Host for all port 443 (SSL) traffic per IP address. The way around this is to dynamically set the document root after receiving a request on port 443. It may sound complicated, but the steps are pretty easy once you see how it is done.

Generating the Wildcard Certificate

First off, you need a wildcard SSL certificate. You can purchase one commercially or just generate one yourself. If you want to roll your you have to have Apache with mod-ssl. (Note that if you are not running as root you will need to sudo all of the below commands):

apt-get install apache2 apache2-common

Now we can generate the certificate. First we generate a key:

openssl genrsa -out server.key 2048
chmod 400 server.key

Next we generate a certificate signing request (CSR). This is where we can designate the wildcard. Run the command below and you will be prompted with a series of questions. When promted for Common Name, you would normally enter your domain name. We want a wildcard so enter *.mydomain.com

, replacing mydomain.com with your domain name:

openssl req -new -key server.key -out server.csr

Finally, create the self-signed wildcard certificate (the below example is valid for a year; change the number of days to reflect how long you want the certificate to be valid):

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

Configuring Apache

First enable the Apache SSL module if it isn’t already:

a2enmod ssl

We also need to enable the vhost_alias module:

a2enmod vhost_alias

Apache needs to be told to listen on port 443. Edit the ports.conf file in /etc/apache2 and add the following:

Listen 443

Now we need a Virtual Host to handle all the port 443 traffic. I recommend creating and editing a new file in /etc/apache2/sites-available:

vi /etc/apache2/sites-available/ssl.mydomain.com

Now to configure the Virtual Host. Add the following to the newly created file:

ServerAdmin webmaster@localhost
ServerName *.mydomain.com
SSLEngine On
SSLCertificateFile /etc/apache2/ssl/apache.pem
SSLProtocol all
SSLCipherSuite HIGH:MEDIUM
VirtualDocumentRoot /var/www/%0/

A little explanation is in order here. The ServerName (and any ServerAlias as well) uses the wildcard notation (i.e. *.mydomain.com) since we want to handle multiple subdomains. Next are the directives to enable SSL and you will need to adjust the SSLCertificateFile path to point to your certificate. The last line is where the magic happens using the VirtualDocumentRoot directive. Thanks to the vhost_alias module, we can set the document root based on the address.

Say we have the two example subdomains from earlier, code.mydomain.com and test.mydomain.com. We have our wildcard certificate in place and someone tries to go to http://code.mydomain.com

. The %0 signifies that the entire string from the address should be substituted, yielding a DocumentRoot of /var/www/code.mydomain.com. If someone tried test.mydomain.com, it would become /var/www/test.mydomain.com. The documentation on the vhost_alias module shows all the options for getting parts or all of the address string for substituting into the DocumentRoot.

Using this method, you can dynamically determine what the root document path should be and serve the correct information. The only catch is that you will have to adhere to a naming scheme that follows however you design your rewrite rule (in my example above, it will be /var/www/<full subdomain path>. But being forced to use a consistent naming scheme for your sites (which should probably be done anyway…) is a pretty good tradeoff for running multiple secure subdomains on a single IP address with a single SSL certificate.

One last thing, make sure to reload Apache so the changes take effect:

/etc/init.d/apache force-reload

If you are looking for a good host that will give you full control over your server, be sure to check out Linode

. We use them for all our Linux hosting and love it!

Tags: , , , , ,
Posted in Guides | 5 Comments »

Installing MySQL gem on Windows & cygwin for Rails

October 26th, 2008 by matt

This post assumes that you’ve followed the Setting up Rails on Windows with Cygwin guide and are using Cygwin on Windows for your Rails development.

If you’re upgrading to Rails 2.2 (or running on edge), you’ll need to build the mysql gem from source, as it’s being removed from the Rails pacakge. You’ll know if you need to do this if you get the following error when building your app:

!!! The bundled mysql.rb driver has been removed from Rails 2.2. Please install the mysql gem and try again: gem install mysql.

Installing MySQL from source

First thing you’ll need to do is to download the source files from MySQL.

The next steps are all from the command line (and will probably take a while to complete!):

tar xzvf mysql-5.0.67.tar.gz
cd mysql-5.0.57
./configure
make
make install

UPDATE: As those who’ve commented here have noted, a common error you may come across while running make is:

readline/readline.h:70:29: sys/ttydefaults.h: No such file or directory

The easiest way to solve this issue is to download the readline packages from cygwin (using the cygwin installer) and running

./configure --without-readline CFLAGS=-O2

Instead of plain ./configure

Also note, if you’ve already run ./configure you’ll need to clean up the directory by running

make distclean

This will actually install the entire MySQL library, but we won’t be using it. We just needed the library files to build the gem with. Once MySQL is built, you just need to install the gem, and you’re good to go:

gem install mysql

Don’t forget to tell MySQL which configuration we want to load. By default, it’ll try to use a local socket, but we want it to use the server we installed in Windows (outside of cygwin). Check out the Getting Cygwin/Rails to work with MySQL section of our previous guide.

Tags: , , ,
Posted in Guides | 21 Comments »

Setting up Rails on Windows with Cygwin

October 2nd, 2008 by matt

Update: If you’re using Rails 2.2, you’ll need to perform some extra work to get MySQL working.

Getting Started

I like developing in Ruby on Rails, but I don’t own a Mac.  I’ve found that setting up a Rails development environment within Windows can get frustrating and cumbersome at times.  I’ve also found that using Cygwin helps to keep all of the Rails related libraries all in one easy to manage location.  OK, enough of the boring stuff, let’s open up that command prompt and get started!

But wait!  Before we begin, I have to talk about one more thing.  One of the frustrating things for me while I was learning Rails was watching all of these Rails screencasts and seeing everyone use Textmate.  Textmate is awesome for Rails development, but it’s not available for Windows (and will probably never be).  Luckily, Alexander Stigsen has been developing a great Textmate app for Windows called “e”. I use e (the text editor) almost everyday and it is great for development in Rails (other languages have good support as well).  One cool thing is that e relies on Cygwin for some of the bundles, so if you do install and use e, you’ll get Cygwin as part of the package.

If you don’t want to use e, that’s cool too.  Just download the Cygwin setup file and follow the same steps (just ignore any asides about setting up e).  Let’s roll.

Installing Cygwin

First things first and that’s installing Cygwin.  Either grab the Cygwin standalone setup, or grab and install e and get to the Cygwin setup screens.

Note to e users: e will setup and install Cygwin the first time you run e, not upon installation of e.  Also, make sure to select “manual” configuration of Cygwin instead of “automatic”.

Read the rest of this entry »

Tags: , , ,
Posted in Guides | 12 Comments »

Ubuntu 8.04 LTS 64-bit Server on Linode

August 7th, 2008 by derek

This guide is a step by step walk through for setting up an Ubuntu 8.04 LTS 64-bit server on Linode.

Assuming a Linode 360 with 12288 megs of space, partition as follows:

  • Ubuntu Image: 11776MB
  • Swap: 512

The default swap size is only 256MB, but the recommended standard is to use between 1 to 2 times the amount of RAM installed on the machine. A base Linode has 360MB of RAM, so 512 is a safe size to use.

Getting Started

First thing, grab your favorite text editor, such as nano or vi using aptitude or apt-get.

aptitude install nano

If you would like auto completion in interactive shells, edit your bash.bashrc file

nano /etc/bash.bashrc

and uncomment the following block to look as follows

[...]
# enable bash completion in interactive shells
if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
[...]

For some reason bash-completion is not installed by default

aptitude install bash-completion

There is no need to reboot, just restart bash and autocomplete should be working for interactive shells

bash

To try it out, you can do something like ‘aptitude inst<tab>’ and it should complete as ‘aptitude install’

Read the rest of this entry »

Tags: , ,
Posted in Guides | 1 Comment »