sshサーバー(sshd)をopenssh9.6p1にアップ

OS: Ubuntu 18.04.6 LTS サーバー

2024/4/5更新

opensshの脆弱性からopenssh9.6へアップグレードした。ところが、sshdサーバーのopensshのバージョンが上がっておらず、嵌ったところがあるので手順をメモ。一部、記憶に頼っているので記載ミスがあるかもしれない。この記事に従って作業する方は自己責任で。(以下、">"は「コマンドプロンプト」を表す。)

 

前調べ:インストールされているopensshのバージョン

 

> ssh -V

OpenSSH_7.6p1 Ubuntu-4ubuntu0.7+esm3, OpenSSL 1.0.2n  7 Dec 2017

 

> which openssl

/usr/bin/openssl

 

make a backup

> sudo cp /usr/bin/openssl /usr/bin/openssl~openssh7.6p1

 

1) openssh9.6p1を入手しコンパイルインストール

rootで作業をする。

> sudo su

 

Build essentials

> sudo apt install build-essential zlib1g-dev libssl-dev libpam0g-dev libselinux1-dev libkrb5-dev

 

Working directory:

> cd ~/Downloads/

> wget -c http://mirror.exonetric.net/pub/OpenBSD/OpenSSH/portable/openssh-9.6p1.tar.gz

 

> tar -xzf openssh-9.6p1.tar.gz

> cd openssh-9.6p1

 

> which openssh

/usr/bin/openssl

 

set -with-ssl-dir= to the head director of the openssl

> ./configure --with-kerberos5 --with-md5-passwords --with-pam --sysconfdir=/etc/ssh --with-ssl-dir=/usr

 

> make

> sudo make install

 

Restart your machine

> sudo systemctl restart sshd

> sudo shutdown -r now

 

Check new version. Open a new terminal and run:

 

> ssh -V

OpenSSH_9.6p1, OpenSSL 1.1.1  11 Sep 2018

 

2) ところが、上記でsshサーバー(sshd)もアップグレードされていると思い込んでいたが、上記手順ではどうもそうではない。

 

外部PCよりssh、オプション-vは接続の詳細をデバッグするため。

> ssh -v <user>@<server>

>> 

Remote: ... openSSH_7.6p1...

<< 

Remote(即ちserver)はopenssh7.6p1のまま、、、???

 

そこで、openssh (openssl)がどこにあるかを調べてみると

 

default install (openssh7.6p1) のsshdは /usr/sbin/sshd

openssh9.6p1は /usr/local/sbin/sshd

(上記(1)のインストールでは、sshdは /usr/local/sbin/sshdにインストールされている。ssh接続が止まらないよう default sshdを入れ替えないのかも?)

 

システムの自動起動serviceは、

/lib/systemd/system/ssh.service

中身では

ExecStart=/usr/sbin/sshd ...

 

即ち、defaultのsshd serverが起動されている。

(pathを/usr/local/sbin/sshdに変更して試験してみたが、openssh9.6p1では途中でtimeout、で起動しない。直接 /usr/sbin/sshdを書き換えていたらsshで接続できなくなる、、、危ない、危ない。)

 

/usr/local/sbin/sshdを起動してみると、

 

> /usr/local/sbin/sshd

 

なら走る、、、。そこで、/usr/local/sbin/sshdを自動で走らせるよう、sshd96p1.serviceを /etc/systemd/system に作成

 

> sudo cd /etc/systemd/system

 

> vi sshd96p1.service

>> 

[Unit]

Description=/usr/local/sbin/sshd service

After=network.target

 

[Service]

User=root

ExecStart=/usr/local/sbin/sshd

Restart=always

type=simple

 

[Install]

WantedBy=multi-user.target

<< 

defaultのsshd自動起動を外しておく

> systemctl disable sshd

 

sshd96p1.serviceを起動

> systemctl daemon-reload

> systemctl enable sshd96p1.service

> systemctl start sshd96p1

 

これで、PCを再起動しても、外部からのsshにはopenssh9.6p1で返答。

 

外部> ssh -v <user>@<server>

>> 

Remote: ... openSSH_9.6p1 ...

<< 

 

愛でたし、愛でたし。

MacOS Preview.app のここがダメ

MacOS Preview.app のここがダメ」
2024/1/31
Preview.app (バージョン11.0 (1018.6.3))
使用OS: MacOS 11.7 Big Sur

[ハイライトとメモ]
テキストをハイライトし、ハイライトにメモを追加する、追加されたメモには、チェックマーク(ボックス)が現れる

(ダメな部分)
1) 何かの拍子にチェックボックスが表示されなくなる。

2)「ハイライトとメモ」(された部分)をControlキーを押しながらクリックして出したコンテキストメニューには、「...消去」はあるが、「表示」は無い。

3) 表示するには、ヘルプには

「表示」>「ハイライトとメモ」と選択(「ハイライトとメモ」(された部分)の横にチェックマークが表示されます)

とあるが、消えたチェックマークは再表示されない。

4) 「表示」>「ハイライトとメモ」と選択すると、サイドバーに「ハイライトとメモ」された部分の一覧が表示されるが、メモの表示領域が限定され、長いメモは全体が表示されない(*)。メモ部分をクリックしてもメモ全体は表示されない。
(*) Preview.app (バージョン11.0 (1056.3.2))では解決されているが、このバージョンはBig Surでは動かない。

5) メモ文全体を表示するには、
- 「ツール」>「インスペクターを表示」、
- インスペクターの「ハイライト(ペンシルマーク)」をクリック、
- メモリストの対応するメモをクリックして、初めてメモ全体が表示される。
メモ全体を表示するのにインスペクターを出さなければならないなら、「表示」>「ハイライトとメモ」のサイドバーのメモ項目をクリックしたらインスペクターが出るようにしておくのはどうか。

[メモ]
「ハイライトとメモ」と似たような機能だが、「任意の場所に」メモをおける「メモ」機能。

メモを追加するには、
- ツールバーの「マークアップツールバー」(丸に鉛筆芯)をクリックし、
- マークアップツールバーにある「メモ」(ノートパッド)を選択

メモの削除は、
- 「ツール」>「インスペクターを表示」、
- インスペクターの「ハイライト(ペンシルマーク)」をクリック、
- メモリストの対応するメモを選択し、deleteキーを押す

(ダメな部分)
1) 「メモ」は「ハイライトとメモ」と似たような機能なので、「メモ」も「ツールバー」に置けるのが最適。

2) メモは、一旦、文書内に配置されると「削除」が困難。
メモの削除は、メモのチェックマーク(ボックス)をcontrol+クリックで削除のコンテクストメニューを出せば良い。

以上。

最後に、
アップルさん、バージョン12では上記の点を改善し、各OSバージョンでのPreview.appをアップデートしないかな?

 

Mac Mailでlinux上のメールボックスをIMAP接続、受信箱に新規メールが出てこない問題

(背景)
メールサーバー上のメールクライアントpineでメールボックスを開いた後、の特殊な条件で起こる。

(理由)

1) Linux上では、新規メールは /var/spool/mail/(user)ファイルに入る
Mac Mailの受信箱は、/var/spool/mail/(user)ファイルを見ている

2) Linux上のpineアプリでメールボックスを開くと、
← /var/spool/mail/(user)ファイルの新規メールは/home/(user)/mboxに移動、
← /var/spool/mail/(user)ファイルは空になる
→ 結果、新規メールがあるにも関わらず、Mac Mailの受信箱は「空」となる

(解決策)
Linux上で、/home/(user)/mboxファイルを、メールボックスdirectoryである/home/(user)/mail内にシンボリックリンクする
> cd /home/(user)/mail
> ln -s ../mbox mbox

(動作)
Mac Mailを起動すると、IMAPメールボックスにmboxが現れる。
mboxをドラッグして最上段に持ってくると、新規メールがmboxにあるかどうか分かり易い、か。

以上。

ubuntu16で、サーバーに接続、つながらない

Ubuntu16のファイルマネージャーを開くと、左パネル、一番下に、「サーバーに接続」の項目がある。これをクリックすると、接続形式とIPアドレス(例えばsambaなら(smb://...))を入力する事で、外部サーバーに接続可能、となっている。

 

が、(私の環境では?)Ubuntu16をインストールしただけでは、接続形式とIPアドレスを入力しても、「接続」のボタンが有効にならない。解決策をググってみたが、日本語ではヒットがない。因に英語で探して見たら、、、有った!ので、備忘録。

 

1) samba(がインストールされていなければ)インストール

> sudo apt install samba

2) gvfs-backendsをインストール(← これが肝)

> sudo apt install gvfs-backends

3) 再起動

 

これで、「サーバーに接続」> 現れた窓に「smb://xx.xx.xx.xx/」を入れ>「接続」をクリック。後は、問い合せに答えて行けば、サーバーのフォルダーがUbuntuのファイルマネージャーに現れた。

 

以上。

 

raspberry piを VPNサーバ かつ DNSサーバ化

raspberry piVPNサーバ (openswan) かつ adblock DNSサーバ (pihole)化

(効用)
1) 自宅内WiFi接続のWeb surfingで、自動的にAdblockがなされる。
2) 外出時、スマホiPhone)等で自宅にVPN接続で、通信の暗号化・Web surfingのAdblock・自宅のコンピュータにアクセス、が可能となる。

導入記、および導入時ハマった箇所のメモ。

(A) VPNサーバ(L2TP)

(1) openswanの導入

> sudo apt-get install openswan xl2tpd lsof
途中、X.509証明書は create > self-signed ...

confファイル等の修正 (参考:http://nlinux.nikkeibp.co.jp/2016/11/article/ )
Openswan-xl2tpd.zipをダウンロード・解凍

> sudo vi /etc/ipsec.conf
#VPNサーバーのアドレス
left=xx.xx.xx.xx (サーバとなるraspberry piIPアドレス

> sudo vi /etc/xl2tpd/xl2tpd.conf
ip range = xx.xx.xx.xx-xx.xx.xx.xx    (アサインするIPアドレスの範囲。DHCPサーバが割り振る範囲と重ならないように)
; VPNサーバーのIPアドレス
local ip = xx.xx.xx.xx

> sudo vi /etc/ipsec.secrets
: PSK "xxxxxxxxxx" (共有パスワード)

> sudo vi /etc/ppp/chap-secrets
#ユーザー名  パスワード
"xxx" * "xxx" * (必要に応じて追加)

> sudo vi /etc/sysctl.conf
# クライアントからの通信をLAN内に転送
# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1

> sudo vi /etc/ppp/options.l2tpd.lns
ms-dns xx.xx.xx.xx (DNSサーバ (次段のpihole)のIPアドレス

> sudo service ipsec restart
> sudo service xl2tpd restart
ipsec, xl2tpd 両方必要 (ハマッたその1)

(2) 外部からの接続を受け付けるルータ (xx.xx.xx.xx)に、外部からのUDPポート 500, 4500を、VPNサーバ(xx.xx.xx.xx)に転送させる設定を入れる。(各ルータの設定法を参照。)

(3) raspberry pi 起動時に /etc/init.d/ipsecが走らない、、、
raspberry piのWeb GUI画面(https://xx.xx.xx.xx/raspberry piIPアドレス

> sudo raspi-config (ハマッたその2)
3 Boot Options > B2 Wait for Network at Boot > Yes 


(B) Adblock DNSサーバ (piholeの導入)

https://pi-hole.net/ を参照し、curl ...で指示に従ってインストール。lighttpdも入れておく。
> curl -sSL https://install.pi-hole.net | bash

インストールが済めば、web browserで、https://xx.xx.xx.xx/admin で、piholeのweb画面を出せる。

(1) Loginパスワードの設定
インストール時にパスワードが設定されるが、、、気がつかなかったら、以下のコマンドで再設定可。VPNサーバを立てるためraspberry piは外部からのアクセスを許可するので、securityのためログインパスワードは必須。

> pihole -a -p

(2) 以下、piholeのweb画面で左パネルのsettingで設定。

> DNS画面
- Customに、xx.xx.xx.xx (raspberry piIPアドレス

- Interfaces... に、[Listen on all interfaces]を選択。(ハマッたその3)
VPNサーバとDNSサーバが共立するにはこの選択が必要。(ここが解るまでに見当違いなtry&errorを相当繰り返した、、、、)

> DHCP画面

上流の光ファイバールータで DNSraspberry piを選択出来れば良いのだが、softbank光ユニットではDNSは独立に指定出来ない(ハマッたその4)。このような時のため、piholeがDHCPサーバ機能を用意している。

- 上流ルータのDHCP機能を停止、したいが、softbank光ユニットは disableボタンが無い、、、。家庭内で固定IPアドレスを振る範囲のみを残す。

- piholeのDHCPのenableを選択。割り振るIPアドレスの範囲が、他(上流ルータ、VPNサーバ)が指定する範囲と重ならないように注意。Gatewayは、raspberry piのアドレスでも上流ルータのアドレスでも問題ない様子。

以上で終了。

(ハマッたところ)
> sudo service ipsec restart
> sudo service xl2tpd restart
ipsec, xl2tpd 両方必要 (ハマッたその1)

> sudo raspi-config (ハマッたその2)
3 Boot Options > B2 Wait for Network at Boot > Yes 

- Interfaces... に、[Listen on all interfaces]を選択。(ハマッたその3)

softbank光ユニットではDNSは独立に指定出来ない(ハマッたその4)

記:
2018/10/26
Raspberry pi3: Raspbian GNU/Linux 8 (jessie)









iOS12.0.1アップデートで 設定に謎のバッジ、、、解決?

iOS12.0.1にアップデートで、謎のバッジが設定アイコンに付いて来た。設定の項目のどれを見ても、メッセージがついていない、にも関わらず、新規メッセージがある事を示すバッジがある、、、。

ネット上でも話題になっているようで、まとめのページがいくつかアップされている。が、10月15日現在、どの解決策も私のiphoneではダメだった、、、。

が、ふとした事から、私の場合解決したので、参考になればと記録しておく。

問題は、「iCloudバックアップ」(「設定」>「使用者情報」(最初の項目で、Apple ID、iCloud、 ...のところ)>iCould>(最後の頃の)「iCloudバックアップ」)。「iCloudバックアップ」をオフにしている人は、オン>オフを行ってみると良いでしょう。

以下、問題の解釈。

私はiCloudを使わないので、現状、「オフ」にしており、バックアップはパソコンにしている。謎のバッジが現れているので、暫くぶりにiphoneをパソコンに接続し、バックアップを取った。このとき、何かの設定で、iCloudのバックアップにマージするかとの問い合わせがあり、Yesとした(ような)。

通常、iCouldバックアップはしないので「オフ」で有る事を確認するため、iphoneのiCouldバックアップを見に行くと、「オン」になっている(当然か?)。「オフ」にするため「オン」をたたくとiCloudバックアップの画面が出て、何とナント、メッセージが現れていた、「...100日以上バックアップが行われていません」(それはそうです、iCloudにはバックアップしません。)

iCloudバックアップの画面で、iCloudバックアップを「オフ」にし、アイコン羅列画面に戻ると、、、(おっ)設定のバッジが消えている(これも当然か)。結論、「...100日以上バックアップが行われていません」のメッセージがバッジの原因だった。

さて、上記が解って振り返ってみると、私が面倒見ている複数のiphoneでも、現れたもの、現れないものがあった。いずれもiCouldバックアップは、現状、「オフ」。バッジが現れる原因(100日以上バックアップが行われていません)となるメッセージは、「一度でも」iCouldバックアップを「行った」機種には(当然)メッセージが送られており、「一度も」iCouldバックアップを「行わなかった」機種には送られない、即ち、バッジの有無が分かれる、と解る。

で、根本的なバグ、不備はiOSにある。メッセージがあるなら、メッセージのある階層まで、「全て」の階層にバッジ(或は何らかの記号、iマークでも良い)を付けるべき。

以上。

(2018年10月15日、iOS12.0.1)





Mac OSX 10.6.8 (でなくても良かった) に 無料 (Free) のClamAV Anti virus の自動化(drag&drop, folder action)を導入

Mac OSX 10.6.8 (でなくても良かった) に 無料 (Free) のClamAV Anti virus の自動化 (drag&drop, folder action)を導入
2016/12/25、12/31, 2020/8/20 改訂中

MacBook Pro 13 (late 2009) を、RAMを2x4GB、SSD storage (1TB )で、現役バリバリで使用している。OSは、OSX 10.6.8 (Snow Leopard) 止まり。使用しているCADアプリ他のアプリ(値段が張るのですよ)が、10.7以降に対応していないため。

OSもCADアプリ他も、必要最低限の機能はあり特に新版を購入する理由は無い、、、。(となるので、ソフト会社は新しいもののみに対応させて、旧版を陳腐化させたいのですよ、ね。)

さて、OSX 10.6.8 自身には問題は無いのだが、ネットワークに接続する際は Anti Virus アプリのインストールが必須と言うのが ITセクションの要請。(Macでvirusにかかったことは無いのだが、それはたまたま運が良かっただけ、、、なのか。)

OSXは 10.7 からAPIを変えたためか、殆どの Anti Virus アプリが 10.7以降対応となっており、10.6.8では フリー(無料)のアプリは皆無。その中で唯一 オープンソース/皆でサポートの ClamAV が利用可能。その昔、OSX 初期時代(古いね)に使用した事はあったが、今も生き残っていてくれた事は 嬉しいとともに驚き。

ClamAVを使用するには素人には敷居が高く(と思っていた)、ClamXAVなるユーザインターフェイスを使用して、簡単に使えていた。が、ClamXAVも(30日間の無料版はあるが)有料化されて、使えなくなっていた。(生きていくためにはお金が必要なのです。)

仕方が無いので、ClamAVの導入に挑戦、いくつもの壁を乗り越え(がこの備忘録)、更に、使用方法が解って来たので、applescriptでスキャンを自動化。スクリプトコマンドコマンドレベルのローレベルで設定・コントロールしているので、何をしている/させたいのか見通せて(ブラックボックスでない)精神的に良いし、自動化で快適に使えているので、今のところメデタシ メデタシ。(あとは、ClamAVコミュニティーが virus databaseを最新に維持してくれる事を願うのみ、、、か。)


[1. ClamAVのインストール]

https://www.clamav.net/downloads

から最新版を入手。この時点で clamav-0.99.2.tar.gz
tar.gzを展開すると、clamav-0.99.2フォルダーができる。
で、terminalを開き (以下、">"はterminalでコマンドを入れる事を意味する)、

> cd ...../clamav-0.99.2
> ./configure
> make
> make install

特にデフォールト設定から変える必要はない。コンパイルに時間がかかる。お茶でも飲んで気長に待つ。途中をみているとwarningらしきものが出ているが、何か解らないので無視。最後に、errorが表示されていないので、インストールは成功、とみて良い(だろう)。

config ファイル → /usr/local/etc/
freshclam, clamscan, clamdscan → /usr/local/bin/
clamd → /usr/local/sbin/

にインストールされている。


[2. clamd 環境の整備]

clamd.confの編集

> cd /usr/local/etc
> sudo cp clamd.conf.sample clamd.conf
> sudo vi clamd.conf

>>> 以下、次の文を無効/有効化する
#Example ← この文は#で無効化する、以下は#を消して有効化
LogFile /var/log/clamd.log
TCPSocket 3310
User clamav ← this is critical to make clamdscan can connect to clamd
LogTime yes
<<<

clamd.log ファイルの作成・属性の編集

> sudo touch /var/log/clamd.log
> sudo chown clamav:clamav /var/log/clamd.log
> sudo chmod 666 /var/log/clamd.log ← worldまで読み書きにしないと書き込みエラーがでる。

clamdの実行

> sudo /usr/local/sbin/clamd & ← "&"をつけてバックグランドで走らせる

(止めるには、例えば Activity monitor.app で停止。Terminal で > kill -9 <PID> でも出来る。走らせるには、「6. Scanの自動化」の script appを導入すれば、scan実行時、自動で走る。)

[3. freshclam 環境の整備]

Virus databaseの入手・自動アップデートは freshclam が行う。

> cd /usr/local/etc
> sudo cp freshclam.conf.sample freshclam.conf
> sudo vi freshclam.conf

>>> 以下の文を無効/有効化
#Example
UpdateLogFile /var/log/freshclam.log
LogTime yes
<<<

virus database 領域の編集

> sudo chown 82:82 /usr/local/share/clamav ← user, group 82は共にclamav
> sudo chmod 777 /usr/local/share/clamav ← clamd, freshclam で使用するので777
> sudo chmod 666 /usr/local/share/clamav/mirrors.dat ← freshclam が書き込む
> sudo chown 82:82 /usr/local/bin/freshclam

freshclam.log ファイルの作成・属性編集

> sudo touch /var/log/freshclam.log
> sudo chmod 666 /var/log/freshclam.log
> sudo chown clamav:clamav /var/log/freshclam.log

freshclamの(daemon モードでの)実行

> freshclam -d

これで virus database が 2時間おきにアップデートされる。


[4. scanの実行]

clamscan.log ファイルの作成・属性の編集

> sudo touch /var/log/clamscan.log
> sudo chown 82:82 /var/log/clamscan.log
> sudo chmod 666 /var/log/clamscan.log ← worldまで読み書きにしないと書き込みエラーがでる(かも)。

scanの個別手動実行は、

> clamdscan -r -i --no-summary <file/folder> > /var/log/clamscan.log &

clamdが走っていないと errorになる。clamd の実行は 2.を参照。各オプションは、

-r フォルダー内のフォルダーもrecursiveにscan
-i virusの検知されたファイルのみリスト
--no-summary 実行のsummaryを出力しない
> /var/log/clamscan.log 結果をファイルに保存。毎回、新規に書き出すため。--log=/var/...オプションだと追記されるので古いものが残ってしまう
& バックグランドで実行

clamdscan と clamscan の違い:

virus databaseをメモリーに保持している(clamdscan → clamdを使用)か、storage から読み出し (clamscan)の差。
実行速度が違う。


[5. White listの作成]

大きな外付け usb disk等を挿入した場合 (例えば、TimeMachine用disk)、scanに非常に時間がかかる。ので、Virusに罹っていないことが解っている場合、或は Virusが入っている事が解っていても Virusは隔離されている、、、ような場合、scanから外したい事が有る。scanから外すために White list の機能を実装する。

White list ファイルは、/var/log/clamWhiteList.log とする。

> sudo touch /var/log/clamWhiteList.log

たとえば、USBDISK と言う名の 外付けdisk をscan対象から外す場合、

> cd /var/log
> sudo vi clamWhiteList.log
>>>
/Volumes/USBDISK/
<<<

の全パスを書き込む。Directory/folder 名の場合、末尾に"/"が必要。


6. scanの自動化 (ドラッグ&ドロップ、フォルダーアクション)

applescriptを独自に作成。(コピーは自由ですが、著作権は私に有りますので宜しく。)

drop_clamscan.app : file/folder を drag&drop して clamdscan
clamscan.app : /Library/Scripts/Folder Action Scripts/ に入れて、フォルダーアクションでフォルダーをscan

OSXの アプリケーションフォルダー>ユーティリティーAppleScriptエディター.appを起動し、

>>> 以下 ...<<< までを書き込み
on open theDroppedItems
--display dialog "clamscan for " & theDroppedItems
set theLogFile to "/var/log/clamscan.log"
scanWithClamAV(theDroppedItems, theLogFile)
end open

on scanWithClamAV(theItems, theLogFile)

(* set theIcon to clamav.icns if exits *)
set theApp to (path to me) as text
set theIcon to appIcon(theApp)
--display dialog theIcon

(* check clamd and freshclam are running *)
set {appName, appDir, appOption} to {"clamd", "/usr/local/sbin/", ""}
isRunning(appName, appDir, appOption, theIcon)

set {appName, appDir, appOption} to {"freshclam", "/usr/local/bin/", " -d"}
isRunning(appName, appDir, appOption, theIcon)

--set theLogFile to "/var/log/clamscan.log"
set isUsedFlag to isUsed(theLogFile, "clamdscan")
if isUsedFlag is true then return 1

set theFile to POSIX path of theLogFile
--set fh to open for access file theFile with write permission
-- make sure theFile exists, otherwise the next line generates an error
set eof of theFile to 0

(* get white list *)
set whiteList to paragraphs of (do shell script "/bin/cat /var/log/clamWhiteList.log")

(* scan dropped items *)
repeat with aFile in theItems

(* check white list *)
set aFilePath to quoted form of POSIX path of aFile as text
set aFileToScan to true
repeat with aWhite in whiteList
--display dialog aFilePath & " " & quoted form of aWhite
if aFilePath is equal to quoted form of aWhite then
set aFileToScan to false
--display dialog "White list ---- " & aFilePath giving up after 5 with icon theIcon as alias
end if
end repeat
--display dialog aFileToScan

(* do scan *)
if aFileToScan then
set theShellScript to ("/usr/local/bin/clamdscan --quiet -i -m --log=" & theLogFile & " --no-summary " & aFilePath)
--display dialog theShellScript
tell me to activate
display dialog "Scanning --- " & aFilePath buttons {"Cancel", "OK"} default button 2 with title "Clamscan" giving up after 3 with icon theIcon as alias
try
do shell script theShellScript
end try
end if

end repeat
--close aFilePath with saving
--display dialog "... scan finished." giving up after 1 with icon theIcon as alias
--close access fh

(* notify virus *)
if aFileToScan then
set volumeSetting to output muted of (get volume settings)
set volume without output muted

set Shows to (do shell script "/bin/cat " & theLogFile)
--set Shows to paragraphs of (read theLogFile)
--display dialog "here"

if Shows contains "FOUND" then
beep 3
--display dialog "Virus: "
--display dialog "Virus: " & Shows buttons {"OK"} default button "OK" with icon theIcon as alias giving up after 30 with title theLogFile
-- keep displayed untill "OK" is clicked
display dialog "Virus: " & Shows buttons {"OK"} default button "OK" with icon theIcon as alias with title theLogFile
else
display dialog "Virus not found --- " & theItems buttons {"OK"} default button "OK" with icon theIcon as alias giving up after 10
end if

if volumeSetting then set volume with output muted
end if

end scanWithClamAV

on isRunning(appName, appDir, appOption, theIcon)
set appNameIsRunning to (do shell script "/bin/ps -arxo state,comm | /usr/bin/grep " & appName & " | /usr/bin/cut -c 1")
if appNameIsRunning is "" then
--display dialog "Start " & appName & " ? " giving up after 2 with icon theIcon as alias
do shell script appDir & appName & appOption & " &"
else
--display dialog appName & " --- running."
end if
end isRunning

on isUsed(fileName, byAppName)
--display dialog (do shell script "/bin/ps -arx | /usr/bin/grep " & fileName)
set fileNameIsUsed to (do shell script "/bin/ps -arx | /usr/bin/grep " & fileName)
-- & appName)
set isUsedFlag to false
repeat with theLine in paragraphs of fileNameIsUsed
--display dialog theLine
if theLine contains byAppName then
display dialog fileName & " --- is being used by other " & byAppName & ". Kill " & byAppName & " by yourself or wait till it finishes."
set isUsedFlag to true
else
--display dialog fileName & " --- not used."
end if
end repeat
return isUsedFlag
end isUsed

on appIcon(theApp)
--display dialog theApp
set clamIcon to theApp & "Contents:Resources:clamav.icns" as text
try
clamIcon as alias
set theIcon to clamIcon
on error
--display dialog clamIcon & " --- not exit."
set thePlist to theApp & "Contents:info.plist"
--display dialog thePlist
tell application "System Events"
set pList to property list file (thePlist)
set theIcon to (value of property list item "CFBundleIconFile" of pList) as text
end tell
set theIcon to theApp & "Contents:Resources:" & theIcon & ".icns"
--display dialog theIcon
end try
--display dialog theIcon with icon theIcon as alias
return theIcon
end appIcon

<<<

ファイル>別名で保存>「名前」は drop_clamscan、「場所」は自分の判断で、ファイルフォーマット「アプリケーション」で「保存」。(ファイルをドロップで起動するには、アプリケーションである必要があるため。)

drop_clamscan.appのやっている事は、
on open ... ← drag&drop対応
scanWithClamAVの前半は、clamd 、freshscan が走っているかどうかをチェック。走っていなければ起動。
また、clamscan.log が別clamdsan に使用されていないかチェック。
ドロップされたファイル/フォルダー毎に、white listをチェックし、入っていなければ clamdscan を実行、結果をログファイルに書き出し
その後、volumeをONし、
virusが検知されていれば、ビープ音を鳴らしlogファイルを表示、無いときは not foundを表示
最後の、volumeをもとに戻す。
display dialogは最少限になっているので、debugしたい場合は "--"を消して有効化する。

drop_clamscan.app をDockにドロップしておけば、scanしたいファイルをDock内のdrop_clamscan.appにドロップする事でscanできる。


つぎに、フォルダーアクションで On-Access Scan を実現する。

上記の applescript appで、

on open theDroppedItems
--display dialog "clamscan for " & theDroppedItems
set theLogFile to "/var/log/clamscan.log"
scanWithClamAV(theDroppedItems, theLogFile)
end open

の代わりに、

on adding folder items to this_folder after receiving added_items
--display dialog "clamscan for " & this_folder
set theLogFile to "/var/log/clamscan1.log"
scanWithClamAV(added_items, theLogFile)
end adding folder items to

を書き込む。「別名で保存」で、

名前: clamscan1
場所: ライブラリー>Scripts>Folder Action Scripts
ファイルフォーマット:スクリプトバンドル

で保存。(ファイルフォーマットはアプリケーションでも良いが、スクリプトバンドルならダブルクリックでスクリプトエディターが起動する。)

対応するログファイルを作成しておく:

> sudo touch /var/log/clamscan1.log
> sudo chown 82:82 /var/log/clamscan.log
> sudo chmod 666 /var/log/clamscan.log ← worldまで読み書きにしないと書き込みエラーがでる(かも)。


監視したいフォルダーをFinderで選択し、フォルダーアクション設定で clamscan.app を関連づける。例えば、

デスクトップ、ダウンロード、Volumes (外付けdiskがマウントされるdirectory)等のフォルダー。

監視を終わらせたいときは、フォルダーアクション設定で関連付けを外す。
また、Scanを途中でも終わらせたいときには、Activityモニター.appで clamd を停止する。

大きなdiskをscan中にdiskを切り離すと、clamdscan は実行中のままになる。このまま、次のclamdscan が自動的に走ろうとすると、clamscan.log ファイルが実行中の clamdscan に占有されているので フォルダーアクション実行がキャンセルされる(しかも、無言で)。script appに、clamdscanが実行中かのチェックを入れ、警告を出すようにした。

注意:フォルダーアクション/clamdscanは、1時に1つしか実行出来ない。大きなフォルダーをスキャンしているときは、新しいスキャンは、実行中のスキャンが終了してから、手動でdrop_clamscan.appにドロップする。


[7. ClamAV が実行されているかの確認]

clamav-0.99.2/test/ フォルダーを drop_clamscan.appにドロップしてみる。testフォルダーには、virusファイルが例題として入っているので、...Found になればOK。


[8. logファイルの確認]

コンソール.appを起動し、/var/log/clamd.log、/var/log/freshscan.log を表示し、ERRORが出ていないか確認。ERRORが出ていたら、適宜、解決するよう対処する。

以上。

(感想:いやー、全てのステップが壁だった。ところで、10.6.8で作業をしたが、ClamAVコンパイル出来、またapplescriptが動作するなら、どのOSXバージョンでも動作するはず。)


おまけ:
drop_clamscan.app、clamscan1.scptd の /Contents/Resources/に適当な iconファイルをclamav.icns 名で放り込んでおくと、virus検知のdialogにそのiconが表示されるようになるようscrip appは書いてある。例えば、ClamAVの画像を icns化して貼付けると、こんな風になる、、、