File: logging.c

package info (click to toggle)
vsftpd 3.0.3-12
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,548 kB
  • sloc: ansic: 16,632; sh: 267; makefile: 51; python: 18
file content (384 lines) | stat: -rw-r--r-- 10,458 bytes parent folder | download | duplicates (2)
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
/*
 * Part of Very Secure FTPd
 * Licence: GPL v2
 * Author: Chris Evans
 *
 * logging.c
 */

#include "logging.h"
#include "tunables.h"
#include "utility.h"
#include "str.h"
#include "sysutil.h"
#include "sysstr.h"
#include "session.h"

/* File local functions */
static int vsf_log_type_is_transfer(enum EVSFLogEntryType type);
static void vsf_log_common(struct vsf_session* p_sess, int succeeded,
                           enum EVSFLogEntryType what,
                           const struct mystr* p_str);
static void vsf_log_do_log_vsftpd_format(struct vsf_session* p_sess,
                                         struct mystr* p_str, int succeeded,
                                         enum EVSFLogEntryType what,
                                         const struct mystr* p_log_str);
static void vsf_log_do_log_wuftpd_format(struct vsf_session* p_sess,
                                         struct mystr* p_str, int succeeded);
static void vsf_log_do_log_to_file(int fd, struct mystr* p_str);

void
vsf_log_init(struct vsf_session* p_sess)
{
  if (tunable_syslog_enable || tunable_tcp_wrappers)
  {
    vsf_sysutil_openlog(1);
  }
  if (!tunable_xferlog_enable && !tunable_dual_log_enable)
  {
    return;
  }
  if (tunable_dual_log_enable || tunable_xferlog_std_format)
  {
    int retval = -1;
    if (tunable_xferlog_file)
    {
      retval = vsf_sysutil_create_or_open_file_append(tunable_xferlog_file,
                                                      0640);
    }
    if (vsf_sysutil_retval_is_error(retval))
    {
      die2("failed to open xferlog log file:", tunable_xferlog_file);
    }
    p_sess->xferlog_fd = retval;
  }
  if (tunable_dual_log_enable || !tunable_xferlog_std_format)
  {
    if (!tunable_syslog_enable)
    {
      int retval = -1;
      if (tunable_vsftpd_log_file)
      {
        retval = vsf_sysutil_create_or_open_file_append(tunable_vsftpd_log_file,
                                                        0640);
      }
      if (vsf_sysutil_retval_is_error(retval))
      {
        die2("failed to open vsftpd log file:", tunable_vsftpd_log_file);
      }
      p_sess->vsftpd_log_fd = retval;
    }
  }
}

static int
vsf_log_type_is_transfer(enum EVSFLogEntryType type)
{
  return (type == kVSFLogEntryDownload || type == kVSFLogEntryUpload);
}

void
vsf_log_start_entry(struct vsf_session* p_sess, enum EVSFLogEntryType what)
{
  if (p_sess->log_type != 0)
  {
    bug("non null log_type in vsf_log_start_entry");
  }
  p_sess->log_type = (unsigned long) what;
  p_sess->log_start_sec = 0;
  p_sess->log_start_usec = 0;
  p_sess->transfer_size = 0;
  str_empty(&p_sess->log_str);
  if (vsf_log_type_is_transfer(what))
  {
    p_sess->log_start_sec = vsf_sysutil_get_time_sec();
    p_sess->log_start_usec = vsf_sysutil_get_time_usec();
  }
}

void
vsf_log_line(struct vsf_session* p_sess, enum EVSFLogEntryType what,
             struct mystr* p_str)
{
  vsf_log_common(p_sess, 1, what, p_str);
}

int
vsf_log_entry_pending(struct vsf_session* p_sess)
{
  if (p_sess->log_type == 0)
  {
    return 0;
  }
  return 1;
}

void
vsf_log_clear_entry(struct vsf_session* p_sess)
{
  p_sess->log_type = 0;
}

void
vsf_log_do_log(struct vsf_session* p_sess, int succeeded)
{
  vsf_log_common(p_sess, succeeded, (enum EVSFLogEntryType) p_sess->log_type,
                 &p_sess->log_str);
  p_sess->log_type = 0;
}

static void
vsf_log_common(struct vsf_session* p_sess, int succeeded,
               enum EVSFLogEntryType what, const struct mystr* p_str)
{
  static struct mystr s_log_str;
  /* Handle xferlog line if appropriate */
  if (p_sess->xferlog_fd != -1 && vsf_log_type_is_transfer(what))
  {
    vsf_log_do_log_wuftpd_format(p_sess, &s_log_str, succeeded);
    vsf_log_do_log_to_file(p_sess->xferlog_fd, &s_log_str);
  }
  /* Handle vsftpd.log line if appropriate */
  if (p_sess->vsftpd_log_fd != -1)
  {
    vsf_log_do_log_vsftpd_format(p_sess, &s_log_str, succeeded, what, p_str);
    vsf_log_do_log_to_file(p_sess->vsftpd_log_fd, &s_log_str);
  }
  /* Handle syslog() line if appropriate */
  if (tunable_syslog_enable)
  {
    int severe = 0;
    vsf_log_do_log_vsftpd_format(p_sess, &s_log_str, succeeded, what, p_str);
    if (what == kVSFLogEntryLogin && !succeeded)
    {
      severe = 1;
    }
    str_syslog(&s_log_str, severe);
  }
}

static void
vsf_log_do_log_to_file(int fd, struct mystr* p_str)
{
  if (!tunable_no_log_lock)
  {
    int retval = vsf_sysutil_lock_file_write(fd);
    if (vsf_sysutil_retval_is_error(retval))
    {
      return;
    }
  }
  str_replace_unprintable(p_str, '?');
  str_append_char(p_str, '\n');
  /* Ignore write failure; maybe the disk filled etc. */
  (void) str_write_loop(p_str, fd);
  if (!tunable_no_log_lock)
  {
    vsf_sysutil_unlock_file(fd);
  }
}

static void
vsf_log_do_log_wuftpd_format(struct vsf_session* p_sess, struct mystr* p_str,
                             int succeeded)
{
  static struct mystr s_filename_str;
  long delta_sec;
  enum EVSFLogEntryType what = (enum EVSFLogEntryType) p_sess->log_type;
  /* Date - vsf_sysutil_get_current_date updates cached time */
  str_alloc_text(p_str, vsf_sysutil_get_current_date());
  str_append_char(p_str, ' ');
  /* Transfer time (in seconds) */
  delta_sec = vsf_sysutil_get_time_sec() - p_sess->log_start_sec;
  if (delta_sec <= 0)
  {
    delta_sec = 1;
  }
  str_append_ulong(p_str, (unsigned long) delta_sec);
  str_append_char(p_str, ' ');
  /* Remote host name */
  str_append_str(p_str, &p_sess->remote_ip_str);
  str_append_char(p_str, ' ');
  /* Bytes transferred */
  str_append_filesize_t(p_str, p_sess->transfer_size);
  str_append_char(p_str, ' ');
  /* Filename */
  str_copy(&s_filename_str, &p_sess->log_str);
  str_replace_char(&s_filename_str, ' ', '_');
  str_append_str(p_str, &s_filename_str);
  str_append_char(p_str, ' ');
  /* Transfer type (ascii/binary) */
  if (p_sess->is_ascii)
  {
    str_append_text(p_str, "a ");
  }
  else
  {
    str_append_text(p_str, "b ");
  }
  /* Special action flag - tar, gzip etc. */
  str_append_text(p_str, "_ ");
  /* Direction of transfer */
  if (what == kVSFLogEntryUpload)
  {
    str_append_text(p_str, "i ");
  }
  else
  {
    str_append_text(p_str, "o ");
  }
  /* Access mode: anonymous/real user, and identity */
  if (p_sess->is_anonymous && !p_sess->is_guest)
  {
    str_append_text(p_str, "a ");
    str_append_str(p_str, &p_sess->anon_pass_str);
  }
  else
  {
    if (p_sess->is_guest)
    {
      str_append_text(p_str, "g ");
    } 
    else
    {
      str_append_text(p_str, "r ");
    }
    str_append_str(p_str, &p_sess->user_str);
  }
  str_append_char(p_str, ' ');
  /* Service name, authentication method, authentication user id */
  str_append_text(p_str, "ftp 0 * ");
  /* Completion status */
  if (succeeded)
  {
    str_append_char(p_str, 'c');
  }
  else
  {
    str_append_char(p_str, 'i');
  }
}

static void
vsf_log_do_log_vsftpd_format(struct vsf_session* p_sess, struct mystr* p_str,
                             int succeeded, enum EVSFLogEntryType what,
                             const struct mystr* p_log_str)
{
  str_empty(p_str);
  if (!tunable_syslog_enable)
  {
    /* Date - vsf_sysutil_get_current_date updates cached time */
    str_append_text(p_str, vsf_sysutil_get_current_date());
    /* Pid */
    str_append_text(p_str, " [pid ");
    str_append_ulong(p_str, vsf_sysutil_getpid());
    str_append_text(p_str, "] ");
  }
  /* User */
  if (!str_isempty(&p_sess->user_str))
  {
    str_append_char(p_str, '[');
    str_append_str(p_str, &p_sess->user_str);
    str_append_text(p_str, "] ");
  }
  /* And the action */
  if (what != kVSFLogEntryFTPInput && what != kVSFLogEntryFTPOutput &&
      what != kVSFLogEntryConnection && what != kVSFLogEntryDebug)
  {
    if (succeeded)
    {
      str_append_text(p_str, "OK ");
    }
    else
    {
      str_append_text(p_str, "FAIL ");
    }
  }
  switch (what)
  {
    case kVSFLogEntryDownload:
      str_append_text(p_str, "DOWNLOAD");
      break;
    case kVSFLogEntryUpload:
      str_append_text(p_str, "UPLOAD");
      break;
    case kVSFLogEntryMkdir:
      str_append_text(p_str, "MKDIR");
      break;
    case kVSFLogEntryLogin:
      str_append_text(p_str, "LOGIN");
      break;
    case kVSFLogEntryFTPInput:
      str_append_text(p_str, "FTP command");
      break;
    case kVSFLogEntryFTPOutput:
      str_append_text(p_str, "FTP response");
      break;
    case kVSFLogEntryConnection:
      str_append_text(p_str, "CONNECT");
      break;
    case kVSFLogEntryDelete:
      str_append_text(p_str, "DELETE");
      break;
    case kVSFLogEntryRename:
      str_append_text(p_str, "RENAME");
      break;
    case kVSFLogEntryRmdir:
      str_append_text(p_str, "RMDIR");
      break;
    case kVSFLogEntryChmod:
      str_append_text(p_str, "CHMOD");
      break;
    case kVSFLogEntryDebug:
      str_append_text(p_str, "DEBUG");
      break;
    case kVSFLogEntryNull:
      /* Fall through */
    default:
      bug("bad entry_type in vsf_log_do_log");
      break;
  }
  str_append_text(p_str, ": Client \"");
  str_append_str(p_str, &p_sess->remote_ip_str);
  str_append_char(p_str, '"');
  if (what == kVSFLogEntryLogin && !str_isempty(&p_sess->anon_pass_str))
  {
    str_append_text(p_str, ", anon password \"");
    str_append_str(p_str, &p_sess->anon_pass_str);
    str_append_char(p_str, '"');
  }
  if (!str_isempty(p_log_str))
  {
    str_append_text(p_str, ", \"");
    str_append_str(p_str, p_log_str);
    str_append_char(p_str, '"');
  }
  if (what != kVSFLogEntryFTPInput && what != kVSFLogEntryFTPOutput &&
      what != kVSFLogEntryDebug)
  {
    if (p_sess->transfer_size)
    {
      str_append_text(p_str, ", ");
      str_append_filesize_t(p_str, p_sess->transfer_size);
      str_append_text(p_str, " bytes");
    }
    if (vsf_log_type_is_transfer(what))
    {
      long delta_sec = vsf_sysutil_get_time_sec() - p_sess->log_start_sec;
      long delta_usec = vsf_sysutil_get_time_usec() - p_sess->log_start_usec;
      double time_delta = (double) delta_sec + ((double) delta_usec /
                                                (double) 1000000);
      double kbyte_rate;
      if (time_delta <= 0)
      {
        time_delta = 0.1;
      }
      kbyte_rate =
        ((double) p_sess->transfer_size / time_delta) / (double) 1024;
      str_append_text(p_str, ", ");
      str_append_double(p_str, kbyte_rate);
      str_append_text(p_str, "Kbyte/sec");
    }
  }
}