-
Notifications
You must be signed in to change notification settings - Fork 1
/
dmesg_checking.c
135 lines (108 loc) · 2.29 KB
/
dmesg_checking.c
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <signal.h>
#include <string.h>
#define MAX_BUF 512
#define SUCCESS 1
#define FAIL 0
int dmesg_check(char *buf){
const char* tok=" ";
const char* ima="type=1800";
char *str;
int count=0;
if(!buf)
return 0;
strtok(buf,tok);
while(str = strtok(NULL,tok)){
count++;
if(count == 3)
break;
}
if(count == 3){
if(!strncmp(str,ima,9)){
return 1;
}
}
return 0;
}
int sudo_check(){
FILE *fp_sudo;
char buf[MAX_BUF]={0,};
char *str = NULL;
const char *key = "\n";
int count, ret=0;
fp_sudo = popen("pgrep sudo | wc -l", "r");
if(fp_sudo == NULL)
goto out;
while(fgets(buf, MAX_BUF, fp_sudo)){
fprintf(stdout,"%s",buf);
if(count = atoi(buf)){
ret = count;
break;
}
memset(buf,0,MAX_BUF);
}
out:
if(fp_sudo)
pclose(fp_sudo);
return ret;
}
int dmesg_IO(){
FILE *fp_dmesg, *fp_log;
char buf[MAX_BUF]={0,};
char buf_cpy[MAX_BUF]={0,};
int ret=FAIL;
fp_dmesg = popen("dmesg", "r");
if(fp_dmesg == NULL){
goto out;
}
fp_log = fopen("/usr/ima-appraisal/log.txt", "wt");
if(fp_log == NULL){
goto out;
}
while(fgets(buf, MAX_BUF, fp_dmesg) != NULL){
strcpy(buf_cpy,buf);
if(dmesg_check(buf_cpy)){
fprintf(fp_log,"%s",buf);
memset(buf,0,sizeof(buf));
memset(buf_cpy,0,sizeof(buf_cpy));
}
}
ret = SUCCESS;
fflush(fp_log);
out:
if(fp_dmesg)
pclose(fp_dmesg);
if(fp_log)
fclose(fp_log);
return ret;
}
int main(){
int pid, ret=1;
int size=0;
if((pid=fork()) <0){
return 1;
}else if(pid > 0){
_exit(0);
}
if(setsid() == -1)
_exit(0);
if(chdir("/") == -1)
_exit(0);
umask(0);
signal(SIGHUP, SIG_IGN);
close(0);
close(1);
close(2);
stdin = freopen("/dev/null", "r", stdin);
stdout = freopen("/dev/null", "w", stdout);
stderr = freopen("/dev/null", "w", stderr);
while(!sudo_check()){}
while(1){
dmesg_IO();
sleep(10);
}
return 0;
}