Squeezeで制限ユーザを作成する(rbash)

■参考

 超制限付きユーザーの作り方
 http://ameblo.jp/itboy/entry-10054454911.html

 特定のコマンドしか実行できないユーザーIDを作成するには
 http://www.atmarkit.co.jp/flinux/rensai/linuxtips/363rbashuser.html

■Squeezeには既に「rbash」がある。
 制限されたbash。

$ whereis rbash
rbash: /bin/rbash /usr/share/man/man1/rbash.1.gz

$ ls -l /bin/rbash | sed s%".*"/"%"/%g
/rbash -> bash

$ grep rbash /etc/shells
/bin/rbash

$ apropos rbash
rbash (1)            - restricted bash, see bash(1)

■細かい制限も出来るが、ひとまずデフォルトで。

$  man -P lv bash | grep -A 40 ^REST | grep -v "^\$"| sed s/"·"/"\."/g
RESTRICTED SHELL
       If bash is started with the name rbash, or the -r option is supplied at
       invocation, the shell becomes restricted.  A restricted shell  is  used
       to  set  up an environment more controlled than the standard shell.  It
       behaves identically to bash with the exception that the  following  are
       disallowed or not performed:
       .      changing directories with cd
       .      setting or unsetting the values of SHELL, PATH, ENV, or BASH_ENV
       .      specifying command names containing /
       .      specifying  a  file  name containing a / as an argument to the .
              builtin command
       .      Specifying a filename containing a slash as an argument  to  the
              -p option to the hash builtin command
       .      importing  function  definitions  from  the shell environment at
              startup
       .      parsing the value of SHELLOPTS from  the  shell  environment  at
              startup
       .      redirecting output using the >, >|, <>, >&, &>, and >> redirect‐
              ion operators
       .      using the exec builtin command to replace the shell with another
              command
       .      adding  or  deleting builtin commands with the -f and -d options
              to the enable builtin command
       .      Using the  enable  builtin  command  to  enable  disabled  shell
              builtins
       .      specifying the -p option to the command builtin command
       .      turning off restricted mode with set +r or set +o restricted.

■「useradd」コマンドに「-k」を渡さなければ、「/etc/skel」が参照される。

$ man useradd | nkf -Lw -c -w -f70 | grep -B 1 -A 5 " \-k オプションを" | sed s/""//g
 -m ホームディレクトリが存在しない場合には、ホームディレクトリを作成す
る。 -k オプションを同時に指定すると skeleton_dir 以下のファイルが、
指定しないと /etc/skel 以下のファイルが、 ホームディレクトリにコピーさ
れる。 ホームディレクトリには、 skeleton_dir または /etc/skel に含まれ
るすべてのディレクトリも作成される。 -k オプションは、 -m オプションと
ともに使われる場合のみ有効である。 デフォルトでは、ホームディレクトリ
を作らず、ファイルのコピーもしない。

■デフォルトの「/etc/skel」配下のファイルは以下。

$ find /etc/skel/ -type f -print | sed s%"/etc/skel/"%%g
.bashrc
.profile
.bash_logout

■PATHの設定があるのは「.profile」

$ find /etc/skel/ -type f -print | grep -v "^#\|^\$" `xargs` | grep PATH
/etc/skel/.profile:    PATH="$HOME/bin:$PATH"

■limitユーザの作成。パスワードもssh鍵も作成する。
 ※パスワードは変えましょう。

$ U="limituser"; \
  PASS="limituser";
  sudo useradd -m -d /home/$U -s /bin/rbash $U; \
  (sleep 1;echo "$PASS";sleep 1;echo "$PASS";) | sudo passwd $U; \
  sudo chown root:root /home/$U/.profile; \
  sudo chmod 755 /home/$U/.profile; \
  echo "" | sudo -u limituser ssh-keygen -t rsa -N "$PASS"
  echo -e "export HOME=/home/$U\nexport PATH=/home/$U\nexport SHELL=/bin/rbash" | sudo tee -a /home/$U/.profile; \
  unset U PASS
新しいUNIXパスワードを入力してください:新しいUNIX パスワードを再入力してください:passwd: パスワードは正しく更新されました
Generating public/private rsa key pair.
Enter file in which to save the key (/home/limituser/.ssh/id_rsa): Created directory '/home/limituser/.ssh'.
Your identification has been saved in /home/limituser/.ssh/id_rsa.
Your public key has been saved in /home/limituser/.ssh/id_rsa.pub.
The key fingerprint is:
XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX limituser@dummyhost
The key’s randomart image is:
+--[ RSA 2048]----+
|              XXX|
|           X X XX|
|          X X X X|
|         X X X X |
|        X X X X X|
|       X X X X X |
|        X X   X  |
|         X     X |
|                 |
X-----------------X

export HOME=/home/limituser
export PATH=/home/limituser
export SHELL=/bin/rbash

■やり直す、または、ユーザの削除。

$ sudo userdel limituser;sudo rm -fr /home/limituser/

■sshログイン。パスが通ってないだけでは無い。

$ ssh -l limituser 127.0.0.1
$ ls
-bash: ls: コマンドが見つかりません
$ /bin/ls
-rbash: /bin/ls: 制限:  '/' をコマンド名に記述できません
$ exit
logout
-rbash: /usr/bin/clear_console: 制限:  '/' をコマンド名に記述できません
Connection to 127.0.0.1 closed.

■私にだけ「su」出来るようにする。

$ echo "alias su='su labunix'" | sudo tee -a /home/limituser/.profile
alias su='su labunix'
$ sudo ln -s /bin/su /home/limituser/su