1 / 25

Advanced Programming in the Unix Environment

Advanced Programming in the Unix Environment. Ch 6. System Data Files and Information. Contents. User Identification Password file Shadow password file group file Other System Data Files Login Accounting System Identification Time and Date. User Identification (Password file). Where?

Download Presentation

Advanced Programming in the Unix Environment

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced Programming in the Unix Environment Ch 6. System Data Files and Information

  2. Contents • User Identification • Password file • Shadow password file • group file • Other System Data Files • Login Accounting • System Identification • Time and Date

  3. User Identification (Password file) • Where? • /etc/passwd • Fields (separated by : ) • login-name • encrypted passwd • numeric user-ID • numeric group ID • comment • home dir • shell program • Superuser • root • UID = 0 linux1:~> cat /etc/passwd root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin:x:2:2:bin:/bin:/bin/sh sys:x:3:3:sys:/dev:/bin/sh sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60::/usr/games:/bin/sh schan:x:6:12:Sangchul Han:/var/cache/man:/bin/sh

  4. User Identification (Password file) • finger linux1:~> finger hchu Login: hchu Name: Directory: /home/professor/hchu Shell: /bin/tcsh On since Sun Apr 16 15:21 (CST) on pts/25 from 61-229-102-75.dynamic.hinet.net Mail last read Sun Dec 11 17:38 2005 (CST) No Plan.

  5. User Identification (access password file) #include <sys/types.h> #include <pwd.h> structpasswd *getpwuid(uid_t uid); structpasswd *getpwnam(const char *name); • get passwd entry by uid or name • getpwuid() used by command ls • getpwnam() used by the login program • Both return a pointer to a static variable structpasswd *getpwent(void); void setpwent(void); void endpwent(void); • No order in the returned passwd entries. • rewind/close these files.

  6. User Identification (access password file) structpasswd { char *pw_name; /* user name */ char *pw_passwd; /* encrypted password */ uid_tpw_uid; /* user uid */ gid_tpw_gid; /* user gid */ time_tpw_change; /* password change time */ char *pw_class; /* user access class */ char *pw_gecos; /* Honeywell login info */ char *pw_dir; /* home directory */ char *pw_shell; /* default shell */ time_tpw_expire; /* account expiration */ intpw_fields; /* internal: fields filled in */ };

  7. Figure 6.2 #include <pwd.h> #include <stddef.h> #include <string.h> struct passwd * getpwnam(const char *name) { struct passwd *ptr; setpwent(); while ((ptr = getpwent()) != NULL) if (strcmp(name, ptr->pw_name) == 0) break; /* found a match */ endpwent(); return(ptr); /* ptr is NULL if no match found */ }

  8. User Identification (Shadow Passwords) • /etc/shadow – shadow passwd file • /etc/passwd • root:x:0:1:Super-User:/root:/bin/tcsh • with “x” indicated for passwd • Store encrypted password in the shadow file • Username, passwd, passwd aging • Not readable by the world • Readable by set-user-ID login/passwd programs • Why? avoid a brute force approach in trying to guess passwds

  9. User Identification (access shadow file) #include <shadow.h> structspwd *getspnam(const char *name); structspwd *getspent(void); void setspent(void); void endspent(void); • No order in the returned passwd entries. • setspent()/endspent rewind/close these files.

  10. User Identification (access shadow passwords) structspwd { char *sp_namp; /* Login name */ char *sp_pwdp; /* Encrypted password */ long sp_lstchg; /* Date of last change */ long sp_min; /* Min #days between changes */ long sp_max; /* Max #days between changes */ long sp_warn; /* #days before pwd expires to warn user to change it */ long sp_inact; /* #days after pwd expires until account is disabled */ long sp_expire; /* #days since 1970-01-01 until account is disabled */ unsigned long sp_flag; /* Reserved */ };

  11. User Identification (group file) • /etc/group – the group database • nuucp::9:root,nuucp #include <sys/types.h> #include <grp.h> structgroup *getgrgid(gid_t gid); structgroup *getgrnam(const char *name); • Both return a pointer to a static variable structgroup *getgrent(void); void setgrent(void); void endgrent(void); • setgrent() open and rewind the group file. • endgrent() close the group file.

  12. Supplementary Group IDs • Introduction of supplementary group ID’s – 4.2BSD • newgrp is the way to change gid since Version 7 • They all can be used to check for file access permissions • Optional in POSIX.1, NGROUP_MAX (16 in common)

  13. Supplementary Group IDs #include <sys/types.h> #include <unistd.h> intgetgroups(int gidsetsize, gid_t grouplist[]); • Up to gidsetsize elements stored in grouplist[] • Special case: gidsetsize = 0  only number is returned. int setgroups(int ngroups, const gid_t grouplist[]); int initgroups(const char *usrname, gid_t basegid); • Only superusers can call setgroups() and initgroups() • Called by the login program

  14. Other System Data Files and Info • BSD Networking Software • /etc/services – getservbyname, getservbyport • /etc/protocols – getprotobyname, getprotobynumber • /etc/networks – getnetbyname, getnetbyaddr • /etc/hosts – gethostbyname, gethostbyaddr • General Principle to the Interfaces • A get function to read the next record • A set function to rewind the file • An end function to close the file • Keyed lookup functions if needed. • Figure 6.6 – Page 153 • Routines for System File Access

  15. linux1:~> cat /etc/hosts 127.0.0.1 localhost.localdomain localhost 140.112.30.32 linux1.csie.ntu.edu.tw linux1 linux1:~> more /etc/networks localnet 140.112.28.0 linux1:~> more /etc/protocols ip 0 IP # internet protocol, pseudo protocol number icmp 1 ICMP # internet control message protocol tcp 6 TCP # transmission control protocol udp 17 UDP # user datagram protocol linux1:~> more /etc/services tcpmux 1/tcp # TCP port service multiplexer echo 7/tcp echo 7/udp systat 11/tcp users daytime 13/tcp daytime 13/udp

  16. Login Accounting (utmp) • Track all current logins • /etc/utmp: • /var/adm/utmp in SVR4 • /var/run/utmp in 4.3+BSD and Linux • Updated by the login program • Erased by init process on logout struct utmp { char ut_line[8]; // tty line char ut_name[8]; // login name long ut_time; // seconds since epoch }

  17. who cmd reads from utmp linux1:~> who b93043 pts/1 2006-04-07 13:39 (council:S.0) r89033 pts/4 2006-04-15 02:31 (bsd5.csie.ntu.edu.tw) b89013 pts/10 2006-04-07 14:51 (218-174-143-212:S.0) b89013 pts/11 2006-04-07 14:51 (218-174-143-212:S.1) b89013 pts/5 2006-04-07 16:13 (218-174-143-212:S.2)

  18. Login Accounting (wtmp) • Track all logins and logouts • /etc/wtmp: • /var/adm/wtmp in SVR4 • /var/log/wtmp in 4.3+BSD and Linux • Updated by the login and init programs, reboot linux1:~> last | grep hchu hchu pts/43 61-229-102-75.dy Sun Apr 16 17:43 still logged in hchu pts/25 61-229-102-75.dy Sun Apr 16 15:21 - 17:43 (02:21) hchu pts/32 140.112.29.47 Sat Apr 15 20:57 - 21:15 (00:17) hchu pts/28 140.112.29.47 Tue Apr 11 20:17 - 04:02 (07:45) hchu pts/28 140.112.29.47 Tue Apr 11 20:11 - 20:16 (00:04)

  19. System Identification #include <sys/utsname.h> intuname(struct utsname *name); struct utsname { char sysname[ ]; /* name of OS */ char nodename[ ]; /* name of the node */ char release[ ]; /* current release of the OS */ char version[ ]; /* current ver of the release */ char machine[ ]; /* name of the HW type */ }; The length of each field == 65 in Linux linux1:~> uname -a Linux linux12.6.16-1-686-smp #1 SMP Mon Apr 3 13:02:49 UTC 2006 i686

  20. #include <sys/utsname.h> intgethostname(char *name, int namelen); • Name of the host on a TCP/IP network – BSD systems intgetdomainname(char *name, int namelen); • Domain of the host linux1:~> hostname linux1 linux1:~> hostname -d csie.ntu.edu.tw linux1:~> hostname -i 140.112.30.32

  21. Time and Date Routines • Time Values • Calendar time • In seconds since the Epoch (00:00:00 January 1, 1970, Coordinated Universal Time, i.e., UTC) • type time_t • Remark: Times in Unix • Keeping time in UTC • Automatic handling of conversions, such as daylight saving time • Keeping of time and date as a single quantity.

  22. Calendar Time #include <time.h> time_ttime(time_t *calptr); • gettimeofday() provides greater resolution (1us) (broken-down time) string struct tm formatted string asctime strftime localtime gmtime mktime ctime (calendar time) time_t time kernel Affected by env var TZ

  23. Calendar Time to GMT/Local #include <time.h> struct tm *gmtime(const time_t *calptr); struct tm *localtime(const time_t *calptr); struct tm { /* broken-down time */ int tm_sec; /* [0, 61], >= 59 for leap seconds*/ int tm_min; /* [0, 59] */ int tm_hour; /* [0, 23] */ int tm_mday; /* [1, 31] */ int tm_mon; /* [0, 11] */ int tm_year; /* years since 1900 */ int tm_wday; /* days since Sunday: [0, 6] */ int tm_yday; /* days since January 1: [0, 365] */ int tm_isdst; /* daylight saving time flag: > 0, 0, < 0 (not available) */ }; • localtime()  local time, gmtime()  UTC time

  24. More calendar time conversion functions time_t mktime(struct tm*tmptr) // convert tm to time_t char *asctime(const struct tm *tmptr); char *ctime(const time_t *calptr); size_t strftime(char *buf, size_t maxsize, const char *format, const struct tm *tmptr); $ date Sun Apr 16 18:39:21 2006 // char format in asctime & ctime • strftime produces formatted string, like printf (see conversion specifiers in Figure 6.9 )

  25. Process time (higher resolution) #include <sys/time.h> int gettimeofday(struct timeval * restrict tp, void *restrict tzp); struct timeval { time_t tv_sec; long tv_usec; }; • gettimeofday() gives # of sec/usec since Epoch • time command calls gettimeofday() to compute elapsed time $ time grep POSIX /usr/include/*.h > /dev/null real 0m0.049s user 0m0.042s sys 0m0.007s

More Related