xref: /illumos-gate/usr/src/lib/libsldap/common/ns_error.c (revision 598f4ceed9327d2d6c2325dd67cae3aa06f7fea6)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 
26 #include <stdlib.h>
27 #include <libintl.h>
28 #include "ns_sldap.h"
29 #include "ns_internal.h"
30 
31 struct ns_ldaperror {
32 	int	e_code;
33 	char	*e_reason;
34 };
35 
36 static mutex_t		ns_error_lock = DEFAULTMUTEX;
37 static boolean_t	error_inited = B_FALSE;
38 
39 static struct ns_ldaperror ns_ldap_errlist[] = {
40 	{NS_LDAP_SUCCESS,	NULL},
41 	{NS_LDAP_OP_FAILED,	NULL},
42 	{NS_LDAP_NOTFOUND,	NULL},
43 	{NS_LDAP_MEMORY,	NULL},
44 	{NS_LDAP_CONFIG,	NULL},
45 	{NS_LDAP_PARTIAL,	NULL},
46 	{NS_LDAP_INTERNAL,	NULL},
47 	{NS_LDAP_INVALID_PARAM,	NULL},
48 	{-1,			NULL}
49 };
50 
51 
52 static void
53 ns_ldaperror_init()
54 {
55 	int 	i = 0;
56 
57 	(void) mutex_lock(&ns_error_lock);
58 	if (!error_inited) {
59 		ns_ldap_errlist[i++].e_reason = gettext("Success");
60 		ns_ldap_errlist[i++].e_reason = gettext("Operation failed");
61 		ns_ldap_errlist[i++].e_reason = gettext("Object not found");
62 		ns_ldap_errlist[i++].e_reason = gettext("Memory failure");
63 		ns_ldap_errlist[i++].e_reason =
64 		    gettext("LDAP configuration problem");
65 		ns_ldap_errlist[i++].e_reason = gettext("Partial result");
66 		ns_ldap_errlist[i++].e_reason = gettext("LDAP error");
67 		ns_ldap_errlist[i++].e_reason = gettext("Invalid parameter");
68 		ns_ldap_errlist[i++].e_reason = gettext("Unknown error");
69 		error_inited = B_TRUE;
70 	}
71 	(void) mutex_unlock(&ns_error_lock);
72 }
73 
74 
75 int
76 __ns_ldap_err2str(int err, char **strmsg)
77 {
78 	int	i;
79 
80 	if (!error_inited)
81 		ns_ldaperror_init();
82 
83 	for (i = 0; (ns_ldap_errlist[i].e_code != err) &&
84 	    (ns_ldap_errlist[i].e_code != -1); i++) {
85 		/* empty for loop */
86 	}
87 	*strmsg = ns_ldap_errlist[i].e_reason;
88 	return (NS_LDAP_SUCCESS);
89 }
90 
91 
92 int
93 __ns_ldap_freeError(ns_ldap_error_t **errorp)
94 {
95 	ns_ldap_error_t *err;
96 
97 	if (errorp == NULL || *errorp == NULL)
98 		return (NS_LDAP_SUCCESS);
99 
100 	err = *errorp;
101 	if (err->message)
102 		free(err->message);
103 
104 	free(err);
105 	*errorp = NULL;
106 	return (NS_LDAP_SUCCESS);
107 }
108