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 "lib/tsocket/tsocket.h"
28 #include "system/network.h"
29 #include "../lib/util/dlinklist.h"
30 #include "lib/ldb/include/ldb.h"
31 #include "auth/gensec/gensec.h"
32 #include "auth/credentials/credentials.h"
33 #include "auth/credentials/credentials_krb5.h"
34 #include "auth/auth.h"
35 #include "dsdb/samdb/samdb.h"
36 #include "rpc_server/dcerpc_server.h"
37 #include "rpc_server/samr/proto.h"
38 #include "libcli/security/security.h"
39 #include "param/param.h"
42 /* TODO: remove all SAMBA4_INTERNAL_HEIMDAL stuff from this file */
43 #ifdef SAMBA4_INTERNAL_HEIMDAL
44 #include "heimdal_build/kpasswdd-glue.h"
47 /* Return true if there is a valid error packet formed in the error_blob */
48 static bool kpasswdd_make_error_reply(struct kdc_server
*kdc
,
51 const char *error_string
,
52 DATA_BLOB
*error_blob
)
54 char *error_string_utf8
;
57 DEBUG(result_code
? 3 : 10, ("kpasswdd: %s\n", error_string
));
59 if (!push_utf8_talloc(mem_ctx
, &error_string_utf8
, error_string
, &len
)) {
63 *error_blob
= data_blob_talloc(mem_ctx
, NULL
, 2 + len
+ 1);
64 if (!error_blob
->data
) {
67 RSSVAL(error_blob
->data
, 0, result_code
);
68 memcpy(error_blob
->data
+ 2, error_string_utf8
, len
+ 1);
72 /* Return true if there is a valid error packet formed in the error_blob */
73 static bool kpasswdd_make_unauth_error_reply(struct kdc_server
*kdc
,
76 const char *error_string
,
77 DATA_BLOB
*error_blob
)
81 DATA_BLOB error_bytes
;
82 krb5_data k5_error_bytes
, k5_error_blob
;
83 ret
= kpasswdd_make_error_reply(kdc
, mem_ctx
, result_code
, error_string
,
88 k5_error_bytes
.data
= error_bytes
.data
;
89 k5_error_bytes
.length
= error_bytes
.length
;
90 kret
= krb5_mk_error(kdc
->smb_krb5_context
->krb5_context
,
91 result_code
, NULL
, &k5_error_bytes
,
92 NULL
, NULL
, NULL
, NULL
, &k5_error_blob
);
96 *error_blob
= data_blob_talloc(mem_ctx
, k5_error_blob
.data
, k5_error_blob
.length
);
97 krb5_data_free(&k5_error_blob
);
98 if (!error_blob
->data
) {
104 static bool kpasswd_make_pwchange_reply(struct kdc_server
*kdc
,
107 enum samPwdChangeReason reject_reason
,
108 struct samr_DomInfo1
*dominfo
,
109 DATA_BLOB
*error_blob
)
111 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
112 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
113 KRB5_KPASSWD_ACCESSDENIED
,
114 "No such user when changing password",
117 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
118 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
119 KRB5_KPASSWD_ACCESSDENIED
,
120 "Not permitted to change password",
123 if (dominfo
&& NT_STATUS_EQUAL(status
, NT_STATUS_PASSWORD_RESTRICTION
)) {
124 const char *reject_string
;
125 switch (reject_reason
) {
126 case SAM_PWD_CHANGE_PASSWORD_TOO_SHORT
:
127 reject_string
= talloc_asprintf(mem_ctx
, "Password too short, password must be at least %d characters long",
128 dominfo
->min_password_length
);
130 case SAM_PWD_CHANGE_NOT_COMPLEX
:
131 reject_string
= "Password does not meet complexity requirements";
133 case SAM_PWD_CHANGE_PWD_IN_HISTORY
:
134 reject_string
= "Password is already in password history";
137 reject_string
= talloc_asprintf(mem_ctx
, "Password must be at least %d characters long, and cannot match any of your %d previous passwords",
138 dominfo
->min_password_length
, dominfo
->password_history_length
);
141 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
142 KRB5_KPASSWD_SOFTERROR
,
146 if (!NT_STATUS_IS_OK(status
)) {
147 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
148 KRB5_KPASSWD_HARDERROR
,
149 talloc_asprintf(mem_ctx
, "failed to set password: %s", nt_errstr(status
)),
153 return kpasswdd_make_error_reply(kdc
, mem_ctx
, KRB5_KPASSWD_SUCCESS
,
159 A user password change
161 Return true if there is a valid error packet (or success) formed in
164 static bool kpasswdd_change_password(struct kdc_server
*kdc
,
166 struct auth_session_info
*session_info
,
167 const DATA_BLOB
*password
,
171 enum samPwdChangeReason reject_reason
;
172 struct samr_DomInfo1
*dominfo
;
173 struct ldb_context
*samdb
;
175 samdb
= samdb_connect(mem_ctx
, kdc
->task
->event_ctx
, kdc
->task
->lp_ctx
, system_session(kdc
->task
->lp_ctx
));
177 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
178 KRB5_KPASSWD_HARDERROR
,
179 "Failed to open samdb",
183 DEBUG(3, ("Changing password of %s\\%s (%s)\n",
184 session_info
->server_info
->domain_name
,
185 session_info
->server_info
->account_name
,
186 dom_sid_string(mem_ctx
, session_info
->security_token
->user_sid
)));
188 /* User password change */
189 status
= samdb_set_password_sid(samdb
, mem_ctx
,
190 session_info
->security_token
->user_sid
,
191 password
, NULL
, NULL
,
192 true, /* this is a user password change */
195 return kpasswd_make_pwchange_reply(kdc
, mem_ctx
,
203 static bool kpasswd_process_request(struct kdc_server
*kdc
,
205 struct gensec_security
*gensec_security
,
210 struct auth_session_info
*session_info
;
213 if (!NT_STATUS_IS_OK(gensec_session_info(gensec_security
,
215 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
216 KRB5_KPASSWD_HARDERROR
,
217 "gensec_session_info failed!",
222 case KRB5_KPASSWD_VERS_CHANGEPW
:
225 if (!convert_string_talloc_convenience(mem_ctx
, lpcfg_iconv_convenience(kdc
->task
->lp_ctx
),
227 (const char *)input
->data
,
229 (void **)&password
.data
, &pw_len
, false)) {
232 password
.length
= pw_len
;
234 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 krb5_context context
= kdc
->smb_krb5_context
->krb5_context
;
245 ChangePasswdDataMS chpw
;
248 krb5_principal principal
;
249 char *set_password_on_princ
;
250 struct ldb_dn
*set_password_on_dn
;
251 bool service_principal_name
= false;
256 ret
= decode_ChangePasswdDataMS(input
->data
, input
->length
,
259 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
260 KRB5_KPASSWD_MALFORMED
,
261 "failed to decode password change structure",
265 if (!convert_string_talloc_convenience(mem_ctx
, lpcfg_iconv_convenience(kdc
->task
->lp_ctx
),
267 (const char *)chpw
.newpasswd
.data
,
268 chpw
.newpasswd
.length
,
269 (void **)&password
.data
, &pw_len
, false)) {
270 free_ChangePasswdDataMS(&chpw
);
274 password
.length
= pw_len
;
276 if ((chpw
.targname
&& !chpw
.targrealm
)
277 || (!chpw
.targname
&& chpw
.targrealm
)) {
278 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
279 KRB5_KPASSWD_MALFORMED
,
280 "Realm and principal must be both present, or neither present",
283 if (chpw
.targname
&& chpw
.targrealm
) {
284 #ifdef SAMBA4_INTERNAL_HEIMDAL
285 if (_krb5_principalname2krb5_principal(kdc
->smb_krb5_context
->krb5_context
,
286 &principal
, *chpw
.targname
,
287 *chpw
.targrealm
) != 0) {
288 free_ChangePasswdDataMS(&chpw
);
289 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
290 KRB5_KPASSWD_MALFORMED
,
291 "failed to extract principal to set",
295 #else /* SAMBA4_INTERNAL_HEIMDAL */
296 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
297 KRB5_KPASSWD_BAD_VERSION
,
298 "Operation Not Implemented",
300 #endif /* SAMBA4_INTERNAL_HEIMDAL */
302 free_ChangePasswdDataMS(&chpw
);
303 return kpasswdd_change_password(kdc
, mem_ctx
, session_info
,
306 free_ChangePasswdDataMS(&chpw
);
308 if (principal
->name
.name_string
.len
>= 2) {
309 service_principal_name
= true;
311 /* We use this, rather than 'no realm' flag,
312 * as we don't want to accept a password
313 * change on a principal from another realm */
315 if (krb5_unparse_name_short(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 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
);
347 if (ret
!= LDB_SUCCESS
) {
348 status
= NT_STATUS_TRANSACTION_ABORTED
;
349 return kpasswd_make_pwchange_reply(kdc
, mem_ctx
,
351 SAM_PWD_CHANGE_NO_ERROR
,
356 if (service_principal_name
) {
357 status
= crack_service_principal_name(samdb
, mem_ctx
,
358 set_password_on_princ
,
359 &set_password_on_dn
, NULL
);
361 status
= crack_user_principal_name(samdb
, mem_ctx
,
362 set_password_on_princ
,
363 &set_password_on_dn
, NULL
);
365 free(set_password_on_princ
);
366 if (!NT_STATUS_IS_OK(status
)) {
367 ldb_transaction_cancel(samdb
);
368 return kpasswd_make_pwchange_reply(kdc
, mem_ctx
,
370 SAM_PWD_CHANGE_NO_ERROR
,
375 if (NT_STATUS_IS_OK(status
)) {
376 /* Admin password set */
377 status
= samdb_set_password(samdb
, mem_ctx
,
378 set_password_on_dn
, NULL
,
379 &password
, NULL
, NULL
,
380 false, /* this is not a user password change */
381 &reject_reason
, &dominfo
);
384 if (NT_STATUS_IS_OK(status
)) {
385 ret
= ldb_transaction_commit(samdb
);
386 if (ret
!= LDB_SUCCESS
) {
387 DEBUG(1,("Failed to commit transaction to set password on %s: %s\n",
388 ldb_dn_get_linearized(set_password_on_dn
),
389 ldb_errstring(samdb
)));
390 status
= NT_STATUS_TRANSACTION_ABORTED
;
393 ldb_transaction_cancel(samdb
);
395 return kpasswd_make_pwchange_reply(kdc
, mem_ctx
,
402 return kpasswdd_make_error_reply(kdc
, mem_ctx
,
403 KRB5_KPASSWD_BAD_VERSION
,
404 talloc_asprintf(mem_ctx
,
405 "Protocol version %u not supported",
411 bool kpasswdd_process(struct kdc_server
*kdc
,
415 struct tsocket_address
*peer_addr
,
416 struct tsocket_address
*my_addr
,
420 const uint16_t header_len
= 6;
423 uint16_t krb_priv_len
;
426 DATA_BLOB ap_req
, krb_priv_req
;
427 DATA_BLOB krb_priv_rep
= data_blob(NULL
, 0);
428 DATA_BLOB ap_rep
= data_blob(NULL
, 0);
429 DATA_BLOB kpasswd_req
, kpasswd_rep
;
430 struct cli_credentials
*server_credentials
;
431 struct gensec_security
*gensec_security
;
432 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
440 /* Be parinoid. We need to ensure we don't just let the
441 * caller lead us into a buffer overflow */
442 if (input
->length
<= header_len
) {
443 talloc_free(tmp_ctx
);
447 len
= RSVAL(input
->data
, 0);
448 if (input
->length
!= len
) {
449 talloc_free(tmp_ctx
);
453 /* There are two different versions of this protocol so far,
454 * plus others in the standards pipe. Fortunetly they all
455 * take a very similar framing */
456 version
= RSVAL(input
->data
, 2);
457 ap_req_len
= RSVAL(input
->data
, 4);
458 if ((ap_req_len
>= len
) || (ap_req_len
+ header_len
) >= len
) {
459 talloc_free(tmp_ctx
);
463 krb_priv_len
= len
- ap_req_len
;
464 ap_req
= data_blob_const(&input
->data
[header_len
], ap_req_len
);
465 krb_priv_req
= data_blob_const(&input
->data
[header_len
+ ap_req_len
], krb_priv_len
);
467 server_credentials
= cli_credentials_init(tmp_ctx
);
468 if (!server_credentials
) {
469 DEBUG(1, ("Failed to init server credentials\n"));
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
->event_ctx
, 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: %s\n",
487 nt_errstr(nt_status
)),
493 talloc_free(tmp_ctx
);
497 /* We don't strictly need to call this wrapper, and could call
498 * gensec_server_start directly, as we have no need for NTLM
499 * and we have a PAC, but this ensures that the wrapper can be
500 * safely extended for other helpful things in future */
501 nt_status
= samba_server_gensec_start(tmp_ctx
, kdc
->task
->event_ctx
,
507 if (!NT_STATUS_IS_OK(nt_status
)) {
508 talloc_free(tmp_ctx
);
512 /* The kerberos PRIV packets include these addresses. MIT
513 * clients check that they are present */
515 /* Skip this part for now, it breaks with a NetAPP filer and
516 * in any case where the client address is behind NAT. If
517 * older MIT clients need this, we might have to insert more
520 nt_status
= gensec_set_local_address(gensec_security
, peer_addr
);
521 if (!NT_STATUS_IS_OK(nt_status
)) {
522 talloc_free(tmp_ctx
);
527 nt_status
= gensec_set_local_address(gensec_security
, my_addr
);
528 if (!NT_STATUS_IS_OK(nt_status
)) {
529 talloc_free(tmp_ctx
);
533 /* We want the GENSEC wrap calls to generate PRIV tokens */
534 gensec_want_feature(gensec_security
, GENSEC_FEATURE_SEAL
);
536 nt_status
= gensec_start_mech_by_name(gensec_security
, "krb5");
537 if (!NT_STATUS_IS_OK(nt_status
)) {
538 talloc_free(tmp_ctx
);
542 /* Accept the AP-REQ and generate teh AP-REP we need for the reply */
543 nt_status
= gensec_update(gensec_security
, tmp_ctx
, ap_req
, &ap_rep
);
544 if (!NT_STATUS_IS_OK(nt_status
) && !NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
546 ret
= kpasswdd_make_unauth_error_reply(kdc
, mem_ctx
,
547 KRB5_KPASSWD_HARDERROR
,
548 talloc_asprintf(mem_ctx
,
549 "gensec_update failed: %s",
550 nt_errstr(nt_status
)),
556 talloc_free(tmp_ctx
);
560 /* Extract the data from the KRB-PRIV half of the message */
561 nt_status
= gensec_unwrap(gensec_security
, tmp_ctx
, &krb_priv_req
, &kpasswd_req
);
562 if (!NT_STATUS_IS_OK(nt_status
)) {
563 ret
= kpasswdd_make_unauth_error_reply(kdc
, mem_ctx
,
564 KRB5_KPASSWD_HARDERROR
,
565 talloc_asprintf(mem_ctx
,
566 "gensec_unwrap failed: %s",
567 nt_errstr(nt_status
)),
573 talloc_free(tmp_ctx
);
577 /* Figure out something to do with it (probably changing a password...) */
578 ret
= kpasswd_process_request(kdc
, tmp_ctx
,
581 &kpasswd_req
, &kpasswd_rep
);
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
);
607 *reply
= data_blob_talloc(mem_ctx
, NULL
, krb_priv_rep
.length
+ ap_rep
.length
+ header_len
);
612 RSSVAL(reply
->data
, 0, reply
->length
);
613 RSSVAL(reply
->data
, 2, 1); /* This is a version 1 reply, MS change/set or otherwise */
614 RSSVAL(reply
->data
, 4, ap_rep
.length
);
615 memcpy(reply
->data
+ header_len
,
618 memcpy(reply
->data
+ header_len
+ ap_rep
.length
,
620 krb_priv_rep
.length
);
622 talloc_free(tmp_ctx
);