Sunday, August 2, 2015

Bài viết liên quan:
+ Users and Groups

Một OS bao giờ cũng tạo ra các User và gom nhiều user có điểm chung lại với nhau thành Group, các user và group được định danh theo con số duy nhất gọi là UID (User IDentifier) và GID (Group IDentifier). Unix/Linux cung cấp các API để giúp chưng trình lấy được thông tin về User và Group mà nó thuộc về.

1. User and Groups 

 #include <sys/types.h>  
 #include <unistd.h>  
 uid_t getuid(void); //trả về UID
 gid_t getgid(void); //trả về GID
 int setuid(uid_t uid);
 int setgid(gid_t gid);
 char *getlogin(void); //trả về login name của user  

Các thông tin trên được lưu trong /etc/passwd có dạng:
 account:password:UID:GID:GECOS:directory:shell  

account is the user name
password is the user password
UID is the numerical user ID
GID is the numerical primary group ID for the user
GECOS is an optional field used for informational purposes; usually it contains the full user name
directory is the user's $HOME directory
shell is the user command interpreter (defaults to /bin/sh)

Ex:
 $ cat /etc/passwd | grep ninhld  
 ninhld:x:1000:1000:ninhld:/home/ninhld:/bin/bash  

2. Password Database
 #include <sys/types.h>  
 #include <pwd.h>  
 struct passwd *getpwuid(uid_t uid);  
 struct passwd *getpwnam(const char *name);  

struct passwd:

passwd Member Description
char *pw_name The user’s login name
uid_t pw_uid The UID number
gid_t pw_gid The GID number
char *pw_dir The user’s home directory
char *pw_gecos The user’s full name
char *pw_shell The user’s default shell

Thông tin về Password Database được lưu trong /etc/passwd

Để lấy tât cả thông tin trong Password Database:
 #include <pwd.h>  
 #include <sys/types.h>  
 void setpwent(void);           //trở về đầu database
 struct passwd *getpwent(void); //sacn tất cả entry, khi không còn thì trả về NULL
 void endpwent(void);           //đóng database khi scan xong 

Ex1:
user.c
 #include <sys/types.h>  
 #include <pwd.h>  
 #include <stdio.h>  
 #include <unistd.h>  
 #include <stdlib.h>  
   
 int main()  
 {  
   uid_t uid;  
   gid_t gid;  
   struct passwd *pw;  
   uid = getuid();  
   gid = getgid();  
   printf("User is %s\n", getlogin());  
   printf("User IDs: uid=%d, gid=%d\n", uid, gid);  
   pw = getpwuid(uid);  
   printf("UID passwd entry:\n name=%s, uid=%d, gid=%d, home=%s, shell=%s\n",  
       pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_shell);  
   pw = getpwnam("root");  
   printf("root passwd entry:\n");  
   printf("name=%s, uid=%d, gid=%d, home=%s, shell=%s\n",  
       pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_shell);  
   exit(0);  
 }  
   

Compile & Execute:
 $ gcc user.c   
 $ ./a.out   
 User is ninhld  
 User IDs: uid=1000, gid=1000  
 UID passwd entry:  
  name=ninhld, uid=1000, gid=1000, home=/home/ninhld, shell=/bin/bash  
 root passwd entry:  
 name=root, uid=0, gid=0, home=/root, shell=/bin/bash  


Ex2:
getpwent.c
 /*  
  * This program loops, reading a login name from standard  
  * input and checking to see if it is a valid name. If it  
  * is not valid, the entire contents of the name in the  
  * password database are printed.  
  */  
 #include <stdio.h>  
 #include <sys/types.h>  
 #include <pwd.h>  
   
 void main()  
 {  
   struct passwd *pw;  
   
   setpwent( );  
   while( ( pw=getpwent( ) ) != ( struct passwd * )0 )  
     printf( "%s\n", pw->pw_name );  
   endpwent();  
 }  
   

Compile & Execute:
 $ gcc getpwent.c   
 $ ./a.out   
 root  
 bin  
 daemon  
 adm  
 lp  
 sync  
 shutdown  
 halt  
 mail  
 operator  
 games  
 ftp  
 nobody  
 avahi-autoipd  
 dbus  
 polkitd  
 usbmuxd  
 rtkit  
 chrony  
 tss  
 unbound  
 openvpn  
 avahi  
 pulse  
 lightdm  
 nm-openconnect  
 colord  
 sshd  
 tcpdump  
 ninhld  
 abrt  
 rpc  
 rpcuser  
 nfsnobody  
 stap-server  
   





Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Lập trình hệ thống nhúng Linux . Powered by Luong Duy Ninh -