Firefox Sync 用に独自サーバーを構築 のバックアップソース(No.4)

更新

[[公開メモ]]

#contents

* 概要 [#q8e005f8]

Firefox Sync を安心して利用するため、
自宅の Debian マシンに独自サーバーを建てました。

** Firefox Sync とは [#j07531ea]

Firefox の様々な設定を、PC 
内部ではなくインターネット上のサーバーに保存することで、
異なる PC にインストールした Firefox 間で情報を共有することを
目的とした機能だそうです。

http://mozilla.jp/firefox/sync/

によれば、

-Bookmark
-History
-Passwords
-Preferences
-Tabs

が共有されるそうです。

この機能、
Firefox 4 からは標準装備になるそうですが、
Firefox 3 については add-on として同等の機能が提供されています。

以下は [Firefox 3.6.11] + [Firefox Sync 1.5] で試した結果です。
** データ保管サーバについて [#e3d39c15]

情報を保存するためのサーバーは Mozilla Project が提供してくれているので、
何も気にしなければ無料でアカウントを取って、すぐに利用できます。

** セキュリティーについて [#c3130b9e]

ただ、Firefox には頻繁に訪れるサイトのアカウント情報なども
保存されていたりするので、その内容を見ず知らずのサーバーに
アップロードするのはセキュリティ上どうなの?

という不安に駆られます。

この点について、
Firefox Sync ではすべての情報をローカルで暗号化してからサーバーに送り、
サーバーから送られてきたデータをローカルで復号する形になっているため、
サーバー運営者であっても暗号化する前の情報を読むことはできないそうです。

** 独自サーバー [#l9ef58ad]

それでもやはり見ず知らずのサーバーに送るのは気持ちが悪い。

という人は、自分でサーバーを立ち上げて、
そこにデータを保管することもできるそうです。

プロトコルは http あるいは https ベースなので、
自分の管理する Web サーバーに php で書かれた
スクリプトをインストールするだけで簡単にサーバーを建てられます。

** ということで [#pd28238b]

自分でサーバーを建てよう、とやってみたのが以下の内容です。

* サーバーとなる php スクリプトをダウンロード [#d310b72c]

サーバースクリプトは、

http://tobyelliott.wordpress.com/2010/01/29/weave-server-1-0-official-release/

を参考に、

http://people.mozilla.org/~telliott/weave_minimal.tgz

を落としてきました。

Firefox Sync は以前 Weave Sync と呼ばれていたそうで、
その名残がスクリプト名として残っています。

 LANG:console
 ~$ wget http://people.mozilla.org/~telliott/weave_minimal.tgz
   => weave_minimal.tgz
 ~$ tar fxz weave_minimal.tgz
 ~$ ls
   weave_minimal/    weave_minimal.tgz
 ~$ mv weave_minimal/ weave/
 ~$ cd weave/
 ~/weave$ ls
   README       index.php               weave_storage.php
   create_user  weave_basic_object.php  
 ~/weave$ cat README
  SERVER SETUP
  
  Add the following line to your apache config:
  
  Alias /weave /<path to this folder>/index.php
  
  Restart your apache server. Point your browser at 
  https://localhost/weave/1.0/blah/info/collection. 
  Enter "blah" for the username and garbage for the 
  pwd. Auth will fail, but it will create the db 
  (you can cancel the subsequent request for auth). 
  You should now see a file called weave_db in the 
  directory.
  
  You can create and delete users by running the 
  create_user script.
  
  CLIENT SETUP
  
  in about.config, set extensions.weave.serverURL to 
  https://<your servername>/weave/
  
  You can run it under http, but this is insecure and 
  not recommended.

* weave フォルダを /var/www へ移動 [#ydc8aa2e]

ここでは自分で管理しているサーバーなので、
フォルダをドキュメントルートに持ってっちゃいます。
[[public_html 下で動かす方法はこちらです。>#z5113595]]

フォルダの移動時にフォルダもろとも owner を www-data にします。
これを忘れると、http://blog.zuzara.com/2008/02/15/248/
のようなエラーに悩まされるので注意が必要です。
え? はい、もちろん悩みましたとも(涙

 LANG:console
 ~/weave$ cd
 ~$ sudo mv weave /var/www
 ~$ cd /var/www
 /var/www$ sudo chown -R www-data:www-data weave

* apache に alias を設定 [#o5ee983c]

weave_minimal の README にあったとおり、

http://server-name.jp/weave/foo/bar へのアクセスを~
http://server-name.jp/weave/index.php/foo/bar と解釈するための alias 
を設定し、apache2 を再起動します。

 LANG:console
 /var/www$ sudo jed /etc/apache2/site-enabled/default
   ...
   
   Alias /weave /var/www/weave/index.php
   ...
 /var/www$ /etc/init.d/apache2 restart

* アクセスしてみる [#m8afec6d]

weave_minimal の README にあったとおり ( s/collection/collections/ が必要ですが)、

エラーになることを前提に、https://localhost/weave/1.0/blah/info/collections 
へアクセスしてみると、weave_db というファイルが生成されるはず。

だめな場合は、httpd デーモンに weave フォルダへの書き込み権限がないか、
apache2, php, json, sqlite3 等のパッケージが足りないかのどちらかだと思います。

前者については、weave フォルダが www-data の所有になっていて、
なおかつ所有者が書き込み可能であることを確認すれば OK です。

後者は・・・私の環境ではすでに入っていたので、正確に何と何が必要かはよくわかりません。
悪しからず。

 LANG:console
 /var/www$ wget http://localhost/weave/1.0/blah/info/collections
  localhost をDNSに問いあわせています... 127.0.0.1, ::1
  localhost|127.0.0.1|:80 に接続しています... 接続しました。
  HTTP による接続要求を送信しました、応答を待っています... 401 Unauthorized
  認証に失敗しました。
 /var/www$ ls weave/
  README       index.php               weave_db
  create_user  weave_basic_object.php  weave_storage.php

* ユーザーを登録する [#pf11774d]

weave フォルダの create_user スクリプトを使います。

登録したら、http://localhost/weave/1.0/(ユーザー名)/info/collections 
へ問い合わせると、ちゃんと空の配列が返るはずです。

 LANG:console
 /var/www$ cd weave
 /var/www/weave$ sudo php ./create_user
  (c)reate, (d)elete or change (p)assword: c
  Please enter username: osamu
  Please enter password:
  osamu created
 /var/www/weave$ wget -q -O -               \
                      --auth-no-challenge   \
                      --http-user=osamu     \
                      --http-password=****  \
                      http://localhost/weave/1.0/osamu/info/collections
  []

* Firefox で動作確認 [#a50c9347]

[ツール(T)]-[オプション(O)] で出てくるダイアログに [Sync] という項目があり、
これを選ぶと [Set Up Firefox Sync(S)] のボタンが現れます。

これを押して、[I Have a Firefox Sync Account] から、~
[Server] を [Use a custom server] として、~
サーバー名として http://server-name.jp/weave/ を入れ、~
先に登録したユーザー名&パスワードを入力します。~

次に聞かれる Sync Key は、サーバーに送る前にデータを
暗号化する際に用いる秘密鍵なので、サーバーへのログイン用
パスワードとは異なるものを別途指定します。

これで、Sync が可能なはずです。

* public_html 下で動かす場合 [#z5113595]

自分で管理しているサーバーでない場合には、
weave フォルダをドキュメントルートに置いたり、
apache に Alias を設定したりできないので、
自分のホームディレクトリの下の public_html 
でスクリプトを動作させることになります。

注意点としては、
+ http デーモンが weave ディレクトリと weave_db の両方に書き込み権限を持っていなければならないこと
+ weave_db を http デーモンに作成されてしまうと自分の権限で書き換えられなくなってしまうので、weave_db は自分のアカウントで作らなければならないこと

が挙げられます。

このせいで、セキュリティ的にはガタガタになります。
覚悟の上で使うよりも、あきらめて Mozzila のサーバーを使うのが正しい選択かもしれません。

手順は以下のようになります。

 LANG:console
 ~$ wget http://people.mozilla.org/~telliott/weave_minimal.tgz
 ~$ tar fxz weave_minimal.tgz
 ~$ mv weave_minimal weave
 ~$ cd weave
 ~/weave$ ls
   README       index.php               weave_storage.php
   create_user  weave_basic_object.php  

この時点で、ユーザーを登録するとともに weave_db を作成してしまいます。

上でしたように httpd に weave_db を作らせると
所有権が www-data:www-data になるため、
後から自分でいじれなくなってしまいます。
それを回避するのがこの手順です。

 LANG:console
 ~/weave$ php create_user
  (c)reate, (d)elete or change (p)assword: c
  Please enter username: osamu
  Please enter password:
  osamu created

そして、weave ディレクトリを public_html に移動して、
パーミッションを調整します。

 LANG:console
 ~/weave$ cd
 ~$ mv weave public_html/
 ~$ cd public_html
 ~public_html$ chmod a+w weave
 ~public_html$ cd weave
 ~public_html/weave$ chmod a+w weave_db

繰り返しになりますが、php から sqlite を使う場合、
weave_db だけでなく、
weave_db のあるディレクトリにも書き込み権限が必要なため、
このような形にせざるを得ません。

お気づきの通り、これだとサーバーにアカウントを持つ人なら誰でも 
weave ディレクトリのファイルを書き換えることができてしまうので、
同じサーバーに信用のおけないユーザーがいる場合には
(たとえばレンタルサーバーを使うような場合には)
セキュリティ的に非常に危険です。

悪意を持ったユーザーがいた場合、
Sync Key を盗むことはどうやってもできないものの、
index.php を書き換えてユーザー名とパスワードを盗むことは
かなり容易に行えます。index.php に書き込み権限が無くても、
一旦消去して、再度生成すれば書き換えが可能であることに
注意してください。まぁ、それだと誰が書き換えたかは一目瞭然になって
しまうので、いたずら半分でやられることは少なそうですが・・・

根本的な解決にはなりませんが、
weave/weave_db を weave/db/weave_db に移して、
weave/ ではなく weave/db/ のみに書き込み権限を与えるくらいは
した方が良いかもしれません。

その場合には、weave_storage.php の
 $db_name = 'weave_db';
の行を
 $db_name = 'db/weave_db';
と変えるだけで・・・いいのかな???

こうすることで、php ファイルを書き換えることができなくなり、
多少は安全になるはずです。
少なくとも簡単にパスワードを抜かれる心配は無くなりますので。

もちろん wave_db は読み書きし放題なわけで、
セキュリティ的には大きな不満が残ります。
まあ、Sync Key が抜かれない限り最悪でもデータを失うだけなので、、、
その場合にはあきらめるしかないですね。

で、weave ディレクトリのパーミッションが適切に設定できたら、~
http://localhost/~osamu/weave/index.php/1.0/osamu/info/collections ~
へアクセスして、スクリプトが正しく動くことを確認します。

 LANG:console
 ~$ wget -q -O -               \
         --auth-no-challenge   \
         --http-user=osamu     \
         --http-password=****  \
         http://localhost/~osamu/weave/index.php/1.0/osamu/info/collections
  []

/~osamu/weave の後ろに index.php/ を入れなければならないことに注意してください。
これが alias あるいは rewrite を使えないことの代償です。

もしサーバーの設定で rewrite が使えるようになっていれば .htaccess 
による設定で URL を適切に書き換えれば、アクセスする際の index.php 
を省略することもできることが次の URL で紹介されています。

http://rabenau.org/blog/posts/creating-your-own-firefox-sync-server/42

その場合には index.php 中の $path を求める部分で REQUEST_URI 
環境変数から適切な部分を上手に切り出す必要があります。

ただ、上記ページの rewrite 設定はあまりうまくないようで、実際には

public_html/weave/.htaccess を

 RewriteEngine On
 RewriteBase /~osamu/weave/
 RewriteRule .* index.php

としました。また、index.php は

 LANG:php
 else if (!empty($_SERVER['REQUEST_URI'])) {
     $path = substr($_SERVER['REQUEST_URI'], 13);
 }

とすることでうまく動作しました。

このマジックナンバー 13 は、'/~osamu/weave' の文字数です。

Firefox Sync の設定では、rewrite を使わない場合には、~
http://server-name.jp/~osamu/weave/index.php/ を、~
rewrite を使う場合には、~
http://server-name.jp/~osamu/weave/ を、~
指定します。

変わるのはここだけなので、個人的には苦労してまで rewrite 
する必要はないと思います。

* https 接続にする [#ca9853a5]

どうせ内容が暗号化されているので http 接続でも良いという
割り切り方もあるのですが、
念のため https で繋ぐにはどうするかを調べました。

Debian で apache2 に https 接続をさせるには、
apache2 の ssl というモジュールを有効にすれば良いみたいです。

レンタルサーバーの場合には、サーバーの設定が https を許していれば
何もしないでそのまま使えますが、
そうでなければユーザーレベルでは ssl を有効にするのは無理だと思います。

apache2 のモジュールを有効にするには a2enmod を使います。

 LANG:console
 /etc/apache2$ sudo a2enmod ssl
   Enabling module ssl.
   See /usr/share/doc/apache2.2-common/README.Debian.gz on how to configure SSL 
   and create self-signed certificates.
   Run '/etc/init.d/apache2 restart' to activate new configuration!
 /etc/apache2$ gunzip -c /usr/share/doc/apache2.2-common/README.Debian.gz | less
   SSL
   ===
   
   Enabling SSL
   ------------
   
   To enable SSL, type (as user root):
   
           a2ensite default-ssl
           a2enmod ssl
   
   If you want to use self-signed certificates, you should install the ssl-cert
   package (see below). Otherwise, just adjust the SSLCertificateFile and
   SSLCertificateKeyFile directives in /etc/apache2/sites-available/default-ssl to
   point to your SSL certificate. Then restart apache:
   
           /etc/init.d/apache2 restart
   
   
   Creating self-signed certificates
   ---------------------------------
   
   If you install the ssl-cert package, a self-signed certificate will be
   automatically created using the hostname currently configured on your computer.
   You can recreate that certificate (e.g. after you have changed /etc/hosts or
   DNS to give the correct hostname) as user root with:
   
           make-ssl-cert generate-default-snakeoil --force-overwrite
   
   To create more certificates with different host names, you can use
   
           make-ssl-cert /usr/share/ssl-cert/ssleay.cnf /path/to/cert-file.crt
   
   This will ask you for the hostname and place both SSL key and certificate in
   the file /path/to/cert-file.crt . Use this file with the SSLCertificateFile
   directive in the apache config (you don't need the SSLCertificateKeyFile in
   this case).
   
   SSL workaround for MSIE
   -----------------------
   
   The SSL workaround for MS Internet Explorer needs to be added to your SSL
   VirtualHost section (it was previously in ssl.conf but caused keepalive to be
   disabled even for non-SSL connections):
   
   SetEnvIf User-Agent ".*MSIE.*"  nokeepalive ssl-unclean-shutdown \
                                   downgrade-1.0 force-response-1.0
   
   The default SSL virtual host in /etc/apache2/sites-available/default-ssl
   already contains this workaround.

かなり親切にやり方が書いてあるので、これをなぞる形で
サイト設定 default-ssl を有効にして、apache を再起動したところ、
エラーが出てしまいました。

/etc/ssl/private/ssl-cert-snakeoil.key が無いのがいけないようです。

 LANG:console
 /etc/apache2$ sudo a2ensite default-ssl
   Enabling site default-ssl.
   Run '/etc/init.d/apache2 reload' to activate new configuration!
 /etc/apache2$ /etc/init.d/apache2 reload
   Syntax error on line 52 of /etc/apache2/sites-enabled/default-ssl:
   SSLCertificateKeyFile: file '/etc/ssl/private/ssl-cert-snakeoil.key' 
   does not exist or is empty
   failed!
 /etc/apache2$ head -52 /etc/apache2/sites-enabled/default-ssl
         ...
         
         SSLCertificateFile    /etc/ssl/certs/ssl-cert-snakeoil.pem
         SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key

上の README によれば、ssl-cert パッケージが入っていれば自動で作成される
はずなのですが、なぜかできてませんでした。

仕方がないので、README の言うとおりに手動で作成したところ、
ちゃんと apache2 が立ちあがりました。

 LANG:console
 /etc/apache2$ sudo aptitude show ssl-cert
   パッケージ: ssl-cert
   状態: インストール済み
   自動的にインストールされた: yes
   バージョン: 1.0.23
   優先度: 任意
   セクション: utils
   メンテナ: Debian Apache Maintainers <debian-apache@lists.debian.org>
   展開サイズ: 98.3k
   依存: debconf (>= 0.5) | debconf-2.0, openssl (>= 0.9.8g-9), adduser, 
         openssl-blacklist
   説明: simple debconf wrapper for OpenSSL
    This package enables unattended installs of packages that need to create 
    SSL certificates.
   
    It is a simple wrapper for OpenSSL's certificate request utility that 
    feeds it with the correct user variables.
   
 /etc/apache2$ sudo make-ssl-cert generate-default-snakeoil
 /etc/apache2$  sudo ls /etc/ssl/private/
   ssl-cert-snakeoil.key
 /etc/apache2$ sudo /etc/init.d/apache2 restart

あとは、default-ssl にも上記と同様の Alias 設定をして、
Firefox Sync の設定でサーバー名を http から https に書き換えれば、
サーバーとの間を安全に通信することができるようになります。

* コメント [#u914a735]

#article_kcaptcha

Counter: 35505 (from 2010/06/03), today: 1, yesterday: 2