blob: 9db77d9e06cbf12e787a866f45ff5605e2ae5016 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller4af51302000-04-16 11:18:38 +10002 *
Damien Miller95def091999-11-25 00:26:21 +11003 * log-server.c
Damien Miller4af51302000-04-16 11:18:38 +10004 *
Damien Miller95def091999-11-25 00:26:21 +11005 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Miller95def091999-11-25 00:26:21 +11007 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Miller95def091999-11-25 00:26:21 +110010 * Created: Mon Mar 20 21:19:30 1995 ylo
Damien Miller4af51302000-04-16 11:18:38 +100011 *
Damien Miller95def091999-11-25 00:26:21 +110012 * Server-side versions of debug(), log(), etc. These normally send the output
13 * to the system log.
Damien Miller4af51302000-04-16 11:18:38 +100014 *
Damien Miller95def091999-11-25 00:26:21 +110015 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
17#include "includes.h"
Damien Miller6536c7d2000-06-22 21:32:31 +100018RCSID("$OpenBSD: log-server.c,v 1.15 2000/06/20 01:39:42 markus 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 */
Damien Miller0437b332000-05-02 09:56:41 +100028static const char *__progname = "sshd";
Damien Miller3f905871999-11-15 17:10:57 +110029#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 Miller4af51302000-04-16 11:18:38 +100041void
Damien Miller95def091999-11-25 00:26:21 +110042log_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
Damien Miller98c7ad62000-03-09 21:27:49 +1100100#define MSGBUFSIZ 1024
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101
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 Miller98c7ad62000-03-09 21:27:49 +1100105 char msgbuf[MSGBUFSIZ];
106 char fmtbuf[MSGBUFSIZ];
Damien Miller95def091999-11-25 00:26:21 +1100107 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 }
Damien Millerb38eff82000-04-01 11:09:21 +1000140 if (log_on_stderr) {
Damien Miller95def091999-11-25 00:26:21 +1100141 fprintf(stderr, "%s\n", msgbuf);
Damien Millerb38eff82000-04-01 11:09:21 +1000142 } else {
143 openlog(__progname, LOG_PID, log_facility);
144 syslog(pri, "%.500s", msgbuf);
145 closelog();
146 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147}