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