-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccesslog.cpp
More file actions
429 lines (365 loc) · 10.4 KB
/
accesslog.cpp
File metadata and controls
429 lines (365 loc) · 10.4 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
* Copyright (c) 2017 Martin Decky
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <boost/tokenizer.hpp>
#include <boost/regex.hpp>
using namespace std;
using namespace boost;
/** Parts of domain name */
typedef vector< string> domain_vector;
/** Character list separator */
typedef char_separator< char> separator_type;
/** Character list tokenizer */
typedef tokenizer< separator_type> tokenizer_type;
typedef struct {
long int year;
long int month;
long int day;
long int hour;
long int minute;
long int second;
long int offset;
} datetime; /**< Date & time entry */
/** Basic prefix of the domain directories */
static const string prefix = "/home/httpd";
/** Basic suffix for the domain directories */
static string suffix = "";
/** Decode integer from string (base 10)
*
* Throws invalid_argument on invalid
* numerical string.
*
* @param decimal String to decode.
*
* @return Decoded integer.
*
*/
static long int decDecode(const string &decimal)
{
char *err;
long int val = strtol(decimal.c_str(), &err, 10);
if ((err == NULL) || (*err != (char) 0))
throw invalid_argument("Not an integer");
return val;
}
/** Encode integer to string (base 10)
*
*
* @param value Integer to encode.
*
* @return Encoded string.
*
*/
static string decEncode(const long int value)
{
ostringstream stream;
stream << value;
return stream.str();
}
/** Add leading zeroes to the string
*
* If the first character is not a digit then no leading zeroes
* are added to the string.
*
* @param str String to add leading zeroes to.
* @param num Number of leading zeroes (default: 2).
*
* @return String with leading zeroes.
*
*/
static string leadzero(const string &str, const unsigned int num = 2)
{
/* First character is not a digit */
if ((str.length() > 0) && ((str[0] < '0') || (str[0] > '9')))
return str;
string ret = str;
while (ret.length() < num)
ret = string("0") + ret;
return ret;
}
/** Decode month number from abbreviation
*
* Throws invalid_argument on invalid
* month abbreviation.
*
* @param month Month abbreviation.
*
* @return Month number (1-based).
*
*/
static long int monthDecode(const string &month)
{
if (month == "Jan")
return 1;
if (month == "Feb")
return 2;
if (month == "Mar")
return 3;
if (month == "Apr")
return 4;
if (month == "May")
return 5;
if (month == "Jun")
return 6;
if (month == "Jul")
return 7;
if (month == "Aug")
return 8;
if (month == "Sep")
return 9;
if (month == "Oct")
return 10;
if (month == "Nov")
return 11;
if (month == "Dec")
return 12;
throw invalid_argument("Invalid month '" + month + "'");
}
/** Get first occurence of a character
*
* @param str String to search in.
* @param delim Character to search.
* @param start Index to start searching from (default: 0).
*
* @return Index of the first occurence of the delimiting
* character.
* @return Length of the string if the delimiting character
* is not found.
*
*/
static string::size_type find_first(const string &str, const char delim,
const string::size_type start = 0)
{
for (string::size_type pos = start; pos < str.length(); pos++) {
/* Found 'delim' */
if (str[pos] == delim)
return pos;
}
return str.length();
}
/** Find first non-occurence of a character
*
* @param str String to search in.
* @param until Character to avoid.
* @param start Index to start searching from (default: 0).
*
* @return Index of the first occurence of a character different
* from the character we want to avoid.
* @return Length of the string if the entire string comprises
* of the character we want to avoid.
*
*/
static string::size_type find_until(const string &str, const char until,
const string::size_type start = 0)
{
for (string::size_type pos = start; pos < str.length(); pos++) {
/* Found character which is not 'until' */
if (str[pos] != until)
return pos;
}
return str.length();
}
/** Split domain name into parts
*
* @param domain Domain name to tokenize.
*
* @return Tokenized domain name.
*
*/
static domain_vector split_domain(const string &domain)
{
domain_vector vector;
/* Split string by '.' character */
separator_type separator(".", "", keep_empty_tokens);
tokenizer_type domain_tokens(domain, separator);
for (tokenizer_type::iterator it = domain_tokens.begin();
it != domain_tokens.end(); ++it)
vector.push_back(*it);
return vector;
}
/** Extract date & time from log entry
*
* Throws invalid_argument on invalid
* or no date & time signature.
*
* @param entry Date & time log entry.
*
* @return Decoded date & time.
*
*/
static datetime extract_datetime(const string &entry)
{
/* Date & time signature: [DD-Mon-YYYY:HH:MM:SS +off] */
regex expression("\\[../.../....:..:..:.. .....\\]");
string::const_iterator begin = entry.begin();
string::const_iterator end = entry.end();
match_results< string::const_iterator> match;
datetime res;
bool valid = false;
/* Regexp match */
if (regex_search(begin, end, match, expression, match_default)) {
string datetime = match[0];
/* Split match by all separating characters */
separator_type separator("[/: ]", "", drop_empty_tokens);
tokenizer_type datetime_tokens(datetime, separator);
unsigned int pos = 0;
for (tokenizer_type::iterator it = datetime_tokens.begin();
it != datetime_tokens.end(); ++it, pos++) {
switch (pos) {
case 0: /* Day */
res.day = decDecode(*it);
break;
case 1: /* Month */
res.month = monthDecode(*it);
break;
case 2: /* Year */
res.year = decDecode(*it);
break;
case 3: /* Hour */
res.hour = decDecode(*it);
break;
case 4: /* Minute */
res.minute = decDecode(*it);
break;
case 5: /* Second */
res.second = decDecode(*it);
break;
case 6: /* Offset */
res.offset = decDecode(*it);
valid = true;
break;
default:
throw invalid_argument("Invalid date & time format");
}
}
}
if (valid)
return res;
throw invalid_argument("Date & time not found or not complete");
}
/** Long write (wrapper for write(2))
*
* @param fd File descriptor.
* @param buf Data to write.
* @param count Number of bytes to write.
*
*/
static void write_long(int fd, const void *buf, size_t count)
{
size_t total = count;
while (total > 0) {
ssize_t written = write(fd, buf, total);
if (written < 0)
return;
total -= written;
buf = (void *) (((char *) buf) + written);
}
}
/** Process log entry and store to domain log
*
* @param entry Log entry to process.
*
*/
static void process_entry(const string &entry)
{
/* Ignore leading spaces */
string::size_type domain_start = find_until(entry, ' ');
/* Find domain name end */
string::size_type domain_end = find_first(entry, ' ', domain_start);
/* Ignore leading spaces in log entry */
string::size_type log_start = find_until(entry, ' ', domain_end);
/* Domain name is not empty */
if ((log_start < entry.length()) && (domain_start < domain_end)) {
string domain =
entry.substr(domain_start, domain_end - domain_start);
domain_vector domain_parts = split_domain(domain);
/* Domain name has two or more parts */
if (domain_parts.size() >= 2) {
string access = entry.substr(log_start);
datetime log_time = extract_datetime(access);
/*
* Domain log path is
* ${PREFIX}/${2ND_LEVEL_DOMAIN}/logs/${YYYY}-${MM}/${DOMAIN}${SUFFIX}
*/
string log_dir = prefix +
string("/") + domain_parts[domain_parts.size() - 2] +
string(".") + domain_parts[domain_parts.size() - 1] +
string("/logs/") + leadzero(decEncode(log_time.year), 4) +
string("-") + leadzero(decEncode(log_time.month)) +
suffix;
/* Make sure the {YYYY}-{MM} directory exists */
mkdir(log_dir.c_str(), S_IRUSR | S_IWUSR | S_IXUSR |
S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
/* Open domain log for appending */
int fd = open((log_dir + string("/") + domain).c_str(),
O_WRONLY | O_CREAT | O_APPEND | O_LARGEFILE,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd >= 0) {
/* Store log entry */
write_long(fd, access.c_str(), access.length());
write_long(fd, "\n", 1);
close(fd);
}
}
}
}
int main(int argc, char *argv[])
{
/* Get optional suffix */
if (argc > 1) {
string arg = argv[1];
regex filter("[a-z]*");
string::const_iterator begin = arg.begin();
string::const_iterator end = arg.end();
match_results< string::const_iterator> match;
if (regex_search(begin, end, match, filter, match_default))
suffix = string(".") + match[0];
}
string entry;
/* Process each line of input */
while (getline(cin, entry, '\n')) {
try {
process_entry(entry);
} catch (std::exception & e) {
cerr << "Exception while processing access log entry: " <<
e.what() << endl;
} catch (...) {
/* All exceptions are treated non-fatal */
cerr << "Unexpected exception while processing "
"access log entry" << endl;
}
}
return 0;
}