Linux Security Shell(原创)

来源:互联网 发布:linux启动网卡 编辑:程序博客网 时间:2024/05/22 15:25

2005/1/18 12:42:23

       Shell linux的人机交互界面,在linux中提供了多套shell可供用户使用,用户可以根据自己的喜好来选择不同的shell

       用户登录系统时,login程序,去读/etc/passwd文件,从中读取。

       Passwd文件的结构为

       account:passsword:UID:GID:GECOS:directory:shell

账号:加密后密码或*:用户ID:ID:Addition Information:$HOME目录:shell,空时用/bin/sh

 

以后的讨论在假定 shell BASH.

shell启动后,根据系统配置文件/etc/profile和用户配置文件$HOME/.profile来初始化shell环境。在.profileshell脚本将会被执行,利用这一点,我们就可以实现我们的Security shell,并且可以满足如下的条件。

 

1.  不备CTRL-C,CTRL-D,TERM等信号所终止。

2.  实现菜单化界面,用户可以选择操作。

3.  界面方便定制,可以通过shell Script来实现。

4.  灵活,可以根据需要添加,修改或删除操作。

 

其实,Security shell运行在系统shell OS,$HOME/.profile文件中的脚本程序。

可以通过trap来处理CTRL-C,INT等信号,而不是对出Security shelltrap本来是用来调试目的的,可以指定脚本程序对某个信号的相应。

信号是系统或其他进程发给指定进程的一个消息,通常是Terminate。如果要忽略CTRL-C中断,仅需要下面的指令。

trap “” 2

# “” 指定的动作,”” 表示什么也不执行,2是指中断信号(Interrupt signal)

trap 2

恢复正常的中断响应

trap ‘echo “CTRL-C disabled.”’ 2

# Ctrl-C发生时,将会显示 CTRL-C disabled.

 

trap 'echo Will Exit' EXIT

# EXIT is the name of the signal generated upon exit from a script.

 

如果要对一个以上的信号作处理,可以采用下面的命令

trap “ … “ sig1 sig2 sig3 ….

下面给出了linux 系统的信号列表

 

# /* Signal List */

#define SIGHUP          1       /* Hangup (POSIX).  */

#define SIGINT          2       /* Interrupt (ANSI).  */

#define SIGQUIT         3       /* Quit (POSIX).  */

#define SIGILL          4       /* Illegal instruction (ANSI).  */

#define SIGTRAP         5       /* Trace trap (POSIX).  */

#define SIGABRT         6       /* Abort (ANSI).  */

#define SIGIOT          6       /* IOT trap (4.2 BSD).  */

#define SIGBUS          7       /* BUS error (4.2 BSD).  */

#define SIGFPE          8       /* Floating-point exception (ANSI).  */

#define SIGKILL         9       /* Kill, unblockable (POSIX).  */

#define SIGUSR1         10      /* User-defined signal 1 (POSIX).  */

#define SIGSEGV         11      /* Segmentation violation (ANSI).  */

#define SIGUSR2         12      /* User-defined signal 2 (POSIX).  */

#define SIGPIPE         13      /* Broken pipe (POSIX).  */

#define SIGALRM         14      /* Alarm clock (POSIX).  */

#define SIGTERM         15      /* Termination (ANSI).  */

#define SIGSTKFLT       16      /* Stack fault.  */

#define SIGCLD          SIGCHLD /* Same as SIGCHLD (System V).  */

#define SIGCHLD         17      /* Child status has changed (POSIX).  */

#define SIGCONT         18      /* Continue (POSIX).  */

#define SIGSTOP         19      /* Stop, unblockable (POSIX).  */

#define SIGTSTP         20      /* Keyboard stop (POSIX).  */

#define SIGTTIN         21      /* Background read from tty (POSIX).  */

#define SIGTTOU         22      /* Background write to tty (POSIX).  */

#define SIGURG          23      /* Urgent condition on socket (4.2 BSD).  */

#define SIGXCPU         24      /* CPU limit exceeded (4.2 BSD).  */

#define SIGXFSZ         25      /* File size limit exceeded (4.2 BSD).  */

#define SIGVTALRM       26      /* Virtual alarm clock (4.2 BSD).  */

#define SIGPROF         27      /* Profiling alarm clock (4.2 BSD).  */

#define SIGWINCH        28      /* Window size change (4.3 BSD, Sun).  */

#define SIGPOLL         SIGIO   /* Pollable event occurred (System V).  */

#define SIGIO           29      /* I/O now possible (4.2 BSD).  */

#define SIGPWR          30      /* Power failure restart (System V).  */

#define SIGSYS          31      /* Bad system call.  */

#define SIGUNUSED       31

 

#define _NSIG           64      /* Biggest signal number + 1

 

其中 2 3 9 15 10 12 是比较关注的信号。

 

Linux系统提供了dialog 这个命令(用来产生checklist,infobox,inputbox,menu,msgbox,password,radiolist,yesno,textbox等界面的对话框),可以提供文本图形的交互方式。只需要少量的编成工作就可以产生漂亮的界面。同时,这个命令用法复杂,可以通过man dialog获取更多的信息,附件中的俩个脚本程序可做参考。

 

如果没有办法,就只用通过shellselect,echo等基本命令来产生界面了。

      

       Security shell的主体是个循环函数,由于利用trap对信号作了处理,所以,这个循环是无法结束的。

 

while true

do

if [ $DFLAG = 0 ]; then

       SelectServiceDlg

       OP=`cat $TMP/select`

       rm $TMP/select -f

      

       if  [ "$OP" = "OS NetWork" ]; then

              OsNetWork

       elif [ "$OP" = "NetWork" ]; then

             NetWork

       elif [ "$OP" = "Authentication" ]; then

             ChangeAuthentication

       elif [ "$OP" = "ChangePasswd" ]; then

             ChangePasswd

       elif [ "$OP" = "RegCert" ]; then

             RegCert

      else

             clear

       fi

else

       SelectService

fi

done

 

不同的操作在子程序中完成,Linux所提供的命令可以在子程序中调用。

 

附件1: Security shell,复制到 $HOME/.profile,用户具有可以访问的权限即可执行。

 

附件2: netconfig,  slackware linux中的网络配置脚本程序,可以作为参考。

 

 

原创粉丝点击