wbinfo: Fix Coverity ID 242685 Resource leak
[Samba/gebeck_regimport.git] / source3 / libads / ads_status.c
blobfc489a9f00576df45d1ed2657f6ca803286e39dd
1 /*
2 Unix SMB/CIFS implementation.
3 ads (active directory) utility library
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Remus Koos 2001
6 Copyright (C) Andrew Bartlett 2001
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "smb_krb5.h"
25 #include "system/gssapi.h"
26 #include "smb_ldap.h"
27 #include "libads/ads_status.h"
30 build a ADS_STATUS structure
32 ADS_STATUS ads_build_error(enum ads_error_type etype,
33 int rc, int minor_status)
35 ADS_STATUS ret;
37 if (etype == ENUM_ADS_ERROR_NT) {
38 DEBUG(0,("don't use ads_build_error with ENUM_ADS_ERROR_NT!\n"));
39 ret.err.rc = -1;
40 ret.error_type = ENUM_ADS_ERROR_SYSTEM;
41 ret.minor_status = 0;
42 return ret;
45 ret.err.rc = rc;
46 ret.error_type = etype;
47 ret.minor_status = minor_status;
48 return ret;
51 ADS_STATUS ads_build_nt_error(enum ads_error_type etype,
52 NTSTATUS nt_status)
54 ADS_STATUS ret;
56 if (etype != ENUM_ADS_ERROR_NT) {
57 DEBUG(0,("don't use ads_build_nt_error without ENUM_ADS_ERROR_NT!\n"));
58 ret.err.rc = -1;
59 ret.error_type = ENUM_ADS_ERROR_SYSTEM;
60 ret.minor_status = 0;
61 return ret;
63 ret.err.nt_status = nt_status;
64 ret.error_type = etype;
65 ret.minor_status = 0;
66 return ret;
70 do a rough conversion between ads error codes and NT status codes
71 we'll need to fill this in more
73 NTSTATUS ads_ntstatus(ADS_STATUS status)
75 switch (status.error_type) {
76 case ENUM_ADS_ERROR_NT:
77 return status.err.nt_status;
78 case ENUM_ADS_ERROR_SYSTEM:
79 return map_nt_error_from_unix(status.err.rc);
80 #ifdef HAVE_LDAP
81 case ENUM_ADS_ERROR_LDAP:
82 if (status.err.rc == LDAP_SUCCESS) {
83 return NT_STATUS_OK;
85 return NT_STATUS_LDAP(status.err.rc);
86 #endif
87 #ifdef HAVE_KRB5
88 case ENUM_ADS_ERROR_KRB5:
89 return krb5_to_nt_status(status.err.rc);
90 #endif
91 default:
92 break;
95 if (ADS_ERR_OK(status)) {
96 return NT_STATUS_OK;
98 return NT_STATUS_UNSUCCESSFUL;
102 return a string for an error from a ads routine
104 const char *ads_errstr(ADS_STATUS status)
106 switch (status.error_type) {
107 case ENUM_ADS_ERROR_SYSTEM:
108 return strerror(status.err.rc);
109 #ifdef HAVE_LDAP
110 case ENUM_ADS_ERROR_LDAP:
111 return ldap_err2string(status.err.rc);
112 #endif
113 #ifdef HAVE_KRB5
114 case ENUM_ADS_ERROR_KRB5:
115 return error_message(status.err.rc);
116 case ENUM_ADS_ERROR_GSS:
118 char *ret;
119 uint32 msg_ctx;
120 uint32 minor;
121 gss_buffer_desc msg1, msg2;
123 msg_ctx = 0;
125 msg1.value = NULL;
126 msg2.value = NULL;
127 gss_display_status(&minor, status.err.rc, GSS_C_GSS_CODE,
128 GSS_C_NULL_OID, &msg_ctx, &msg1);
129 gss_display_status(&minor, status.minor_status, GSS_C_MECH_CODE,
130 GSS_C_NULL_OID, &msg_ctx, &msg2);
131 ret = talloc_asprintf(talloc_tos(), "%s : %s",
132 (char *)msg1.value, (char *)msg2.value);
133 SMB_ASSERT(ret != NULL);
134 gss_release_buffer(&minor, &msg1);
135 gss_release_buffer(&minor, &msg2);
136 return ret;
138 #endif
139 case ENUM_ADS_ERROR_NT:
140 return get_friendly_nt_error_msg(ads_ntstatus(status));
141 default:
142 return "Unknown ADS error type!? (not compiled in?)";
146 #ifdef HAVE_KRB5
147 NTSTATUS gss_err_to_ntstatus(uint32 maj, uint32 min)
149 ADS_STATUS adss = ADS_ERROR_GSS(maj, min);
150 DEBUG(10,("gss_err_to_ntstatus: Error %s\n",
151 ads_errstr(adss) ));
152 return ads_ntstatus(adss);
154 #endif