Coverage Report

Created: 2024-09-30 06:24

/src/proftpd/src/xferlog.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * ProFTPD - FTP server daemon
3
 * Copyright (c) 2003-2020 The ProFTPD Project team
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18
 *
19
 * As a special exemption, Public Flood Software/MacGyver aka Habeeb J. Dihu
20
 * and other respective copyright holders give permission to link this program
21
 * with OpenSSL, and distribute the resulting executable, without including
22
 * the source code for OpenSSL in the source distribution.
23
 */
24
25
/* ProFTPD xferlog(5) logging support. */
26
27
#include "conf.h"
28
29
#define LOGBUFFER_SIZE  2048
30
31
static int xferlogfd = -1;
32
33
0
void xferlog_close(void) {
34
0
  if (xferlogfd != -1) {
35
0
    (void) close(xferlogfd);
36
0
  }
37
38
0
  xferlogfd = -1;
39
0
}
40
41
0
int xferlog_open(const char *path) {
42
43
0
  if (path == NULL) {
44
0
    if (xferlogfd != -1) {
45
0
      xferlog_close();
46
0
    }
47
48
0
    return 0;
49
0
  }
50
51
0
  if (xferlogfd < 0) {
52
0
    pr_log_debug(DEBUG6, "opening TransferLog '%s'", path);
53
0
    pr_log_openfile(path, &xferlogfd, PR_TUNABLE_XFER_LOG_MODE);
54
55
0
    if (xferlogfd < 0) {
56
0
      int xerrno = errno;
57
58
0
      pr_log_pri(PR_LOG_NOTICE, "unable to open TransferLog '%s': %s",
59
0
        path, strerror(xerrno));
60
61
0
      errno = xerrno;
62
0
    }
63
0
  }
64
65
0
  return xferlogfd;
66
0
}
67
68
int xferlog_write(long xfertime, const char *remhost, off_t fsize,
69
    const char *fname, char xfertype, char direction, char access_mode,
70
0
    const char *user, char abort_flag, const char *action_flags) {
71
0
  pool *tmp_pool;
72
0
  const char *rfc1413_ident = NULL, *xfer_proto;
73
0
  char buf[LOGBUFFER_SIZE] = {'\0'}, fbuf[LOGBUFFER_SIZE] = {'\0'};
74
0
  int have_ident = FALSE, len;
75
0
  register unsigned int i = 0;
76
77
0
  if (xferlogfd == -1 ||
78
0
      remhost == NULL ||
79
0
      user == NULL ||
80
0
      fname == NULL) {
81
0
    return 0;
82
0
  }
83
84
0
  for (i = 0; (i + 1 < sizeof(fbuf)) && fname[i] != '\0'; i++) {
85
0
    fbuf[i] = (PR_ISSPACE(fname[i]) || PR_ISCNTRL(fname[i])) ? '_' :
86
0
      fname[i];
87
0
  }
88
0
  fbuf[i] = '\0';
89
90
0
  rfc1413_ident = pr_table_get(session.notes, "mod_ident.rfc1413-ident", NULL);
91
0
  if (rfc1413_ident != NULL) {
92
0
    have_ident = TRUE;
93
94
    /* If the retrieved identity is "UNKNOWN", then change the string to be
95
     * "*", since "*" is to be logged in the xferlog, as per the doc, when
96
     * the authenticated user ID is not available.
97
     */
98
0
    if (strcmp(rfc1413_ident, "UNKNOWN") == 0) {
99
0
      rfc1413_ident = "*";
100
0
    }
101
102
0
  } else {
103
    /* If an authenticated user ID is not available, log "*", as per the
104
     * xferlog man page/spec.
105
     */
106
0
    rfc1413_ident = "*";
107
0
  }
108
109
0
  xfer_proto = pr_session_get_protocol(0);
110
0
  tmp_pool = make_sub_pool(session.pool);
111
0
  pr_pool_tag(tmp_pool, "TransferLog message pool");
112
113
0
  len = pr_snprintf(buf, sizeof(buf),
114
0
    "%s %ld %s %" PR_LU " %s %c %s %c %c %s %s %c %s %c\n",
115
0
      pr_strtime3(tmp_pool, time(NULL), FALSE),
116
0
      xfertime,
117
0
      remhost,
118
0
      (pr_off_t) fsize,
119
0
      fbuf,
120
0
      xfertype,
121
0
      action_flags,
122
0
      direction,
123
0
      access_mode,
124
0
      user,
125
0
      xfer_proto,
126
0
      have_ident ? '1' : '0',
127
0
      rfc1413_ident,
128
0
      abort_flag);
129
0
  buf[sizeof(buf)-1] = '\0';
130
131
0
  pr_log_event_generate(PR_LOG_TYPE_XFERLOG, xferlogfd, -1, buf, len);
132
0
  destroy_pool(tmp_pool);
133
134
0
  return write(xferlogfd, buf, len);
135
0
}