/src/proftpd/modules/mod_log.c
Line | Count | Source |
1 | | /* |
2 | | * ProFTPD - FTP server daemon |
3 | | * Copyright (c) 1997, 1998 Public Flood Software |
4 | | * Copyright (c) 1999, 2000 MacGyver aka Habeeb J. Dihu <macgyver@tos.net> |
5 | | * Copyright (c) 2001-2026 The ProFTPD Project team |
6 | | * |
7 | | * This program is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 2 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * This program is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, see <https://www.gnu.org/licenses/>. |
19 | | * |
20 | | * As a special exemption, Public Flood Software/MacGyver aka Habeeb J. Dihu |
21 | | * and other respective copyright holders give permission to link this program |
22 | | * with OpenSSL, and distribute the resulting executable, without including |
23 | | * the source code for OpenSSL in the source distribution. |
24 | | */ |
25 | | |
26 | | /* Flexible logging module for proftpd */ |
27 | | |
28 | | #include "conf.h" |
29 | | #include "privs.h" |
30 | | #include "logfmt.h" |
31 | | #include "jot.h" |
32 | | |
33 | 0 | #define MOD_LOG_VERSION "mod_log/1.0" |
34 | | |
35 | | module log_module; |
36 | | |
37 | | /* Max path length plus 128 bytes for additional info. */ |
38 | | #define EXTENDED_LOG_BUFFER_SIZE (PR_TUNABLE_PATH_MAX + 128) |
39 | | |
40 | 0 | #define EXTENDED_LOG_MODE 0644 |
41 | 0 | #define EXTENDED_LOG_FORMAT_DEFAULT "default" |
42 | | |
43 | | typedef struct logformat_struc logformat_t; |
44 | | typedef struct logfile_struc logfile_t; |
45 | | |
46 | | struct logformat_struc { |
47 | | logformat_t *next, *prev; |
48 | | |
49 | | char *lf_fmt_name; |
50 | | unsigned char *lf_format; |
51 | | }; |
52 | | |
53 | | struct logfile_struc { |
54 | | logfile_t *next, *prev; |
55 | | |
56 | | char *lf_filename; |
57 | | int lf_fd; |
58 | | int lf_syslog_level; |
59 | | |
60 | | logformat_t *lf_format; |
61 | | pr_jot_filters_t *lf_jot_filters; |
62 | | |
63 | | /* Pointer to the "owning" configuration */ |
64 | | config_rec *lf_conf; |
65 | | }; |
66 | | |
67 | | /* Value for lf_fd signalling that data should be logged via syslog, rather |
68 | | * than written to a file. |
69 | | */ |
70 | 0 | #define EXTENDED_LOG_SYSLOG -4 |
71 | | |
72 | | static pool *log_pool = NULL; |
73 | | static logformat_t *formats = NULL; |
74 | | static xaset_t *format_set = NULL; |
75 | | static logfile_t *logs = NULL; |
76 | | static xaset_t *log_set = NULL; |
77 | | |
78 | | static const char *trace_channel = "extlog"; |
79 | | |
80 | | /* format string args: |
81 | | %A - Anonymous username (password given) |
82 | | %a - Remote client IP address |
83 | | %b - Bytes sent for request |
84 | | %{basename} - Basename of path |
85 | | %c - Class |
86 | | %D - full directory path |
87 | | %d - directory (for client) |
88 | | %E - End-of-session reason |
89 | | %{FOOBAR}e - Contents of environment variable FOOBAR |
90 | | %F - Transfer path (filename for client) |
91 | | %f - Filename |
92 | | %g - Local user's primary group name |
93 | | %H - Local IP address of server handling session |
94 | | %h - Remote client DNS name |
95 | | %I - Total number of "raw" bytes read in from network |
96 | | %J - Request (command) arguments (file.txt, etc) |
97 | | %L - Local IP address contacted by client |
98 | | %l - Remote logname (from identd) |
99 | | %m - Request (command) method (RETR, etc) |
100 | | %O - Total number of "raw" bytes written out to network |
101 | | %P - Process ID of child serving request |
102 | | %p - Port of server serving request |
103 | | %R - Response time for command/request, in milliseconds |
104 | | %r - Full request (command) |
105 | | %s - Response code (status) |
106 | | %S - Response string |
107 | | %T - Time taken to transfer file, in seconds |
108 | | %t - Time |
109 | | %{format}t - Formatted time (strftime(3) format) |
110 | | %U - Original username sent by client |
111 | | %u - Local user |
112 | | %V - DNS name of server serving request |
113 | | %v - ServerName of server serving request |
114 | | %w - RNFR path ("whence" a rename comes, i.e. the source) |
115 | | %{epoch} - Unix epoch (seconds since Jan 1 1970) |
116 | | %{file-modified} - Indicates whether a file is being modified |
117 | | (i.e. already exists) or not. |
118 | | %{file-offset} - Contains the offset at which the file is read/written |
119 | | %{file-size} - Contains the file size at the end of the transfer |
120 | | %{iso8601} - ISO-8601 timestamp: YYYY-MM-dd HH:mm:ss,SSS |
121 | | for example: "1999-11-27 15:49:37,459" |
122 | | %{microsecs} - 6 digits of microseconds of current time |
123 | | %{millisecs} - 3 digits of milliseconds of current time |
124 | | %{protocol} - Current protocol (e.g. "ftp", "sftp", etc) |
125 | | %{uid} - UID of logged-in user |
126 | | %{gid} - Primary GID of logged-in user |
127 | | %{transfer-failure} - reason, or "-" |
128 | | %{transfer-millisecs}- Time taken to transfer file, in milliseconds |
129 | | %{transfer-status} - "success", "failed", "cancelled", "timeout", or "-" |
130 | | %{transfer-type} - "binary" or "ASCII" |
131 | | %{version} - ProFTPD version |
132 | | */ |
133 | | |
134 | | /* Necessary prototypes */ |
135 | | static int log_sess_init(void); |
136 | | static void log_xfer_stalled_ev(const void *, void *); |
137 | | |
138 | | static void parse_logformat(const char *directive, char *fmt_name, |
139 | 0 | char *fmt_text) { |
140 | 0 | int res; |
141 | 0 | pool *tmp_pool; |
142 | 0 | pr_jot_ctx_t *jot_ctx; |
143 | 0 | pr_jot_parsed_t *jot_parsed; |
144 | 0 | unsigned char format_buf[4096] = {'\0'}; |
145 | 0 | size_t fmt_len; |
146 | 0 | logformat_t *lf; |
147 | | |
148 | | /* This function can cause potential problems. Custom LogFormats |
149 | | * might overrun the format buffer. Fixing this problem involves a |
150 | | * rewrite of most of this module. This will happen post 1.2.0. |
151 | | */ |
152 | |
|
153 | 0 | tmp_pool = make_sub_pool(log_pool); |
154 | 0 | jot_ctx = pcalloc(tmp_pool, sizeof(pr_jot_ctx_t)); |
155 | 0 | jot_parsed = pcalloc(tmp_pool, sizeof(pr_jot_parsed_t)); |
156 | 0 | jot_parsed->bufsz = jot_parsed->buflen = sizeof(format_buf); |
157 | 0 | jot_parsed->ptr = jot_parsed->buf = format_buf; |
158 | |
|
159 | 0 | jot_ctx->log = jot_parsed; |
160 | |
|
161 | 0 | res = pr_jot_parse_logfmt(tmp_pool, fmt_text, jot_ctx, pr_jot_parse_on_meta, |
162 | 0 | pr_jot_parse_on_unknown, pr_jot_parse_on_other, 0); |
163 | 0 | if (res < 0) { |
164 | 0 | pr_log_pri(PR_LOG_NOTICE, MOD_LOG_VERSION |
165 | 0 | ": error parsing LogFormat '%s': %s", fmt_text, strerror(errno)); |
166 | |
|
167 | 0 | destroy_pool(tmp_pool); |
168 | 0 | return; |
169 | 0 | } |
170 | | |
171 | 0 | fmt_len = jot_parsed->bufsz - jot_parsed->buflen; |
172 | |
|
173 | 0 | lf = (logformat_t *) pcalloc(log_pool, sizeof(logformat_t)); |
174 | 0 | lf->lf_fmt_name = pstrdup(log_pool, fmt_name); |
175 | 0 | lf->lf_format = palloc(log_pool, fmt_len + 1); |
176 | 0 | memcpy(lf->lf_format, format_buf, fmt_len); |
177 | 0 | lf->lf_format[fmt_len] = '\0'; |
178 | |
|
179 | 0 | if (format_set == NULL) { |
180 | 0 | format_set = xaset_create(log_pool, NULL); |
181 | 0 | } |
182 | |
|
183 | 0 | xaset_insert_end(format_set, (xasetmember_t *) lf); |
184 | 0 | formats = (logformat_t *) format_set->xas_list; |
185 | |
|
186 | 0 | if (directive != NULL) { |
187 | 0 | config_rec *c; |
188 | 0 | char *ptr; |
189 | | |
190 | | /* Store the parsed format in the config tree as well, for use by other |
191 | | * logging-related modules. |
192 | | */ |
193 | 0 | c = add_config_param(directive, 2, NULL, NULL); |
194 | 0 | c->argv[0] = pstrdup(c->pool, fmt_name); |
195 | 0 | c->argv[1] = palloc(c->pool, fmt_len + 1); |
196 | |
|
197 | 0 | ptr = c->argv[1]; |
198 | 0 | memcpy(ptr, format_buf, fmt_len); |
199 | 0 | ptr[fmt_len] = '\0'; |
200 | 0 | } |
201 | |
|
202 | 0 | destroy_pool(tmp_pool); |
203 | 0 | } |
204 | | |
205 | | /* Syntax: LogFormat name "format string" */ |
206 | 0 | MODRET set_logformat(cmd_rec *cmd) { |
207 | 0 | CHECK_ARGS(cmd, 2); |
208 | 0 | CHECK_CONF(cmd, CONF_ROOT|CONF_GLOBAL); |
209 | |
|
210 | 0 | if (strlen(cmd->argv[1]) == 0) { |
211 | 0 | CONF_ERROR(cmd, "missing required name parameter"); |
212 | 0 | } |
213 | | |
214 | 0 | parse_logformat(cmd->argv[0], cmd->argv[1], cmd->argv[2]); |
215 | 0 | return PR_HANDLED(cmd); |
216 | 0 | } |
217 | | |
218 | | /* usage: LogOptions opt1 ... */ |
219 | 0 | MODRET set_logoptions(cmd_rec *cmd) { |
220 | 0 | register unsigned int i; |
221 | 0 | int ctx; |
222 | 0 | unsigned long log_opts = PR_LOG_OPT_DEFAULT; |
223 | 0 | config_rec *c; |
224 | |
|
225 | 0 | if (cmd->argc < 2) { |
226 | 0 | CONF_ERROR(cmd, "wrong number of parameters"); |
227 | 0 | } |
228 | | |
229 | 0 | CHECK_CONF(cmd, CONF_ROOT|CONF_GLOBAL|CONF_VIRTUAL); |
230 | |
|
231 | 0 | for (i = 1; i < cmd->argc; i++) { |
232 | 0 | char action, *opt; |
233 | |
|
234 | 0 | opt = cmd->argv[i]; |
235 | 0 | action = *opt; |
236 | |
|
237 | 0 | if (action != '+' && |
238 | 0 | action != '-') { |
239 | 0 | CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "bad LogOption: '", opt, "'", |
240 | 0 | NULL)); |
241 | 0 | } |
242 | | |
243 | 0 | opt++; |
244 | |
|
245 | 0 | if (strcasecmp(opt, "Timestamp") == 0) { |
246 | 0 | switch (action) { |
247 | 0 | case '-': |
248 | 0 | log_opts &= ~PR_LOG_OPT_USE_TIMESTAMP; |
249 | 0 | break; |
250 | | |
251 | 0 | case '+': |
252 | 0 | log_opts |= PR_LOG_OPT_USE_TIMESTAMP; |
253 | 0 | break; |
254 | 0 | } |
255 | |
|
256 | 0 | } else if (strcasecmp(opt, "Hostname") == 0) { |
257 | 0 | switch (action) { |
258 | 0 | case '-': |
259 | 0 | log_opts &= ~PR_LOG_OPT_USE_HOSTNAME; |
260 | 0 | break; |
261 | | |
262 | 0 | case '+': |
263 | 0 | log_opts |= PR_LOG_OPT_USE_HOSTNAME; |
264 | 0 | break; |
265 | 0 | } |
266 | |
|
267 | 0 | } else if (strcasecmp(opt, "VirtualHost") == 0) { |
268 | 0 | switch (action) { |
269 | 0 | case '-': |
270 | 0 | log_opts &= ~PR_LOG_OPT_USE_VHOST; |
271 | 0 | break; |
272 | | |
273 | 0 | case '+': |
274 | 0 | log_opts |= PR_LOG_OPT_USE_VHOST; |
275 | 0 | break; |
276 | 0 | } |
277 | |
|
278 | 0 | } else if (strcasecmp(opt, "RoleBasedProcessLabels") == 0) { |
279 | 0 | switch (action) { |
280 | 0 | case '-': |
281 | 0 | log_opts &= ~PR_LOG_OPT_USE_ROLE_BASED_PROCESS_LABELS; |
282 | 0 | break; |
283 | | |
284 | 0 | case '+': |
285 | 0 | log_opts |= PR_LOG_OPT_USE_ROLE_BASED_PROCESS_LABELS; |
286 | 0 | break; |
287 | 0 | } |
288 | |
|
289 | 0 | } else { |
290 | 0 | CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unknown LogOption: '", |
291 | 0 | opt, "'", NULL)); |
292 | 0 | } |
293 | 0 | } |
294 | | |
295 | 0 | c = add_config_param(cmd->argv[0], 1, NULL); |
296 | 0 | c->argv[0] = palloc(c->pool, sizeof(unsigned long)); |
297 | 0 | *((unsigned long *) c->argv[0]) = log_opts; |
298 | |
|
299 | 0 | ctx = (cmd->config && cmd->config->config_type != CONF_PARAM ? |
300 | 0 | cmd->config->config_type : cmd->server->config_type ? |
301 | 0 | cmd->server->config_type : CONF_ROOT); |
302 | |
|
303 | 0 | if (ctx == CONF_ROOT) { |
304 | | /* If we're the "server config" context, set the LogOptions here, |
305 | | * too. This will apply these LogOptions to the daemon process. |
306 | | */ |
307 | 0 | if (pr_log_set_options(log_opts) < 0) { |
308 | 0 | pr_log_debug(DEBUG6, "%s: error setting LogOptions (%lu): %s", |
309 | 0 | (char *) cmd->argv[0], log_opts, strerror(errno)); |
310 | 0 | } |
311 | 0 | } |
312 | |
|
313 | 0 | return PR_HANDLED(cmd); |
314 | 0 | } |
315 | | |
316 | | /* Syntax: ExtendedLog file [<cmd-classes> [<name>]] */ |
317 | 0 | MODRET set_extendedlog(cmd_rec *cmd) { |
318 | 0 | config_rec *c = NULL; |
319 | 0 | int argc; |
320 | 0 | char *path; |
321 | |
|
322 | 0 | CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON); |
323 | |
|
324 | 0 | argc = cmd->argc; |
325 | |
|
326 | 0 | if (argc < 2) { |
327 | 0 | CONF_ERROR(cmd, "Syntax: ExtendedLog file [<cmd-classes> [<name>]]"); |
328 | 0 | } |
329 | | |
330 | 0 | c = add_config_param(cmd->argv[0], 3, NULL, NULL, NULL); |
331 | |
|
332 | 0 | path = cmd->argv[1]; |
333 | 0 | if (strncasecmp(path, "syslog:", 7) == 0) { |
334 | 0 | char *ptr; |
335 | |
|
336 | 0 | ptr = strchr(path, ':'); |
337 | |
|
338 | 0 | if (pr_log_str2sysloglevel(++ptr) < 0) { |
339 | 0 | CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unknown syslog level: '", |
340 | 0 | ptr, "'", NULL)); |
341 | 0 | } |
342 | | |
343 | 0 | c->argv[0] = pstrdup(log_pool, path); |
344 | |
|
345 | 0 | } else if (path[0] != '/') { |
346 | 0 | CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "relative paths not allowed: '", |
347 | 0 | path, "'", NULL)); |
348 | |
|
349 | 0 | } else { |
350 | 0 | c->argv[0] = pstrdup(log_pool, path); |
351 | 0 | } |
352 | | |
353 | 0 | if (argc > 2) { |
354 | 0 | pr_jot_filters_t *jot_filters; |
355 | 0 | char *ptr, *rules, *cmd_sifts = NULL; |
356 | |
|
357 | 0 | rules = cmd->argv[2]; |
358 | | |
359 | | /* Check for command sifting rules in the provided classes. */ |
360 | 0 | ptr = strchr(rules, '+'); |
361 | 0 | if (ptr != NULL) { |
362 | 0 | cmd_sifts = ptr + 1; |
363 | 0 | *ptr = '\0'; |
364 | 0 | } |
365 | |
|
366 | 0 | jot_filters = pr_jot_filters_create(c->pool, rules, |
367 | 0 | PR_JOT_FILTER_TYPE_CLASSES, 0); |
368 | 0 | if (jot_filters == NULL) { |
369 | 0 | CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "invalid log class in '", rules, |
370 | 0 | "': ", strerror(errno), NULL)); |
371 | 0 | } |
372 | | |
373 | 0 | if (cmd_sifts != NULL) { |
374 | 0 | int res; |
375 | |
|
376 | 0 | res = pr_jot_filters_parse_sifts(c->pool, jot_filters, cmd_sifts, 0); |
377 | 0 | if (res < 0) { |
378 | 0 | CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error parsing commands in '", |
379 | 0 | cmd_sifts, "': ", strerror(errno), NULL)); |
380 | 0 | } |
381 | 0 | } |
382 | | |
383 | 0 | c->argv[1] = jot_filters; |
384 | 0 | } |
385 | | |
386 | 0 | if (argc > 3) { |
387 | 0 | c->argv[2] = pstrdup(log_pool, cmd->argv[3]); |
388 | 0 | } |
389 | |
|
390 | 0 | return PR_HANDLED(cmd); |
391 | 0 | } |
392 | | |
393 | | /* Syntax: AllowLogSymlinks <on|off> */ |
394 | 0 | MODRET set_allowlogsymlinks(cmd_rec *cmd) { |
395 | 0 | int allow_log_symlinks = -1; |
396 | 0 | config_rec *c = NULL; |
397 | |
|
398 | 0 | CHECK_ARGS(cmd, 1); |
399 | 0 | CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL); |
400 | |
|
401 | 0 | allow_log_symlinks = get_boolean(cmd, 1); |
402 | 0 | if (allow_log_symlinks == -1) { |
403 | 0 | CONF_ERROR(cmd, "expected Boolean parameter"); |
404 | 0 | } |
405 | | |
406 | 0 | c = add_config_param(cmd->argv[0], 1, NULL); |
407 | 0 | c->argv[0] = pcalloc(c->pool, sizeof(unsigned char)); |
408 | 0 | *((unsigned char *) c->argv[0]) = allow_log_symlinks; |
409 | |
|
410 | 0 | return PR_HANDLED(cmd); |
411 | 0 | } |
412 | | |
413 | | /* Syntax: ServerLog <filename> */ |
414 | 0 | MODRET set_serverlog(cmd_rec *cmd) { |
415 | 0 | CHECK_ARGS(cmd, 1); |
416 | 0 | CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL); |
417 | |
|
418 | 0 | add_config_param_str(cmd->argv[0], 1, cmd->argv[1]); |
419 | 0 | return PR_HANDLED(cmd); |
420 | 0 | } |
421 | | |
422 | | /* Syntax: SystemLog <filename> */ |
423 | 0 | MODRET set_systemlog(cmd_rec *cmd) { |
424 | 0 | CHECK_ARGS(cmd, 1); |
425 | 0 | CHECK_CONF(cmd, CONF_ROOT|CONF_GLOBAL); |
426 | |
|
427 | 0 | (void) add_config_param_str(cmd->argv[0], 1, cmd->argv[1]); |
428 | 0 | return PR_HANDLED(cmd); |
429 | 0 | } |
430 | | |
431 | 0 | static struct tm *get_gmtoff(pool *p, int *tz) { |
432 | 0 | time_t now; |
433 | 0 | struct tm *gmt, *tm = NULL; |
434 | | |
435 | | /* Note that the ordering of the calls to gmtime(3) and pr_localtime() |
436 | | * here are IMPORTANT; gmtime(3) MUST be called first. Otherwise, |
437 | | * the TZ environment variable may not be honored as one would expect; |
438 | | * see: |
439 | | * https://forums.proftpd.org/smf/index.php/topic,11971.0.html |
440 | | */ |
441 | 0 | time(&now); |
442 | |
|
443 | 0 | #if defined(HAVE_GMTIME_R) |
444 | 0 | gmt = gmtime_r(&now, pcalloc(p, sizeof(struct tm))); |
445 | | #else |
446 | | gmt = gmtime(&now); |
447 | | #endif /* HAVE_GMTIME_R */ |
448 | 0 | if (gmt != NULL) { |
449 | 0 | tm = pr_localtime(p, &now); |
450 | 0 | if (tm != NULL) { |
451 | 0 | int days, hours, minutes; |
452 | |
|
453 | 0 | days = tm->tm_yday - gmt->tm_yday; |
454 | 0 | hours = ((days < -1 ? 24 : 1 < days ? -24 : days * 24) |
455 | 0 | + tm->tm_hour - gmt->tm_hour); |
456 | 0 | minutes = hours * 60 + tm->tm_min - gmt->tm_min; |
457 | 0 | *tz = minutes; |
458 | 0 | } |
459 | 0 | } |
460 | |
|
461 | 0 | return tm; |
462 | 0 | } |
463 | | |
464 | | /* Note: maybe the pr_buffer_t should be made to look like this? */ |
465 | | struct extlog_buffer { |
466 | | char *ptr, *buf; |
467 | | size_t bufsz, buflen; |
468 | | }; |
469 | | |
470 | | static void extlog_buffer_append(struct extlog_buffer *log, const char *text, |
471 | 0 | size_t text_len) { |
472 | 0 | if (text == NULL || |
473 | 0 | text_len == 0) { |
474 | 0 | return; |
475 | 0 | } |
476 | | |
477 | 0 | if (text_len > log->buflen) { |
478 | 0 | text_len = log->buflen; |
479 | 0 | } |
480 | |
|
481 | 0 | pr_trace_msg(trace_channel, 19, "appending text '%.*s' (%lu) to buffer", |
482 | 0 | (int) text_len, text, (unsigned long) text_len); |
483 | |
|
484 | 0 | if (text_len == 0) { |
485 | 0 | return; |
486 | 0 | } |
487 | | |
488 | 0 | memcpy(log->buf, text, text_len); |
489 | 0 | log->buf += text_len; |
490 | 0 | log->buflen -= text_len; |
491 | 0 | } |
492 | | |
493 | | static int resolve_on_meta(pool *p, pr_jot_ctx_t *jot_ctx, |
494 | 0 | unsigned char logfmt_id, const char *jot_hint, const void *val) { |
495 | 0 | struct extlog_buffer *log; |
496 | |
|
497 | 0 | log = jot_ctx->log; |
498 | 0 | if (log->buflen > 0) { |
499 | 0 | const char *text = NULL; |
500 | 0 | size_t text_len = 0; |
501 | 0 | char buf[1024]; |
502 | |
|
503 | 0 | switch (logfmt_id) { |
504 | 0 | case LOGFMT_META_MICROSECS: { |
505 | 0 | unsigned long num; |
506 | |
|
507 | 0 | num = *((double *) val); |
508 | 0 | text_len = pr_snprintf(buf, sizeof(buf)-1, "%06lu", num); |
509 | 0 | buf[text_len] = '\0'; |
510 | 0 | text = buf; |
511 | 0 | break; |
512 | 0 | } |
513 | | |
514 | 0 | case LOGFMT_META_MILLISECS: { |
515 | 0 | unsigned long num; |
516 | |
|
517 | 0 | num = *((double *) val); |
518 | 0 | text_len = pr_snprintf(buf, sizeof(buf)-1, "%03lu", num); |
519 | 0 | buf[text_len] = '\0'; |
520 | 0 | text = buf; |
521 | 0 | break; |
522 | 0 | } |
523 | | |
524 | 0 | case LOGFMT_META_LOCAL_PORT: |
525 | 0 | case LOGFMT_META_REMOTE_PORT: |
526 | 0 | case LOGFMT_META_RESPONSE_CODE: |
527 | 0 | case LOGFMT_META_XFER_PORT: { |
528 | 0 | int num; |
529 | |
|
530 | 0 | num = *((double *) val); |
531 | 0 | text_len = pr_snprintf(buf, sizeof(buf)-1, "%d", num); |
532 | 0 | buf[text_len] = '\0'; |
533 | 0 | text = buf; |
534 | 0 | break; |
535 | 0 | } |
536 | | |
537 | 0 | case LOGFMT_META_UID: { |
538 | 0 | uid_t uid; |
539 | |
|
540 | 0 | uid = *((double *) val); |
541 | 0 | text = pr_uid2str(p, uid); |
542 | 0 | break; |
543 | 0 | } |
544 | | |
545 | 0 | case LOGFMT_META_GID: { |
546 | 0 | gid_t gid; |
547 | |
|
548 | 0 | gid = *((double *) val); |
549 | 0 | text = pr_gid2str(p, gid); |
550 | 0 | break; |
551 | 0 | } |
552 | | |
553 | 0 | case LOGFMT_META_BYTES_SENT: |
554 | 0 | case LOGFMT_META_FILE_OFFSET: |
555 | 0 | case LOGFMT_META_FILE_SIZE: |
556 | 0 | case LOGFMT_META_RAW_BYTES_IN: |
557 | 0 | case LOGFMT_META_RAW_BYTES_OUT: |
558 | 0 | case LOGFMT_META_RESPONSE_MS: |
559 | 0 | case LOGFMT_META_XFER_MS: { |
560 | 0 | off_t num; |
561 | |
|
562 | 0 | num = *((double *) val); |
563 | 0 | text_len = pr_snprintf(buf, sizeof(buf)-1, "%" PR_LU, (pr_off_t) num); |
564 | 0 | buf[text_len] = '\0'; |
565 | 0 | text = buf; |
566 | 0 | break; |
567 | 0 | } |
568 | | |
569 | 0 | case LOGFMT_META_EPOCH: |
570 | 0 | case LOGFMT_META_PID: { |
571 | 0 | unsigned long num; |
572 | |
|
573 | 0 | num = *((double *) val); |
574 | 0 | text_len = pr_snprintf(buf, sizeof(buf)-1, "%lu", num); |
575 | 0 | buf[text_len] = '\0'; |
576 | 0 | text = buf; |
577 | 0 | break; |
578 | 0 | } |
579 | | |
580 | 0 | case LOGFMT_META_FILE_MODIFIED: { |
581 | 0 | int truth; |
582 | |
|
583 | 0 | truth = *((int *) val); |
584 | 0 | text = truth ? "true" : "false"; |
585 | 0 | break; |
586 | 0 | } |
587 | | |
588 | 0 | case LOGFMT_META_SECONDS: { |
589 | 0 | float num; |
590 | |
|
591 | 0 | num = *((double *) val); |
592 | 0 | text_len = pr_snprintf(buf, sizeof(buf)-1, "%0.3f", num); |
593 | 0 | buf[text_len] = '\0'; |
594 | 0 | text = buf; |
595 | 0 | break; |
596 | 0 | } |
597 | | |
598 | | /* mod_log has a different implementation of META_TIME than the Jot |
599 | | * API. Thus we do it ourselves here. |
600 | | */ |
601 | 0 | case LOGFMT_META_TIME: { |
602 | 0 | char sign, *time_fmt = "[%d/%b/%Y:%H:%M:%S "; |
603 | 0 | struct tm t; |
604 | 0 | int internal_fmt = TRUE, with_tz = FALSE; |
605 | |
|
606 | 0 | if (jot_hint != NULL) { |
607 | 0 | time_fmt = (char *) jot_hint; |
608 | 0 | internal_fmt = FALSE; |
609 | 0 | } |
610 | |
|
611 | 0 | t = *get_gmtoff(p, &with_tz); |
612 | 0 | sign = (with_tz < 0 ? '-' : '+'); |
613 | 0 | if (with_tz < 0) { |
614 | 0 | with_tz = -with_tz; |
615 | 0 | } |
616 | |
|
617 | 0 | if (time_fmt != NULL) { |
618 | 0 | memset(buf, '\0', sizeof(buf)); |
619 | 0 | text_len = strftime(buf, sizeof(buf) - 1, time_fmt, &t); |
620 | 0 | if (internal_fmt == TRUE) { |
621 | 0 | if (text_len < sizeof(buf)) { |
622 | 0 | text_len += pr_snprintf(buf + text_len, |
623 | 0 | sizeof(buf) - text_len - 1, "%c%.2d%.2d]", sign, |
624 | 0 | (with_tz / 60), (with_tz % 60)); |
625 | 0 | } |
626 | 0 | } |
627 | |
|
628 | 0 | text = buf; |
629 | 0 | } |
630 | |
|
631 | 0 | break; |
632 | 0 | } |
633 | | |
634 | 0 | case LOGFMT_META_ANON_PASS: |
635 | 0 | case LOGFMT_META_BASENAME: |
636 | 0 | case LOGFMT_META_CLASS: |
637 | 0 | case LOGFMT_META_CMD_PARAMS: |
638 | 0 | case LOGFMT_META_COMMAND: |
639 | 0 | case LOGFMT_META_DIR_NAME: |
640 | 0 | case LOGFMT_META_DIR_PATH: |
641 | 0 | case LOGFMT_META_ENV_VAR: |
642 | 0 | case LOGFMT_META_EOS_REASON: |
643 | 0 | case LOGFMT_META_FILENAME: |
644 | 0 | case LOGFMT_META_GROUP: |
645 | 0 | case LOGFMT_META_IDENT_USER: |
646 | 0 | case LOGFMT_META_ISO8601: |
647 | 0 | case LOGFMT_META_LOCAL_FQDN: |
648 | 0 | case LOGFMT_META_LOCAL_IP: |
649 | 0 | case LOGFMT_META_LOCAL_NAME: |
650 | 0 | case LOGFMT_META_METHOD: |
651 | 0 | case LOGFMT_META_NOTE_VAR: |
652 | 0 | case LOGFMT_META_ORIGINAL_USER: |
653 | 0 | case LOGFMT_META_PROTOCOL: |
654 | 0 | case LOGFMT_META_REMOTE_HOST: |
655 | 0 | case LOGFMT_META_REMOTE_IP: |
656 | 0 | case LOGFMT_META_RENAME_FROM: |
657 | 0 | case LOGFMT_META_RESPONSE_STR: |
658 | 0 | case LOGFMT_META_USER: |
659 | 0 | case LOGFMT_META_VERSION: |
660 | 0 | case LOGFMT_META_VHOST_IP: |
661 | 0 | case LOGFMT_META_XFER_FAILURE: |
662 | 0 | case LOGFMT_META_XFER_PATH: |
663 | 0 | case LOGFMT_META_XFER_SPEED: |
664 | 0 | case LOGFMT_META_XFER_STATUS: |
665 | 0 | case LOGFMT_META_XFER_TYPE: |
666 | 0 | default: |
667 | 0 | text = val; |
668 | 0 | break; |
669 | 0 | } |
670 | | |
671 | 0 | if (text != NULL && |
672 | 0 | text_len == 0) { |
673 | 0 | text_len = strlen(text); |
674 | 0 | } |
675 | |
|
676 | 0 | extlog_buffer_append(log, text, text_len); |
677 | 0 | } |
678 | | |
679 | 0 | return 0; |
680 | 0 | } |
681 | | |
682 | | static int resolve_on_default(pool *p, pr_jot_ctx_t *jot_ctx, |
683 | 0 | unsigned char logfmt_id) { |
684 | 0 | struct extlog_buffer *log; |
685 | |
|
686 | 0 | log = jot_ctx->log; |
687 | 0 | if (log->buflen > 0) { |
688 | 0 | const char *text = NULL; |
689 | 0 | size_t text_len = 0; |
690 | |
|
691 | 0 | switch (logfmt_id) { |
692 | 0 | case LOGFMT_META_ANON_PASS: |
693 | 0 | case LOGFMT_META_IDENT_USER: |
694 | 0 | text = "UNKNOWN"; |
695 | 0 | text_len = strlen(text); |
696 | 0 | break; |
697 | | |
698 | 0 | case LOGFMT_META_BASENAME: |
699 | 0 | case LOGFMT_META_BYTES_SENT: |
700 | 0 | case LOGFMT_META_CLASS: |
701 | 0 | case LOGFMT_META_FILENAME: |
702 | 0 | case LOGFMT_META_FILE_OFFSET: |
703 | 0 | case LOGFMT_META_FILE_SIZE: |
704 | 0 | case LOGFMT_META_GROUP: |
705 | 0 | case LOGFMT_META_ORIGINAL_USER: |
706 | 0 | case LOGFMT_META_RENAME_FROM: |
707 | 0 | case LOGFMT_META_RESPONSE_CODE: |
708 | 0 | case LOGFMT_META_RESPONSE_MS: |
709 | 0 | case LOGFMT_META_RESPONSE_STR: |
710 | 0 | case LOGFMT_META_SECONDS: |
711 | 0 | case LOGFMT_META_USER: |
712 | 0 | case LOGFMT_META_XFER_FAILURE: |
713 | 0 | case LOGFMT_META_XFER_MS: |
714 | 0 | case LOGFMT_META_XFER_PATH: |
715 | 0 | case LOGFMT_META_XFER_PORT: |
716 | 0 | case LOGFMT_META_XFER_SPEED: |
717 | 0 | case LOGFMT_META_XFER_STATUS: |
718 | 0 | case LOGFMT_META_XFER_TYPE: |
719 | 0 | text = "-"; |
720 | 0 | text_len = 1; |
721 | 0 | break; |
722 | | |
723 | | /* These explicitly do NOT have default values. */ |
724 | 0 | case LOGFMT_META_CMD_PARAMS: |
725 | 0 | case LOGFMT_META_COMMAND: |
726 | 0 | case LOGFMT_META_DIR_NAME: |
727 | 0 | case LOGFMT_META_DIR_PATH: |
728 | 0 | case LOGFMT_META_ENV_VAR: |
729 | 0 | case LOGFMT_META_EOS_REASON: |
730 | 0 | case LOGFMT_META_NOTE_VAR: |
731 | 0 | case LOGFMT_META_METHOD: |
732 | 0 | default: |
733 | 0 | break; |
734 | 0 | } |
735 | | |
736 | 0 | extlog_buffer_append(log, text, text_len); |
737 | 0 | } |
738 | | |
739 | 0 | return 0; |
740 | 0 | } |
741 | | |
742 | | static int resolve_on_other(pool *p, pr_jot_ctx_t *jot_ctx, |
743 | 0 | unsigned char *text, size_t text_len) { |
744 | 0 | struct extlog_buffer *log; |
745 | |
|
746 | 0 | log = jot_ctx->log; |
747 | 0 | extlog_buffer_append(log, (const char *) text, text_len); |
748 | 0 | return 0; |
749 | 0 | } |
750 | | |
751 | | /* from src/log.c */ |
752 | | extern int syslog_sockfd; |
753 | | |
754 | 0 | static void log_event(cmd_rec *cmd, logfile_t *lf) { |
755 | 0 | int res; |
756 | 0 | unsigned char *f = NULL; |
757 | 0 | char logbuf[EXTENDED_LOG_BUFFER_SIZE] = {'\0'}; |
758 | 0 | logformat_t *fmt = NULL; |
759 | 0 | size_t logbuflen; |
760 | 0 | pool *tmp_pool; |
761 | 0 | pr_jot_ctx_t *jot_ctx; |
762 | 0 | struct extlog_buffer *log; |
763 | |
|
764 | 0 | fmt = lf->lf_format; |
765 | 0 | f = fmt->lf_format; |
766 | |
|
767 | 0 | tmp_pool = make_sub_pool(cmd->tmp_pool); |
768 | 0 | jot_ctx = pcalloc(tmp_pool, sizeof(pr_jot_ctx_t)); |
769 | 0 | log = pcalloc(tmp_pool, sizeof(struct extlog_buffer)); |
770 | 0 | log->bufsz = log->buflen = sizeof(logbuf) - 1; |
771 | 0 | log->ptr = log->buf = logbuf; |
772 | |
|
773 | 0 | jot_ctx->log = log; |
774 | |
|
775 | 0 | res = pr_jot_resolve_logfmt(tmp_pool, cmd, lf->lf_jot_filters, f, jot_ctx, |
776 | 0 | resolve_on_meta, resolve_on_default, resolve_on_other); |
777 | 0 | if (res < 0) { |
778 | | /* EPERM indicates that the event was filtered, thus is not necessarily |
779 | | * an unexpected condition. |
780 | | */ |
781 | 0 | if (errno != EPERM) { |
782 | 0 | pr_log_pri(PR_LOG_NOTICE, MOD_LOG_VERSION |
783 | 0 | ": error formatting ExtendedLog message: %s", strerror(errno)); |
784 | 0 | } |
785 | |
|
786 | 0 | destroy_pool(tmp_pool); |
787 | 0 | return; |
788 | 0 | } |
789 | | |
790 | 0 | extlog_buffer_append(log, "\n", 1); |
791 | 0 | logbuflen = (log->bufsz - log->buflen); |
792 | |
|
793 | 0 | if (lf->lf_fd != EXTENDED_LOG_SYSLOG) { |
794 | 0 | pr_log_event_generate(PR_LOG_TYPE_EXTLOG, lf->lf_fd, -1, logbuf, logbuflen); |
795 | | |
796 | | /* What about short writes? */ |
797 | 0 | if (write(lf->lf_fd, logbuf, logbuflen) < 0) { |
798 | 0 | pr_log_pri(PR_LOG_ALERT, "error: cannot write ExtendedLog '%s': %s", |
799 | 0 | lf->lf_filename, strerror(errno)); |
800 | 0 | } |
801 | |
|
802 | 0 | } else { |
803 | 0 | pr_log_event_generate(PR_LOG_TYPE_EXTLOG, syslog_sockfd, |
804 | 0 | lf->lf_syslog_level, logbuf, logbuflen); |
805 | 0 | pr_syslog(syslog_sockfd, lf->lf_syslog_level, "%s", logbuf); |
806 | 0 | } |
807 | |
|
808 | 0 | destroy_pool(tmp_pool); |
809 | 0 | } |
810 | | |
811 | 0 | MODRET log_any(cmd_rec *cmd) { |
812 | 0 | logfile_t *lf = NULL; |
813 | | |
814 | | /* If not in anon mode, only handle logs for main servers */ |
815 | 0 | for (lf = logs; lf; lf = lf->next) { |
816 | 0 | pr_signals_handle(); |
817 | | |
818 | | /* Skip any unopened files (obviously); make sure that special fd |
819 | | * for syslog is NOT skipped, though. |
820 | | */ |
821 | 0 | if (lf->lf_fd < 0 && |
822 | 0 | lf->lf_fd != EXTENDED_LOG_SYSLOG) { |
823 | 0 | continue; |
824 | 0 | } |
825 | | |
826 | | /* If this is not an <Anonymous> section, and this IS an <Anonymous> |
827 | | * ExtendedLog, skip it. |
828 | | */ |
829 | 0 | if (session.anon_config == NULL && |
830 | 0 | lf->lf_conf != NULL && |
831 | 0 | lf->lf_conf->config_type == CONF_ANON) { |
832 | 0 | continue; |
833 | 0 | } |
834 | | |
835 | 0 | log_event(cmd, lf); |
836 | 0 | } |
837 | |
|
838 | 0 | return PR_DECLINED(cmd); |
839 | 0 | } |
840 | | |
841 | | /* Event handlers |
842 | | */ |
843 | | |
844 | 0 | static void log_exit_ev(const void *event_data, void *user_data) { |
845 | 0 | pool *tmp_pool; |
846 | 0 | cmd_rec *cmd; |
847 | 0 | int responses_blocked; |
848 | |
|
849 | 0 | tmp_pool = make_sub_pool(session.pool); |
850 | 0 | cmd = pr_cmd_alloc(tmp_pool, 1, pstrdup(tmp_pool, "EXIT")); |
851 | 0 | cmd->cmd_class |= CL_DISCONNECT; |
852 | |
|
853 | 0 | responses_blocked = pr_response_blocked(); |
854 | 0 | if (responses_blocked == FALSE) { |
855 | 0 | (void) pr_response_block(TRUE); |
856 | 0 | } |
857 | |
|
858 | 0 | (void) pr_cmd_dispatch_phase(cmd, LOG_CMD, |
859 | 0 | PR_CMD_DISPATCH_FL_CLEAR_RESPONSE); |
860 | |
|
861 | 0 | pr_response_block(responses_blocked); |
862 | 0 | destroy_pool(tmp_pool); |
863 | 0 | } |
864 | | |
865 | 0 | static void log_postparse_ev(const void *event_data, void *user_data) { |
866 | 0 | config_rec *c; |
867 | |
|
868 | 0 | c = find_config(main_server->conf, CONF_PARAM, "SystemLog", FALSE); |
869 | 0 | if (c != NULL) { |
870 | 0 | char *path; |
871 | |
|
872 | 0 | path = c->argv[0]; |
873 | 0 | log_closesyslog(); |
874 | |
|
875 | 0 | if (strcasecmp(path, "none") != 0) { |
876 | 0 | int res, xerrno; |
877 | |
|
878 | 0 | path = dir_canonical_path(main_server->pool, path); |
879 | |
|
880 | 0 | pr_signals_block(); |
881 | 0 | PRIVS_ROOT |
882 | 0 | res = log_opensyslog(path); |
883 | 0 | xerrno = errno; |
884 | 0 | PRIVS_RELINQUISH |
885 | 0 | pr_signals_unblock(); |
886 | |
|
887 | 0 | if (res < 0) { |
888 | 0 | if (res == PR_LOG_WRITABLE_DIR) { |
889 | 0 | pr_log_pri(PR_LOG_ERR, |
890 | 0 | "unable to open SystemLog '%s': %s is a world-writable directory", |
891 | 0 | path, path); |
892 | |
|
893 | 0 | } else if (res == PR_LOG_SYMLINK) { |
894 | 0 | pr_log_pri(PR_LOG_ERR, |
895 | 0 | "unable to open SystemLog '%s': %s is a symbolic link", path, path); |
896 | |
|
897 | 0 | } else { |
898 | 0 | if (xerrno != ENXIO) { |
899 | 0 | pr_log_pri(PR_LOG_ERR, |
900 | 0 | "unable to open SystemLog '%s': %s", path, strerror(xerrno)); |
901 | |
|
902 | 0 | } else { |
903 | 0 | pr_log_pri(PR_LOG_ERR, |
904 | 0 | "unable to open SystemLog '%s': " |
905 | 0 | "FIFO reader process must be running first", path); |
906 | 0 | } |
907 | 0 | } |
908 | |
|
909 | 0 | exit(1); |
910 | 0 | } |
911 | |
|
912 | 0 | } else { |
913 | 0 | log_discard(); |
914 | 0 | } |
915 | 0 | } |
916 | 0 | } |
917 | | |
918 | 0 | static void log_restart_ev(const void *event_data, void *user_data) { |
919 | 0 | destroy_pool(log_pool); |
920 | |
|
921 | 0 | formats = NULL; |
922 | 0 | format_set = NULL; |
923 | 0 | logs = NULL; |
924 | 0 | log_set = NULL; |
925 | |
|
926 | 0 | log_pool = make_sub_pool(permanent_pool); |
927 | 0 | pr_pool_tag(log_pool, "mod_log pool"); |
928 | |
|
929 | 0 | parse_logformat(NULL, "", "%h %l %u %t \"%r\" %s %b"); |
930 | 0 | } |
931 | | |
932 | 0 | static void log_sess_reinit_ev(const void *event_data, void *user_data) { |
933 | 0 | int res; |
934 | 0 | logfile_t *lf = NULL; |
935 | | |
936 | | /* A HOST command changed the main_server pointer, reinitialize ourselves. */ |
937 | |
|
938 | 0 | pr_event_unregister(&log_module, "core.exit", log_exit_ev); |
939 | 0 | pr_event_unregister(&log_module, "core.session-reinit", log_sess_reinit_ev); |
940 | 0 | pr_event_unregister(&log_module, "core.timeout-stalled", log_xfer_stalled_ev); |
941 | | |
942 | | /* XXX If ServerLog configured, close/reopen syslog? */ |
943 | | |
944 | | /* Close all ExtendedLog files, to prevent duplicate fds. */ |
945 | 0 | for (lf = logs; lf; lf = lf->next) { |
946 | 0 | if (lf->lf_fd > -1) { |
947 | | /* No need to close the special EXTENDED_LOG_SYSLOG (i.e. fake) fd. */ |
948 | 0 | if (lf->lf_fd != EXTENDED_LOG_SYSLOG) { |
949 | 0 | (void) close(lf->lf_fd); |
950 | 0 | } |
951 | |
|
952 | 0 | lf->lf_fd = -1; |
953 | 0 | } |
954 | 0 | } |
955 | | |
956 | | /* Restore original LogOptions settings. */ |
957 | 0 | (void) pr_log_set_options(PR_LOG_OPT_DEFAULT); |
958 | |
|
959 | 0 | res = log_sess_init(); |
960 | 0 | if (res < 0) { |
961 | 0 | pr_session_disconnect(&log_module, |
962 | 0 | PR_SESS_DISCONNECT_SESSION_INIT_FAILED, NULL); |
963 | 0 | } |
964 | 0 | } |
965 | | |
966 | 0 | static void log_xfer_stalled_ev(const void *event_data, void *user_data) { |
967 | 0 | if (session.curr_cmd_rec != NULL) { |
968 | | /* Automatically dispatch the current command, at the LOG_CMD_ERR phase, |
969 | | * so that the ExtendedLog entry for the command gets written out. This |
970 | | * should handle any LIST/MLSD/NLST commands as well (Bug#3696). |
971 | | */ |
972 | 0 | (void) log_any(session.curr_cmd_rec); |
973 | 0 | } |
974 | 0 | } |
975 | | |
976 | | /* Initialization handlers |
977 | | */ |
978 | | |
979 | 0 | static int log_init(void) { |
980 | 0 | log_pool = make_sub_pool(permanent_pool); |
981 | 0 | pr_pool_tag(log_pool, "mod_log pool"); |
982 | | |
983 | | /* Add the "default" extendedlog format */ |
984 | 0 | parse_logformat(NULL, "", "%h %l %u %t \"%r\" %s %b"); |
985 | |
|
986 | 0 | pr_event_register(&log_module, "core.postparse", log_postparse_ev, NULL); |
987 | 0 | pr_event_register(&log_module, "core.restart", log_restart_ev, NULL); |
988 | |
|
989 | 0 | return 0; |
990 | 0 | } |
991 | | |
992 | 0 | static void find_extendedlogs(void) { |
993 | 0 | config_rec *c; |
994 | 0 | char *logfname, *logfmt_name = NULL; |
995 | 0 | logformat_t *logfmt; |
996 | 0 | logfile_t *extlog = NULL; |
997 | 0 | unsigned long config_flags = (PR_CONFIG_FIND_FL_SKIP_DIR|PR_CONFIG_FIND_FL_SKIP_LIMIT|PR_CONFIG_FIND_FL_SKIP_DYNDIR); |
998 | | |
999 | | /* We DO actually want the recursion here. The reason is that we want |
1000 | | * to find ALL_ ExtendedLog directives in the configuration, including |
1001 | | * those in <Anonymous> sections. We have the ability to use root privs |
1002 | | * now, to make sure these files can be opened, but after the user has |
1003 | | * authenticated (and we know for sure whether they're anonymous or not), |
1004 | | * root privs may be permanently revoked. |
1005 | | * |
1006 | | * We mitigate the cost of the recursive search (especially for configs |
1007 | | * with thousands of <Directory>/<Limit> sections) by specifying the |
1008 | | * find_config() flags to skip those sections; we are only interested |
1009 | | * in the top-level (CONF_ROOT, CONF_VIRTUAL) and <Anonymous> sections. |
1010 | | */ |
1011 | |
|
1012 | 0 | c = find_config2(main_server->conf, CONF_PARAM, "ExtendedLog", TRUE, |
1013 | 0 | config_flags); |
1014 | 0 | while (c != NULL) { |
1015 | 0 | pr_jot_filters_t *jot_filters = NULL; |
1016 | |
|
1017 | 0 | pr_signals_handle(); |
1018 | |
|
1019 | 0 | logfname = c->argv[0]; |
1020 | 0 | logfmt_name = NULL; |
1021 | |
|
1022 | 0 | if (c->argc > 1) { |
1023 | 0 | jot_filters = c->argv[1]; |
1024 | |
|
1025 | 0 | if (c->argc > 2) { |
1026 | 0 | if (c->argv[2] != NULL) { |
1027 | 0 | logfmt_name = c->argv[2]; |
1028 | 0 | } |
1029 | 0 | } |
1030 | 0 | } |
1031 | | |
1032 | | /* No logging for this round. If, however, this was found in an |
1033 | | * <Anonymous> section, add a logfile entry for it anyway; the anonymous |
1034 | | * directive might be trying to override a higher-level config; see |
1035 | | * Bug#1908. |
1036 | | */ |
1037 | 0 | if (c->parent != NULL && |
1038 | 0 | c->parent->config_type != CONF_ANON) { |
1039 | 0 | goto loop_extendedlogs; |
1040 | 0 | } |
1041 | | |
1042 | 0 | if (logfmt_name != NULL) { |
1043 | | /* Search for the format name */ |
1044 | 0 | for (logfmt = formats; logfmt; logfmt = logfmt->next) { |
1045 | 0 | if (strcmp(logfmt->lf_fmt_name, logfmt_name) == 0) { |
1046 | 0 | break; |
1047 | 0 | } |
1048 | 0 | } |
1049 | |
|
1050 | 0 | if (logfmt == NULL) { |
1051 | 0 | if (strcasecmp(logfmt_name, EXTENDED_LOG_FORMAT_DEFAULT) == 0) { |
1052 | | /* Try again, this time looking for the default LogFormat |
1053 | | * name, which is registered using a name of "". |
1054 | | */ |
1055 | 0 | for (logfmt = formats; logfmt; logfmt = logfmt->next) { |
1056 | 0 | if (strcmp(logfmt->lf_fmt_name, "") == 0) { |
1057 | 0 | break; |
1058 | 0 | } |
1059 | 0 | } |
1060 | 0 | } |
1061 | 0 | } |
1062 | |
|
1063 | 0 | if (logfmt == NULL) { |
1064 | 0 | pr_log_pri(PR_LOG_NOTICE, |
1065 | 0 | "ExtendedLog '%s' uses unknown format name '%s'", logfname, |
1066 | 0 | logfmt_name); |
1067 | 0 | goto loop_extendedlogs; |
1068 | 0 | } |
1069 | |
|
1070 | 0 | } else { |
1071 | 0 | logfmt = formats; |
1072 | 0 | } |
1073 | | |
1074 | 0 | extlog = (logfile_t *) pcalloc(session.pool, sizeof(logfile_t)); |
1075 | |
|
1076 | 0 | extlog->lf_filename = pstrdup(session.pool, logfname); |
1077 | 0 | extlog->lf_fd = -1; |
1078 | 0 | extlog->lf_syslog_level = -1; |
1079 | 0 | extlog->lf_jot_filters = jot_filters; |
1080 | 0 | extlog->lf_format = logfmt; |
1081 | 0 | extlog->lf_conf = c->parent; |
1082 | 0 | if (log_set == NULL) { |
1083 | 0 | log_set = xaset_create(session.pool, NULL); |
1084 | 0 | } |
1085 | |
|
1086 | 0 | xaset_insert(log_set, (xasetmember_t *) extlog); |
1087 | 0 | logs = (logfile_t *) log_set->xas_list; |
1088 | |
|
1089 | 0 | loop_extendedlogs: |
1090 | 0 | c = find_config_next2(c, c->next, CONF_PARAM, "ExtendedLog", TRUE, |
1091 | 0 | config_flags); |
1092 | 0 | } |
1093 | 0 | } |
1094 | | |
1095 | 0 | MODRET log_pre_dele(cmd_rec *cmd) { |
1096 | 0 | char *path; |
1097 | |
|
1098 | 0 | jot_set_deleted_filesz(0); |
1099 | |
|
1100 | 0 | path = dir_canonical_path(cmd->tmp_pool, |
1101 | 0 | pr_fs_decode_path(cmd->tmp_pool, cmd->arg)); |
1102 | 0 | if (path != NULL) { |
1103 | 0 | struct stat st; |
1104 | | |
1105 | | /* Briefly cache the size of the file being deleted, so that it can be |
1106 | | * logged properly using %b. |
1107 | | */ |
1108 | 0 | pr_fs_clear_cache2(path); |
1109 | 0 | if (pr_fsio_stat(path, &st) == 0) { |
1110 | 0 | jot_set_deleted_filesz(st.st_size); |
1111 | 0 | } |
1112 | 0 | } |
1113 | |
|
1114 | 0 | return PR_DECLINED(cmd); |
1115 | 0 | } |
1116 | | |
1117 | 0 | MODRET log_post_pass(cmd_rec *cmd) { |
1118 | 0 | logfile_t *lf; |
1119 | | |
1120 | | /* Authentication is complete, if we aren't in anon-mode, close |
1121 | | * all extendedlogs opened inside <Anonymous> blocks. |
1122 | | */ |
1123 | 0 | if (!session.anon_config) { |
1124 | 0 | for (lf = logs; lf; lf = lf->next) { |
1125 | 0 | if (lf->lf_fd != -1 && |
1126 | 0 | lf->lf_fd != EXTENDED_LOG_SYSLOG && |
1127 | 0 | lf->lf_conf && |
1128 | 0 | lf->lf_conf->config_type == CONF_ANON) { |
1129 | 0 | pr_log_debug(DEBUG7, "mod_log: closing ExtendedLog '%s' (fd %d)", |
1130 | 0 | lf->lf_filename, lf->lf_fd); |
1131 | 0 | (void) close(lf->lf_fd); |
1132 | 0 | lf->lf_fd = -1; |
1133 | 0 | } |
1134 | 0 | } |
1135 | |
|
1136 | 0 | } else { |
1137 | | /* Close all logs which were opened inside a _different_ anonymous |
1138 | | * context. |
1139 | | */ |
1140 | 0 | for (lf = logs; lf; lf = lf->next) { |
1141 | 0 | if (lf->lf_fd != -1 && |
1142 | 0 | lf->lf_fd != EXTENDED_LOG_SYSLOG && |
1143 | 0 | lf->lf_conf && |
1144 | 0 | lf->lf_conf != session.anon_config) { |
1145 | 0 | pr_log_debug(DEBUG7, "mod_log: closing ExtendedLog '%s' (fd %d)", |
1146 | 0 | lf->lf_filename, lf->lf_fd); |
1147 | 0 | (void) close(lf->lf_fd); |
1148 | 0 | lf->lf_fd = -1; |
1149 | 0 | } |
1150 | 0 | } |
1151 | | |
1152 | | /* If any ExtendedLogs set inside our context match an outer log, |
1153 | | * close the outer (this allows overriding inside <Anonymous>). |
1154 | | */ |
1155 | 0 | for (lf = logs; lf; lf = lf->next) { |
1156 | 0 | if (lf->lf_conf && |
1157 | 0 | lf->lf_conf == session.anon_config) { |
1158 | | /* This should "override" any lower-level extendedlog with the |
1159 | | * same filename. |
1160 | | */ |
1161 | 0 | logfile_t *lfi = NULL; |
1162 | |
|
1163 | 0 | for (lfi = logs; lfi; lfi = lfi->next) { |
1164 | 0 | if (lfi->lf_fd != -1 && |
1165 | 0 | lfi->lf_fd != EXTENDED_LOG_SYSLOG && |
1166 | 0 | !lfi->lf_conf && |
1167 | 0 | strcmp(lfi->lf_filename, lf->lf_filename) == 0) { |
1168 | 0 | pr_log_debug(DEBUG7, "mod_log: closing ExtendedLog '%s' (fd %d)", |
1169 | 0 | lf->lf_filename, lfi->lf_fd); |
1170 | 0 | (void) close(lfi->lf_fd); |
1171 | 0 | lfi->lf_fd = -1; |
1172 | 0 | } |
1173 | 0 | } |
1174 | | |
1175 | | /* Go ahead and close the log if it's CL_NONE */ |
1176 | 0 | if (lf->lf_fd != -1 && |
1177 | 0 | lf->lf_fd != EXTENDED_LOG_SYSLOG && |
1178 | 0 | pr_jot_filters_include_classes(lf->lf_jot_filters, CL_NONE) == TRUE) { |
1179 | 0 | (void) close(lf->lf_fd); |
1180 | 0 | lf->lf_fd = -1; |
1181 | 0 | } |
1182 | 0 | } |
1183 | 0 | } |
1184 | 0 | } |
1185 | |
|
1186 | 0 | return PR_DECLINED(cmd); |
1187 | 0 | } |
1188 | | |
1189 | | /* Open all the log files */ |
1190 | | static int dispatched_connect = FALSE; |
1191 | | |
1192 | 0 | static int log_sess_init(void) { |
1193 | 0 | config_rec *c; |
1194 | 0 | char *serverlog_name = NULL; |
1195 | 0 | logfile_t *lf = NULL; |
1196 | |
|
1197 | 0 | pr_event_register(&log_module, "core.session-reinit", log_sess_reinit_ev, |
1198 | 0 | NULL); |
1199 | |
|
1200 | 0 | c = find_config(main_server->conf, CONF_PARAM, "LogOptions", FALSE); |
1201 | 0 | if (c != NULL) { |
1202 | 0 | unsigned long log_opts; |
1203 | |
|
1204 | 0 | log_opts = *((unsigned long *) c->argv[0]); |
1205 | 0 | if (pr_log_set_options(log_opts) < 0) { |
1206 | 0 | pr_log_debug(DEBUG6, "%s: error setting LogOptions (%lu): %s", |
1207 | 0 | c->name, log_opts, strerror(errno)); |
1208 | 0 | } |
1209 | 0 | } |
1210 | | |
1211 | | /* Open the ServerLog, if present. */ |
1212 | 0 | serverlog_name = get_param_ptr(main_server->conf, "ServerLog", FALSE); |
1213 | 0 | if (serverlog_name != NULL) { |
1214 | 0 | log_closesyslog(); |
1215 | |
|
1216 | 0 | if (strncasecmp(serverlog_name, "none", 5) != 0) { |
1217 | 0 | int res, xerrno; |
1218 | |
|
1219 | 0 | PRIVS_ROOT |
1220 | 0 | res = log_opensyslog(serverlog_name); |
1221 | 0 | xerrno = errno; |
1222 | 0 | PRIVS_RELINQUISH |
1223 | |
|
1224 | 0 | if (res < 0) { |
1225 | 0 | if (xerrno != ENXIO) { |
1226 | 0 | pr_log_debug(DEBUG4, "unable to open ServerLog '%s': %s", |
1227 | 0 | serverlog_name, strerror(xerrno)); |
1228 | |
|
1229 | 0 | } else { |
1230 | 0 | pr_log_debug(DEBUG4, |
1231 | 0 | "unable to open ServerLog '%s': " |
1232 | 0 | "FIFO reader process must be running first", serverlog_name); |
1233 | 0 | } |
1234 | 0 | } |
1235 | 0 | } |
1236 | |
|
1237 | 0 | } else { |
1238 | 0 | c = find_config(main_server->conf, CONF_PARAM, "SystemLog", FALSE); |
1239 | 0 | if (c != NULL) { |
1240 | 0 | char *path; |
1241 | |
|
1242 | 0 | path = c->argv[0]; |
1243 | 0 | log_closesyslog(); |
1244 | |
|
1245 | 0 | if (strcasecmp(path, "none") != 0) { |
1246 | 0 | int res, xerrno; |
1247 | |
|
1248 | 0 | path = dir_canonical_path(main_server->pool, path); |
1249 | |
|
1250 | 0 | pr_signals_block(); |
1251 | 0 | PRIVS_ROOT |
1252 | 0 | res = log_opensyslog(path); |
1253 | 0 | xerrno = errno; |
1254 | 0 | PRIVS_RELINQUISH |
1255 | 0 | pr_signals_unblock(); |
1256 | |
|
1257 | 0 | if (res < 0) { |
1258 | 0 | if (res == PR_LOG_WRITABLE_DIR) { |
1259 | 0 | pr_log_pri(PR_LOG_ERR, |
1260 | 0 | "unable to open SystemLog '%s': %s is a world-writable directory", |
1261 | 0 | path, path); |
1262 | |
|
1263 | 0 | } else if (res == PR_LOG_SYMLINK) { |
1264 | 0 | pr_log_pri(PR_LOG_ERR, |
1265 | 0 | "unable to open SystemLog '%s': %s is a symbolic link", path, |
1266 | 0 | path); |
1267 | |
|
1268 | 0 | } else { |
1269 | 0 | if (xerrno != ENXIO) { |
1270 | 0 | pr_log_pri(PR_LOG_ERR, |
1271 | 0 | "unable to open SystemLog '%s': %s", path, strerror(xerrno)); |
1272 | |
|
1273 | 0 | } else { |
1274 | 0 | pr_log_pri(PR_LOG_ERR, |
1275 | 0 | "unable to open SystemLog '%s': " |
1276 | 0 | "FIFO reader process must be running first", path); |
1277 | 0 | } |
1278 | 0 | } |
1279 | 0 | } |
1280 | |
|
1281 | 0 | } else { |
1282 | 0 | log_discard(); |
1283 | 0 | } |
1284 | 0 | } |
1285 | 0 | } |
1286 | | |
1287 | | /* Open all the ExtendedLog files. */ |
1288 | 0 | find_extendedlogs(); |
1289 | |
|
1290 | 0 | for (lf = logs; lf; lf = lf->next) { |
1291 | 0 | if (lf->lf_fd == -1) { |
1292 | | |
1293 | | /* Is this ExtendedLog to be written to a file, or to syslog? */ |
1294 | 0 | if (strncasecmp(lf->lf_filename, "syslog:", 7) != 0) { |
1295 | 0 | int res = 0, xerrno; |
1296 | |
|
1297 | 0 | pr_log_debug(DEBUG7, "mod_log: opening ExtendedLog '%s'", |
1298 | 0 | lf->lf_filename); |
1299 | |
|
1300 | 0 | pr_signals_block(); |
1301 | 0 | PRIVS_ROOT |
1302 | 0 | res = pr_log_openfile(lf->lf_filename, &(lf->lf_fd), EXTENDED_LOG_MODE); |
1303 | 0 | xerrno = errno; |
1304 | 0 | PRIVS_RELINQUISH |
1305 | 0 | pr_signals_unblock(); |
1306 | |
|
1307 | 0 | if (res < 0) { |
1308 | 0 | if (res == -1) { |
1309 | 0 | if (xerrno != ENXIO) { |
1310 | 0 | pr_log_pri(PR_LOG_NOTICE, "unable to open ExtendedLog '%s': %s", |
1311 | 0 | lf->lf_filename, strerror(xerrno)); |
1312 | |
|
1313 | 0 | } else { |
1314 | 0 | pr_log_pri(PR_LOG_NOTICE, "unable to open ExtendedLog '%s': " |
1315 | 0 | "FIFO reader process must be running first", lf->lf_filename); |
1316 | 0 | } |
1317 | |
|
1318 | 0 | } else if (res == PR_LOG_WRITABLE_DIR) { |
1319 | 0 | pr_log_pri(PR_LOG_WARNING, "unable to open ExtendedLog '%s': " |
1320 | 0 | "parent directory is world-writable", lf->lf_filename); |
1321 | |
|
1322 | 0 | } else if (res == PR_LOG_SYMLINK) { |
1323 | 0 | pr_log_pri(PR_LOG_WARNING, "unable to open ExtendedLog '%s': " |
1324 | 0 | "%s is a symbolic link", lf->lf_filename, lf->lf_filename); |
1325 | 0 | } |
1326 | 0 | } |
1327 | |
|
1328 | 0 | } else { |
1329 | 0 | char *tmp = strchr(lf->lf_filename, ':'); |
1330 | |
|
1331 | 0 | lf->lf_syslog_level = pr_log_str2sysloglevel(++tmp); |
1332 | 0 | lf->lf_fd = EXTENDED_LOG_SYSLOG; |
1333 | 0 | } |
1334 | 0 | } |
1335 | 0 | } |
1336 | | |
1337 | | /* Register event handlers for the session. */ |
1338 | 0 | pr_event_register(&log_module, "core.exit", log_exit_ev, NULL); |
1339 | 0 | pr_event_register(&log_module, "core.timeout-stalled", log_xfer_stalled_ev, |
1340 | 0 | NULL); |
1341 | | |
1342 | | /* Have we send our CONNECT event yet? */ |
1343 | 0 | if (dispatched_connect == FALSE) { |
1344 | 0 | pool *tmp_pool; |
1345 | 0 | cmd_rec *cmd; |
1346 | 0 | int responses_blocked; |
1347 | |
|
1348 | 0 | tmp_pool = make_sub_pool(session.pool); |
1349 | 0 | cmd = pr_cmd_alloc(tmp_pool, 1, pstrdup(tmp_pool, "CONNECT")); |
1350 | 0 | cmd->cmd_class |= CL_CONNECT; |
1351 | |
|
1352 | 0 | responses_blocked = pr_response_blocked(); |
1353 | 0 | if (responses_blocked == FALSE) { |
1354 | 0 | (void) pr_response_block(TRUE); |
1355 | 0 | } |
1356 | |
|
1357 | 0 | (void) pr_cmd_dispatch_phase(cmd, LOG_CMD, |
1358 | 0 | PR_CMD_DISPATCH_FL_CLEAR_RESPONSE); |
1359 | |
|
1360 | 0 | pr_response_block(responses_blocked); |
1361 | 0 | destroy_pool(tmp_pool); |
1362 | 0 | dispatched_connect = TRUE; |
1363 | 0 | } |
1364 | |
|
1365 | 0 | return 0; |
1366 | 0 | } |
1367 | | |
1368 | | /* Module API tables |
1369 | | */ |
1370 | | |
1371 | | static conftable log_conftab[] = { |
1372 | | { "AllowLogSymlinks", set_allowlogsymlinks, NULL }, |
1373 | | { "ExtendedLog", set_extendedlog, NULL }, |
1374 | | { "LogFormat", set_logformat, NULL }, |
1375 | | { "LogOptions", set_logoptions, NULL }, |
1376 | | { "ServerLog", set_serverlog, NULL }, |
1377 | | { "SystemLog", set_systemlog, NULL }, |
1378 | | { NULL, NULL, NULL } |
1379 | | }; |
1380 | | |
1381 | | static cmdtable log_cmdtab[] = { |
1382 | | { PRE_CMD, C_DELE, G_NONE, log_pre_dele, FALSE, FALSE }, |
1383 | | { LOG_CMD, C_ANY, G_NONE, log_any, FALSE, FALSE }, |
1384 | | { LOG_CMD_ERR, C_ANY, G_NONE, log_any, FALSE, FALSE }, |
1385 | | { POST_CMD, C_PASS, G_NONE, log_post_pass, FALSE, FALSE }, |
1386 | | { 0, NULL } |
1387 | | }; |
1388 | | |
1389 | | module log_module = { |
1390 | | NULL, NULL, |
1391 | | |
1392 | | /* Module API version */ |
1393 | | 0x20, |
1394 | | |
1395 | | /* Module name */ |
1396 | | "log", |
1397 | | |
1398 | | /* Module configuration handler table */ |
1399 | | log_conftab, |
1400 | | |
1401 | | /* Module command handler table */ |
1402 | | log_cmdtab, |
1403 | | |
1404 | | /* Module authentication handler table */ |
1405 | | NULL, |
1406 | | |
1407 | | /* Module initialization */ |
1408 | | log_init, |
1409 | | |
1410 | | /* Session initialization */ |
1411 | | log_sess_init, |
1412 | | |
1413 | | /* Module version */ |
1414 | | MOD_LOG_VERSION |
1415 | | }; |