1/*	$NetBSD: openpam_log.c,v 1.2 2011/12/25 22:27:55 christos Exp $	*/
2
3/*-
4 * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
5 * Copyright (c) 2004-2011 Dag-Erling Sm��rgrav
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 * Id: openpam_log.c 437 2011-09-13 12:00:13Z des
38 */
39
40#ifdef HAVE_CONFIG_H
41# include "config.h"
42#endif
43
44#include <ctype.h>
45#include <stdarg.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#include <syslog.h>
50
51#include <security/pam_appl.h>
52
53#include "openpam_impl.h"
54
55#ifdef OPENPAM_DEBUG
56int openpam_debug = 1;
57#else
58int openpam_debug = 0;
59#endif
60
61#if !defined(openpam_log)
62
63/*
64 * OpenPAM extension
65 *
66 * Log a message through syslog
67 */
68
69void
70openpam_log(int level, const char *fmt, ...)
71{
72	va_list ap;
73	int priority;
74
75	switch (level) {
76	case PAM_LOG_DEBUG:
77		if (!openpam_debug)
78			return;
79		priority = LOG_DEBUG;
80		break;
81	case PAM_LOG_VERBOSE:
82		priority = LOG_INFO;
83		break;
84	case PAM_LOG_NOTICE:
85		priority = LOG_NOTICE;
86		break;
87	case PAM_LOG_ERROR:
88	default:
89		priority = LOG_ERR;
90		break;
91	}
92	va_start(ap, fmt);
93	vsyslog(priority, fmt, ap);
94	va_end(ap);
95}
96
97#else
98
99void
100_openpam_log(int level, const char *func, const char *fmt, ...)
101{
102	va_list ap;
103	char *msg;
104	int priority, rv;
105
106	switch (level) {
107	case PAM_LOG_DEBUG:
108		if (!openpam_debug)
109			return;
110		priority = LOG_DEBUG;
111		break;
112	case PAM_LOG_VERBOSE:
113		priority = LOG_INFO;
114		break;
115	case PAM_LOG_NOTICE:
116		priority = LOG_NOTICE;
117		break;
118	case PAM_LOG_ERROR:
119	default:
120		priority = LOG_ERR;
121		break;
122	}
123
124	va_start(ap, fmt);
125	rv = vasprintf(&msg, fmt, ap);
126	va_end(ap);
127
128	if (rv < 0) {
129		syslog(priority, "Can't format message from %s: %s (%m)",
130		    func, fmt);
131		return;
132	}
133	syslog(priority, "in %s(): %s", func, msg);
134	FREE(msg);
135}
136
137#endif
138
139/**
140 * The =openpam_log function logs messages using =syslog.
141 * It is primarily intended for internal use by the library and modules.
142 *
143 * The =level argument indicates the importance of the message.
144 * The following levels are defined:
145 *
146 *	=PAM_LOG_DEBUG:
147 *		Debugging messages.
148 *		These messages are normally not logged unless the global
149 *		integer variable :openpam_debug is set to a non-zero
150 *		value, in which case they are logged with a =syslog
151 *		priority of =LOG_DEBUG.
152 *	=PAM_LOG_VERBOSE:
153 *		Information about the progress of the authentication
154 *		process, or other non-essential messages.
155 *		These messages are logged with a =syslog priority of
156 *		=LOG_INFO.
157 *	=PAM_LOG_NOTICE:
158 *		Messages relating to non-fatal errors.
159 *		These messages are logged with a =syslog priority of
160 *		=LOG_NOTICE.
161 *	=PAM_LOG_ERROR:
162 *		Messages relating to serious errors.
163 *		These messages are logged with a =syslog priority of
164 *		=LOG_ERR.
165 *
166 * The remaining arguments are a =printf format string and the
167 * corresponding arguments.
168 */
169