Home | History | Annotate | Line # | Download | only in libpam
      1 /*	$NetBSD: openpam_log.c,v 1.5 2023/06/30 21:46:20 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
      5  * Copyright (c) 2004-2011 Dag-Erling Smrgrav
      6  * All rights reserved.
      7  *
      8  * This software was developed for the FreeBSD Project by ThinkSec AS and
      9  * Network Associates Laboratories, the Security Research Division of
     10  * Network Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
     11  * ("CBOSS"), as part of the DARPA CHATS research program.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  * 3. The name of the author may not be used to endorse or promote
     22  *    products derived from this software without specific prior written
     23  *    permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  */
     37 
     38 #ifdef HAVE_CONFIG_H
     39 # include "config.h"
     40 #endif
     41 
     42 #include <sys/cdefs.h>
     43 __RCSID("$NetBSD: openpam_log.c,v 1.5 2023/06/30 21:46:20 christos Exp $");
     44 
     45 #include <errno.h>
     46 #include <stdarg.h>
     47 #include <stdio.h>
     48 #include <stdlib.h>
     49 #include <syslog.h>
     50 
     51 #include <security/pam_appl.h>
     52 
     53 #include "openpam_impl.h"
     54 #include "openpam_asprintf.h"
     55 
     56 int openpam_debug = 0;
     57 
     58 #if !defined(openpam_log)
     59 
     60 /*
     61  * OpenPAM extension
     62  *
     63  * Log a message through syslog
     64  */
     65 
     66 void
     67 openpam_log(int level, const char *fmt, ...)
     68 {
     69 	va_list ap;
     70 	int priority;
     71 	int serrno;
     72 
     73 	switch (level) {
     74 	case PAM_LOG_LIBDEBUG:
     75 	case PAM_LOG_DEBUG:
     76 		if (!openpam_debug)
     77 			return;
     78 		priority = LOG_DEBUG;
     79 		break;
     80 	case PAM_LOG_VERBOSE:
     81 		priority = LOG_INFO;
     82 		break;
     83 	case PAM_LOG_NOTICE:
     84 		priority = LOG_NOTICE;
     85 		break;
     86 	case PAM_LOG_ERROR:
     87 	default:
     88 		priority = LOG_ERR;
     89 		break;
     90 	}
     91 	serrno = errno;
     92 	va_start(ap, fmt);
     93 	vsyslog(priority, fmt, ap);
     94 	va_end(ap);
     95 	errno = serrno;
     96 }
     97 
     98 #else
     99 
    100 #if defined(__clang__) || __GNUC_PREREQ__(4, 5)
    101 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
    102 #endif
    103 void
    104 _openpam_log(int level, const char *func, const char *fmt, ...)
    105 {
    106 	va_list ap;
    107 	char *format;
    108 	int priority;
    109 	int serrno;
    110 
    111 	switch ((enum openpam_log_primitives)level) {
    112 	case PAM_LOG_LIBDEBUG:
    113 	case PAM_LOG_DEBUG:
    114 		if (!openpam_debug)
    115 			return;
    116 		priority = LOG_DEBUG;
    117 		break;
    118 	case PAM_LOG_VERBOSE:
    119 		priority = LOG_INFO;
    120 		break;
    121 	case PAM_LOG_NOTICE:
    122 		priority = LOG_NOTICE;
    123 		break;
    124 	case PAM_LOG_ERROR:
    125 	default:
    126 		priority = LOG_ERR;
    127 		break;
    128 	}
    129 	serrno = errno;
    130 	va_start(ap, fmt);
    131 	if (asprintf(&format, "in %s(): %s", func, fmt) > 0) {
    132 		errno = serrno;
    133 		vsyslog(priority, format, ap);
    134 		FREE(format);
    135 	} else {
    136 		errno = serrno;
    137 		vsyslog(priority, fmt, ap);
    138 	}
    139 	va_end(ap);
    140 	errno = serrno;
    141 }
    142 
    143 #endif
    144 
    145 /**
    146  * The =openpam_log function logs messages using =syslog.
    147  * It is primarily intended for internal use by the library and modules.
    148  *
    149  * The =level argument indicates the importance of the message.
    150  * The following levels are defined:
    151  *
    152  *	=PAM_LOG_LIBDEBUG:
    153  *		Debugging messages.
    154  *		For internal use only.
    155  *	=PAM_LOG_DEBUG:
    156  *		Debugging messages.
    157  *		These messages are normally not logged unless the global
    158  *		integer variable :openpam_debug is set to a non-zero
    159  *		value, in which case they are logged with a =syslog
    160  *		priority of =LOG_DEBUG.
    161  *	=PAM_LOG_VERBOSE:
    162  *		Information about the progress of the authentication
    163  *		process, or other non-essential messages.
    164  *		These messages are logged with a =syslog priority of
    165  *		=LOG_INFO.
    166  *	=PAM_LOG_NOTICE:
    167  *		Messages relating to non-fatal errors.
    168  *		These messages are logged with a =syslog priority of
    169  *		=LOG_NOTICE.
    170  *	=PAM_LOG_ERROR:
    171  *		Messages relating to serious errors.
    172  *		These messages are logged with a =syslog priority of
    173  *		=LOG_ERR.
    174  *
    175  * The remaining arguments are a =printf format string and the
    176  * corresponding arguments.
    177  *
    178  * The =openpam_log function does not modify the value of :errno.
    179  */
    180