System Programming Chapter 6 1 Administration Misc. • The mid-term will be held on April 26, 2006 in class. It will cover Chapters 1 to 6. • MP2 is due on April 28, 2006 (Friday). 2 Fun Stuff • • • • SuperCilia Skin (e.g., glass blowing in the wind, tactile interface) Fuzzmail (http://www.fuzzmail.org/) Pendland’s social network You are in control? 3 Simple Chapter! • What is system data files? – – – – Commonly used “system files” by programs What are they? Where are they? How to access them (data structure)? • Password file / Shadow passwords file • Group file (Primacy group vs. supplementary groups) 4 What/where are they? • User identification – Password & shadow passwords – Group file (Primacy group vs. supplementary groups) • System identification – Networks, services, protocols, OS, etc. • Time information 5 User Identification Password file • Where? – /etc/passwd – /etc/group • /etc/passwd – local machine or NIS DB – User database in POSIX.1 <pwd.h> – Login-name, encrypted passwd, numeric user-ID, numeric group ID, comment, home dir, shell program 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:games:/usr/games:/bin/sh man:x:6:12:man:/var/cache/man:/bin/sh 6 User Identification Password file • /etc/passwd (continue) – Superuser root – UID = 0 – Command 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-10275.dynamic.hinet.net Mail last read Sun Dec 11 17:38 2005 (CST) No Plan. 7 User Identification access password file #include <sys/types.h> #include <pwd.h> struct passwd *getpwuid(uid_t uid); get password by uid struct passwd *getpwnam(const char *name); – getpwuid() used by Command ls – getpwnam() used by the login program – Both use a static variable for returning – POSIX.1 struct passwd *getpwent(void); void setpwent(void); void endpwent(void); – No order in the returned pwd entries. – setpwent()/endpwent rewind/close these files. – Non-POSIX.1 but XSI extension 8 User Identification access password file? struct passwd { char *pw_name; /* user name */ char *pw_passwd; /* encrypted password */ uid_t pw_uid; /* user uid */ gid_t pw_gid; /* user gid */ time_t pw_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_t pw_expire; /* account expiration */ int pw_fields; /* internal: fields filled in */ }; 9 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 */ } 10 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 11 Cryptography 101 (MD5) • Basically a hash function producing 128 bits value • User enters a plaintext password K (after login: prompt) • System applies a hash function MD5 to generate crypotext C – E.g., MD5(“quickbrownfox") = 9e107d9d372bb6826bd81d3542a419d6 • System matches C to encrypted password on /etc/shadow • Very computationally expensive (infeasible) to get K from MD5 & C 12 User Identification access shadow file #include <shadow.h> struct spwd *getspnam(const char *name); struct spwd *getspent(void); void setspent(void); void endspent(void); – No order in the returned pwd entries. – setspent()/endspent rewind/close these files. – Non-POSIX.1 but XSI extension 13 User Identification access shadow passwords struct spwd { char *sp_namp; char *sp_pwdp; long sp_lstchg; long sp_min; long sp_max; long sp_warn; /* Login name */ /* Encrypted password */ /* Date of last change */ /* Min #days between changes */ /* Max #days between changes */ /* #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 */ }; 14 User Identification group file • /etc/group – the group database – nuucp::9:root,nuucp – Figure 6.2 – Page 149 • gr_passwd not in POSIX.1 (in SVR4 & 4.3+BSD) #include <sys/types.h> #include <grp.h> struct group *getgrent(void); void setgrent(void); void endgrent(void); – setgrent() open (if not) and rewind the group file. – endgrent() close the group file. struct group *getgrgid(gid_t gid); struct group *getgrnam(const char *name); – A static variable for returned values. 15 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) 16 Supplementary Group IDs #include <sys/types.h> #include <unistd.h> int getgroups(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 17 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.3 – Page 153 • Routines for System File Access 18 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 #hopopt 0 HOPOPT # IPv6 Hop-by-Hop Option [RFC1883] icmp 1 ICMP # internet control message protocol igmp 2 IGMP # Internet Group Management tcp 6 TCP # transmission control protocol pup 12 PUP # PARC universal packet protocol udp 17 UDP # user datagram protocol linux1:~> more /etc/services tcpmux 1/tcp echo 7/tcp echo 7/udp systat 11/tcp users daytime 13/tcp daytime 13/udp # TCP port service multiplexer 19 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 • tty is a Unix command that prints to standard output the name of the file connected to standard input. struct utmp { char ut_line[8]; // tty line char ut_name[8]; // login name long ut_time; // seconds since epoch } 20 who cmd reads from utmp linux1:~> who b93043 pts/1 r89033 pts/4 b89013 pts/10 b89013 pts/11 b89013 pts/5 2006-04-07 13:39 (council:S.0) 2006-04-15 02:31 (bsd5.csie.ntu.edu.tw) 2006-04-07 14:51 (218-174-143-212:S.0) 2006-04-07 14:51 (218-174-143-212:S.1) 2006-04-07 16:13 (218-174-143-212:S.2) 21 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) 22 System Identification #include <sys/utsname.h> int uname(struct utsname *name); struct utsname { char sysname[9] /* name of OS */ char nodename[9]; /* name of the node */ char release[9]; /* current release of the OS */ char version[9]; /* current ver of the release */ char machine[9]; /* name of the HW type */ } – sysconf() linux1:~> uname -a Linux linux1 2.6.16-1-686-smp #1 SMP Mon Apr 3 13:02:49 UTC 2006 i686 GNU/Linux 23 #include <sys/utsname.h> int gethostname(char *name, int namelen); – Name of the host on a TCP/IP network – BSD systems – MAXHOSTNAMELEN in <sys/param.h> linux1:~> hostname linux1 linux1:~> hostname -d csie.ntu.edu.tw linux1:~> hostname -i 140.112.30.32 24 System Identification • Commands: hostname, sethostname (/etc/rc), uname • $ uname –a SunOS ntucsa 5.6 Generic_105181-26 sun4u sparc sun4u – Nodename is not good to reference on a network. – Most versions of System V has this info compiled into the kernel when the kernel is built. 25 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. 26 Time and Date Routines • Time Values (continued) – Process time • In clock ticks (divided by CLK_TCK -> secs) • type clock_t • Clock time, user/system CPU time > time grep _POSIX_SOURCE */*.h > /dev/null 0.25u 0.25s 0:03.51 14.2% 27 Calendar Time #include <time.h> time_t time(time_t *calptr); – As a func in BSD, call gettimeofday (1us) – Time initialization: settimeofday (BSD, 1us), stime (SVR4) (broken-down time) string struct tm strftime time_t (calendar time) time kernel formatted string mktime localtime gmtime ctime asctime 28 Affected by env var TZ Calendar Time to GMT/Local #include <time.h> struct tm *gmtime(const time_t *calptr); struct tm *localtime(const time_t *calptr); struct tm { /* borken-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 29 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 ) 30 Process time • Higher resolution (microseconds) #include <sys/time.h> int gettimeofday(struct timeval * restrict tp, void *restrict tzp); struct timeval { time_t tv_sec; long tv_usec; } 31