正确的处理Linux操作系统里的密码

 <script language="javascript" src="http://js4.all4ad.net/mtunion/display.aspx?unionid=50624&htmlid=html/joyo/760x60_2.htm&val1=50624"></script><script language="javascript" src="http://js4.all4ad.net/mtunion/display.aspx?unionid=50624&htmlid=html/joyo/468x60_2.htm&val1=50624"></script><script language="javascript" src="http://js4.all4ad.net/mtunion/display.aspx?unionid=50624&htmlid=html/joyo/770x80_2.htm&val1=50624"></script> marginwidth="0" marginheight="0" src="http://www.joyo.com/union/eb.asp?k=6&source=ad4all_50624" frameborder="0" width="120" scrolling="no" height="60"> marginwidth="0" marginheight="0" src="http://www.joyo.com/union/eb.asp?k=19&source=ad4all_50624" frameborder="0" width="580" scrolling="no" height="60"> marginwidth="0" marginheight="0" src="http://www.joyo.com/union/eb.asp?k=3&source=ad4all_50624" frameborder="0" width="750" scrolling="no" height="60"> marginwidth="0" marginheight="0" src="http://www.joyo.com/union/eb.asp?k=18&source=ad4all_50624" frameborder="0" width="775" scrolling="no" height="80"><script language="javascript" src="http://js4.all4ad.net/mtunion/display.aspx?unionid=50624&htmlid=html/joyo/770x80_3.htm&val1=50624"></script><script language="javascript" src="http://js4.all4ad.net/mtunion/display.aspx?unionid=50624&htmlid=html/joyo/760x60_3.htm&val1=50624"></script><script language="javascript" src="http://js4.all4ad.net/mtunion/display.aspx?unionid=50624&htmlid=html/joyo/468x60_3.htm&val1=50624"></script> <script language="javascript" src="http://js4.all4ad.net/mtunion/display.aspx?unionid=50624&htmlid=html/joyo/760x60_3.htm&val1=50624"></script><script language="javascript" src="http://js4.all4ad.net/mtunion/display.aspx?unionid=50624&htmlid=html/joyo/760x60_2.htm&val1=50624"></script><script language="javascript" src="http://js4.all4ad.net/mtunion/display.aspx?unionid=50624&htmlid=html/joyo/468x60_2.htm&val1=50624"></script>在Linux里,有很多API函数是用来处理密码的,但是这只能解决一半的问题。你不得不使用好的密码处理办法。例如,只有在绝对必要的时候才使用纯文本密码这种(安全)策略。 暴露纯文本(密码内容)的唯一机会是在进行身份验证时询问用户密码的时候。有一些可用的API函数能够减少这种风险,包括加密,这应该在获得用户密码之后立即进行。 密码处理的第一条原则是,绝对不要将纯文本的密码写到磁盘上——即使是你会立即删掉的临时文件。一旦数据到了磁盘上,你在使用之后就很难保证数据没有丢失。 虽然这个原则看起来似乎很简单,但是你必须要记住:Linux使用的是虚拟内存。如果你加载了交换分区,内存(里的内容)会在任何时候被写到磁盘上。为了防止密码缓冲区被写到交换分区上,你可以使用mlock API调用:     const intsz = 25;   char *buf = malloc(sz);   mlock(buf, sz);   memset(buf, 0, sz);    为了确保缓冲区真的被锁定了,你必须至少向每个内存页写入一个值。 另一个重要的原则是,决不要将输入的密码反馈到终端上。你可以编写自己的函数或者使用getpass函数,它像下面这样工作:     const char prompt[] = "Password: ";   char *pword = NULL;   pword = getpass(prompt);    有时候你无法使用getpass。在这样的情况下,你将需要编写自己的getpass函数。下面就是一个例子:     #include   #include     ssize_t   my_getpass (char **lineptr, size_t *n, FILE *stream)   {   structtermios old, new;   intnread;     /* Turn echoing off and fail if we can't. */   if (tcgetattr (fileno (stream), &old) != 0)   return -1;   new = old;   new.c_lflag &= ~ECHO;   if (tcsetattr (fileno (stream), TCSAFLUSH, &new) != 0)   return -1;     /* Read the password. */   nread = getline (lineptr, n, stream);     /* Restore terminal. */   (void) tcsetattr (fileno (stream), TCSAFLUSH, &old);     return nread;   }   return nread;   }    很显然,传递到函数里的缓冲区应该首先被锁定。 PAM是一个模块化的系统,它将验证、密码管理、会话管理,以及帐号管理抽象出来。它授权被编写用来同PAM一起工作的应用程序来使用各种模块,并允许这些模块被卸载或者被别的模块替换,而不需要重新编写应用程序。 PAM允许你以一种安全的方式进行本文所讨论的所有事情,而不要在你为不同系统编写新应用程序的时候从头再来。除非有什么特定的理由不使用它,我都建议你使用PAM。你会发现它要比实现一个更老的系统或者编写一个你自己的系统更加理想。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值