Annotation of src/usr.bin/ssh/log-server.c, Revision 1.4
1.1 deraadt 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(), etc. These normally send the output
13: to the system log.
14:
15: */
16:
17: #include "includes.h"
1.4 ! deraadt 18: RCSID("$Id: log-server.c,v 1.3 1999/09/30 05:03:04 deraadt Exp $");
1.1 deraadt 19:
20: #include <syslog.h>
21: #include <sys/syslog.h>
22: #include "packet.h"
23: #include "xmalloc.h"
24: #include "ssh.h"
25:
26: static int log_debug = 0;
27: static int log_quiet = 0;
28: static int log_on_stderr = 0;
29:
30: /* Initialize the log.
31: av0 program name (should be argv[0])
32: on_stderr print also on stderr
33: debug send debugging messages to system log
34: quiet don\'t log anything
35: */
36:
37: void log_init(char *av0, int on_stderr, int debug, int quiet,
38: SyslogFacility facility)
39: {
40: int log_facility;
41:
42: switch (facility)
43: {
44: case SYSLOG_FACILITY_DAEMON:
45: log_facility = LOG_DAEMON;
46: break;
47: case SYSLOG_FACILITY_USER:
48: log_facility = LOG_USER;
49: break;
50: case SYSLOG_FACILITY_AUTH:
51: log_facility = LOG_AUTH;
52: break;
53: case SYSLOG_FACILITY_LOCAL0:
54: log_facility = LOG_LOCAL0;
55: break;
56: case SYSLOG_FACILITY_LOCAL1:
57: log_facility = LOG_LOCAL1;
58: break;
59: case SYSLOG_FACILITY_LOCAL2:
60: log_facility = LOG_LOCAL2;
61: break;
62: case SYSLOG_FACILITY_LOCAL3:
63: log_facility = LOG_LOCAL3;
64: break;
65: case SYSLOG_FACILITY_LOCAL4:
66: log_facility = LOG_LOCAL4;
67: break;
68: case SYSLOG_FACILITY_LOCAL5:
69: log_facility = LOG_LOCAL5;
70: break;
71: case SYSLOG_FACILITY_LOCAL6:
72: log_facility = LOG_LOCAL6;
73: break;
74: case SYSLOG_FACILITY_LOCAL7:
75: log_facility = LOG_LOCAL7;
76: break;
77: default:
78: fprintf(stderr, "Unrecognized internal syslog facility code %d\n",
79: (int)facility);
80: exit(1);
81: }
82:
83: log_debug = debug;
84: log_quiet = quiet;
85: log_on_stderr = on_stderr;
86: closelog(); /* Close any previous log. */
87: openlog(av0, LOG_PID, log_facility);
88: }
89:
90: #define MSGBUFSIZE 1024
91:
92: #define DECL_MSGBUF char msgbuf[MSGBUFSIZE]
93:
94: /* Log this message (information that usually should go to the log). */
95:
96: void log(const char *fmt, ...)
97: {
98: va_list args;
99: DECL_MSGBUF;
100: if (log_quiet)
101: return;
102: va_start(args, fmt);
103: vsnprintf(msgbuf, MSGBUFSIZE, fmt, args);
104: va_end(args);
105: if (log_on_stderr)
106: fprintf(stderr, "log: %s\n", msgbuf);
107: syslog(LOG_INFO, "log: %.500s", msgbuf);
108: }
109:
110: /* Debugging messages that should not be logged during normal operation. */
111:
112: void debug(const char *fmt, ...)
113: {
114: va_list args;
115: DECL_MSGBUF;
116: if (!log_debug || log_quiet)
117: return;
118: va_start(args, fmt);
119: vsnprintf(msgbuf, MSGBUFSIZE, fmt, args);
120: va_end(args);
121: if (log_on_stderr)
122: fprintf(stderr, "debug: %s\n", msgbuf);
123: syslog(LOG_DEBUG, "debug: %.500s", msgbuf);
124: }
125:
126: /* Error messages that should be logged. */
127:
128: void error(const char *fmt, ...)
129: {
130: va_list args;
131: DECL_MSGBUF;
132: if (log_quiet)
133: return;
134: va_start(args, fmt);
135: vsnprintf(msgbuf, MSGBUFSIZE, fmt, args);
136: va_end(args);
137: if (log_on_stderr)
138: fprintf(stderr, "error: %s\n", msgbuf);
139: syslog(LOG_ERR, "error: %.500s", msgbuf);
140: }
141:
142: struct fatal_cleanup
143: {
144: struct fatal_cleanup *next;
145: void (*proc)(void *);
146: void *context;
147: };
148:
149: static struct fatal_cleanup *fatal_cleanups = NULL;
150:
151: /* Registers a cleanup function to be called by fatal() before exiting. */
152:
153: void fatal_add_cleanup(void (*proc)(void *), void *context)
154: {
155: struct fatal_cleanup *cu;
156:
157: cu = xmalloc(sizeof(*cu));
158: cu->proc = proc;
159: cu->context = context;
160: cu->next = fatal_cleanups;
161: fatal_cleanups = cu;
162: }
163:
164: /* Removes a cleanup frunction to be called at fatal(). */
165:
166: void fatal_remove_cleanup(void (*proc)(void *context), void *context)
167: {
168: struct fatal_cleanup **cup, *cu;
169:
170: for (cup = &fatal_cleanups; *cup; cup = &cu->next)
171: {
172: cu = *cup;
173: if (cu->proc == proc && cu->context == context)
174: {
175: *cup = cu->next;
176: xfree(cu);
177: return;
178: }
179: }
180: fatal("fatal_remove_cleanup: no such cleanup function: 0x%lx 0x%lx\n",
181: (unsigned long)proc, (unsigned long)context);
182: }
183:
184: /* Fatal messages. This function never returns. */
185:
186: void fatal(const char *fmt, ...)
187: {
188: va_list args;
189: struct fatal_cleanup *cu, *next_cu;
190: static int fatal_called = 0;
191: #if defined(KRB4)
192: extern char *ticket;
193: #endif /* KRB4 */
194: DECL_MSGBUF;
195:
196: if (log_quiet)
197: exit(1);
198: va_start(args, fmt);
199: vsnprintf(msgbuf, MSGBUFSIZE, fmt, args);
200: va_end(args);
201: if (log_on_stderr)
202: fprintf(stderr, "fatal: %s\n", msgbuf);
203: syslog(LOG_ERR, "fatal: %.500s", msgbuf);
204:
205: if (fatal_called)
206: exit(1);
207: fatal_called = 1;
208:
209: /* Call cleanup functions. */
210: for (cu = fatal_cleanups; cu; cu = next_cu)
211: {
212: next_cu = cu->next;
213: debug("Calling cleanup 0x%lx(0x%lx)",
214: (unsigned long)cu->proc, (unsigned long)cu->context);
215: (*cu->proc)(cu->context);
216: }
217: #if defined(KRB4)
218: /* If you forwarded a ticket you get one shot for proper
219: authentication. */
220: /* If tgt was passed unlink file */
221: if (ticket)
222: {
223: if (strcmp(ticket,"none"))
1.2 dugsong 224: unlink(ticket);
1.1 deraadt 225: else
226: ticket = NULL;
227: }
228: #endif /* KRB4 */
229:
230: /* If local XAUTHORITY was created, remove it. */
231: if (xauthfile) unlink(xauthfile);
232:
233: exit(1);
234: }