17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*a7e661a2SAnthony Scarpino  * Common Development and Distribution License (the "License").
6*a7e661a2SAnthony Scarpino  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*a7e661a2SAnthony Scarpino  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <stdarg.h>
297c478bd9Sstevel@tonic-gate #include <syslog.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <cryptoutil.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #define	CRYPTO_DEBUG_ENV	"SUNW_CRYPTO_DEBUG"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate static char *_cryptodebug_prefix = NULL;
367c478bd9Sstevel@tonic-gate static int _cryptodebug_enabled = -1; /* -1 unknown, 0 disabled, 1 enabled */
37*a7e661a2SAnthony Scarpino static int _cryptoerror_enabled = 1; /* 0 disabled, 1 enabled */
387c478bd9Sstevel@tonic-gate static boolean_t _cryptodebug_syslog = B_TRUE;
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
417c478bd9Sstevel@tonic-gate void
cryptodebug(const char * fmt,...)427c478bd9Sstevel@tonic-gate cryptodebug(const char *fmt, ...)
437c478bd9Sstevel@tonic-gate {
447c478bd9Sstevel@tonic-gate 	va_list args;
457c478bd9Sstevel@tonic-gate 	char fmtbuf[BUFSIZ];
467c478bd9Sstevel@tonic-gate 	char msgbuf[BUFSIZ];
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 	if (fmt == NULL || _cryptodebug_enabled != 1)
497c478bd9Sstevel@tonic-gate 		return;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
527c478bd9Sstevel@tonic-gate 	if (_cryptodebug_prefix == NULL) {
537c478bd9Sstevel@tonic-gate 		(void) vsnprintf(msgbuf, sizeof (msgbuf), fmt, args);
547c478bd9Sstevel@tonic-gate 	} else {
557c478bd9Sstevel@tonic-gate 		(void) snprintf(fmtbuf, sizeof (fmtbuf), "%s: %s",
567c478bd9Sstevel@tonic-gate 		    _cryptodebug_prefix, fmt);
577c478bd9Sstevel@tonic-gate 		(void) vsnprintf(msgbuf, sizeof (msgbuf), fmtbuf, args);
587c478bd9Sstevel@tonic-gate 	}
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	if (_cryptodebug_syslog) {
617c478bd9Sstevel@tonic-gate 		syslog(LOG_DEBUG, msgbuf);
627c478bd9Sstevel@tonic-gate 	} else {
637c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s\n", msgbuf);
647c478bd9Sstevel@tonic-gate 	}
657c478bd9Sstevel@tonic-gate 	va_end(args);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * cryptoerror
707c478bd9Sstevel@tonic-gate  *
717c478bd9Sstevel@tonic-gate  * This is intended to be used both by interactive commands like cryptoadm(1m)
727c478bd9Sstevel@tonic-gate  * digest(1) etc, and by libraries libpkcs11, libelfsign etc.
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  * A library probably wants most (all?) of its errors going to syslog but
757c478bd9Sstevel@tonic-gate  * commands are usually happy for them to go to stderr.
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  * If a syslog priority is passed we log on that priority.  Otherwise we
787c478bd9Sstevel@tonic-gate  * use LOG_STDERR to mean use stderr instead. LOG_STDERR is defined in
797c478bd9Sstevel@tonic-gate  * cryptoutil.h
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
837c478bd9Sstevel@tonic-gate void
cryptoerror(int priority,const char * fmt,...)847c478bd9Sstevel@tonic-gate cryptoerror(int priority, const char *fmt, ...)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	char fmtbuf[BUFSIZ];
877c478bd9Sstevel@tonic-gate 	char msgbuf[BUFSIZ];
887c478bd9Sstevel@tonic-gate 	va_list args;
897c478bd9Sstevel@tonic-gate 
90*a7e661a2SAnthony Scarpino 	if (fmt == NULL || _cryptoerror_enabled == 0)
917c478bd9Sstevel@tonic-gate 		return;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
947c478bd9Sstevel@tonic-gate 	if (_cryptodebug_prefix == NULL) {
957c478bd9Sstevel@tonic-gate 		(void) vsnprintf(msgbuf, sizeof (msgbuf), fmt, args);
967c478bd9Sstevel@tonic-gate 	} else {
977c478bd9Sstevel@tonic-gate 		(void) snprintf(fmtbuf, sizeof (fmtbuf), "%s: %s",
987c478bd9Sstevel@tonic-gate 		    _cryptodebug_prefix, fmt);
997c478bd9Sstevel@tonic-gate 		(void) vsnprintf(msgbuf, sizeof (msgbuf), fmtbuf, args);
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	if ((priority == LOG_STDERR) || (priority < 0))  {
1037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s\n", msgbuf);
1047c478bd9Sstevel@tonic-gate 	} else {
1057c478bd9Sstevel@tonic-gate 		syslog(priority, msgbuf);
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 	va_end(args);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
110*a7e661a2SAnthony Scarpino void
cryptoerror_off()111*a7e661a2SAnthony Scarpino cryptoerror_off()
112*a7e661a2SAnthony Scarpino {
113*a7e661a2SAnthony Scarpino 	_cryptoerror_enabled = 0;
114*a7e661a2SAnthony Scarpino }
115*a7e661a2SAnthony Scarpino 
116*a7e661a2SAnthony Scarpino void
cryptoerror_on()117*a7e661a2SAnthony Scarpino cryptoerror_on()
118*a7e661a2SAnthony Scarpino {
119*a7e661a2SAnthony Scarpino 	_cryptoerror_enabled = 1;
120*a7e661a2SAnthony Scarpino }
121*a7e661a2SAnthony Scarpino 
1227c478bd9Sstevel@tonic-gate void
cryptodebug_init(const char * prefix)1237c478bd9Sstevel@tonic-gate cryptodebug_init(const char *prefix)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	char *envval = NULL;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (prefix != NULL) {
1287c478bd9Sstevel@tonic-gate 		_cryptodebug_prefix = strdup(prefix);
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if (_cryptodebug_enabled == -1) {
1327c478bd9Sstevel@tonic-gate 		envval = getenv(CRYPTO_DEBUG_ENV);
1337c478bd9Sstevel@tonic-gate 		/*
1347c478bd9Sstevel@tonic-gate 		 * If unset or it isn't one of syslog or stderr
1357c478bd9Sstevel@tonic-gate 		 * disable debug.
1367c478bd9Sstevel@tonic-gate 		 */
1377c478bd9Sstevel@tonic-gate 		if (envval == NULL || (strcmp(envval, "") == 0)) {
1387c478bd9Sstevel@tonic-gate 			_cryptodebug_enabled = 0;
1397c478bd9Sstevel@tonic-gate 			return;
1407c478bd9Sstevel@tonic-gate 		} else if (strcmp(envval, "stderr") == 0) {
1417c478bd9Sstevel@tonic-gate 			_cryptodebug_syslog = B_FALSE;
1427c478bd9Sstevel@tonic-gate 			_cryptodebug_enabled = 1;
1437c478bd9Sstevel@tonic-gate 		} else if (strcmp(envval, "syslog") == 0) {
1447c478bd9Sstevel@tonic-gate 			_cryptodebug_syslog = B_TRUE;
1457c478bd9Sstevel@tonic-gate 			_cryptodebug_enabled = 1;
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	openlog(_cryptodebug_prefix, LOG_PID, LOG_USER);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate #pragma fini(_cryptodebug_fini)
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate static void
_cryptodebug_fini(void)1557c478bd9Sstevel@tonic-gate _cryptodebug_fini(void)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	if (_cryptodebug_prefix != NULL)
1587c478bd9Sstevel@tonic-gate 		free(_cryptodebug_prefix);
1597c478bd9Sstevel@tonic-gate }
160