240-491 adv. unix: fileagain/171 advanced unix v objectives to look at the low-level manipulation of...

Download 240-491 Adv. UNIX: FileAgain/171 Advanced UNIX v Objectives to look at the low-level manipulation of files and their properties (e.g. permissions) 240-491

If you can't read please download the document

Upload: ophelia-cunningham

Post on 19-Jan-2018

220 views

Category:

Documents


0 download

DESCRIPTION

Adv. UNIX: FileAgain/ chown() 7. The File Creation Mask 8. link(), unlink() 9. stat(), fstat() 10. ustat()

TRANSCRIPT

Adv. UNIX: FileAgain/171 Advanced UNIX v Objectives to look at the low-level manipulation of files and their properties (e.g. permissions) Special Topics in Comp. Eng. 2 Semester 2, The File Again Adv. UNIX: FileAgain/172 Overview 1. Users and Ownership 2. Extra File Permissions 3. creat() Revisited 4. access() 5. chmod() continued Adv. UNIX: FileAgain/ chown() 7. The File Creation Mask 8. link(), unlink() 9. stat(), fstat() 10. ustat() Adv. UNIX: FileAgain/ Users and Ownership v File ownership is based on your user-id integer (uid) The uid is stored in the 3rd field of /etc/passwd : ad:x:42497:100:... Group-id is 4th field; meaning stored in /etc/group Adv. UNIX: FileAgain/175 Real uids v The uid of the user who started the program is used as its real uid. v The real uid affects what the program can do (e.g. create, delete files). continued Adv. UNIX: FileAgain/176 For example, the uid of /usr/bin/vi is root : $ ls -alt /usr/bin/vi lrwxrwxrwx 1 root root 20 Apr But when I use vi, its real uid is ad (not root ), so I can only edit my files. ad's file vi ad Adv. UNIX: FileAgain/177 Effective uids v Programs can change to use the effective uid the uid of the program owner e.g. the passwd program changes to use its effective uid ( root ) so that it can edit the /etc/passwd file v This feature is used by many system tools, such as logging programs. Adv. UNIX: FileAgain/178 Real and Effective Group-ids v There are also real and effective group-ids. v Usually a program uses the real group-id (i.e. the group-id of the user). v Sometimes useful to use effective group-id (i.e. group-id of program owner): e.g. software shared across teams Adv. UNIX: FileAgain/ Extra File Permissions Octal ValueMeaning Set user-id on execution. Symbolic: --s Set group-id on execution. Symbolic: s --- v These specify that a program should use the effective user/group id during execution. continued Adv. UNIX: FileAgain/1710 v For example: $ ls -alt /usr/bin/passwd -rwsr-xr-x 1 root root May 24... Adv. UNIX: FileAgain/1711 Sticky Bit OctalMeaning Save text image on execution. Symbolic: t v This specifies that the program code should stay resident in memory after termination. this makes the start-up of the next execution faster v Obsolete due to virtual memory. Adv. UNIX: FileAgain/ creat() Revisited e.g: fd = creat(file, 0644); v The permissions are only used if "file" is being created permissions are applied after the file is closed v The permissions of an existing file are unchanged, but the file is opened, and its contents are truncated (deleted). continued Adv. UNIX: FileAgain/1713 v If the file exists then the (real) uid and group-id of the process are checked against the file's permissions if they do not match, then creat() will fail v If the file does not exist then the (real) uid and group-id of the process are checked against the directory's permissions if they do not match, then creat() will fail Adv. UNIX: FileAgain/1714 Silly Creation v v #include #include #include #include int main() { int fd; /* file either exists already or does not */ if ((fd = creat(file, 0444)) < 0){ fprintf(stderr, 1st creat() fail due to insufficient user/group ID\n); exit(1); } continued make read-only Adv. UNIX: FileAgain/1715 /* could write into file here */ close(fd); /* if new file then read-only now */ /* if existing file then same permissions */ if ((fd = creat(file, 0444)) < 0){ fprintf(stderr, 2nd creat() fail due to insufficient user/group ID\n); exit(1); } printf(Either you are root\n); printf(Or file already exists and is writeable\n); return 0; } Adv. UNIX: FileAgain/ access() #include int access(char *pathname, int access_mode); According to the real uid, can the program using access() access the file? Return 0 if ok, -1 on error. e.g. jim is using ad's edit program but it should only edit jim's files jim's file ad's edit jim Adv. UNIX: FileAgain/1717 Mode Values Mode Values Meaning R_OK 04 Has calling process read access? W_OK 02 Has calling process write access? X_OK 01 Can calling process execute the file? F_OK 0 Does file exist? Adv. UNIX: FileAgain/1718 Example: ad's edit Program #include #include int main() { if (access(file, R_OK) < 0) { fprintf(stderr, User cannot read the file\n); exit(1); } : Adv. UNIX: FileAgain/ chmod() v #include #include int chmod(char *pathname, mode_t new_mode); int fchmod(int fd, mode_t new_mode); v Alter permissions of an existing file; return 0 if ok, -1 on error. continued Adv. UNIX: FileAgain/1720 chmod() can only be used by the current file owner or root (the superuser). Example: if (chmod(file, 0644) < 0) perror(Call to chmod() failed); Adv. UNIX: FileAgain/ chown() #include #include int chown(char *pathname, int owner_id, int group_id); int fchown(int fd, int owner_id, int group_id); v Alter owner and group ids of a file; return 0 if ok, -1 on error. continued Adv. UNIX: FileAgain/1722 chown() can only be used by the current file owner or the superuser be careful, a change may make the file inaccessible to you! Adv. UNIX: FileAgain/1723 Hacker Attack (does not work) Mr. Hacker writes a deleteAll program inside the code he uses chown() to change the owner to be root if he could now change the real UID or effective UID to root, then the program could attack not possible (unless you are root already) Adv. UNIX: FileAgain/ The File Creation Mask v The file creation mask specifies permission bits to always turn off whenever a file is created. At UNIX level, use umask : umask 022 switch off group & others write; umask 066 switch off group & others read and write; umask read current setting. Adv. UNIX: FileAgain/1725 umask works sliently on files created with creat() and open() the call: fd = creat(file, mode); is really: fd = creat(file (~mask) & mode); e.g. with umask 022 : fd = creat(foo, 0666); is really: fd = creat(foo, 0644); Adv. UNIX: FileAgain/1726 umask() #include #include mode_t umask(mode_t newmask); Set file creation mask to newmask, and return old mask value. Adv. UNIX: FileAgain/1727 Example int spcl_create(char *fnm, mode_t mode) /* switch off mask for this use of creat() */ { mode_t old_mask; int fd; /* set file creation mask to 0 */ old_mask = umask(0); if ((fd = creat(fnm, mode)) < 0) perror(spcl_create); /* restore old mask */ umask(old_mask); return fd; } Adv. UNIX: FileAgain/ link(), unlink() #include int link(char *exist-path, char *new-path); Make a new link from exist-path to new- path ; return 0 if ok, -1 on error same as UNIX command ln Example: link(/usr/ad/chap.2, /usr/ben/chap.2); Adv. UNIX: FileAgain/1729 unlink() Revisited #include int unlink(char *pathname); v Removes the link and reduces the original files link count by 1. If the link count == 0 then the file is deleted. unlink() looks at directory permissions: the process must be able to write and execute in the directory Adv. UNIX: FileAgain/1730 Simplified mv.c #include #include int main(int argc, char *argv[]) /* cannot deal with wild cards in names or directories */ { if (argc != 3) { fprintf(stderr, Usage: mv f1 f2\n); exit(1); } : continued Usage: mv foo.txt bar.txt Adv. UNIX: FileAgain/1731 if (link(argv[1], argv[2]) < 0) { perror(link); exit(1); } if (unlink(argv[1]) < 0) { perror(unlink); unlink(argv[2]); exit(1); } printf(succeeded\n); return 0; } No file creation, so fast Adv. UNIX: FileAgain/1732 remove(), rename() #include int remove(char *pathname); int rename(char *oldname, char *newname); v Return 0 if ok, -1 on error. Same functionality as link() and unlink(). Adv. UNIX: FileAgain/ stat(), fstat() #include #include int stat(char *pathname, struct stat *buffer); int fstat(int fd, struct stat *buffer); v Read the properties associated with a file (e.g. permissions); return 0 if ok, -1 on error. Great for analysing files. Adv. UNIX: FileAgain/ struct stat v v struct stat{ dev_t st_dev; /* device num. */ dev_t st_rdev; /* device num. (spcl. files)*/ ino_t st_ino; /* i-node num. */ mode_t st_mode; /* file type, mode, permsns */ nlink_t st_nlink; /* num. of links */ uid_t st_uid; /* uid of owner */ gid_t st_gid; /* group-id of owner */ off_t st_size; /* size in bytes */ time_t st_atime; /* last access time */ time_t st_mtime; /* last mod. time */ time_t st_ctime; /* last stat chg time */ long st_blksize;/* best I/O block size */ long st_blocks;/* no. of 512 blocks used*/ } We will look at these in detail. Adv. UNIX: FileAgain/ st_dev st_dev holds the device number of the file system where the file is located: usually a hard disk Adv. UNIX: FileAgain/ st_rdev st_rdev holds the device number for a special file. v A special file is used to describe a device (peripheral) attached to the machine: CD drives, keyboard, harddisk, microphone, etc. Special files are usually stored in /dev Adv. UNIX: FileAgain/1737 st_rdev Format v Two parts: major device number, minor device number. v Major device number: specifies the device type. The system uses it to choose the right device driver. v Minor device number: represents the actual device port number, drive number, etc. Adv. UNIX: FileAgain/1738 ls -l Major and minor device numbers can be displayed with ls -l : $ ls -l /dev/ttyp0 crw--w ad tty 4, 192 Aug 13 10:19 /dev/ttyp0 major device number minor device number file type Adv. UNIX: FileAgain/ st_ino (I-node number) v Each file has a unique i-node number (index number). v The i-node number can be used to look up a files information (i-node) in a system table (the i-list). v A files i-node contains: user and group ids of its owner permission bits etc. Adv. UNIX: FileAgain/ File Types 1. Regular File (text/binary) 2. Directory File 3. Character Special File e.g. I/O peripherals, such as /dev/ttyp0 4. Block Special File e.g. cdrom, such as /dev/mcd 5. FIFO (named pipes) 6. Sockets 7. Symbolic Links Adv. UNIX: FileAgain/1741 File Mix on a Typical System v File Type Count Percentage regular file30, % directory1, symbolic link char special block special610.2 socket50.0 FIFO10.0 Adv. UNIX: FileAgain/ st_mode Field v This field contains type and mode information in bit format. It is extracted by AND- ing the value stored there with various constants see man stat also and also and some data structures are in some data structures are in Adv. UNIX: FileAgain/ Getting the Type Information AND the st_mode field with S_IFMT to get the type bits. v Test the result against: S_IFREG Regular file S_IFDIR Directory S_IFSOCK Socket etc. Adv. UNIX: FileAgain/1744 Example struct stat sbuf; : if (stat(file, &sbuf) == 0) if ((sbuf.st_mode & S_IFMT) == S_IFDIR) printf(A directory\n); Adv. UNIX: FileAgain/1745 Type Info. Macros Modern UNIX systems include test macros in and : S_ISREG() regular file S_ISDIR() directory file S_ISCHR() char. special file S_ISBLK() block special file S_ISFIFO() pipe or FIFO S_ISLNK() symbolic link S_ISSOCK() socket Adv. UNIX: FileAgain/1746 Example stat(file, &sbuf) S_ISREG(sbuf.st_mode) S_ISDIR(sbuf.st_mode) struct stat sbuf; : if (stat(file, &sbuf) == 0) { if (S_ISREG(sbuf.st_mode)) printf(A regular file\n); else if (S_ISDIR(sbuf.st_mode)) printf(A directory\n); else... } Adv. UNIX: FileAgain/ Getting Mode Information AND the st_mode field with one of the following masks and test for non-zero: S_ISUID set-user-id bit is set S_ISGID set-group-id bit is set S_ISVTX sticky bit is set v Example: if ((sbuf.st_mode & S_ISUID) != 0) printf(set-user-id bit is set\n); Adv. UNIX: FileAgain/ Getting Permission Info. AND the st_mode field with one of the following masks and test for non-zero: S_IRUSR 0400user read S_IWUSR 0200user write S_IXUSR 0100user execute S_IRGRP 0040group read S_IWGRP 0020group write S_IXGRP 0010group execute S_IROTH 0004other read S_IWOTH 0002other write S_IXOTH 0001other execute Adv. UNIX: FileAgain/1749 Example v v struct stat sbuf; : printf(Permissions: ); if ((sbuf.st_mode & S_IRUSR) != 0) printf(user read, ); if ((sbuf.st_mode & S_IWUSR) != 0) printf(user write, ); : Adv. UNIX: FileAgain/1750 v Or use octal values, which are easy to combine: if ((sbuf.st_mode & 0444) != 0) printf(readable by everyone\n); Adv. UNIX: FileAgain/ utime() #include #include int utime(char *pathname, struct utimebuf *times); v Alter the access and modification times of a file; returns 0 if ok, -1 on error. Adv. UNIX: FileAgain/1752 utimebuf struct utimebuf { time_t actime;/* access time */ time_t modtime;/* modification time */ }; If times is NULL then both times are set to the current time. Adv. UNIX: FileAgain/1753 Example: truncate & reset time v v #include #include #include #include #include int main() {... struct stat sbuf; struct utimebuf tbuf; : continued Truncate a file, but do not alter its time info. Adv. UNIX: FileAgain/1754 /* store the current times, etc. */ if (stat(file, &sbuf) < 0) { printf(stat error\n); exit(1); } /* open and truncate file */ if (open(file, O_RDWR|O_TRUNC) < 0){ printf(open error\n); exit(1); } /* reset file times */ tbuf.actime = sbuf.st_atime; tbuf.modtime = sbuf.st_mtime; if (utime(file, &tbuf) < 0) { printf(utime error\n); exit(1); } Truncation alters the file times. Adv. UNIX: FileAgain/ ustat() v #include int ustat(dev_t dev, struct ustat *buffer); v Access information about the (special) device. v Return 0 if ok, -1 on error. Adv. UNIX: FileAgain/1756 struct ustat v struct ustat{ daddr_t f_tfree;/* no. free disk blocks */ ino_t f_tinode;/* no. free inodes */ char f_fname[6];/* file system name */ char f_fpack[6];/* pack name of hard disk*/ } f_fname and f_pack are often filled with NULL characters ustat() defaults to returning info. about the machine's hard disk Adv. UNIX: FileAgain/1757 Example ( df mimic) struct stat sbuf; struct ustat ubuf; : if (stat(/dev/tty0, &sbuf) < 0) // keyboard printf(stat error\n); else { if (ustat(sbuf.st_rdev, &ubuf) < 0) printf(ustat error\n); else printf(free blocks %d; free inodes %d\n, ubuf.f_tfree, ubuf.f_tinode); : df summarises free disk space Adv. UNIX: FileAgain/1758 ustat() is becoming obsolete #include int statfs(char *pathname, struct statfs *buf); int fstatfs(int fd, struct statfs *buf); Set buf to contain information about the file system. pathname is any file in the system; returns 0 if ok, -1 on error. Adv. UNIX: FileAgain/1759 statfs v v struct statfs { long f_type; /* filesystem type */ long f_bsize; /* best transfer size */ long f_blocks; /* total data blocks */ long f_bfree; /* free blocks */ long f_bavail; /* free for non-su */ long f_files; /* total i-nodes */ long f_ffree; /* free i-nodes */ fsid_t f_fsid; /* filesystem id */ long f_namelen; /* max filename length */ long f_spare[6]; /* spare */ };