xref: /netbsd-src/external/bsd/openpam/dist/lib/libpam/openpam_log.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: openpam_log.c,v 1.3 2017/05/06 19:50:09 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  * $OpenPAM: openpam_log.c 938 2017-04-30 21:34:42Z des $
38  */
39 
40 #ifdef HAVE_CONFIG_H
41 # include "config.h"
42 #endif
43 
44 #include <sys/cdefs.h>
45 __RCSID("$NetBSD: openpam_log.c,v 1.3 2017/05/06 19:50:09 christos Exp $");
46 
47 #include <errno.h>
48 #include <stdarg.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <syslog.h>
52 
53 #include <security/pam_appl.h>
54 
55 #include "openpam_impl.h"
56 #include "openpam_asprintf.h"
57 
58 int openpam_debug = 0;
59 
60 #if !defined(openpam_log)
61 
62 /*
63  * OpenPAM extension
64  *
65  * Log a message through syslog
66  */
67 
68 void
69 openpam_log(int level, const char *fmt, ...)
70 {
71 	va_list ap;
72 	int priority;
73 	int serrno;
74 
75 	switch (level) {
76 	case PAM_LOG_LIBDEBUG:
77 	case PAM_LOG_DEBUG:
78 		if (!openpam_debug)
79 			return;
80 		priority = LOG_DEBUG;
81 		break;
82 	case PAM_LOG_VERBOSE:
83 		priority = LOG_INFO;
84 		break;
85 	case PAM_LOG_NOTICE:
86 		priority = LOG_NOTICE;
87 		break;
88 	case PAM_LOG_ERROR:
89 	default:
90 		priority = LOG_ERR;
91 		break;
92 	}
93 	serrno = errno;
94 	va_start(ap, fmt);
95 	vsyslog(priority, fmt, ap);
96 	va_end(ap);
97 	errno = serrno;
98 }
99 
100 #else
101 
102 #if defined(__clang__) || __GNUC_PREREQ__(4, 5)
103 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
104 #endif
105 void
106 _openpam_log(int level, const char *func, const char *fmt, ...)
107 {
108 	va_list ap;
109 	char *format;
110 	int priority;
111 	int serrno;
112 
113 	switch (level) {
114 	case PAM_LOG_LIBDEBUG:
115 	case PAM_LOG_DEBUG:
116 		if (!openpam_debug)
117 			return;
118 		priority = LOG_DEBUG;
119 		break;
120 	case PAM_LOG_VERBOSE:
121 		priority = LOG_INFO;
122 		break;
123 	case PAM_LOG_NOTICE:
124 		priority = LOG_NOTICE;
125 		break;
126 	case PAM_LOG_ERROR:
127 	default:
128 		priority = LOG_ERR;
129 		break;
130 	}
131 	serrno = errno;
132 	va_start(ap, fmt);
133 	if (asprintf(&format, "in %s(): %s", func, fmt) > 0) {
134 		errno = serrno;
135 		vsyslog(priority, format, ap);
136 		FREE(format);
137 	} else {
138 		errno = serrno;
139 		vsyslog(priority, fmt, ap);
140 	}
141 	va_end(ap);
142 	errno = serrno;
143 }
144 
145 #endif
146 
147 /**
148  * The =openpam_log function logs messages using =syslog.
149  * It is primarily intended for internal use by the library and modules.
150  *
151  * The =level argument indicates the importance of the message.
152  * The following levels are defined:
153  *
154  *	=PAM_LOG_LIBDEBUG:
155  *		Debugging messages.
156  *		For internal use only.
157  *	=PAM_LOG_DEBUG:
158  *		Debugging messages.
159  *		These messages are normally not logged unless the global
160  *		integer variable :openpam_debug is set to a non-zero
161  *		value, in which case they are logged with a =syslog
162  *		priority of =LOG_DEBUG.
163  *	=PAM_LOG_VERBOSE:
164  *		Information about the progress of the authentication
165  *		process, or other non-essential messages.
166  *		These messages are logged with a =syslog priority of
167  *		=LOG_INFO.
168  *	=PAM_LOG_NOTICE:
169  *		Messages relating to non-fatal errors.
170  *		These messages are logged with a =syslog priority of
171  *		=LOG_NOTICE.
172  *	=PAM_LOG_ERROR:
173  *		Messages relating to serious errors.
174  *		These messages are logged with a =syslog priority of
175  *		=LOG_ERR.
176  *
177  * The remaining arguments are a =printf format string and the
178  * corresponding arguments.
179  *
180  * The =openpam_log function does not modify the value of :errno.
181  */
182