免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: 独孤九贱
打印 上一主题 下一主题

请教一个Shell配置方面的问题 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2006-02-16 11:34 |只看该作者
原帖由 寂寞烈火 于 2006-2-16 11:25 发表
将拥护的shell加个-r,将其限制,然后把常用的命令拷到这个拥护的家目录

man bash
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 >>  redirec-
              tion 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.

       These restrictions are enforced after any startup files are read.

       When a command that is found to be a shell script is executed (see COM-
       MAND  EXECUTION  above),  rbash turns off any restrictions in the shell
       spawned to execute the script.

又学一招

论坛徽章:
0
12 [报告]
发表于 2006-02-16 11:43 |只看该作者

回复 11楼 platinum 的帖子

谢谢大家啦!

其实这种要求一点也不奇怪,就是要求非root用户登录后,只能运行当前目录的命令,而不访问其它任何资源!一般的嵌入式系统都要这种需求吧!

也就是bash -r那种类似效果了!

网上搜了大半天,没有个所以然,有说可以修改profile,达到这种目的,正在找详细配置说明!

论坛徽章:
0
13 [报告]
发表于 2006-02-16 11:44 |只看该作者
原帖由 独孤九贱 于 2006-2-16 11:43 发表
其实这种要求一点也不奇怪,就是要求非root用户登录后,只能运行当前目录的命令,而不访问其它任何资源!一般的嵌入式系统都要这种需求吧!

如果做嵌入式的话,恐怕是要自己编程写 shell 程序的吧

论坛徽章:
0
14 [报告]
发表于 2006-02-16 11:47 |只看该作者
原帖由 platinum 于 2006-2-16 11:44 发表

如果做嵌入式的话,恐怕是要自己编程写 shell 程序的吧


应该不用了,正在看profile 的配置,希望有帮助;
另外,楼上的chinaux 那篇贴子,非常值得参考!

唯一担心的就是:
Some commands require library files under /usr/lib, so when such a command is copied, its libary file needs to be copied to the directory /home/xxx/lib as well.

想请问一下chinaux,如果程序被限制进来后,有什么办法不影响程序本身对原有根文件系统的库、文件等资源的访问呢?
比如,我配置了网卡,需要一个程序去修改/etc/sysconfig/network-scripts/XXX等诸如此类的……
我也是参考了下边这段话:

——————————————————————————————————————————————————
4.受限shell
  bash本身也有一定的限制功能,就是通过其(受限shell)restricted shell功能,所做的限制包括:
阻止用户改变目录和环境变量;
重定向输出;
使用绝对路径运行命令;
使用exec命令和其它一些操作。

如果bash是以rbash为名启动,或者使用-r选项运行,它就称为受限shell。受限shell能够使系统环境更好控制。如果启动受限shell,以下操作将被禁止:
使用cd命令切换目录;
设置或者取消SHELL、PATH、ENV或者BASH_ENV环境变量;
以绝对路径运行命令; 使用绝对路径指定的文件名作为内置命令.的参数;
使用绝对路径指定的文件名作为内置命令hash的参数;
在启动时,从shell环境导入函数的定义;
在启动时,解析SHELLOPTS的值; 使用>、>|、<>、>&、&>和>>等重定向操作重定向输出;
使用exec命令使其它的命令代替当前shell;
使用-f和-d选项打开/关闭内置命令;
对内置命令使用-p选项;
关闭受限模式。

  下面我们实际试验一下:
#aduser nixe0n
#passwd nixe0n
#ln -s /bin/bash /bin/rbash
#echo "/bin/rbash">>/etc/shells
#chsh -s /bin/rbash nixe0n
#cd ~nixe0n
#su nixe0n
$
  然后,执行:   $cd /   就会出现以下错误:   rbash: cd: restricted

[ 本帖最后由 独孤九贱 于 2006-2-16 12:30 编辑 ]

论坛徽章:
0
15 [报告]
发表于 2006-02-16 11:49 |只看该作者
acl试试?

论坛徽章:
0
16 [报告]
发表于 2006-02-16 14:42 |只看该作者
原帖由 独孤九贱 于 2006-2-16 11:47 发表
想请问一下chinaux,如果程序被限制进来后,有什么办法不影响程序本身对原有根文件系统的库、文件等资源的访问呢?
比如,我配置了网卡,需要一个程序去修改/etc/sysconfig/network-scripts/XXX等诸如此类的……



It can be done via sudo, and the binary is no need to be copied to /home/xxx/bin. only "sudo" needs to be copied.

e.g., I'd like the restricted user "xxx" can run "lsof" as root, and run "dmesg" as the user of "chinaux":
1. # cp -a /usr/sbin/sudo /x/home/bin
2. # visudo
and add the followiong 2 lines:
xxx             ALL=/usr/sbin/lsof
xxx             ALL=(chinaux) /bin/dmesg

then, "xxx" can run "sudo  /usr/sbin/lsof" & "sudo -u chinaux /bin/dmesg" (don't configure "vi" in sudo)

as for file modification, if the file is owned by "xxx", then copy "/bin/rvi" (restricted vi, it comes from vim-minimal, see vi for more info)  to /home/xxx/bin, "xxx" will be able to run "rvi /anypath/a_file_owned_by_xxx"; if the owned by the other,  you might have to do more:
1. mkdir /home/xxx/tmp; touch  /xxx /home/xxx/tmp/me; chown -R xxx /home/xxx/tmp
2. configure the 2 lines on sudoers:

xxx ALL=(xxx) /bin/cp /etc/sysconfig/network-scripts/ifcfg-eth0 /xxx /home/xxx/tmp
xxx ALL=/bin/cp /xxx /home/xxx/tmp/ifcfg-eth0  /etc/sysconfig/network-scripts

and then, "xxx" runs the following to modify /etc/sysconfig/network-scripts/ifcfg-eth0:

$ sudo -u xxx /bin/cp /etc/sysconfig/network-scripts/ifcfg-eth0 /xxx /home/xxx/tmp
$ rvi tmp/ifcfg-eth0
$ sudo /bin/cp /xxx /home/xxx/tmp/ifcfg-eth0  /etc/sysconfig/network-scripts

to avoid repeatedly typing the password,  put "NOPASSWD:" just in the front of the command, e.g.:
xxx             ALL=(chinaux) NOPASSWD: /bin/dmesg

[ 本帖最后由 chinaux 于 2006-2-16 14:48 编辑 ]

论坛徽章:
0
17 [报告]
发表于 2006-02-16 14:51 |只看该作者
原帖由 chinaux 于 2006-2-16 14:42 发表
if the owned by the other,  you might have to do more:
1. mkdir /home/xxx/tmp; touch  /xxx /home/xxx/tmp/me; chown -R xxx /home/xxx/tmp
2. configure the 2 lines on sudoers:

xxx ALL=(xxx) /bin/cp /etc/sysconfig/network-scripts/ifcfg-eth0 /xxx /home/xxx/tmp
xxx ALL=/bin/cp /xxx /home/xxx/tmp/ifcfg-eth0  /etc/sysconfig/network-scripts

and then, "xxx" runs the following to modify /etc/sysconfig/network-scripts/ifcfg-eth0:

$ sudo -u xxx /bin/cp /etc/sysconfig/network-scripts/ifcfg-eth0 /xxx /home/xxx/tmp
$ rvi tmp/ifcfg-eth0
$ sudo /bin/cp /xxx /home/xxx/tmp/ifcfg-eth0  /etc/sysconfig/network-scripts


those can be simplified:

configure the line in sudo:
xxx            ALL=/bin/rvi /etc/sysconfig/network-scripts/ifcfg-eth0

论坛徽章:
0
18 [报告]
发表于 2006-02-16 15:01 |只看该作者
原帖由 jiecho 于 2006-2-16 11:49 发表
acl试试?



no ....

论坛徽章:
0
19 [报告]
发表于 2006-02-16 16:20 |只看该作者
理论上讲selinux也可以实现,这个可以微调的灵活程度就非常好了,据称有个老外把一台机器root开放给外面,用selinux作限制,1年内保证了机器不倒。

论坛徽章:
0
20 [报告]
发表于 2006-02-16 19:58 |只看该作者
原帖由 colddawn 于 2006-2-16 16:20 发表
理论上讲selinux也可以实现,这个可以微调的灵活程度就非常好了,据称有个老外把一台机器root开放给外面,用selinux作限制,1年内保证了机器不倒。


试了N种办法,都没有满意,决定今晚赶工,自己写一个Shell……运行好了,改天把代码贴上来
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP