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/>.
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
,
38 const char *error_string
,
39 DATA_BLOB
*error_blob
)
41 char *error_string_utf8
;
44 DEBUG(result_code
? 3 : 10, ("kpasswdd: %s\n", error_string
));
46 if (!push_utf8_talloc(mem_ctx
, &error_string_utf8
, error_string
, &len
)) {
50 *error_blob
= data_blob_talloc(mem_ctx
, NULL
, 2 + len
+ 1);
51 if (!error_blob
->data
) {
54 RSSVAL(error_blob
->data
, 0, result_code
);
55 memcpy(error_blob
->data
+ 2, error_string_utf8
, len
+ 1);
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
,
63 const char *error_string
,
64 DATA_BLOB
*error_blob
)
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
,
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
);
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
) {
91 static bool kpasswd_make_pwchange_reply(struct kdc_server
*kdc
,
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",
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",
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
);
117 case SAM_PWD_CHANGE_NOT_COMPLEX
:
118 reject_string
= "Password does not meet complexity requirements";
120 case SAM_PWD_CHANGE_PWD_IN_HISTORY
:
121 reject_string
= "Password is already in password history";
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
);
128 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
129 KRB5_KPASSWD_SOFTERROR
,
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
)),
140 return kpasswdd_make_error_reply(kdc
, mem_ctx
, KRB5_KPASSWD_SUCCESS
,
146 A user password change
148 Return true if there is a valid error packet (or success) formed in
151 static bool kpasswdd_change_password(struct kdc_server
*kdc
,
153 struct auth_session_info
*session_info
,
154 const DATA_BLOB
*password
,
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
;
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
);
174 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
175 KRB5_KPASSWD_ACCESSDENIED
,
176 "No such user when changing password",
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",
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
,
193 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
194 KRB5_KPASSWD_HARDERROR
,
195 "Failed to open samdb",
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 */
211 return kpasswd_make_pwchange_reply(kdc
, mem_ctx
,
219 static bool kpasswd_process_request(struct kdc_server
*kdc
,
221 struct gensec_security
*gensec_security
,
226 struct auth_session_info
*session_info
;
229 if (!NT_STATUS_IS_OK(gensec_session_info(gensec_security
,
232 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
233 KRB5_KPASSWD_HARDERROR
,
234 "gensec_session_info failed!",
239 case KRB5_KPASSWD_VERS_CHANGEPW
:
242 if (!convert_string_talloc_handle(mem_ctx
, lpcfg_iconv_handle(kdc
->task
->lp_ctx
),
244 (const char *)input
->data
,
246 (void **)&password
.data
, &pw_len
)) {
249 password
.length
= pw_len
;
251 return kpasswdd_change_password(kdc
, mem_ctx
, session_info
,
254 case KRB5_KPASSWD_VERS_SETPW
:
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
;
265 krb5_principal principal
;
266 char *set_password_on_princ
;
267 struct ldb_dn
*set_password_on_dn
;
268 bool service_principal_name
= false;
273 ret
= decode_ChangePasswdDataMS(input
->data
, input
->length
,
276 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
277 KRB5_KPASSWD_MALFORMED
,
278 "failed to decode password change structure",
282 if (!convert_string_talloc_handle(mem_ctx
, lpcfg_iconv_handle(kdc
->task
->lp_ctx
),
284 (const char *)chpw
.newpasswd
.data
,
285 chpw
.newpasswd
.length
,
286 (void **)&password
.data
, &pw_len
)) {
287 free_ChangePasswdDataMS(&chpw
);
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",
301 if (chpw
.targname
&& chpw
.targrealm
) {
302 ret
= krb5_build_principal_ext(kdc
->smb_krb5_context
->krb5_context
,
304 strlen(*chpw
.targrealm
),
307 free_ChangePasswdDataMS(&chpw
);
308 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
309 KRB5_KPASSWD_MALFORMED
,
310 "failed to get principal",
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",
322 free_ChangePasswdDataMS(&chpw
);
323 return kpasswdd_change_password(kdc
, mem_ctx
, session_info
,
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!",
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!",
351 krb5_free_principal(context
, principal
);
353 samdb
= samdb_connect(mem_ctx
, kdc
->task
->event_ctx
, kdc
->task
->lp_ctx
, session_info
, 0);
355 free(set_password_on_princ
);
356 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
357 KRB5_KPASSWD_HARDERROR
,
358 "Unable to open database!",
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
,
373 SAM_PWD_CHANGE_NO_ERROR
,
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
);
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
,
392 SAM_PWD_CHANGE_NO_ERROR
,
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
;
415 ldb_transaction_cancel(samdb
);
417 return kpasswd_make_pwchange_reply(kdc
, mem_ctx
,
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",
433 enum kdc_process_ret
kpasswdd_process(struct kdc_server
*kdc
,
437 struct tsocket_address
*peer_addr
,
438 struct tsocket_address
*my_addr
,
442 const uint16_t header_len
= 6;
445 uint16_t krb_priv_len
;
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
);
459 return KDC_PROCESS_FAILED
;
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
);
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!"),
520 talloc_free(tmp_ctx
);
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
,
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 */
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
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
;
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
)),
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
)),
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
,
608 &kpasswd_req
, &kpasswd_rep
);
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
)),
630 talloc_free(tmp_ctx
);
631 return KDC_PROCESS_FAILED
;
635 *reply
= data_blob_talloc(mem_ctx
, NULL
, krb_priv_rep
.length
+ ap_rep
.length
+ header_len
);
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
,
647 memcpy(reply
->data
+ header_len
+ ap_rep
.length
,
649 krb_priv_rep
.length
);
651 talloc_free(tmp_ctx
);
652 return KDC_PROCESS_OK
;