Do You Coding?

[function] access ํ•จ์ˆ˜ ์•Œ์•„๋ณด๊ธฐ ๋ณธ๋ฌธ

CS & Engineering/C

[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์ง„์ˆ˜)๋ฅผ ๋„ฃ์–ด๋„ ๋˜‘๊ฐ™์ด ๋™์ž‘ํ•œ๋‹ค.