Blame usr/sbin/pkcsslotd/log.c

Packit 8681c6
/*
Packit 8681c6
 * COPYRIGHT (c) International Business Machines Corp. 2001-2017
Packit 8681c6
 *
Packit 8681c6
 * This program is provided under the terms of the Common Public License,
Packit 8681c6
 * version 1.0 (CPL-1.0). Any use, reproduction or distribution for this
Packit 8681c6
 * software constitutes recipient's acceptance of CPL-1.0 terms which can be
Packit 8681c6
 * found in the file LICENSE file or at
Packit 8681c6
 * https://opensource.org/licenses/cpl1.0.php
Packit 8681c6
 */
Packit 8681c6
Packit 8681c6
#include <stdio.h>
Packit 8681c6
#include <stdlib.h>
Packit 8681c6
#include <string.h>
Packit 8681c6
#include <syslog.h>
Packit 8681c6
#include <stdarg.h>
Packit 8681c6
#include <unistd.h>
Packit 8681c6
#include <sys/types.h>
Packit 8681c6
Packit 8681c6
#include "log.h"
Packit 8681c6
#include "err.h"
Packit 8681c6
#include "slotmgr.h"
Packit 8681c6
#include "pkcsslotd.h"
Packit 8681c6
Packit 8681c6
Packit 8681c6
#define DEFAULT_PROGRAM_NAME "Program"
Packit 8681c6
Packit 8681c6
#ifndef PROGRAM_NAME
Packit 8681c6
#define PROGRAM_NAME DEFAULT_PROGRAM_NAME
Packit 8681c6
#endif                          /* PROGRAM_NAME */
Packit 8681c6
Packit 8681c6
Packit 8681c6
#ifdef DEV
Packit 8681c6
Packit 8681c6
#ifndef DEFAULT_LOG_FILE
Packit 8681c6
#define DEFAULT_LOG_FILE "/tmp/" ## PROGRAM_NAME ## ".log"
Packit 8681c6
#endif                          /* DEFAULT_LOG_FILE */
Packit 8681c6
Packit 8681c6
#else                           /* production build */
Packit 8681c6
Packit 8681c6
#ifndef DEFAULT_LOG_FILE
Packit 8681c6
#define DEFAULT_LOG_FILE NULL
Packit 8681c6
#endif                          /* DEFAULT_LOG_FILE */
Packit 8681c6
Packit 8681c6
#endif                          /* DEV */
Packit 8681c6
Packit 8681c6
Packit 8681c6
#ifndef LOG_FILE
Packit 8681c6
#define LOG_FILE DEFAULT_LOG_FILE
Packit 8681c6
#endif                          /* LOG_FILE */
Packit 8681c6
Packit 8681c6
Packit 8681c6
#ifndef DEFAULT_DEBUG_LEVEL
Packit 8681c6
Packit 8681c6
  /*********************
Packit 8681c6
     DEFAULT_DEBUG_LEVEL is generally defined in another file (pkcsslotd.h?)
Packit 8681c6
     The #defines here generally won't change much.
Packit 8681c6
   *********************/
Packit 8681c6
Packit 8681c6
#ifdef DEV
Packit 8681c6
#define DEFAULT_DEBUG_LEVEL DEBUG_LEVEL0
Packit 8681c6
#else
Packit 8681c6
#define DEFAULT_DEBUG_LEVEL DEBUG_NONE
Packit 8681c6
#endif                          /* DEFAULT_DEBUG_LEVEL */
Packit 8681c6
Packit 8681c6
#endif                          /* DEFAULT_DEBUG_LEVEL */
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
static u_int32 DefaultLogOption =
Packit 8681c6
    (LOG_CONS | LOG_NOWAIT | LOG_ODELAY | LOG_PID);
Packit 8681c6
static BOOL Initialized = FALSE;
Packit 8681c6
static BOOL LoggingInitialized = FALSE;
Packit 8681c6
static u_int32 SysDebugLevel = DEFAULT_DEBUG_LEVEL;
Packit 8681c6
static char *ProgramName = PROGRAM_NAME;
Packit 8681c6
static LoggingFacilityInfo LogInfo[MAX_LOGGING_FACILITIES];
Packit 8681c6
static LogHandle hLogDebug;
Packit 8681c6
static LogHandle hLogErr;
Packit 8681c6
static LogHandle hLogLog;
Packit 8681c6
static LogHandle hLogTrace;
Packit 8681c6
static LogHandle hLogWarn;
Packit 8681c6
static LogHandle hLogInfo;
Packit 8681c6
Packit 8681c6
Packit 8681c6
static LoggingFacility SystemLogFacilities[] = {
Packit 8681c6
    {"  DEBUG", &hLogDebug, LOG_FILE, TRUE, LOG_DEBUG},
Packit 8681c6
    {"   INFO", &hLogInfo, LOG_FILE, TRUE, LOG_INFO},
Packit 8681c6
    {"  TRACE", &hLogTrace, LOG_FILE, TRUE, LOG_INFO},
Packit 8681c6
    {"    LOG", &hLogLog, LOG_FILE, TRUE, LOG_NOTICE},
Packit 8681c6
    {"WARNING", &hLogWarn, LOG_FILE, TRUE, LOG_WARNING},
Packit 8681c6
    {"  ERROR", &hLogErr, LOG_FILE, TRUE, LOG_ERR}
Packit 8681c6
};
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
/*****************************************
Packit 8681c6
 *        Function Prototypes            *
Packit 8681c6
 *****************************************/
Packit 8681c6
Packit 8681c6
static int InitDataStructs(void);
Packit 8681c6
static BOOL InitLogging(void);
Packit 8681c6
static pLoggingFacilityInfo GetLogInfoPtr(LogHandle hLog);
Packit 8681c6
static BOOL GetFreeLogInfo(pLogHandle Dest);
Packit 8681c6
static void CloseAllLoggingFacilities(void);
Packit 8681c6
static BOOL SyslogOpen(pLoggingFacilityInfo pInfo);
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
/*************************************************************
Packit 8681c6
 *  GetCurrentTimeString -
Packit 8681c6
 *
Packit Service 8aa27d
 *     Writes the current date & time into *buf
Packit 8681c6
 *
Packit 8681c6
 *************************************************************/
Packit 8681c6
Packit Service 8aa27d
BOOL GetCurrentTimeString(char *buf)
Packit 8681c6
{
Packit 8681c6
    /* Note: The specs for ctime_r and asctime_r say that Buffer needs
Packit 8681c6
     * to be 26 characters long.  Not sure if that includes a triling
Packit 8681c6
     * NULL - SCM */
Packit 8681c6
Packit 8681c6
    time_t t;
Packit 8681c6
    struct tm tm;
Packit 8681c6
Packit Service 8aa27d
    ASSERT(buf != NULL);
Packit 8681c6
Packit 8681c6
    time(&t);
Packit 8681c6
    localtime_r(&t, &tm;;
Packit Service 8aa27d
    asctime_r(&tm, buf);
Packit 8681c6
    /* asctime_r puts a \n at the end, so we'll remove that */
Packit Service 8aa27d
    buf[strlen(buf) - 1] = '\0';
Packit 8681c6
Packit 8681c6
    return TRUE;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
/***********************************************************************
Packit 8681c6
 *  InitDataStructs -
Packit 8681c6
 *
Packit 8681c6
 *  Called durining initalization to set up the LogInfo array
Packit 8681c6
 *
Packit 8681c6
 ***********************************************************************/
Packit 8681c6
Packit 8681c6
static int InitDataStructs(void)
Packit 8681c6
{
Packit 8681c6
    unsigned int i;
Packit 8681c6
Packit 8681c6
    for (i = 0; i < (sizeof(LogInfo) / sizeof(LogInfo[0])); i++) {
Packit 8681c6
        LogInfo[i].Initialized = FALSE;
Packit 8681c6
        LogInfo[i].Descrip[0] = '\0';
Packit 8681c6
        LogInfo[i].LogOption = DefaultLogOption;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    Initialized = TRUE;
Packit 8681c6
Packit 8681c6
    return TRUE;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
/**********************************************************************
Packit 8681c6
 *  GetFreeLogInfo -
Packit 8681c6
 *
Packit 8681c6
 *  Return the handle for the next available Log Facility structure
Packit 8681c6
 *
Packit 8681c6
 *  After calling this function, the facility will be marked as in use
Packit 8681c6
 *
Packit 8681c6
 ***********************************************************************/
Packit 8681c6
Packit 8681c6
static BOOL GetFreeLogInfo(pLogHandle Dest)
Packit 8681c6
{
Packit 8681c6
    u_int32 i;
Packit 8681c6
Packit 8681c6
    if (!Initialized) {
Packit 8681c6
        InitDataStructs();
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    for (i = 0; i < (sizeof(LogInfo) / sizeof(LogInfo[0])); i++) {
Packit 8681c6
        if (LogInfo[i].Initialized == FALSE) {
Packit 8681c6
            /*
Packit 8681c6
               Set this here so that we don't return the same identifier twice
Packit 8681c6
               in the case where GetFreeLogInfo() is called twice in a row
Packit 8681c6
             */
Packit 8681c6
            LogInfo[i].Initialized = TRUE;
Packit 8681c6
            *Dest = i;
Packit 8681c6
            return TRUE;
Packit 8681c6
        }
Packit 8681c6
    }
Packit 8681c6
#ifdef DEV
Packit 8681c6
    fprintf(stderr, "No available thread logging structs.\n");
Packit 8681c6
#endif
Packit 8681c6
Packit 8681c6
    return FALSE;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
/**********************************************************************
Packit 8681c6
 *  GetLogInfoPtr -
Packit 8681c6
 *
Packit 8681c6
 *  Given a handle, return a pointer to the appropriate LoggingFacilityInfo
Packit 8681c6
 *  structure
Packit 8681c6
 *
Packit 8681c6
 ***********************************************************************/
Packit 8681c6
Packit 8681c6
static pLoggingFacilityInfo GetLogInfoPtr(LogHandle hLog)
Packit 8681c6
{
Packit 8681c6
    if (hLog >= (sizeof(LogInfo) / sizeof(LogInfo[0]))) {
Packit 8681c6
#ifdef DEV
Packit 8681c6
        fprintf(stderr, "Illegal LogHandle value: %#X\n", hLog);
Packit 8681c6
#endif
Packit 8681c6
        return NULL;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    if (LogInfo[hLog].Initialized != TRUE) {
Packit 8681c6
#ifdef DEV
Packit 8681c6
        fprintf(stderr,
Packit 8681c6
                "GetLogInfoPtr() called for a non-initialized handle\n");
Packit 8681c6
#endif
Packit 8681c6
        return NULL;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    return &(LogInfo[hLog]);
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
/***********************************************************************
Packit 8681c6
 *  NewLoggingFacility -
Packit 8681c6
 *
Packit 8681c6
 *  Given an ID ( char string which will appear in the messages ),
Packit 8681c6
 *  open a logging facility and return a handle to it in
Packit 8681c6
 *  pLoggingStuff->phLog
Packit 8681c6
 *
Packit 8681c6
 ***********************************************************************/
Packit 8681c6
Packit 8681c6
BOOL NewLoggingFacility(char *ID, pLoggingFacility pStuff)
Packit 8681c6
{
Packit 8681c6
    pLoggingFacilityInfo pInfo = NULL;
Packit 8681c6
    LogHandle hLog;
Packit 8681c6
    pLogHandle Result;
Packit 8681c6
Packit 8681c6
    /* See if there's room in the array.
Packit 8681c6
     * This'd be nice if it were dynamically allocated */
Packit 8681c6
    if (!GetFreeLogInfo(&hLog)) {
Packit 8681c6
        return FALSE;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    /* Get a pointer to the syslog_data structure */
Packit 8681c6
    if ((pInfo = GetLogInfoPtr(hLog)) == NULL) {
Packit 8681c6
        return FALSE;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    Result = pStuff->phLog;
Packit 8681c6
Packit 8681c6
Packit 8681c6
    /*
Packit 8681c6
       Set this before the filename is checked because we
Packit 8681c6
       may want to use the descrip and/or filename in the logs
Packit 8681c6
     */
Packit 8681c6
    pInfo->UseSyslog = pStuff->UseSyslog;
Packit 8681c6
    pInfo->LogOption = DefaultLogOption;
Packit 8681c6
    pInfo->pid = 0;
Packit 8681c6
    pInfo->LogLevel = pStuff->LogLevel;
Packit 8681c6
Packit Service 8aa27d
    /* truncate description to fit buffer */
Packit Service 8aa27d
    snprintf(pInfo->Descrip, sizeof(pInfo->Descrip), "%s %s",
Packit Service 8aa27d
             pStuff->Label, ID);
Packit 8681c6
Packit 8681c6
    /* Some sanity checking on filename... */
Packit 8681c6
    if ((pStuff->Filename != NULL) && (strlen(pStuff->Filename) > 0)) {
Packit 8681c6
Packit 8681c6
        FILE *fd;
Packit 8681c6
Packit 8681c6
#if TRUNCATE_LOGS_ON_START
Packit 8681c6
Packit 8681c6
        /*
Packit 8681c6
         *  Truncating files on the start will present problems if the user
Packit 8681c6
         *  creates their own logging facilities after the program's been
Packit 8681c6
         *  running for a while. But the non-syslog logging is intended for
Packit 8681c6
         *  debug purposes only, anyway.
Packit 8681c6
         */
Packit 8681c6
Packit 8681c6
        char FileMode[] = "w";
Packit 8681c6
#else
Packit 8681c6
        char FileMode[] = "a";
Packit 8681c6
#endif                          /* TRUNCATE_LOGS_ON_START */
Packit 8681c6
Packit 8681c6
        if ((fd = fopen((pStuff->Filename), FileMode)) == NULL) {
Packit 8681c6
#ifdef DEV
Packit 8681c6
            fprintf(stderr, "%s could not be opened\n", pStuff->Filename);
Packit 8681c6
#endif
Packit 8681c6
            pInfo->Filename = NULL;
Packit 8681c6
        } else {
Packit 8681c6
            /* Tag the file */
Packit 8681c6
Packit 8681c6
            char buf[100];
Packit 8681c6
Packit 8681c6
            GetCurrentTimeString(&(buf[0]));
Packit 8681c6
Packit 8681c6
#ifdef DEV
Packit 8681c6
#if TRUNCATE_LOGS_ON_START
Packit 8681c6
            /* buf contains the date stamp */
Packit 8681c6
            fprintf(fd, "********* %s %s truncated *********\n", buf,
Packit 8681c6
                    pStuff->Filename);
Packit 8681c6
#else
Packit 8681c6
            fprintf(fd, "********* %s \"%s\" logging to %s *********\n", buf,
Packit 8681c6
                    pInfo->Descrip, pStuff->Filename);
Packit 8681c6
#endif                          /* TRUNCATE_LOGS_ON_START */
Packit 8681c6
#endif
Packit 8681c6
Packit 8681c6
            fflush(fd);
Packit 8681c6
            fclose(fd);
Packit 8681c6
            pInfo->Filename = pStuff->Filename;
Packit 8681c6
        }
Packit 8681c6
    } else {
Packit 8681c6
        pInfo->Filename = NULL;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
Packit 8681c6
    if (pInfo->UseSyslog) {
Packit 8681c6
        /* open the logging facility */
Packit 8681c6
        if (!SyslogOpen(pInfo)) {
Packit 8681c6
            return FALSE;
Packit 8681c6
        }
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    /* Redundant; Initialized is set to 1 in GetFreeLogInfo */
Packit 8681c6
    pInfo->Initialized = TRUE;
Packit 8681c6
    *Result = hLog;
Packit 8681c6
Packit 8681c6
    return TRUE;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
/***********************************************************************
Packit 8681c6
 *  CloseLoggingFacility -
Packit 8681c6
 *
Packit 8681c6
 *  Closes the logging facility whose handle is hLog.
Packit 8681c6
 *  Sets up the data structure for reuse later if desired
Packit 8681c6
 *
Packit 8681c6
 ***********************************************************************/
Packit 8681c6
Packit 8681c6
BOOL CloseLoggingFacility(LogHandle hLog)
Packit 8681c6
{
Packit 8681c6
    pLoggingFacilityInfo pInfo = NULL;
Packit 8681c6
Packit 8681c6
    if ((pInfo = GetLogInfoPtr(hLog)) == NULL) {
Packit 8681c6
        return FALSE;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    pInfo->Descrip[0] = '\0';
Packit 8681c6
    pInfo->LogOption = 0;
Packit 8681c6
    pInfo->Filename = NULL;
Packit 8681c6
    pInfo->pid = 0;
Packit 8681c6
Packit 8681c6
    if (pInfo->UseSyslog) {
Packit 8681c6
        closelog();
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    pInfo->Initialized = FALSE;
Packit 8681c6
Packit 8681c6
    return TRUE;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
/*****************************************
Packit 8681c6
 * CloseAllLoggingFacilities -
Packit 8681c6
 *
Packit 8681c6
 * Closes down all the logging stuff we've set up
Packit 8681c6
 *****************************************/
Packit 8681c6
static void CloseAllLoggingFacilities(void)
Packit 8681c6
{
Packit 8681c6
    u_int32 i = 0;
Packit 8681c6
Packit 8681c6
    for (i = 0; i < (sizeof(LogInfo) / sizeof(LogInfo[0])); i++) {
Packit 8681c6
        /* Makes assumption that these handles all are sequential. Bad Style */
Packit 8681c6
        if (LogInfo[i].Initialized) {
Packit 8681c6
            CloseLoggingFacility(i);
Packit 8681c6
        }
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    return;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
/***********************************************************************
Packit 8681c6
 *  PKCS_Log -
Packit 8681c6
 *
Packit 8681c6
 *  The primitive logging function which logs a message on hLog
Packit 8681c6
 *
Packit 8681c6
 ***********************************************************************/
Packit 8681c6
Packit Service 8aa27d
BOOL PKCS_Log(pLogHandle phLog, char *fmt, va_list ap)
Packit 8681c6
{
Packit Service 8aa27d
    char buf[PATH_MAX];
Packit 8681c6
    pLoggingFacilityInfo pInfo;
Packit Service 8aa27d
    int snres;
Packit 8681c6
Packit Service 8aa27d
    if (fmt == NULL) {
Packit 8681c6
        return FALSE;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    if ((pInfo = GetLogInfoPtr(*phLog)) == NULL) {
Packit 8681c6
        return FALSE;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    if ((pInfo->pid != getpid()) && (pInfo->UseSyslog)) {
Packit 8681c6
        /* Looks like our PID changed since the last call. We have to re-open */
Packit 8681c6
        if (!SyslogOpen(pInfo)) {
Packit 8681c6
            return FALSE;
Packit 8681c6
        }
Packit 8681c6
    }
Packit 8681c6
Packit Service 8aa27d
    snres = vsnprintf(buf, PATH_MAX, fmt, ap);
Packit Service 8aa27d
    if (snres < 0 || snres >= PATH_MAX) {
Packit 8681c6
        /* Error reporting functions should be rather robust,
Packit 8681c6
         * don't you think? */
Packit 8681c6
        /* vsprintf reporting an error */
Packit 8681c6
        //fprintf(stderr, "PKCS_ErrLog -
Packit 8681c6
        //vsprintf error for format string %s\n", Format);
Packit 8681c6
        return FALSE;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    /* Get rid of trailing newlines. */
Packit Service 8aa27d
    while (strlen(buf) && (buf[strlen(buf) - 1] == '\n')) {
Packit Service 8aa27d
        buf[strlen(buf) - 1] = '\0';
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
    // Development work only. No loging to anything other than syslog for
Packit 8681c6
    // production level code
Packit 8681c6
Packit 8681c6
    /*
Packit 8681c6
     * 1/17/00 SCM - If we're not a daemon, we need to print something to
Packit 8681c6
     * stderr for warnings and errors regardless of development/production.
Packit 8681c6
     * This is for errors that occur during startup. I'll agree that we don't
Packit 8681c6
     * need to write to a log file in production mode, however.
Packit 8681c6
     */
Packit 8681c6
Packit 8681c6
    /*
Packit 8681c6
     * Production mode: Write to stderr if we're not a daemon, and the priority
Packit 8681c6
     * of the message is at least LOG_WARNING Development mode: Write to stderr
Packit 8681c6
     * if we're not a daemon
Packit 8681c6
     */
Packit 8681c6
Packit 8681c6
    if (!IsDaemon()) {
Packit 8681c6
        BOOL WriteNow;
Packit 8681c6
Packit 8681c6
#ifdef DEV
Packit 8681c6
        WriteNow = TRUE;
Packit 8681c6
#else
Packit 8681c6
        WriteNow = (pInfo->LogLevel <= LOG_WARNING);
Packit 8681c6
#endif                          /* DEV */
Packit 8681c6
Packit 8681c6
        if (WriteNow) {
Packit 8681c6
            fprintf(stderr, "%s[%d.%d]: %s\n", pInfo->Descrip, getpid(),
Packit Service 8aa27d
                    (int) pthread_self(), buf);
Packit 8681c6
        }
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    /* Don't log to a separate log file in production mode */
Packit 8681c6
#ifdef DEV
Packit 8681c6
    if (pInfo->Filename != NULL) {
Packit 8681c6
Packit 8681c6
        FILE *fd;
Packit 8681c6
Packit 8681c6
        if ((fd = fopen(pInfo->Filename, "a+")) == NULL) {
Packit 8681c6
            fprintf(stderr, "PKCS_Log: fopen failed for %s\n", pInfo->Filename);
Packit 8681c6
        } else {
Packit Service 8aa27d
            char timebuf[32];       /* Specs say 26-character array */
Packit 8681c6
Packit Service 8aa27d
            GetCurrentTimeString(timebuf);
Packit 8681c6
Packit 8681c6
            /* Date/Time stamp, descrip, Error message */
Packit Service 8aa27d
            fprintf(fd, "%s %s[%d.%d]: ", timebuf, pInfo->Descrip, getpid(),
Packit 8681c6
                    pthread_self());
Packit Service 8aa27d
            fprintf(fd, "%s\n", buf);
Packit 8681c6
            fflush(fd);
Packit 8681c6
            fclose(fd);
Packit 8681c6
        }
Packit 8681c6
Packit 8681c6
    }                           /* end if pInfo->Filename */
Packit 8681c6
#endif                          /* DEV */
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
    /* Always log to syslog, if we're using it */
Packit 8681c6
    if (pInfo->UseSyslog) {
Packit Service 8aa27d
        syslog(pInfo->LogLevel, "%s", buf);
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    return TRUE;
Packit 8681c6
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
/****************************************************************************
Packit 8681c6
 *
Packit 8681c6
 *  Would like to have a generic function to which I pass the hLog where I'd
Packit 8681c6
 *  like to do the logging and have a #defined macro which passes it along...
Packit 8681c6
 *
Packit 8681c6
 *  But the preprocessor and variable # args don't work & play well together
Packit 8681c6
 *
Packit 8681c6
 ****************************************************************************/
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
/*****************************************
Packit 8681c6
 * DbgLog -
Packit 8681c6
 *
Packit 8681c6
 *   Log messages using the debug facility
Packit 8681c6
 *****************************************/
Packit 8681c6
Packit 8681c6
void DbgLog(u_int32 DebugLevel, char *Format, ...)
Packit 8681c6
{
Packit 8681c6
    va_list ap;
Packit 8681c6
Packit 8681c6
    if (DebugLevel > SysDebugLevel) {
Packit 8681c6
        return;
Packit 8681c6
    }
Packit 8681c6
    if (!LoggingInitialized) {
Packit 8681c6
        InitLogging();
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    va_start(ap, Format);
Packit 8681c6
    PKCS_Log(&hLogDebug, Format, ap);
Packit 8681c6
    va_end(ap);
Packit 8681c6
Packit 8681c6
    return;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
/*****************************************
Packit 8681c6
 * ErrLog -
Packit 8681c6
 *
Packit 8681c6
 *   Log Messges using the error facility
Packit 8681c6
 *****************************************/
Packit 8681c6
Packit 8681c6
void ErrLog(char *Format, ...)
Packit 8681c6
{
Packit 8681c6
    va_list ap;
Packit 8681c6
Packit 8681c6
    if (!LoggingInitialized) {
Packit 8681c6
        InitLogging();
Packit 8681c6
    }
Packit 8681c6
    va_start(ap, Format);
Packit 8681c6
    PKCS_Log(&hLogErr, Format, ap);
Packit 8681c6
    va_end(ap);
Packit 8681c6
Packit 8681c6
    return;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
/*****************************************
Packit 8681c6
 * LogLog -
Packit 8681c6
 *
Packit 8681c6
 *   Log messages using the log facility
Packit 8681c6
 *****************************************/
Packit 8681c6
void LogLog(char *Format, ...)
Packit 8681c6
{
Packit 8681c6
    va_list ap;
Packit 8681c6
Packit 8681c6
    if (!LoggingInitialized) {
Packit 8681c6
        InitLogging();
Packit 8681c6
    }
Packit 8681c6
    va_start(ap, Format);
Packit 8681c6
    PKCS_Log(&hLogLog, Format, ap);
Packit 8681c6
    va_end(ap);
Packit 8681c6
Packit 8681c6
    return;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
/*****************************************
Packit 8681c6
 * WarnLog -
Packit 8681c6
 *
Packit 8681c6
 *   Log messages using the warning facility
Packit 8681c6
 *****************************************/
Packit 8681c6
void WarnLog(char *Format, ...)
Packit 8681c6
{
Packit 8681c6
    va_list ap;
Packit 8681c6
Packit 8681c6
    if (!LoggingInitialized) {
Packit 8681c6
        InitLogging();
Packit 8681c6
    }
Packit 8681c6
    va_start(ap, Format);
Packit 8681c6
    PKCS_Log(&hLogWarn, Format, ap);
Packit 8681c6
    va_end(ap);
Packit 8681c6
Packit 8681c6
    return;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
/*****************************************
Packit 8681c6
 * TraceLog -
Packit 8681c6
 *
Packit 8681c6
 *   Log messages using the trace facility
Packit 8681c6
 *****************************************/
Packit 8681c6
void TraceLog(char *Format, ...)
Packit 8681c6
{
Packit 8681c6
    va_list ap;
Packit 8681c6
Packit 8681c6
    if (!LoggingInitialized) {
Packit 8681c6
        InitLogging();
Packit 8681c6
    }
Packit 8681c6
    va_start(ap, Format);
Packit 8681c6
    PKCS_Log(&hLogTrace, Format, ap);
Packit 8681c6
    va_end(ap);
Packit 8681c6
Packit 8681c6
    return;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
/*****************************************
Packit 8681c6
 * InfoLog -
Packit 8681c6
 *
Packit 8681c6
 *   Log messages using the info facility
Packit 8681c6
 *****************************************/
Packit 8681c6
Packit 8681c6
void InfoLog(char *Format, ...)
Packit 8681c6
{
Packit 8681c6
    va_list ap;
Packit 8681c6
Packit 8681c6
    if (!LoggingInitialized) {
Packit 8681c6
        InitLogging();
Packit 8681c6
    }
Packit 8681c6
    va_start(ap, Format);
Packit 8681c6
    PKCS_Log(&hLogInfo, Format, ap);
Packit 8681c6
    va_end(ap);
Packit 8681c6
Packit 8681c6
    return;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
/***********************************************************************
Packit 8681c6
 * InitLogging -
Packit 8681c6
 *
Packit 8681c6
 *   Sets up the various logging facilities.  Must be called before
Packit 8681c6
 *   any of the logging functions can be used.
Packit 8681c6
 ***********************************************************************/
Packit 8681c6
Packit 8681c6
static BOOL InitLogging(void)
Packit 8681c6
{
Packit 8681c6
Packit 8681c6
    unsigned int i;
Packit 8681c6
    char *s = ProgramName;
Packit 8681c6
Packit 8681c6
Packit 8681c6
    /* if ProgramName is NULL, we'll just print the level... */
Packit 8681c6
    if (ProgramName == NULL) {
Packit 8681c6
        s = "";
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    /* Set up logging for all the facilities in SystemLogFacilities[] */
Packit 8681c6
    for (i = 0;
Packit 8681c6
         i < (sizeof(SystemLogFacilities) / (sizeof(SystemLogFacilities[0])));
Packit 8681c6
         i++) {
Packit 8681c6
Packit 8681c6
        if (!NewLoggingFacility(s, &(SystemLogFacilities[i]))) {
Packit 8681c6
#ifdef DEV
Packit 8681c6
            fprintf(stderr, "InitLogging: NewLoggingFacility failed: %s\n", s);
Packit 8681c6
#endif
Packit 8681c6
            return FALSE;
Packit 8681c6
        }
Packit 8681c6
Packit 8681c6
    }                           /* end for i */
Packit 8681c6
Packit 8681c6
    atexit(CloseAllLoggingFacilities);
Packit 8681c6
    LoggingInitialized = TRUE;
Packit 8681c6
Packit 8681c6
    return TRUE;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
/*************************************************************
Packit 8681c6
 * SetDebugLevel -
Packit 8681c6
 *
Packit 8681c6
 *
Packit 8681c6
 *  Sets the level at which debug messages get logged to Val.
Packit 8681c6
 *  Returns the old value
Packit 8681c6
 *************************************************************/
Packit 8681c6
Packit 8681c6
u_int32 SetDebugLevel(u_int32 Val)
Packit 8681c6
{
Packit 8681c6
    u_int32 OldVal = SysDebugLevel;
Packit 8681c6
Packit 8681c6
    SysDebugLevel = Val;
Packit 8681c6
Packit 8681c6
    return OldVal;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
/*************************************************************
Packit 8681c6
 * GetDebugLevel
Packit 8681c6
 *
Packit 8681c6
 *   Returns the level at which the program will log debug messages
Packit 8681c6
 *
Packit 8681c6
 *************************************************************/
Packit 8681c6
Packit 8681c6
u_int32 GetDebugLevel(void)
Packit 8681c6
{
Packit 8681c6
    return SysDebugLevel;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
#if 0
Packit 8681c6
int main(int argc, char *argv[], char *envp[])
Packit 8681c6
{
Packit 8681c6
Packit 8681c6
    ErrLog("This is an error test, attempt 1");
Packit 8681c6
    DbgLog(DEBUG_LEVEL0, "This is a DEBUG test level 0, attempt 1");
Packit 8681c6
    DbgLog(DEBUG_LEVEL1, "This is a DEBUG test level 1, attempt 1");
Packit 8681c6
    SetDebugLevel(DEBUG_NONE);
Packit 8681c6
    DbgLog(DEBUG_LEVEL1, "This is a DEBUG test level 1, attempt 2");
Packit 8681c6
    DbgLog(DEBUG_LEVEL0, "This is a DEBUG test level 0, attempt 2");
Packit 8681c6
    ErrLog("This is an error test, attempt 2");
Packit 8681c6
    return 0;
Packit 8681c6
Packit 8681c6
}
Packit 8681c6
#endif                          /* 0 */
Packit 8681c6
Packit 8681c6
Packit 8681c6
Packit 8681c6
static BOOL SyslogOpen(pLoggingFacilityInfo pInfo)
Packit 8681c6
{
Packit 8681c6
    ASSERT(pInfo != NULL);
Packit 8681c6
Packit 8681c6
    if (!(pInfo->UseSyslog)) {
Packit 8681c6
        /* it's not really an error to call SyslogOpen for a facility
Packit 8681c6
         * that doesn't use it */
Packit 8681c6
        return TRUE;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    if (pInfo->pid != 0) {
Packit 8681c6
        /* We've been initialized before, so close the previous instance */
Packit 8681c6
        closelog();
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    /* Default to log all messages. */
Packit 8681c6
    setlogmask(LOG_UPTO(LOG_DEBUG));
Packit 8681c6
Packit 8681c6
    /* Mark this as having been set by this process */
Packit 8681c6
    pInfo->pid = getpid();
Packit 8681c6
Packit 8681c6
    return TRUE;
Packit 8681c6
}