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"
33 #include "dsdb/common/util.h"
34 #include "kdc/kpasswd_glue.h"
36 /* Return true if there is a valid error packet formed in the error_blob */
37 static bool kpasswdd_make_error_reply(struct kdc_server
*kdc
,
40 const char *error_string
,
41 DATA_BLOB
*error_blob
)
43 char *error_string_utf8
;
46 DEBUG(result_code
? 3 : 10, ("kpasswdd: %s\n", error_string
));
48 if (!push_utf8_talloc(mem_ctx
, &error_string_utf8
, error_string
, &len
)) {
52 *error_blob
= data_blob_talloc(mem_ctx
, NULL
, 2 + len
+ 1);
53 if (!error_blob
->data
) {
56 RSSVAL(error_blob
->data
, 0, result_code
);
57 memcpy(error_blob
->data
+ 2, error_string_utf8
, len
+ 1);
61 /* Return true if there is a valid error packet formed in the error_blob */
62 static bool kpasswdd_make_unauth_error_reply(struct kdc_server
*kdc
,
65 const char *error_string
,
66 DATA_BLOB
*error_blob
)
70 DATA_BLOB error_bytes
;
71 krb5_data k5_error_bytes
, k5_error_blob
;
72 ret
= kpasswdd_make_error_reply(kdc
, mem_ctx
, result_code
, error_string
,
77 k5_error_bytes
.data
= error_bytes
.data
;
78 k5_error_bytes
.length
= error_bytes
.length
;
79 kret
= krb5_mk_error(kdc
->smb_krb5_context
->krb5_context
,
80 result_code
, NULL
, &k5_error_bytes
,
81 NULL
, NULL
, NULL
, NULL
, &k5_error_blob
);
85 *error_blob
= data_blob_talloc(mem_ctx
, k5_error_blob
.data
, k5_error_blob
.length
);
86 krb5_data_free(&k5_error_blob
);
87 if (!error_blob
->data
) {
93 static bool kpasswd_make_pwchange_reply(struct kdc_server
*kdc
,
96 enum samPwdChangeReason reject_reason
,
97 struct samr_DomInfo1
*dominfo
,
98 DATA_BLOB
*error_blob
)
100 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
101 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
102 KRB5_KPASSWD_ACCESSDENIED
,
103 "No such user when changing password",
106 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
107 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
108 KRB5_KPASSWD_ACCESSDENIED
,
109 "Not permitted to change password",
112 if (dominfo
&& NT_STATUS_EQUAL(status
, NT_STATUS_PASSWORD_RESTRICTION
)) {
113 const char *reject_string
;
114 switch (reject_reason
) {
115 case SAM_PWD_CHANGE_PASSWORD_TOO_SHORT
:
116 reject_string
= talloc_asprintf(mem_ctx
, "Password too short, password must be at least %d characters long.",
117 dominfo
->min_password_length
);
119 case SAM_PWD_CHANGE_NOT_COMPLEX
:
120 reject_string
= "Password does not meet complexity requirements";
122 case SAM_PWD_CHANGE_PWD_IN_HISTORY
:
123 reject_string
= talloc_asprintf(mem_ctx
, "Password is already in password history. New password must not match any of your %d previous passwords.",
124 dominfo
->password_history_length
);
127 reject_string
= "Password change rejected, password changes may not be permitted on this account, or the minimum password age may not have elapsed.";
130 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
131 KRB5_KPASSWD_SOFTERROR
,
135 if (!NT_STATUS_IS_OK(status
)) {
136 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
137 KRB5_KPASSWD_HARDERROR
,
138 talloc_asprintf(mem_ctx
, "failed to set password: %s", nt_errstr(status
)),
142 return kpasswdd_make_error_reply(kdc
, mem_ctx
, KRB5_KPASSWD_SUCCESS
,
148 A user password change
150 Return true if there is a valid error packet (or success) formed in
153 static bool kpasswdd_change_password(struct kdc_server
*kdc
,
155 struct auth_session_info
*session_info
,
156 const DATA_BLOB
*password
,
160 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
161 enum samPwdChangeReason reject_reason
;
162 struct samr_DomInfo1
*dominfo
;
163 const char *error_string
;
165 status
= samdb_kpasswd_change_password(mem_ctx
,
167 kdc
->task
->event_ctx
,
175 if (!NT_STATUS_IS_OK(status
)) {
176 return kpasswdd_make_error_reply(kdc
,
178 KRB5_KPASSWD_ACCESSDENIED
,
183 return kpasswd_make_pwchange_reply(kdc
,
191 static bool kpasswd_process_request(struct kdc_server
*kdc
,
193 struct gensec_security
*gensec_security
,
198 struct auth_session_info
*session_info
;
201 if (!NT_STATUS_IS_OK(gensec_session_info(gensec_security
,
204 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
205 KRB5_KPASSWD_HARDERROR
,
206 "gensec_session_info failed!",
211 case KRB5_KPASSWD_VERS_CHANGEPW
:
214 if (!convert_string_talloc_handle(mem_ctx
, lpcfg_iconv_handle(kdc
->task
->lp_ctx
),
216 (const char *)input
->data
,
218 (void **)&password
.data
, &pw_len
)) {
221 password
.length
= pw_len
;
223 return kpasswdd_change_password(kdc
, mem_ctx
, session_info
,
226 case KRB5_KPASSWD_VERS_SETPW
:
229 enum samPwdChangeReason reject_reason
= SAM_PWD_CHANGE_NO_ERROR
;
230 struct samr_DomInfo1
*dominfo
= NULL
;
231 struct ldb_context
*samdb
;
232 krb5_context context
= kdc
->smb_krb5_context
->krb5_context
;
234 ChangePasswdDataMS chpw
;
237 krb5_principal principal
;
238 char *set_password_on_princ
;
239 struct ldb_dn
*set_password_on_dn
;
240 bool service_principal_name
= false;
245 ret
= decode_ChangePasswdDataMS(input
->data
, input
->length
,
248 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
249 KRB5_KPASSWD_MALFORMED
,
250 "failed to decode password change structure",
254 if (!convert_string_talloc_handle(mem_ctx
, lpcfg_iconv_handle(kdc
->task
->lp_ctx
),
256 (const char *)chpw
.newpasswd
.data
,
257 chpw
.newpasswd
.length
,
258 (void **)&password
.data
, &pw_len
)) {
259 free_ChangePasswdDataMS(&chpw
);
263 password
.length
= pw_len
;
265 if ((chpw
.targname
&& !chpw
.targrealm
)
266 || (!chpw
.targname
&& chpw
.targrealm
)) {
267 free_ChangePasswdDataMS(&chpw
);
268 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
269 KRB5_KPASSWD_MALFORMED
,
270 "Realm and principal must be both present, or neither present",
273 if (chpw
.targname
&& chpw
.targrealm
) {
274 ret
= krb5_build_principal_ext(kdc
->smb_krb5_context
->krb5_context
,
276 strlen(*chpw
.targrealm
),
279 free_ChangePasswdDataMS(&chpw
);
280 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
281 KRB5_KPASSWD_MALFORMED
,
282 "failed to get principal",
285 if (copy_PrincipalName(chpw
.targname
, &principal
->name
)) {
286 free_ChangePasswdDataMS(&chpw
);
287 krb5_free_principal(context
, principal
);
288 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
289 KRB5_KPASSWD_MALFORMED
,
290 "failed to extract principal to set",
294 free_ChangePasswdDataMS(&chpw
);
295 return kpasswdd_change_password(kdc
, mem_ctx
, session_info
,
298 free_ChangePasswdDataMS(&chpw
);
300 if (principal
->name
.name_string
.len
>= 2) {
301 service_principal_name
= true;
303 /* We use this, rather than 'no realm' flag,
304 * as we don't want to accept a password
305 * change on a principal from another realm */
307 if (krb5_unparse_name_short(context
, principal
, &set_password_on_princ
) != 0) {
308 krb5_free_principal(context
, principal
);
309 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
310 KRB5_KPASSWD_MALFORMED
,
311 "krb5_unparse_name failed!",
315 if (krb5_unparse_name(context
, principal
, &set_password_on_princ
) != 0) {
316 krb5_free_principal(context
, principal
);
317 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
318 KRB5_KPASSWD_MALFORMED
,
319 "krb5_unparse_name failed!",
323 krb5_free_principal(context
, principal
);
325 samdb
= samdb_connect(mem_ctx
, kdc
->task
->event_ctx
, kdc
->task
->lp_ctx
, session_info
, 0);
327 free(set_password_on_princ
);
328 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
329 KRB5_KPASSWD_HARDERROR
,
330 "Unable to open database!",
334 DEBUG(3, ("%s\\%s (%s) is changing password of %s\n",
335 session_info
->info
->domain_name
,
336 session_info
->info
->account_name
,
337 dom_sid_string(mem_ctx
, &session_info
->security_token
->sids
[PRIMARY_USER_SID_INDEX
]),
338 set_password_on_princ
));
339 ret
= ldb_transaction_start(samdb
);
340 if (ret
!= LDB_SUCCESS
) {
341 free(set_password_on_princ
);
342 status
= NT_STATUS_TRANSACTION_ABORTED
;
343 return kpasswd_make_pwchange_reply(kdc
, mem_ctx
,
345 SAM_PWD_CHANGE_NO_ERROR
,
350 if (service_principal_name
) {
351 status
= crack_service_principal_name(samdb
, mem_ctx
,
352 set_password_on_princ
,
353 &set_password_on_dn
, NULL
);
355 status
= crack_user_principal_name(samdb
, mem_ctx
,
356 set_password_on_princ
,
357 &set_password_on_dn
, NULL
);
359 free(set_password_on_princ
);
360 if (!NT_STATUS_IS_OK(status
)) {
361 ldb_transaction_cancel(samdb
);
362 return kpasswd_make_pwchange_reply(kdc
, mem_ctx
,
364 SAM_PWD_CHANGE_NO_ERROR
,
369 if (NT_STATUS_IS_OK(status
)) {
370 /* Admin password set */
371 status
= samdb_set_password(samdb
, mem_ctx
,
372 set_password_on_dn
, NULL
,
373 &password
, NULL
, NULL
,
374 NULL
, NULL
, /* this is not a user password change */
375 &reject_reason
, &dominfo
);
378 if (NT_STATUS_IS_OK(status
)) {
379 ret
= ldb_transaction_commit(samdb
);
380 if (ret
!= LDB_SUCCESS
) {
381 DEBUG(1,("Failed to commit transaction to set password on %s: %s\n",
382 ldb_dn_get_linearized(set_password_on_dn
),
383 ldb_errstring(samdb
)));
384 status
= NT_STATUS_TRANSACTION_ABORTED
;
387 ldb_transaction_cancel(samdb
);
389 return kpasswd_make_pwchange_reply(kdc
, mem_ctx
,
396 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
397 KRB5_KPASSWD_BAD_VERSION
,
398 talloc_asprintf(mem_ctx
,
399 "Protocol version %u not supported",
405 enum kdc_process_ret
kpasswdd_process(struct kdc_server
*kdc
,
409 struct tsocket_address
*peer_addr
,
410 struct tsocket_address
*my_addr
,
414 const uint16_t header_len
= 6;
417 uint16_t krb_priv_len
;
420 DATA_BLOB ap_req
, krb_priv_req
;
421 DATA_BLOB krb_priv_rep
= data_blob(NULL
, 0);
422 DATA_BLOB ap_rep
= data_blob(NULL
, 0);
423 DATA_BLOB kpasswd_req
, kpasswd_rep
;
424 struct cli_credentials
*server_credentials
;
425 struct gensec_security
*gensec_security
;
426 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
431 return KDC_PROCESS_FAILED
;
435 talloc_free(tmp_ctx
);
436 return KDC_PROCESS_PROXY
;
439 /* Be parinoid. We need to ensure we don't just let the
440 * caller lead us into a buffer overflow */
441 if (input
->length
<= header_len
) {
442 talloc_free(tmp_ctx
);
443 return KDC_PROCESS_FAILED
;
446 len
= RSVAL(input
->data
, 0);
447 if (input
->length
!= len
) {
448 talloc_free(tmp_ctx
);
449 return KDC_PROCESS_FAILED
;
452 /* There are two different versions of this protocol so far,
453 * plus others in the standards pipe. Fortunetly they all
454 * take a very similar framing */
455 version
= RSVAL(input
->data
, 2);
456 ap_req_len
= RSVAL(input
->data
, 4);
457 if ((ap_req_len
>= len
) || (ap_req_len
+ header_len
) >= len
) {
458 talloc_free(tmp_ctx
);
459 return KDC_PROCESS_FAILED
;
462 krb_priv_len
= len
- ap_req_len
;
463 ap_req
= data_blob_const(&input
->data
[header_len
], ap_req_len
);
464 krb_priv_req
= data_blob_const(&input
->data
[header_len
+ ap_req_len
], krb_priv_len
);
466 server_credentials
= cli_credentials_init(tmp_ctx
);
467 if (!server_credentials
) {
468 DEBUG(1, ("Failed to init server credentials\n"));
469 talloc_free(tmp_ctx
);
470 return KDC_PROCESS_FAILED
;
473 /* We want the credentials subsystem to use the krb5 context
474 * we already have, rather than a new context */
475 cli_credentials_set_krb5_context(server_credentials
, kdc
->smb_krb5_context
);
476 cli_credentials_set_conf(server_credentials
, kdc
->task
->lp_ctx
);
478 keytab_name
= talloc_asprintf(server_credentials
, "HDB:samba4&%p", kdc
->base_ctx
);
480 cli_credentials_set_username(server_credentials
, "kadmin/changepw", CRED_SPECIFIED
);
481 ret
= cli_credentials_set_keytab_name(server_credentials
, kdc
->task
->lp_ctx
, keytab_name
, CRED_SPECIFIED
);
483 ret
= kpasswdd_make_unauth_error_reply(kdc
, mem_ctx
,
484 KRB5_KPASSWD_HARDERROR
,
485 talloc_asprintf(mem_ctx
,
486 "Failed to obtain server credentials for kadmin/changepw!"),
492 talloc_free(tmp_ctx
);
496 /* We don't strictly need to call this wrapper, and could call
497 * gensec_server_start directly, as we have no need for NTLM
498 * and we have a PAC, but this ensures that the wrapper can be
499 * safely extended for other helpful things in future */
500 nt_status
= samba_server_gensec_start(tmp_ctx
, kdc
->task
->event_ctx
,
506 if (!NT_STATUS_IS_OK(nt_status
)) {
507 talloc_free(tmp_ctx
);
508 return KDC_PROCESS_FAILED
;
511 /* The kerberos PRIV packets include these addresses. MIT
512 * clients check that they are present */
514 /* Skip this part for now, it breaks with a NetAPP filer and
515 * in any case where the client address is behind NAT. If
516 * older MIT clients need this, we might have to insert more
519 nt_status
= gensec_set_remote_address(gensec_security
, peer_addr
);
520 if (!NT_STATUS_IS_OK(nt_status
)) {
521 talloc_free(tmp_ctx
);
522 return KDC_PROCESS_FAILED
;
526 nt_status
= gensec_set_local_address(gensec_security
, my_addr
);
527 if (!NT_STATUS_IS_OK(nt_status
)) {
528 talloc_free(tmp_ctx
);
529 return KDC_PROCESS_FAILED
;
532 /* We want the GENSEC wrap calls to generate PRIV tokens */
533 gensec_want_feature(gensec_security
, GENSEC_FEATURE_SEAL
);
535 nt_status
= gensec_start_mech_by_name(gensec_security
, "krb5");
536 if (!NT_STATUS_IS_OK(nt_status
)) {
537 talloc_free(tmp_ctx
);
538 return KDC_PROCESS_FAILED
;
541 /* Accept the AP-REQ and generate teh AP-REP we need for the reply */
542 nt_status
= gensec_update_ev(gensec_security
, tmp_ctx
, kdc
->task
->event_ctx
, ap_req
, &ap_rep
);
543 if (!NT_STATUS_IS_OK(nt_status
) && !NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
545 ret
= kpasswdd_make_unauth_error_reply(kdc
, mem_ctx
,
546 KRB5_KPASSWD_HARDERROR
,
547 talloc_asprintf(mem_ctx
,
548 "gensec_update failed: %s",
549 nt_errstr(nt_status
)),
555 talloc_free(tmp_ctx
);
556 return KDC_PROCESS_FAILED
;
559 /* Extract the data from the KRB-PRIV half of the message */
560 nt_status
= gensec_unwrap(gensec_security
, tmp_ctx
, &krb_priv_req
, &kpasswd_req
);
561 if (!NT_STATUS_IS_OK(nt_status
)) {
562 ret
= kpasswdd_make_unauth_error_reply(kdc
, mem_ctx
,
563 KRB5_KPASSWD_HARDERROR
,
564 talloc_asprintf(mem_ctx
,
565 "gensec_unwrap failed: %s",
566 nt_errstr(nt_status
)),
572 talloc_free(tmp_ctx
);
573 return KDC_PROCESS_FAILED
;
576 /* Figure out something to do with it (probably changing a password...) */
577 ret
= kpasswd_process_request(kdc
, tmp_ctx
,
580 &kpasswd_req
, &kpasswd_rep
);
583 talloc_free(tmp_ctx
);
584 return KDC_PROCESS_FAILED
;
587 /* And wrap up the reply: This ensures that the error message
588 * or success can be verified by the client */
589 nt_status
= gensec_wrap(gensec_security
, tmp_ctx
,
590 &kpasswd_rep
, &krb_priv_rep
);
591 if (!NT_STATUS_IS_OK(nt_status
)) {
592 ret
= kpasswdd_make_unauth_error_reply(kdc
, mem_ctx
,
593 KRB5_KPASSWD_HARDERROR
,
594 talloc_asprintf(mem_ctx
,
595 "gensec_wrap failed: %s",
596 nt_errstr(nt_status
)),
602 talloc_free(tmp_ctx
);
603 return KDC_PROCESS_FAILED
;
607 *reply
= data_blob_talloc(mem_ctx
, NULL
, krb_priv_rep
.length
+ ap_rep
.length
+ header_len
);
609 talloc_free(tmp_ctx
);
610 return KDC_PROCESS_FAILED
;
613 RSSVAL(reply
->data
, 0, reply
->length
);
614 RSSVAL(reply
->data
, 2, 1); /* This is a version 1 reply, MS change/set or otherwise */
615 RSSVAL(reply
->data
, 4, ap_rep
.length
);
616 memcpy(reply
->data
+ header_len
,
619 memcpy(reply
->data
+ header_len
+ ap_rep
.length
,
621 krb_priv_rep
.length
);
623 talloc_free(tmp_ctx
);
624 return KDC_PROCESS_OK
;