742304b6fc4976a55c392b12b7c197b1e3c489fe
[samba.git] / source / libads / ads_status.c
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
7    
8    
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.
13    
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.
18    
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/>.
21 */
22
23 #include "includes.h"
24
25 /*
26   build a ADS_STATUS structure
27 */
28 ADS_STATUS ads_build_error(enum ads_error_type etype, 
29                            int rc, int minor_status)
30 {
31         ADS_STATUS ret;
32
33         if (etype == ENUM_ADS_ERROR_NT) {
34                 DEBUG(0,("don't use ads_build_error with ENUM_ADS_ERROR_NT!\n"));
35                 ret.err.rc = -1;
36                 ret.error_type = ENUM_ADS_ERROR_SYSTEM;
37                 ret.minor_status = 0;
38                 return ret;     
39         }       
40                 
41         ret.err.rc = rc;
42         ret.error_type = etype;         
43         ret.minor_status = minor_status;
44         return ret;
45 }
46
47 ADS_STATUS ads_build_nt_error(enum ads_error_type etype, 
48                            NTSTATUS nt_status)
49 {
50         ADS_STATUS ret;
51
52         if (etype != ENUM_ADS_ERROR_NT) {
53                 DEBUG(0,("don't use ads_build_nt_error without ENUM_ADS_ERROR_NT!\n"));
54                 ret.err.rc = -1;
55                 ret.error_type = ENUM_ADS_ERROR_SYSTEM;
56                 ret.minor_status = 0;
57                 return ret;     
58         }
59         ret.err.nt_status = nt_status;
60         ret.error_type = etype;         
61         ret.minor_status = 0;
62         return ret;
63 }
64
65 /*
66   do a rough conversion between ads error codes and NT status codes
67   we'll need to fill this in more
68 */
69 NTSTATUS ads_ntstatus(ADS_STATUS status)
70 {
71         switch (status.error_type) {
72         case ENUM_ADS_ERROR_NT:
73                 return status.err.nt_status;    
74         case ENUM_ADS_ERROR_SYSTEM:
75                 return map_nt_error_from_unix(status.err.rc);
76 #ifdef HAVE_LDAP
77         case ENUM_ADS_ERROR_LDAP:
78                 if (status.err.rc == LDAP_SUCCESS) {
79                         return NT_STATUS_OK;
80                 }
81                 return NT_STATUS_LDAP(status.err.rc);
82 #endif
83 #ifdef HAVE_KRB5
84         case ENUM_ADS_ERROR_KRB5:
85                 return krb5_to_nt_status(status.err.rc);
86 #endif
87 #ifdef HAVE_GSSAPI
88         case ENUM_ADS_ERROR_GSS:
89                 return map_nt_error_from_gss(status.err.rc, status.minor_status);
90 #endif
91         default:
92                 break;
93         }
94
95         if (ADS_ERR_OK(status)) {
96                 return NT_STATUS_OK;
97         }
98         return NT_STATUS_UNSUCCESSFUL;
99 }
100
101 /*
102   return a string for an error from a ads routine
103 */
104 const char *ads_errstr(ADS_STATUS status)
105 {
106         static char *ret;
107
108         SAFE_FREE(ret);
109
110         switch (status.error_type) {
111         case ENUM_ADS_ERROR_SYSTEM:
112                 return strerror(status.err.rc);
113 #ifdef HAVE_LDAP
114         case ENUM_ADS_ERROR_LDAP:
115                 return ldap_err2string(status.err.rc);
116 #endif
117 #ifdef HAVE_KRB5
118         case ENUM_ADS_ERROR_KRB5: 
119                 return error_message(status.err.rc);
120 #endif
121 #ifdef HAVE_GSSAPI
122         case ENUM_ADS_ERROR_GSS:
123         {
124                 uint32 msg_ctx;
125                 uint32 minor;
126                 gss_buffer_desc msg1, msg2;
127
128                 msg_ctx = 0;
129                 
130                 msg1.value = NULL;
131                 msg2.value = NULL;
132                 gss_display_status(&minor, status.err.rc, GSS_C_GSS_CODE,
133                                    GSS_C_NULL_OID, &msg_ctx, &msg1);
134                 gss_display_status(&minor, status.minor_status, GSS_C_MECH_CODE,
135                                    GSS_C_NULL_OID, &msg_ctx, &msg2);
136                 asprintf(&ret, "%s : %s", (char *)msg1.value, (char *)msg2.value);
137                 gss_release_buffer(&minor, &msg1);
138                 gss_release_buffer(&minor, &msg2);
139                 return ret;
140         }
141 #endif
142         case ENUM_ADS_ERROR_NT: 
143                 return get_friendly_nt_error_msg(ads_ntstatus(status));
144         default:
145                 return "Unknown ADS error type!? (not compiled in?)";
146         }
147 }
148
149 #ifdef HAVE_GSSAPI
150 NTSTATUS gss_err_to_ntstatus(uint32 maj, uint32 min)
151 {
152         ADS_STATUS adss = ADS_ERROR_GSS(maj, min);
153         DEBUG(10,("gss_err_to_ntstatus: Error %s\n",
154                 ads_errstr(adss) ));
155         return ads_ntstatus(adss);
156 }
157 #endif