로그인 한 사람 확인

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <unistd.h>
 
int main(int argc, char **argv)
{
    char bufs[256]="";
 
    printf("login user name:%s\n",getlogin());
 
    cuserid(bufs);
    printf("effective user name:%s\n",bufs);
 
    return 0;
}
 
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

 

사용자 ID 얻기

#include <sys/types.h>

#include <unistd.h>

uid_t getuid(void);

uid_t geteuid(void);

실제 사용자 ID와 유효 사용자 ID를 구한다, 성공하면 해당 ID를 반환

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
 
int main()
{
    struct passwd *lpwd;
    printf("UID\t:%d\n",getuid());
    printf("EUID\t:%d\n",geteuid());
 
    lpwd=getpwuid(getuid());
    printf("UNAME : %s\n",lpwd->pw_name);
 
    lpwd-getpwuid(geteuid());
    printf("EUNAME : %s\n",lpwd->pw_name);
 
    return 0;
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

사용자 ID 변경

#include <sys/types.h>

#include <unistd.h>

int setuid(uid_t uid);

int seteuid(uid_t uid);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
int main()
{
    int fd,state;
    state=seteuid(500); //500: 계정의 UID /etc/passwd 파일 참조
 
    if(state<0)
    {
        perror("error");
        return -1;
    }
    if((fd=open("test.txt",O_CREAT | O_RDWR, S_IRWXU | S_IRUSR))<0)
    {
        perror("error");
        return -1;
    }
    close(fd);
    return 0;
}
 
    
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

패스워드 파일 /etc/passwd

#include <pwd.h>

struct passwd *getpwnam(const char* name);

- 패스워드를 줄단위로 읽어서 name과 같은 로그인명을 포함하는 줄을 찾아 그 내용을 반환

strcut passwd *getpwuid(uid_t uid);

- uid와 같은 사용자 ID가 포함된 줄을 찾아서 반환

strcut passwd *fgetpwent(FILE *fp);

- 패스워드 파일 대신 fp 파일의 내용을 읽어 반환

새로운 struct passwd의 내용으로 덮어쓰는 정적 데이터의 포인터를 반환

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <stdio.h>
 
int main()
{
    struct passwd *u_info;
    struct group *g_info;
 
    u_info=getpwnam("valentis");
    g_info=getgrnam("root");
 
    chown("/home/test.txt",u_info->pw_uid,g_info->gr_gid);
 
    return 0;
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <pwd.h>
#include <sys/types.h>
 
int main()
{
    char buf[80];
    int uid;
    struct passwd *pwinfo=NULL;
    fgets(buf,80,stdin);
    uid=atoi(buf);
    pwinfo=getpwuid(uid);
 
    printf("%d : %s, %s %s\n",uid,pwinfo->pw_name,pwinfo->pw_dir,pwinfo->pw_shell);
    return 0;
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

strcut passwd *getpwent(void);  패스어드 파일을 순차적으로 읽어서 반환

void setpwent(void); 패스워드 파일이 열려있지 않다면 파일을 열고 읽기/쓰기 오프셋 파일의 시작 지점으로 설정

void entpwent(void); 패스워드를 닫는다.

 

 

그룹파일

#include <grp.h>

struct group *getgrnam(const char* name); 

그룹 파일을 읽어서 name과 일치하는 로그인명을 포함하는 줄을 찾는다.

struct group *getgrgid(gid_t gid);

그룹의 gid와 같은 줄을 찾는다.

struct group *fgetgrnam(FILE *fp);

그룹 파일을 대체하는 파일을 읽는다.

strcut group 구조체 포인터를 반환

 

struct group *getgrent(void); 호출때마다 다음 줄을 가져온다.

void setgrent(void); 그룹 파일을 열고 오프셋을 시작점에 위치시킨다.

void entgrent(void); 그룹 파일을 닫는다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <unistd.h>
#include <grp.h>
 
int main()
{
    struct group *group_entry;
 
    while(NULL!=(group_entry=getgrent())){
        printf("group name : %s\n",group_entry->gr_name);
        printf("group id : %d\n",group_entry->gr_gid);
    }
 
    setgrent(); //그룹 파일의 첫 번째 행으로 이동
    group_entry = getgrent(); //그룹 파일 첫 행을 출력
 
    printf("group name : %s\n",group_entry->gr_name);
    printf("group id : %d\n",group_entry->gr_gid);
    endgrent(); //그룹 파일 닫음
 
    return 0;
}
 
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

 

utmp와 wtmp 파일

utmp 파일: 현재 시스템의 상태 정보 포함, 로그인한 사용자 정보

wtmp 파일: utmp와 형식이 같다. 히스토리 정보 포함

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pwd.h>
#include <utmp.h>
#include <unistd.h>
 
int main(int argc,char *argv[])
{
    struct utmp entry;
 
    system("echo before adding entry:;who");
    entry.ut_pid=getpid();
    strcpy(entry.ut_line,ttyname(0)+strlen("/dev/"));
    // only correct for ptys named /dev/tty[pqr][0-9a-z] 
    strcpy(entry.ut_id,ttyname(0)+strlen("/dev/tty"));
    time(&entry.ut_time);
    strcpy(entry.ut_user,getpwuid(getuid())->pw_name);
 
    memset(entry.ut_host,0,UT_HOSTSIZE);
    entry.ut_addr=0;
    setutent();
    pututline(&entry);
 
 
    system("echo after adding entry:;who");
    entry.ut_type=DEAD_PROCESS;
    memset(entry.ut_line,0,UT_LINESIZE);
    entry.ut_time=0;
    memset(entry.ut_user,0,UT_NAMESIZE);
    setutent();
    pututline(&entry);
 
    system("echo after removing entry:;who");
    endutent();
    return 0;
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string.h>
#include <stdlib.h>
#include <pwd.h>
#include <unistd.h>
#include <utmpx.h>
#include <stdio.h>
int main(void)
{
    struct utmpx *utx;
 
    printf("Login User\n");
    while((utx=getutxent())!=NULL)
    {
        if(utx->ut_type != USER_PROCESS)
            continue;
        printf("%10s %5s\n",utx->ut_user,utx->ut_line);
    }
 
    return 0;
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

+ Recent posts