こんにちはー!ニアです。
FTPなどでアップデートしたWordPressのプラグインやテーマ、アップロードコンテンツにおいて、ファイルとディレクトリの所有者及びパーミッションを一括で設定できるスクリプト(Bash)を作ってみました。
1. プラグイン「wp-content/plugins」用
# !/bin/bash
# 変数
# 所有者
OWNER=ftp_username
# グループ
GROUP=www
# wp-contentのディレクトリ
WPCDIR=/var/www/html/wp-content
# wp-content/plugins内のディレクトリに設定するパーミッション
PERDIR=775
# wp-content/plugins内のファイルに設定するパーミッション
PERFILE=664
# 翻訳ファイルの言語
LANG=ja
# コマンドライン引数にプラグインのディレクトリ名が指定された時、
# そのディレクトリ内のファイルとディレクトリの所有者とパーミッションを設定します。
if [ $# -gt 0 ]; then
# コマンドライン引数に指定されたプラグインディレクトリ名を列挙します。
for i in $@
do
# コマンドライン引数に'../'が含まれているかどうかをチェックします。(意図しないディレクトリにアクセスするインジェクション対策)
if [ `echo $i | grep '\.\./'` ]; then
echo -e "\033[1;35mError\033[0;39m - Cannot use '../' in commandline argments for security."
# 指定されたプラグインのディレクトリが存在するかチェックします。
elif [ -e $WPCDIR/plugins/$i ]; then
echo -e "\033[1;32mInfo.\033[0;39m - Directory:'wp-content/plugins/$i' is found."
# そのプラグインのディレクトリ内の所有者とグループを設定します。
chown -R $OWNER:$GROUP $WPCDIR/plugins/$i
echo -e "\033[1;32mInfo.\033[0;39m - Set the files and directories owner in the directory:'wp-content/plugins/$i' to $OWNER:$GROUP."
# そのプラグインのディレクトリ内のディレクトリのパーミッションを設定します。
find $WPCDIR/plugins/$i/ -type d -exec chmod $PERDIR {} \;
echo -e "\033[1;32mInfo.\033[0;39m - Set the directories permissions in the directory:'wp-content/plugins/$i' to $PERDIR."
# そのプラグインのディレクトリ内のファイルのパーミッションを設定します。(但し、.htaccessと.htpasswdを除きます。)
find $WPCDIR/plugins/$i/ -type f -not -name '.htaccess' -not -name '.htpasswd' -exec chmod $PERFILE {} \;
echo -e "\033[1;32mInfo.\033[0;39m - Set the files permissions in the directory:'wp-content/plugins/$i' to $PERFILE."
# 指定されたプラグインの翻訳ファイルがあるかどうかチェックします。
if [ -e $WPCDIR/languages/plugins/$i-$LANG.mo ] || [ -e $WPCDIR/languages/plugins/$i-$LANG.po ]; then
echo -e "\033[1;32mInfo.\033[0;39m - Directory of translation file:'$WPCDIR/languages/plugins/$i-$LANG' is found."
# そのプラグインの翻訳ファイルの所有者とグループを設定します。
chown $OWNER:$GROUP $WPCDIR/languages/plugins/$i-$LANG.*
echo -e "\033[1;32mInfo.\033[0;39m - Set the files and directories owner in the directory:'wp-content/languages/plugins/$i-$LANG' to $OWNER:$GROUP."
# そのプラグインの翻訳ファイルのパーミッションを設定します。
chmod $PERFILE $WPCDIR/languages/plugins/$i-$LANG.*
echo -e "\033[1;32mInfo.\033[0;39m - Set the files permissions in the directory:'wp-content/languages/plugins/$i-$LANG' to $PERFILE."
fi
else
echo -e "\033[1;35mError\033[0;39m - Directory:'wp-content/plugins/$i' is not found."
fi
done
# コマンドライン引数になにも指定していない場合、
else
# プラグインディレクトリ内のすべてのファイルとディレクトリの所有者及びグループを設定します。
chown -R $OWNER:$GROUP $WPCDIR/plugins
echo -e "\033[1;32mInfo.\033[0;39m - Set the files and directories owner in all of the plug-in directory to $OWNER:$GROUP."
# プラグインの翻訳ファイルの所有者及びグループを設定します。
chown -R $OWNER:$GROUP $WPCDIR/languages/plugins
echo -e "\033[1;32mInfo.\033[0;39m - Set the files and directories owner in all of the directory of plug-in translation file to $OWNER:$GROUP."
# プラグインディレクトリ内のすべてのディレクトリのパーミッションを設定します。
find $WPCDIR/plugins/ -type d -exec chmod $PERDIR {} \;
echo -e "\033[1;32mInfo.\033[0;39m - Set the directories permissions in all of the plug-in directory to $PERDIR."
# プラグインディレクトリ内のすべてのファイルのパーミッションを設定します。(但し、.htaccessと.htpasswdを除きます。)
find $WPCDIR/plugins/ -type f -not -name '.htaccess' -not -name '.htpasswd' -exec chmod $PERFILE {} \;
echo -e "\033[1;32mInfo.\033[0;39m - Set the files permissions in the directory:'wp-content/plugins/$1' to $PERFILE."
# プラグインの翻訳ファイルのパーミッションを設定します。
find $WPCDIR/languages/plugins/ -type f -not -name '.htaccess' -not -name '.htpasswd' -exec chmod $PERFILE {} \;
echo -e "\033[1;32mInfo.\033[0;39m - Set the files permissions in the directory:'wp-content/languages/plugins/$1' to $PERFILE."
fi
使い方
# すべてのプラグインのファイルとディレクトリの所有者及びパーミッションを設定します。
$ bash plugins.sh
# プラグインディレクトリ名(ここでは「lemon」)を指定して、所有者及びパーミッションを設定します。
$ bash plugins.sh lemon
# 複数のプラグインディレクトリ名(ここでは「lemon」、「lime」、「orange」)を指定して、所有者及びパーミッションを設定します。
$ bash plugins.sh lemon lime orange
2. テーマ「wp-contents/themes」用
# !/bin/bash
# 変数
# 所有者
OWNER=ftp_username
# グループ
GROUP=www
# wp-contentのディレクトリ
WPCDIR=/var/www/html/wp-content
# wp-content/themes内のディレクトリに設定するパーミッション
PERDIR=775
# wp-content/themes内のファイルに設定するパーミッション
PERFILE=664
# 翻訳ファイルの言語
LANG=ja
# コマンドライン引数にテーマのディレクトリ名が指定された時、
# そのディレクトリ内のファイルとディレクトリの所有者とパーミッションを設定します。
if [ $# -gt 0 ]; then
# コマンドライン引数に指定されたテーマディレクトリ名を列挙します。
for i in $@
do
# コマンドライン引数に'../'が含まれているかどうかをチェックします。(意図しないディレクトリにアクセスするインジェクション対策)
if [ `echo $i | grep '\.\./'` ]; then
echo -e "\033[1;35mError\033[0;39m - Cannot use '../' in commandline argments for security."
# 指定されたテーマのディレクトリが存在するかチェックします。
elif [ -e $WPCDIR/themes/$i ]; then
echo -e "\033[1;32mInfo.\033[0;39m - Directory:'wp-content/themes/$i' is found."
# そのテーマのディレクトリ内の所有者とグループを設定します。
chown -R $OWNER:$GROUP $WPCDIR/themes/$i
echo -e "\033[1;32mInfo.\033[0;39m - Set the files and directories owner in the directory:'wp-content/themes/$i' to $OWNER:$GROUP."
# そのテーマのディレクトリ内のディレクトリのパーミッションを設定します。
find $WPCDIR/themes/$i/ -type d -exec chmod $PERDIR {} \;
echo -e "\033[1;32mInfo.\033[0;39m - Set the directories permissions in the directory:'wp-content/themes/$i' to $PERDIR."
# そのテーマのディレクトリ内のファイルのパーミッションを設定します。(但し、.htaccessと.htpasswdを除きます。)
find $WPCDIR/themes/$i/ -type f -not -name '.htaccess' -not -name '.htpasswd' -exec chmod $PERFILE {} \;
echo -e "\033[1;32mInfo.\033[0;39m - Set the files permissions in the directory:'wp-content/themes/$i' to $PERFILE."
# 指定されたテーマの翻訳ファイルがあるかどうかチェックします。
if [ -e $WPCDIR/languages/themes/$i-$LANG.mo ] || [ -e $WPCDIR/languages/themes/$i-$LANG.po ]; then
echo -e "\033[1;32mInfo.\033[0;39m - Directory of translation file:'$WPCDIR/languages/themes/$i-$LANG' is found."
# そのテーマの翻訳ファイルの所有者とグループを設定します。
chown $OWNER:$GROUP $WPCDIR/languages/themes/$i-$LANG.*
echo -e "\033[1;32mInfo.\033[0;39m - Set the files and directories owner in the directory:'wp-content/languages/themes/$i-$LANG' to $OWNER:$GROUP."
# そのテーマの翻訳ファイルのパーミッションを設定します。
chmod $PERFILE $WPCDIR/languages/themes/$i-$LANG.*
echo -e "\033[1;32mInfo.\033[0;39m - Set the files permissions in the directory:'wp-content/languages/themes/$i-$LANG' to $PERFILE."
fi
else
echo -e "\033[1;35mError\033[0;39m - Directory:'wp-content/themes/$i' is not found."
fi
done
# コマンドライン引数になにも指定していない場合、
else
# テーマディレクトリ内のすべてのファイルとディレクトリの所有者及びグループを設定します。
chown -R $OWNER:$GROUP $WPCDIR/themes
echo -e "\033[1;32mInfo.\033[0;39m - Set the files and directories owner in all of the theme directory to $OWNER:$GROUP."
# テーマの翻訳ファイルの所有者及びグループを設定します。
chown -R $OWNER:$GROUP $WPCDIR/languages/themes
echo -e "\033[1;32mInfo.\033[0;39m - Set the files and directories owner in all of the directory of theme translation file to $OWNER:$GROUP."
# テーマディレクトリ内のすべてのディレクトリのパーミッションを設定します。
find $WPCDIR/themes/ -type d -exec chmod $PERDIR {} \;
echo -e "\033[1;32mInfo.\033[0;39m - Set the directories permissions in all of the theme directory to $PERDIR."
# テーマディレクトリ内のすべてのファイルのパーミッションを設定します。(但し、.htaccessと.htpasswdを除きます。)
find $WPCDIR/themes/ -type f -not -name '.htaccess' -not -name '.htpasswd' -exec chmod $PERFILE {} \;
echo -e "\033[1;32mInfo.\033[0;39m - Set the files permissions in the directory:'wp-content/themes/$1' to $PERFILE."
# テーマの翻訳ファイルのパーミッションを設定します。
find $WPCDIR/languages/themes/ -type f -not -name '.htaccess' -not -name '.htpasswd' -exec chmod $PERFILE {} \;
echo -e "\033[1;32mInfo.\033[0;39m - Set the files permissions in the directory:'wp-content/languages/themes/$1' to $PERFILE."
fi
使い方
# すべてのテーマのファイルとディレクトリの所有者及びパーミッションを設定します。
$ bash themes.sh
# テーマディレクトリ名(ここでは「lemon」)を指定して、所有者及びパーミッションを設定します。
$ bash themes.sh lemon
# 複数のテーマディレクトリ名(ここでは「lemon」、「lime」、「orange」)を指定して、所有者及びパーミッションを設定します。
$ bash themes.sh lemon lime orange
3. アップロードファイル「wp-content/uploads」用
# !/bin/bash
# 変数
# 所有者
OWNER=ftp_username
# グループ
GROUP=www
# wp-contentのディレクトリ
WPCDIR=/var/www/html/wp-content
# wp-content/uploads内のディレクトリに設定するパーミッション
PERDIR=775
# wp-content/uploads内のファイルに設定するパーミッション
PERFILE=664
# 翻訳ファイルの言語
LANG=ja
# コマンドライン引数にアップロードディレクトリ名が指定された時、
# そのディレクトリ内のファイルとディレクトリの所有者とパーミッションを設定します。
if [ $# -gt 0 ]; then
# コマンドライン引数に指定されたアップロードディレクトリ名を列挙します。
for i in $@
do
# コマンドライン引数に'../'が含まれているかどうかをチェックします。(意図しないディレクトリにアクセスするインジェクション対策)
if [ `echo $i | grep '\.\./'` ]; then
echo -e "\033[1;35mError\033[0;39m - Cannot use '../' in commandline argments for security."
# 指定されたアップロードディレクトリが存在するかチェックします。
elif [ -e $WPCDIR/uploads/$i ]; then
echo -e "\033[1;32mInfo.\033[0;39m - Directory:'wp-content/uploads/$i' is found."
# そのアップロードディレクトリ内の所有者とグループを設定します。
chown -R $OWNER:$GROUP $WPCDIR/uploads/$i
echo -e "\033[1;32mInfo.\033[0;39m - Set the files and directories owner in the directory:'wp-content/uploads/$i' to $OWNER:$GROUP."
# そのアップロードディレクトリ内のディレクトリのパーミッションを設定します。
find $WPCDIR/uploads/$i/ -type d -exec chmod $PERDIR {} \;
echo -e "\033[1;32mInfo.\033[0;39m - Set the directories permissions in the directory:'wp-content/uploads/$i' to $PERDIR."
# そのアップロードディレクトリ内のファイルのパーミッションを設定します。(但し、.htaccessと.htpasswdを除きます。)
find $WPCDIR/uploads/$i/ -type f -not -name '.htaccess' -not -name '.htpasswd' -exec chmod $PERFILE {} \;
echo -e "\033[1;32mInfo.\033[0;39m - Set the files permissions in the directory:'wp-content/uploads/$i' to $PERFILE."
else
echo -e "\033[1;35mError\033[0;39m - Directory:'wp-content/uploads/$i' is not found."
fi
done
# コマンドライン引数になにも指定していない場合、
else
# アップロードディレクトリ内のすべてのファイルとディレクトリの所有者及びグループを設定します。
chown -R $OWNER:$GROUP $WPCDIR/uploads
echo -e "\033[1;32mInfo.\033[0;39m - Set the files and directories owner in all of the uploads directory to $OWNER:$GROUP."
# アップロードディレクトリ内のすべてのディレクトリのパーミッションを設定します。
find $WPCDIR/uploads/ -type d -exec chmod $PERDIR {} \;
echo -e "\033[1;32mInfo.\033[0;39m - Set the directories permissions in all of the uploads directory to $PERDIR."
# アップロードディレクトリ内のすべてのファイルのパーミッションを設定します。(但し、.htaccessと.htpasswdを除きます。)
find $WPCDIR/uploads/ -type f -not -name '.htaccess' -not -name '.htpasswd' -exec chmod $PERFILE {} \;
echo -e "\033[1;32mInfo.\033[0;39m - Set the files permissions in the directory:'wp-content/uploads/$1' to $PERFILE."
fi
使い方
# すべてのアップロードファイルとディレクトリの所有者及びパーミッションを設定します。
$ bash uploads.sh
# ディレクトリ名(ここでは「lemon」)を指定して、所有者及びパーミッションを設定します。
$ bash uploads.sh lemon
# 複数のディレクトリ名(ここでは「lemon」、「lime」、「orange」)を指定して、所有者及びパーミッションを設定します。
$ bash uploads.sh lemon lime orange
# 2015年12月にWordPressのアップローダーでアップロードしたファイルの所有者及びパーミッションを設定します。
$ bash uploads.sh 2015/12
4. grepを使って、コマンドライン引数に「../」が含まれているかどうかをチェックしている目的
今回紹介したスクリプトの中で、コマンドライン引数に「../」が含まれているかどうかをチェックしており、「../」が含まれていた場合、エラーメッセージを表示して、その引数に対応するディレクトリの所有者とパーミッションの設定処理をスキップさせています。
# $i : コマンドライン引数
# コマンドライン引数に'../'が含まれているかどうかをチェックします。(意図しないディレクトリにアクセスするインジェクション対策)
if [ `echo $i | grep '\.\./'` ]; then
echo -e "\033[1;35mError\033[0;39m - Cannot use '../' in commandline argments for security."
これは、wp-content/plugins (またはthemesもしくはuploads) ディレクトリ以外にアクセスしてしまうのを防ぐためです。
例えば、2.のplugins.shスクリプトに引数「../../wp-admin」を付けて実行するとします。
$ bash plugins.sh ../../wp-admin
すると、変数**$iには「../../wp-admin**」が代入されるので、プラグインディレクトリ名を示す「$WPCDIR/plugins/$i」の値は「/var/www/html/wp-content/plugins/../../wp-admin」($WPCDIRの値が「/var/www/html/wp-content」の時)となりますが、これは「/var/www/html/wp-admin」こと、WordPressの管理画面のディレクトリと同じです。
5. おわりに
今回紹介した3つスクリプトファイルは、Gistにアップロードしています。
https://gist.github.com/Nia-TN1012/d7d8bbbd3288b2be88c95602d6b33b4b
使用する時は、間違ってサーバーの公開ディレクトリの中に置かないように気を付けてね。
それでは、See you next!