-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurelog.c
97 lines (79 loc) · 1.91 KB
/
securelog.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
#define _GNU_SOURCE
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <signal.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#define MAX_LOG_ENTRY_SIZE 4096
void help(void) {
fprintf(stderr, "Usage: ./securelog <YYYY-mm-ddTHH:MM:SS>_<message>\n");
}
size_t strnlen(const char *s, size_t maxlen) {
size_t z;
for (z=0; z < maxlen && s[z]; z++);
return z;
}
int check_log_format(char *msg) {
struct tm logtime;
char *p = strptime(msg, "%Y-%m-%dT%H:%M:%S", &logtime);
if (!p) {
fprintf(stderr, "Could not match ISO8601 date format\n");
return 1;
}
if (*p != '_') {
fprintf(stderr, "Could not find delimiter '_'\n");
return 1;
}
return 0;
}
int append_to_file(const char *path, char *data) {
int fd = open(path, O_APPEND | O_WRONLY);
if (fd < 0) {
perror(path);
return 1;
}
size_t size = strlen(data);
if (write(fd, data, size) != size) {
perror("write");
return 2;
}
if (write(fd, "\n", 1) != 1) {
perror("write");
return 3;
}
return 0;
}
int log_message(char *msg) {
char buf[MAX_LOG_ENTRY_SIZE];
time_t now_epoch = time(NULL);
struct tm now = *localtime(&now_epoch);
size_t t = strftime(buf, 32, "%Y-%m-%dT%H:%M:%S_", &now);
size_t u;
u = sprintf(buf + t, "%05d_", getuid());
strcpy(buf + u + t, msg);
return append_to_file("./secure.log", buf);
}
int main(int argc, char **argv) {
if (argc <= 1) {
help();
exit(1);
}
char *msg = argv[1];
if (check_log_format(msg)) {
fprintf(stderr, "Wrong log format\n");
help();
exit(2);
}
if (log_message(msg)) {
fprintf(stderr, "Could not log message\n");
exit(3);
}
return 0;
}