root/cserver/wlog.c

Revision 1203, 7.5 kB (checked in by efphe, 1 year ago)

moving code from fenilot.org

Line 
1 /*
2  * Note:
3  *  This file is stolen from openssh.
4  *  I needed to hack it a little bit.
5  *
6  *      Federico Tomassini AKA efphe (effetom AT gmail DOT com)
7  */
8
9 /*
10  * Author: Tatu Ylonen <ylo@cs.hut.fi>
11  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
12  *                    All rights reserved
13  *
14  * As far as I am concerned, the code I have written for this software
15  * can be used freely for any purpose.  Any derived versions of this
16  * software must be clearly marked as such, and if the derived work is
17  * incompatible with the protocol description in the RFC file, it must be
18  * called by a name other than "ssh" or "Secure Shell".
19  */
20 /*
21  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the above copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
33  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
35  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
36  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
41  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  */
43
44
45 #include "wlog.h"
46
47 #include <syslog.h>
48 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H)
49 #endif
50
51 static LogLevel log_level = SYSLOG_LEVEL_INFO;
52 static int log_on_stderr = 1;
53 static int log_facility = LOG_DAEMON;
54 static char *argv0;
55
56 extern char *__progname;
57
58 #define LOG_SYSLOG_VIS  (VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL)
59 #define LOG_STDERR_VIS  (VIS_SAFE|VIS_OCTAL)
60
61 /* textual representation of log-facilities/levels
62
63 static struct {
64         const char *name;
65         SyslogFacility val;
66 } log_facilities[] = {
67         { "DAEMON",     SYSLOG_FACILITY_DAEMON },
68         { "USER",       SYSLOG_FACILITY_USER },
69         { "AUTH",       SYSLOG_FACILITY_AUTH },
70 #ifdef LOG_AUTHPRIV
71         { "AUTHPRIV",   SYSLOG_FACILITY_AUTHPRIV },
72 #endif
73         { "LOCAL0",     SYSLOG_FACILITY_LOCAL0 },
74         { "LOCAL1",     SYSLOG_FACILITY_LOCAL1 },
75         { "LOCAL2",     SYSLOG_FACILITY_LOCAL2 },
76         { "LOCAL3",     SYSLOG_FACILITY_LOCAL3 },
77         { "LOCAL4",     SYSLOG_FACILITY_LOCAL4 },
78         { "LOCAL5",     SYSLOG_FACILITY_LOCAL5 },
79         { "LOCAL6",     SYSLOG_FACILITY_LOCAL6 },
80         { "LOCAL7",     SYSLOG_FACILITY_LOCAL7 },
81         { NULL,         SYSLOG_FACILITY_NOT_SET }
82 };
83
84 static struct {
85         const char *name;
86         LogLevel val;
87 } log_levels[] =
88 {
89         { "QUIET",      SYSLOG_LEVEL_QUIET },
90         { "FATAL",      SYSLOG_LEVEL_FATAL },
91         { "ERROR",      SYSLOG_LEVEL_ERROR },
92         { "INFO",       SYSLOG_LEVEL_INFO },
93         { "VERBOSE",    SYSLOG_LEVEL_VERBOSE },
94         { "DEBUG",      SYSLOG_LEVEL_DEBUG1 },
95         { "DEBUG1",     SYSLOG_LEVEL_DEBUG1 },
96         { "DEBUG2",     SYSLOG_LEVEL_DEBUG2 },
97         { "DEBUG3",     SYSLOG_LEVEL_DEBUG3 },
98         { NULL,         SYSLOG_LEVEL_NOT_SET }
99 }; */
100
101 /* Error messages that should be logged. */
102
103 void
104 error(const char *fmt,...)
105 {
106         va_list args;
107
108         va_start(args, fmt);
109         do_log(SYSLOG_LEVEL_ERROR, fmt, args);
110         va_end(args);
111 }
112
113 /* Log this message (information that usually should go to the log). */
114
115 void
116 logit(const char *fmt,...)
117 {
118         va_list args;
119
120         va_start(args, fmt);
121         do_log(SYSLOG_LEVEL_INFO, fmt, args);
122         va_end(args);
123 }
124
125 /* More detailed messages (information that does not need to go to the log). */
126
127 void
128 verbose(const char *fmt,...)
129 {
130         va_list args;
131
132         va_start(args, fmt);
133         do_log(SYSLOG_LEVEL_VERBOSE, fmt, args);
134         va_end(args);
135 }
136
137 /* Debugging messages that should not be logged during normal operation. */
138
139 void
140 debug(const char *fmt,...)
141 {
142         va_list args;
143
144         va_start(args, fmt);
145         do_log(SYSLOG_LEVEL_DEBUG1, fmt, args);
146         va_end(args);
147 }
148
149 void
150 debug2(const char *fmt,...)
151 {
152         va_list args;
153
154         va_start(args, fmt);
155         do_log(SYSLOG_LEVEL_DEBUG2, fmt, args);
156         va_end(args);
157 }
158
159 void
160 debug3(const char *fmt,...)
161 {
162         va_list args;
163
164         va_start(args, fmt);
165         do_log(SYSLOG_LEVEL_DEBUG3, fmt, args);
166         va_end(args);
167 }
168
169 void
170 fatal(const char *fmt,...)
171 {
172     va_list args;
173     va_start(args, fmt);
174     do_log(SYSLOG_LEVEL_FATAL, fmt, args);
175     va_end(args);
176     kill(getpid(), SIGINT);
177 }
178
179 /*
180  * Initialize the log.
181  */
182
183 void
184 log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr)
185 {
186         argv0 = av0;
187
188         switch (level) {
189         case SYSLOG_LEVEL_QUIET:
190         case SYSLOG_LEVEL_FATAL:
191         case SYSLOG_LEVEL_ERROR:
192         case SYSLOG_LEVEL_INFO:
193         case SYSLOG_LEVEL_VERBOSE:
194         case SYSLOG_LEVEL_DEBUG1:
195         case SYSLOG_LEVEL_DEBUG2:
196         case SYSLOG_LEVEL_DEBUG3:
197                 log_level = level;
198                 break;
199         default:
200                 fprintf(stderr, "Unrecognized internal syslog level code %d\n",
201                     (int) level);
202                 exit(1);
203         }
204
205         log_on_stderr = on_stderr;
206         if (on_stderr)
207                 return;
208
209         switch (facility) {
210         case SYSLOG_FACILITY_DAEMON:
211                 log_facility = LOG_DAEMON;
212                 break;
213         case SYSLOG_FACILITY_USER:
214                 log_facility = LOG_USER;
215                 break;
216         case SYSLOG_FACILITY_AUTH:
217                 log_facility = LOG_AUTH;
218                 break;
219 #ifdef LOG_AUTHPRIV
220         case SYSLOG_FACILITY_AUTHPRIV:
221                 log_facility = LOG_AUTHPRIV;
222                 break;
223 #endif
224         case SYSLOG_FACILITY_LOCAL0:
225                 log_facility = LOG_LOCAL0;
226                 break;
227         case SYSLOG_FACILITY_LOCAL1:
228                 log_facility = LOG_LOCAL1;
229                 break;
230         case SYSLOG_FACILITY_LOCAL2:
231                 log_facility = LOG_LOCAL2;
232                 break;
233         case SYSLOG_FACILITY_LOCAL3:
234                 log_facility = LOG_LOCAL3;
235                 break;
236         case SYSLOG_FACILITY_LOCAL4:
237                 log_facility = LOG_LOCAL4;
238                 break;
239         case SYSLOG_FACILITY_LOCAL5:
240                 log_facility = LOG_LOCAL5;
241                 break;
242         case SYSLOG_FACILITY_LOCAL6:
243                 log_facility = LOG_LOCAL6;
244                 break;
245         case SYSLOG_FACILITY_LOCAL7:
246                 log_facility = LOG_LOCAL7;
247                 break;
248         default:
249                 fprintf(stderr,
250                     "Unrecognized internal syslog facility code %d\n",
251                     (int) facility);
252                 exit(1);
253         }
254
255         /*
256          * If an external library (eg libwrap) attempts to use syslog
257          * immediately after reexec, syslog may be pointing to the wrong
258          * facility, so we force an open/close of syslog here.
259          */
260         openlog(argv0, LOG_PID, log_facility);
261         closelog();
262 }
263
264 #define MSGBUFSIZ 1024
265
266 void
267 do_log(LogLevel level, const char *fmt, va_list args)
268 {
269         char msgbuf[MSGBUFSIZ];
270         char fmtbuf[MSGBUFSIZ];
271         char *txt = NULL;
272         int pri = LOG_INFO;
273
274         if (level > log_level)
275                 return;
276
277         switch (level) {
278         case SYSLOG_LEVEL_FATAL:
279                 if (!log_on_stderr)
280                         txt = "fatal";
281                 pri = LOG_CRIT;
282                 break;
283         case SYSLOG_LEVEL_ERROR:
284                 if (!log_on_stderr)
285                         txt = "error";
286                 pri = LOG_ERR;
287                 break;
288         case SYSLOG_LEVEL_INFO:
289                 pri = LOG_INFO;
290                 break;
291         case SYSLOG_LEVEL_VERBOSE:
292                 pri = LOG_INFO;
293                 break;
294         case SYSLOG_LEVEL_DEBUG1:
295                 txt = "debug1";
296                 pri = LOG_DEBUG;
297                 break;
298         case SYSLOG_LEVEL_DEBUG2:
299                 txt = "debug2";
300                 pri = LOG_DEBUG;
301                 break;
302         case SYSLOG_LEVEL_DEBUG3:
303                 txt = "debug3";
304                 pri = LOG_DEBUG;
305                 break;
306         default:
307                 txt = "internal error";
308                 pri = LOG_ERR;
309                 break;
310         }
311         if (txt != NULL) {
312                 snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", txt, fmt);
313                 vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args);
314         } else {
315                 vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
316         }
317         if (log_on_stderr) {
318                 snprintf(fmtbuf, sizeof fmtbuf, "%s\r\n", msgbuf);
319                 write(STDERR_FILENO, fmtbuf, strlen(fmtbuf));
320         } else {
321                 openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
322                 syslog(pri, "%.500s", msgbuf);
323                 closelog();
324         }
325 }
Note: See TracBrowser for help on using the browser.