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