[BACK]Return to log-server.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/log-server.c, Revision 1.3

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.3     ! deraadt    18: RCSID("$Id: log-server.c,v 1.2 1999/09/29 18:16:19 dugsong 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: #ifdef HAVE_VSNPRINTF
                     93: #define DECL_MSGBUF char msgbuf[MSGBUFSIZE]
                     94: #else
                     95: static char msgbuf[MSGBUFSIZE];
                     96: #define DECL_MSGBUF
                     97: #endif
                     98:
                     99: /* Log this message (information that usually should go to the log). */
                    100:
                    101: void log(const char *fmt, ...)
                    102: {
                    103:   va_list args;
                    104:   DECL_MSGBUF;
                    105:   if (log_quiet)
                    106:     return;
                    107:   va_start(args, fmt);
                    108:   vsnprintf(msgbuf, MSGBUFSIZE, fmt, args);
                    109:   va_end(args);
                    110:   if (log_on_stderr)
                    111:     fprintf(stderr, "log: %s\n", msgbuf);
                    112:   syslog(LOG_INFO, "log: %.500s", msgbuf);
                    113: }
                    114:
                    115: /* Debugging messages that should not be logged during normal operation. */
                    116:
                    117: void debug(const char *fmt, ...)
                    118: {
                    119:   va_list args;
                    120:   DECL_MSGBUF;
                    121:   if (!log_debug || log_quiet)
                    122:     return;
                    123:   va_start(args, fmt);
                    124:   vsnprintf(msgbuf, MSGBUFSIZE, fmt, args);
                    125:   va_end(args);
                    126:   if (log_on_stderr)
                    127:     fprintf(stderr, "debug: %s\n", msgbuf);
                    128:   syslog(LOG_DEBUG, "debug: %.500s", msgbuf);
                    129: }
                    130:
                    131: /* Error messages that should be logged. */
                    132:
                    133: void error(const char *fmt, ...)
                    134: {
                    135:   va_list args;
                    136:   DECL_MSGBUF;
                    137:   if (log_quiet)
                    138:     return;
                    139:   va_start(args, fmt);
                    140:   vsnprintf(msgbuf, MSGBUFSIZE, fmt, args);
                    141:   va_end(args);
                    142:   if (log_on_stderr)
                    143:     fprintf(stderr, "error: %s\n", msgbuf);
                    144:   syslog(LOG_ERR, "error: %.500s", msgbuf);
                    145: }
                    146:
                    147: struct fatal_cleanup
                    148: {
                    149:   struct fatal_cleanup *next;
                    150:   void (*proc)(void *);
                    151:   void *context;
                    152: };
                    153:
                    154: static struct fatal_cleanup *fatal_cleanups = NULL;
                    155:
                    156: /* Registers a cleanup function to be called by fatal() before exiting. */
                    157:
                    158: void fatal_add_cleanup(void (*proc)(void *), void *context)
                    159: {
                    160:   struct fatal_cleanup *cu;
                    161:
                    162:   cu = xmalloc(sizeof(*cu));
                    163:   cu->proc = proc;
                    164:   cu->context = context;
                    165:   cu->next = fatal_cleanups;
                    166:   fatal_cleanups = cu;
                    167: }
                    168:
                    169: /* Removes a cleanup frunction to be called at fatal(). */
                    170:
                    171: void fatal_remove_cleanup(void (*proc)(void *context), void *context)
                    172: {
                    173:   struct fatal_cleanup **cup, *cu;
                    174:
                    175:   for (cup = &fatal_cleanups; *cup; cup = &cu->next)
                    176:     {
                    177:       cu = *cup;
                    178:       if (cu->proc == proc && cu->context == context)
                    179:        {
                    180:          *cup = cu->next;
                    181:          xfree(cu);
                    182:          return;
                    183:        }
                    184:     }
                    185:   fatal("fatal_remove_cleanup: no such cleanup function: 0x%lx 0x%lx\n",
                    186:        (unsigned long)proc, (unsigned long)context);
                    187: }
                    188:
                    189: /* Fatal messages.  This function never returns. */
                    190:
                    191: void fatal(const char *fmt, ...)
                    192: {
                    193:   va_list args;
                    194:   struct fatal_cleanup *cu, *next_cu;
                    195:   static int fatal_called = 0;
                    196: #if defined(KRB4)
                    197:   extern char *ticket;
                    198: #endif /* KRB4 */
                    199:   DECL_MSGBUF;
                    200:
                    201:   if (log_quiet)
                    202:     exit(1);
                    203:   va_start(args, fmt);
                    204:   vsnprintf(msgbuf, MSGBUFSIZE, fmt, args);
                    205:   va_end(args);
                    206:   if (log_on_stderr)
                    207:     fprintf(stderr, "fatal: %s\n", msgbuf);
                    208:   syslog(LOG_ERR, "fatal: %.500s", msgbuf);
                    209:
                    210:   if (fatal_called)
                    211:     exit(1);
                    212:   fatal_called = 1;
                    213:
                    214:   /* Call cleanup functions. */
                    215:   for (cu = fatal_cleanups; cu; cu = next_cu)
                    216:     {
                    217:       next_cu = cu->next;
                    218:       debug("Calling cleanup 0x%lx(0x%lx)",
                    219:            (unsigned long)cu->proc, (unsigned long)cu->context);
                    220:       (*cu->proc)(cu->context);
                    221:     }
                    222: #if defined(KRB4)
                    223:   /* If you forwarded a ticket you get one shot for proper
                    224:      authentication. */
                    225:   /* If tgt was passed unlink file */
                    226:   if (ticket)
                    227:     {
                    228:       if (strcmp(ticket,"none"))
1.2       dugsong   229:        unlink(ticket);
1.1       deraadt   230:       else
                    231:        ticket = NULL;
                    232:     }
                    233: #endif /* KRB4 */
                    234:
                    235:   /* If local XAUTHORITY was created, remove it. */
                    236:   if (xauthfile) unlink(xauthfile);
                    237:
                    238:   exit(1);
                    239: }