aboutsummaryrefslogtreecommitdiff
path: root/proto.c
blob: acbc2511598bf501ce693d0488d91c717afb9508 (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* SPDX-License-Identifier: ISC */
#include <stddef.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

#include "syslogd.h"

static const char *months[] = {
	"Jan", "Feb", "Mar", "Apr",
	"May", "Jun", "Jul", "Aug",
	"Sep", "Oct", "Nov", "Dec"
};

static const int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

static int isleap(int year)
{
	return ((year % 4 == 0) && (year % 100 != 0)) || ((year % 400) == 0);
}

static int mdays(int year, int month)
{
	return (isleap(year) && month == 2) ? 29 : days[month - 1];
}

static char *read_num(char *str, int *out, int maxval)
{
	if (str == NULL || !isdigit(*str))
		return NULL;
	for (*out = 0; isdigit(*str); ++str) {
		(*out) = (*out) * 10 + (*str) - '0';
		if ((*out) > maxval)
			return NULL;
	}
	return str;
}

static char *skip_space(char *str)
{
	if (str == NULL || !isspace(*str))
		return NULL;
	while (isspace(*str))
		++str;
	return str;
}

static char *read_date_bsd(char *str, struct tm *tm)
{
	int year, month, day, hour, minute, second;
	time_t t;

	/* decode date */
	for (month = 0; month < 12; ++month) {
		if (strncmp(str, months[month], 3) == 0) {
			str = skip_space(str + 3);
			break;
		}
	}

	str = read_num(str, &day, 31);
	str = skip_space(str);

	t = time(NULL);
	if (localtime_r(&t, tm) == NULL)
		return NULL;

	year = tm->tm_year;

	/* sanity check */
	if (str == NULL || month >= 12 || day < 1)
		return NULL;
	if (month == 11 && tm->tm_mon == 0)
		--year;
	if (day > mdays(year + 1900, month + 1))
		return NULL;

	/* decode time */
	str = read_num(str, &hour, 23);
	if (str == NULL || *(str++) != ':')
		return NULL;
	str = read_num(str, &minute, 59);
	if (str == NULL || *(str++) != ':')
		return NULL;
	str = read_num(str, &second, 59);
	str = skip_space(str);

	/* store result */
	memset(tm, 0, sizeof(*tm));
	tm->tm_sec = second;
	tm->tm_min = minute;
	tm->tm_hour = hour;
	tm->tm_mday = day;
	tm->tm_mon = month;
	tm->tm_year = year;
	return str;
}

static char *decode_priority(char *str, int *priority)
{
	while (isspace(*str))
		++str;
	if (*(str++) != '<')
		return NULL;
	str = read_num(str, priority, 23 * 8 + 7);
	if (str == NULL || *(str++) != '>')
		return NULL;
	while (isspace(*str))
		++str;
	return str;
}

int syslog_msg_parse(syslog_msg_t *msg, char *str)
{
	char *ident, *ptr;
	struct tm tstamp;
	pid_t pid = 0;
	int priority;
	size_t len;

	memset(msg, 0, sizeof(*msg));

	str = decode_priority(str, &priority);
	if (str == NULL)
		return -1;

	msg->facility = priority >> 3;
	msg->level = priority & 0x07;

	str = read_date_bsd(str, &tstamp);
	if (str == NULL)
		return -1;

	ident = str;
	while (*str != '\0' && *str != ':')
		++str;

	if (*str == ':') {
		*(str++) = '\0';
		while (isspace(*str))
			++str;

		ptr = ident;
		while (*ptr != '[' && *ptr != '\0')
			++ptr;

		if (*ptr == '[') {
			*(ptr++) = '\0';

			while (isdigit(*ptr))
				pid = pid * 10 + *(ptr++) - '0';
		}
	} else {
		ident = NULL;
	}

	if (ident != NULL && ident[0] == '\0')
		ident = NULL;

	msg->timestamp = mktime(&tstamp);
	msg->pid = pid;
	msg->ident = ident;
	msg->message = str;

	len = strlen(str);
	while (len > 0 && isspace(str[len - 1]))
		--len;
	str[len] = '\0';

	if (ident != NULL) {
		for (ptr = ident; *ptr != '\0'; ++ptr) {
			if (!isalnum(*ptr))
				*ptr = '_';
		}
	}

	return 0;
}