Coverage Report

Created: 2024-06-18 06:23

/src/hpn-ssh/log.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD: log.c,v 1.61 2023/12/06 21:06:48 djm Exp $ */
2
/*
3
 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4
 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5
 *                    All rights reserved
6
 *
7
 * As far as I am concerned, the code I have written for this software
8
 * can be used freely for any purpose.  Any derived versions of this
9
 * software must be clearly marked as such, and if the derived work is
10
 * incompatible with the protocol description in the RFC file, it must be
11
 * called by a name other than "ssh" or "Secure Shell".
12
 */
13
/*
14
 * Copyright (c) 2000 Markus Friedl.  All rights reserved.
15
 *
16
 * Redistribution and use in source and binary forms, with or without
17
 * modification, are permitted provided that the following conditions
18
 * are met:
19
 * 1. Redistributions of source code must retain the above copyright
20
 *    notice, this list of conditions and the following disclaimer.
21
 * 2. Redistributions in binary form must reproduce the above copyright
22
 *    notice, this list of conditions and the following disclaimer in the
23
 *    documentation and/or other materials provided with the distribution.
24
 *
25
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
 */
36
37
#include "includes.h"
38
39
#include <sys/types.h>
40
41
#include <fcntl.h>
42
#include <stdarg.h>
43
#include <stdio.h>
44
#include <stdlib.h>
45
#include <string.h>
46
#include <syslog.h>
47
#include <unistd.h>
48
#include <errno.h>
49
#include "packet.h" /* needed for host and port look ups */
50
#ifdef HAVE_SYS_TIME_H
51
# include <sys/time.h> /* to get current time */
52
#endif
53
54
#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
55
# include <vis.h>
56
#endif
57
58
#include "log.h"
59
#include "match.h"
60
61
static LogLevel log_level = SYSLOG_LEVEL_INFO;
62
static int log_on_stderr = 1;
63
static int log_stderr_fd = STDERR_FILENO;
64
static int log_facility = LOG_AUTH;
65
static const char *argv0;
66
static log_handler_fn *log_handler;
67
static void *log_handler_ctx;
68
static char **log_verbose;
69
static size_t nlog_verbose;
70
71
extern char *__progname;
72
73
extern struct ssh *active_state;
74
75
0
#define LOG_SYSLOG_VIS  (VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL)
76
0
#define LOG_STDERR_VIS  (VIS_SAFE|VIS_OCTAL)
77
78
/* textual representation of log-facilities/levels */
79
80
static struct {
81
  const char *name;
82
  SyslogFacility val;
83
} log_facilities[] = {
84
  { "DAEMON", SYSLOG_FACILITY_DAEMON },
85
  { "USER", SYSLOG_FACILITY_USER },
86
  { "AUTH", SYSLOG_FACILITY_AUTH },
87
#ifdef LOG_AUTHPRIV
88
  { "AUTHPRIV", SYSLOG_FACILITY_AUTHPRIV },
89
#endif
90
  { "LOCAL0", SYSLOG_FACILITY_LOCAL0 },
91
  { "LOCAL1", SYSLOG_FACILITY_LOCAL1 },
92
  { "LOCAL2", SYSLOG_FACILITY_LOCAL2 },
93
  { "LOCAL3", SYSLOG_FACILITY_LOCAL3 },
94
  { "LOCAL4", SYSLOG_FACILITY_LOCAL4 },
95
  { "LOCAL5", SYSLOG_FACILITY_LOCAL5 },
96
  { "LOCAL6", SYSLOG_FACILITY_LOCAL6 },
97
  { "LOCAL7", SYSLOG_FACILITY_LOCAL7 },
98
  { NULL,   SYSLOG_FACILITY_NOT_SET }
99
};
100
101
static struct {
102
  const char *name;
103
  LogLevel val;
104
} log_levels[] =
105
{
106
  { "QUIET",  SYSLOG_LEVEL_QUIET },
107
  { "FATAL",  SYSLOG_LEVEL_FATAL },
108
  { "ERROR",  SYSLOG_LEVEL_ERROR },
109
  { "INFO", SYSLOG_LEVEL_INFO },
110
  { "VERBOSE",  SYSLOG_LEVEL_VERBOSE },
111
  { "DEBUG",  SYSLOG_LEVEL_DEBUG1 },
112
  { "DEBUG1", SYSLOG_LEVEL_DEBUG1 },
113
  { "DEBUG2", SYSLOG_LEVEL_DEBUG2 },
114
  { "DEBUG3", SYSLOG_LEVEL_DEBUG3 },
115
  { NULL,   SYSLOG_LEVEL_NOT_SET }
116
};
117
118
LogLevel
119
log_level_get(void)
120
0
{
121
0
  return log_level;
122
0
}
123
124
SyslogFacility
125
log_facility_number(char *name)
126
0
{
127
0
  int i;
128
129
0
  if (name != NULL)
130
0
    for (i = 0; log_facilities[i].name; i++)
131
0
      if (strcasecmp(log_facilities[i].name, name) == 0)
132
0
        return log_facilities[i].val;
133
0
  return SYSLOG_FACILITY_NOT_SET;
134
0
}
135
136
const char *
137
log_facility_name(SyslogFacility facility)
138
0
{
139
0
  u_int i;
140
141
0
  for (i = 0;  log_facilities[i].name; i++)
142
0
    if (log_facilities[i].val == facility)
143
0
      return log_facilities[i].name;
144
0
  return NULL;
145
0
}
146
147
LogLevel
148
log_level_number(char *name)
149
0
{
150
0
  int i;
151
152
0
  if (name != NULL)
153
0
    for (i = 0; log_levels[i].name; i++)
154
0
      if (strcasecmp(log_levels[i].name, name) == 0)
155
0
        return log_levels[i].val;
156
0
  return SYSLOG_LEVEL_NOT_SET;
157
0
}
158
159
const char *
160
log_level_name(LogLevel level)
161
0
{
162
0
  u_int i;
163
164
0
  for (i = 0; log_levels[i].name != NULL; i++)
165
0
    if (log_levels[i].val == level)
166
0
      return log_levels[i].name;
167
0
  return NULL;
168
0
}
169
170
void
171
log_verbose_add(const char *s)
172
0
{
173
0
  char **tmp;
174
175
  /* Ignore failures here */
176
0
  if ((tmp = recallocarray(log_verbose, nlog_verbose, nlog_verbose + 1,
177
0
      sizeof(*log_verbose))) != NULL) {
178
0
    log_verbose = tmp;
179
0
    if ((log_verbose[nlog_verbose] = strdup(s)) != NULL)
180
0
      nlog_verbose++;
181
0
  }
182
0
}
183
184
void
185
log_verbose_reset(void)
186
0
{
187
0
  size_t i;
188
189
0
  for (i = 0; i < nlog_verbose; i++)
190
0
    free(log_verbose[i]);
191
0
  free(log_verbose);
192
0
  log_verbose = NULL;
193
0
  nlog_verbose = 0;
194
0
}
195
196
/*
197
 * Initialize the log.
198
 */
199
200
void
201
log_init(const char *av0, LogLevel level, SyslogFacility facility,
202
    int on_stderr)
203
0
{
204
#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
205
  struct syslog_data sdata = SYSLOG_DATA_INIT;
206
#endif
207
208
0
  argv0 = av0;
209
210
0
  if (log_change_level(level) != 0) {
211
0
    fprintf(stderr, "Unrecognized internal syslog level code %d\n",
212
0
        (int) level);
213
0
    exit(1);
214
0
  }
215
216
0
  log_handler = NULL;
217
0
  log_handler_ctx = NULL;
218
219
0
  log_on_stderr = on_stderr;
220
0
  if (on_stderr)
221
0
    return;
222
223
0
  switch (facility) {
224
0
  case SYSLOG_FACILITY_DAEMON:
225
0
    log_facility = LOG_DAEMON;
226
0
    break;
227
0
  case SYSLOG_FACILITY_USER:
228
0
    log_facility = LOG_USER;
229
0
    break;
230
0
  case SYSLOG_FACILITY_AUTH:
231
0
    log_facility = LOG_AUTH;
232
0
    break;
233
0
#ifdef LOG_AUTHPRIV
234
0
  case SYSLOG_FACILITY_AUTHPRIV:
235
0
    log_facility = LOG_AUTHPRIV;
236
0
    break;
237
0
#endif
238
0
  case SYSLOG_FACILITY_LOCAL0:
239
0
    log_facility = LOG_LOCAL0;
240
0
    break;
241
0
  case SYSLOG_FACILITY_LOCAL1:
242
0
    log_facility = LOG_LOCAL1;
243
0
    break;
244
0
  case SYSLOG_FACILITY_LOCAL2:
245
0
    log_facility = LOG_LOCAL2;
246
0
    break;
247
0
  case SYSLOG_FACILITY_LOCAL3:
248
0
    log_facility = LOG_LOCAL3;
249
0
    break;
250
0
  case SYSLOG_FACILITY_LOCAL4:
251
0
    log_facility = LOG_LOCAL4;
252
0
    break;
253
0
  case SYSLOG_FACILITY_LOCAL5:
254
0
    log_facility = LOG_LOCAL5;
255
0
    break;
256
0
  case SYSLOG_FACILITY_LOCAL6:
257
0
    log_facility = LOG_LOCAL6;
258
0
    break;
259
0
  case SYSLOG_FACILITY_LOCAL7:
260
0
    log_facility = LOG_LOCAL7;
261
0
    break;
262
0
  default:
263
0
    fprintf(stderr,
264
0
        "Unrecognized internal syslog facility code %d\n",
265
0
        (int) facility);
266
0
    exit(1);
267
0
  }
268
269
  /*
270
   * If an external library (eg libwrap) attempts to use syslog
271
   * immediately after reexec, syslog may be pointing to the wrong
272
   * facility, so we force an open/close of syslog here.
273
   */
274
#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
275
  openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata);
276
  closelog_r(&sdata);
277
#else
278
0
  openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
279
0
  closelog();
280
0
#endif
281
0
}
282
283
int
284
log_change_level(LogLevel new_log_level)
285
0
{
286
  /* no-op if log_init has not been called */
287
0
  if (argv0 == NULL)
288
0
    return 0;
289
290
0
  switch (new_log_level) {
291
0
  case SYSLOG_LEVEL_QUIET:
292
0
  case SYSLOG_LEVEL_FATAL:
293
0
  case SYSLOG_LEVEL_ERROR:
294
0
  case SYSLOG_LEVEL_INFO:
295
0
  case SYSLOG_LEVEL_VERBOSE:
296
0
  case SYSLOG_LEVEL_DEBUG1:
297
0
  case SYSLOG_LEVEL_DEBUG2:
298
0
  case SYSLOG_LEVEL_DEBUG3:
299
0
    log_level = new_log_level;
300
0
    return 0;
301
0
  default:
302
0
    return -1;
303
0
  }
304
0
}
305
306
int
307
log_is_on_stderr(void)
308
0
{
309
0
  return log_on_stderr && log_stderr_fd == STDERR_FILENO;
310
0
}
311
312
/* redirect what would usually get written to stderr to specified file */
313
void
314
log_redirect_stderr_to(const char *logfile)
315
0
{
316
0
  int fd;
317
318
0
  if (logfile == NULL) {
319
0
    if (log_stderr_fd != STDERR_FILENO) {
320
0
      close(log_stderr_fd);
321
0
      log_stderr_fd = STDERR_FILENO;
322
0
    }
323
0
    return;
324
0
  }
325
326
0
  if ((fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0600)) == -1) {
327
0
    fprintf(stderr, "Couldn't open logfile %s: %s\n", logfile,
328
0
        strerror(errno));
329
0
    exit(1);
330
0
  }
331
0
  log_stderr_fd = fd;
332
0
}
333
334
#define MSGBUFSIZ 1024
335
336
void
337
set_log_handler(log_handler_fn *handler, void *ctx)
338
0
{
339
0
  log_handler = handler;
340
0
  log_handler_ctx = ctx;
341
0
}
342
343
static void
344
do_log(LogLevel level, int force, const char *suffix, const char *fmt,
345
    va_list args)
346
0
{
347
#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
348
  struct syslog_data sdata = SYSLOG_DATA_INIT;
349
#endif
350
0
  char msgbuf[MSGBUFSIZ];
351
0
  char fmtbuf[MSGBUFSIZ];
352
0
  char *txt = NULL;
353
0
  int pri = LOG_INFO;
354
0
  int saved_errno = errno;
355
0
  log_handler_fn *tmp_handler;
356
0
  const char *progname = argv0 != NULL ? argv0 : __progname;
357
358
0
  if (!force && level > log_level)
359
0
    return;
360
361
0
  switch (level) {
362
0
  case SYSLOG_LEVEL_FATAL:
363
0
    if (!log_on_stderr)
364
0
      txt = "fatal";
365
0
    pri = LOG_CRIT;
366
0
    break;
367
0
  case SYSLOG_LEVEL_ERROR:
368
0
    if (!log_on_stderr)
369
0
      txt = "error";
370
0
    pri = LOG_ERR;
371
0
    break;
372
0
  case SYSLOG_LEVEL_INFO:
373
0
    pri = LOG_INFO;
374
0
    break;
375
0
  case SYSLOG_LEVEL_VERBOSE:
376
0
    pri = LOG_INFO;
377
0
    break;
378
0
  case SYSLOG_LEVEL_DEBUG1:
379
0
    txt = "debug1";
380
0
    pri = LOG_DEBUG;
381
0
    break;
382
0
  case SYSLOG_LEVEL_DEBUG2:
383
0
    txt = "debug2";
384
0
    pri = LOG_DEBUG;
385
0
    break;
386
0
  case SYSLOG_LEVEL_DEBUG3:
387
0
    txt = "debug3";
388
0
    pri = LOG_DEBUG;
389
0
    break;
390
0
  default:
391
0
    txt = "internal error";
392
0
    pri = LOG_ERR;
393
0
    break;
394
0
  }
395
0
  if (txt != NULL && log_handler == NULL) {
396
0
    snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", txt, fmt);
397
0
    vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args);
398
0
  } else {
399
0
    vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
400
0
  }
401
0
  if (suffix != NULL) {
402
0
    snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", msgbuf, suffix);
403
0
    strlcpy(msgbuf, fmtbuf, sizeof(msgbuf));
404
0
  }
405
0
  strnvis(fmtbuf, msgbuf, sizeof(fmtbuf),
406
0
      log_on_stderr ? LOG_STDERR_VIS : LOG_SYSLOG_VIS);
407
0
  if (log_handler != NULL) {
408
    /* Avoid recursion */
409
0
    tmp_handler = log_handler;
410
0
    log_handler = NULL;
411
0
    tmp_handler(level, force, fmtbuf, log_handler_ctx);
412
0
    log_handler = tmp_handler;
413
0
  } else if (log_on_stderr) {
414
0
    snprintf(msgbuf, sizeof msgbuf, "%s%s%.*s\r\n",
415
0
        (log_on_stderr > 1) ? progname : "",
416
0
        (log_on_stderr > 1) ? ": " : "",
417
0
        (int)sizeof msgbuf - 3, fmtbuf);
418
0
    (void)write(log_stderr_fd, msgbuf, strlen(msgbuf));
419
0
  } else {
420
#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
421
    openlog_r(progname, LOG_PID, log_facility, &sdata);
422
    syslog_r(pri, &sdata, "%.500s", fmtbuf);
423
    closelog_r(&sdata);
424
#else
425
0
    openlog(progname, LOG_PID, log_facility);
426
0
    syslog(pri, "%.500s", fmtbuf);
427
0
    closelog();
428
0
#endif
429
0
  }
430
0
  errno = saved_errno;
431
0
}
432
433
void
434
sshlog(const char *file, const char *func, int line, int showfunc,
435
    LogLevel level, const char *suffix, const char *fmt, ...)
436
70
{
437
70
  va_list args;
438
439
70
  va_start(args, fmt);
440
70
  sshlogv(file, func, line, showfunc, level, suffix, fmt, args);
441
70
  va_end(args);
442
70
}
443
444
void
445
sshlogdie(const char *file, const char *func, int line, int showfunc,
446
    LogLevel level, const char *suffix, const char *fmt, ...)
447
0
{
448
0
  va_list args;
449
450
0
  va_start(args, fmt);
451
0
  sshlogv(file, func, line, showfunc, SYSLOG_LEVEL_INFO,
452
0
      suffix, fmt, args);
453
0
  va_end(args);
454
0
  cleanup_exit(255);
455
0
}
456
457
void
458
sshsigdie(const char *file, const char *func, int line, int showfunc,
459
    LogLevel level, const char *suffix, const char *fmt, ...)
460
0
{
461
0
  va_list args;
462
463
0
  va_start(args, fmt);
464
0
  sshlogv(file, func, line, showfunc, SYSLOG_LEVEL_FATAL,
465
0
      suffix, fmt, args);
466
0
  va_end(args);
467
0
  _exit(1);
468
0
}
469
470
void
471
sshlogv(const char *file, const char *func, int line, int showfunc,
472
    LogLevel level, const char *suffix, const char *fmt, va_list args)
473
70
{
474
70
  char tag[128], fmt2[MSGBUFSIZ + 128];
475
70
  int forced = 0;
476
70
  const char *cp;
477
70
  size_t i;
478
479
  /* short circuit processing early if we're not going to log anything */
480
70
  if (nlog_verbose == 0 && level > log_level)
481
70
    return;
482
483
0
  snprintf(tag, sizeof(tag), "%.48s:%.48s():%d (pid=%ld)",
484
0
      (cp = strrchr(file, '/')) == NULL ? file : cp + 1, func, line,
485
0
      (long)getpid());
486
0
  for (i = 0; i < nlog_verbose; i++) {
487
0
    if (match_pattern_list(tag, log_verbose[i], 0) == 1) {
488
0
      forced = 1;
489
0
      break;
490
0
    }
491
0
  }
492
493
0
  if (forced)
494
0
    snprintf(fmt2, sizeof(fmt2), "%s: %s", tag, fmt);
495
0
  else if (showfunc)
496
0
    snprintf(fmt2, sizeof(fmt2), "%s: %s", func, fmt);
497
0
  else
498
0
    strlcpy(fmt2, fmt, sizeof(fmt2));
499
500
0
  do_log(level, forced, suffix, fmt2, args);
501
0
}
502
503
void
504
sshlogdirect(LogLevel level, int forced, const char *fmt, ...)
505
0
{
506
0
  va_list args;
507
508
0
  va_start(args, fmt);
509
0
  do_log(level, forced, NULL, fmt, args);
510
0
  va_end(args);
511
0
}