Do You Coding?
[function] access ํจ์ ์์๋ณด๊ธฐ ๋ณธ๋ฌธ

๐ ๋งค๋ด์ผ (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์ง์)๋ฅผ ๋ฃ์ด๋ ๋๊ฐ์ด ๋์ํ๋ค.
'CS & Engineering > 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 |