はじめに
先日リリースしたpdfveil
を使って暗号化したファイル(hoge.veil)をfileコマンド認識させたいと思い、実装したのでその時につまったところなどを書きます。
pdfveil
を知らない方はぜひ以下の記事を読んでみてください。
各ファイルに記述する内容
では早速file
コマンドを実装していきましょう。
pdfveil.magic
今回は暗号化されたveilファイルの先頭にVEIL
という文字列を入れています。
それをプロジェクトのルールに作成したpdfveil.magic
で認識させ、表示する文言を設定します。
0 string VEIL PDFVeil encrypted file
簡単に説明します。
文字 | 意味 |
---|---|
0 | ファイルの先頭を見ろ |
string | 文字列を指定 |
VEIL | もしVEILを見つけたら実行せよ |
PDFVeil encrypted file | 説明文 |
debian/rules
debパッケージを作成する時に作るビルドのルール等を記述するrulesファイルにも書いていきます。
#!/usr/bin/make -f
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS = -O2 -pipe -fstack-protector-strong -Wformat -Werror=format-security
export DEB_LDFLAGS = -Wl,-z,relro -Wl,-z,now
%:
dh $@ --with python3 --buildsystem=pybuild
override_dh_install:
dh_install
cp ./pdfveil.magic /usr/share/file/magic
file -C -m /usr/share/file/magic > /usr/share/file/magic.mgc
override_dh_install
の部分が重要です。
先ほど作ったpdfveil.magic
をfileコマンドが参照する/usr/share/file/magic
にコピーします。
その後コンパイルして実行可能ファイルにします。
これで、一応実装は完了しているはずです。
一度file
コマンドで確認してみましょう。
$ file memo.veil
Warning: using regular magic file `/usr/share/misc/magic'
file: file `/usr/share/misc/magic.mgc' is too small (No such file or directory)
memo.veil: PDFVeil encrypted file
一応指定した説明は表示されていますが、警告gでてしまっていて見づらくなっています。
この警告の原因はおそらくrules
で既存の/usr/share/file
内のファイルでディレクトリと競合してしまっているためだと思います。
そこで、rules
を修正します。
#!/usr/bin/make -f
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS = -O2 -pipe -fstack-protector-strong -Wformat -Werror=format-security
export DEB_LDFLAGS = -Wl,-z,relro -Wl,-z,now
%:
dh $@ --with python3 --buildsystem=pybuild
override_dh_install:
dh_install
mkdir -p debian/pdfveil/usr/share/pdfveil
cp ./pdfveil.magic debian/pdfveil/usr/share/pdfveil/pdfveil_magic
file -C -m debian/pdfveil/usr/share/pdfveil/pdfveil_magic > debian/pdfveil/usr/share/pdfveil/pdfveil.mgc
/usr/share/pdfveil
というディレクトリを新しく作りそこにファイルを置くようにしました。
しかし、これではfile
コマンドを実行した際に参照されません。
debian/postint
そこで、postint
を新しく作成して記述します。
このファイルはdebianパッケージをインストールした後に、実行されるスクリプトです。
今回は、alias
を使って先ほどのmgcファイル
をfile
で認識させようと思います。
#!/bin/sh
set -e
# pdfveil の magic ファイルがインストールされていることを確認
if [ -f /usr/share/file/magic.mgc ] && [ -f /usr/share/pdfveil/pdfveil_magic ]; then
# ユーザーの ~/.bashrc に alias を設定する
echo "alias file='file -m /usr/share/file/magic.mgc -m /usr/share/pdfveil/pdfveil_magic'" >> /home/$SUDO_USER/.bashrc
fi
# 新しい設定を反映させるためにシェルを再読み込み
if [ -f /home/$SUDO_USER/.bashrc ]; then
. /home/$SUDO_USER/.bashrc
fi
これでfile
コマンドを使った時にaliasで呼び出されるようになります。
$ chmod +x debian/postint
忘れずに実行権限を付与しておきましょう。
これで設定は完了です。
ビルドして、fileコマンドを使ってみましょう!
$ file memo.veil
memo.veil: PDFVeil encrypted file
ちゃんと指定した文字列が出力されました!!!