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

proftpd / proftpd / 26693818498

30 May 2026 08:22PM UTC coverage: 93.024% (+0.4%) from 92.637%
26693818498

push

github

51329 of 55178 relevant lines covered (93.02%)

222.12 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_cleanup(void *user_data) {
79
  destroy_pool(trace_pool);
2✔
80
  trace_pool = NULL;
2✔
81
  trace_tab = NULL;
82
}
2✔
83

2✔
84
static void trace_restart_ev(const void *event_data, void *user_data) {
85
  trace_opts = PR_TRACE_OPT_DEFAULT;
2✔
86

2✔
87
  (void) close(trace_logfd);
2✔
88
  trace_logfd = -1;
2✔
89

90
  if (trace_pool != NULL) {
2✔
91
    destroy_pool(trace_pool);
92
    trace_pool = NULL;
2✔
93
    trace_tab = NULL;
94

4,903✔
95
    pr_event_unregister(NULL, "core.restart", trace_restart_ev);
96
  }
4,903✔
97
}
4,903✔
98

4,903✔
99
static int trace_write(const char *channel, int level, const char *msg,
4,903✔
100
    int discard) {
101
  pool *tmp_pool;
4,903✔
102
  char buf[TRACE_BUFFER_SIZE];
103
  size_t buflen = 0, len = 0;
104
  int use_conn_ips = FALSE;
105

4,903✔
106
  if (trace_logfd < 0) {
4,903✔
107
    return 0;
4,903✔
108
  }
109

4,903✔
110
  memset(buf, '\0', sizeof(buf));
4,899✔
111
  tmp_pool = make_sub_pool(trace_pool);
112
  pr_pool_tag(tmp_pool, "Trace message pool");
4,899✔
113

4,899✔
114
  if (trace_opts & PR_TRACE_OPT_USE_TIMESTAMP) {
4,899✔
115
    struct tm *tm;
116

4,899✔
117
    if (trace_opts & PR_TRACE_OPT_USE_TIMESTAMP_MILLIS) {
4,899✔
118
      struct timeval now;
119
      unsigned long millis;
4,899✔
120

4,899✔
121
      gettimeofday(&now, NULL);
122
      tm = pr_localtime(tmp_pool, (const time_t *) &(now.tv_sec));
123

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

4,899✔
127
      /* Convert microsecs to millisecs. */
128
      millis = now.tv_usec / 1000;
129

×
130
      len = pr_snprintf(buf + buflen, sizeof(buf) - buflen, ",%03lu ", millis);
131
      buflen += len;
×
132

×
133
    } else {
134
      time_t now;
×
135

×
136
      now = time(NULL);
137
      tm = pr_localtime(tmp_pool, &now);
138

139
      len = strftime(buf, sizeof(buf)-1, "%Y-%m-%d %H:%M:%S ", tm);
4,903✔
140
      buflen = len;
3✔
141
    }
142
  }
143

144
  if ((trace_opts & PR_TRACE_OPT_LOG_CONN_IPS) &&
145
      session.c != NULL) {
1✔
146
    /* We can only support the "+ConnIPs" TraceOption if there actually
147
     * is a client connected in this process.  We might be the daemon
148
     * process, in which there is no client.
1✔
149
     */
4,902✔
150
    use_conn_ips = TRUE;
4,902✔
151
  }
152

4,902✔
153
  if (use_conn_ips == FALSE) {
154
    len = pr_snprintf(buf + buflen, sizeof(buf) - buflen, "[%u] <%s:%d>: %s",
155
      (unsigned int) (session.pid ? session.pid : getpid()), channel, level,
1✔
156
      msg);
1✔
157
    buflen += len;
158

1✔
159
  } else {
1✔
160
    const char *client_ip, *server_ip;
1✔
161
    int server_port;
162

2✔
163
    client_ip = pr_netaddr_get_ipstr(session.c->remote_addr);
164
    server_ip = pr_netaddr_get_ipstr(session.c->local_addr);
1✔
165
    server_port = pr_netaddr_get_port(session.c->local_addr);
166

167
    len = pr_snprintf(buf + buflen, sizeof(buf) - buflen,
1✔
168
      "[%u] (client %s, server %s:%d) <%s:%d>: %s",
169
      (unsigned int) (session.pid ? session.pid : getpid()),
170
      client_ip != NULL ? client_ip : "none",
4,903✔
171
      server_ip != NULL ? server_ip : "none", server_port, channel, level, msg);
4,903✔
172
    buflen += len;
173
  }
4,903✔
174

4,903✔
175
  destroy_pool(tmp_pool);
4,903✔
176
  buf[sizeof(buf)-1] = '\0';
177

178
  if (buflen < (sizeof(buf) - 1)) {
×
179
    buf[buflen] = '\n';
×
180
    buflen++;
×
181

×
182
  } else {
×
183
    buf[sizeof(buf)-5] = '.';
184
    buf[sizeof(buf)-4] = '.';
185
    buf[sizeof(buf)-3] = '.';
4,903✔
186
    buf[sizeof(buf)-2] = '.';
187
    buflen = sizeof(buf)-1;
4,903✔
188
  }
189

190
  pr_log_event_generate(PR_LOG_TYPE_TRACELOG, trace_logfd, level, buf, buflen);
191

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

2✔
201
  return write(trace_logfd, buf, buflen);
1✔
202
}
1✔
203

204
pr_table_t *pr_trace_get_table(void) {
205
  if (trace_tab == NULL) {
206
    errno = ENOENT;
207
    return NULL;
208
  }
7,652✔
209

7,652✔
210
  return trace_tab;
211
}
7,652✔
212

3✔
213
static const struct trace_levels *trace_get_levels(const char *channel) {
3✔
214
  const void *value;
215

216
  if (channel == NULL) {
7,649✔
217
    errno = EINVAL;
7,646✔
218
    return NULL;
3✔
219
  }
3✔
220

221
  if (trace_tab == NULL ||
222
      trace_logfd < 0) {
7,646✔
223
    errno = EPERM;
7,646✔
224
    return NULL;
2,300✔
225
  }
2,300✔
226

227
  value = pr_table_get(trace_tab, channel, NULL);
228
  if (value == NULL) {
229
    errno = ENOENT;
230
    return NULL;
231
  }
389✔
232

778✔
233
  return value;
234
}
235

393✔
236
int pr_trace_get_level(const char *channel) {
393✔
237
  return pr_trace_get_max_level(channel);
238
}
393✔
239

393✔
240
int pr_trace_get_max_level(const char *channel) {
241
  const struct trace_levels *levels;
242

243
  levels = trace_get_levels(channel);
387✔
244
  if (levels == NULL) {
245
    return -1;
246
  }
4✔
247

4✔
248
  return levels->max_level;
249
}
4✔
250

4✔
251
int pr_trace_get_min_level(const char *channel) {
252
  const struct trace_levels *levels;
253

254
  levels = trace_get_levels(channel);
1✔
255
  if (levels == NULL) {
256
    return -1;
257
  }
15✔
258

15✔
259
  return levels->min_level;
15✔
260
}
261

15✔
262
int pr_trace_parse_levels(char *str, int *min_level, int *max_level) {
15✔
263
  int low = 1, high = -1;
264
  char *ptr = NULL, *tmp = NULL;
1✔
265

1✔
266
  if (str == NULL ||
267
      min_level == NULL ||
268
      max_level == NULL) {
269
    errno = EINVAL;
14✔
270
    return -1;
1✔
271
  }
1✔
272

273
  /* Watch for blank strings for levels (i.e. misconfigured/typo in config). */
274
  if (*str == '\0') {
275
    errno = EINVAL;
13✔
276
    return -1;
2✔
277
  }
2✔
278

279
  /* Check for a value range. */
280
  if (*str == '-') {
11✔
281
    errno = EINVAL;
11✔
282
    return -1;
283
  }
4✔
284

4✔
285
  ptr = strchr(str, '-');
4✔
286
  if (ptr == NULL) {
×
287
    /* Just a single value. */
×
288
    errno = 0;
289
    high = (int) strtol(str, &ptr, 10);
290
    if (errno == ERANGE) {
4✔
291
      errno = EINVAL;
1✔
292
      return -1;
1✔
293
    }
294

295
    if (ptr && *ptr) {
3✔
296
      errno = EINVAL;
1✔
297
      return -1;
1✔
298
    }
299

300
    if (high < 0) {
301
      errno = EINVAL;
302
      return -1;
303
    }
2✔
304

1✔
305
    /* A special case is where the single value is zero.  If this is the
306
     * case, we make sure that the min value is the same.
307
     */
1✔
308
    if (high != 0) {
309
      *min_level = 1;
310

2✔
311
    } else {
2✔
312
      *min_level = 0;
313
    }
314

315
    *max_level = high;
7✔
316
    return 0;
317
  }
7✔
318

7✔
319
  /* We have a range of values. */
×
320
  *ptr = '\0';
×
321

322
  low = (int) strtol(str, &tmp, 10);
323
  if (errno == ERANGE) {
7✔
324
    errno = EINVAL;
1✔
325
    return -1;
1✔
326
  }
1✔
327

328
  if (tmp && *tmp) {
6✔
329
    *ptr = '-';
330
    errno = EINVAL;
6✔
331
    return -1;
1✔
332
  }
1✔
333
  *ptr = '-';
334

335
  if (low < 0) {
5✔
336
    errno = EINVAL;
5✔
337
    return -1;
5✔
338
  }
×
339

×
340
  tmp = NULL;
341
  high = (int) strtol(ptr + 1, &tmp, 10);
342
  if (errno == ERANGE) {
5✔
343
    errno = EINVAL;
1✔
344
    return -1;
1✔
345
  }
346

347
  if (tmp && *tmp) {
4✔
348
    errno = EINVAL;
2✔
349
    return -1;
2✔
350
  }
351

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

357
  if (high < low) {
1✔
358
    errno = EINVAL;
1✔
359
    return -1;
1✔
360
  }
361

362
  *min_level = low;
4✔
363
  *max_level = high;
4✔
364
  return 0;
365
}
4✔
366

1✔
367
int pr_trace_set_file(const char *path) {
×
368
  int res, xerrno;
×
369

370
  if (path == NULL) {
371
    if (trace_logfd < 0) {
1✔
372
      errno = EINVAL;
1✔
373
      return -1;
1✔
374
    }
375

376
    (void) close(trace_logfd);
3✔
377
    trace_logfd = -1;
3✔
378
    return 0;
3✔
379
  }
3✔
380

3✔
381
  pr_signals_block();
3✔
382
  PRIVS_ROOT
383
  res = pr_log_openfile(path, &trace_logfd, 0660);
3✔
384
  xerrno = errno;
2✔
385
  PRIVS_RELINQUISH
2✔
386
  pr_signals_unblock();
387

2✔
388
  if (res < 0) {
389
    if (res == -1) {
×
390
      pr_log_debug(DEBUG1, "unable to open TraceLog '%s': %s", path,
×
391
        strerror(xerrno));
392
      errno = xerrno;
393

×
394
    } else if (res == PR_LOG_WRITABLE_DIR) {
395
      pr_log_debug(DEBUG1,
×
396
        "unable to open TraceLog '%s': parent directory is world-writable",
×
397
        path);
398
      errno = EPERM;
399

×
400
    } else if (res == PR_LOG_SYMLINK) {
401
      pr_log_debug(DEBUG1,
402
        "unable to open TraceLog '%s': cannot log to a symbolic link",
2✔
403
        path);
404
      errno = EPERM;
405
    }
406

407
    return res;
408
  }
2,350✔
409

2,350✔
410
  return 0;
1✔
411
}
1✔
412

1✔
413
int pr_trace_set_levels(const char *channel, int min_level, int max_level) {
414
  if (channel == NULL) {
415
    if (trace_tab == NULL) {
416
      errno = EINVAL;
417
      return -1;
418
    }
2,349✔
419

1✔
420
    return 0;
1✔
421
  }
422

423
  if (min_level > max_level) {
2,348✔
424
    errno = EINVAL;
425
    return -1;
426
  }
427

428
  if (trace_tab == NULL &&
2,348✔
429
      min_level < 0) {
738✔
430
    return 0;
738✔
431
  }
432

738✔
433
  if (trace_pool == NULL) {
434
    trace_pool = make_sub_pool(permanent_pool);
435
    pr_pool_tag(trace_pool, "Trace API");
738✔
436

437
    trace_tab = pr_table_alloc(trace_pool, 0);
438

2,348✔
439
    /* Register a handler for churning the log pool during HUP. */
2,348✔
440
    pr_event_register(NULL, "core.restart", trace_restart_ev, NULL);
441

2,348✔
442
    /* Make sure to clean our things up when the permanent_pool is destroyed. */
2,348✔
443
    register_cleanup2(permanent_pool, NULL, trace_cleanup);
2,348✔
444
  }
445

2,348✔
446
  if (min_level >= 0) {
2,346✔
447
    struct trace_levels *levels;
448

2,346✔
449
    levels = pcalloc(trace_pool, sizeof(struct trace_levels));
2,346✔
450
    levels->min_level = min_level;
1,179✔
451
    levels->max_level = max_level;
452

×
453
    if (strcmp(channel, PR_TRACE_DEFAULT_CHANNEL) != 0) {
454
      int count;
455

456
      count = pr_table_exists(trace_tab, channel);
1,167✔
457
      if (count <= 0) {
458
        if (pr_table_add(trace_tab, pstrdup(trace_pool, channel), levels,
×
459
            sizeof(struct trace_levels)) < 0) {
460
          return -1;
461
        }
462

463
      } else {
464
        if (pr_table_set(trace_tab, pstrdup(trace_pool, channel), levels,
465
            sizeof(struct trace_levels)) < 0) {
64✔
466
          return -1;
62✔
467
        }
468
      }
469

470
    } else {
471
      register unsigned int i;
×
472

×
473
      for (i = 0; trace_channels[i]; i++) {
474
        (void) pr_trace_set_levels(trace_channels[i], min_level, max_level);
475
      }
476
    }
477

×
478
  } else {
×
479
    if (strcmp(channel, PR_TRACE_DEFAULT_CHANNEL) != 0) {
480
      (void) pr_table_remove(trace_tab, channel, NULL);
481

482
    } else {
483
      register unsigned int i;
484

485
      for (i = 0; trace_channels[i]; i++) {
486
        (void) pr_table_remove(trace_tab, trace_channels[i], NULL);
13✔
487
      }
13✔
488
    }
13✔
489
  }
490

491
  return 0;
909✔
492
}
909✔
493

899✔
494
int pr_trace_set_options(unsigned long opts) {
495
  trace_opts = opts;
899✔
496
  return 0;
899✔
497
}
498

499
int pr_trace_use_stderr(int use_stderr) {
500
  if (use_stderr) {
501
    int res;
899✔
502

899✔
503
    res = dup(STDERR_FILENO);
504
    if (res < 0) {
505
      return -1;
10✔
506
    }
10✔
507

508
    /* Avoid a file descriptor leak by closing any existing fd. */
509
    (void) close(trace_logfd);
510
    trace_logfd = res;
511

512
  } else {
8,811✔
513
    (void) close(trace_logfd);
8,811✔
514
    trace_logfd = -1;
8,811✔
515
  }
516

8,811✔
517
  return 0;
8,811✔
518
}
519

3✔
520
int pr_trace_msg(const char *channel, int level, const char *fmt, ...) {
3✔
521
  int res;
522
  va_list msg;
523

524
  if (channel == NULL ||
8,812✔
525
      fmt == NULL ||
4✔
526
      level <= 0) {
527
    errno = EINVAL;
528
    return -1;
529
  }
8,804✔
530

8,804✔
531
  /* If no one's listening... */
8,804✔
532
  if (trace_logfd < 0 &&
533
      pr_log_event_listening(PR_LOG_TYPE_TRACELOG) <= 0) {
8,804✔
534
    return 0;
535
  }
536

8,804✔
537
  va_start(msg, fmt);
538
  res = pr_trace_vmsg(channel, level, fmt, msg);
8,804✔
539
  va_end(msg);
8,804✔
540

8,804✔
541
  return res;
542
}
543

544
int pr_trace_vmsg(const char *channel, int level, const char *fmt,
545
    va_list msg) {
546
  char buf[TRACE_BUFFER_SIZE];
547
  const struct trace_levels *levels;
548
  int buflen, discard = FALSE, listening;
8,804✔
549

8,804✔
550
  /* Writing a trace message at level zero is NOT helpful; this makes it
551
   * impossible to quell messages to that trace channel by setting the level
×
552
   * filter to zero.  That being the case, treat level of zero as an invalid
×
553
   * level.
554
   */
555

8,804✔
556
  if (channel == NULL ||
1,549✔
557
      fmt == NULL ||
1,549✔
558
      level <= 0) {
559
    errno = EINVAL;
560
    return -1;
561
  }
7,255✔
562

563
  if (trace_tab == NULL) {
564
    errno = EPERM;
565
    return -1;
7,255✔
566
  }
567

7,255✔
568
  /* If no one's listening... */
7,255✔
569
  if (trace_logfd < 0) {
2,297✔
570
    return 0;
571
  }
2,297✔
572

573
  listening = pr_log_event_listening(PR_LOG_TYPE_TRACELOG);
574

575
  levels = trace_get_levels(channel);
576
  if (levels == NULL) {
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->min_level) {
4,958✔
586
    discard = TRUE;
4,958✔
587

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

593
  if (discard == FALSE &&
594
      level > levels->max_level) {
4,903✔
595
    discard = TRUE;
596

597
    if (listening <= 0) {
4,903✔
598
      return 0;
599
    }
4,903✔
600
  }
601

4,902✔
602
  buflen = pr_vsnprintf(buf, sizeof(buf)-1, fmt, msg);
603

604
  /* Always make sure the buffer is NUL-terminated. */
605
  buf[sizeof(buf)-1] = '\0';
606

607
  if (buflen > 0 &&
608
      (size_t) buflen < sizeof(buf)) {
609
    buf[buflen] = '\0';
610

611
  } else {
4,910✔
612
    /* Note that vsnprintf() returns the number of characters _that would have
4,910✔
613
     * been printed if buffer were unlimited_.  Be careful of this.
7✔
614
     */
7✔
615
    buflen = sizeof(buf)-1;
7✔
616
  }
617

618
  /* Trim trailing newlines. */
4,903✔
619
  while (buflen >= 1 &&
620
         buf[buflen-1] == '\n') {
621
    pr_signals_handle();
622
    buf[buflen-1] = '\0';
623
    buflen--;
624
  }
625

626
  return trace_write(channel, level, buf, discard);
627
}
628

629
#else
630

631
pr_table_t *pr_trace_get_table(void) {
632
  errno = ENOSYS;
633
  return NULL;
634
}
635

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

641
int pr_trace_get_max_level(const char *channel) {
642
  errno = ENOSYS;
643
  return -1;
644
}
645

646
int pr_trace_get_min_level(const char *channel) {
647
  errno = ENOSYS;
648
  return -1;
649
}
650

651
int pr_trace_parse_levels(char *str, int *min_level, int *max_level) {
652
  errno = ENOSYS;
653
  return -1;
654
}
655

656
int pr_trace_set_file(const char *path) {
657
  errno = ENOSYS;
658
  return -1;
659
}
660

661
int pr_trace_set_levels(const char *channel, int min_level, int max_level) {
662
  errno = ENOSYS;
663
  return -1;
664
}
665

666
int pr_trace_set_options(unsigned long opts) {
667
  errno = ENOSYS;
668
  return -1;
669
}
670

671
int pr_trace_msg(const char *channel, int level, const char *fmt, ...) {
672
  errno = ENOSYS;
673
  return -1;
674
}
675

676
int pr_trace_vmsg(const char *channel, int level, const char *fmt,
677
    va_list vargs) {
678
  errno = ENOSYS;
679
  return -1;
680
}
681

682
#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