• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

proftpd / proftpd / 26127302613

19 May 2026 09:51PM UTC coverage: 93.024% (+0.4%) from 92.635%
26127302613

push

github

51329 of 55178 relevant lines covered (93.02%)

215.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.55
/src/trace.c
1
/*
2
 * ProFTPD - FTP server daemon
3
 * Copyright (c) 2006-2026 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, see <https://www.gnu.org/licenses/>.
17
 *
18
 * As a special exemption, the ProFTPD Project and other respective copyright
19
 * holders give permission to link this program with OpenSSL, and distribute
20
 * the resulting executable, without including the source code for OpenSSL
21
 * in the source distribution.
22
 */
23

24
/* Trace functions. */
25

26
#include "conf.h"
27
#include "privs.h"
28

29
#ifdef PR_USE_TRACE
30

31
#define TRACE_BUFFER_SIZE                (PR_TUNABLE_BUFFER_SIZE * 8)
32

33
static int trace_logfd = -1;
34
static unsigned long trace_opts = PR_TRACE_OPT_DEFAULT;
35
static pool *trace_pool = NULL;
36
static pr_table_t *trace_tab = NULL;
37

38
struct trace_levels {
39
  int min_level;
40
  int max_level;
41
};
42

43
static const char *trace_channels[] = {
44
  "auth",
45
  "binding",
46
  "command",
47
  "config",
48
  "ctrls",
49
  "data",
50
  "delay",
51
  "directory",
52
  "dns",
53
  "dso",
54
  "encode",
55
  "event",
56
  "facl",
57
  "fsio",
58
  "ident",
59
  "inet",
60
  "lock",
61
  "log",
62
  "module",
63
  "netacl",
64
  "netio",
65
  "pam",
66
  "pool",
67
  "regexp",
68
  "response",
69
  "scoreboard",
70
  "signal",
71
  "site",
72
  "timer",
73
  "var",
74
  "xfer",
75
  NULL
76
};
77

78
static void trace_restart_ev(const void *event_data, void *user_data) {
79
  trace_opts = PR_TRACE_OPT_DEFAULT;
2✔
80

2✔
81
  (void) close(trace_logfd);
82
  trace_logfd = -1;
2✔
83

2✔
84
  if (trace_pool != NULL) {
85
    destroy_pool(trace_pool);
2✔
86
    trace_pool = NULL;
2✔
87
    trace_tab = NULL;
2✔
88

2✔
89
    pr_event_unregister(NULL, "core.restart", trace_restart_ev);
90
  }
2✔
91
}
92

2✔
93
static int trace_write(const char *channel, int level, const char *msg,
94
    int discard) {
4,903✔
95
  pool *tmp_pool;
96
  char buf[TRACE_BUFFER_SIZE];
4,903✔
97
  size_t buflen = 0, len = 0;
4,903✔
98
  int use_conn_ips = FALSE;
4,903✔
99

4,903✔
100
  if (trace_logfd < 0) {
101
    return 0;
4,903✔
102
  }
103

104
  memset(buf, '\0', sizeof(buf));
105
  tmp_pool = make_sub_pool(trace_pool);
4,903✔
106
  pr_pool_tag(tmp_pool, "Trace message pool");
4,903✔
107

4,903✔
108
  if (trace_opts & PR_TRACE_OPT_USE_TIMESTAMP) {
109
    struct tm *tm;
4,903✔
110

4,899✔
111
    if (trace_opts & PR_TRACE_OPT_USE_TIMESTAMP_MILLIS) {
112
      struct timeval now;
4,899✔
113
      unsigned long millis;
4,899✔
114

4,899✔
115
      gettimeofday(&now, NULL);
116
      tm = pr_localtime(tmp_pool, (const time_t *) &(now.tv_sec));
4,899✔
117

4,899✔
118
      len = strftime(buf, sizeof(buf)-1, "%Y-%m-%d %H:%M:%S", tm);
119
      buflen = len;
4,899✔
120

4,899✔
121
      /* Convert microsecs to millisecs. */
122
      millis = now.tv_usec / 1000;
123

4,899✔
124
      len = pr_snprintf(buf + buflen, sizeof(buf) - buflen, ",%03lu ", millis);
125
      buflen += len;
4,899✔
126

4,899✔
127
    } else {
128
      time_t now;
129

×
130
      now = time(NULL);
131
      tm = pr_localtime(tmp_pool, &now);
×
132

×
133
      len = strftime(buf, sizeof(buf)-1, "%Y-%m-%d %H:%M:%S ", tm);
134
      buflen = len;
×
135
    }
×
136
  }
137

138
  if ((trace_opts & PR_TRACE_OPT_LOG_CONN_IPS) &&
139
      session.c != NULL) {
4,903✔
140
    /* We can only support the "+ConnIPs" TraceOption if there actually
3✔
141
     * is a client connected in this process.  We might be the daemon
142
     * process, in which there is no client.
143
     */
144
    use_conn_ips = TRUE;
145
  }
1✔
146

147
  if (use_conn_ips == FALSE) {
148
    len = pr_snprintf(buf + buflen, sizeof(buf) - buflen, "[%u] <%s:%d>: %s",
1✔
149
      (unsigned int) (session.pid ? session.pid : getpid()), channel, level,
4,902✔
150
      msg);
4,902✔
151
    buflen += len;
152

4,902✔
153
  } else {
154
    const char *client_ip, *server_ip;
155
    int server_port;
1✔
156

1✔
157
    client_ip = pr_netaddr_get_ipstr(session.c->remote_addr);
158
    server_ip = pr_netaddr_get_ipstr(session.c->local_addr);
1✔
159
    server_port = pr_netaddr_get_port(session.c->local_addr);
1✔
160

1✔
161
    len = pr_snprintf(buf + buflen, sizeof(buf) - buflen,
162
      "[%u] (client %s, server %s:%d) <%s:%d>: %s",
2✔
163
      (unsigned int) (session.pid ? session.pid : getpid()),
164
      client_ip != NULL ? client_ip : "none",
1✔
165
      server_ip != NULL ? server_ip : "none", server_port, channel, level, msg);
166
    buflen += len;
167
  }
1✔
168

169
  destroy_pool(tmp_pool);
170
  buf[sizeof(buf)-1] = '\0';
4,903✔
171

4,903✔
172
  if (buflen < (sizeof(buf) - 1)) {
173
    buf[buflen] = '\n';
4,903✔
174
    buflen++;
4,903✔
175

4,903✔
176
  } else {
177
    buf[sizeof(buf)-5] = '.';
178
    buf[sizeof(buf)-4] = '.';
×
179
    buf[sizeof(buf)-3] = '.';
×
180
    buf[sizeof(buf)-2] = '.';
×
181
    buflen = sizeof(buf)-1;
×
182
  }
×
183

184
  pr_log_event_generate(PR_LOG_TYPE_TRACELOG, trace_logfd, level, buf, buflen);
185

4,903✔
186
  if (discard) {
187
    /* This log message would not have been written to disk, so just discard
4,903✔
188
     * it.  The discard value is TRUE when there's a log listener for
189
     * TraceLog logging events, and the Trace log level configuration would
190
     * otherwise have filtered out this log message.
191
     */
192
    return 0;
193
  }
194

195
  return write(trace_logfd, buf, buflen);
196
}
4,903✔
197

198
pr_table_t *pr_trace_get_table(void) {
199
  if (trace_tab == NULL) {
2✔
200
    errno = ENOENT;
2✔
201
    return NULL;
1✔
202
  }
1✔
203

204
  return trace_tab;
205
}
206

207
static const struct trace_levels *trace_get_levels(const char *channel) {
208
  const void *value;
7,652✔
209

7,652✔
210
  if (channel == NULL) {
211
    errno = EINVAL;
7,652✔
212
    return NULL;
3✔
213
  }
3✔
214

215
  if (trace_tab == NULL ||
216
      trace_logfd < 0) {
7,649✔
217
    errno = EPERM;
7,646✔
218
    return NULL;
3✔
219
  }
3✔
220

221
  value = pr_table_get(trace_tab, channel, NULL);
222
  if (value == NULL) {
7,646✔
223
    errno = ENOENT;
7,646✔
224
    return NULL;
2,300✔
225
  }
2,300✔
226

227
  return value;
228
}
229

230
int pr_trace_get_level(const char *channel) {
231
  return pr_trace_get_max_level(channel);
389✔
232
}
778✔
233

234
int pr_trace_get_max_level(const char *channel) {
235
  const struct trace_levels *levels;
393✔
236

393✔
237
  levels = trace_get_levels(channel);
238
  if (levels == NULL) {
393✔
239
    return -1;
393✔
240
  }
241

242
  return levels->max_level;
243
}
387✔
244

245
int pr_trace_get_min_level(const char *channel) {
246
  const struct trace_levels *levels;
4✔
247

4✔
248
  levels = trace_get_levels(channel);
249
  if (levels == NULL) {
4✔
250
    return -1;
4✔
251
  }
252

253
  return levels->min_level;
254
}
1✔
255

256
int pr_trace_parse_levels(char *str, int *min_level, int *max_level) {
257
  int low = 1, high = -1;
15✔
258
  char *ptr = NULL, *tmp = NULL;
15✔
259

15✔
260
  if (str == NULL ||
261
      min_level == NULL ||
15✔
262
      max_level == NULL) {
15✔
263
    errno = EINVAL;
264
    return -1;
1✔
265
  }
1✔
266

267
  /* Watch for blank strings for levels (i.e. misconfigured/typo in config). */
268
  if (*str == '\0') {
269
    errno = EINVAL;
14✔
270
    return -1;
1✔
271
  }
1✔
272

273
  /* Check for a value range. */
274
  if (*str == '-') {
275
    errno = EINVAL;
13✔
276
    return -1;
2✔
277
  }
2✔
278

279
  ptr = strchr(str, '-');
280
  if (ptr == NULL) {
11✔
281
    /* Just a single value. */
11✔
282
    errno = 0;
283
    high = (int) strtol(str, &ptr, 10);
4✔
284
    if (errno == ERANGE) {
4✔
285
      errno = EINVAL;
4✔
286
      return -1;
×
287
    }
×
288

289
    if (ptr && *ptr) {
290
      errno = EINVAL;
4✔
291
      return -1;
1✔
292
    }
1✔
293

294
    if (high < 0) {
295
      errno = EINVAL;
3✔
296
      return -1;
1✔
297
    }
1✔
298

299
    /* A special case is where the single value is zero.  If this is the
300
     * case, we make sure that the min value is the same.
301
     */
302
    if (high != 0) {
303
      *min_level = 1;
2✔
304

1✔
305
    } else {
306
      *min_level = 0;
307
    }
1✔
308

309
    *max_level = high;
310
    return 0;
2✔
311
  }
2✔
312

313
  /* We have a range of values. */
314
  *ptr = '\0';
315

7✔
316
  low = (int) strtol(str, &tmp, 10);
317
  if (errno == ERANGE) {
7✔
318
    errno = EINVAL;
7✔
319
    return -1;
×
320
  }
×
321

322
  if (tmp && *tmp) {
323
    *ptr = '-';
7✔
324
    errno = EINVAL;
1✔
325
    return -1;
1✔
326
  }
1✔
327
  *ptr = '-';
328

6✔
329
  if (low < 0) {
330
    errno = EINVAL;
6✔
331
    return -1;
1✔
332
  }
1✔
333

334
  tmp = NULL;
335
  high = (int) strtol(ptr + 1, &tmp, 10);
5✔
336
  if (errno == ERANGE) {
5✔
337
    errno = EINVAL;
5✔
338
    return -1;
×
339
  }
×
340

341
  if (tmp && *tmp) {
342
    errno = EINVAL;
5✔
343
    return -1;
1✔
344
  }
1✔
345

346
  if (high < 0) {
347
    errno = EINVAL;
4✔
348
    return -1;
2✔
349
  }
2✔
350

351
  if (high < low) {
352
    errno = EINVAL;
2✔
353
    return -1;
1✔
354
  }
1✔
355

356
  *min_level = low;
357
  *max_level = high;
1✔
358
  return 0;
1✔
359
}
1✔
360

361
int pr_trace_set_file(const char *path) {
362
  int res, xerrno;
4✔
363

4✔
364
  if (path == NULL) {
365
    if (trace_logfd < 0) {
4✔
366
      errno = EINVAL;
1✔
367
      return -1;
×
368
    }
×
369

370
    (void) close(trace_logfd);
371
    trace_logfd = -1;
1✔
372
    return 0;
1✔
373
  }
1✔
374

375
  pr_signals_block();
376
  PRIVS_ROOT
3✔
377
  res = pr_log_openfile(path, &trace_logfd, 0660);
3✔
378
  xerrno = errno;
3✔
379
  PRIVS_RELINQUISH
3✔
380
  pr_signals_unblock();
3✔
381

3✔
382
  if (res < 0) {
383
    if (res == -1) {
3✔
384
      pr_log_debug(DEBUG1, "unable to open TraceLog '%s': %s", path,
2✔
385
        strerror(xerrno));
2✔
386
      errno = xerrno;
387

2✔
388
    } else if (res == PR_LOG_WRITABLE_DIR) {
389
      pr_log_debug(DEBUG1,
×
390
        "unable to open TraceLog '%s': parent directory is world-writable",
×
391
        path);
392
      errno = EPERM;
393

×
394
    } else if (res == PR_LOG_SYMLINK) {
395
      pr_log_debug(DEBUG1,
×
396
        "unable to open TraceLog '%s': cannot log to a symbolic link",
×
397
        path);
398
      errno = EPERM;
399
    }
×
400

401
    return res;
402
  }
2✔
403

404
  return 0;
405
}
406

407
int pr_trace_set_levels(const char *channel, int min_level, int max_level) {
408
  if (channel == NULL) {
2,350✔
409
    if (trace_tab == NULL) {
2,350✔
410
      errno = EINVAL;
1✔
411
      return -1;
1✔
412
    }
1✔
413

414
    return 0;
415
  }
416

417
  if (min_level > max_level) {
418
    errno = EINVAL;
2,349✔
419
    return -1;
1✔
420
  }
1✔
421

422
  if (trace_tab == NULL &&
423
      min_level < 0) {
2,348✔
424
    return 0;
425
  }
426

427
  if (trace_pool == NULL) {
428
    trace_pool = make_sub_pool(permanent_pool);
2,348✔
429
    pr_pool_tag(trace_pool, "Trace API");
738✔
430

738✔
431
    trace_tab = pr_table_alloc(trace_pool, 0);
432

738✔
433
    /* Register a handler for churning the log pool during HUP. */
434
    pr_event_register(NULL, "core.restart", trace_restart_ev, NULL);
435
  }
738✔
436

437
  if (min_level >= 0) {
438
    struct trace_levels *levels;
2,348✔
439

2,348✔
440
    levels = pcalloc(trace_pool, sizeof(struct trace_levels));
441
    levels->min_level = min_level;
2,348✔
442
    levels->max_level = max_level;
2,348✔
443

2,348✔
444
    if (strcmp(channel, PR_TRACE_DEFAULT_CHANNEL) != 0) {
445
      int count;
2,348✔
446

2,346✔
447
      count = pr_table_exists(trace_tab, channel);
448
      if (count <= 0) {
2,346✔
449
        if (pr_table_add(trace_tab, pstrdup(trace_pool, channel), levels,
2,346✔
450
            sizeof(struct trace_levels)) < 0) {
1,179✔
451
          return -1;
452
        }
×
453

454
      } else {
455
        if (pr_table_set(trace_tab, pstrdup(trace_pool, channel), levels,
456
            sizeof(struct trace_levels)) < 0) {
1,167✔
457
          return -1;
458
        }
×
459
      }
460

461
    } else {
462
      register unsigned int i;
463

464
      for (i = 0; trace_channels[i]; i++) {
465
        (void) pr_trace_set_levels(trace_channels[i], min_level, max_level);
64✔
466
      }
62✔
467
    }
468

469
  } else {
470
    if (strcmp(channel, PR_TRACE_DEFAULT_CHANNEL) != 0) {
471
      (void) pr_table_remove(trace_tab, channel, NULL);
×
472

×
473
    } else {
474
      register unsigned int i;
475

476
      for (i = 0; trace_channels[i]; i++) {
477
        (void) pr_table_remove(trace_tab, trace_channels[i], NULL);
×
478
      }
×
479
    }
480
  }
481

482
  return 0;
483
}
484

485
int pr_trace_set_options(unsigned long opts) {
486
  trace_opts = opts;
13✔
487
  return 0;
13✔
488
}
13✔
489

490
int pr_trace_use_stderr(int use_stderr) {
491
  if (use_stderr) {
909✔
492
    int res;
909✔
493

899✔
494
    res = dup(STDERR_FILENO);
495
    if (res < 0) {
899✔
496
      return -1;
899✔
497
    }
498

499
    /* Avoid a file descriptor leak by closing any existing fd. */
500
    (void) close(trace_logfd);
501
    trace_logfd = res;
899✔
502

899✔
503
  } else {
504
    (void) close(trace_logfd);
505
    trace_logfd = -1;
10✔
506
  }
10✔
507

508
  return 0;
509
}
510

511
int pr_trace_msg(const char *channel, int level, const char *fmt, ...) {
512
  int res;
8,811✔
513
  va_list msg;
8,811✔
514

8,811✔
515
  if (channel == NULL ||
516
      fmt == NULL ||
8,811✔
517
      level <= 0) {
8,811✔
518
    errno = EINVAL;
519
    return -1;
3✔
520
  }
3✔
521

522
  /* If no one's listening... */
523
  if (trace_logfd < 0 &&
524
      pr_log_event_listening(PR_LOG_TYPE_TRACELOG) <= 0) {
8,812✔
525
    return 0;
4✔
526
  }
527

528
  va_start(msg, fmt);
529
  res = pr_trace_vmsg(channel, level, fmt, msg);
8,804✔
530
  va_end(msg);
8,804✔
531

8,804✔
532
  return res;
533
}
8,804✔
534

535
int pr_trace_vmsg(const char *channel, int level, const char *fmt,
536
    va_list msg) {
8,804✔
537
  char buf[TRACE_BUFFER_SIZE];
538
  const struct trace_levels *levels;
8,804✔
539
  int buflen, discard = FALSE, listening;
8,804✔
540

8,804✔
541
  /* Writing a trace message at level zero is NOT helpful; this makes it
542
   * impossible to quell messages to that trace channel by setting the level
543
   * filter to zero.  That being the case, treat level of zero as an invalid
544
   * level.
545
   */
546

547
  if (channel == NULL ||
548
      fmt == NULL ||
8,804✔
549
      level <= 0) {
8,804✔
550
    errno = EINVAL;
551
    return -1;
×
552
  }
×
553

554
  if (trace_tab == NULL) {
555
    errno = EPERM;
8,804✔
556
    return -1;
1,549✔
557
  }
1,549✔
558

559
  /* If no one's listening... */
560
  if (trace_logfd < 0) {
561
    return 0;
7,255✔
562
  }
563

564
  listening = pr_log_event_listening(PR_LOG_TYPE_TRACELOG);
565

7,255✔
566
  levels = trace_get_levels(channel);
567
  if (levels == NULL) {
7,255✔
568
    discard = TRUE;
7,255✔
569

2,297✔
570
    if (listening <= 0) {
571
      return 0;
2,297✔
572
    }
573
  }
574

575
  if (discard == FALSE &&
576
      level < levels->min_level) {
4,958✔
577
    discard = TRUE;
4,958✔
578

×
579
    if (listening <= 0) {
580
      return 0;
×
581
    }
582
  }
583

584
  if (discard == FALSE &&
585
      level > levels->max_level) {
4,958✔
586
    discard = TRUE;
4,958✔
587

55✔
588
    if (listening <= 0) {
589
      return 0;
55✔
590
    }
591
  }
592

593
  buflen = pr_vsnprintf(buf, sizeof(buf)-1, fmt, msg);
594

4,903✔
595
  /* Always make sure the buffer is NUL-terminated. */
596
  buf[sizeof(buf)-1] = '\0';
597

4,903✔
598
  if (buflen > 0 &&
599
      (size_t) buflen < sizeof(buf)) {
4,903✔
600
    buf[buflen] = '\0';
601

4,902✔
602
  } else {
603
    /* Note that vsnprintf() returns the number of characters _that would have
604
     * been printed if buffer were unlimited_.  Be careful of this.
605
     */
606
    buflen = sizeof(buf)-1;
607
  }
608

609
  /* Trim trailing newlines. */
610
  while (buflen >= 1 &&
611
         buf[buflen-1] == '\n') {
4,910✔
612
    pr_signals_handle();
4,910✔
613
    buf[buflen-1] = '\0';
7✔
614
    buflen--;
7✔
615
  }
7✔
616

617
  return trace_write(channel, level, buf, discard);
618
}
4,903✔
619

620
#else
621

622
pr_table_t *pr_trace_get_table(void) {
623
  errno = ENOSYS;
624
  return NULL;
625
}
626

627
int pr_trace_get_level(const char *channel) {
628
  errno = ENOSYS;
629
  return -1;
630
}
631

632
int pr_trace_get_max_level(const char *channel) {
633
  errno = ENOSYS;
634
  return -1;
635
}
636

637
int pr_trace_get_min_level(const char *channel) {
638
  errno = ENOSYS;
639
  return -1;
640
}
641

642
int pr_trace_parse_levels(char *str, int *min_level, int *max_level) {
643
  errno = ENOSYS;
644
  return -1;
645
}
646

647
int pr_trace_set_file(const char *path) {
648
  errno = ENOSYS;
649
  return -1;
650
}
651

652
int pr_trace_set_levels(const char *channel, int min_level, int max_level) {
653
  errno = ENOSYS;
654
  return -1;
655
}
656

657
int pr_trace_set_options(unsigned long opts) {
658
  errno = ENOSYS;
659
  return -1;
660
}
661

662
int pr_trace_msg(const char *channel, int level, const char *fmt, ...) {
663
  errno = ENOSYS;
664
  return -1;
665
}
666

667
int pr_trace_vmsg(const char *channel, int level, const char *fmt,
668
    va_list vargs) {
669
  errno = ENOSYS;
670
  return -1;
671
}
672

673
#endif /* PR_USE_TRACE */
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc