ldb:attrib_handlers: use NUMERIC_CMP in ldb_comparison_fold
[Samba.git] / source4 / kdc / kpasswd-service-heimdal.c
blobb4709de3e5488a31c5004f784a9d8b39555b67c9
1 /*
2 Unix SMB/CIFS implementation.
4 Samba kpasswd implementation
6 Copyright (c) 2016 Andreas Schneider <asn@samba.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "samba/service_task.h"
24 #include "param/param.h"
25 #include "auth/auth.h"
26 #include "auth/gensec/gensec.h"
27 #include "gensec_krb5_helpers.h"
28 #include "kdc/kdc-server.h"
29 #include "kdc/kpasswd_glue.h"
30 #include "kdc/kpasswd-service.h"
31 #include "kdc/kpasswd-helper.h"
33 #undef DBGC_CLASS
34 #define DBGC_CLASS DBGC_KERBEROS
36 static krb5_error_code kpasswd_change_password(struct kdc_server *kdc,
37 TALLOC_CTX *mem_ctx,
38 const struct gensec_security *gensec_security,
39 struct auth_session_info *session_info,
40 DATA_BLOB *password,
41 DATA_BLOB *kpasswd_reply,
42 const char **error_string)
44 NTSTATUS status;
45 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
46 enum samPwdChangeReason reject_reason;
47 const char *reject_string = NULL;
48 struct samr_DomInfo1 *dominfo;
49 bool ok;
50 int ret;
53 * We're doing a password change (rather than a password set), so check
54 * that we were given an initial ticket.
56 ret = gensec_krb5_initial_ticket(gensec_security);
57 if (ret != 1) {
58 *error_string = "Expected an initial ticket";
59 return KRB5_KPASSWD_INITIAL_FLAG_NEEDED;
62 status = samdb_kpasswd_change_password(mem_ctx,
63 kdc->task->lp_ctx,
64 kdc->task->event_ctx,
65 session_info,
66 password,
67 &reject_reason,
68 &dominfo,
69 &reject_string,
70 &result);
71 if (!NT_STATUS_IS_OK(status)) {
72 ok = kpasswd_make_error_reply(mem_ctx,
73 KRB5_KPASSWD_ACCESSDENIED,
74 reject_string,
75 kpasswd_reply);
76 if (!ok) {
77 *error_string = "Failed to create reply";
78 return KRB5_KPASSWD_HARDERROR;
80 /* We want to send an an authenticated packet. */
81 return 0;
84 ok = kpasswd_make_pwchange_reply(mem_ctx,
85 result,
86 reject_reason,
87 dominfo,
88 kpasswd_reply);
89 if (!ok) {
90 *error_string = "Failed to create reply";
91 return KRB5_KPASSWD_HARDERROR;
94 return 0;
97 static krb5_error_code kpasswd_set_password(struct kdc_server *kdc,
98 TALLOC_CTX *mem_ctx,
99 const struct gensec_security *gensec_security,
100 struct auth_session_info *session_info,
101 DATA_BLOB *decoded_data,
102 DATA_BLOB *kpasswd_reply,
103 const char **error_string)
105 krb5_context context = kdc->smb_krb5_context->krb5_context;
106 krb5_error_code code;
107 krb5_principal target_principal;
108 ChangePasswdDataMS chpw = {};
109 size_t chpw_len = 0;
110 DATA_BLOB password = data_blob_null;
111 enum samPwdChangeReason reject_reason = SAM_PWD_CHANGE_NO_ERROR;
112 struct samr_DomInfo1 *dominfo = NULL;
113 char *target_principal_string = NULL;
114 bool is_service_principal = false;
115 NTSTATUS status;
116 bool ok;
118 code = decode_ChangePasswdDataMS(decoded_data->data,
119 decoded_data->length,
120 &chpw,
121 &chpw_len);
122 if (code != 0) {
123 DBG_WARNING("decode_ChangePasswdDataMS failed\n");
124 ok = kpasswd_make_error_reply(mem_ctx,
125 KRB5_KPASSWD_MALFORMED,
126 "Failed to decode packet",
127 kpasswd_reply);
128 if (!ok) {
129 *error_string = "Failed to create reply";
130 return KRB5_KPASSWD_HARDERROR;
132 return 0;
135 ok = convert_string_talloc_handle(mem_ctx,
136 lpcfg_iconv_handle(kdc->task->lp_ctx),
137 CH_UTF8,
138 CH_UTF16,
139 chpw.newpasswd.data,
140 chpw.newpasswd.length,
141 &password.data,
142 &password.length);
143 if (!ok) {
144 free_ChangePasswdDataMS(&chpw);
145 DBG_WARNING("String conversion failed\n");
146 *error_string = "String conversion failed";
147 return KRB5_KPASSWD_HARDERROR;
150 if ((chpw.targname != NULL && chpw.targrealm == NULL) ||
151 (chpw.targname == NULL && chpw.targrealm != NULL)) {
152 free_ChangePasswdDataMS(&chpw);
153 ok = kpasswd_make_error_reply(mem_ctx,
154 KRB5_KPASSWD_MALFORMED,
155 "Realm and principal must be "
156 "both present, or neither present",
157 kpasswd_reply);
158 if (!ok) {
159 *error_string = "Failed to create reply";
160 return KRB5_KPASSWD_HARDERROR;
162 return 0;
165 if (chpw.targname == NULL || chpw.targrealm == NULL) {
166 free_ChangePasswdDataMS(&chpw);
167 return kpasswd_change_password(kdc,
168 mem_ctx,
169 gensec_security,
170 session_info,
171 &password,
172 kpasswd_reply,
173 error_string);
175 code = krb5_build_principal_ext(context,
176 &target_principal,
177 strlen(*chpw.targrealm),
178 *chpw.targrealm,
180 if (code != 0) {
181 free_ChangePasswdDataMS(&chpw);
182 return kpasswd_make_error_reply(mem_ctx,
183 KRB5_KPASSWD_MALFORMED,
184 "Failed to parse principal",
185 kpasswd_reply);
187 code = copy_PrincipalName(chpw.targname,
188 &target_principal->name);
189 free_ChangePasswdDataMS(&chpw);
190 if (code != 0) {
191 krb5_free_principal(context, target_principal);
192 return kpasswd_make_error_reply(mem_ctx,
193 KRB5_KPASSWD_MALFORMED,
194 "Failed to parse principal",
195 kpasswd_reply);
198 if (target_principal->name.name_string.len >= 2) {
199 is_service_principal = true;
201 code = krb5_unparse_name_short(context,
202 target_principal,
203 &target_principal_string);
204 } else {
205 code = krb5_unparse_name(context,
206 target_principal,
207 &target_principal_string);
209 krb5_free_principal(context, target_principal);
210 if (code != 0) {
211 ok = kpasswd_make_error_reply(mem_ctx,
212 KRB5_KPASSWD_MALFORMED,
213 "Failed to parse principal",
214 kpasswd_reply);
215 if (!ok) {
216 *error_string = "Failed to create reply";
217 return KRB5_KPASSWD_HARDERROR;
221 status = kpasswd_samdb_set_password(mem_ctx,
222 kdc->task->event_ctx,
223 kdc->task->lp_ctx,
224 session_info,
225 is_service_principal,
226 target_principal_string,
227 &password,
228 &reject_reason,
229 &dominfo);
230 if (!NT_STATUS_IS_OK(status)) {
231 DBG_ERR("kpasswd_samdb_set_password failed - %s\n",
232 nt_errstr(status));
235 ok = kpasswd_make_pwchange_reply(mem_ctx,
236 status,
237 reject_reason,
238 dominfo,
239 kpasswd_reply);
240 if (!ok) {
241 *error_string = "Failed to create reply";
242 return KRB5_KPASSWD_HARDERROR;
245 return 0;
248 krb5_error_code kpasswd_handle_request(struct kdc_server *kdc,
249 TALLOC_CTX *mem_ctx,
250 struct gensec_security *gensec_security,
251 uint16_t verno,
252 DATA_BLOB *decoded_data,
253 DATA_BLOB *kpasswd_reply,
254 const char **error_string)
256 struct auth_session_info *session_info;
257 NTSTATUS status;
258 krb5_error_code code;
260 status = gensec_session_info(gensec_security,
261 mem_ctx,
262 &session_info);
263 if (!NT_STATUS_IS_OK(status)) {
264 *error_string = talloc_asprintf(mem_ctx,
265 "gensec_session_info failed - %s",
266 nt_errstr(status));
267 return KRB5_KPASSWD_HARDERROR;
271 * Since the kpasswd service shares its keys with the krbtgt, we might
272 * have received a TGT rather than a kpasswd ticket. We need to check
273 * the ticket type to ensure that TGTs cannot be misused in this manner.
275 code = kpasswd_check_non_tgt(session_info,
276 error_string);
277 if (code != 0) {
278 DBG_WARNING("%s\n", *error_string);
279 return code;
282 switch(verno) {
283 case KRB5_KPASSWD_VERS_CHANGEPW: {
284 DATA_BLOB password = data_blob_null;
285 bool ok;
287 ok = convert_string_talloc_handle(mem_ctx,
288 lpcfg_iconv_handle(kdc->task->lp_ctx),
289 CH_UTF8,
290 CH_UTF16,
291 decoded_data->data,
292 decoded_data->length,
293 &password.data,
294 &password.length);
295 if (!ok) {
296 *error_string = "String conversion failed!";
297 DBG_WARNING("%s\n", *error_string);
298 return KRB5_KPASSWD_HARDERROR;
301 return kpasswd_change_password(kdc,
302 mem_ctx,
303 gensec_security,
304 session_info,
305 &password,
306 kpasswd_reply,
307 error_string);
309 case KRB5_KPASSWD_VERS_SETPW: {
310 return kpasswd_set_password(kdc,
311 mem_ctx,
312 gensec_security,
313 session_info,
314 decoded_data,
315 kpasswd_reply,
316 error_string);
318 default:
319 *error_string = talloc_asprintf(mem_ctx,
320 "Protocol version %u not supported",
321 verno);
322 return KRB5_KPASSWD_BAD_VERSION;
325 return 0;