系统数据文件和信息

/etc/passwd

NAME
       getpwnam, getpwnam_r, getpwuid, getpwuid_r - get password file entry

SYNOPSIS
       #include <sys/types.h>
       #include <pwd.h>

       struct passwd *getpwnam(const char *name);
//传入一个文件名,获得一个struct passwd * 指针
       struct passwd *getpwuid(uid_t uid);
       
struct passwd {
           char   *pw_name;       /* username */
           char   *pw_passwd;     /* user password */
           uid_t   pw_uid;        /* user ID */
           gid_t   pw_gid;        /* group ID */
           char   *pw_gecos;    /* user information */
           char   *pw_dir;        /* home directory */
           char   *pw_shell;      /* shell program */
           };

getpwuid()的实现

//getpwuid 实现
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<pwd.h>

int main(int argc,char ** argv)
{
    struct passwd *pwdline;
    if(argc < 2)
    {
        fprintf(stderr,"Usage...\n");
        exit(1);
    }


    pwdline = getpwuid(atoi(argv[1]));

    puts(pwdline->pw_name);

    exit(0);
}

/etc/group

getgrgid()   getgrnam()
NAME
       getgrnam, getgrnam_r, getgrgid, getgrgid_r - get group file entry

SYNOPSIS
       #include <sys/types.h>
       #include <grp.h>

       struct group *getgrnam(const char *name);

       struct group *getgrgid(gid_t gid);


struct group {
           char   *gr_name;        /* group name */
           char   *gr_passwd;      /* group password */
           gid_t   gr_gid;         /* group ID */
           char  **gr_mem;         /* NULL-terminated array of pointers  to names of group members */
           };

/etc/shadow

由于/etc/passwd文件是所有用户都可读的,如果用户的密码太简单或规律比较明显的话,一台普通的计算机就能够很容易地将它破解,因此对安全性要求较高的Linux系统都把加密后的口令字分离出来,单独存放在一个文件中,这个文件是/etc/shadow文件。 有超级用户才拥有该文件读权限,这就保证了用户密码的安全性。
详细信息见此链接
getspnam() 、 crypt() //用来加密

NAME
       getspnam,  getspnam_r,  getspent, getspent_r, setspent, endspent, fgetspent, fgetspent_r, sgetspent,
       sgetspent_r, putspent, lckpwdf, ulckpwdf - get shadow password file entry

SYNOPSIS
       /* General shadow password file API */
       #include <shadow.h>

       struct spwd *getspnam(const char *name);

       struct spwd *getspent(void);


struct spwd {
           char *sp_namp;     /* Login name */
           char *sp_pwdp;     /* Encrypted password */
           long  sp_lstchg;   /* Date of last change(measured in days since 1970-01-01 00:00:00 +0000 (UTC)) */
           long  sp_min;      /* Min # of days between changes */
           long  sp_max;      /* Max # of days between changes */
           long  sp_warn;     /* # of days before password expires to warn user to change it */
           long  sp_inact;    /* # of days after password expires until account is disabled */
           long  sp_expire;   /* Date when account expires(measured in days since 1970-01-01 00:00:00 +0000 (UTC)) */
           unsigned long sp_flag;  /* Reserved */
           };

NAME
       crypt, crypt_r - password and data encryption

SYNOPSIS
       #define _XOPEN_SOURCE       /* See feature_test_macros(7) */  宏定义,所以还需要往makefile 中加入一个宏 -D__XOPEN_SOURCE
       #include <unistd.h>

       char *crypt(const char *key, const char *salt);
    //第一个参数:原串 第二个参数:掺杂的串  
    //通过指定方式返回char * 指针

ctypt()详细信息点此

//一个简单的确认用户名的程序
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<shadow.h>
#include<string.h>
int main(int argc,char** argv)
{
    char *input_pass;
    struct spwd *shdowline;
    char *crypted_pass;
    if(argc < 2)
    {
        fprintf(stderr,"Usage...\n");
        exit(1);
    }                                      //getpass - get a password
    input_pass = getpass("PassWord:");     //获得原文    char *getpass(const char *prompt);

    shdowline = getspnam(argv[1]);        //获得格式    struct spwd *getspnam(const char *name);


    //char *crypt(const char *key, const char *salt);
    crypted_pass = crypt(input_pass,shdowline->sp_pwdp);  //进行加密 
    

    //验证口令是否一样
    if(strcmp(shdowline->sp_pwdp,crypted_pass)==0)
    {
        puts("OK");
    }
    else{
        puts("wrong.");
    }
    exit(0);
}

时间戳

详细解释

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值