blob: 34f77b2be0f818928979d52fd54d6caecf560ddb [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 *
3 * log-server.c
4 *
5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
6 *
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
9 *
10 * Created: Mon Mar 20 21:19:30 1995 ylo
11 *
12 * Server-side versions of debug(), log(), etc. These normally send the output
13 * to the system log.
14 *
15 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
17#include "includes.h"
Damien Miller95def091999-11-25 00:26:21 +110018RCSID("$Id: log-server.c,v 1.5 1999/11/24 13:26:22 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100019
20#include <syslog.h>
21#include "packet.h"
22#include "xmalloc.h"
23#include "ssh.h"
24
Damien Miller3f905871999-11-15 17:10:57 +110025#ifdef HAVE___PROGNAME
26extern char *__progname;
27#else /* HAVE___PROGNAME */
28const char *__progname = "sshd";
29#endif /* HAVE___PROGNAME */
30
Damien Miller5ce662a1999-11-11 17:57:39 +110031static LogLevel log_level = SYSLOG_LEVEL_INFO;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032static int log_on_stderr = 0;
Damien Miller2ccf6611999-11-15 15:25:10 +110033static int log_facility = LOG_AUTH;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100034
35/* Initialize the log.
Damien Miller95def091999-11-25 00:26:21 +110036 * av0 program name (should be argv[0])
37 * on_stderr print also on stderr
38 * level logging level
39 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040
Damien Miller95def091999-11-25 00:26:21 +110041void
42log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043{
Damien Miller95def091999-11-25 00:26:21 +110044 switch (level) {
45 case SYSLOG_LEVEL_QUIET:
46 case SYSLOG_LEVEL_ERROR:
47 case SYSLOG_LEVEL_FATAL:
48 case SYSLOG_LEVEL_INFO:
49 case SYSLOG_LEVEL_VERBOSE:
50 case SYSLOG_LEVEL_DEBUG:
51 log_level = level;
52 break;
53 default:
54 fprintf(stderr, "Unrecognized internal syslog level code %d\n",
55 (int) level);
56 exit(1);
57 }
58 switch (facility) {
59 case SYSLOG_FACILITY_DAEMON:
60 log_facility = LOG_DAEMON;
61 break;
62 case SYSLOG_FACILITY_USER:
63 log_facility = LOG_USER;
64 break;
65 case SYSLOG_FACILITY_AUTH:
66 log_facility = LOG_AUTH;
67 break;
68 case SYSLOG_FACILITY_LOCAL0:
69 log_facility = LOG_LOCAL0;
70 break;
71 case SYSLOG_FACILITY_LOCAL1:
72 log_facility = LOG_LOCAL1;
73 break;
74 case SYSLOG_FACILITY_LOCAL2:
75 log_facility = LOG_LOCAL2;
76 break;
77 case SYSLOG_FACILITY_LOCAL3:
78 log_facility = LOG_LOCAL3;
79 break;
80 case SYSLOG_FACILITY_LOCAL4:
81 log_facility = LOG_LOCAL4;
82 break;
83 case SYSLOG_FACILITY_LOCAL5:
84 log_facility = LOG_LOCAL5;
85 break;
86 case SYSLOG_FACILITY_LOCAL6:
87 log_facility = LOG_LOCAL6;
88 break;
89 case SYSLOG_FACILITY_LOCAL7:
90 log_facility = LOG_LOCAL7;
91 break;
92 default:
93 fprintf(stderr, "Unrecognized internal syslog facility code %d\n",
94 (int) facility);
95 exit(1);
96 }
97 log_on_stderr = on_stderr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098}
99
100#define MSGBUFSIZE 1024
101
Damien Miller5ce662a1999-11-11 17:57:39 +1100102void
103do_log(LogLevel level, const char *fmt, va_list args)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104{
Damien Miller95def091999-11-25 00:26:21 +1100105 char msgbuf[MSGBUFSIZE];
106 char fmtbuf[MSGBUFSIZE];
107 char *txt = NULL;
108 int pri = LOG_INFO;
Damien Miller5ce662a1999-11-11 17:57:39 +1100109
Damien Miller95def091999-11-25 00:26:21 +1100110 if (level > log_level)
111 return;
112 switch (level) {
113 case SYSLOG_LEVEL_ERROR:
114 txt = "error";
115 pri = LOG_ERR;
116 break;
117 case SYSLOG_LEVEL_FATAL:
118 txt = "fatal";
119 pri = LOG_ERR;
120 break;
121 case SYSLOG_LEVEL_INFO:
122 case SYSLOG_LEVEL_VERBOSE:
123 pri = LOG_INFO;
124 break;
125 case SYSLOG_LEVEL_DEBUG:
126 txt = "debug";
127 pri = LOG_DEBUG;
128 break;
129 default:
130 txt = "internal error";
131 pri = LOG_ERR;
132 break;
133 }
134 if (txt != NULL) {
135 snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", txt, fmt);
136 vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args);
137 } else {
138 vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
139 }
140 if (log_on_stderr)
141 fprintf(stderr, "%s\n", msgbuf);
142 openlog(__progname, LOG_PID, log_facility);
143 syslog(pri, "%.500s", msgbuf);
144 closelog();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145}