📌 매뉴얼 (Linux)
NAME
access - check user's permissions for a file
: 파일에 대한 사용자 권한 확인
SYNOPSIS
#include <unistd.h>
int access(const char *pathname, int mode);
DESCRIPTION
access() checks whether the calling process can access the file pathname. If pathname is a
symbolic link, it is dereferenced.
The mode specifies the accessibility check(s) to be performed, and is either the value F_OK,
or a mask consisting of the bitwise OR of one or more of R_OK, W_OK, and X_OK. F_OK tests for
the existence of the file. R_OK, W_OK, and X_OK test whether the file exists and grants read,
write, and execute permissions, respectively.
The check is done using the calling process's real UID and GID, rather than the effective IDs
as is done when actually attempting an operation (e.g., open(2)) on the file. Similarly, for
the root user, the check uses the set of permitted capabilities rather than the set of effec‐
tive capabilities; and for non-root users, the check uses an empty set of capabilities.
This allows set-user-ID programs and capability-endowed programs to easily determine the in‐
voking user's authority. In other words, access() does not answer the "can I read/write/exe‐
cute this file?" question. It answers a slightly different question: "(assuming I'm a setuid
binary) can the user who invoked me read/write/execute this file?", which gives set-user-ID
programs the possibility to prevent malicious users from causing them to read files which
users shouldn't be able to read.
If the calling process is privileged (i.e., its real UID is zero), then an X_OK check is suc‐
cessful for a regular file if execute permission is enabled for any of the file owner, group,
or other.
: access()는 호출 프로세스가 파일 경로 이름에 액세스할 수 있는지 여부를 확인합니다.
경로 이름이 기호 링크인 경우 참조되지 않습니다.
모드는 수행할 접근성 검사(들)를 지정하며, 값 F_OK 또는 R_OK, W_OK 및 X_OK 중 하나 이상의
비트 단위 OR로 구성된 마스크입니다. F_OK는 파일의 존재 여부를 테스트합니다.
R_OK, W_OK 및 X_OK는 파일의 존재 여부를 테스트하고 각각 읽기, 쓰기 및 실행 권한을 부여합니다.
파일에서 실제로 작업(예: open(2))을 시도할 때처럼 유효한 ID가 아닌 호출 프로세스의 실제 UID 및 GID를
사용하여 검사를 수행합니다. 마찬가지로 루트 사용자의 경우 검사는 유효한 기능 집합이 아닌 허용된
기능 집합을 사용하고 루트 사용자가 아닌 경우 검사는 빈 기능 집합을 사용합니다.
이를 통해 set-user-ID 프로그램과 capability endowed 프로그램은 호출하는 사용자의 권한을
쉽게 결정할 수 있습니다. 즉 access()는 "내가 이 파일을 읽거나 쓸 수 있습니까?"라는 질문에 답하지 않습니다.
"(내가 setuid binary라고 가정할 때) 나를 호출한 사용자가 이 파일을 읽거나 쓸 수 있습니까?"라는
약간 다른 질문에 답합니다. 이 질문은 set-user-ID 프로그램이 악의적인 사용자가 읽을 수 없는 파일을
읽게 하는 것을 방지할 수 있는 가능성을 제공합니다.
호출 프로세스에 권한이 있는 경우(즉, 실제 UID가 0인 경우) 파일 소유자, 그룹 또는 기타 파일에 대해
실행 권한이 설정된 경우 일반 파일에 대해 X_OK 검사가 성공합니다.
RETURN VALUE
On success (all requested permissions granted, or mode is F_OK and the file exists), zero is
returned. On error (at least one bit in mode asked for a permission that is denied, or mode
is F_OK and the file does not exist, or some other error occurred), -1 is returned, and errno
is set appropriately.
: 성공 시(요청된 모든 권한이 부여됨 또는 mode가 F_OK이고 파일이 존재함) 0이 반환됩니다.
오류 시(mode에서 하나 이상의 비트가 거부된 권한을 요청받았거나 mode가 F_OK이고
파일이 존재하지 않거나 일부 다른 오류가 발생함) -1이 반환되고 errno가 적절하게 설정됩니다.
📌 함수 설명
access 함수는 특정 파일에 대한 사용자 권한이 있는지 검사하는 함수이며,
F_OK, R_OK, W_OK, X_OK 중 하나 이상의 권한을 확인한다.
첫번째 인자로는 pathname을, 두번째 인자로는 mode를 받는데,
pathname은 '체크하고자하는 디렉토리 또는 파일명', mode는 '적절한 마스크 값'을 넣으면 된다.
여기서 마스크 값이란, 연산에 사용되는 데이터로, shell에서는 umask 명령어를 통해 설정할 수 있고,
mode 변수에 들어갈 수 있는 마스크 값은 다음과 같다.
R_OK (= 4) | 파일 존재 여부 + 읽기 권한 여부 |
W_OK (= 2) | 파일 존재 여부 + 쓰기 권한 여부 |
X_OK (= 1) | 파일 존재 여부 + 실행 권한 여부 |
F_OK (= 0) | 파일 존재 여부 |
return 값으로는 int형이 오며, '요청된 모든 권한이 부여되었거나, F_OK일 때 파일이 존재'하면 0이 반환되고,
'요청된 권한이 거부된 권한이거나, F_OK이고 파일 존재하지 않거나 오류가 발생'하면 -1이 반환된다. (또한 errno 설정됨.)
mode는 소유자, 그룹 사용자, 기타 사용자 순서로 주어지며,
예시로 777일 경우 -rwxrwxrwx 로 모든 권한이 부여된 상태고,
544일 경우 -r-xr--r-- 로 소유자에게는 읽기 및 실행, 그룹과 기타 사용자는 읽기만 부여된 상태이다.
chmod [권한] [파일명] 를 통해 변경 가능하다.
#include <stdio.h>
#include <unistd.h>
int main()
{
char *pathname = "./test.txt";
int mode = R_OK;
if(access(pathname, mode) == 0 )
{
printf("읽을 수 있음.");
}
else
{
printf("권한이 없거나 존재하지 않음.");
}
return (0);
}
결과1) test.txt의 mode가 -rwxr--r-- (744) 일 경우
읽을 수 있음. |
결과2) test.txt의 mode가 --wx--x--x (311) 일 경우
권한이 없거나 존재하지 않음. |
결과3) test.txt의 mode가 -r-------- (400) 일 경우
읽을 수 있음. |
결과4) test.txt 파일이 존재하지 않는 경우
권한이 없거나 존재하지 않음. |
R_OK로 설정해둔 mode에 대해서 부합하는지 access가 검사해준다.
현재 user의 권한에 r-- 이 속해있으면 결과가 참으로 나온다.
#include <stdio.h>
#include <unistd.h>
int main()
{
char *pathname = "./test.txt";
int mode = R_OK | W_OK;
if(access(pathname, mode) == 0 )
{
printf("읽을 수 있음.");
}
else
{
printf("권한이 없거나 존재하지 않음.");
}
return (0);
}
위처럼 mode에 여러개를 넣어 사용할 수 있는데, 이를 10진수로 변형하여 살펴보면,
mode mask | binary | mode | decimal |
F_OK | 0 | --- | 0 |
X_OK | 1 | --x | 1 |
W_OK | 10 | -w- | 2 |
W_OK | X_OK | 11 | -wx | 3 |
R_OK | 100 | r-- | 4 |
R_OK | X_OK | 101 | r-x | 5 |
R_OK | W_OK | 110 | rw- | 6 |
R_OK | W_OK | X_OK | 111 | rwx | 7 |
이렇게 생각할 수 있다. mode에 mask 대신 decimal (10진수)를 넣어도 똑같이 동작한다.
'프로그래밍 > C' 카테고리의 다른 글
[function] dup2 함수 알아보기 (2) | 2024.05.28 |
---|---|
[function] dup 함수 알아보기 (0) | 2024.05.27 |
[function] strerror 함수 알아보기 (0) | 2024.05.23 |
[function] perror 함수 알아보기 (2) | 2024.05.20 |
[library] tolower 구현하기 (0) | 2024.05.13 |