s3: Fix a cut&paste error
[Samba/vl.git] / source4 / kdc / kpasswdd.c
blob478dcaf573249bc0f16d01cd96aeed7dce654382
1 /*
2 Unix SMB/CIFS implementation.
4 kpasswd Server implementation
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
7 Copyright (C) Andrew Tridgell 2005
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.
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.
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/>.
23 #include "includes.h"
24 #include "smbd/service_task.h"
25 #include "auth/gensec/gensec.h"
26 #include "auth/credentials/credentials.h"
27 #include "auth/auth.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "../lib/util/util_ldb.h"
30 #include "libcli/security/security.h"
31 #include "param/param.h"
32 #include "kdc/kdc-glue.h"
34 /* Return true if there is a valid error packet formed in the error_blob */
35 static bool kpasswdd_make_error_reply(struct kdc_server *kdc,
36 TALLOC_CTX *mem_ctx,
37 uint16_t result_code,
38 const char *error_string,
39 DATA_BLOB *error_blob)
41 char *error_string_utf8;
42 size_t len;
44 DEBUG(result_code ? 3 : 10, ("kpasswdd: %s\n", error_string));
46 if (!push_utf8_talloc(mem_ctx, &error_string_utf8, error_string, &len)) {
47 return false;
50 *error_blob = data_blob_talloc(mem_ctx, NULL, 2 + len + 1);
51 if (!error_blob->data) {
52 return false;
54 RSSVAL(error_blob->data, 0, result_code);
55 memcpy(error_blob->data + 2, error_string_utf8, len + 1);
56 return true;
59 /* Return true if there is a valid error packet formed in the error_blob */
60 static bool kpasswdd_make_unauth_error_reply(struct kdc_server *kdc,
61 TALLOC_CTX *mem_ctx,
62 uint16_t result_code,
63 const char *error_string,
64 DATA_BLOB *error_blob)
66 bool ret;
67 int kret;
68 DATA_BLOB error_bytes;
69 krb5_data k5_error_bytes, k5_error_blob;
70 ret = kpasswdd_make_error_reply(kdc, mem_ctx, result_code, error_string,
71 &error_bytes);
72 if (!ret) {
73 return false;
75 k5_error_bytes.data = error_bytes.data;
76 k5_error_bytes.length = error_bytes.length;
77 kret = krb5_mk_error(kdc->smb_krb5_context->krb5_context,
78 result_code, NULL, &k5_error_bytes,
79 NULL, NULL, NULL, NULL, &k5_error_blob);
80 if (kret) {
81 return false;
83 *error_blob = data_blob_talloc(mem_ctx, k5_error_blob.data, k5_error_blob.length);
84 krb5_data_free(&k5_error_blob);
85 if (!error_blob->data) {
86 return false;
88 return true;
91 static bool kpasswd_make_pwchange_reply(struct kdc_server *kdc,
92 TALLOC_CTX *mem_ctx,
93 NTSTATUS status,
94 enum samPwdChangeReason reject_reason,
95 struct samr_DomInfo1 *dominfo,
96 DATA_BLOB *error_blob)
98 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
99 return kpasswdd_make_error_reply(kdc, mem_ctx,
100 KRB5_KPASSWD_ACCESSDENIED,
101 "No such user when changing password",
102 error_blob);
104 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
105 return kpasswdd_make_error_reply(kdc, mem_ctx,
106 KRB5_KPASSWD_ACCESSDENIED,
107 "Not permitted to change password",
108 error_blob);
110 if (dominfo && NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION)) {
111 const char *reject_string;
112 switch (reject_reason) {
113 case SAM_PWD_CHANGE_PASSWORD_TOO_SHORT:
114 reject_string = talloc_asprintf(mem_ctx, "Password too short, password must be at least %d characters long",
115 dominfo->min_password_length);
116 break;
117 case SAM_PWD_CHANGE_NOT_COMPLEX:
118 reject_string = "Password does not meet complexity requirements";
119 break;
120 case SAM_PWD_CHANGE_PWD_IN_HISTORY:
121 reject_string = "Password is already in password history";
122 break;
123 default:
124 reject_string = talloc_asprintf(mem_ctx, "Password must be at least %d characters long, and cannot match any of your %d previous passwords",
125 dominfo->min_password_length, dominfo->password_history_length);
126 break;
128 return kpasswdd_make_error_reply(kdc, mem_ctx,
129 KRB5_KPASSWD_SOFTERROR,
130 reject_string,
131 error_blob);
133 if (!NT_STATUS_IS_OK(status)) {
134 return kpasswdd_make_error_reply(kdc, mem_ctx,
135 KRB5_KPASSWD_HARDERROR,
136 talloc_asprintf(mem_ctx, "failed to set password: %s", nt_errstr(status)),
137 error_blob);
140 return kpasswdd_make_error_reply(kdc, mem_ctx, KRB5_KPASSWD_SUCCESS,
141 "Password changed",
142 error_blob);
146 A user password change
148 Return true if there is a valid error packet (or success) formed in
149 the error_blob
151 static bool kpasswdd_change_password(struct kdc_server *kdc,
152 TALLOC_CTX *mem_ctx,
153 struct auth_session_info *session_info,
154 const DATA_BLOB *password,
155 DATA_BLOB *reply)
157 NTSTATUS status;
158 enum samPwdChangeReason reject_reason;
159 struct samr_DomInfo1 *dominfo;
160 struct samr_Password *oldLmHash, *oldNtHash;
161 struct ldb_context *samdb;
162 const char * const attrs[] = { "dBCSPwd", "unicodePwd", NULL };
163 struct ldb_message **res;
164 int ret;
166 /* Fetch the old hashes to get the old password in order to perform
167 * the password change operation. Naturally it would be much better to
168 * have a password hash from an authentication around but this doesn't
169 * seem to be the case here. */
170 ret = gendb_search(kdc->samdb, mem_ctx, NULL, &res, attrs,
171 "(&(objectClass=user)(sAMAccountName=%s))",
172 session_info->info->account_name);
173 if (ret != 1) {
174 return kpasswdd_make_error_reply(kdc, mem_ctx,
175 KRB5_KPASSWD_ACCESSDENIED,
176 "No such user when changing password",
177 reply);
180 status = samdb_result_passwords(mem_ctx, kdc->task->lp_ctx, res[0],
181 &oldLmHash, &oldNtHash);
182 if (!NT_STATUS_IS_OK(status)) {
183 return kpasswdd_make_error_reply(kdc, mem_ctx,
184 KRB5_KPASSWD_ACCESSDENIED,
185 "Not permitted to change password",
186 reply);
189 /* Start a SAM with user privileges for the password change */
190 samdb = samdb_connect(mem_ctx, kdc->task->event_ctx, kdc->task->lp_ctx,
191 session_info, 0);
192 if (!samdb) {
193 return kpasswdd_make_error_reply(kdc, mem_ctx,
194 KRB5_KPASSWD_HARDERROR,
195 "Failed to open samdb",
196 reply);
199 DEBUG(3, ("Changing password of %s\\%s (%s)\n",
200 session_info->info->domain_name,
201 session_info->info->account_name,
202 dom_sid_string(mem_ctx, &session_info->security_token->sids[PRIMARY_USER_SID_INDEX])));
204 /* Performs the password change */
205 status = samdb_set_password_sid(samdb, mem_ctx,
206 &session_info->security_token->sids[PRIMARY_USER_SID_INDEX],
207 password, NULL, NULL,
208 oldLmHash, oldNtHash, /* this is a user password change */
209 &reject_reason,
210 &dominfo);
211 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
212 status,
213 reject_reason,
214 dominfo,
215 reply);
219 static bool kpasswd_process_request(struct kdc_server *kdc,
220 TALLOC_CTX *mem_ctx,
221 struct gensec_security *gensec_security,
222 uint16_t version,
223 DATA_BLOB *input,
224 DATA_BLOB *reply)
226 struct auth_session_info *session_info;
227 size_t pw_len;
229 if (!NT_STATUS_IS_OK(gensec_session_info(gensec_security,
230 mem_ctx,
231 &session_info))) {
232 return kpasswdd_make_error_reply(kdc, mem_ctx,
233 KRB5_KPASSWD_HARDERROR,
234 "gensec_session_info failed!",
235 reply);
238 switch (version) {
239 case KRB5_KPASSWD_VERS_CHANGEPW:
241 DATA_BLOB password;
242 if (!convert_string_talloc_handle(mem_ctx, lpcfg_iconv_handle(kdc->task->lp_ctx),
243 CH_UTF8, CH_UTF16,
244 (const char *)input->data,
245 input->length,
246 (void **)&password.data, &pw_len)) {
247 return false;
249 password.length = pw_len;
251 return kpasswdd_change_password(kdc, mem_ctx, session_info,
252 &password, reply);
254 case KRB5_KPASSWD_VERS_SETPW:
256 NTSTATUS status;
257 enum samPwdChangeReason reject_reason = SAM_PWD_CHANGE_NO_ERROR;
258 struct samr_DomInfo1 *dominfo = NULL;
259 struct ldb_context *samdb;
260 krb5_context context = kdc->smb_krb5_context->krb5_context;
262 ChangePasswdDataMS chpw;
263 DATA_BLOB password;
265 krb5_principal principal;
266 char *set_password_on_princ;
267 struct ldb_dn *set_password_on_dn;
268 bool service_principal_name = false;
270 size_t len;
271 int ret;
273 ret = decode_ChangePasswdDataMS(input->data, input->length,
274 &chpw, &len);
275 if (ret) {
276 return kpasswdd_make_error_reply(kdc, mem_ctx,
277 KRB5_KPASSWD_MALFORMED,
278 "failed to decode password change structure",
279 reply);
282 if (!convert_string_talloc_handle(mem_ctx, lpcfg_iconv_handle(kdc->task->lp_ctx),
283 CH_UTF8, CH_UTF16,
284 (const char *)chpw.newpasswd.data,
285 chpw.newpasswd.length,
286 (void **)&password.data, &pw_len)) {
287 free_ChangePasswdDataMS(&chpw);
288 return false;
291 password.length = pw_len;
293 if ((chpw.targname && !chpw.targrealm)
294 || (!chpw.targname && chpw.targrealm)) {
295 free_ChangePasswdDataMS(&chpw);
296 return kpasswdd_make_error_reply(kdc, mem_ctx,
297 KRB5_KPASSWD_MALFORMED,
298 "Realm and principal must be both present, or neither present",
299 reply);
301 if (chpw.targname && chpw.targrealm) {
302 ret = krb5_build_principal_ext(kdc->smb_krb5_context->krb5_context,
303 &principal,
304 strlen(*chpw.targrealm),
305 *chpw.targrealm, 0);
306 if (ret) {
307 free_ChangePasswdDataMS(&chpw);
308 return kpasswdd_make_error_reply(kdc, mem_ctx,
309 KRB5_KPASSWD_MALFORMED,
310 "failed to get principal",
311 reply);
313 if (copy_PrincipalName(chpw.targname, &principal->name)) {
314 free_ChangePasswdDataMS(&chpw);
315 krb5_free_principal(context, principal);
316 return kpasswdd_make_error_reply(kdc, mem_ctx,
317 KRB5_KPASSWD_MALFORMED,
318 "failed to extract principal to set",
319 reply);
321 } else {
322 free_ChangePasswdDataMS(&chpw);
323 return kpasswdd_change_password(kdc, mem_ctx, session_info,
324 &password, reply);
326 free_ChangePasswdDataMS(&chpw);
328 if (principal->name.name_string.len >= 2) {
329 service_principal_name = true;
331 /* We use this, rather than 'no realm' flag,
332 * as we don't want to accept a password
333 * change on a principal from another realm */
335 if (krb5_unparse_name_short(context, principal, &set_password_on_princ) != 0) {
336 krb5_free_principal(context, principal);
337 return kpasswdd_make_error_reply(kdc, mem_ctx,
338 KRB5_KPASSWD_MALFORMED,
339 "krb5_unparse_name failed!",
340 reply);
342 } else {
343 if (krb5_unparse_name(context, principal, &set_password_on_princ) != 0) {
344 krb5_free_principal(context, principal);
345 return kpasswdd_make_error_reply(kdc, mem_ctx,
346 KRB5_KPASSWD_MALFORMED,
347 "krb5_unparse_name failed!",
348 reply);
351 krb5_free_principal(context, principal);
353 samdb = samdb_connect(mem_ctx, kdc->task->event_ctx, kdc->task->lp_ctx, session_info, 0);
354 if (!samdb) {
355 free(set_password_on_princ);
356 return kpasswdd_make_error_reply(kdc, mem_ctx,
357 KRB5_KPASSWD_HARDERROR,
358 "Unable to open database!",
359 reply);
362 DEBUG(3, ("%s\\%s (%s) is changing password of %s\n",
363 session_info->info->domain_name,
364 session_info->info->account_name,
365 dom_sid_string(mem_ctx, &session_info->security_token->sids[PRIMARY_USER_SID_INDEX]),
366 set_password_on_princ));
367 ret = ldb_transaction_start(samdb);
368 if (ret != LDB_SUCCESS) {
369 free(set_password_on_princ);
370 status = NT_STATUS_TRANSACTION_ABORTED;
371 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
372 status,
373 SAM_PWD_CHANGE_NO_ERROR,
374 NULL,
375 reply);
378 if (service_principal_name) {
379 status = crack_service_principal_name(samdb, mem_ctx,
380 set_password_on_princ,
381 &set_password_on_dn, NULL);
382 } else {
383 status = crack_user_principal_name(samdb, mem_ctx,
384 set_password_on_princ,
385 &set_password_on_dn, NULL);
387 free(set_password_on_princ);
388 if (!NT_STATUS_IS_OK(status)) {
389 ldb_transaction_cancel(samdb);
390 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
391 status,
392 SAM_PWD_CHANGE_NO_ERROR,
393 NULL,
394 reply);
397 if (NT_STATUS_IS_OK(status)) {
398 /* Admin password set */
399 status = samdb_set_password(samdb, mem_ctx,
400 set_password_on_dn, NULL,
401 &password, NULL, NULL,
402 NULL, NULL, /* this is not a user password change */
403 &reject_reason, &dominfo);
406 if (NT_STATUS_IS_OK(status)) {
407 ret = ldb_transaction_commit(samdb);
408 if (ret != LDB_SUCCESS) {
409 DEBUG(1,("Failed to commit transaction to set password on %s: %s\n",
410 ldb_dn_get_linearized(set_password_on_dn),
411 ldb_errstring(samdb)));
412 status = NT_STATUS_TRANSACTION_ABORTED;
414 } else {
415 ldb_transaction_cancel(samdb);
417 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
418 status,
419 reject_reason,
420 dominfo,
421 reply);
423 default:
424 return kpasswdd_make_error_reply(kdc, mem_ctx,
425 KRB5_KPASSWD_BAD_VERSION,
426 talloc_asprintf(mem_ctx,
427 "Protocol version %u not supported",
428 version),
429 reply);
433 enum kdc_process_ret kpasswdd_process(struct kdc_server *kdc,
434 TALLOC_CTX *mem_ctx,
435 DATA_BLOB *input,
436 DATA_BLOB *reply,
437 struct tsocket_address *peer_addr,
438 struct tsocket_address *my_addr,
439 int datagram_reply)
441 bool ret;
442 const uint16_t header_len = 6;
443 uint16_t len;
444 uint16_t ap_req_len;
445 uint16_t krb_priv_len;
446 uint16_t version;
447 NTSTATUS nt_status;
448 DATA_BLOB ap_req, krb_priv_req;
449 DATA_BLOB krb_priv_rep = data_blob(NULL, 0);
450 DATA_BLOB ap_rep = data_blob(NULL, 0);
451 DATA_BLOB kpasswd_req, kpasswd_rep;
452 struct cli_credentials *server_credentials;
453 struct gensec_security *gensec_security;
454 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
456 char *keytab_name;
458 if (!tmp_ctx) {
459 return KDC_PROCESS_FAILED;
462 if (kdc->am_rodc) {
463 talloc_free(tmp_ctx);
464 return KDC_PROCESS_PROXY;
467 /* Be parinoid. We need to ensure we don't just let the
468 * caller lead us into a buffer overflow */
469 if (input->length <= header_len) {
470 talloc_free(tmp_ctx);
471 return KDC_PROCESS_FAILED;
474 len = RSVAL(input->data, 0);
475 if (input->length != len) {
476 talloc_free(tmp_ctx);
477 return KDC_PROCESS_FAILED;
480 /* There are two different versions of this protocol so far,
481 * plus others in the standards pipe. Fortunetly they all
482 * take a very similar framing */
483 version = RSVAL(input->data, 2);
484 ap_req_len = RSVAL(input->data, 4);
485 if ((ap_req_len >= len) || (ap_req_len + header_len) >= len) {
486 talloc_free(tmp_ctx);
487 return KDC_PROCESS_FAILED;
490 krb_priv_len = len - ap_req_len;
491 ap_req = data_blob_const(&input->data[header_len], ap_req_len);
492 krb_priv_req = data_blob_const(&input->data[header_len + ap_req_len], krb_priv_len);
494 server_credentials = cli_credentials_init(tmp_ctx);
495 if (!server_credentials) {
496 DEBUG(1, ("Failed to init server credentials\n"));
497 talloc_free(tmp_ctx);
498 return KDC_PROCESS_FAILED;
501 /* We want the credentials subsystem to use the krb5 context
502 * we already have, rather than a new context */
503 cli_credentials_set_krb5_context(server_credentials, kdc->smb_krb5_context);
504 cli_credentials_set_conf(server_credentials, kdc->task->lp_ctx);
506 keytab_name = talloc_asprintf(server_credentials, "HDB:samba4&%p", kdc->base_ctx);
508 cli_credentials_set_username(server_credentials, "kadmin/changepw", CRED_SPECIFIED);
509 ret = cli_credentials_set_keytab_name(server_credentials, kdc->task->lp_ctx, keytab_name, CRED_SPECIFIED);
510 if (ret != 0) {
511 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
512 KRB5_KPASSWD_HARDERROR,
513 talloc_asprintf(mem_ctx,
514 "Failed to obtain server credentials for kadmin/changepw!"),
515 &krb_priv_rep);
516 ap_rep.length = 0;
517 if (ret) {
518 goto reply;
520 talloc_free(tmp_ctx);
521 return ret;
524 /* We don't strictly need to call this wrapper, and could call
525 * gensec_server_start directly, as we have no need for NTLM
526 * and we have a PAC, but this ensures that the wrapper can be
527 * safely extended for other helpful things in future */
528 nt_status = samba_server_gensec_start(tmp_ctx, kdc->task->event_ctx,
529 kdc->task->msg_ctx,
530 kdc->task->lp_ctx,
531 server_credentials,
532 "kpasswd",
533 &gensec_security);
534 if (!NT_STATUS_IS_OK(nt_status)) {
535 talloc_free(tmp_ctx);
536 return KDC_PROCESS_FAILED;
539 /* The kerberos PRIV packets include these addresses. MIT
540 * clients check that they are present */
541 #if 0
542 /* Skip this part for now, it breaks with a NetAPP filer and
543 * in any case where the client address is behind NAT. If
544 * older MIT clients need this, we might have to insert more
545 * complex code */
547 nt_status = gensec_set_local_address(gensec_security, peer_addr);
548 if (!NT_STATUS_IS_OK(nt_status)) {
549 talloc_free(tmp_ctx);
550 return KDC_PROCESS_FAILED;
552 #endif
554 nt_status = gensec_set_local_address(gensec_security, my_addr);
555 if (!NT_STATUS_IS_OK(nt_status)) {
556 talloc_free(tmp_ctx);
557 return KDC_PROCESS_FAILED;
560 /* We want the GENSEC wrap calls to generate PRIV tokens */
561 gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
563 nt_status = gensec_start_mech_by_name(gensec_security, "krb5");
564 if (!NT_STATUS_IS_OK(nt_status)) {
565 talloc_free(tmp_ctx);
566 return KDC_PROCESS_FAILED;
569 /* Accept the AP-REQ and generate teh AP-REP we need for the reply */
570 nt_status = gensec_update(gensec_security, tmp_ctx, ap_req, &ap_rep);
571 if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
573 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
574 KRB5_KPASSWD_HARDERROR,
575 talloc_asprintf(mem_ctx,
576 "gensec_update failed: %s",
577 nt_errstr(nt_status)),
578 &krb_priv_rep);
579 ap_rep.length = 0;
580 if (ret) {
581 goto reply;
583 talloc_free(tmp_ctx);
584 return KDC_PROCESS_FAILED;
587 /* Extract the data from the KRB-PRIV half of the message */
588 nt_status = gensec_unwrap(gensec_security, tmp_ctx, &krb_priv_req, &kpasswd_req);
589 if (!NT_STATUS_IS_OK(nt_status)) {
590 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
591 KRB5_KPASSWD_HARDERROR,
592 talloc_asprintf(mem_ctx,
593 "gensec_unwrap failed: %s",
594 nt_errstr(nt_status)),
595 &krb_priv_rep);
596 ap_rep.length = 0;
597 if (ret) {
598 goto reply;
600 talloc_free(tmp_ctx);
601 return KDC_PROCESS_FAILED;
604 /* Figure out something to do with it (probably changing a password...) */
605 ret = kpasswd_process_request(kdc, tmp_ctx,
606 gensec_security,
607 version,
608 &kpasswd_req, &kpasswd_rep);
609 if (!ret) {
610 /* Argh! */
611 talloc_free(tmp_ctx);
612 return KDC_PROCESS_FAILED;
615 /* And wrap up the reply: This ensures that the error message
616 * or success can be verified by the client */
617 nt_status = gensec_wrap(gensec_security, tmp_ctx,
618 &kpasswd_rep, &krb_priv_rep);
619 if (!NT_STATUS_IS_OK(nt_status)) {
620 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
621 KRB5_KPASSWD_HARDERROR,
622 talloc_asprintf(mem_ctx,
623 "gensec_wrap failed: %s",
624 nt_errstr(nt_status)),
625 &krb_priv_rep);
626 ap_rep.length = 0;
627 if (ret) {
628 goto reply;
630 talloc_free(tmp_ctx);
631 return KDC_PROCESS_FAILED;
634 reply:
635 *reply = data_blob_talloc(mem_ctx, NULL, krb_priv_rep.length + ap_rep.length + header_len);
636 if (!reply->data) {
637 talloc_free(tmp_ctx);
638 return KDC_PROCESS_FAILED;
641 RSSVAL(reply->data, 0, reply->length);
642 RSSVAL(reply->data, 2, 1); /* This is a version 1 reply, MS change/set or otherwise */
643 RSSVAL(reply->data, 4, ap_rep.length);
644 memcpy(reply->data + header_len,
645 ap_rep.data,
646 ap_rep.length);
647 memcpy(reply->data + header_len + ap_rep.length,
648 krb_priv_rep.data,
649 krb_priv_rep.length);
651 talloc_free(tmp_ctx);
652 return KDC_PROCESS_OK;