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 /* Return true if there is a valid error packet formed in the error_blob */
47 static bool kpasswdd_make_error_reply(struct kdc_server
*kdc
,
50 const char *error_string
,
51 DATA_BLOB
*error_blob
)
53 char *error_string_utf8
;
56 DEBUG(result_code
? 3 : 10, ("kpasswdd: %s\n", error_string
));
58 if (!push_utf8_talloc(mem_ctx
, &error_string_utf8
, error_string
, &len
)) {
62 *error_blob
= data_blob_talloc(mem_ctx
, NULL
, 2 + len
+ 1);
63 if (!error_blob
->data
) {
66 RSSVAL(error_blob
->data
, 0, result_code
);
67 memcpy(error_blob
->data
+ 2, error_string_utf8
, len
+ 1);
71 /* Return true if there is a valid error packet formed in the error_blob */
72 static bool kpasswdd_make_unauth_error_reply(struct kdc_server
*kdc
,
75 const char *error_string
,
76 DATA_BLOB
*error_blob
)
80 DATA_BLOB error_bytes
;
81 krb5_data k5_error_bytes
, k5_error_blob
;
82 ret
= kpasswdd_make_error_reply(kdc
, mem_ctx
, result_code
, error_string
,
87 k5_error_bytes
.data
= error_bytes
.data
;
88 k5_error_bytes
.length
= error_bytes
.length
;
89 kret
= krb5_mk_error(kdc
->smb_krb5_context
->krb5_context
,
90 result_code
, NULL
, &k5_error_bytes
,
91 NULL
, NULL
, NULL
, NULL
, &k5_error_blob
);
95 *error_blob
= data_blob_talloc(mem_ctx
, k5_error_blob
.data
, k5_error_blob
.length
);
96 krb5_data_free(&k5_error_blob
);
97 if (!error_blob
->data
) {
103 static bool kpasswd_make_pwchange_reply(struct kdc_server
*kdc
,
106 enum samPwdChangeReason reject_reason
,
107 struct samr_DomInfo1
*dominfo
,
108 DATA_BLOB
*error_blob
)
110 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
111 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
112 KRB5_KPASSWD_ACCESSDENIED
,
113 "No such user when changing password",
116 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
117 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
118 KRB5_KPASSWD_ACCESSDENIED
,
119 "Not permitted to change password",
122 if (dominfo
&& NT_STATUS_EQUAL(status
, NT_STATUS_PASSWORD_RESTRICTION
)) {
123 const char *reject_string
;
124 switch (reject_reason
) {
125 case SAM_PWD_CHANGE_PASSWORD_TOO_SHORT
:
126 reject_string
= talloc_asprintf(mem_ctx
, "Password too short, password must be at least %d characters long",
127 dominfo
->min_password_length
);
129 case SAM_PWD_CHANGE_NOT_COMPLEX
:
130 reject_string
= "Password does not meet complexity requirements";
132 case SAM_PWD_CHANGE_PWD_IN_HISTORY
:
133 reject_string
= "Password is already in password history";
136 reject_string
= talloc_asprintf(mem_ctx
, "Password must be at least %d characters long, and cannot match any of your %d previous passwords",
137 dominfo
->min_password_length
, dominfo
->password_history_length
);
140 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
141 KRB5_KPASSWD_SOFTERROR
,
145 if (!NT_STATUS_IS_OK(status
)) {
146 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
147 KRB5_KPASSWD_HARDERROR
,
148 talloc_asprintf(mem_ctx
, "failed to set password: %s", nt_errstr(status
)),
152 return kpasswdd_make_error_reply(kdc
, mem_ctx
, KRB5_KPASSWD_SUCCESS
,
158 A user password change
160 Return true if there is a valid error packet (or sucess) formed in
163 static bool kpasswdd_change_password(struct kdc_server
*kdc
,
165 struct auth_session_info
*session_info
,
166 const DATA_BLOB
*password
,
170 enum samPwdChangeReason reject_reason
;
171 struct samr_DomInfo1
*dominfo
;
172 struct ldb_context
*samdb
;
174 samdb
= samdb_connect(mem_ctx
, kdc
->task
->event_ctx
, kdc
->task
->lp_ctx
, system_session(kdc
->task
->lp_ctx
));
176 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
177 KRB5_KPASSWD_HARDERROR
,
178 "Failed to open samdb",
182 DEBUG(3, ("Changing password of %s\\%s (%s)\n",
183 session_info
->server_info
->domain_name
,
184 session_info
->server_info
->account_name
,
185 dom_sid_string(mem_ctx
, session_info
->security_token
->user_sid
)));
187 /* User password change */
188 status
= samdb_set_password_sid(samdb
, mem_ctx
,
189 session_info
->security_token
->user_sid
,
190 password
, NULL
, NULL
,
191 true, /* this is a user password change */
194 return kpasswd_make_pwchange_reply(kdc
, mem_ctx
,
202 static bool kpasswd_process_request(struct kdc_server
*kdc
,
204 struct gensec_security
*gensec_security
,
209 struct auth_session_info
*session_info
;
212 if (!NT_STATUS_IS_OK(gensec_session_info(gensec_security
,
214 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
215 KRB5_KPASSWD_HARDERROR
,
216 "gensec_session_info failed!",
221 case KRB5_KPASSWD_VERS_CHANGEPW
:
224 if (!convert_string_talloc_convenience(mem_ctx
, lp_iconv_convenience(kdc
->task
->lp_ctx
),
226 (const char *)input
->data
,
228 (void **)&password
.data
, &pw_len
, false)) {
231 password
.length
= pw_len
;
233 return kpasswdd_change_password(kdc
, mem_ctx
, session_info
,
237 case KRB5_KPASSWD_VERS_SETPW
:
240 enum samPwdChangeReason reject_reason
= SAM_PWD_CHANGE_NO_ERROR
;
241 struct samr_DomInfo1
*dominfo
= NULL
;
242 struct ldb_context
*samdb
;
243 struct ldb_message
*msg
;
244 krb5_context context
= kdc
->smb_krb5_context
->krb5_context
;
246 ChangePasswdDataMS chpw
;
249 krb5_principal principal
;
250 char *set_password_on_princ
;
251 struct ldb_dn
*set_password_on_dn
;
256 msg
= ldb_msg_new(mem_ctx
);
261 ret
= decode_ChangePasswdDataMS(input
->data
, input
->length
,
264 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
265 KRB5_KPASSWD_MALFORMED
,
266 "failed to decode password change structure",
270 if (!convert_string_talloc_convenience(mem_ctx
, lp_iconv_convenience(kdc
->task
->lp_ctx
),
272 (const char *)chpw
.newpasswd
.data
,
273 chpw
.newpasswd
.length
,
274 (void **)&password
.data
, &pw_len
, false)) {
275 free_ChangePasswdDataMS(&chpw
);
279 password
.length
= pw_len
;
281 if ((chpw
.targname
&& !chpw
.targrealm
)
282 || (!chpw
.targname
&& chpw
.targrealm
)) {
283 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
284 KRB5_KPASSWD_MALFORMED
,
285 "Realm and principal must be both present, or neither present",
288 if (chpw
.targname
&& chpw
.targrealm
) {
289 #ifdef SAMBA4_INTERNAL_HEIMDAL
290 if (_krb5_principalname2krb5_principal(kdc
->smb_krb5_context
->krb5_context
,
291 &principal
, *chpw
.targname
,
292 *chpw
.targrealm
) != 0) {
293 free_ChangePasswdDataMS(&chpw
);
294 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
295 KRB5_KPASSWD_MALFORMED
,
296 "failed to extract principal to set",
300 #else /* SAMBA4_INTERNAL_HEIMDAL */
301 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
302 KRB5_KPASSWD_BAD_VERSION
,
303 "Operation Not Implemented",
305 #endif /* SAMBA4_INTERNAL_HEIMDAL */
307 free_ChangePasswdDataMS(&chpw
);
308 return kpasswdd_change_password(kdc
, mem_ctx
, session_info
,
311 free_ChangePasswdDataMS(&chpw
);
313 if (krb5_unparse_name(context
, principal
, &set_password_on_princ
) != 0) {
314 krb5_free_principal(context
, principal
);
315 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
316 KRB5_KPASSWD_MALFORMED
,
317 "krb5_unparse_name failed!",
321 krb5_free_principal(context
, principal
);
323 samdb
= samdb_connect(mem_ctx
, kdc
->task
->event_ctx
, kdc
->task
->lp_ctx
, session_info
);
325 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
326 KRB5_KPASSWD_HARDERROR
,
327 "Unable to open database!",
331 DEBUG(3, ("%s\\%s (%s) is changing password of %s\n",
332 session_info
->server_info
->domain_name
,
333 session_info
->server_info
->account_name
,
334 dom_sid_string(mem_ctx
, session_info
->security_token
->user_sid
),
335 set_password_on_princ
));
336 ret
= ldb_transaction_start(samdb
);
338 status
= NT_STATUS_TRANSACTION_ABORTED
;
339 return kpasswd_make_pwchange_reply(kdc
, mem_ctx
,
341 SAM_PWD_CHANGE_NO_ERROR
,
346 status
= crack_user_principal_name(samdb
, mem_ctx
,
347 set_password_on_princ
,
348 &set_password_on_dn
, NULL
);
349 free(set_password_on_princ
);
350 if (!NT_STATUS_IS_OK(status
)) {
351 ldb_transaction_cancel(samdb
);
352 return kpasswd_make_pwchange_reply(kdc
, mem_ctx
,
354 SAM_PWD_CHANGE_NO_ERROR
,
359 msg
= ldb_msg_new(mem_ctx
);
361 ldb_transaction_cancel(samdb
);
362 status
= NT_STATUS_NO_MEMORY
;
364 msg
->dn
= ldb_dn_copy(msg
, set_password_on_dn
);
366 status
= NT_STATUS_NO_MEMORY
;
370 if (NT_STATUS_IS_OK(status
)) {
371 /* Admin password set */
372 status
= samdb_set_password(samdb
, mem_ctx
,
373 set_password_on_dn
, NULL
,
374 msg
, &password
, NULL
, NULL
,
375 false, /* this is not a user password change */
376 &reject_reason
, &dominfo
);
379 if (NT_STATUS_IS_OK(status
)) {
380 /* modify the samdb record */
381 ret
= samdb_replace(samdb
, mem_ctx
, msg
);
383 DEBUG(2,("Failed to modify record to set password on %s: %s\n",
384 ldb_dn_get_linearized(msg
->dn
),
385 ldb_errstring(samdb
)));
386 status
= NT_STATUS_ACCESS_DENIED
;
389 if (NT_STATUS_IS_OK(status
)) {
390 ret
= ldb_transaction_commit(samdb
);
392 DEBUG(1,("Failed to commit transaction to set password on %s: %s\n",
393 ldb_dn_get_linearized(msg
->dn
),
394 ldb_errstring(samdb
)));
395 status
= NT_STATUS_TRANSACTION_ABORTED
;
398 ldb_transaction_cancel(samdb
);
400 return kpasswd_make_pwchange_reply(kdc
, mem_ctx
,
407 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
408 KRB5_KPASSWD_BAD_VERSION
,
409 talloc_asprintf(mem_ctx
,
410 "Protocol version %u not supported",
417 bool kpasswdd_process(struct kdc_server
*kdc
,
421 struct socket_address
*peer_addr
,
422 struct socket_address
*my_addr
,
426 const uint16_t header_len
= 6;
429 uint16_t krb_priv_len
;
432 DATA_BLOB ap_req
, krb_priv_req
;
433 DATA_BLOB krb_priv_rep
= data_blob(NULL
, 0);
434 DATA_BLOB ap_rep
= data_blob(NULL
, 0);
435 DATA_BLOB kpasswd_req
, kpasswd_rep
;
436 struct cli_credentials
*server_credentials
;
437 struct gensec_security
*gensec_security
;
438 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
446 /* Be parinoid. We need to ensure we don't just let the
447 * caller lead us into a buffer overflow */
448 if (input
->length
<= header_len
) {
449 talloc_free(tmp_ctx
);
453 len
= RSVAL(input
->data
, 0);
454 if (input
->length
!= len
) {
455 talloc_free(tmp_ctx
);
459 /* There are two different versions of this protocol so far,
460 * plus others in the standards pipe. Fortunetly they all
461 * take a very similar framing */
462 version
= RSVAL(input
->data
, 2);
463 ap_req_len
= RSVAL(input
->data
, 4);
464 if ((ap_req_len
>= len
) || (ap_req_len
+ header_len
) >= len
) {
465 talloc_free(tmp_ctx
);
469 krb_priv_len
= len
- ap_req_len
;
470 ap_req
= data_blob_const(&input
->data
[header_len
], ap_req_len
);
471 krb_priv_req
= data_blob_const(&input
->data
[header_len
+ ap_req_len
], krb_priv_len
);
473 server_credentials
= cli_credentials_init(tmp_ctx
);
474 if (!server_credentials
) {
475 DEBUG(1, ("Failed to init server credentials\n"));
479 /* We want the credentials subsystem to use the krb5 context
480 * we already have, rather than a new context */
481 cli_credentials_set_krb5_context(server_credentials
, kdc
->smb_krb5_context
);
482 cli_credentials_set_conf(server_credentials
, kdc
->task
->lp_ctx
);
484 keytab_name
= talloc_asprintf(server_credentials
, "HDB:samba4&%p", kdc
->hdb_samba4_context
);
486 cli_credentials_set_username(server_credentials
, "kadmin/changepw", CRED_SPECIFIED
);
487 ret
= cli_credentials_set_keytab_name(server_credentials
, kdc
->task
->event_ctx
, kdc
->task
->lp_ctx
, keytab_name
, CRED_SPECIFIED
);
489 ret
= kpasswdd_make_unauth_error_reply(kdc
, mem_ctx
,
490 KRB5_KPASSWD_HARDERROR
,
491 talloc_asprintf(mem_ctx
,
492 "Failed to obtain server credentials for kadmin/changepw: %s\n",
493 nt_errstr(nt_status
)),
499 talloc_free(tmp_ctx
);
503 /* We don't strictly need to call this wrapper, and could call
504 * gensec_server_start directly, as we have no need for NTLM
505 * and we have a PAC, but this ensures that the wrapper can be
506 * safely extended for other helpful things in future */
507 nt_status
= samba_server_gensec_start(tmp_ctx
, kdc
->task
->event_ctx
,
513 if (!NT_STATUS_IS_OK(nt_status
)) {
514 talloc_free(tmp_ctx
);
518 /* The kerberos PRIV packets include these addresses. MIT
519 * clients check that they are present */
521 /* Skip this part for now, it breaks with a NetAPP filer and
522 * in any case where the client address is behind NAT. If
523 * older MIT clients need this, we might have to insert more
526 nt_status
= gensec_set_peer_addr(gensec_security
, peer_addr
);
527 if (!NT_STATUS_IS_OK(nt_status
)) {
528 talloc_free(tmp_ctx
);
533 nt_status
= gensec_set_my_addr(gensec_security
, my_addr
);
534 if (!NT_STATUS_IS_OK(nt_status
)) {
535 talloc_free(tmp_ctx
);
539 /* We want the GENSEC wrap calls to generate PRIV tokens */
540 gensec_want_feature(gensec_security
, GENSEC_FEATURE_SEAL
);
542 nt_status
= gensec_start_mech_by_name(gensec_security
, "krb5");
543 if (!NT_STATUS_IS_OK(nt_status
)) {
544 talloc_free(tmp_ctx
);
548 /* Accept the AP-REQ and generate teh AP-REP we need for the reply */
549 nt_status
= gensec_update(gensec_security
, tmp_ctx
, ap_req
, &ap_rep
);
550 if (!NT_STATUS_IS_OK(nt_status
) && !NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
552 ret
= kpasswdd_make_unauth_error_reply(kdc
, mem_ctx
,
553 KRB5_KPASSWD_HARDERROR
,
554 talloc_asprintf(mem_ctx
,
555 "gensec_update failed: %s",
556 nt_errstr(nt_status
)),
562 talloc_free(tmp_ctx
);
566 /* Extract the data from the KRB-PRIV half of the message */
567 nt_status
= gensec_unwrap(gensec_security
, tmp_ctx
, &krb_priv_req
, &kpasswd_req
);
568 if (!NT_STATUS_IS_OK(nt_status
)) {
569 ret
= kpasswdd_make_unauth_error_reply(kdc
, mem_ctx
,
570 KRB5_KPASSWD_HARDERROR
,
571 talloc_asprintf(mem_ctx
,
572 "gensec_unwrap failed: %s",
573 nt_errstr(nt_status
)),
579 talloc_free(tmp_ctx
);
583 /* Figure out something to do with it (probably changing a password...) */
584 ret
= kpasswd_process_request(kdc
, tmp_ctx
,
587 &kpasswd_req
, &kpasswd_rep
);
593 /* And wrap up the reply: This ensures that the error message
594 * or success can be verified by the client */
595 nt_status
= gensec_wrap(gensec_security
, tmp_ctx
,
596 &kpasswd_rep
, &krb_priv_rep
);
597 if (!NT_STATUS_IS_OK(nt_status
)) {
598 ret
= kpasswdd_make_unauth_error_reply(kdc
, mem_ctx
,
599 KRB5_KPASSWD_HARDERROR
,
600 talloc_asprintf(mem_ctx
,
601 "gensec_wrap failed: %s",
602 nt_errstr(nt_status
)),
608 talloc_free(tmp_ctx
);
613 *reply
= data_blob_talloc(mem_ctx
, NULL
, krb_priv_rep
.length
+ ap_rep
.length
+ header_len
);
618 RSSVAL(reply
->data
, 0, reply
->length
);
619 RSSVAL(reply
->data
, 2, 1); /* This is a version 1 reply, MS change/set or otherwise */
620 RSSVAL(reply
->data
, 4, ap_rep
.length
);
621 memcpy(reply
->data
+ header_len
,
624 memcpy(reply
->data
+ header_len
+ ap_rep
.length
,
626 krb_priv_rep
.length
);
628 talloc_free(tmp_ctx
);