欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

linux用戶及密碼的存儲和校驗方法

51CTO博客   發(fā)布時間:2022-05-22 15:32:49   作者:五個板栗   我要評論
這篇文章主要介紹了linux用戶及密碼的存儲和校驗方法, linux中用戶和密碼由/etc/passwd文件和/etc/shadow文件存儲。更多相關(guān)內(nèi)容介紹需要的小伙伴可以參考下面文章內(nèi)容

前言:

 在linux中,用戶和密碼由/etc/passwd文件和/etc/shadow文件存儲。

/etc/passwd文件

在passwd中,每一行代表一個用戶記錄,每條記錄分為7個字段,字段間用':'分割,以下是每個字段的描述。
  • 用戶名:用戶綁定的名字
  • 加密后的密碼(可選)
  • 用戶id,用來標(biāo)識用戶
  • 用戶組id,表示用戶所屬組
  • 用戶描述信息
  • 用戶的家目錄
  • 用戶命令解釋器
如果第二項密碼字段為一個小寫的'x',則表示用戶的密碼存儲在/etc/shadow文件中,即在/etc/shadow文件中也有一行對應(yīng)該用戶記錄的記錄。

/etc/shadow文件

shadow是一個包含系統(tǒng)帳戶密碼信息和可選時間信息的文件。shadow文件每一行代表一條記錄,每一條記錄有9個字段,以下是每個字段的描述。

登錄用戶名:必須對應(yīng)到/etc/passwd文件中的一條記錄中的用戶登錄名

  • 加密密碼:加密后的密碼字符串
  • 最后一次密碼修改日期
  • 用戶更改密碼前必須等待的最小密碼期限
  • 用戶必須更改密碼的最大密碼期限
  • 密碼過期警告時間
  • 密碼不活動期
  • 用戶到期時間
  • 保留,用于將來擴展其他屬性

測試用戶

新建一個用戶test,設(shè)置密碼為test,然后查看其passwd記錄以及shadow記錄。

└─$ sudo adduser --home /home/test --shell /bin/bash test
Adding user `test' ...
Adding new group `test' (1001) ...
configuration error - unknown item 'NONEXISTENT' (notify administrator)
configuration error - unknown item 'PREVENT_NO_AUTH' (notify administrator)
Adding new user `test' (1001) with group `test' ...
configuration error - unknown item 'NONEXISTENT' (notify administrator)
configuration error - unknown item 'PREVENT_NO_AUTH' (notify administrator)
Creating home directory `/home/test' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for test
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] yes
└─$ cat /etc/passwd | grep test
test:x:1001:1001:,,,:/home/test:/bin/bash
└─$ sudo cat /etc/shadow | grep test
test:$y$j9T$1L/PH8ddqysyageBD6TnF1$MNm4An/z6LHdRq0JEOnFuviXBequ23gbIG8U1A/f4F.:19
112:0:99999:7:::

用戶驗證API

可使用crypt和shadow的api對用戶和密碼進行驗證

#include <crypt.h>
#include <unistd.h>
char *crypt(const char *key, const char *salt);
/* General shadow password file API */
#include <shadow.h>
struct spwd *getspnam(const char *name);
struct spwd *getspent(void);
void setspent(void);
void endspent(void);
struct spwd *fgetspent(FILE *stream);
struct spwd *sgetspent(const char *s);
int putspent(const struct spwd *p, FILE *stream);
int lckpwdf(void);
int ulckpwdf(void);

下面的代碼實例讓用戶輸入用戶名和密碼,通過crypt和shadow API進行檢驗用戶輸入的用戶名和密碼是否匹配?

#include <unistd.h>
#include <stdio.h>
#include <shadow.h>
#include <crypt.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
/*
* user_auth - Authentication user name and password.
* @user: user name.
* @passwd: user password string.
*
* on success return 0, return errno on error.
*/
int user_auth(const char *user, const char *passwd)
{
  struct spwd *user_spwd;
  char *encrypted_pwd;
    if (!user || !passwd)
      return -EINVAL;
  user_spwd = getspnam(user);
    if (!user_spwd)
      return -errno;
  encrypted_pwd = crypt(passwd, user_spwd->sp_pwdp);
    if (!encrypted_pwd)
      return -errno;
      return abs(strcmp(encrypted_pwd, user_spwd->sp_pwdp));
}
int main(int argc, char *argv[])
{
  int ret;  
  char *user = argv[1];
  char *passwd = argv[2];
  ret = user_auth(user, passwd);
  if (ret < 0) {
    printf("user auth failed, ret=%d\n", ret);
    return ret;
}
  if (ret > 0) {
    printf("passwd not match, auth failed\n");
    return ret;
}
    printf("auth okay!\n");
    return 0;
}

編譯該程序是指定鏈接庫crypt

gcc auth.c -lcrypt

然后使用該程序驗證我們添加的用戶test,密碼是test,正確的輸入用戶名和密碼時輸出結(jié)果如下:

└─$ sudo ./a.out test test
auth okay!

如果故意輸入錯誤密碼,再次執(zhí)行得到如下結(jié)果:

└─$ sudo ./a.out test hello
passwd not match, auth failed

到此這篇關(guān)于linux用戶及密碼的存儲和校驗方法的文章就介紹到這了,更多相關(guān)linux存儲和校驗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論