linux当中的用户和组

9700阅读 0评论2020-01-19 stolennnxb
分类:LINUX

用户和组ID的主要用途有:
1. 确定各种系统资源的所有权;
2. 对赋予进程访问上述资源的权限加以控制

密码文件:/etc/passwd
针对系统中的每一个用户账号,这个文件都会有一行数据进行描述,每一行包含7个field,之间用冒号分割,如下:

点击(此处)折叠或打开

  1. abc:x:1000:100:Michael Kerrisk:/home/abc:/bin/bash

这7个field分别表示:
1. 登录名
2. 经过加密的密码(一版都启用shadow密码,这个字段会是字符x)
3. 用户id,UID
4. 组id, GID
5. 注释
6. 主目录
7. 登录shell
在单机系统中,所有密码信息都存储在/etc/passwd文件中。然而,如果使用了NIS或LDAP在网络环境中分发密码,那么部分密码信息可能会由远端系统保存,但对应用程序来说都是透明的。

shadow密码文件/etc/shadow

其理念是:用户的所有非敏感信息存放于“人人可读”的密码文件中,而经过加密处理的用户密码则由shadow密码文件单独维护,仅供拥有特权的程序读取
shadow密码文件包含有登录名、经过加密的密码、以及其他若干与安全性相关的字段

组文件:/etc/group
系统中每个组在族文件/etc/group中都对应着一条记录,每条记录包含4个字段,之间以冒号分隔,分别表示为:1. 组名;2. 经过加密处理的密码;3. 组id, 用户列表

获取用户和组的信息:

点击(此处)折叠或打开

  1. #include <pwd.h>
  2. struct passwd *getpwnam(const char *name);
  3. struct passwd *getpwuid(uid_t uid);
  4. /*both return a pointer on success, or NULL on error;找不到时不设置errno*/
返回指针指向如下结构体,各个字段分别对应上述7个字段:

点击(此处)折叠或打开

  1. struct passwd{
  2.     char *pw_name;
  3.     char *pw_passwd;
  4.     uid_t pw_uid;
  5.     gid_t pw_gid;
  6.     char *pw_gecos;
  7.     char *pw_dir;
  8.     char *pw_shell;
  9. };
返回的指针指向一个静态分配的结构,而其中包含的指针也是指向静态分配的信息,用法如下:

点击(此处)折叠或打开

  1. struct passwd *pwd;
  2. errno = 0;
  3. pwd = getpwnam(name);
  4. if(pwd == NULL){
  5.     if (errno == 0)
  6.         /*not found*/;
  7.     else
  8.         /*error*/;
  9. }
在组文件中获取记录同上:


点击(此处)折叠或打开

  1. #include <grp.h>
  2. struct group *getgrnam(const char *name);
  3. struct group *getgrgid(gid_t gid);
  4. /*both return a pointer on success, or NULL on error*/
  5. struct group{
  6.     char *gr_name;
  7.     char *gr_passwd;
  8.     gid_t gr_gid;
  9.     char **gr_mem;
  10. };
扫描密码文件和组文件中的所有记录:

点击(此处)折叠或打开

  1. #include <pwd.h>
  2. struct passwd *getpwent(void);
  3. /*returns pointer on success, or NULL on end of stream or error*/
  4. void setpwent(void);/*重返文件起始处*/
  5. void endpwent(void);/*关闭相应资源*/


  6. struct passwd *pwd;
  7. while((pwd = getpwent()) != NULL)
  8.     printf("%-8s %5ld\n",pwd->pw_name, (long)pwd->pw_uid);
  9. endpwent();
  10. #include <grp.h>
  11. struct group *getgrent(void);
  12. /*returns pointer on success, or NULL on end of stream or error*/
  13. void setgrent(void);/*重返文件起始处*/
  14. void endgrent(void);/*关闭相应资源*/
从shadow密码文件中获取记录

点击(此处)折叠或打开

  1. #include <shadow.h>
  2. struct spwd *getspnam(const char *name);
  3. /*returns pointer on success, or NULL on not found or error*/
  4. struct spwd *getspent(void);
  5. /*returns pointer on success, or NULL on end of stream or error*/
  6. void setspent(void);
  7. void endspent(void);

  8. struct spwd{
  9.     char *sp_namep;/*login name*/
  10.     char *sp_pwdp;/*encrypted password*/
  11.     long sp_lstchg;/*time of last password change (days since 1 Jan 1970)*/
  12.     long sp_min;/*min number of days between password changes*/
  13.     long sp_max;/*max number of days before change required*/
  14.     long sp_warn;/*number of days beforehand that user is warned of upcoming password expiration*/
  15.     long sp_inact;/*number of days after expiration that account is considered inactive and locked*/
  16.     long sp_expire;/*date when account expires*/
  17.     unsigned long sp_flag;/*reserved for future use*/
  18. };
密码加密和用户认证:

点击(此处)折叠或打开

  1. #define _XOPEN_SOURCE
  2. #include <unistd.h>
  3. /*最长8字符key, salt指向一个两字符的字符串*/
  4. char *crypt(const char *key, const char *salt);
  5. /*returns pointer to statically allocated string containing encrypted password on success, or NULL on error*/

  6. #define _BSD_SOURCE
  7. #include <unistd.h>
  8. char *getpass(const char *prompt);
  9. /*returns pointer to statically allocated input password string on success, or NULL on error*/

登录账号以及系统标识:


点击(此处)折叠或打开

  1. struct utmp{
  2.     char ut_line[8];/*tty line: "ttyh0", "ttyd0", "ttyp0", ...*/
  3.     char ut_name[8];/*login name*/
  4.     long ut_time; /*seconds since Epoch*/
  5. };

  6. #include <sys/utsname.h>
  7. int uname(struct utsname *name);
  8. /*returns non-negative value if ok, -1 on error*/

  9. struct utsname{
  10.     char sysname[];/*name of the os*/
  11.     char nodename[];/*name of this node*/
  12.     char release[];/*current release of os*/
  13.     char version[];/*current version of this release*/
  14.     char machine[];/*name of hardware type*/
  15. };










上一篇:Linux当中内存分配
下一篇:Linux当中的进程凭证