]> andersk Git - openssh.git/blob - log-server.c
change my e-mail to a portable one.
[openssh.git] / log-server.c
1 /*
2  * Author: Tatu Ylonen <ylo@cs.hut.fi>
3  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4  *                    All rights reserved
5  * Server-side versions of debug(), log(), etc.  These normally send the output
6  * to the system log.
7  *
8  * As far as I am concerned, the code I have written for this software
9  * can be used freely for any purpose.  Any derived versions of this
10  * software must be clearly marked as such, and if the derived work is
11  * incompatible with the protocol description in the RFC file, it must be
12  * called by a name other than "ssh" or "Secure Shell".
13  *
14  *
15  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 #include "includes.h"
39 RCSID("$OpenBSD: log-server.c,v 1.17 2000/09/12 20:53:10 markus Exp $");
40
41 #include <syslog.h>
42 #include "packet.h"
43 #include "xmalloc.h"
44 #include "ssh.h"
45
46 static LogLevel log_level = SYSLOG_LEVEL_INFO;
47 static int log_on_stderr = 0;
48 static int log_facility = LOG_AUTH;
49
50 /* Initialize the log.
51  *   av0        program name (should be argv[0])
52  *   on_stderr  print also on stderr
53  *   level      logging level
54  */
55
56 void
57 log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr)
58 {
59         switch (level) {
60         case SYSLOG_LEVEL_QUIET:
61         case SYSLOG_LEVEL_ERROR:
62         case SYSLOG_LEVEL_FATAL:
63         case SYSLOG_LEVEL_INFO:
64         case SYSLOG_LEVEL_VERBOSE:
65         case SYSLOG_LEVEL_DEBUG1:
66         case SYSLOG_LEVEL_DEBUG2:
67         case SYSLOG_LEVEL_DEBUG3:
68                 log_level = level;
69                 break;
70         default:
71                 fprintf(stderr, "Unrecognized internal syslog level code %d\n",
72                         (int) level);
73                 exit(1);
74         }
75         switch (facility) {
76         case SYSLOG_FACILITY_DAEMON:
77                 log_facility = LOG_DAEMON;
78                 break;
79         case SYSLOG_FACILITY_USER:
80                 log_facility = LOG_USER;
81                 break;
82         case SYSLOG_FACILITY_AUTH:
83                 log_facility = LOG_AUTH;
84                 break;
85 #ifdef LOG_AUTHPRIV
86         case SYSLOG_FACILITY_AUTHPRIV:
87                 log_facility = LOG_AUTHPRIV;
88                 break;
89 #endif
90         case SYSLOG_FACILITY_LOCAL0:
91                 log_facility = LOG_LOCAL0;
92                 break;
93         case SYSLOG_FACILITY_LOCAL1:
94                 log_facility = LOG_LOCAL1;
95                 break;
96         case SYSLOG_FACILITY_LOCAL2:
97                 log_facility = LOG_LOCAL2;
98                 break;
99         case SYSLOG_FACILITY_LOCAL3:
100                 log_facility = LOG_LOCAL3;
101                 break;
102         case SYSLOG_FACILITY_LOCAL4:
103                 log_facility = LOG_LOCAL4;
104                 break;
105         case SYSLOG_FACILITY_LOCAL5:
106                 log_facility = LOG_LOCAL5;
107                 break;
108         case SYSLOG_FACILITY_LOCAL6:
109                 log_facility = LOG_LOCAL6;
110                 break;
111         case SYSLOG_FACILITY_LOCAL7:
112                 log_facility = LOG_LOCAL7;
113                 break;
114         default:
115                 fprintf(stderr, "Unrecognized internal syslog facility code %d\n",
116                         (int) facility);
117                 exit(1);
118         }
119         log_on_stderr = on_stderr;
120 }
121
122 #define MSGBUFSIZ 1024
123
124 void
125 do_log(LogLevel level, const char *fmt, va_list args)
126 {
127         char msgbuf[MSGBUFSIZ];
128         char fmtbuf[MSGBUFSIZ];
129         char *txt = NULL;
130         int pri = LOG_INFO;
131         extern char *__progname;
132
133         if (level > log_level)
134                 return;
135         switch (level) {
136         case SYSLOG_LEVEL_ERROR:
137                 txt = "error";
138                 pri = LOG_ERR;
139                 break;
140         case SYSLOG_LEVEL_FATAL:
141                 txt = "fatal";
142                 pri = LOG_ERR;
143                 break;
144         case SYSLOG_LEVEL_INFO:
145         case SYSLOG_LEVEL_VERBOSE:
146                 pri = LOG_INFO;
147                 break;
148         case SYSLOG_LEVEL_DEBUG1:
149                 txt = "debug1";
150                 pri = LOG_DEBUG;
151                 break;
152         case SYSLOG_LEVEL_DEBUG2:
153                 txt = "debug2";
154                 pri = LOG_DEBUG;
155                 break;
156         case SYSLOG_LEVEL_DEBUG3:
157                 txt = "debug3";
158                 pri = LOG_DEBUG;
159                 break;
160         default:
161                 txt = "internal error";
162                 pri = LOG_ERR;
163                 break;
164         }
165         if (txt != NULL) {
166                 snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", txt, fmt);
167                 vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args);
168         } else {
169                 vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
170         }
171         if (log_on_stderr) {
172                 fprintf(stderr, "%s\n", msgbuf);
173         } else {
174                 openlog(__progname, LOG_PID, log_facility);
175                 syslog(pri, "%.500s", msgbuf);
176                 closelog();
177         }
178 }
This page took 0.141152 seconds and 5 git commands to generate.