Monthly Archives: 10月 2006

links for 2006-10-31


links for 2006-10-30


[memo] GentooでUPS(APC ES 500)を使う


APC ES 500

Amazonで\7,000ちょっとという値段だったので、購入したのが届いた。流石に24時間稼働だとUPSがないと怖い。今年の雷で瞬断も多くあったので。付属のPowerCuteはWindowsとMacOS Xと商用Linuxにしか対応していないので自力でどうにかする方法をメモ。

準備と接続、認識確認

emerge apcupsd

なにはなくともコレがないと始まらない。とりあえずインストールしたら一旦シャットダウンしてコンセントをUPSにつなぎ変え、付属のUSB通信ケーブルでUPSとサーバを接続して再起動。再起動したらdmesgを見てサーバがES500を認識しているか確認。

usbcore: registered new driver hiddev
hiddev0: USB HID v1.10 Device [APC APC ES 500 FW:803.p6.A USB FW:p6] on usb-0000:00:10.2-2

上記のようなメッセージがあれば問題ない。が、

es500 hid device not claimed

とか出てたら認識していない。俺の場合は思い切り認識していなかった。要するにまたカーネル再構築。だがもう手慣れたモンなのでサクサクと再構築する。こういうときはマシンパワーがあると非常に楽だ。お約束の以下の呪文を唱える。

cd /usr/src/linux
make menuconfig

そして今の設定をLOADして、以下のところにチェックを付けて保存。

Device Drivers --->
    USB Support --->
        /dev/hiddev raw HID device support

またお約束の呪文。

make && make modules_install
cp arch/x86_64/boot/bzImage /boot/なんか適当な名前

そしたら/boot/grub/grub.confを編集して再起動すればいい。今度はちゃんと認識されているはず。念のためapcaccess statusとやってエラーが出ないことを確認する。

設定と動作確認

次はapcupsdの設定を行う。

cat /etc/apcupsd/apcupsd.conf
UPSCABLE usb
UPSTYPE usb
DEVICE
LOCKFILE /var/lock
ONBATTERYDELAY 6
BATTERYLEVEL 5
MINUTES 3
TIMEOUT 10
ANNOY 300
ANNOYDELAY 60
NOLOGON disable
KILLDELAY 0
NETSERVER on
NISIP 0.0.0.0
NISPORT 3551
EVENTSFILE /var/log/apcupsd.events
EVENTSFILEMAX 10
UPSCLASS standalone
UPSMODE disable
STATTIME 0
STATFILE /var/log/apcupsd.status
LOGSTATS off
DATATIME 0

殆どデフォルト。TIMEOUTに関しては動作確認のため10にした。これは10だと電源断から10秒でシャットダウンするということ。実際にapcupsdを動かして試してみる。

/etc/init.d/apcupsd start
rc-update add apcupsd default

思い切ってUPSのコンセントをぶっこ抜いてみる。ピーピーピーと警告音が鳴り、10秒後にシャットダウン処理が走れば成功。忘れないようにTIMEOUTは0にしておくこと。ここを0にしておくとUPS側のバッテリーで頑張ってくれるようになる。どのくらい頑張るかはBATTERYLEVELとMINUTESで決まる。BATTERYLEVELはバッテリ残量が残り何%になったらシャットダウンするかで、MINUTESは残り時間何分でシャットダウンするか。どちらかの条件を満たすとシャットダウンされる。お好みで変える。

[修正版][memo] postfix + dovecotでSMTP AUTH & IMAPなメールサーバを立ち上げる


[memo] postfix + dovecotでSMTP AUTH & IMAPなメールサーバを立ち上げるがあまり正しくないので修正版。

ssmtpの削除

Gentooではいきなりpostfixを入れようとしても、デフォルトでssmtpが入っていてblockされるのでssmtpを消すのが先決。

emerge -C ssmtp

postfixとdovecotのインストール

Gentooの基本だけど、emergeするときはemerge -pvでどんなフラグが有効になるか必ず確認。必要ないならpackage.useで外す。あとsaslが必要なのでこれは付けておく。あとなんでかわからないけどpostfixにsaslを付けるとついてくるcyrus-saslで、gdbmやcryptをUSEするとコンパイルに失敗したので外した。

emerge -pv postfix dovecot
echo "mmail-mta/postfix -mysql sasl" >> /etc/portage/package.use
echo "dev-libs/cyrus-sasl -mysql -java -gdbm -crypt" >> /etc/portage/package.use
emerge postfix dovecot

オレオレ認証局の作成

Gentooでの方法なので他のディストリではパスが違うようなので注意。

mkdir /etc/certs
chmod 700 /etc/certs
cd /etc/certs
cp /etc/ssl/misc/CA.pl .
perl ./CA.pl -newca
CA certificate filename (or enter to create)
(ENTER)
Making CA certificate ...
Generating a 1024 bit RSA private key
..................................++++++
.......................++++++
writing new private key to './demoCA/private/cakey.pem'
Enter PEM pass phrase:(パスフレーズを入力)
Verifying - Enter PEM pass phrase:(同じパスフレーズをもう一度入力)
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:JP
State or Province Name (full name) [Some-State]:Kanagawa
Locality Name (eg, city) []:Yokohama
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Private
Organizational Unit Name (eg, section) []:Private
Common Name (eg, YOUR name) []:nobu666.fam.cx
Email Address []:root@nobu666.fam.cx

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:(ENTER)
An optional company name []:(ENTER)
Using configuration from /etc/ssl/openssl.cnf
Enter pass phrase for ./demoCA/private/cakey.pem:
Check that the request matches the signature
Signature ok
Certificate Details:
 (略)
Certificate is to be certified until Oct 28 06:15:58 2009 GMT (1095 days)

Write out database with 1 new entries
Data Base Updated

クライアント用CA証明書とサーバ用CA証明書を作成

cd /etc/certs/demoCA
openssl x509 -inform pem -in cacert.pem -outform der -out cacert.der
openssl x509 -in ./cacert.pem -out ./cacert.crt

デジタル証明書のリクエストファイルを作成

cd /etc/certs
perl ./CA.pl -newreq-nodes
Generating a 1024 bit RSA private key
...++++++
...............................................................................................................++++++
writing new private key to 'newkey.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:JP
State or Province Name (full name) [Some-State]:Kanagawa
Locality Name (eg, city) []:Yokohama
Organization Name (eg, company) [Internet Widgits Pty Ltd]:nobu666.fam.cx
Organizational Unit Name (eg, section) []:Admin
Common Name (eg, YOUR name) []:nobu666.fam.cx
Email Address []:root@nobu666.fam.cx

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:(ENTER)
An optional company name []:(ENTER)
Request is in newreq.pem, private key is in newkey.pem

リクエストファイルからX.509サーバ証明書の作成と署名

perl ./CA.pl -sign
Using configuration from /etc/ssl/openssl.cnf
Enter pass phrase for ./demoCA/private/cakey.pem:
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number:
            ce:db:05:2a:6a:dc:75:17
        Validity
            Not Before: Oct 29 06:51:11 2006 GMT
            Not After : Oct 29 06:51:11 2007 GMT
        Subject:
            countryName               = JP
            stateOrProvinceName       = Kanagawa
            localityName              = Yokohama
            organizationName          = nobu666.fam.cx
            organizationalUnitName    = Admin
            commonName                = nobu666.fam.cx
            emailAddress              = root@nobu666.fam.cx
        X509v3 extensions:
            X509v3 Basic Constraints:
                CA:FALSE
            Netscape Comment:
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier:
                7B:0E:46:62:02:FB:B6:A6:05:DC:DE:75:2E:2D:5A:29:5D:31:79:F8
            X509v3 Authority Key Identifier:
                keyid:A7:51:EC:0A:BD:B6:70:68:A5:01:ED:8E:76:1F:FE:0C:4D:B2:A0:19

Certificate is to be certified until Oct 29 06:51:11 2007 GMT (365 days)
Sign the certificate? [y/n]:y

1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
Signed certificate is in newcert.pem

openssl x509 -in newcert.pem -out server.crt

postfix用にシンボリックリンクを張る

ln -sf /etc/certs/server.crt /etc/ssl/postfix/server.crt
ln -sf /etc/certs/newkey.pem /etc/ssl/postfix/server.key

postfixの設定

vi /etc/postfix/main.cf

queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/lib/postfix
mail_owner = postfix
myhostname = nobu666.fam.cx
mydomain = nobu666.fam.cx
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, $mydomain, mail.$mydomain, localhost
unknown_local_recipient_reject_code = 550
mynetworks_style = subnet
mynetworks = 192.168.0.0/24, 127.0.0.0/8
relay_domains = $mydestination
alias_maps = hash:/etc/mail/aliases
alias_database = hash:/etc/mail/aliases
home_mailbox = .Maildir/

header_checks = regexp:/etc/postfix/header_checks
smtpd_banner = $myhostname ESMTP $mail_name ($mail_version)
mydestination = $myhostname, localhost.$mydomain, $myhostname.$mydomain, mail.$mydomain, localhost
debug_peer_level = 2
debugger_command =
         PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
         xxgdb $daemon_directory/$process_name $process_id & sleep 5
sendmail_path = /usr/sbin/sendmail
mailq_path = /usr/bin/mailq
setgid_group = postdrop
html_directory = /usr/share/doc/postfix-2.2.10/html
manpage_directory = /usr/share/man
sample_directory = /etc/postfix
readme_directory = /usr/share/doc/postfix-2.2.10/readme
smtpd_use_tls = yes
smtpd_tls_loglevel = 1
smtpd_tls_cert_file = /etc/ssl/postfix/server.crt
smtpd_tls_key_file = /etc/ssl/postfix/server.key
smtpd_tls_session_cache_database = btree:/etc/postfix/smtpd_scache
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtp_sasl_security_options = noanonymous
allow_mail_to_commands = alias,forward,include
message_size_limit = 1024000
smtpd_client_restrictions =
            permit_mynetworks,
            permit_sasl_authenticated,
            permit_auth_destination,
            reject

vi /etc/postfix/master.cf

submission inet n      -       n       -       -       smtpd
        -o smtpd_etrn_restrictions=reject
        -o smtpd_client_restrictions=permit_sasl_authenticated,reject

saslの設定

vi /etc/sasl2/smtpd.conf

pwcheck_method: saslauthd

mkdir /var/state/saslauthd
chmod 700 /var/state/saslauthd
chown postfix /var/state/saslauthd

dovecotの設定

vi /etc/dovecot.conf

protocols = pop3 imap
ssl_disable = no
ssl_cert_file = /etc/ssl/postfix/server.crt
ssl_key_file = /etc/ssl/postfix/server.key
default_mail_env = maildir:%h/.Maildir

chmod 755 /var/run/dovecot
chmod 750 /var/run/dovecot/login

あとはデフォルトのまま。

クライアントの設定

まずはオレオレ認証局の証明書をクライアントに入れる必要がある(やらなくてもいいが警告が出るので…)。別にどうやってクライアントに渡してもOK。FTPとかSCPとかで適当に。メーラによって操作が違うがThunderbirdの場合は、ツール→オプション→プライバシー→セキュリティ→証明書を表示→認証局証明書→インポートと進んで、cacert.derを選択すればOK。あとはIMAPでアカウントを作って、受信は143番ポートでTLSを使うようにし、送信は587番ポートで同じくTLSを使うように設定する。送信テストするとユーザ名とパスワードが聞かれるので「パスワード無しで送信できないこと」「間違ったパスワードを入れて送信できないこと」「正しいパスワードを入れて送信できること」の3つを確認して終了。

links for 2006-10-28


[memo] iptablesが使えないのでkernel再構築 & sshdへのアタックをどうにかする


なんつーかね、某krからのsshdへのBrute force attacksが酷すぎる。auth.logを埋め尽くすほどのアタックを食らったので対策することに。だが普通にインストールするとiptablesが動いてくれないので、カーネル再構築に挑戦する。

別に今回はkernelのバージョンを上げたいわけではなく、単純にカーネルのオプションを変えたいだけなので、emerge gentoo-sourcesはやらない。以下、rootになって作業を行う。あとmenuconfigはscreenの中でやると表示が乱れて酷いことになるので、screenをデタッチするなり抜けるなりしてからやったほうがいい。

cd /usr/src/linux
make menuconfig

Confgurationが立ち上がったら、まずLoad an Alternate Configuration Fileを選ぶ。”.config”が入力されているはずなので、そのままOKすると現在の設定が読み込まれる。そしたら

Networking  --->
  Networking options  --->
    Network packet filtering (replaces ipchains)  --->
      Core Netfilter Configuration  --->
         Netfilter Xtables support (required for ip_tables)

を選択。その下に色々あるが、よくわからないならMにしといてあとでModuleとして読めるようにしておくと吉。必要なのに何もチェックしないでおくと、再度カーネル再構築が必要になって面倒なので。チェックしたら1つ戻ってIP: Netfilter Configurationの中に入る。とりあえず必須なのはIP tables support (required for filtering/masq/NAT)と、IP range match support、Packet filtering、REJECT target support、address type match support、Full NAT、MASQUERADE target supportとかそのへん。終わったらTOPまで戻って、念のため設定ファイルをなんか名前付けて保存しておいたほうがいい。

あとはその辺のサイトに書いてあるとおり、お約束でOK。

make && make modules_install
cp arch/x86_64/boot/bzImage /boot/なんか適当な名前
vi /boot/grub/grub.conf

grub.confの設定では、元々書いてあるものを絶対に消さないこと。消しちゃうと万が一再構築したカーネルで起動できなかったときに面倒。

[sourecode lang="text"]
title=Gentoo Linux 2.6.17-r8
root (hd0,0)
kernel /さっきbootの下にcpしたファイル名 root=/dev/sda3 vga=ask
[/sourcecode]

とかで問題なし。vga=askの部分はまぁお好みで。/usr/src/linux/Documentation/fb/vesafb.txtとかを参考に。基本的にはこれでrebootして、上のtitleでつけた名前を選択して起動してやればとりあえずOKなはずだ。失敗したら残しておいた方で起動して修正すべし。消してしまったお馬鹿さんはinstall cdから起動して/dev/sda1とかをmountして編集すれば戻せる。あとvgaとかvideoのオプションは、間違うと画面に何も表示されなくなるので注意。まぁリモート操作がメインならそれでもいいのかも知れないけど、何かあったときにアレなんで…心配ならaskにしておいて選択するのが間違いない。

そこまで出来たらDebian GNU/Linux 3.1(sarge)運用ノートのSSH Brute force attacksの通りに設定してから

/etc/init.d/iptables save
/etc/init.d/iptables start
rc-update add iptables default

以上で終了。エラーが出てiptablesが動かない場合は必要なモジュールが選択漏れでコンパイルされていないとか、Mを選択したのにmodprobeなりinsmodしてない。てか俺もこの手順にたどり着くまで5回くらい再構築してこんな時間に…もう寝る!

[memo] postfix + dovecotでSMTP AUTH & IMAPなメールサーバを立ち上げる


2006/10/29追記: イマイチ不正確なので[修正版][memo] postfix + dovecotでSMTP AUTH & IMAPなメールサーバを立ち上げるで書き直しました。そちらをみてください。

[memo] サーバのCPU動作周波数を動的に変える


[memo] サーバのCPU動作周波数を下げるの続き。

やってみないとわからんので、cpufreq_ondemandを試してみる。

modprobe cpufreq_ondemand
echo ondemand > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

cd /sys/devices/system/cpu/cpu0/cpufreq/ondemand/
ls
ignore_nice_load  sampling_down_factor  sampling_rate  sampling_rate_max  sampling_rate_min  up_threshold

cat ignore_nice_load
0

cat sampling_down_factor
1

cat sampling_rate
1040000

cat sampling_rate_max
520000000

cat sampling_rate_min
520000

cat up_threshold
80

詳細はKernel メモ ondemand参照。とりあえずignore_nice_loadは1に設定しておいた。急がないけど負荷になりそうな作業なんかはniceすること。

実際にemerge –syncとかして/proc/cpuinfoを見てたら、普段1GHzで動作しているのが2GHz→1.8GHzとなって1GHzに戻った。上手く動いているっぽい。試しにnice revdep-rebuildとかすると1GHzから変わらないことも確認。とりあえずこのまま運用してみる。

[memo] TortoiseSVNからsvn+sshを使う


普通にsshで繋がるようになったらsvn+sshを使いたくなる。サーバー側はemerge subversionするぐらいで設定は特にない。svnadmin create /var/svn/hoge とか svn import ~/moge file:///var/svn/hogeとかしておくだけ。svnserveを立ち上げる必要はない。svn+sshで接続するときは、sshで認証した後svnserve -tを勝手にしてくれ、接続を切るとsvnserveプロセスもなくなってくれる。よってルータで3690番のポートを開けておくとかいう作業は必要ない。22番だけ開いていればいい。

クライアント側は、Poderosaで作った鍵(例えばファイル名をid_rsaとする)しかない場合はPuTTY用の鍵を作る。puttygenを起動して、秘密鍵を読み込んでパスフレーズを入力すれば変換される。変換された秘密鍵をid_rsa.ppkとして保存。とりあえずPuTTYを起動して接続-データで自動ログインのユーザ名と、接続-SSH-認証で認証のためのプライベートキーファイルでさっきの.ppkファイルを設定。接続してみてパスフレーズ入力してログイン出来ればOK。

次にputty.exeと同じフォルダにあるpageant.exeを起動する。タスクトレイに常駐するので右クリックして鍵の追加を選んで、.ppkを選択する。パスフレーズを入力すれば作業終わり。以降、PuTTYでもTortoiseSVNでもパスフレーズは入力しないでもいい。ただ、これはWindowsにログインしなおすと消えてしまうので、ログイン毎に鍵の追加→パスフレーズの入力を行う必要がある。ランチャーに登録するかスタートアップに入れておく。

後はTortoiseSVNの設定で、ネットワークにあるSSHクライアントにTortoisePlink.exeを設定。これでTortoiseSVNからリボジトリブラウザを開いて、URL欄に svn+ssh://ユーザ名@ホスト名/var/svn/hoge と入力してエラーが出なければ終了。

[memo] USEフラグをいい感じに管理する


俺のようにしょっちゅうemergeしたい人はUSEフラグの管理が大変だ。知らないうちに勝手にgtk2フラグが有効になっててXがインストールされたりして大変ウザイ。そんなわけでGentoo使いの先輩に教えて貰ったeixとeuseを使ってみる。

sudo emerge eix
sudo eix-sync

euseはgentoolkitに入っている。eixはemerge –searchと似たようなモンだがこっちのほうが速いし使いやすい。んで、本題のeuseだが、使い方自体は–helpすりゃわかるんだが。フラグの説明見たり、どれが使われてるか見れたり、フラグを有効にしたり無効にしたりできる。つーか詳細はman euseで(w euse使うとmake.confのUSEフラグの部分が整形されて見やすくなるのも嬉しい。まぁ説明見るだけなら /usr/portage/profiles/use.desc
見ればいいんだが。

同じくgentoolkitに含まれているequeryも激しく便利。こいつで

equery depends mysql

とかやってやればmysqlフラグに依存しているパッケージを調べることが出来るし、dependsをdepgraphに変えてやると依存関係をツリーで見れる。超便利。