summaryrefslogtreecommitdiff
path: root/log-server.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>1999-11-11 17:57:39 +1100
committerDamien Miller <djm@mindrot.org>1999-11-11 17:57:39 +1100
commit5ce662a9202240a2f5fa6a9334d58186bdaba50c (patch)
tree9fe37122fa27f070abc3c9c28531877d43673b7f /log-server.c
parentab5e0d0c27e00dca463c67395c2b5941e778836e (diff)
- Merged more OpenBSD CVS changes:
- [auth-krb4.c auth-passwd.c] remove x11- and krb-cleanup from fatal() + krb-cleanup cleanup - [clientloop.c log-client.c log-server.c ] [readconf.c readconf.h servconf.c servconf.h ] [ssh.1 ssh.c ssh.h sshd.8] add LogLevel {QUIET, FATAL, ERROR, INFO, CHAT, DEBUG} to ssh/sshd, obsoletes QuietMode and FascistLogging in sshd.
Diffstat (limited to 'log-server.c')
-rw-r--r--log-server.c202
1 files changed, 58 insertions, 144 deletions
diff --git a/log-server.c b/log-server.c
index fce96b01d..6642dbedf 100644
--- a/log-server.c
+++ b/log-server.c
@@ -15,29 +15,42 @@ to the system log.
15*/ 15*/
16 16
17#include "includes.h" 17#include "includes.h"
18RCSID("$Id: log-server.c,v 1.1 1999/10/27 03:42:44 damien Exp $"); 18RCSID("$Id: log-server.c,v 1.2 1999/11/11 06:57:39 damien Exp $");
19 19
20#include <syslog.h> 20#include <syslog.h>
21#include "packet.h" 21#include "packet.h"
22#include "xmalloc.h" 22#include "xmalloc.h"
23#include "ssh.h" 23#include "ssh.h"
24 24
25static int log_debug = 0; 25static LogLevel log_level = SYSLOG_LEVEL_INFO;
26static int log_quiet = 0;
27static int log_on_stderr = 0; 26static int log_on_stderr = 0;
28 27
29/* Initialize the log. 28/* Initialize the log.
30 av0 program name (should be argv[0]) 29 av0 program name (should be argv[0])
31 on_stderr print also on stderr 30 on_stderr print also on stderr
32 debug send debugging messages to system log 31 level logging level
33 quiet don\'t log anything
34 */ 32 */
35 33
36void log_init(char *av0, int on_stderr, int debug, int quiet, 34void log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr)
37 SyslogFacility facility)
38{ 35{
39 int log_facility; 36 int log_facility;
40 37
38 switch (level)
39 {
40 case SYSLOG_LEVEL_QUIET:
41 case SYSLOG_LEVEL_ERROR:
42 case SYSLOG_LEVEL_FATAL:
43 case SYSLOG_LEVEL_INFO:
44 case SYSLOG_LEVEL_CHAT:
45 case SYSLOG_LEVEL_DEBUG:
46 log_level = level;
47 break;
48 default:
49 fprintf(stderr, "Unrecognized internal syslog level code %d\n",
50 (int)level);
51 exit(1);
52 }
53
41 switch (facility) 54 switch (facility)
42 { 55 {
43 case SYSLOG_FACILITY_DAEMON: 56 case SYSLOG_FACILITY_DAEMON:
@@ -79,8 +92,6 @@ void log_init(char *av0, int on_stderr, int debug, int quiet,
79 exit(1); 92 exit(1);
80 } 93 }
81 94
82 log_debug = debug;
83 log_quiet = quiet;
84 log_on_stderr = on_stderr; 95 log_on_stderr = on_stderr;
85 closelog(); /* Close any previous log. */ 96 closelog(); /* Close any previous log. */
86 openlog(av0, LOG_PID, log_facility); 97 openlog(av0, LOG_PID, log_facility);
@@ -88,146 +99,49 @@ void log_init(char *av0, int on_stderr, int debug, int quiet,
88 99
89#define MSGBUFSIZE 1024 100#define MSGBUFSIZE 1024
90 101
91#define DECL_MSGBUF char msgbuf[MSGBUFSIZE] 102void
92 103do_log(LogLevel level, const char *fmt, va_list args)
93/* Log this message (information that usually should go to the log). */
94
95void log(const char *fmt, ...)
96{
97 va_list args;
98 DECL_MSGBUF;
99 if (log_quiet)
100 return;
101 va_start(args, fmt);
102 vsnprintf(msgbuf, MSGBUFSIZE, fmt, args);
103 va_end(args);
104 if (log_on_stderr)
105 fprintf(stderr, "log: %s\n", msgbuf);
106 syslog(LOG_INFO, "log: %.500s", msgbuf);
107}
108
109/* Debugging messages that should not be logged during normal operation. */
110
111void debug(const char *fmt, ...)
112{ 104{
113 va_list args; 105 char msgbuf[MSGBUFSIZE];
114 DECL_MSGBUF; 106 char fmtbuf[MSGBUFSIZE];
115 if (!log_debug || log_quiet) 107 char *txt = NULL;
116 return; 108 int pri = LOG_INFO;
117 va_start(args, fmt);
118 vsnprintf(msgbuf, MSGBUFSIZE, fmt, args);
119 va_end(args);
120 if (log_on_stderr)
121 fprintf(stderr, "debug: %s\n", msgbuf);
122 syslog(LOG_DEBUG, "debug: %.500s", msgbuf);
123}
124 109
125/* Error messages that should be logged. */ 110 if (level > log_level)
126
127void error(const char *fmt, ...)
128{
129 va_list args;
130 DECL_MSGBUF;
131 if (log_quiet)
132 return; 111 return;
133 va_start(args, fmt); 112 switch (level)
134 vsnprintf(msgbuf, MSGBUFSIZE, fmt, args);
135 va_end(args);
136 if (log_on_stderr)
137 fprintf(stderr, "error: %s\n", msgbuf);
138 syslog(LOG_ERR, "error: %.500s", msgbuf);
139}
140
141struct fatal_cleanup
142{
143 struct fatal_cleanup *next;
144 void (*proc)(void *);
145 void *context;
146};
147
148static struct fatal_cleanup *fatal_cleanups = NULL;
149
150/* Registers a cleanup function to be called by fatal() before exiting. */
151
152void fatal_add_cleanup(void (*proc)(void *), void *context)
153{
154 struct fatal_cleanup *cu;
155
156 cu = xmalloc(sizeof(*cu));
157 cu->proc = proc;
158 cu->context = context;
159 cu->next = fatal_cleanups;
160 fatal_cleanups = cu;
161}
162
163/* Removes a cleanup frunction to be called at fatal(). */
164
165void fatal_remove_cleanup(void (*proc)(void *context), void *context)
166{
167 struct fatal_cleanup **cup, *cu;
168
169 for (cup = &fatal_cleanups; *cup; cup = &cu->next)
170 { 113 {
171 cu = *cup; 114 case SYSLOG_LEVEL_ERROR:
172 if (cu->proc == proc && cu->context == context) 115 txt = "error";
173 { 116 pri = LOG_ERR;
174 *cup = cu->next; 117 break;
175 xfree(cu); 118 case SYSLOG_LEVEL_FATAL:
176 return; 119 txt = "fatal";
177 } 120 pri = LOG_ERR;
121 break;
122 case SYSLOG_LEVEL_INFO:
123 pri = LOG_INFO;
124 break;
125 case SYSLOG_LEVEL_CHAT:
126 pri = LOG_INFO;
127 break;
128 case SYSLOG_LEVEL_DEBUG:
129 txt = "debug";
130 pri = LOG_DEBUG;
131 break;
132 default:
133 txt = "internal error";
134 pri = LOG_ERR;
135 break;
178 } 136 }
179 fatal("fatal_remove_cleanup: no such cleanup function: 0x%lx 0x%lx\n",
180 (unsigned long)proc, (unsigned long)context);
181}
182
183/* Fatal messages. This function never returns. */
184 137
185void fatal(const char *fmt, ...) 138 if (txt != NULL) {
186{ 139 snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", txt, fmt);
187 va_list args; 140 vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args);
188 struct fatal_cleanup *cu, *next_cu; 141 }else{
189 static int fatal_called = 0; 142 vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
190#if defined(KRB4) 143 }
191 extern char *ticket;
192#endif /* KRB4 */
193 DECL_MSGBUF;
194
195 if (log_quiet)
196 exit(1);
197 va_start(args, fmt);
198 vsnprintf(msgbuf, MSGBUFSIZE, fmt, args);
199 va_end(args);
200 if (log_on_stderr) 144 if (log_on_stderr)
201 fprintf(stderr, "fatal: %s\n", msgbuf); 145 fprintf(stderr, "%s\n", msgbuf);
202 syslog(LOG_ERR, "fatal: %.500s", msgbuf); 146 syslog(pri, "%.500s", msgbuf);
203
204 if (fatal_called)
205 exit(1);
206 fatal_called = 1;
207
208 /* Call cleanup functions. */
209 for (cu = fatal_cleanups; cu; cu = next_cu)
210 {
211 next_cu = cu->next;
212 debug("Calling cleanup 0x%lx(0x%lx)",
213 (unsigned long)cu->proc, (unsigned long)cu->context);
214 (*cu->proc)(cu->context);
215 }
216#if defined(KRB4)
217 /* If you forwarded a ticket you get one shot for proper
218 authentication. */
219 /* If tgt was passed unlink file */
220 if (ticket)
221 {
222 if (strcmp(ticket,"none"))
223 unlink(ticket);
224 else
225 ticket = NULL;
226 }
227#endif /* KRB4 */
228
229 /* If local XAUTHORITY was created, remove it. */
230 if (xauthfile) unlink(xauthfile);
231
232 exit(1);
233} 147}