10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*8309SAnthony.Scarpino@Sun.COM * Common Development and Distribution License (the "License").
6*8309SAnthony.Scarpino@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*8309SAnthony.Scarpino@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <stdio.h>
270Sstevel@tonic-gate #include <stdlib.h>
280Sstevel@tonic-gate #include <stdarg.h>
290Sstevel@tonic-gate #include <syslog.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate #include <cryptoutil.h>
320Sstevel@tonic-gate
330Sstevel@tonic-gate #define CRYPTO_DEBUG_ENV "SUNW_CRYPTO_DEBUG"
340Sstevel@tonic-gate
350Sstevel@tonic-gate static char *_cryptodebug_prefix = NULL;
360Sstevel@tonic-gate static int _cryptodebug_enabled = -1; /* -1 unknown, 0 disabled, 1 enabled */
37*8309SAnthony.Scarpino@Sun.COM static int _cryptoerror_enabled = 1; /* 0 disabled, 1 enabled */
380Sstevel@tonic-gate static boolean_t _cryptodebug_syslog = B_TRUE;
390Sstevel@tonic-gate
400Sstevel@tonic-gate /*PRINTFLIKE1*/
410Sstevel@tonic-gate void
cryptodebug(const char * fmt,...)420Sstevel@tonic-gate cryptodebug(const char *fmt, ...)
430Sstevel@tonic-gate {
440Sstevel@tonic-gate va_list args;
450Sstevel@tonic-gate char fmtbuf[BUFSIZ];
460Sstevel@tonic-gate char msgbuf[BUFSIZ];
470Sstevel@tonic-gate
480Sstevel@tonic-gate if (fmt == NULL || _cryptodebug_enabled != 1)
490Sstevel@tonic-gate return;
500Sstevel@tonic-gate
510Sstevel@tonic-gate va_start(args, fmt);
520Sstevel@tonic-gate if (_cryptodebug_prefix == NULL) {
530Sstevel@tonic-gate (void) vsnprintf(msgbuf, sizeof (msgbuf), fmt, args);
540Sstevel@tonic-gate } else {
550Sstevel@tonic-gate (void) snprintf(fmtbuf, sizeof (fmtbuf), "%s: %s",
560Sstevel@tonic-gate _cryptodebug_prefix, fmt);
570Sstevel@tonic-gate (void) vsnprintf(msgbuf, sizeof (msgbuf), fmtbuf, args);
580Sstevel@tonic-gate }
590Sstevel@tonic-gate
600Sstevel@tonic-gate if (_cryptodebug_syslog) {
610Sstevel@tonic-gate syslog(LOG_DEBUG, msgbuf);
620Sstevel@tonic-gate } else {
630Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", msgbuf);
640Sstevel@tonic-gate }
650Sstevel@tonic-gate va_end(args);
660Sstevel@tonic-gate }
670Sstevel@tonic-gate
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate * cryptoerror
700Sstevel@tonic-gate *
710Sstevel@tonic-gate * This is intended to be used both by interactive commands like cryptoadm(1m)
720Sstevel@tonic-gate * digest(1) etc, and by libraries libpkcs11, libelfsign etc.
730Sstevel@tonic-gate *
740Sstevel@tonic-gate * A library probably wants most (all?) of its errors going to syslog but
750Sstevel@tonic-gate * commands are usually happy for them to go to stderr.
760Sstevel@tonic-gate *
770Sstevel@tonic-gate * If a syslog priority is passed we log on that priority. Otherwise we
780Sstevel@tonic-gate * use LOG_STDERR to mean use stderr instead. LOG_STDERR is defined in
790Sstevel@tonic-gate * cryptoutil.h
800Sstevel@tonic-gate */
810Sstevel@tonic-gate
820Sstevel@tonic-gate /*PRINTFLIKE2*/
830Sstevel@tonic-gate void
cryptoerror(int priority,const char * fmt,...)840Sstevel@tonic-gate cryptoerror(int priority, const char *fmt, ...)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate char fmtbuf[BUFSIZ];
870Sstevel@tonic-gate char msgbuf[BUFSIZ];
880Sstevel@tonic-gate va_list args;
890Sstevel@tonic-gate
90*8309SAnthony.Scarpino@Sun.COM if (fmt == NULL || _cryptoerror_enabled == 0)
910Sstevel@tonic-gate return;
920Sstevel@tonic-gate
930Sstevel@tonic-gate va_start(args, fmt);
940Sstevel@tonic-gate if (_cryptodebug_prefix == NULL) {
950Sstevel@tonic-gate (void) vsnprintf(msgbuf, sizeof (msgbuf), fmt, args);
960Sstevel@tonic-gate } else {
970Sstevel@tonic-gate (void) snprintf(fmtbuf, sizeof (fmtbuf), "%s: %s",
980Sstevel@tonic-gate _cryptodebug_prefix, fmt);
990Sstevel@tonic-gate (void) vsnprintf(msgbuf, sizeof (msgbuf), fmtbuf, args);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate if ((priority == LOG_STDERR) || (priority < 0)) {
1030Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", msgbuf);
1040Sstevel@tonic-gate } else {
1050Sstevel@tonic-gate syslog(priority, msgbuf);
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate va_end(args);
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate void
cryptoerror_off()111*8309SAnthony.Scarpino@Sun.COM cryptoerror_off()
112*8309SAnthony.Scarpino@Sun.COM {
113*8309SAnthony.Scarpino@Sun.COM _cryptoerror_enabled = 0;
114*8309SAnthony.Scarpino@Sun.COM }
115*8309SAnthony.Scarpino@Sun.COM
116*8309SAnthony.Scarpino@Sun.COM void
cryptoerror_on()117*8309SAnthony.Scarpino@Sun.COM cryptoerror_on()
118*8309SAnthony.Scarpino@Sun.COM {
119*8309SAnthony.Scarpino@Sun.COM _cryptoerror_enabled = 1;
120*8309SAnthony.Scarpino@Sun.COM }
121*8309SAnthony.Scarpino@Sun.COM
122*8309SAnthony.Scarpino@Sun.COM void
cryptodebug_init(const char * prefix)1230Sstevel@tonic-gate cryptodebug_init(const char *prefix)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate char *envval = NULL;
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate if (prefix != NULL) {
1280Sstevel@tonic-gate _cryptodebug_prefix = strdup(prefix);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate if (_cryptodebug_enabled == -1) {
1320Sstevel@tonic-gate envval = getenv(CRYPTO_DEBUG_ENV);
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate * If unset or it isn't one of syslog or stderr
1350Sstevel@tonic-gate * disable debug.
1360Sstevel@tonic-gate */
1370Sstevel@tonic-gate if (envval == NULL || (strcmp(envval, "") == 0)) {
1380Sstevel@tonic-gate _cryptodebug_enabled = 0;
1390Sstevel@tonic-gate return;
1400Sstevel@tonic-gate } else if (strcmp(envval, "stderr") == 0) {
1410Sstevel@tonic-gate _cryptodebug_syslog = B_FALSE;
1420Sstevel@tonic-gate _cryptodebug_enabled = 1;
1430Sstevel@tonic-gate } else if (strcmp(envval, "syslog") == 0) {
1440Sstevel@tonic-gate _cryptodebug_syslog = B_TRUE;
1450Sstevel@tonic-gate _cryptodebug_enabled = 1;
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate openlog(_cryptodebug_prefix, LOG_PID, LOG_USER);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate #pragma fini(_cryptodebug_fini)
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate static void
_cryptodebug_fini(void)1550Sstevel@tonic-gate _cryptodebug_fini(void)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate if (_cryptodebug_prefix != NULL)
1580Sstevel@tonic-gate free(_cryptodebug_prefix);
1590Sstevel@tonic-gate }
160