Jan 21, 2011

MacBook Air に vtun 導入

某所で vtun を利用した vpn サーバを構築しているので、 MacBook Air に vtun クライアントを導入して vpn 接続出来る様にする。 vtun ver.3 は MacPort からも導入できるのだが、 諸処の事情により接続先のサーバが vtun ver.2 で運用されているので 今回は MacPort ではなく手作業にてインストールする事にする。

vtun に必要な圧縮関係の lzo ライブラリは MacPort でも導入できるのだが、 依存関係が多く古い perl までインストール要求するので 今回はこちらも手作業にて導入する。 vtun が依存しているのは lzo の 1.0 系なので、 LZO のオフィシャルサイト から lzo 1.0 系の最新版である lzo-1.8.tar.gz をダウンロードして展開し、 MacPort の流儀に合わせて --prefix=/opt/local を指定、 共有ライブラリを有効にするために --enable-shared を指定して configure を実行し make する。

$ ./configure --prefix=/opt/local --enable-shared
$ make
$ sudo make install clean
    

次に vtun ver.2 のソースを Sourceforge からダウンローし展開する。
vtun ver.2 のソースはそのままでは Mac 上ではコンパイルできないので 以下のパッチを適用する。

diff -cr vtun.orig/auth.c vtun/auth.c
*** vtun.orig/auth.c	2002-12-17 06:40:44.000000000 +0900
--- vtun/auth.c	2011-01-20 18:22:49.000000000 +0900
***************
*** 66,73 ****
  #include <openssl/blowfish.h>
  #include <openssl/rand.h>
  #else /* YAY - We're MAC OS */
! #include <sys/md5.h>
! #include <crypto/blowfish.h>
  #endif  /* __APPLE_CC__ */
  
  void gen_chal(char *buf)
--- 66,73 ----
  #include <openssl/blowfish.h>
  #include <openssl/rand.h>
  #else /* YAY - We're MAC OS */
! #include <openssl/md5.h>
! #include <openssl/blowfish.h>
  #endif  /* __APPLE_CC__ */
  
  void gen_chal(char *buf)
diff -cr vtun.orig/lfd_encrypt.c vtun/lfd_encrypt.c
*** vtun.orig/lfd_encrypt.c	2002-12-17 06:40:46.000000000 +0900
--- vtun/lfd_encrypt.c	2011-01-20 18:23:21.000000000 +0900
***************
*** 53,60 ****
  #include <openssl/md5.h>
  #include <openssl/blowfish.h>
  #else /* YAY - We're MAC OS */
! #include <sys/md5.h>
! #include <crypto/blowfish.h>
  #endif  /* __APPLE_CC__ */
  
  #define ENC_BUF_SIZE VTUN_FRAME_SIZE + 16 
--- 53,60 ----
  #include <openssl/md5.h>
  #include <openssl/blowfish.h>
  #else /* YAY - We're MAC OS */
! #include <openssl/md5.h>
! #include <openssl/blowfish.h>
  #endif  /* __APPLE_CC__ */
  
  #define ENC_BUF_SIZE VTUN_FRAME_SIZE + 16 
  	
パッチ適用後、これも --prefix=/opt/local を指定、 導入した lzo 関係のパスを指定するために --with-lzo-headers--with-lzo-lib をそれぞれ指定して configure を実行し make する。

$ ./configure --prefix=/opt/local --with-lzo-include=/opt/local/include --with-lzo-lib=/opt/local/lib
$ make
$ sudo make install clean
    

これで vtun はインストールできたが、 Mac OS X は標準では vtun が利用するトンネルデバイス /dev/tunX が存在しないので Sourceforge から Mac OS X 用の TUN/TAP ドライバをダウンロードしてインストールする。
インストール後にシステムを再起動するか /Library/Extensions/tun.kext をロードする事で /dev/tunX が生成されるので、 /opt/local/sbin/vtund を実行して vpn 接続できるか確認する。

$ sudo kextload /Library/Extensions/tun.kext
$ sudo /opt/local/sbin/vtund -f /opt/local/etc/vtund.conf ホスト サーバアドレス
$ ifconfig tunX
tunX: flags=8851<UP,POINTOPOINT,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    inet XXX.XXX.XXX.XXX --> YYY.YYY.YYY.YYY netmask 0xff000000 
    open (pid PPP)
    

vpn 接続ができればこの様なスクリプトを作成して root 権限で実行すると vpn 接続の開始・終了が出来る様になる。

  1#!/bin/sh
  2base="/opt/local"
  3
  4vtund="${base}/sbin/vtund"
  5conf="${base}/etc/vtund.conf"
  6opt="ホスト サーバアドレス
  7
  8start()
  9{
 10
 11    test -x ${vtund} -a -f ${conf} && ${vtund} -f ${conf} ${opt}
 12
 13}
 14
 15stop()
 16{
 17
 18    ps ax | sed -n '/[v]tund/s/ *\([0-9]*\) .*/\1/p' | xargs kill
 19
 20}
 21
 22case $1 in
 23    start )
 24        start;;
 25    stop )
 26        stop;;
 27    restart )
 28        stop && start;;
 29    * )
 30        echo "Usage: ${0##*/} [start][stop][restart]" 1>&2
 31        exit 255;;
 32esac
    

Edit this entry...

wikieditish message: Ready to edit this entry.
















A quick preview will be rendered here when you click "Preview" button.