4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
31 #include <cryptoutil.h>
33 #define CRYPTO_DEBUG_ENV "SUNW_CRYPTO_DEBUG"
35 static char *_cryptodebug_prefix
= NULL
;
36 static int _cryptodebug_enabled
= -1; /* -1 unknown, 0 disabled, 1 enabled */
37 static int _cryptoerror_enabled
= 1; /* 0 disabled, 1 enabled */
38 static boolean_t _cryptodebug_syslog
= B_TRUE
;
42 cryptodebug(const char *fmt
, ...)
48 if (fmt
== NULL
|| _cryptodebug_enabled
!= 1)
52 if (_cryptodebug_prefix
== NULL
) {
53 (void) vsnprintf(msgbuf
, sizeof (msgbuf
), fmt
, args
);
55 (void) snprintf(fmtbuf
, sizeof (fmtbuf
), "%s: %s",
56 _cryptodebug_prefix
, fmt
);
57 (void) vsnprintf(msgbuf
, sizeof (msgbuf
), fmtbuf
, args
);
60 if (_cryptodebug_syslog
) {
61 syslog(LOG_DEBUG
, msgbuf
);
63 (void) fprintf(stderr
, "%s\n", msgbuf
);
71 * This is intended to be used both by interactive commands like cryptoadm(1m)
72 * digest(1) etc, and by libraries libpkcs11, libelfsign etc.
74 * A library probably wants most (all?) of its errors going to syslog but
75 * commands are usually happy for them to go to stderr.
77 * If a syslog priority is passed we log on that priority. Otherwise we
78 * use LOG_STDERR to mean use stderr instead. LOG_STDERR is defined in
84 cryptoerror(int priority
, const char *fmt
, ...)
90 if (fmt
== NULL
|| _cryptoerror_enabled
== 0)
94 if (_cryptodebug_prefix
== NULL
) {
95 (void) vsnprintf(msgbuf
, sizeof (msgbuf
), fmt
, args
);
97 (void) snprintf(fmtbuf
, sizeof (fmtbuf
), "%s: %s",
98 _cryptodebug_prefix
, fmt
);
99 (void) vsnprintf(msgbuf
, sizeof (msgbuf
), fmtbuf
, args
);
102 if ((priority
== LOG_STDERR
) || (priority
< 0)) {
103 (void) fprintf(stderr
, "%s\n", msgbuf
);
105 syslog(priority
, msgbuf
);
113 _cryptoerror_enabled
= 0;
119 _cryptoerror_enabled
= 1;
123 cryptodebug_init(const char *prefix
)
127 if (prefix
!= NULL
) {
128 _cryptodebug_prefix
= strdup(prefix
);
131 if (_cryptodebug_enabled
== -1) {
132 envval
= getenv(CRYPTO_DEBUG_ENV
);
134 * If unset or it isn't one of syslog or stderr
137 if (envval
== NULL
|| (strcmp(envval
, "") == 0)) {
138 _cryptodebug_enabled
= 0;
140 } else if (strcmp(envval
, "stderr") == 0) {
141 _cryptodebug_syslog
= B_FALSE
;
142 _cryptodebug_enabled
= 1;
143 } else if (strcmp(envval
, "syslog") == 0) {
144 _cryptodebug_syslog
= B_TRUE
;
145 _cryptodebug_enabled
= 1;
149 openlog(_cryptodebug_prefix
, LOG_PID
, LOG_USER
);
152 #pragma fini(_cryptodebug_fini)
155 _cryptodebug_fini(void)
157 free(_cryptodebug_prefix
);