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/>.
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"
32 #include "../lib/util/asn1.h"
34 #define RFC3244_VERSION 0xff80
36 krb5_error_code decode_krb5_setpw_req(const krb5_data *code,
37 krb5_data **password_out,
38 krb5_principal *target_out);
41 * A fallback for when MIT refuses to parse a setpw structure without the
42 * (optional) target principal and realm
44 static bool decode_krb5_setpw_req_simple(TALLOC_CTX *mem_ctx,
45 const DATA_BLOB *decoded_data,
46 DATA_BLOB *clear_data)
48 struct asn1_data *asn1 = NULL;
51 asn1 = asn1_init(mem_ctx, 3);
56 ret = asn1_load(asn1, *decoded_data);
61 ret = asn1_start_tag(asn1, ASN1_SEQUENCE(0));
65 ret = asn1_start_tag(asn1, ASN1_CONTEXT(0));
69 ret = asn1_read_OctetString(asn1, mem_ctx, clear_data);
74 ret = asn1_end_tag(asn1);
78 ret = asn1_end_tag(asn1);
86 static krb5_error_code kpasswd_change_password(struct kdc_server *kdc,
88 const struct gensec_security *gensec_security,
89 struct auth_session_info *session_info,
91 DATA_BLOB *kpasswd_reply,
92 const char **error_string)
95 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
96 enum samPwdChangeReason reject_reason;
97 const char *reject_string = NULL;
98 struct samr_DomInfo1 *dominfo;
103 * We're doing a password change (rather than a password set), so check
104 * that we were given an initial ticket.
106 ret = gensec_krb5_initial_ticket(gensec_security);
108 *error_string = "Expected an initial ticket";
109 return KRB5_KPASSWD_INITIAL_FLAG_NEEDED;
112 status = samdb_kpasswd_change_password(mem_ctx,
114 kdc->task->event_ctx,
122 if (!NT_STATUS_IS_OK(status)) {
123 ok = kpasswd_make_error_reply(mem_ctx,
124 KRB5_KPASSWD_ACCESSDENIED,
128 *error_string = "Failed to create reply";
129 return KRB5_KPASSWD_HARDERROR;
131 /* We want to send an an authenticated packet. */
135 ok = kpasswd_make_pwchange_reply(mem_ctx,
141 *error_string = "Failed to create reply";
142 return KRB5_KPASSWD_HARDERROR;
148 static krb5_error_code kpasswd_set_password(struct kdc_server *kdc,
150 const struct gensec_security *gensec_security,
151 struct auth_session_info *session_info,
152 DATA_BLOB *decoded_data,
153 DATA_BLOB *kpasswd_reply,
154 const char **error_string)
156 krb5_context context = kdc->smb_krb5_context->krb5_context;
157 DATA_BLOB clear_data;
158 krb5_data k_dec_data;
159 krb5_data *k_clear_data = NULL;
160 krb5_principal target_principal = NULL;
161 krb5_error_code code;
163 char *target_realm = NULL;
164 char *target_name = NULL;
165 char *target_principal_string = NULL;
166 bool is_service_principal = false;
168 size_t num_components;
169 enum samPwdChangeReason reject_reason = SAM_PWD_CHANGE_NO_ERROR;
170 struct samr_DomInfo1 *dominfo = NULL;
173 k_dec_data.length = decoded_data->length;
174 k_dec_data.data = (char *)decoded_data->data;
176 code = decode_krb5_setpw_req(&k_dec_data,
180 clear_data.data = (uint8_t *)k_clear_data->data;
181 clear_data.length = k_clear_data->length;
183 target_principal = NULL;
186 * The MIT decode failed, so fall back to trying the simple
187 * case, without target_principal.
189 ok = decode_krb5_setpw_req_simple(mem_ctx,
193 DBG_WARNING("decode_krb5_setpw_req failed: %s\n",
194 error_message(code));
195 ok = kpasswd_make_error_reply(mem_ctx,
196 KRB5_KPASSWD_MALFORMED,
197 "Failed to decode packet",
200 *error_string = "Failed to create reply";
201 return KRB5_KPASSWD_HARDERROR;
207 ok = convert_string_talloc_handle(mem_ctx,
208 lpcfg_iconv_handle(kdc->task->lp_ctx),
213 (void **)&password.data,
215 if (k_clear_data != NULL) {
216 krb5_free_data(context, k_clear_data);
219 DBG_WARNING("String conversion failed\n");
220 *error_string = "String conversion failed";
221 return KRB5_KPASSWD_HARDERROR;
224 if (target_principal != NULL) {
225 target_realm = smb_krb5_principal_get_realm(
226 mem_ctx, context, target_principal);
227 code = krb5_unparse_name_flags(context,
229 KRB5_PRINCIPAL_UNPARSE_NO_REALM,
232 DBG_WARNING("Failed to parse principal\n");
233 *error_string = "String conversion failed";
234 return KRB5_KPASSWD_HARDERROR;
238 if ((target_name != NULL && target_realm == NULL) ||
239 (target_name == NULL && target_realm != NULL)) {
240 krb5_free_principal(context, target_principal);
241 TALLOC_FREE(target_realm);
242 SAFE_FREE(target_name);
244 ok = kpasswd_make_error_reply(mem_ctx,
245 KRB5_KPASSWD_MALFORMED,
246 "Realm and principal must be "
247 "both present, or neither "
251 *error_string = "Failed to create reply";
252 return KRB5_KPASSWD_HARDERROR;
257 if (target_name != NULL && target_realm != NULL) {
258 TALLOC_FREE(target_realm);
259 SAFE_FREE(target_name);
261 krb5_free_principal(context, target_principal);
262 TALLOC_FREE(target_realm);
263 SAFE_FREE(target_name);
265 return kpasswd_change_password(kdc,
274 num_components = krb5_princ_size(context, target_principal);
275 if (num_components >= 2) {
276 is_service_principal = true;
277 code = krb5_unparse_name_flags(context,
279 KRB5_PRINCIPAL_UNPARSE_SHORT,
280 &target_principal_string);
282 code = krb5_unparse_name(context,
284 &target_principal_string);
286 krb5_free_principal(context, target_principal);
288 ok = kpasswd_make_error_reply(mem_ctx,
289 KRB5_KPASSWD_MALFORMED,
290 "Failed to parse principal",
293 *error_string = "Failed to create reply";
294 return KRB5_KPASSWD_HARDERROR;
298 status = kpasswd_samdb_set_password(mem_ctx,
299 kdc->task->event_ctx,
302 is_service_principal,
303 target_principal_string,
307 if (!NT_STATUS_IS_OK(status)) {
308 DBG_ERR("kpasswd_samdb_set_password failed - %s\n",
312 ok = kpasswd_make_pwchange_reply(mem_ctx,
318 *error_string = "Failed to create reply";
319 return KRB5_KPASSWD_HARDERROR;
325 krb5_error_code kpasswd_handle_request(struct kdc_server *kdc,
327 struct gensec_security *gensec_security,
329 DATA_BLOB *decoded_data,
330 DATA_BLOB *kpasswd_reply,
331 const char **error_string)
333 struct auth_session_info *session_info;
335 krb5_error_code code;
337 status = gensec_session_info(gensec_security,
340 if (!NT_STATUS_IS_OK(status)) {
341 *error_string = talloc_asprintf(mem_ctx,
342 "gensec_session_info failed - "
345 return KRB5_KPASSWD_HARDERROR;
349 * Since the kpasswd service shares its keys with the krbtgt, we might
350 * have received a TGT rather than a kpasswd ticket. We need to check
351 * the ticket type to ensure that TGTs cannot be misused in this manner.
353 code = kpasswd_check_non_tgt(session_info,
356 DBG_WARNING("%s\n", *error_string);
365 ok = convert_string_talloc_handle(mem_ctx,
366 lpcfg_iconv_handle(kdc->task->lp_ctx),
369 (const char *)decoded_data->data,
370 decoded_data->length,
371 (void **)&password.data,
374 *error_string = "String conversion failed!";
375 DBG_WARNING("%s\n", *error_string);
376 return KRB5_KPASSWD_HARDERROR;
379 return kpasswd_change_password(kdc,
387 case RFC3244_VERSION: {
388 return kpasswd_set_password(kdc,
397 return KRB5_KPASSWD_BAD_VERSION;