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