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"
33 static krb5_error_code kpasswd_change_password(struct kdc_server *kdc,
35 const struct gensec_security *gensec_security,
36 struct auth_session_info *session_info,
38 DATA_BLOB *kpasswd_reply,
39 const char **error_string)
42 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
43 enum samPwdChangeReason reject_reason;
44 const char *reject_string = NULL;
45 struct samr_DomInfo1 *dominfo;
50 * We're doing a password change (rather than a password set), so check
51 * that we were given an initial ticket.
53 ret = gensec_krb5_initial_ticket(gensec_security);
55 *error_string = "Expected an initial ticket";
56 return KRB5_KPASSWD_INITIAL_FLAG_NEEDED;
59 status = samdb_kpasswd_change_password(mem_ctx,
69 if (!NT_STATUS_IS_OK(status)) {
70 ok = kpasswd_make_error_reply(mem_ctx,
71 KRB5_KPASSWD_ACCESSDENIED,
75 *error_string = "Failed to create reply";
76 return KRB5_KPASSWD_HARDERROR;
78 /* We want to send an an authenticated packet. */
82 ok = kpasswd_make_pwchange_reply(mem_ctx,
88 *error_string = "Failed to create reply";
89 return KRB5_KPASSWD_HARDERROR;
95 static krb5_error_code kpasswd_set_password(struct kdc_server *kdc,
97 const struct gensec_security *gensec_security,
98 struct auth_session_info *session_info,
99 DATA_BLOB *decoded_data,
100 DATA_BLOB *kpasswd_reply,
101 const char **error_string)
103 krb5_context context = kdc->smb_krb5_context->krb5_context;
104 krb5_error_code code;
105 krb5_principal target_principal;
106 ChangePasswdDataMS chpw = {};
108 DATA_BLOB password = data_blob_null;
109 enum samPwdChangeReason reject_reason = SAM_PWD_CHANGE_NO_ERROR;
110 struct samr_DomInfo1 *dominfo = NULL;
111 char *target_principal_string = NULL;
112 bool is_service_principal = false;
116 code = decode_ChangePasswdDataMS(decoded_data->data,
117 decoded_data->length,
121 DBG_WARNING("decode_ChangePasswdDataMS failed\n");
122 ok = kpasswd_make_error_reply(mem_ctx,
123 KRB5_KPASSWD_MALFORMED,
124 "Failed to decode packet",
127 *error_string = "Failed to create reply";
128 return KRB5_KPASSWD_HARDERROR;
133 ok = convert_string_talloc_handle(mem_ctx,
134 lpcfg_iconv_handle(kdc->task->lp_ctx),
137 (const char *)chpw.newpasswd.data,
138 chpw.newpasswd.length,
139 (void **)&password.data,
142 free_ChangePasswdDataMS(&chpw);
143 DBG_WARNING("String conversion failed\n");
144 *error_string = "String conversion failed";
145 return KRB5_KPASSWD_HARDERROR;
148 if ((chpw.targname != NULL && chpw.targrealm == NULL) ||
149 (chpw.targname == NULL && chpw.targrealm != NULL)) {
150 free_ChangePasswdDataMS(&chpw);
151 ok = kpasswd_make_error_reply(mem_ctx,
152 KRB5_KPASSWD_MALFORMED,
153 "Realm and principal must be "
154 "both present, or neither present",
157 *error_string = "Failed to create reply";
158 return KRB5_KPASSWD_HARDERROR;
163 if (chpw.targname == NULL || chpw.targrealm == NULL) {
164 free_ChangePasswdDataMS(&chpw);
165 return kpasswd_change_password(kdc,
173 code = krb5_build_principal_ext(context,
175 strlen(*chpw.targrealm),
179 free_ChangePasswdDataMS(&chpw);
180 return kpasswd_make_error_reply(mem_ctx,
181 KRB5_KPASSWD_MALFORMED,
182 "Failed to parse principal",
185 code = copy_PrincipalName(chpw.targname,
186 &target_principal->name);
187 free_ChangePasswdDataMS(&chpw);
189 krb5_free_principal(context, target_principal);
190 return kpasswd_make_error_reply(mem_ctx,
191 KRB5_KPASSWD_MALFORMED,
192 "Failed to parse principal",
196 if (target_principal->name.name_string.len >= 2) {
197 is_service_principal = true;
199 code = krb5_unparse_name_short(context,
201 &target_principal_string);
203 code = krb5_unparse_name(context,
205 &target_principal_string);
207 krb5_free_principal(context, target_principal);
209 ok = kpasswd_make_error_reply(mem_ctx,
210 KRB5_KPASSWD_MALFORMED,
211 "Failed to parse principal",
214 *error_string = "Failed to create reply";
215 return KRB5_KPASSWD_HARDERROR;
219 status = kpasswd_samdb_set_password(mem_ctx,
220 kdc->task->event_ctx,
223 is_service_principal,
224 target_principal_string,
228 if (!NT_STATUS_IS_OK(status)) {
229 DBG_ERR("kpasswd_samdb_set_password failed - %s\n",
233 ok = kpasswd_make_pwchange_reply(mem_ctx,
239 *error_string = "Failed to create reply";
240 return KRB5_KPASSWD_HARDERROR;
246 krb5_error_code kpasswd_handle_request(struct kdc_server *kdc,
248 struct gensec_security *gensec_security,
250 DATA_BLOB *decoded_data,
251 DATA_BLOB *kpasswd_reply,
252 const char **error_string)
254 struct auth_session_info *session_info;
257 status = gensec_session_info(gensec_security,
260 if (!NT_STATUS_IS_OK(status)) {
261 *error_string = talloc_asprintf(mem_ctx,
262 "gensec_session_info failed - %s",
264 return KRB5_KPASSWD_HARDERROR;
268 case KRB5_KPASSWD_VERS_CHANGEPW: {
269 DATA_BLOB password = data_blob_null;
272 ok = convert_string_talloc_handle(mem_ctx,
273 lpcfg_iconv_handle(kdc->task->lp_ctx),
276 (const char *)decoded_data->data,
277 decoded_data->length,
278 (void **)&password.data,
281 *error_string = "String conversion failed!";
282 DBG_WARNING("%s\n", *error_string);
283 return KRB5_KPASSWD_HARDERROR;
286 return kpasswd_change_password(kdc,
294 case KRB5_KPASSWD_VERS_SETPW: {
295 return kpasswd_set_password(kdc,
304 *error_string = talloc_asprintf(mem_ctx,
305 "Protocol version %u not supported",
307 return KRB5_KPASSWD_BAD_VERSION;