/
server-logfile.c
456 lines (419 loc) · 15.1 KB
/
server-logfile.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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*****************************************************************************
* Written by Chris Dunlap <cdunlap@llnl.gov>.
* Copyright (C) 2007-2013 Lawrence Livermore National Security, LLC.
* Copyright (C) 2001-2007 The Regents of the University of California.
* UCRL-CODE-2002-009.
*
* This file is part of ConMan: The Console Manager.
* For details, see <http://conman.googlecode.com/>.
*
* ConMan is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* ConMan is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with ConMan. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
#if HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "common.h"
#include "log.h"
#include "server.h"
#include "util-file.h"
#include "util-str.h"
int parse_logfile_opts(logopt_t *opts, const char *str,
char *errbuf, int errlen)
{
/* Parses 'str' for logfile device options 'opts'.
* The 'opts' struct should be initialized to a default value.
* The 'str' string is of the form "(sanitize|nosanitize)".
* Returns 0 and updates the 'opts' struct on success; o/w, returns -1
* (writing an error message into 'errbuf' if defined).
*/
logopt_t optsTmp;
char buf[MAX_LINE];
const char * const separators = " \t\n.,;";
char *tok;
assert(opts != NULL);
/* By setting the tmp opts to the 'opts' that are passed in,
* we establish defaults for any values that are not changed by 'str'.
*/
optsTmp = *opts;
if ((str == NULL) || str[0] == '\0') {
if ((errbuf != NULL) && (errlen > 0))
snprintf(errbuf, errlen, "encountered empty options string");
return(-1);
}
/* Since strtok() modifies its string, we need to create a working copy.
*/
if (strlcpy(buf, str, sizeof(buf)) >= sizeof(buf)) {
if ((errbuf != NULL) && (errlen > 0))
snprintf(errbuf, errlen, "logopt string exceeded buffer size");
return(-1);
}
/* Parse the string.
*/
tok = strtok(buf, separators);
while (tok != NULL) {
if (!strcasecmp(tok, "lock"))
optsTmp.enableLock = 1;
else if (!strcasecmp(tok, "nolock"))
optsTmp.enableLock = 0;
else if (!strcasecmp(tok, "sanitize"))
optsTmp.enableSanitize = 1;
else if (!strcasecmp(tok, "nosanitize"))
optsTmp.enableSanitize = 0;
else if (!strcasecmp(tok, "timestamp"))
optsTmp.enableTimestamp = 1;
else if (!strcasecmp(tok, "notimestamp"))
optsTmp.enableTimestamp = 0;
else {
log_msg(LOG_WARNING, "ignoring unrecognized token '%s'", tok);
}
tok = strtok(NULL, separators);
}
*opts = optsTmp;
return(0);
}
obj_t * create_logfile_obj(server_conf_t *conf, char *name,
obj_t *console, logopt_t *opts, char *errbuf, int errlen)
{
/* Creates a new logfile object and adds it to the master objs list.
* Note: the logfile is open and set for non-blocking I/O.
* Note: the logfile will later be opened and set for non-blocking I/O
* by main:open_objs:reopen_obj:open_logfile_obj().
* Returns the new object, or NULL on error.
*/
ListIterator i;
obj_t *logfile;
char buf[MAX_LINE];
char *pname;
obj_t *obj;
assert(conf != NULL);
assert((name != NULL) && (name[0] != '\0'));
assert(console != NULL);
assert(opts != NULL);
/* Check for duplicate logfile names.
* While the write-lock will protect against two separate daemons
* using the same logfile, it will not protect against two logfile
* objects within the same daemon process using the same filename.
* So that check is performed here.
*/
if (strchr(name, '%')
&& (format_obj_string(buf, sizeof(buf), console, name) >= 0)) {
pname = buf;
}
else {
pname = name;
}
i = list_iterator_create(conf->objs);
while ((logfile = list_next(i))) {
if (!is_logfile_obj(logfile)) {
continue;
}
if (!strcmp(logfile->name, pname)) {
break;
}
}
list_iterator_destroy(i);
if (logfile) {
snprintf(errbuf, errlen, "console [%s] already logging to \"%s\"",
logfile->aux.logfile.console->name, pname);
return(NULL);
}
logfile = create_obj(conf, name, -1, CONMAN_OBJ_LOGFILE);
logfile->aux.logfile.console = console;
logfile->aux.logfile.lineState = CONMAN_LOG_LINE_INIT;
logfile->aux.logfile.opts = *opts;
logfile->aux.logfile.gotTruncate = !!conf->enableZeroLogs;
if (logfile->aux.logfile.opts.enableSanitize
|| logfile->aux.logfile.opts.enableTimestamp) {
logfile->aux.logfile.gotProcessing = 1;
}
else {
logfile->aux.logfile.gotProcessing = 0;
}
if (strchr(name, '%')) {
logfile->aux.logfile.fmtName = create_string(name);
}
else {
logfile->aux.logfile.fmtName = NULL;
}
if (is_process_obj(console)) {
console->aux.process.logfile = logfile;
}
else if (is_serial_obj(console)) {
console->aux.serial.logfile = logfile;
}
else if (is_telnet_obj(console)) {
console->aux.telnet.logfile = logfile;
}
else if (is_unixsock_obj(console)) {
console->aux.unixsock.logfile = logfile;
}
#if WITH_FREEIPMI
else if (is_ipmi_obj(console)) {
console->aux.ipmi.logfile = logfile;
}
#endif /* WITH_FREEIPMI */
else {
log_err(0, "INTERNAL: Unrecognized console [%s] type=%d",
console->name, console->type);
}
/* Add obj to the master conf->objs list
* before its corresponding console obj.
*/
i = list_iterator_create(conf->objs);
while ((obj = list_next(i))) {
if (obj == console) {
list_insert(i, logfile);
break;
}
}
list_iterator_destroy(i);
if (!obj) {
log_err(0, "INTERNAL: Console [%s] object not found in master list",
console->name);
}
return(logfile);
}
int open_logfile_obj(obj_t *logfile)
{
/* (Re)opens the specified 'logfile' obj.
* Since this logfile can be re-opened after the daemon has chdir()'d,
* it must be specified with an absolute pathname.
* Returns 0 if the logfile is successfully opened; o/w, returns -1.
*/
char dirname[PATH_MAX];
int flags;
char *now;
char *msg;
assert(logfile != NULL);
assert(is_logfile_obj(logfile));
assert(logfile->name != NULL);
assert(logfile->name[0] == '/');
assert(logfile->aux.logfile.console != NULL);
assert(logfile->aux.logfile.console->name != NULL);
if (logfile->fd >= 0) {
if (close(logfile->fd) < 0) /* log err and continue */
log_msg(LOG_WARNING, "Unable to close logfile \"%s\": %s",
logfile->name, strerror(errno));
logfile->fd = -1;
}
/* Perform conversion specifier expansion.
*/
if (logfile->aux.logfile.fmtName) {
char buf[MAX_LINE];
if (format_obj_string(buf, sizeof(buf),
logfile->aux.logfile.console,
logfile->aux.logfile.fmtName) < 0) {
log_msg(LOG_WARNING,
"Unable to open logfile for [%s]: filename exceeded buffer",
logfile->aux.logfile.console->name);
logfile->fd = -1;
return(-1);
}
free(logfile->name);
logfile->name = create_string(buf);
}
/* Create intermediate directories.
*/
if (get_dir_name(logfile->name, dirname, sizeof(dirname))) {
(void) create_dirs(dirname);
}
/* Only truncate on the initial open if ZeroLogs was enabled.
*/
flags = O_WRONLY | O_CREAT | O_APPEND | O_NONBLOCK;
if (logfile->aux.logfile.gotTruncate) {
logfile->aux.logfile.gotTruncate = 0;
flags |= O_TRUNC;
}
if ((logfile->fd = open(logfile->name, flags, S_IRUSR | S_IWUSR)) < 0) {
log_msg(LOG_WARNING, "Unable to open logfile \"%s\": %s",
logfile->name, strerror(errno));
return(-1);
}
if (logfile->aux.logfile.opts.enableLock
&& (get_write_lock(logfile->fd) < 0)) {
log_msg(LOG_WARNING, "Unable to lock \"%s\"", logfile->name);
close(logfile->fd); /* ignore err on close() */
logfile->fd = -1;
return(-1);
}
logfile->gotEOF = 0;
set_fd_nonblocking(logfile->fd); /* redundant, just playing it safe */
set_fd_closed_on_exec(logfile->fd);
now = create_long_time_string(0);
msg = create_format_string("%sConsole [%s] log opened at %s%s",
CONMAN_MSG_PREFIX, logfile->aux.logfile.console->name, now,
CONMAN_MSG_SUFFIX);
write_obj_data(logfile, msg, strlen(msg), 0);
free(now);
free(msg);
/*
* Since the above console log message is not marked "informational",
* the test in write_obj_data() to re-init the line state will not
* be triggered. Thusly, we re-initialize the line state here.
*/
logfile->aux.logfile.lineState = CONMAN_LOG_LINE_INIT;
DPRINTF((9, "Opened [%s] logfile: fd=%d file=%s.\n",
logfile->aux.logfile.console->name, logfile->fd, logfile->name));
return(0);
}
obj_t * get_console_logfile_obj(obj_t *console)
{
/* Returns a ptr to the logfile obj associated with 'console'
* if one exists and is currently active; o/w, returns NULL.
*/
obj_t *logfile = NULL;
assert(console != NULL);
assert(is_console_obj(console));
if (is_process_obj(console)) {
logfile = console->aux.process.logfile;
}
else if (is_serial_obj(console)) {
logfile = console->aux.serial.logfile;
}
else if (is_telnet_obj(console)) {
logfile = console->aux.telnet.logfile;
}
else if (is_unixsock_obj(console)) {
logfile = console->aux.unixsock.logfile;
}
#if WITH_FREEIPMI
else if (is_ipmi_obj(console)) {
logfile = console->aux.ipmi.logfile;
}
#endif /* WITH_FREEIPMI */
else {
log_err(0, "INTERNAL: Unrecognized console [%s] type=%d",
console->name, console->type);
}
if (!logfile || (logfile->fd < 0)) {
return(NULL);
}
assert(is_logfile_obj(logfile));
return(logfile);
}
int write_log_data(obj_t *log, const void *src, int len)
{
/* Writes a potentially modified version of the buffer (src) of length (len)
* into the logfile obj (log); the original (src) buffer is not modified.
* This routine is intended for writing data that was read from a console,
* not informational messages.
* If sanitized logs are enabled, data is stripped to 7-bit ASCII and
* control/binary characters are displayed as two-character printable
* sequences.
* If newline timestamping is enabled, the current timestamp is appended
* after each newline.
* Returns the number of bytes written into the logfile obj's buffer.
*/
const int minbuf = 25; /* cr/lf + timestamp + meta/char */
unsigned char buf[MAX_BUF_SIZE - 1];
const unsigned char *p;
unsigned char *q;
const unsigned char * const qLast = buf + sizeof(buf);
int n = 0;
assert(is_logfile_obj(log));
assert(sizeof(buf) >= minbuf);
/* If no additional processing is needed, listen to Biff Tannen:
* "make like a tree and get outta here".
*/
if (!log->aux.logfile.gotProcessing) {
return(write_obj_data(log, src, len, 0));
}
DPRINTF((15, "Processing %d bytes for [%s] log \"%s\".\n",
len, log->aux.logfile.console->name, log->name));
for (p=src, q=buf; len>0; p++, len--) {
/*
* A newline state machine is used to properly sanitize CR/LF line
* terminations. This is responsible for coalescing multiple CRs,
* swapping LF/CR to CR/LF, transcribing CR/NUL to CR/LF,
* prepending a CR to a lonely LF, and appending a LF to a
* lonely CR to prevent characters from being overwritten.
*/
if (*p == '\r') {
if (log->aux.logfile.lineState == CONMAN_LOG_LINE_DATA) {
log->aux.logfile.lineState = CONMAN_LOG_LINE_CR;
}
else if (log->aux.logfile.lineState == CONMAN_LOG_LINE_INIT) {
if (log->aux.logfile.opts.enableTimestamp)
q += write_time_string(0, (char *) q, qLast - q);
log->aux.logfile.lineState = CONMAN_LOG_LINE_CR;
}
else {
; /* ignore */
}
}
else if (*p == '\n') {
if ( (log->aux.logfile.lineState == CONMAN_LOG_LINE_INIT)
|| (log->aux.logfile.lineState == CONMAN_LOG_LINE_LF) ) {
if (log->aux.logfile.opts.enableTimestamp)
q += write_time_string(0, (char *) q, qLast - q);
}
*q++ = '\r';
*q++ = '\n';
log->aux.logfile.lineState = CONMAN_LOG_LINE_LF;
}
else if ( (*p == '\0')
&& ( (log->aux.logfile.lineState == CONMAN_LOG_LINE_CR)
|| (log->aux.logfile.lineState == CONMAN_LOG_LINE_LF) ) ) {
; /* ignore */
}
else {
if (log->aux.logfile.lineState == CONMAN_LOG_LINE_CR) {
*q++ = '\r';
*q++ = '\n';
}
if (log->aux.logfile.lineState != CONMAN_LOG_LINE_DATA) {
if (log->aux.logfile.opts.enableTimestamp)
q += write_time_string(0, (char *) q, qLast - q);
}
log->aux.logfile.lineState = CONMAN_LOG_LINE_DATA;
if (log->aux.logfile.opts.enableSanitize) {
int c = *p & 0x7F; /* strip data to 7-bit ASCII */
if (c < 0x20) { /* ASCII ctrl-chars */
*q++ = (*p & 0x80) ? '~' : '^';
*q++ = c + '@';
}
else if (c == 0x7F) { /* ASCII DEL char */
*q++ = (*p & 0x80) ? '~' : '^';
*q++ = '?';
}
else {
if (*p & 0x80)
*q++ = '`';
*q++ = c;
}
}
else {
*q++ = *p;
}
}
/* Flush internal buffer before it overruns.
*/
if ((qLast - q) < minbuf) {
assert((q >= buf) && (q <= qLast));
n += write_obj_data(log, buf, q - buf, 0);
q = buf;
}
}
assert((q >= buf) && (q <= qLast));
n += write_obj_data(log, buf, q - buf, 0);
return(n);
}