openpam_log.c revision 91684
1/*- 2 * Copyright (c) 2002 Networks Associates Technologies, Inc. 3 * All rights reserved. 4 * 5 * This software was developed for the FreeBSD Project by ThinkSec AS and 6 * NAI Labs, the Security Research Division of Network Associates, Inc. 7 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 8 * DARPA CHATS research program. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. The name of the author may not be used to endorse or promote 19 * products derived from this software without specific prior written 20 * permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * $P4: //depot/projects/openpam/lib/openpam_log.c#9 $ 35 */ 36 37#include <ctype.h> 38#include <stdarg.h> 39#include <stdio.h> 40#include <stdlib.h> 41#include <syslog.h> 42 43#include <security/pam_appl.h> 44 45#include "openpam_impl.h" 46 47#if defined(openpam_log) 48 49/* 50 * OpenPAM extension 51 * 52 * Log a message through syslog(3) 53 */ 54 55void 56_openpam_log(int level, const char *func, const char *fmt, ...) 57{ 58 va_list ap; 59 char *format; 60 int len, priority; 61 62 switch (level) { 63 case PAM_LOG_DEBUG: 64 priority = LOG_DEBUG; 65 break; 66 case PAM_LOG_VERBOSE: 67 priority = LOG_INFO; 68 break; 69 case PAM_LOG_NOTICE: 70 priority = LOG_NOTICE; 71 break; 72 case PAM_LOG_ERROR: 73 priority = LOG_ERR; 74 break; 75 } 76 va_start(ap, fmt); 77 for (len = strlen(fmt); len > 0 && isspace(fmt[len]); len--) 78 /* nothing */; 79 if ((format = malloc(strlen(func) + len + 16)) != NULL) { 80 sprintf(format, "in %s(): %.*s\n", func, len, fmt); 81 vsyslog(priority, format, ap); 82#ifdef DEBUG 83 vfprintf(stderr, format, ap); 84#endif 85 free(format); 86 } else { 87 vsyslog(priority, fmt, ap); 88 } 89 va_end(ap); 90} 91 92#else 93 94/* 95 * If openpam_log isn't defined as a macro, we're on a platform that 96 * doesn't support varadic macros (or it does but we aren't aware of 97 * it). Do the next best thing. 98 */ 99 100void 101openpam_log(int level, const char *fmt, ...) 102{ 103 va_list ap; 104 int priority; 105 106 switch (level) { 107 case PAM_LOG_DEBUG: 108 priority = LOG_DEBUG; 109 break; 110 case PAM_LOG_VERBOSE: 111 priority = LOG_INFO; 112 break; 113 case PAM_LOG_NOTICE: 114 priority = LOG_NOTICE; 115 break; 116 case PAM_LOG_ERROR: 117 priority = LOG_ERR; 118 break; 119 } 120 va_start(ap, fmt); 121 vsyslog(priority, fmt, ap); 122 va_end(ap); 123} 124 125#endif 126 127/* 128 * NOLIST 129 */ 130