Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 Special Topics in Comp. Eng. 2 Semester 2, 2000-2001 17. The File Again

2 240-491 Adv. UNIX: FileAgain/172 Overview 1. Users and Ownership 2. Extra File Permissions 3. creat() Revisited 4. access() 5. chmod() continued

3 240-491 Adv. UNIX: FileAgain/173 6. chown() 7. The File Creation Mask 8. link(), unlink() 9. stat(), fstat() 10. ustat()

4 240-491 Adv. UNIX: FileAgain/174 1. 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

5 240-491 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

6 240-491 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 13...  But when I use vi, its real uid is ad (not root ), so I can only edit my files. ad's file vi ad

7 240-491 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.

8 240-491 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

9 240-491 Adv. UNIX: FileAgain/179 2. Extra File Permissions  Octal ValueMeaning 04000 Set user-id on execution. Symbolic: --s --- --- 02000 Set group-id on execution. Symbolic: --- --s --- v These specify that a program should use the effective user/group id during execution. continued

10 240-491 Adv. UNIX: FileAgain/1710 v For example: –$ ls -alt /usr/bin/passwd -rwsr-xr-x 1 root root 25692 May 24...

11 240-491 Adv. UNIX: FileAgain/1711 Sticky Bit  OctalMeaning 01000 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.

12 240-491 Adv. UNIX: FileAgain/1712 3. 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

13 240-491 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

14 240-491 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

15 240-491 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; }

16 240-491 Adv. UNIX: FileAgain/1716 4. 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

17 240-491 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?

18 240-491 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); } :

19 240-491 Adv. UNIX: FileAgain/1719 5. 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

20 240-491 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”);

21 240-491 Adv. UNIX: FileAgain/1721 6. 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

22 240-491 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!

23 240-491 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)

24 240-491 Adv. UNIX: FileAgain/1724 7. 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.

25 240-491 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);

26 240-491 Adv. UNIX: FileAgain/1726 umask()  #include #include mode_t umask(mode_t newmask);  Set file creation mask to newmask, and return old mask value.

27 240-491 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; }

28 240-491 Adv. UNIX: FileAgain/1728 8. 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”);

29 240-491 Adv. UNIX: FileAgain/1729 unlink() Revisited  #include int unlink(char *pathname); v Removes the link and reduces the original file’s 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

30 240-491 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

31 240-491 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

32 240-491 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().

33 240-491 Adv. UNIX: FileAgain/1733 9. 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.

34 240-491 Adv. UNIX: FileAgain/1734 9.1. 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.

35 240-491 Adv. UNIX: FileAgain/1735 9.1.1. st_dev  st_dev holds the device number of the file system where the file is located: –usually a hard disk

36 240-491 Adv. UNIX: FileAgain/1736 9.1.2. 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

37 240-491 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.

38 240-491 Adv. UNIX: FileAgain/1738 ls -l  Major and minor device numbers can be displayed with ls -l : $ ls -l /dev/ttyp0 crw--w---- 1 ad tty 4, 192 Aug 13 10:19 /dev/ttyp0 major device number minor device number file type

39 240-491 Adv. UNIX: FileAgain/1739 9.1.3. 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 file’s information (i-node) in a system table (the i-list). v A file’s i-node contains: –user and group ids of its owner –permission bits –etc.

40 240-491 Adv. UNIX: FileAgain/1740 9.2. 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

41 240-491 Adv. UNIX: FileAgain/1741 File Mix on a Typical System v File Type Count Percentage regular file30,36991.7% directory1,9015.7 symbolic link4161.3 char special3731.1 block special610.2 socket50.0 FIFO10.0

42 240-491 Adv. UNIX: FileAgain/1742 9.3. 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

43 240-491 Adv. UNIX: FileAgain/1743 9.4. 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.

44 240-491 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”);

45 240-491 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

46 240-491 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... }

47 240-491 Adv. UNIX: FileAgain/1747 9.5. 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”);

48 240-491 Adv. UNIX: FileAgain/1748 9.6. 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

49 240-491 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, ”); :

50 240-491 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”);

51 240-491 Adv. UNIX: FileAgain/1751 9.7. 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.

52 240-491 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.

53 240-491 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.

54 240-491 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.

55 240-491 Adv. UNIX: FileAgain/1755 10. 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.

56 240-491 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

57 240-491 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

58 240-491 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.

59 240-491 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 */ };


Download ppt "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."

Similar presentations


Ads by Google