login-id:password:uid:gid:user info:home-dir:shell
keith : x :35 :10 ::user :keith :bin/ksh
$ usermod -G group1,group2 user1
$ id
uid=509(user1) gid=509(group1) groups=509(group1),510(group2)
Type of user | Type of file permission |
---|---|
user | read |
group | write |
others | execute |
ls -l /bin/vi
-rwxr-xr-x 1 root root 377404 Apr 2 2001 /bin/vi
Octal | Symbol |
---|---|
00400 | S_IRUSR |
00200 | S_IWUSR |
00100 | S_IXUSR |
00040 | S_IRGRP |
00020 | S_IWGRP |
00010 | S_IXGRP |
00004 | S_IROTH |
00002 | S_IWOTH |
00001 | S_IXOTH |
S_IRUSER | S_IRGRP | S_IROTH = 0444 = r--r--r--
fd = open(pathname, O_RDONLY | O_CREAT | O_EXCL, 0600);
/* 파일이 존재하면 errno = EEXIST 에러 발생으로 -1을 반환한다. */
-r--r--rw- usr1 grp1 file1
-r--rw---- usr2 grp1 file2
-------r-- usr1 grp1 file3
-rw-rw-rw- usr1 grp2 file4
--w-rw---- usr2 grp2 file5
-rwxrwxrwx usr1 grp1 a.out
fd1 = open(“file1”, O_RDONLY); //usr1 read 권한 허용
fd2 = open(“file2”, O_RDONLY); //grp1 read 권한 허용
fd3 = open(“file3”, O_RDONLY); //usr와 grp의 read 권한 없음 -> 실패
fd4 = open(“file4”, O_RDONLY); //usr1 read 권한 허용
fd4 = open(“file5”, O_RDONLY); //usr와 grp이 다름 -> 실패
Octal | Symbol | Meaning |
---|---|---|
04000 | S_ISUID | set user-id on execution |
02000 | S_ISGID | set group-id on execution |
$ ls -l /bin/passwd
-rwsr-xr-x 1 root root 22984 날짜 /usr/bin/passwd
$
$ passwd
(current) UNIX password:
#include <sys/stat.h>
mode_t umask(mode_t cmask);
mode_t oldmast;
...
oldmast = umask(022);
#include <fcntl.h>
#include <sys/stat.h>
int specialcreat (const char *pathname, mode_t mode){
mode_t oldu;
int filedes:
/* file 생성 마스크를 0으로 설정 */
if ( (oldu = umask(0)) == ―1){
perror ("saving old mask");
return (-1);
}
/* file을 생성한다 */
if((filedes=open(pathname, O_WRONLY|O_CREAT|O_EXCL, mode))== -1)
perror ("opening file");
/* 비록 개방에 실패하더라도, 과거의 file 모드를 복원한다. */
if (umask (oldu) == -1)
perror ("restoring old mask");
return filedes;
}
#include <unistd.h>
int access(const char *pathname, int amode);
/* 성공시 0, 실패시 -1을 반환한다. */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
main()
{
char *filename = "afile";
if (access (filename, R_OK) == -1)
{
fprintf (stderr, "User cannot read file %s\n", filename);
exit (1);
}
printf ("%s readable, proceeding\n", filename);
/* 프로그램의 나머지 부분... */
}
#include <unistd.h>
int chmod(const char *pathname, mode_t newmode);
/* 성공시 0, 실패시 -1을 반환한다. */
if( chmod(pathname, 0644) == -1 )
perror(“call to chmod failed”);
#include <unistd.h>
int chown(const char *pathname, uid_t owner_id, gid_t group_id);
/* 성공시 0, 실패시 -1을 반환한다. */
owner-id : new owner
group-id : new group
errno EPERM : 파일 소유자 변경시 일어날 수 있는 오류
set-user-id와 set-group-id 권한은 소유주가 바뀌면 비트세팅을 0으로 되돌린다.
$ ln /dirA/name1 /dirB/name2
$ ls -i /dirA/name1 /dirB/name2
12345 /dirA/name1
12345 /dirB/name2
$ ln -s /dirA/name1 /dirB/name2
lrwxrwxrwx 1 root root 날짜 name -> dirA/name
#include <unistd.h>
int link(const char *orginal_path, const char *new_path);
/* 성공시 0, 실패시 -1을 반환한다. */
link(“/usr/keith/chap.2”, “/usr/ben/2.chap”);
#include <unistd.h>
int unlink(const char *pathname);
/* 성공시 0, 실패시 -1을 반환한다. */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
char *usage = "usage: move file1 file2\n";
main (int argc, char **argv){
if (argc != 3){
fprintf (stderr, usage); exit (1);
}
if ( link (argv[1], argv[2]) == -1){
perror ("link failed");
exit (1);
}
if ( unlink(argv[1]) == -1){
perror ("unlink failed");
unlink (argv[2]);
exit (1);
}
printf ("Succeeded\n");
}
#include <stdio.h>
int remove(const char *pathname);
/* 성공시 0, 실패시 -1을 반환한다. */
#include <stdio.h>
int rename(const char *oldname, const char *newname);
/* 성공시 0, 실패시 -1을 반환한다. */
#include <sys/stat.h>
int stat(const char *pathname, struct stat *buf);
int fstat(int filedes, struct stat *buf);
int lstat(const char *pathname, struct stat *buf);
/* 성공시 0, 실패시 -1을 반환한다. */
struct stat {
mode_t st_mode; /* file type & mode (permissions) */
ino_t st_ino; /* i-node number (serial number) */
dev_t st_dev; /* device number (file system) */
dev_t st_rdev; /* device number for special files */
nlink_t st_nlink; /* number of links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
off_t st_size; /* size in bytes, for regular files */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last file status change */
blksize_t st_blksize; /* best I/O block size */
blkcnt_t st_blocks; /* number of disk blocks allocated */
};