CS631 - Advanced Programming in The UNIX Environment - Files and Directories
CS631 - Advanced Programming in The UNIX Environment - Files and Directories
CS631 - Advanced Programming in The UNIX Environment - Files and Directories
Slide 1
Slide 2
Code Reading
HW#2
Slide 3
All these functions return extended attributes about the referenced file (in
the case of symbolic links, lstat(2) returns attributes of the link, others
return stats of the referenced file).
Slide 4
All these functions return extended attributes about the referenced file (in
the case of symbolic links, lstat(2) returns attributes of the link, others
return stats of the referenced file).
struct stat {
dev_t
ino_t
mode_t
dev_t
nlink_t
uid_t
gid_t
off_t
time_t
time_t
time_t
long
long
};
st_dev;
st_ino;
st_mode;
st_rdev;
st_nlink;
st_uid;
st_gid;
st_size;
st_atime;
st_mtime;
st_ctime;
st_blocks;
st_blksize;
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
Slide 5
Slide 6
Slide 7
Slide 8
Slide 9
setuid(2)/seteuid(2)
#include <unistd.h>
int seteuid(uid t uid);
int setuid(uid t euid);
Returns: 0 if OK, -1 on error
uid t geteuid(void);
uid t getuid(void);
Returns: uid t; no error
Slide 10
access(2)
#include <unistd.h>
int access(const char *path, int mode);
Returns: 0 if OK, -1 on error
Tests file accessibility on the basis of the real uid and gid. Allows
setuid/setgid programs to see if the real user could access the file
without it having to drop permissions to do so.
The mode paramenter can be a bitwise OR of:
R OK test for read permission
W OK test for write permission
X OK test for execute permission
F OK test for existence of file
Slide 11
access(2)
$ cc -Wall access.c
$ ./a.out /etc/passwd
access ok for /etc/passwd
open ok for /etc/passwd
$ ./a.out /etc/master.passwd
access error for /etc/master.passwd
open error for /etc/master.passwd
$ sudo chown root a.out
$ sudo chmod 4755 a.out
$ ./a.out /etc/passwd
access ok for /etc/passwd
open ok for /etc/passwd
$ ./a.out /etc/master.passwd
access error for /etc/master.passwd
open ok for /etc/master.passwd
$
Slide 12
Slide 13
Slide 14
Slide 15
Slide 16
Slide 17
Slide 18
Slide 19
Slide 20
Slide 21
Slide 22
Slide 23
Slide 24
umask(2)
#include <sys/stat.h>
mode t umask(mode t numask);
Returns: previous file mode creation mask
umask(2) sets the file creation mode mask. Any bits that are on in the file
creation mask are turned off in the files mode.
Important because a user can set a default umask. If a program needs to
be able to insure certain permissions on a file, it may need to turn off (or
modify) the umask, which affects only the current process.
Slide 25
umask(2)
$ cc -Wall umask.c
$ umask 022
$ touch foo
$ ./a.out
$ ls -l foo*
-rw-r--r-- 1 jschauma
-rw-r--r-- 1 jschauma
-rw-rw-rw- 1 jschauma
-rw------- 1 jschauma
staff
staff
staff
staff
0
0
0
0
Sep
Sep
Sep
Sep
26
26
26
26
18:35
18:36
18:36
18:36
foo
foo1
foo2
foo3
Slide 26
Slide 27
staff
staff
staff
staff
Slide 28
Changes st uid and st gid for a file. For BSD, must be superuser.
Some SVR4s let users chown files they own. POSIX.1 allows either
depending on POSIX CHOWN RESTRICTED (a kernel constant).
owner or group can be -1 to indicate that it should remain the same.
Non-superusers can change the st gid field if both:
effective-user ID == st uid and
owner == files user ID and group == effective-group ID (or one of the
supplementary group IDs)
chown and friends (should) clear all setuid or setgid bits.
Lecture 03: Files and Directories
Slide 29
Slide 30
wheel
68 Sep 24 18:52 .
wheel
wheel
wheel
Slide 31
Homework
Reading:
manual pages for the functions covered
Stevens Chap. 4.1 through 4.13
Playing:
in your shell, set your umask to various values and see what happens
to new files you create (example: Stevens # 4.3)
Verify that turning off user-read permission for a file that you own
denies you access to the file, even if group- or other permissions
allow reading.
Midterm Assignment:
https://www.cs.stevens.edu/~jschauma/631/f16-midterm.html