openpam_log.c revision 91100
191094Sdes/*-
291094Sdes * Copyright (c) 2002 Networks Associates Technologies, Inc.
391094Sdes * All rights reserved.
491094Sdes *
591094Sdes * This software was developed for the FreeBSD Project by ThinkSec AS and
691094Sdes * NAI Labs, the Security Research Division of Network Associates, Inc.
791094Sdes * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
891094Sdes * DARPA CHATS research program.
991094Sdes *
1091094Sdes * Redistribution and use in source and binary forms, with or without
1191094Sdes * modification, are permitted provided that the following conditions
1291094Sdes * are met:
1391094Sdes * 1. Redistributions of source code must retain the above copyright
1491094Sdes *    notice, this list of conditions and the following disclaimer.
1591094Sdes * 2. Redistributions in binary form must reproduce the above copyright
1691094Sdes *    notice, this list of conditions and the following disclaimer in the
1791094Sdes *    documentation and/or other materials provided with the distribution.
1891094Sdes * 3. The name of the author may not be used to endorse or promote
1991094Sdes *    products derived from this software without specific prior written
2091094Sdes *    permission.
2191094Sdes *
2291094Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2391094Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2491094Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2591094Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2691094Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2791094Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2891094Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2991094Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3091094Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3191094Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3291094Sdes * SUCH DAMAGE.
3391094Sdes *
3491094Sdes * $Id$
3591094Sdes */
3691094Sdes
3791097Sdes#include <ctype.h>
3891094Sdes#include <stdarg.h>
3991094Sdes#include <stdio.h>
4091094Sdes#include <stdlib.h>
4191094Sdes#include <syslog.h>
4291094Sdes
4391094Sdes#include <security/pam_appl.h>
4491094Sdes
4591094Sdes#include "openpam_impl.h"
4691094Sdes
4791094Sdes#if defined(openpam_log)
4891094Sdes
4991094Sdes/*
5091100Sdes * OpenPAM extension
5191100Sdes *
5291094Sdes * Log a message through syslog(3)
5391094Sdes */
5491094Sdes
5591094Sdesvoid
5691094Sdes_openpam_log(int level, const char *func, const char *fmt, ...)
5791094Sdes{
5891094Sdes	va_list ap;
5991094Sdes	char *format;
6091097Sdes	int len, priority;
6191094Sdes
6291094Sdes	switch (level) {
6391094Sdes	case PAM_LOG_DEBUG:
6491094Sdes		priority = LOG_DEBUG;
6591094Sdes		break;
6691094Sdes	case PAM_LOG_VERBOSE:
6791094Sdes		priority = LOG_INFO;
6891094Sdes		break;
6991094Sdes	case PAM_LOG_NOTICE:
7091094Sdes		priority = LOG_NOTICE;
7191094Sdes		break;
7291094Sdes	case PAM_LOG_ERROR:
7391094Sdes		priority = LOG_ERR;
7491094Sdes		break;
7591094Sdes	}
7691094Sdes	va_start(ap, fmt);
7791097Sdes	for (len = strlen(fmt); len > 0 && isspace(fmt[len]); len--)
7891097Sdes		/* nothing */;
7991097Sdes	if ((format = malloc(strlen(func) + len + 16)) != NULL) {
8091097Sdes		sprintf(format, "in %s(): %.*s\n", func, len, fmt);
8191094Sdes		vsyslog(priority, format, ap);
8291097Sdes#ifdef DEBUG
8391097Sdes		vfprintf(stderr, format, ap);
8491097Sdes#endif
8591094Sdes		free(format);
8691094Sdes	} else {
8791094Sdes		vsyslog(priority, fmt, ap);
8891094Sdes	}
8991094Sdes	va_end(ap);
9091094Sdes}
9191094Sdes
9291094Sdes#else
9391094Sdes
9491094Sdes/*
9591094Sdes * If openpam_log isn't defined as a macro, we're on a platform that
9691094Sdes * doesn't support varadic macros (or it does but we aren't aware of
9791094Sdes * it).  Do the next best thing.
9891094Sdes */
9991094Sdes
10091094Sdesvoid
10191094Sdesopenpam_log(int level, const char *fmt, ...)
10291094Sdes{
10391094Sdes	va_list ap;
10491094Sdes	int priority;
10591094Sdes
10691094Sdes	switch (level) {
10791094Sdes	case PAM_LOG_DEBUG:
10891094Sdes		priority = LOG_DEBUG;
10991094Sdes		break;
11091094Sdes	case PAM_LOG_VERBOSE:
11191094Sdes		priority = LOG_INFO;
11291094Sdes		break;
11391094Sdes	case PAM_LOG_NOTICE:
11491094Sdes		priority = LOG_NOTICE;
11591094Sdes		break;
11691094Sdes	case PAM_LOG_ERROR:
11791094Sdes		priority = LOG_ERR;
11891094Sdes		break;
11991094Sdes	}
12091094Sdes	va_start(ap, fmt);
12191094Sdes	vsyslog(priority, fmt, ap);
12291094Sdes	va_end(ap);
12391094Sdes}
12491094Sdes
12591094Sdes#endif
12691100Sdes
12791100Sdes/*
12891100Sdes * NOLIST
12991100Sdes */
130