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 "lib/events/events.h"
26 #include "lib/socket/socket.h"
27 #include "system/network.h"
28 #include "../lib/util/dlinklist.h"
29 #include "lib/ldb/include/ldb.h"
30 #include "auth/gensec/gensec.h"
31 #include "auth/credentials/credentials.h"
32 #include "auth/credentials/credentials_krb5.h"
33 #include "auth/auth.h"
34 #include "dsdb/samdb/samdb.h"
35 #include "rpc_server/dcerpc_server.h"
36 #include "rpc_server/samr/proto.h"
37 #include "libcli/security/security.h"
38 #include "param/param.h"
41 /* TODO: remove all SAMBA4_INTERNAL_HEIMDAL stuff from this file */
42 #ifdef SAMBA4_INTERNAL_HEIMDAL
43 #include "heimdal_build/kpasswdd-glue.h"
46 /* hold information about one kdc socket */
47 struct kpasswd_socket
{
48 struct socket_context
*sock
;
49 struct kdc_server
*kdc
;
50 struct tevent_fd
*fde
;
52 /* a queue of outgoing replies that have been deferred */
53 struct kdc_reply
*send_queue
;
56 /* Return true if there is a valid error packet formed in the error_blob */
57 static bool kpasswdd_make_error_reply(struct kdc_server
*kdc
,
60 const char *error_string
,
61 DATA_BLOB
*error_blob
)
63 char *error_string_utf8
;
66 DEBUG(result_code
? 3 : 10, ("kpasswdd: %s\n", error_string
));
68 if (!push_utf8_talloc(mem_ctx
, &error_string_utf8
, error_string
, &len
)) {
72 *error_blob
= data_blob_talloc(mem_ctx
, NULL
, 2 + len
+ 1);
73 if (!error_blob
->data
) {
76 RSSVAL(error_blob
->data
, 0, result_code
);
77 memcpy(error_blob
->data
+ 2, error_string_utf8
, len
+ 1);
81 /* Return true if there is a valid error packet formed in the error_blob */
82 static bool kpasswdd_make_unauth_error_reply(struct kdc_server
*kdc
,
85 const char *error_string
,
86 DATA_BLOB
*error_blob
)
90 DATA_BLOB error_bytes
;
91 krb5_data k5_error_bytes
, k5_error_blob
;
92 ret
= kpasswdd_make_error_reply(kdc
, mem_ctx
, result_code
, error_string
,
97 k5_error_bytes
.data
= error_bytes
.data
;
98 k5_error_bytes
.length
= error_bytes
.length
;
99 kret
= krb5_mk_error(kdc
->smb_krb5_context
->krb5_context
,
100 result_code
, NULL
, &k5_error_bytes
,
101 NULL
, NULL
, NULL
, NULL
, &k5_error_blob
);
105 *error_blob
= data_blob_talloc(mem_ctx
, k5_error_blob
.data
, k5_error_blob
.length
);
106 krb5_data_free(&k5_error_blob
);
107 if (!error_blob
->data
) {
113 static bool kpasswd_make_pwchange_reply(struct kdc_server
*kdc
,
116 enum samPwdChangeReason reject_reason
,
117 struct samr_DomInfo1
*dominfo
,
118 DATA_BLOB
*error_blob
)
120 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
121 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
122 KRB5_KPASSWD_ACCESSDENIED
,
123 "No such user when changing password",
126 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
127 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
128 KRB5_KPASSWD_ACCESSDENIED
,
129 "Not permitted to change password",
132 if (dominfo
&& NT_STATUS_EQUAL(status
, NT_STATUS_PASSWORD_RESTRICTION
)) {
133 const char *reject_string
;
134 switch (reject_reason
) {
135 case SAM_PWD_CHANGE_PASSWORD_TOO_SHORT
:
136 reject_string
= talloc_asprintf(mem_ctx
, "Password too short, password must be at least %d characters long",
137 dominfo
->min_password_length
);
139 case SAM_PWD_CHANGE_NOT_COMPLEX
:
140 reject_string
= "Password does not meet complexity requirements";
142 case SAM_PWD_CHANGE_PWD_IN_HISTORY
:
143 reject_string
= "Password is already in password history";
146 reject_string
= talloc_asprintf(mem_ctx
, "Password must be at least %d characters long, and cannot match any of your %d previous passwords",
147 dominfo
->min_password_length
, dominfo
->password_history_length
);
150 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
151 KRB5_KPASSWD_SOFTERROR
,
155 if (!NT_STATUS_IS_OK(status
)) {
156 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
157 KRB5_KPASSWD_HARDERROR
,
158 talloc_asprintf(mem_ctx
, "failed to set password: %s", nt_errstr(status
)),
162 return kpasswdd_make_error_reply(kdc
, mem_ctx
, KRB5_KPASSWD_SUCCESS
,
168 A user password change
170 Return true if there is a valid error packet (or sucess) formed in
173 static bool kpasswdd_change_password(struct kdc_server
*kdc
,
175 struct auth_session_info
*session_info
,
176 const DATA_BLOB
*password
,
180 enum samPwdChangeReason reject_reason
;
181 struct samr_DomInfo1
*dominfo
;
182 struct ldb_context
*samdb
;
184 samdb
= samdb_connect(mem_ctx
, kdc
->task
->event_ctx
, kdc
->task
->lp_ctx
, system_session(kdc
->task
->lp_ctx
));
186 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
187 KRB5_KPASSWD_HARDERROR
,
188 "Failed to open samdb",
192 DEBUG(3, ("Changing password of %s\\%s (%s)\n",
193 session_info
->server_info
->domain_name
,
194 session_info
->server_info
->account_name
,
195 dom_sid_string(mem_ctx
, session_info
->security_token
->user_sid
)));
197 /* User password change */
198 status
= samdb_set_password_sid(samdb
, mem_ctx
,
199 session_info
->security_token
->user_sid
,
200 password
, NULL
, NULL
,
201 true, /* this is a user password change */
204 return kpasswd_make_pwchange_reply(kdc
, mem_ctx
,
212 static bool kpasswd_process_request(struct kdc_server
*kdc
,
214 struct gensec_security
*gensec_security
,
219 struct auth_session_info
*session_info
;
222 if (!NT_STATUS_IS_OK(gensec_session_info(gensec_security
,
224 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
225 KRB5_KPASSWD_HARDERROR
,
226 "gensec_session_info failed!",
231 case KRB5_KPASSWD_VERS_CHANGEPW
:
234 if (!convert_string_talloc_convenience(mem_ctx
, lp_iconv_convenience(kdc
->task
->lp_ctx
),
236 (const char *)input
->data
,
238 (void **)&password
.data
, &pw_len
, false)) {
241 password
.length
= pw_len
;
243 return kpasswdd_change_password(kdc
, mem_ctx
, session_info
,
247 case KRB5_KPASSWD_VERS_SETPW
:
250 enum samPwdChangeReason reject_reason
= SAM_PWD_CHANGE_NO_ERROR
;
251 struct samr_DomInfo1
*dominfo
= NULL
;
252 struct ldb_context
*samdb
;
253 struct ldb_message
*msg
;
254 krb5_context context
= kdc
->smb_krb5_context
->krb5_context
;
256 ChangePasswdDataMS chpw
;
259 krb5_principal principal
;
260 char *set_password_on_princ
;
261 struct ldb_dn
*set_password_on_dn
;
266 msg
= ldb_msg_new(mem_ctx
);
271 ret
= decode_ChangePasswdDataMS(input
->data
, input
->length
,
274 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
275 KRB5_KPASSWD_MALFORMED
,
276 "failed to decode password change structure",
280 if (!convert_string_talloc_convenience(mem_ctx
, lp_iconv_convenience(kdc
->task
->lp_ctx
),
282 (const char *)chpw
.newpasswd
.data
,
283 chpw
.newpasswd
.length
,
284 (void **)&password
.data
, &pw_len
, false)) {
285 free_ChangePasswdDataMS(&chpw
);
289 password
.length
= pw_len
;
291 if ((chpw
.targname
&& !chpw
.targrealm
)
292 || (!chpw
.targname
&& chpw
.targrealm
)) {
293 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
294 KRB5_KPASSWD_MALFORMED
,
295 "Realm and principal must be both present, or neither present",
298 if (chpw
.targname
&& chpw
.targrealm
) {
299 #ifdef SAMBA4_INTERNAL_HEIMDAL
300 if (_krb5_principalname2krb5_principal(kdc
->smb_krb5_context
->krb5_context
,
301 &principal
, *chpw
.targname
,
302 *chpw
.targrealm
) != 0) {
303 free_ChangePasswdDataMS(&chpw
);
304 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
305 KRB5_KPASSWD_MALFORMED
,
306 "failed to extract principal to set",
310 #else /* SAMBA4_INTERNAL_HEIMDAL */
311 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
312 KRB5_KPASSWD_BAD_VERSION
,
313 "Operation Not Implemented",
315 #endif /* SAMBA4_INTERNAL_HEIMDAL */
317 free_ChangePasswdDataMS(&chpw
);
318 return kpasswdd_change_password(kdc
, mem_ctx
, session_info
,
321 free_ChangePasswdDataMS(&chpw
);
323 if (krb5_unparse_name(context
, principal
, &set_password_on_princ
) != 0) {
324 krb5_free_principal(context
, principal
);
325 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
326 KRB5_KPASSWD_MALFORMED
,
327 "krb5_unparse_name failed!",
331 krb5_free_principal(context
, principal
);
333 samdb
= samdb_connect(mem_ctx
, kdc
->task
->event_ctx
, kdc
->task
->lp_ctx
, session_info
);
335 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
336 KRB5_KPASSWD_HARDERROR
,
337 "Unable to open database!",
341 DEBUG(3, ("%s\\%s (%s) is changing password of %s\n",
342 session_info
->server_info
->domain_name
,
343 session_info
->server_info
->account_name
,
344 dom_sid_string(mem_ctx
, session_info
->security_token
->user_sid
),
345 set_password_on_princ
));
346 ret
= ldb_transaction_start(samdb
);
348 status
= NT_STATUS_TRANSACTION_ABORTED
;
349 return kpasswd_make_pwchange_reply(kdc
, mem_ctx
,
351 SAM_PWD_CHANGE_NO_ERROR
,
356 status
= crack_user_principal_name(samdb
, mem_ctx
,
357 set_password_on_princ
,
358 &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 msg
= ldb_msg_new(mem_ctx
);
371 ldb_transaction_cancel(samdb
);
372 status
= NT_STATUS_NO_MEMORY
;
374 msg
->dn
= ldb_dn_copy(msg
, set_password_on_dn
);
376 status
= NT_STATUS_NO_MEMORY
;
380 if (NT_STATUS_IS_OK(status
)) {
381 /* Admin password set */
382 status
= samdb_set_password(samdb
, mem_ctx
,
383 set_password_on_dn
, NULL
,
384 msg
, &password
, NULL
, NULL
,
385 false, /* this is not a user password change */
386 &reject_reason
, &dominfo
);
389 if (NT_STATUS_IS_OK(status
)) {
390 /* modify the samdb record */
391 ret
= samdb_replace(samdb
, mem_ctx
, msg
);
393 DEBUG(2,("Failed to modify record to set password on %s: %s\n",
394 ldb_dn_get_linearized(msg
->dn
),
395 ldb_errstring(samdb
)));
396 status
= NT_STATUS_ACCESS_DENIED
;
399 if (NT_STATUS_IS_OK(status
)) {
400 ret
= ldb_transaction_commit(samdb
);
402 DEBUG(1,("Failed to commit transaction to set password on %s: %s\n",
403 ldb_dn_get_linearized(msg
->dn
),
404 ldb_errstring(samdb
)));
405 status
= NT_STATUS_TRANSACTION_ABORTED
;
408 ldb_transaction_cancel(samdb
);
410 return kpasswd_make_pwchange_reply(kdc
, mem_ctx
,
417 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
418 KRB5_KPASSWD_BAD_VERSION
,
419 talloc_asprintf(mem_ctx
,
420 "Protocol version %u not supported",
427 bool kpasswdd_process(struct kdc_server
*kdc
,
431 struct socket_address
*peer_addr
,
432 struct socket_address
*my_addr
,
436 const uint16_t header_len
= 6;
439 uint16_t krb_priv_len
;
442 DATA_BLOB ap_req
, krb_priv_req
;
443 DATA_BLOB krb_priv_rep
= data_blob(NULL
, 0);
444 DATA_BLOB ap_rep
= data_blob(NULL
, 0);
445 DATA_BLOB kpasswd_req
, kpasswd_rep
;
446 struct cli_credentials
*server_credentials
;
447 struct gensec_security
*gensec_security
;
448 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
456 /* Be parinoid. We need to ensure we don't just let the
457 * caller lead us into a buffer overflow */
458 if (input
->length
<= header_len
) {
459 talloc_free(tmp_ctx
);
463 len
= RSVAL(input
->data
, 0);
464 if (input
->length
!= len
) {
465 talloc_free(tmp_ctx
);
469 /* There are two different versions of this protocol so far,
470 * plus others in the standards pipe. Fortunetly they all
471 * take a very similar framing */
472 version
= RSVAL(input
->data
, 2);
473 ap_req_len
= RSVAL(input
->data
, 4);
474 if ((ap_req_len
>= len
) || (ap_req_len
+ header_len
) >= len
) {
475 talloc_free(tmp_ctx
);
479 krb_priv_len
= len
- ap_req_len
;
480 ap_req
= data_blob_const(&input
->data
[header_len
], ap_req_len
);
481 krb_priv_req
= data_blob_const(&input
->data
[header_len
+ ap_req_len
], krb_priv_len
);
483 server_credentials
= cli_credentials_init(tmp_ctx
);
484 if (!server_credentials
) {
485 DEBUG(1, ("Failed to init server credentials\n"));
489 /* We want the credentials subsystem to use the krb5 context
490 * we already have, rather than a new context */
491 cli_credentials_set_krb5_context(server_credentials
, kdc
->smb_krb5_context
);
492 cli_credentials_set_conf(server_credentials
, kdc
->task
->lp_ctx
);
494 keytab_name
= talloc_asprintf(server_credentials
, "HDB:samba4&%p", kdc
->hdb_samba4_context
);
496 cli_credentials_set_username(server_credentials
, "kadmin/changepw", CRED_SPECIFIED
);
497 ret
= cli_credentials_set_keytab_name(server_credentials
, kdc
->task
->event_ctx
, kdc
->task
->lp_ctx
, keytab_name
, CRED_SPECIFIED
);
499 ret
= kpasswdd_make_unauth_error_reply(kdc
, mem_ctx
,
500 KRB5_KPASSWD_HARDERROR
,
501 talloc_asprintf(mem_ctx
,
502 "Failed to obtain server credentials for kadmin/changepw: %s\n",
503 nt_errstr(nt_status
)),
509 talloc_free(tmp_ctx
);
513 /* We don't strictly need to call this wrapper, and could call
514 * gensec_server_start directly, as we have no need for NTLM
515 * and we have a PAC, but this ensures that the wrapper can be
516 * safely extended for other helpful things in future */
517 nt_status
= samba_server_gensec_start(tmp_ctx
, kdc
->task
->event_ctx
,
523 if (!NT_STATUS_IS_OK(nt_status
)) {
524 talloc_free(tmp_ctx
);
528 /* The kerberos PRIV packets include these addresses. MIT
529 * clients check that they are present */
531 /* Skip this part for now, it breaks with a NetAPP filer and
532 * in any case where the client address is behind NAT. If
533 * older MIT clients need this, we might have to insert more
536 nt_status
= gensec_set_peer_addr(gensec_security
, peer_addr
);
537 if (!NT_STATUS_IS_OK(nt_status
)) {
538 talloc_free(tmp_ctx
);
543 nt_status
= gensec_set_my_addr(gensec_security
, my_addr
);
544 if (!NT_STATUS_IS_OK(nt_status
)) {
545 talloc_free(tmp_ctx
);
549 /* We want the GENSEC wrap calls to generate PRIV tokens */
550 gensec_want_feature(gensec_security
, GENSEC_FEATURE_SEAL
);
552 nt_status
= gensec_start_mech_by_name(gensec_security
, "krb5");
553 if (!NT_STATUS_IS_OK(nt_status
)) {
554 talloc_free(tmp_ctx
);
558 /* Accept the AP-REQ and generate teh AP-REP we need for the reply */
559 nt_status
= gensec_update(gensec_security
, tmp_ctx
, ap_req
, &ap_rep
);
560 if (!NT_STATUS_IS_OK(nt_status
) && !NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
562 ret
= kpasswdd_make_unauth_error_reply(kdc
, mem_ctx
,
563 KRB5_KPASSWD_HARDERROR
,
564 talloc_asprintf(mem_ctx
,
565 "gensec_update failed: %s",
566 nt_errstr(nt_status
)),
572 talloc_free(tmp_ctx
);
576 /* Extract the data from the KRB-PRIV half of the message */
577 nt_status
= gensec_unwrap(gensec_security
, tmp_ctx
, &krb_priv_req
, &kpasswd_req
);
578 if (!NT_STATUS_IS_OK(nt_status
)) {
579 ret
= kpasswdd_make_unauth_error_reply(kdc
, mem_ctx
,
580 KRB5_KPASSWD_HARDERROR
,
581 talloc_asprintf(mem_ctx
,
582 "gensec_unwrap failed: %s",
583 nt_errstr(nt_status
)),
589 talloc_free(tmp_ctx
);
593 /* Figure out something to do with it (probably changing a password...) */
594 ret
= kpasswd_process_request(kdc
, tmp_ctx
,
597 &kpasswd_req
, &kpasswd_rep
);
603 /* And wrap up the reply: This ensures that the error message
604 * or success can be verified by the client */
605 nt_status
= gensec_wrap(gensec_security
, tmp_ctx
,
606 &kpasswd_rep
, &krb_priv_rep
);
607 if (!NT_STATUS_IS_OK(nt_status
)) {
608 ret
= kpasswdd_make_unauth_error_reply(kdc
, mem_ctx
,
609 KRB5_KPASSWD_HARDERROR
,
610 talloc_asprintf(mem_ctx
,
611 "gensec_wrap failed: %s",
612 nt_errstr(nt_status
)),
618 talloc_free(tmp_ctx
);
623 *reply
= data_blob_talloc(mem_ctx
, NULL
, krb_priv_rep
.length
+ ap_rep
.length
+ header_len
);
628 RSSVAL(reply
->data
, 0, reply
->length
);
629 RSSVAL(reply
->data
, 2, 1); /* This is a version 1 reply, MS change/set or otherwise */
630 RSSVAL(reply
->data
, 4, ap_rep
.length
);
631 memcpy(reply
->data
+ header_len
,
634 memcpy(reply
->data
+ header_len
+ ap_rep
.length
,
636 krb_priv_rep
.length
);
638 talloc_free(tmp_ctx
);