openpam_log.c revision 91684
1238046Smarcel/*-
2238046Smarcel * Copyright (c) 2002 Networks Associates Technologies, Inc.
3238046Smarcel * All rights reserved.
4238046Smarcel *
5238046Smarcel * This software was developed for the FreeBSD Project by ThinkSec AS and
6238046Smarcel * NAI Labs, the Security Research Division of Network Associates, Inc.
7238046Smarcel * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
8238046Smarcel * DARPA CHATS research program.
9238046Smarcel *
10238046Smarcel * Redistribution and use in source and binary forms, with or without
11238046Smarcel * modification, are permitted provided that the following conditions
12238046Smarcel * are met:
13238046Smarcel * 1. Redistributions of source code must retain the above copyright
14238046Smarcel *    notice, this list of conditions and the following disclaimer.
15238046Smarcel * 2. Redistributions in binary form must reproduce the above copyright
16238046Smarcel *    notice, this list of conditions and the following disclaimer in the
17238046Smarcel *    documentation and/or other materials provided with the distribution.
18238046Smarcel * 3. The name of the author may not be used to endorse or promote
19238046Smarcel *    products derived from this software without specific prior written
20238046Smarcel *    permission.
21238046Smarcel *
22238046Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23238046Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24238046Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25238046Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26238046Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27238046Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28238046Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29238046Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30238046Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31238046Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32238046Smarcel * SUCH DAMAGE.
33238046Smarcel *
34238046Smarcel * $P4: //depot/projects/openpam/lib/openpam_log.c#9 $
35238046Smarcel */
36238046Smarcel
37238046Smarcel#include <ctype.h>
38238046Smarcel#include <stdarg.h>
39238046Smarcel#include <stdio.h>
40238046Smarcel#include <stdlib.h>
41238046Smarcel#include <syslog.h>
42238046Smarcel
43238046Smarcel#include <security/pam_appl.h>
44238046Smarcel
45238046Smarcel#include "openpam_impl.h"
46238046Smarcel
47238046Smarcel#if defined(openpam_log)
48238046Smarcel
49238046Smarcel/*
50238046Smarcel * OpenPAM extension
51238046Smarcel *
52238046Smarcel * Log a message through syslog(3)
53238046Smarcel */
54238046Smarcel
55238046Smarcelvoid
56238046Smarcel_openpam_log(int level, const char *func, const char *fmt, ...)
57238046Smarcel{
58238046Smarcel	va_list ap;
59238046Smarcel	char *format;
60238046Smarcel	int len, priority;
61238046Smarcel
62238046Smarcel	switch (level) {
63238046Smarcel	case PAM_LOG_DEBUG:
64238046Smarcel		priority = LOG_DEBUG;
65238046Smarcel		break;
66238046Smarcel	case PAM_LOG_VERBOSE:
67238046Smarcel		priority = LOG_INFO;
68238046Smarcel		break;
69238046Smarcel	case PAM_LOG_NOTICE:
70238046Smarcel		priority = LOG_NOTICE;
71238046Smarcel		break;
72238046Smarcel	case PAM_LOG_ERROR:
73238046Smarcel		priority = LOG_ERR;
74238046Smarcel		break;
75238046Smarcel	}
76238046Smarcel	va_start(ap, fmt);
77238046Smarcel	for (len = strlen(fmt); len > 0 && isspace(fmt[len]); len--)
78238046Smarcel		/* nothing */;
79238046Smarcel	if ((format = malloc(strlen(func) + len + 16)) != NULL) {
80238046Smarcel		sprintf(format, "in %s(): %.*s\n", func, len, fmt);
81238046Smarcel		vsyslog(priority, format, ap);
82238046Smarcel#ifdef DEBUG
83238046Smarcel		vfprintf(stderr, format, ap);
84238046Smarcel#endif
85238046Smarcel		free(format);
86238046Smarcel	} else {
87238046Smarcel		vsyslog(priority, fmt, ap);
88238046Smarcel	}
89238046Smarcel	va_end(ap);
90238046Smarcel}
91238046Smarcel
92238046Smarcel#else
93238046Smarcel
94238046Smarcel/*
95238046Smarcel * If openpam_log isn't defined as a macro, we're on a platform that
96238046Smarcel * doesn't support varadic macros (or it does but we aren't aware of
97238046Smarcel * 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