xref: /titanic_50/usr/src/lib/libsldap/common/ns_error.c (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1999-2002 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdlib.h>
30 #include <libintl.h>
31 #include "ns_sldap.h"
32 #include "ns_internal.h"
33 
34 struct ns_ldaperror {
35 	int	e_code;
36 	char	*e_reason;
37 };
38 
39 static mutex_t		ns_error_lock = DEFAULTMUTEX;
40 static boolean_t	error_inited = B_FALSE;
41 
42 static struct ns_ldaperror ns_ldap_errlist[] = {
43 	{NS_LDAP_SUCCESS,	NULL},
44 	{NS_LDAP_OP_FAILED,	NULL},
45 	{NS_LDAP_NOTFOUND,	NULL},
46 	{NS_LDAP_MEMORY,	NULL},
47 	{NS_LDAP_CONFIG,	NULL},
48 	{NS_LDAP_PARTIAL,	NULL},
49 	{NS_LDAP_INTERNAL,	NULL},
50 	{NS_LDAP_INVALID_PARAM,	NULL},
51 	{-1,			NULL}
52 };
53 
54 
55 static void
56 ns_ldaperror_init()
57 {
58 	int 	i = 0;
59 
60 	(void) mutex_lock(&ns_error_lock);
61 	if (!error_inited) {
62 		ns_ldap_errlist[i++].e_reason = gettext("Success");
63 		ns_ldap_errlist[i++].e_reason = gettext("Operation failed");
64 		ns_ldap_errlist[i++].e_reason = gettext("Object not found");
65 		ns_ldap_errlist[i++].e_reason = gettext("Memory failure");
66 		ns_ldap_errlist[i++].e_reason =
67 			gettext("LDAP configuration problem");
68 		ns_ldap_errlist[i++].e_reason = gettext("Partial result");
69 		ns_ldap_errlist[i++].e_reason = gettext("LDAP error");
70 		ns_ldap_errlist[i++].e_reason = gettext("Invalid parameter");
71 		ns_ldap_errlist[i++].e_reason = gettext("Unknown error");
72 		error_inited = B_TRUE;
73 	}
74 	(void) mutex_unlock(&ns_error_lock);
75 }
76 
77 
78 int
79 __ns_ldap_err2str(int err, char **strmsg)
80 {
81 	int	i;
82 
83 	if (!error_inited)
84 		ns_ldaperror_init();
85 
86 	for (i = 0; (ns_ldap_errlist[i].e_code != err) &&
87 			(ns_ldap_errlist[i].e_code != -1); i++) {
88 		/* empty for loop */
89 	}
90 	*strmsg = ns_ldap_errlist[i].e_reason;
91 	return (NS_LDAP_SUCCESS);
92 }
93 
94 
95 int
96 __ns_ldap_freeError(ns_ldap_error_t **errorp)
97 {
98 	ns_ldap_error_t *err = *errorp;
99 	if (err) {
100 		if (err->message)
101 			free(err->message);
102 		free(err);
103 	}
104 	*errorp = NULL;
105 	return (NS_LDAP_SUCCESS);
106 }
107