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

Advanced Programming in the Unix Environment

Embed Size (px)

DESCRIPTION

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? - PowerPoint PPT Presentation

Citation preview

Advanced Programming in the Unix Advanced Programming in the Unix EnvironmentEnvironment

Ch 6. System Data Files and Ch 6. System Data Files and InformationInformation

1

ContentsContents

User IdentificationUser Identification– Password filePassword file

– Shadow password fileShadow password file

– group filegroup file

Other System Data FilesOther System Data Files Login AccountingLogin Accounting System IdentificationSystem Identification Time and DateTime and Date

2

User Identification (Password file)User Identification (Password file)

Where?Where?– /etc/passwd/etc/passwd

Fields (separated by : )Fields (separated by : )– login-namelogin-name

– encrypted passwdencrypted passwd

– numeric user-IDnumeric user-ID

– numeric group IDnumeric group ID

– commentcomment

– home dirhome dir

– shell programshell program

SuperuserSuperuser– rootroot

– UID = 0UID = 0

linux1:~> cat /etc/passwdlinux1:~> cat /etc/passwd

root:x:0:0:root:/root:/bin/bashroot:x:0:0:root:/root:/bin/bash

daemon:x:1:1:daemon:/usr/sbin:/bin/shdaemon:x:1:1:daemon:/usr/sbin:/bin/sh

bin:x:2:2:bin:/bin:/bin/shbin:x:2:2:bin:/bin:/bin/sh

sys:x:3:3:sys:/dev:/bin/shsys:x:3:3:sys:/dev:/bin/sh

sync:x:4:65534:sync:/bin:/bin/syncsync:x:4:65534:sync:/bin:/bin/sync

games:x:5:60::/usr/games:/bin/shgames:x:5:60::/usr/games:/bin/sh

schan:x:6:12:Sangchul schan:x:6:12:Sangchul Han:/var/cache/man:/bin/shHan:/var/cache/man:/bin/sh

3

User Identification (Password file)User Identification (Password file) fingerfinger

linux1:~> finger hchulinux1:~> finger hchuLogin: hchu Name:Login: hchu Name:Directory: /home/professor/hchu Shell: /bin/tcshDirectory: /home/professor/hchu Shell: /bin/tcshOn since Sun Apr 16 15:21 (CST) on pts/25 from 61-229-102-On since Sun Apr 16 15:21 (CST) on pts/25 from 61-229-102-

75.dynamic.hinet.net75.dynamic.hinet.netMail last read Sun Dec 11 17:38 2005 (CST)Mail last read Sun Dec 11 17:38 2005 (CST)No Plan.No Plan.

4

User Identification (access password file)User Identification (access password file)

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

#include <pwd.h>#include <pwd.h>

structstruct passwd *getpwuid(uid_t uid);passwd *getpwuid(uid_t uid);

structstruct passwd *getpwnam(const char passwd *getpwnam(const char *name);*name);– get passwd entry by uid or nameget passwd entry by uid or name

– getpwuid() used by command getpwuid() used by command llss

– getpwnam() used by the login getpwnam() used by the login programprogram

– Both return a pointer to a static Both return a pointer to a static variablevariable

structstruct passwd *getpwent(void);passwd *getpwent(void);

void setpwent(void);void setpwent(void);

void endpwent(void);void endpwent(void);– No order in the returned passwd No order in the returned passwd

entries.entries.

– rewind/close these files.rewind/close these files.

5

User Identification (access password file)User Identification (access password file)struct passwd {struct passwd { char *pw_name; /* user name */char *pw_name; /* user name */ char *pw_passwd; /* encrypted password */char *pw_passwd; /* encrypted password */ uid_t pw_uid; /* user uid */uid_t pw_uid; /* user uid */ gid_t pw_gid; /* user gid */gid_t pw_gid; /* user gid */ time_t pw_change; /* password change time */time_t pw_change; /* password change time */ char *pw_class; /* user access class */char *pw_class; /* user access class */ char *pw_gecos; /* Honeywell login info */char *pw_gecos; /* Honeywell login info */ char *pw_dir; /* home directory */char *pw_dir; /* home directory */ char *pw_shell; /* default shell */char *pw_shell; /* default shell */ time_t pw_expire; /* account expiration */time_t pw_expire; /* account expiration */ int pw_fields; /* internal: fields filled in */int pw_fields; /* internal: fields filled in */};};

6

Figure 6.2Figure 6.2#include <pwd.h>#include <pwd.h>#include <stddef.h>#include <stddef.h>#include <string.h>#include <string.h>

struct passwd *struct passwd *getpwnam(const char *name)getpwnam(const char *name){{ struct passwd *ptr;struct passwd *ptr;

setpwent();setpwent(); while ((ptr = getpwent()) != NULL)while ((ptr = getpwent()) != NULL) if (strcmp(name, ptr->pw_name) == 0)if (strcmp(name, ptr->pw_name) == 0) break; /* found a match */break; /* found a match */ endpwent();endpwent(); return(ptr); /* ptr is NULL if no match found */return(ptr); /* ptr is NULL if no match found */}}

7

User Identification (Shadow Passwords)User Identification (Shadow Passwords)

/etc/shadow – shadow passwd file/etc/shadow – shadow passwd file– /etc/passwd/etc/passwd

• root:x:0:1:Super-User:/root:/bin/tcshroot:x:0:1:Super-User:/root:/bin/tcsh• with “x” indicated for passwdwith “x” indicated for passwd

– Store encrypted password in the shadow fileStore encrypted password in the shadow file• Username, passwd, passwd agingUsername, passwd, passwd aging• Not readable by the worldNot readable by the world• Readable by set-user-ID login/passwd programsReadable by set-user-ID login/passwd programs

– Why? avoid a Why? avoid a brute force approachbrute force approach in trying to guess passwds in trying to guess passwds

8

User Identification (access shadow file)User Identification (access shadow file)

#include <shadow.h>#include <shadow.h>

structstruct spwd *getspnam(const char *name);spwd *getspnam(const char *name);

structstruct spwd *getspent(void);spwd *getspent(void);

void setspent(void);void setspent(void);

void endspent(void);void endspent(void);– No order in the returned passwd entries.No order in the returned passwd entries.

– setspent()/endspent rewind/close these files.setspent()/endspent rewind/close these files.

9

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

10

User Identification (group file)User Identification (group file) /etc/group – the group database/etc/group – the group database

– nuucp::9:root,nuucpnuucp::9:root,nuucp

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

#include <grp.h>#include <grp.h>

structstruct group *getgrgid(gid_t gid);group *getgrgid(gid_t gid);

structstruct group *getgrnam(const char group *getgrnam(const char *name);*name);– Both return a pointer to a static Both return a pointer to a static

variablevariable

structstruct group *getgrent(void);group *getgrent(void);

void setgrent(void);void setgrent(void);

void endgrent(void);void endgrent(void);– setgrent() open and rewind the setgrent() open and rewind the

group file.group file.

– endgrent() close the group file.endgrent() close the group file.

11

Supplementary Group IDsSupplementary Group IDs Introduction of supplementary group ID’s – 4.2BSDIntroduction of supplementary group ID’s – 4.2BSD

– newgrp is the way to change gid since Version 7newgrp is the way to change gid since Version 7

– They all can be used to check for file access permissionsThey all can be used to check for file access permissions

– Optional in POSIX.1, NGROUP_MAX (16 in common)Optional in POSIX.1, NGROUP_MAX (16 in common)

12

Supplementary Group IDsSupplementary Group IDs

#include <sys/types.h>#include <sys/types.h>#include <unistd.h>#include <unistd.h>

intint getgroups(int gidsetsize, gid_t grouplist[]);getgroups(int gidsetsize, gid_t grouplist[]);– Up to gidsetsize elements stored in grouplist[]Up to gidsetsize elements stored in grouplist[]– Special case: gidsetsize = 0 Special case: gidsetsize = 0 only number is returned. only number is returned.

int setgroups(int ngroups, const gid_t grouplist[]);int setgroups(int ngroups, const gid_t grouplist[]);int initgroups(const char *usrname, gid_t basegid);int initgroups(const char *usrname, gid_t basegid);

– Only superusers can call setgroups() and initgroups()Only superusers can call setgroups() and initgroups()• Called by the login programCalled by the login program

13

Other System Data Files and InfoOther System Data Files and Info BSD Networking SoftwareBSD Networking Software

– /etc/services – getservbyname, getservbyport/etc/services – getservbyname, getservbyport– /etc/protocols – getprotobyname, getprotobynumber/etc/protocols – getprotobyname, getprotobynumber– /etc/networks – getnetbyname, getnetbyaddr/etc/networks – getnetbyname, getnetbyaddr– /etc/hosts – gethostbyname, gethostbyaddr/etc/hosts – gethostbyname, gethostbyaddr

General Principle to the InterfacesGeneral Principle to the Interfaces– A A getget function to read the next record function to read the next record– A A setset function to rewind the file function to rewind the file– An An endend function to close the file function to close the file– Keyed lookup functions if needed.Keyed lookup functions if needed.– Figure 6.6 – Page 153Figure 6.6 – Page 153

• Routines for System File AccessRoutines for System File Access

14

linux1:~> cat /etc/hostslinux1:~> cat /etc/hosts127.0.0.1 localhost.localdomain localhost127.0.0.1 localhost.localdomain localhost140.112.30.32 linux1.csie.ntu.edu.tw linux1140.112.30.32 linux1.csie.ntu.edu.tw linux1

linux1:~> more /etc/networkslinux1:~> more /etc/networkslocalnet 140.112.28.0localnet 140.112.28.0

linux1:~> more /etc/protocolslinux1:~> more /etc/protocolsip 0 IP # internet protocol, pseudo protocol numberip 0 IP # internet protocol, pseudo protocol numbericmp 1 ICMP # internet control message protocolicmp 1 ICMP # internet control message protocoltcp 6 TCP # transmission control protocoltcp 6 TCP # transmission control protocoludp 17 UDP # user datagram protocoludp 17 UDP # user datagram protocol

linux1:~> more /etc/serviceslinux1:~> more /etc/servicestcpmux 1/tcp # TCP port service multiplexertcpmux 1/tcp # TCP port service multiplexerecho 7/tcpecho 7/tcpecho 7/udpecho 7/udpsystat 11/tcp userssystat 11/tcp usersdaytime 13/tcpdaytime 13/tcpdaytime 13/udpdaytime 13/udp

15

Login Accounting (utmp)Login Accounting (utmp) Track all current loginsTrack all current logins /etc/utmp:/etc/utmp:

– /var/adm/utmp in SVR4/var/adm/utmp in SVR4

– /var/run/utmp in 4.3+BSD and Linux/var/run/utmp in 4.3+BSD and Linux Updated by the login programUpdated by the login program Erased by Erased by initinit process on logout process on logout

struct utmp {struct utmp {

char ut_line[8]; // tty linechar ut_line[8]; // tty line

char ut_name[8]; // login name char ut_name[8]; // login name

long ut_time; // seconds since epochlong ut_time; // seconds since epoch

}}

16

who cmd reads from utmpwho cmd reads from utmp

linux1:~> wholinux1:~> who

b93043 pts/1 2006-04-07 13:39 (council:S.0)b93043 pts/1 2006-04-07 13:39 (council:S.0)

r89033 pts/4 2006-04-15 02:31 (bsd5.csie.ntu.edu.tw)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/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/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)b89013 pts/5 2006-04-07 16:13 (218-174-143-212:S.2)

17

Login Accounting (wtmp)Login Accounting (wtmp) Track all logins and logoutsTrack all logins and logouts /etc/wtmp:/etc/wtmp:

– /var/adm/wtmp in SVR4 /var/adm/wtmp in SVR4 – /var/log/wtmp in 4.3+BSD and Linux/var/log/wtmp in 4.3+BSD and Linux

Updated by the login and Updated by the login and initinit programs, reboot programs, reboot

linux1:~> last | grep hchulinux1:~> last | grep hchuhchu pts/43 61-229-102-75.dy Sun Apr 16 17:43 still logged inhchu pts/43 61-229-102-75.dy Sun Apr 16 17:43 still logged inhchu pts/25 61-229-102-75.dy Sun Apr 16 15:21 - 17:43 (02:21)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/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:17 - 04:02 (07:45)hchu pts/28 140.112.29.47 Tue Apr 11 20:11 - 20:16 (00:04)hchu pts/28 140.112.29.47 Tue Apr 11 20:11 - 20:16 (00:04)

18

System IdentificationSystem Identification

#include <sys/utsname.h>#include <sys/utsname.h>

intint uname(struct utsname *name);uname(struct utsname *name);struct utsname {struct utsname {

char sysname[ ]; /* name of OS */char sysname[ ]; /* name of OS */

char nodename[ ]; /* name of the node */char nodename[ ]; /* name of the node */

char release[ ]; /* current release of the OS */char release[ ]; /* current release of the OS */

char version[ ]; /* current ver of the release */char version[ ]; /* current ver of the release */

char machine[ ]; /* name of the HW type */ char machine[ ]; /* name of the HW type */

};};

The length of each field == 65 in LinuxThe length of each field == 65 in Linux

linux1:~> uname -alinux1:~> uname -a

LinuxLinux linux1 linux1 2.6.16-1-686-smp2.6.16-1-686-smp #1 SMP Mon Apr 3 13:02:49 UTC 2006 #1 SMP Mon Apr 3 13:02:49 UTC 2006 i686i686

19

#include <sys/utsname.h>#include <sys/utsname.h>

intint gethostname(char *name, int namelen);gethostname(char *name, int namelen);– Name of the host on a TCP/IP network – BSD systemsName of the host on a TCP/IP network – BSD systems

intint getdomainname(char *name, int namelen);getdomainname(char *name, int namelen);– Domain of the hostDomain of the host

linux1:~> hostnamelinux1:~> hostname

linux1linux1

linux1:~> hostname -dlinux1:~> hostname -d

csie.ntu.edu.twcsie.ntu.edu.tw

linux1:~> hostname -ilinux1:~> hostname -i

140.112.30.32140.112.30.32

20

Time and Date RoutinesTime and Date Routines

Time ValuesTime Values– Calendar timeCalendar time

• In seconds since the Epoch (00:00:00 January 1, 1970, Coordinated In seconds since the Epoch (00:00:00 January 1, 1970, Coordinated Universal Time, i.e., UTC)Universal Time, i.e., UTC)

• type type time_ttime_t

– Remark: Times in UnixRemark: Times in Unix• Keeping time in UTCKeeping time in UTC• Automatic handling of conversions, such as Automatic handling of conversions, such as daylight savingdaylight saving time time• Keeping of time and date as a single quantity.Keeping of time and date as a single quantity.

21

Calendar TimeCalendar Time

#include <time.h>#include <time.h>

time_ttime_t time(time_t *calptr);time(time_t *calptr);– gettimeofday() provides greater resolution (1us)gettimeofday() provides greater resolution (1us)

22

time_t

kerneltime

(calendar time)

struct tmm

ktim

elo

caltim

egm

time

string

(broken-down time)

formatted stringstrftimeasctime

ctime

Affected by env var TZ

Calendar Time to GMT/LocalCalendar Time to GMT/Local

#include <time.h>#include <time.h>

struct tm *gmtime(const time_t *calptr);struct tm *gmtime(const time_t *calptr);struct tm *localtime(const time_t *calptr);struct tm *localtime(const time_t *calptr);

struct tm { /* broken-down time */struct tm { /* broken-down time */int tm_sec; /* [0, 61], >= 59 for leap seconds*/int tm_sec; /* [0, 61], >= 59 for leap seconds*/int tm_min; /* [0, 59] */int tm_min; /* [0, 59] */int tm_hour; /* [0, 23] */int tm_hour; /* [0, 23] */int tm_mday; /* [1, 31] */int tm_mday; /* [1, 31] */int tm_mon; /* [0, 11] */int tm_mon; /* [0, 11] */int tm_year; /* years since 1900 */int tm_year; /* years since 1900 */int tm_wday; /* days since Sunday: [0, 6] */int tm_wday; /* days since Sunday: [0, 6] */int tm_yday; /* days since January 1: [0, 365] */int tm_yday; /* days since January 1: [0, 365] */int tm_isdst; /* daylight saving time flag: > 0, 0, < 0 (not available) */int tm_isdst; /* daylight saving time flag: > 0, 0, < 0 (not available) */

};};

– localtime() localtime() local time, gmtime() local time, gmtime() UTC time UTC time

23

More calendar time conversion functionsMore calendar time conversion functions

time_t mktime(struct tm*tmptr) // convert tm to time_ttime_t mktime(struct tm*tmptr) // convert tm to time_t

char *asctime(const struct tm *tmptr); char *asctime(const struct tm *tmptr);

char *ctime(const time_t *calptr);char *ctime(const time_t *calptr);

size_t strftime(char *buf, size_t maxsize, const char *format, const size_t strftime(char *buf, size_t maxsize, const char *format, const struct tm *tmptr);struct tm *tmptr);

$ date$ date

Sun Apr 16 18:39:21 2006 // char format in asctime & ctimeSun Apr 16 18:39:21 2006 // char format in asctime & ctime strftime produces formatted string, like printf (see conversion strftime produces formatted string, like printf (see conversion

specifiers in Figure 6.9 )specifiers in Figure 6.9 )

24

Process time (higher resolution)Process time (higher resolution)

#include <sys/time.h>#include <sys/time.h>

int gettimeofday(struct timeval * restrict tp, void *restrict tzp);int gettimeofday(struct timeval * restrict tp, void *restrict tzp);

struct timeval {struct timeval {

time_t tv_sec;time_t tv_sec;

long tv_usec;long tv_usec;

};};

gettimeofdaygettimeofday() gives # of sec/usec since Epoch() gives # of sec/usec since Epoch timetime command calls command calls gettimeofdaygettimeofday() to compute elapsed time() to compute elapsed time

$ time grep POSIX /usr/include/*.h > /dev/null $ time grep POSIX /usr/include/*.h > /dev/null

real 0m0.049sreal 0m0.049s

user 0m0.042suser 0m0.042s

sys 0m0.007ssys 0m0.007s

25