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
,
231 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
232 KRB5_KPASSWD_HARDERROR
,
233 "gensec_session_info failed!",
238 case KRB5_KPASSWD_VERS_CHANGEPW
:
241 if (!convert_string_talloc_handle(mem_ctx
, lpcfg_iconv_handle(kdc
->task
->lp_ctx
),
243 (const char *)input
->data
,
245 (void **)&password
.data
, &pw_len
)) {
248 password
.length
= pw_len
;
250 return kpasswdd_change_password(kdc
, mem_ctx
, session_info
,
253 case KRB5_KPASSWD_VERS_SETPW
:
256 enum samPwdChangeReason reject_reason
= SAM_PWD_CHANGE_NO_ERROR
;
257 struct samr_DomInfo1
*dominfo
= NULL
;
258 struct ldb_context
*samdb
;
259 krb5_context context
= kdc
->smb_krb5_context
->krb5_context
;
261 ChangePasswdDataMS chpw
;
264 krb5_principal principal
;
265 char *set_password_on_princ
;
266 struct ldb_dn
*set_password_on_dn
;
267 bool service_principal_name
= false;
272 ret
= decode_ChangePasswdDataMS(input
->data
, input
->length
,
275 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
276 KRB5_KPASSWD_MALFORMED
,
277 "failed to decode password change structure",
281 if (!convert_string_talloc_handle(mem_ctx
, lpcfg_iconv_handle(kdc
->task
->lp_ctx
),
283 (const char *)chpw
.newpasswd
.data
,
284 chpw
.newpasswd
.length
,
285 (void **)&password
.data
, &pw_len
)) {
286 free_ChangePasswdDataMS(&chpw
);
290 password
.length
= pw_len
;
292 if ((chpw
.targname
&& !chpw
.targrealm
)
293 || (!chpw
.targname
&& chpw
.targrealm
)) {
294 free_ChangePasswdDataMS(&chpw
);
295 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
296 KRB5_KPASSWD_MALFORMED
,
297 "Realm and principal must be both present, or neither present",
300 if (chpw
.targname
&& chpw
.targrealm
) {
301 ret
= krb5_build_principal_ext(kdc
->smb_krb5_context
->krb5_context
,
303 strlen(*chpw
.targrealm
),
306 free_ChangePasswdDataMS(&chpw
);
307 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
308 KRB5_KPASSWD_MALFORMED
,
309 "failed to get principal",
312 if (copy_PrincipalName(chpw
.targname
, &principal
->name
)) {
313 free_ChangePasswdDataMS(&chpw
);
314 krb5_free_principal(context
, principal
);
315 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
316 KRB5_KPASSWD_MALFORMED
,
317 "failed to extract principal to set",
321 free_ChangePasswdDataMS(&chpw
);
322 return kpasswdd_change_password(kdc
, mem_ctx
, session_info
,
325 free_ChangePasswdDataMS(&chpw
);
327 if (principal
->name
.name_string
.len
>= 2) {
328 service_principal_name
= true;
330 /* We use this, rather than 'no realm' flag,
331 * as we don't want to accept a password
332 * change on a principal from another realm */
334 if (krb5_unparse_name_short(context
, principal
, &set_password_on_princ
) != 0) {
335 krb5_free_principal(context
, principal
);
336 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
337 KRB5_KPASSWD_MALFORMED
,
338 "krb5_unparse_name failed!",
342 if (krb5_unparse_name(context
, principal
, &set_password_on_princ
) != 0) {
343 krb5_free_principal(context
, principal
);
344 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
345 KRB5_KPASSWD_MALFORMED
,
346 "krb5_unparse_name failed!",
350 krb5_free_principal(context
, principal
);
352 samdb
= samdb_connect(mem_ctx
, kdc
->task
->event_ctx
, kdc
->task
->lp_ctx
, session_info
, 0);
354 free(set_password_on_princ
);
355 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
356 KRB5_KPASSWD_HARDERROR
,
357 "Unable to open database!",
361 DEBUG(3, ("%s\\%s (%s) is changing password of %s\n",
362 session_info
->info
->domain_name
,
363 session_info
->info
->account_name
,
364 dom_sid_string(mem_ctx
, &session_info
->security_token
->sids
[PRIMARY_USER_SID_INDEX
]),
365 set_password_on_princ
));
366 ret
= ldb_transaction_start(samdb
);
367 if (ret
!= LDB_SUCCESS
) {
368 free(set_password_on_princ
);
369 status
= NT_STATUS_TRANSACTION_ABORTED
;
370 return kpasswd_make_pwchange_reply(kdc
, mem_ctx
,
372 SAM_PWD_CHANGE_NO_ERROR
,
377 if (service_principal_name
) {
378 status
= crack_service_principal_name(samdb
, mem_ctx
,
379 set_password_on_princ
,
380 &set_password_on_dn
, NULL
);
382 status
= crack_user_principal_name(samdb
, mem_ctx
,
383 set_password_on_princ
,
384 &set_password_on_dn
, NULL
);
386 free(set_password_on_princ
);
387 if (!NT_STATUS_IS_OK(status
)) {
388 ldb_transaction_cancel(samdb
);
389 return kpasswd_make_pwchange_reply(kdc
, mem_ctx
,
391 SAM_PWD_CHANGE_NO_ERROR
,
396 if (NT_STATUS_IS_OK(status
)) {
397 /* Admin password set */
398 status
= samdb_set_password(samdb
, mem_ctx
,
399 set_password_on_dn
, NULL
,
400 &password
, NULL
, NULL
,
401 NULL
, NULL
, /* this is not a user password change */
402 &reject_reason
, &dominfo
);
405 if (NT_STATUS_IS_OK(status
)) {
406 ret
= ldb_transaction_commit(samdb
);
407 if (ret
!= LDB_SUCCESS
) {
408 DEBUG(1,("Failed to commit transaction to set password on %s: %s\n",
409 ldb_dn_get_linearized(set_password_on_dn
),
410 ldb_errstring(samdb
)));
411 status
= NT_STATUS_TRANSACTION_ABORTED
;
414 ldb_transaction_cancel(samdb
);
416 return kpasswd_make_pwchange_reply(kdc
, mem_ctx
,
423 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
424 KRB5_KPASSWD_BAD_VERSION
,
425 talloc_asprintf(mem_ctx
,
426 "Protocol version %u not supported",
432 enum kdc_process_ret
kpasswdd_process(struct kdc_server
*kdc
,
436 struct tsocket_address
*peer_addr
,
437 struct tsocket_address
*my_addr
,
441 const uint16_t header_len
= 6;
444 uint16_t krb_priv_len
;
447 DATA_BLOB ap_req
, krb_priv_req
;
448 DATA_BLOB krb_priv_rep
= data_blob(NULL
, 0);
449 DATA_BLOB ap_rep
= data_blob(NULL
, 0);
450 DATA_BLOB kpasswd_req
, kpasswd_rep
;
451 struct cli_credentials
*server_credentials
;
452 struct gensec_security
*gensec_security
;
453 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
458 return KDC_PROCESS_FAILED
;
462 talloc_free(tmp_ctx
);
463 return KDC_PROCESS_PROXY
;
466 /* Be parinoid. We need to ensure we don't just let the
467 * caller lead us into a buffer overflow */
468 if (input
->length
<= header_len
) {
469 talloc_free(tmp_ctx
);
470 return KDC_PROCESS_FAILED
;
473 len
= RSVAL(input
->data
, 0);
474 if (input
->length
!= len
) {
475 talloc_free(tmp_ctx
);
476 return KDC_PROCESS_FAILED
;
479 /* There are two different versions of this protocol so far,
480 * plus others in the standards pipe. Fortunetly they all
481 * take a very similar framing */
482 version
= RSVAL(input
->data
, 2);
483 ap_req_len
= RSVAL(input
->data
, 4);
484 if ((ap_req_len
>= len
) || (ap_req_len
+ header_len
) >= len
) {
485 talloc_free(tmp_ctx
);
486 return KDC_PROCESS_FAILED
;
489 krb_priv_len
= len
- ap_req_len
;
490 ap_req
= data_blob_const(&input
->data
[header_len
], ap_req_len
);
491 krb_priv_req
= data_blob_const(&input
->data
[header_len
+ ap_req_len
], krb_priv_len
);
493 server_credentials
= cli_credentials_init(tmp_ctx
);
494 if (!server_credentials
) {
495 DEBUG(1, ("Failed to init server credentials\n"));
496 talloc_free(tmp_ctx
);
497 return KDC_PROCESS_FAILED
;
500 /* We want the credentials subsystem to use the krb5 context
501 * we already have, rather than a new context */
502 cli_credentials_set_krb5_context(server_credentials
, kdc
->smb_krb5_context
);
503 cli_credentials_set_conf(server_credentials
, kdc
->task
->lp_ctx
);
505 keytab_name
= talloc_asprintf(server_credentials
, "HDB:samba4&%p", kdc
->base_ctx
);
507 cli_credentials_set_username(server_credentials
, "kadmin/changepw", CRED_SPECIFIED
);
508 ret
= cli_credentials_set_keytab_name(server_credentials
, kdc
->task
->lp_ctx
, keytab_name
, CRED_SPECIFIED
);
510 ret
= kpasswdd_make_unauth_error_reply(kdc
, mem_ctx
,
511 KRB5_KPASSWD_HARDERROR
,
512 talloc_asprintf(mem_ctx
,
513 "Failed to obtain server credentials for kadmin/changepw!"),
519 talloc_free(tmp_ctx
);
523 /* We don't strictly need to call this wrapper, and could call
524 * gensec_server_start directly, as we have no need for NTLM
525 * and we have a PAC, but this ensures that the wrapper can be
526 * safely extended for other helpful things in future */
527 nt_status
= samba_server_gensec_start(tmp_ctx
, kdc
->task
->event_ctx
,
533 if (!NT_STATUS_IS_OK(nt_status
)) {
534 talloc_free(tmp_ctx
);
535 return KDC_PROCESS_FAILED
;
538 /* The kerberos PRIV packets include these addresses. MIT
539 * clients check that they are present */
541 /* Skip this part for now, it breaks with a NetAPP filer and
542 * in any case where the client address is behind NAT. If
543 * older MIT clients need this, we might have to insert more
546 nt_status
= gensec_set_local_address(gensec_security
, peer_addr
);
547 if (!NT_STATUS_IS_OK(nt_status
)) {
548 talloc_free(tmp_ctx
);
549 return KDC_PROCESS_FAILED
;
553 nt_status
= gensec_set_local_address(gensec_security
, my_addr
);
554 if (!NT_STATUS_IS_OK(nt_status
)) {
555 talloc_free(tmp_ctx
);
556 return KDC_PROCESS_FAILED
;
559 /* We want the GENSEC wrap calls to generate PRIV tokens */
560 gensec_want_feature(gensec_security
, GENSEC_FEATURE_SEAL
);
562 nt_status
= gensec_start_mech_by_name(gensec_security
, "krb5");
563 if (!NT_STATUS_IS_OK(nt_status
)) {
564 talloc_free(tmp_ctx
);
565 return KDC_PROCESS_FAILED
;
568 /* Accept the AP-REQ and generate teh AP-REP we need for the reply */
569 nt_status
= gensec_update(gensec_security
, tmp_ctx
, ap_req
, &ap_rep
);
570 if (!NT_STATUS_IS_OK(nt_status
) && !NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
572 ret
= kpasswdd_make_unauth_error_reply(kdc
, mem_ctx
,
573 KRB5_KPASSWD_HARDERROR
,
574 talloc_asprintf(mem_ctx
,
575 "gensec_update failed: %s",
576 nt_errstr(nt_status
)),
582 talloc_free(tmp_ctx
);
583 return KDC_PROCESS_FAILED
;
586 /* Extract the data from the KRB-PRIV half of the message */
587 nt_status
= gensec_unwrap(gensec_security
, tmp_ctx
, &krb_priv_req
, &kpasswd_req
);
588 if (!NT_STATUS_IS_OK(nt_status
)) {
589 ret
= kpasswdd_make_unauth_error_reply(kdc
, mem_ctx
,
590 KRB5_KPASSWD_HARDERROR
,
591 talloc_asprintf(mem_ctx
,
592 "gensec_unwrap failed: %s",
593 nt_errstr(nt_status
)),
599 talloc_free(tmp_ctx
);
600 return KDC_PROCESS_FAILED
;
603 /* Figure out something to do with it (probably changing a password...) */
604 ret
= kpasswd_process_request(kdc
, tmp_ctx
,
607 &kpasswd_req
, &kpasswd_rep
);
610 talloc_free(tmp_ctx
);
611 return KDC_PROCESS_FAILED
;
614 /* And wrap up the reply: This ensures that the error message
615 * or success can be verified by the client */
616 nt_status
= gensec_wrap(gensec_security
, tmp_ctx
,
617 &kpasswd_rep
, &krb_priv_rep
);
618 if (!NT_STATUS_IS_OK(nt_status
)) {
619 ret
= kpasswdd_make_unauth_error_reply(kdc
, mem_ctx
,
620 KRB5_KPASSWD_HARDERROR
,
621 talloc_asprintf(mem_ctx
,
622 "gensec_wrap failed: %s",
623 nt_errstr(nt_status
)),
629 talloc_free(tmp_ctx
);
630 return KDC_PROCESS_FAILED
;
634 *reply
= data_blob_talloc(mem_ctx
, NULL
, krb_priv_rep
.length
+ ap_rep
.length
+ header_len
);
636 talloc_free(tmp_ctx
);
637 return KDC_PROCESS_FAILED
;
640 RSSVAL(reply
->data
, 0, reply
->length
);
641 RSSVAL(reply
->data
, 2, 1); /* This is a version 1 reply, MS change/set or otherwise */
642 RSSVAL(reply
->data
, 4, ap_rep
.length
);
643 memcpy(reply
->data
+ header_len
,
646 memcpy(reply
->data
+ header_len
+ ap_rep
.length
,
648 krb_priv_rep
.length
);
650 talloc_free(tmp_ctx
);
651 return KDC_PROCESS_OK
;