Sunday, May 30, 2010

帐号

1. 问题的来源
一直以来, 都有一种错误的认为: 使用useradd增加帐号和配置好ssh-key就可以使用此帐号登陆 .
实现上不如此. 这是需要usePAM的支持. PAM日后再细细学习, 现在先学习学习一般认证

于是, 把login和su命令的代码翻出来看看. 它们都使用如下的方式:

login(不扯PAM)的代码:
 798     pp = getpass(_("Password: "));
 799
 800 #  ifdef CRYPTOCARD
 801     if (strncmp(pp, "CRYPTO", 6) == 0) {
 802         if (pwd && cryptocard()) break;
 803     }
 804 #  endif /* CRYPTOCARD */
 805
 806     p = crypt(pp, salt);
 807     setpriority(PRIO_PROCESS, 0, 0);
.......
 835     if (pwd && !strcmp(p, pwd->pw_passwd))
 836       break;
 837
 838     printf(_("Login incorrect\n"));

su的代码:
265   unencrypted = getpass (_("Password:"));
266   if (!unencrypted)
267     {
268       error (0, 0, _("getpass: cannot open /dev/tty"));
269       return false;
270     }
271   encrypted = crypt (unencrypted, correct);
272   memset (unencrypted, 0, strlen (unencrypted));
273   return STREQ (encrypted, correct);
274 }

都是:
1. 使用了crypt函数
2. 最后都是字符串比较
3. 需要/etc/shadow里的加密后字符串
* 比红色部分可以看出getpass是在stdin被定重向后会报错(hard wire型的程序), 因为它使用了不回显功能(只有tty才有, pipe没有)

在python官网里看到可爱的python代码是这样的:
crypt.crypt(cleartext, cryptedpasswd) == cryptedpasswd

* 可以知道crypt函数的第二个参数可以是salt, 也可以不是salt, 而是加密后的字符串
* 自来http://docs.python.org/library/crypt.html?highlight=crypt#crypt.crypt



2. Lock 与 Unlock 是怎么一个概念?

在man 5 shadow得到的信息:

If the password field contains some string that is not valid result of crypt(3), for instance ! or *, the user will not be able to use a unix password to log in, subject to pam(7).

从上面的C代码和crypt函数的特性可以猜测: Lock无疑就是让方程两边永远不相等, 代码没有其它测试的必要!

具体到ssh的实例, 当使用一指定用户登陆ssh server时, server并不去分别Lock与不Lock的帐号,
虽然没有看过sshd的代码, 但是从表现上: 不会马上打印: Lock user或者马上disconnect, 如使用全世界都公认的halt默认帐号也是提示输入密码:
$ ssh halt@localhost
halt@localhost's password:

3. 其它
su切换用户
FAIL : 以为切换什么用户都是输入root密码
大概看了一把su的代码, 才知道不是这样的~~~~
知道了下面的真相:
1. 需要输入切换到用户的密码
2. restricted shell : 凡不在/etc/shells里登陆的shell都是受限shell. 为了安全的

nologin
平时nologin默认打印信息是: This account is currently not available.
你也是可以修改的: /etc/nologin.txt, 例如下面的例子:

$ sudo su rpc
我顶~ 乱来~ 去问你妈要密码, 不然见一次骂一次
 * 这个例子有点geek~





No comments:

Post a Comment

Note: Only a member of this blog may post a comment.