1 | /* |
---|
2 | |
---|
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_msg(), etc. These normally send the |
---|
13 | output to the system log. |
---|
14 | |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * $Id: log-server.c,v 1.4 1999-03-08 18:20:06 danw Exp $ |
---|
19 | * $Log: not supported by cvs2svn $ |
---|
20 | * Revision 1.3 1998/01/24 01:47:25 danw |
---|
21 | * merge in changes for 1.2.22 |
---|
22 | * |
---|
23 | * Revision 1.2 1997/11/12 21:16:16 danw |
---|
24 | * Athena-login changes (including some krb4 stuff) |
---|
25 | * |
---|
26 | * Revision 1.1.1.1 1997/10/17 22:26:02 danw |
---|
27 | * Import of ssh 1.2.21 |
---|
28 | * |
---|
29 | * Revision 1.1.1.2 1998/01/24 01:25:21 danw |
---|
30 | * Import of ssh 1.2.22 |
---|
31 | * |
---|
32 | * Revision 1.1.1.3 1999/03/08 17:43:06 danw |
---|
33 | * Import of ssh 1.2.26 |
---|
34 | * |
---|
35 | * Revision 1.6 1998/05/23 20:21:43 kivinen |
---|
36 | * Changed () -> (void). |
---|
37 | * |
---|
38 | * Revision 1.5 1998/01/02 06:18:49 kivinen |
---|
39 | * Fixed kerberos ticket name handling. |
---|
40 | * |
---|
41 | * Revision 1.4 1997/04/17 04:05:51 kivinen |
---|
42 | * Added return to end of syslog_severity to remove warning about |
---|
43 | * it. |
---|
44 | * |
---|
45 | * Revision 1.3 1997/03/27 03:09:58 kivinen |
---|
46 | * Added kerberos patches from Glenn Machin. |
---|
47 | * |
---|
48 | * Revision 1.2 1996/10/29 22:38:23 kivinen |
---|
49 | * log -> log_msg. |
---|
50 | * |
---|
51 | * Revision 1.1.1.1 1996/02/18 21:38:12 ylo |
---|
52 | * Imported ssh-1.2.13. |
---|
53 | * |
---|
54 | * Revision 1.5 1995/10/02 01:22:57 ylo |
---|
55 | * Include sys/syslog.h if needed. |
---|
56 | * |
---|
57 | * Revision 1.4 1995/09/09 21:26:42 ylo |
---|
58 | * /m/shadows/u2/users/ylo/ssh/README |
---|
59 | * |
---|
60 | * Revision 1.3 1995/08/21 23:25:00 ylo |
---|
61 | * Added support for syslog facility. |
---|
62 | * |
---|
63 | * Revision 1.2 1995/07/13 01:26:21 ylo |
---|
64 | * Removed "Last modified" header. |
---|
65 | * Added cvs log. |
---|
66 | * |
---|
67 | * $Endlog$ |
---|
68 | */ |
---|
69 | |
---|
70 | #include "includes.h" |
---|
71 | #include <syslog.h> |
---|
72 | #ifdef NEED_SYS_SYSLOG_H |
---|
73 | #include <sys/syslog.h> |
---|
74 | #endif /* NEED_SYS_SYSLOG_H */ |
---|
75 | #include "packet.h" |
---|
76 | #include "xmalloc.h" |
---|
77 | #include "ssh.h" |
---|
78 | |
---|
79 | static int log_debug = 0; |
---|
80 | static int log_quiet = 0; |
---|
81 | static int log_on_stderr = 0; |
---|
82 | |
---|
83 | #ifdef KERBEROS |
---|
84 | extern krb5_context ssh_context; |
---|
85 | #endif |
---|
86 | |
---|
87 | /* Initialize the log. |
---|
88 | av0 program name (should be argv[0]) |
---|
89 | on_stderr print also on stderr |
---|
90 | debug send debugging messages to system log |
---|
91 | quiet don\'t log anything |
---|
92 | */ |
---|
93 | |
---|
94 | void log_init(char *av0, int on_stderr, int debug, int quiet, |
---|
95 | SyslogFacility facility) |
---|
96 | { |
---|
97 | int log_facility; |
---|
98 | |
---|
99 | switch (facility) |
---|
100 | { |
---|
101 | case SYSLOG_FACILITY_DAEMON: |
---|
102 | log_facility = LOG_DAEMON; |
---|
103 | break; |
---|
104 | case SYSLOG_FACILITY_USER: |
---|
105 | log_facility = LOG_USER; |
---|
106 | break; |
---|
107 | case SYSLOG_FACILITY_AUTH: |
---|
108 | log_facility = LOG_AUTH; |
---|
109 | break; |
---|
110 | case SYSLOG_FACILITY_LOCAL0: |
---|
111 | log_facility = LOG_LOCAL0; |
---|
112 | break; |
---|
113 | case SYSLOG_FACILITY_LOCAL1: |
---|
114 | log_facility = LOG_LOCAL1; |
---|
115 | break; |
---|
116 | case SYSLOG_FACILITY_LOCAL2: |
---|
117 | log_facility = LOG_LOCAL2; |
---|
118 | break; |
---|
119 | case SYSLOG_FACILITY_LOCAL3: |
---|
120 | log_facility = LOG_LOCAL3; |
---|
121 | break; |
---|
122 | case SYSLOG_FACILITY_LOCAL4: |
---|
123 | log_facility = LOG_LOCAL4; |
---|
124 | break; |
---|
125 | case SYSLOG_FACILITY_LOCAL5: |
---|
126 | log_facility = LOG_LOCAL5; |
---|
127 | break; |
---|
128 | case SYSLOG_FACILITY_LOCAL6: |
---|
129 | log_facility = LOG_LOCAL6; |
---|
130 | break; |
---|
131 | case SYSLOG_FACILITY_LOCAL7: |
---|
132 | log_facility = LOG_LOCAL7; |
---|
133 | break; |
---|
134 | default: |
---|
135 | fprintf(stderr, "Unrecognized internal syslog facility code %d\n", |
---|
136 | (int)facility); |
---|
137 | exit(1); |
---|
138 | } |
---|
139 | |
---|
140 | log_debug = debug; |
---|
141 | log_quiet = quiet; |
---|
142 | log_on_stderr = on_stderr; |
---|
143 | closelog(); /* Close any previous log. */ |
---|
144 | openlog(av0, LOG_PID, log_facility); |
---|
145 | } |
---|
146 | |
---|
147 | /* Log this message (information that usually should go to the log). */ |
---|
148 | |
---|
149 | void log_msg(const char *fmt, ...) |
---|
150 | { |
---|
151 | char buf[1024]; |
---|
152 | va_list args; |
---|
153 | if (log_quiet) |
---|
154 | return; |
---|
155 | va_start(args, fmt); |
---|
156 | vsprintf(buf, fmt, args); |
---|
157 | va_end(args); |
---|
158 | if (log_on_stderr) |
---|
159 | fprintf(stderr, "log: %s\n", buf); |
---|
160 | syslog(LOG_INFO, "log: %.500s", buf); |
---|
161 | } |
---|
162 | |
---|
163 | /* Converts portable syslog severity to machine-specific syslog severity. */ |
---|
164 | |
---|
165 | static int syslog_severity(int severity) |
---|
166 | { |
---|
167 | switch (severity) |
---|
168 | { |
---|
169 | case SYSLOG_SEVERITY_DEBUG: |
---|
170 | return LOG_DEBUG; |
---|
171 | case SYSLOG_SEVERITY_INFO: |
---|
172 | return LOG_INFO; |
---|
173 | case SYSLOG_SEVERITY_NOTICE: |
---|
174 | return LOG_NOTICE; |
---|
175 | case SYSLOG_SEVERITY_WARNING: |
---|
176 | return LOG_WARNING; |
---|
177 | case SYSLOG_SEVERITY_ERR: |
---|
178 | return LOG_ERR; |
---|
179 | case SYSLOG_SEVERITY_CRIT: |
---|
180 | return LOG_CRIT; |
---|
181 | default: |
---|
182 | fatal("syslog_severity: bad severity %d", severity); |
---|
183 | } |
---|
184 | return 0; |
---|
185 | } |
---|
186 | |
---|
187 | /* Log this message (information that usually should go to the log) at |
---|
188 | the given severity level. */ |
---|
189 | |
---|
190 | void log_severity(SyslogSeverity severity, const char *fmt, ...) |
---|
191 | { |
---|
192 | char buf[1024]; |
---|
193 | va_list args; |
---|
194 | if (log_quiet) |
---|
195 | return; |
---|
196 | va_start(args, fmt); |
---|
197 | vsprintf(buf, fmt, args); |
---|
198 | va_end(args); |
---|
199 | if (log_on_stderr) |
---|
200 | fprintf(stderr, "log: %s\n", buf); |
---|
201 | syslog(syslog_severity(severity), "log: %.500s", buf); |
---|
202 | } |
---|
203 | |
---|
204 | /* Debugging messages that should not be logged during normal operation. */ |
---|
205 | |
---|
206 | void debug(const char *fmt, ...) |
---|
207 | { |
---|
208 | char buf[1024]; |
---|
209 | va_list args; |
---|
210 | if (!log_debug || log_quiet) |
---|
211 | return; |
---|
212 | va_start(args, fmt); |
---|
213 | vsprintf(buf, fmt, args); |
---|
214 | va_end(args); |
---|
215 | if (log_on_stderr) |
---|
216 | fprintf(stderr, "debug: %s\n", buf); |
---|
217 | syslog(LOG_DEBUG, "debug: %.500s", buf); |
---|
218 | } |
---|
219 | |
---|
220 | /* Error messages that should be logged. */ |
---|
221 | |
---|
222 | void error(const char *fmt, ...) |
---|
223 | { |
---|
224 | char buf[1024]; |
---|
225 | va_list args; |
---|
226 | if (log_quiet) |
---|
227 | return; |
---|
228 | va_start(args, fmt); |
---|
229 | vsprintf(buf, fmt, args); |
---|
230 | va_end(args); |
---|
231 | if (log_on_stderr) |
---|
232 | fprintf(stderr, "error: %s\n", buf); |
---|
233 | syslog(LOG_ERR, "error: %.500s", buf); |
---|
234 | } |
---|
235 | |
---|
236 | struct fatal_cleanup |
---|
237 | { |
---|
238 | struct fatal_cleanup *next; |
---|
239 | void (*proc)(void *); |
---|
240 | void *context; |
---|
241 | }; |
---|
242 | |
---|
243 | static struct fatal_cleanup *fatal_cleanups = NULL; |
---|
244 | |
---|
245 | /* Registers a cleanup function to be called by fatal() before exiting. */ |
---|
246 | |
---|
247 | void fatal_add_cleanup(void (*proc)(void *), void *context) |
---|
248 | { |
---|
249 | struct fatal_cleanup *cu; |
---|
250 | |
---|
251 | cu = xmalloc(sizeof(*cu)); |
---|
252 | cu->proc = proc; |
---|
253 | cu->context = context; |
---|
254 | cu->next = fatal_cleanups; |
---|
255 | fatal_cleanups = cu; |
---|
256 | } |
---|
257 | |
---|
258 | /* Removes a cleanup frunction to be called at fatal(). */ |
---|
259 | |
---|
260 | void fatal_remove_cleanup(void (*proc)(void *context), void *context) |
---|
261 | { |
---|
262 | struct fatal_cleanup **cup, *cu; |
---|
263 | |
---|
264 | for (cup = &fatal_cleanups; *cup; cup = &cu->next) |
---|
265 | { |
---|
266 | cu = *cup; |
---|
267 | if (cu->proc == proc && cu->context == context) |
---|
268 | { |
---|
269 | *cup = cu->next; |
---|
270 | xfree(cu); |
---|
271 | return; |
---|
272 | } |
---|
273 | } |
---|
274 | fatal("fatal_remove_cleanup: no such cleanup function: 0x%lx 0x%lx\n", |
---|
275 | (unsigned long)proc, (unsigned long)context); |
---|
276 | } |
---|
277 | |
---|
278 | static void do_fatal_cleanups(void) |
---|
279 | { |
---|
280 | struct fatal_cleanup *cu, *next_cu; |
---|
281 | static int fatal_called = 0; |
---|
282 | #ifdef KERBEROS |
---|
283 | extern char *ticket; |
---|
284 | #endif |
---|
285 | |
---|
286 | if (!fatal_called) |
---|
287 | { |
---|
288 | fatal_called = 1; |
---|
289 | |
---|
290 | /* Call cleanup functions. */ |
---|
291 | for (cu = fatal_cleanups; cu; cu = next_cu) |
---|
292 | { |
---|
293 | next_cu = cu->next; |
---|
294 | debug("Calling cleanup 0x%lx(0x%lx)", |
---|
295 | (unsigned long)cu->proc, (unsigned long)cu->context); |
---|
296 | (*cu->proc)(cu->context); |
---|
297 | } |
---|
298 | #ifdef KERBEROS |
---|
299 | /* If you forwarded a ticket you get one shot for proper |
---|
300 | authentication. */ |
---|
301 | /* If tgt was passed, destroy it */ |
---|
302 | if (ticket) |
---|
303 | { |
---|
304 | if (strcmp(ticket,"none")) |
---|
305 | { |
---|
306 | krb5_ccache ccache; |
---|
307 | if (!krb5_cc_resolve(ssh_context, ticket, &ccache)) |
---|
308 | krb5_cc_destroy(ssh_context, ccache); |
---|
309 | dest_tkt(); |
---|
310 | } |
---|
311 | else |
---|
312 | ticket = NULL; |
---|
313 | } |
---|
314 | #endif /* KERBEROS */ |
---|
315 | } |
---|
316 | } |
---|
317 | |
---|
318 | /* Fatal messages. This function never returns. */ |
---|
319 | |
---|
320 | void fatal(const char *fmt, ...) |
---|
321 | { |
---|
322 | char buf[1024]; |
---|
323 | va_list args; |
---|
324 | |
---|
325 | if (log_quiet) |
---|
326 | exit(1); |
---|
327 | va_start(args, fmt); |
---|
328 | vsprintf(buf, fmt, args); |
---|
329 | va_end(args); |
---|
330 | if (log_on_stderr) |
---|
331 | fprintf(stderr, "fatal: %s\n", buf); |
---|
332 | syslog(LOG_ERR, "fatal: %.500s", buf); |
---|
333 | |
---|
334 | do_fatal_cleanups(); |
---|
335 | |
---|
336 | exit(1); |
---|
337 | } |
---|
338 | |
---|
339 | void fatal_severity(SyslogSeverity severity, const char *fmt, ...) |
---|
340 | { |
---|
341 | char buf[1024]; |
---|
342 | va_list args; |
---|
343 | |
---|
344 | if (log_quiet) |
---|
345 | exit(1); |
---|
346 | va_start(args, fmt); |
---|
347 | vsprintf(buf, fmt, args); |
---|
348 | va_end(args); |
---|
349 | if (log_on_stderr) |
---|
350 | fprintf(stderr, "fatal: %s\n", buf); |
---|
351 | syslog(syslog_severity(severity), "fatal: %.500s", buf); |
---|
352 | |
---|
353 | do_fatal_cleanups(); |
---|
354 | |
---|
355 | exit(1); |
---|
356 | } |
---|