2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2004
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "libnet/libnet.h"
23 #include "libcli/auth/libcli_auth.h"
24 #include "librpc/gen_ndr/ndr_samr_c.h"
25 #include "source4/librpc/rpc/dcerpc.h"
26 #include "auth/credentials/credentials.h"
27 #include "libcli/smb/smb_constants.h"
28 #include "librpc/rpc/dcerpc_samr.h"
29 #include "source3/rpc_client/init_samr.h"
30 #include "lib/param/loadparm.h"
31 #include "lib/param/param.h"
33 #include "lib/crypto/gnutls_helpers.h"
34 #include <gnutls/gnutls.h>
35 #include <gnutls/crypto.h>
37 static NTSTATUS
libnet_ChangePassword_samr_aes(TALLOC_CTX
*mem_ctx
,
38 struct dcerpc_binding_handle
*h
,
39 struct lsa_String
*server
,
40 struct lsa_String
*account
,
41 const char *old_password
,
42 const char *new_password
,
43 const char **error_string
)
45 struct samr_ChangePasswordUser4 r
;
46 uint8_t old_nt_key_data
[16] = {0};
47 gnutls_datum_t old_nt_key
= {
48 .data
= old_nt_key_data
,
49 .size
= sizeof(old_nt_key_data
),
51 uint8_t cek_data
[16] = {0};
54 .length
= sizeof(cek_data
),
56 struct samr_EncryptedPasswordAES pwd_buf
= {
61 .length
= sizeof(pwd_buf
.salt
),
63 gnutls_datum_t salt_datum
= {
65 .size
= sizeof(pwd_buf
.salt
),
67 uint64_t pbkdf2_iterations
= generate_random_u64_range(5000, 1000000);
71 E_md4hash(old_password
, old_nt_key_data
);
73 generate_nonce_buffer(salt
.data
, salt
.length
);
75 rc
= gnutls_pbkdf2(GNUTLS_MAC_SHA512
,
81 BURN_DATA(old_nt_key_data
);
83 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
84 if (!NT_STATUS_IS_OK(status
)) {
89 status
= init_samr_CryptPasswordAES(mem_ctx
,
94 data_blob_clear(&cek
);
95 if (!NT_STATUS_IS_OK(status
)) {
99 pwd_buf
.PBKDF2Iterations
= pbkdf2_iterations
;
101 r
.in
.server
= server
;
102 r
.in
.account
= account
;
103 r
.in
.password
= &pwd_buf
;
105 status
= dcerpc_samr_ChangePasswordUser4_r(h
, mem_ctx
, &r
);
106 if (!NT_STATUS_IS_OK(status
)) {
109 if (!NT_STATUS_IS_OK(r
.out
.result
)) {
110 status
= r
.out
.result
;
111 *error_string
= talloc_asprintf(mem_ctx
,
112 "samr_ChangePasswordUser4 for "
113 "'%s\\%s' failed: %s",
126 static NTSTATUS
libnet_ChangePassword_samr_rc4(TALLOC_CTX
*mem_ctx
,
127 struct dcerpc_binding_handle
*h
,
128 struct lsa_String
*server
,
129 struct lsa_String
*account
,
130 const char *old_password
,
131 const char *new_password
,
132 const char **error_string
)
134 struct samr_OemChangePasswordUser2 oe2
;
135 struct samr_ChangePasswordUser2 pw2
;
136 struct samr_ChangePasswordUser3 pw3
;
137 struct samr_CryptPassword nt_pass
, lm_pass
;
138 uint8_t old_nt_hash
[16], new_nt_hash
[16];
139 uint8_t old_lm_hash
[16], new_lm_hash
[16];
140 struct samr_Password nt_verifier
, lm_verifier
;
141 struct lsa_AsciiString a_server
, a_account
;
142 gnutls_cipher_hd_t cipher_hnd
= NULL
;
143 gnutls_datum_t nt_session_key
= {
145 .size
= sizeof(old_nt_hash
),
147 gnutls_datum_t lm_session_key
= {
149 .size
= sizeof(old_lm_hash
),
151 struct samr_DomInfo1
*dominfo
= NULL
;
152 struct userPwdChangeFailureInformation
*reject
= NULL
;
156 E_md4hash(old_password
, old_nt_hash
);
157 E_md4hash(new_password
, new_nt_hash
);
159 E_deshash(old_password
, old_lm_hash
);
160 E_deshash(new_password
, new_lm_hash
);
162 /* prepare samr_ChangePasswordUser3 */
163 encode_pw_buffer(lm_pass
.data
, new_password
, STR_UNICODE
);
165 rc
= gnutls_cipher_init(&cipher_hnd
,
166 GNUTLS_CIPHER_ARCFOUR_128
,
170 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
174 rc
= gnutls_cipher_encrypt(cipher_hnd
,
177 gnutls_cipher_deinit(cipher_hnd
);
179 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
183 rc
= E_old_pw_hash(new_lm_hash
, old_lm_hash
, lm_verifier
.hash
);
185 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_ACCESS_DISABLED_BY_POLICY_OTHER
);
189 encode_pw_buffer(nt_pass
.data
, new_password
, STR_UNICODE
);
191 rc
= gnutls_cipher_init(&cipher_hnd
,
192 GNUTLS_CIPHER_ARCFOUR_128
,
196 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
200 rc
= gnutls_cipher_encrypt(cipher_hnd
,
203 gnutls_cipher_deinit(cipher_hnd
);
205 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
209 rc
= E_old_pw_hash(new_nt_hash
, old_nt_hash
, nt_verifier
.hash
);
211 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_ACCESS_DISABLED_BY_POLICY_OTHER
);
215 pw3
.in
.server
= server
;
216 pw3
.in
.account
= account
;
217 pw3
.in
.nt_password
= &nt_pass
;
218 pw3
.in
.nt_verifier
= &nt_verifier
;
219 pw3
.in
.lm_change
= 1;
220 pw3
.in
.lm_password
= &lm_pass
;
221 pw3
.in
.lm_verifier
= &lm_verifier
;
222 pw3
.in
.password3
= NULL
;
223 pw3
.out
.dominfo
= &dominfo
;
224 pw3
.out
.reject
= &reject
;
226 /* 2. try samr_ChangePasswordUser3 */
227 status
= dcerpc_samr_ChangePasswordUser3_r(h
, mem_ctx
, &pw3
);
228 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE
)) {
229 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(pw3
.out
.result
)) {
230 status
= pw3
.out
.result
;
232 if (!NT_STATUS_IS_OK(status
)) {
233 *error_string
= talloc_asprintf(
235 "samr_ChangePasswordUser3 failed: %s",
238 talloc_asprintf(mem_ctx
,
239 "samr_ChangePasswordUser3 for "
240 "'%s\\%s' failed: %s",
248 /* prepare samr_ChangePasswordUser2 */
249 encode_pw_buffer(lm_pass
.data
, new_password
, STR_ASCII
| STR_TERMINATE
);
251 rc
= gnutls_cipher_init(&cipher_hnd
,
252 GNUTLS_CIPHER_ARCFOUR_128
,
256 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
260 rc
= gnutls_cipher_encrypt(cipher_hnd
,
263 gnutls_cipher_deinit(cipher_hnd
);
265 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
269 rc
= E_old_pw_hash(new_lm_hash
, old_lm_hash
, lm_verifier
.hash
);
271 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_ACCESS_DISABLED_BY_POLICY_OTHER
);
275 encode_pw_buffer(nt_pass
.data
, new_password
, STR_UNICODE
);
277 rc
= gnutls_cipher_init(&cipher_hnd
,
278 GNUTLS_CIPHER_ARCFOUR_128
,
282 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
285 rc
= gnutls_cipher_encrypt(cipher_hnd
,
288 gnutls_cipher_deinit(cipher_hnd
);
290 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
294 rc
= E_old_pw_hash(new_nt_hash
, old_nt_hash
, nt_verifier
.hash
);
296 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_ACCESS_DISABLED_BY_POLICY_OTHER
);
300 pw2
.in
.server
= server
;
301 pw2
.in
.account
= account
;
302 pw2
.in
.nt_password
= &nt_pass
;
303 pw2
.in
.nt_verifier
= &nt_verifier
;
304 pw2
.in
.lm_change
= 1;
305 pw2
.in
.lm_password
= &lm_pass
;
306 pw2
.in
.lm_verifier
= &lm_verifier
;
308 /* 3. try samr_ChangePasswordUser2 */
309 status
= dcerpc_samr_ChangePasswordUser2_r(h
, mem_ctx
, &pw2
);
310 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE
)) {
311 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(pw2
.out
.result
)) {
312 status
= pw2
.out
.result
;
314 if (!NT_STATUS_IS_OK(status
)) {
316 talloc_asprintf(mem_ctx
,
317 "samr_ChangePasswordUser2 for "
318 "'%s\\%s' failed: %s",
327 /* prepare samr_OemChangePasswordUser2 */
328 a_server
.string
= server
->string
;
329 a_account
.string
= account
->string
;
331 encode_pw_buffer(lm_pass
.data
, new_password
, STR_ASCII
);
333 rc
= gnutls_cipher_init(&cipher_hnd
,
334 GNUTLS_CIPHER_ARCFOUR_128
,
338 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
342 rc
= gnutls_cipher_encrypt(cipher_hnd
,
345 gnutls_cipher_deinit(cipher_hnd
);
347 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
351 rc
= E_old_pw_hash(new_lm_hash
, old_lm_hash
, lm_verifier
.hash
);
353 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_ACCESS_DISABLED_BY_POLICY_OTHER
);
357 oe2
.in
.server
= &a_server
;
358 oe2
.in
.account
= &a_account
;
359 oe2
.in
.password
= &lm_pass
;
360 oe2
.in
.hash
= &lm_verifier
;
362 /* 4. try samr_OemChangePasswordUser2 */
363 status
= dcerpc_samr_OemChangePasswordUser2_r(h
, mem_ctx
, &oe2
);
364 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE
)) {
365 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(oe2
.out
.result
)) {
366 status
= oe2
.out
.result
;
368 if (!NT_STATUS_IS_OK(oe2
.out
.result
)) {
370 talloc_asprintf(mem_ctx
,
371 "samr_OemChangePasswordUser2 "
372 "for '%s\\%s' failed: %s",
380 status
= NT_STATUS_OK
;
386 * do a password change using DCERPC/SAMR calls
387 * 1. connect to the SAMR pipe of users domain PDC (maybe a standalone server or workstation)
388 * 2. try samr_ChangePasswordUser3
389 * 3. try samr_ChangePasswordUser2
390 * 4. try samr_OemChangePasswordUser2
392 static NTSTATUS
libnet_ChangePassword_samr(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, union libnet_ChangePassword
*r
)
395 struct libnet_RpcConnect c
;
396 struct lsa_String server
, account
;
400 /* prepare connect to the SAMR pipe of the users domain PDC */
401 c
.level
= LIBNET_RPC_CONNECT_PDC
;
402 c
.in
.name
= r
->samr
.in
.domain_name
;
403 c
.in
.dcerpc_iface
= &ndr_table_samr
;
404 c
.in
.dcerpc_flags
= DCERPC_ANON_FALLBACK
;
406 /* 1. connect to the SAMR pipe of users domain PDC (maybe a standalone server or workstation) */
407 status
= libnet_RpcConnect(ctx
, mem_ctx
, &c
);
408 if (!NT_STATUS_IS_OK(status
)) {
409 r
->samr
.out
.error_string
= talloc_asprintf(mem_ctx
,
410 "Connection to SAMR pipe of PDC of domain '%s' failed: %s",
411 r
->samr
.in
.domain_name
, nt_errstr(status
));
415 /* prepare password change for account */
416 server
.string
= talloc_asprintf(mem_ctx
, "\\\\%s", dcerpc_server_name(c
.out
.dcerpc_pipe
));
417 account
.string
= r
->samr
.in
.account_name
;
419 status
= libnet_ChangePassword_samr_aes(
421 c
.out
.dcerpc_pipe
->binding_handle
,
424 r
->samr
.in
.oldpassword
,
425 r
->samr
.in
.newpassword
,
426 &(r
->samr
.out
.error_string
));
427 if (NT_STATUS_IS_OK(status
)) {
429 } else if (NT_STATUS_EQUAL(status
,
430 NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE
) ||
431 NT_STATUS_EQUAL(status
, NT_STATUS_NOT_SUPPORTED
) ||
432 NT_STATUS_EQUAL(status
, NT_STATUS_NOT_IMPLEMENTED
)) {
434 * Don't fallback to RC4 based SAMR if weak crypto is not
437 if (lpcfg_weak_crypto(ctx
->lp_ctx
) ==
438 SAMBA_WEAK_CRYPTO_DISALLOWED
) {
442 /* libnet_ChangePassword_samr_aes is implemented and failed */
446 status
= libnet_ChangePassword_samr_rc4(
448 c
.out
.dcerpc_pipe
->binding_handle
,
451 r
->samr
.in
.oldpassword
,
452 r
->samr
.in
.newpassword
,
453 &(r
->samr
.out
.error_string
));
454 if (!NT_STATUS_IS_OK(status
)) {
459 /* close connection */
460 talloc_unlink(ctx
, c
.out
.dcerpc_pipe
);
465 static NTSTATUS
libnet_ChangePassword_generic(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, union libnet_ChangePassword
*r
)
468 union libnet_ChangePassword r2
;
470 r2
.samr
.level
= LIBNET_CHANGE_PASSWORD_SAMR
;
471 r2
.samr
.in
.account_name
= r
->generic
.in
.account_name
;
472 r2
.samr
.in
.domain_name
= r
->generic
.in
.domain_name
;
473 r2
.samr
.in
.oldpassword
= r
->generic
.in
.oldpassword
;
474 r2
.samr
.in
.newpassword
= r
->generic
.in
.newpassword
;
476 status
= libnet_ChangePassword(ctx
, mem_ctx
, &r2
);
478 r
->generic
.out
.error_string
= r2
.samr
.out
.error_string
;
483 NTSTATUS
libnet_ChangePassword(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, union libnet_ChangePassword
*r
)
485 switch (r
->generic
.level
) {
486 case LIBNET_CHANGE_PASSWORD_GENERIC
:
487 return libnet_ChangePassword_generic(ctx
, mem_ctx
, r
);
488 case LIBNET_CHANGE_PASSWORD_SAMR
:
489 return libnet_ChangePassword_samr(ctx
, mem_ctx
, r
);
490 case LIBNET_CHANGE_PASSWORD_KRB5
:
491 return NT_STATUS_NOT_IMPLEMENTED
;
492 case LIBNET_CHANGE_PASSWORD_LDAP
:
493 return NT_STATUS_NOT_IMPLEMENTED
;
494 case LIBNET_CHANGE_PASSWORD_RAP
:
495 return NT_STATUS_NOT_IMPLEMENTED
;
498 return NT_STATUS_INVALID_LEVEL
;
501 static NTSTATUS
libnet_SetPassword_samr_handle_26(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, union libnet_SetPassword
*r
)
503 struct dcerpc_binding_handle
*b
=
504 r
->samr_handle
.in
.dcerpc_pipe
->binding_handle
;
506 struct samr_SetUserInfo2 sui
;
507 union samr_UserInfo u_info
;
508 DATA_BLOB session_key
;
510 if (r
->samr_handle
.in
.info21
) {
511 return NT_STATUS_INVALID_PARAMETER_MIX
;
514 /* prepare samr_SetUserInfo2 level 26 */
516 u_info
.info26
.password_expired
= 0;
518 status
= dcerpc_binding_handle_transport_session_key(b
,
521 if (!NT_STATUS_IS_OK(status
)) {
522 r
->samr_handle
.out
.error_string
= talloc_asprintf(mem_ctx
,
523 "transport_session_key failed: %s",
528 status
= encode_rc4_passwd_buffer(r
->samr_handle
.in
.newpassword
,
530 &u_info
.info26
.password
);
531 data_blob_clear_free(&session_key
);
532 if (!NT_STATUS_IS_OK(status
)) {
533 r
->samr_handle
.out
.error_string
=
534 talloc_asprintf(mem_ctx
,
535 "encode_rc4_passwd_buffer failed: %s",
540 sui
.in
.user_handle
= r
->samr_handle
.in
.user_handle
;
541 sui
.in
.info
= &u_info
;
544 /* 7. try samr_SetUserInfo2 level 26 to set the password */
545 status
= dcerpc_samr_SetUserInfo2_r(b
, mem_ctx
, &sui
);
546 /* check result of samr_SetUserInfo2 level 26 */
547 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(sui
.out
.result
)) {
548 status
= sui
.out
.result
;
550 if (!NT_STATUS_IS_OK(status
)) {
551 r
->samr_handle
.out
.error_string
552 = talloc_asprintf(mem_ctx
,
553 "SetUserInfo2 level 26 for [%s] failed: %s",
554 r
->samr_handle
.in
.account_name
, nt_errstr(status
));
560 static NTSTATUS
libnet_SetPassword_samr_handle_25(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, union libnet_SetPassword
*r
)
562 struct dcerpc_binding_handle
*b
=
563 r
->samr_handle
.in
.dcerpc_pipe
->binding_handle
;
565 struct samr_SetUserInfo2 sui
;
566 union samr_UserInfo u_info
;
567 DATA_BLOB session_key
;
569 if (!r
->samr_handle
.in
.info21
) {
570 return NT_STATUS_INVALID_PARAMETER_MIX
;
573 /* prepare samr_SetUserInfo2 level 25 */
575 u_info
.info25
.info
= *r
->samr_handle
.in
.info21
;
576 u_info
.info25
.info
.fields_present
|= SAMR_FIELD_NT_PASSWORD_PRESENT
;
578 status
= dcerpc_binding_handle_transport_session_key(b
,
581 if (!NT_STATUS_IS_OK(status
)) {
582 r
->samr_handle
.out
.error_string
= talloc_asprintf(mem_ctx
,
583 "transport_session_key failed: %s",
588 status
= encode_rc4_passwd_buffer(r
->samr_handle
.in
.newpassword
,
590 &u_info
.info25
.password
);
591 data_blob_clear_free(&session_key
);
592 if (!NT_STATUS_IS_OK(status
)) {
593 r
->samr_handle
.out
.error_string
=
594 talloc_asprintf(mem_ctx
,
595 "encode_rc4_passwd_buffer failed: %s",
601 sui
.in
.user_handle
= r
->samr_handle
.in
.user_handle
;
602 sui
.in
.info
= &u_info
;
605 /* 8. try samr_SetUserInfo2 level 25 to set the password */
606 status
= dcerpc_samr_SetUserInfo2_r(b
, mem_ctx
, &sui
);
607 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(sui
.out
.result
)) {
608 status
= sui
.out
.result
;
610 if (!NT_STATUS_IS_OK(status
)) {
611 r
->samr_handle
.out
.error_string
612 = talloc_asprintf(mem_ctx
,
613 "SetUserInfo2 level 25 for [%s] failed: %s",
614 r
->samr_handle
.in
.account_name
, nt_errstr(status
));
620 static NTSTATUS
libnet_SetPassword_samr_handle_24(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, union libnet_SetPassword
*r
)
622 struct dcerpc_binding_handle
*b
=
623 r
->samr_handle
.in
.dcerpc_pipe
->binding_handle
;
625 struct samr_SetUserInfo2 sui
;
626 union samr_UserInfo u_info
;
627 DATA_BLOB session_key
;
628 gnutls_cipher_hd_t cipher_hnd
= NULL
;
629 gnutls_datum_t enc_session_key
;
632 if (r
->samr_handle
.in
.info21
) {
633 return NT_STATUS_INVALID_PARAMETER_MIX
;
636 /* prepare samr_SetUserInfo2 level 24 */
638 encode_pw_buffer(u_info
.info24
.password
.data
, r
->samr_handle
.in
.newpassword
, STR_UNICODE
);
639 u_info
.info24
.password_expired
= 0;
641 status
= dcerpc_binding_handle_transport_session_key(b
,
644 if (!NT_STATUS_IS_OK(status
)) {
645 r
->samr_handle
.out
.error_string
= talloc_asprintf(mem_ctx
,
646 "transport_session_key failed: %s",
651 enc_session_key
= (gnutls_datum_t
) {
652 .data
= session_key
.data
,
653 .size
= session_key
.length
,
656 rc
= gnutls_cipher_init(&cipher_hnd
,
657 GNUTLS_CIPHER_ARCFOUR_128
,
660 data_blob_clear_free(&session_key
);
662 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
666 rc
= gnutls_cipher_encrypt(cipher_hnd
,
667 u_info
.info24
.password
.data
,
669 gnutls_cipher_deinit(cipher_hnd
);
671 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
675 sui
.in
.user_handle
= r
->samr_handle
.in
.user_handle
;
676 sui
.in
.info
= &u_info
;
679 /* 9. try samr_SetUserInfo2 level 24 to set the password */
680 status
= dcerpc_samr_SetUserInfo2_r(b
, mem_ctx
, &sui
);
681 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(sui
.out
.result
)) {
682 status
= sui
.out
.result
;
684 if (!NT_STATUS_IS_OK(status
)) {
685 r
->samr_handle
.out
.error_string
686 = talloc_asprintf(mem_ctx
,
687 "SetUserInfo2 level 24 for [%s] failed: %s",
688 r
->samr_handle
.in
.account_name
, nt_errstr(status
));
692 data_blob_clear(&session_key
);
696 static NTSTATUS
libnet_SetPassword_samr_handle_23(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, union libnet_SetPassword
*r
)
698 struct dcerpc_binding_handle
*b
=
699 r
->samr_handle
.in
.dcerpc_pipe
->binding_handle
;
701 struct samr_SetUserInfo2 sui
;
702 union samr_UserInfo u_info
;
703 DATA_BLOB session_key
;
704 gnutls_cipher_hd_t cipher_hnd
= NULL
;
705 gnutls_datum_t _session_key
;
708 if (!r
->samr_handle
.in
.info21
) {
709 return NT_STATUS_INVALID_PARAMETER_MIX
;
712 /* prepare samr_SetUserInfo2 level 23 */
714 u_info
.info23
.info
= *r
->samr_handle
.in
.info21
;
715 u_info
.info23
.info
.fields_present
|= SAMR_FIELD_NT_PASSWORD_PRESENT
;
716 encode_pw_buffer(u_info
.info23
.password
.data
, r
->samr_handle
.in
.newpassword
, STR_UNICODE
);
718 status
= dcerpc_binding_handle_transport_session_key(b
,
721 if (!NT_STATUS_IS_OK(status
)) {
722 r
->samr_handle
.out
.error_string
723 = talloc_asprintf(mem_ctx
,
724 "transport_session_key failed: %s",
729 _session_key
= (gnutls_datum_t
) {
730 .data
= session_key
.data
,
731 .size
= session_key
.length
,
734 rc
= gnutls_cipher_init(&cipher_hnd
,
735 GNUTLS_CIPHER_ARCFOUR_128
,
738 data_blob_clear_free(&session_key
);
740 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
744 rc
= gnutls_cipher_encrypt(cipher_hnd
,
745 u_info
.info23
.password
.data
,
747 data_blob_clear_free(&session_key
);
748 gnutls_cipher_deinit(cipher_hnd
);
750 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
754 sui
.in
.user_handle
= r
->samr_handle
.in
.user_handle
;
755 sui
.in
.info
= &u_info
;
758 /* 10. try samr_SetUserInfo2 level 23 to set the password */
759 status
= dcerpc_samr_SetUserInfo2_r(b
, mem_ctx
, &sui
);
760 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(sui
.out
.result
)) {
761 status
= sui
.out
.result
;
763 if (!NT_STATUS_IS_OK(status
)) {
764 r
->samr_handle
.out
.error_string
765 = talloc_asprintf(mem_ctx
,
766 "SetUserInfo2 level 23 for [%s] failed: %s",
767 r
->samr_handle
.in
.account_name
, nt_errstr(status
));
774 static NTSTATUS
libnet_SetPassword_samr_handle_18(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, union libnet_SetPassword
*r
)
776 struct dcerpc_binding_handle
*b
=
777 r
->samr_handle
.in
.dcerpc_pipe
->binding_handle
;
779 struct samr_SetUserInfo2 sui
;
780 union samr_UserInfo u_info
;
781 struct samr_Password ntpwd
;
784 DATA_BLOB session_key
;
787 if (r
->samr_handle
.in
.info21
) {
788 return NT_STATUS_INVALID_PARAMETER_MIX
;
791 /* prepare samr_SetUserInfo2 level 18 (nt_hash) */
793 E_md4hash(r
->samr_handle
.in
.newpassword
, ntpwd
.hash
);
794 ntpwd_in
= data_blob_const(ntpwd
.hash
, sizeof(ntpwd
.hash
));
795 ntpwd_out
= data_blob_const(u_info
.info18
.nt_pwd
.hash
,
796 sizeof(u_info
.info18
.nt_pwd
.hash
));
797 u_info
.info18
.nt_pwd_active
= 1;
798 u_info
.info18
.password_expired
= 0;
800 status
= dcerpc_binding_handle_transport_session_key(b
,
803 if (!NT_STATUS_IS_OK(status
)) {
804 r
->samr_handle
.out
.error_string
= talloc_asprintf(mem_ctx
,
805 "transport_session_key failed: %s",
810 rc
= sess_crypt_blob(&ntpwd_out
, &ntpwd_in
,
811 &session_key
, SAMBA_GNUTLS_ENCRYPT
);
812 data_blob_clear_free(&session_key
);
814 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
818 sui
.in
.user_handle
= r
->samr_handle
.in
.user_handle
;
819 sui
.in
.info
= &u_info
;
822 /* 9. try samr_SetUserInfo2 level 18 to set the password */
823 status
= dcerpc_samr_SetUserInfo2_r(b
, mem_ctx
, &sui
);
824 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(sui
.out
.result
)) {
825 status
= sui
.out
.result
;
827 if (!NT_STATUS_IS_OK(status
)) {
828 r
->samr_handle
.out
.error_string
829 = talloc_asprintf(mem_ctx
,
830 "SetUserInfo2 level 18 for [%s] failed: %s",
831 r
->samr_handle
.in
.account_name
, nt_errstr(status
));
835 data_blob_clear(&session_key
);
840 * 1. try samr_SetUserInfo2 level 26 to set the password
841 * 2. try samr_SetUserInfo2 level 25 to set the password
842 * 3. try samr_SetUserInfo2 level 24 to set the password
843 * 4. try samr_SetUserInfo2 level 23 to set the password
845 static NTSTATUS
libnet_SetPassword_samr_handle(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, union libnet_SetPassword
*r
)
849 enum libnet_SetPassword_level levels
[] = {
850 LIBNET_SET_PASSWORD_SAMR_HANDLE_26
,
851 LIBNET_SET_PASSWORD_SAMR_HANDLE_25
,
852 LIBNET_SET_PASSWORD_SAMR_HANDLE_24
,
853 LIBNET_SET_PASSWORD_SAMR_HANDLE_23
,
857 if (r
->samr_handle
.samr_level
!= 0) {
858 r
->generic
.level
= r
->samr_handle
.samr_level
;
859 return libnet_SetPassword(ctx
, mem_ctx
, r
);
862 for (i
=0; i
< ARRAY_SIZE(levels
); i
++) {
863 r
->generic
.level
= levels
[i
];
864 status
= libnet_SetPassword(ctx
, mem_ctx
, r
);
865 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_INFO_CLASS
)
866 || NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER_MIX
)
867 || NT_STATUS_EQUAL(status
, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE
)) {
868 /* Try another password set mechanism */
877 * set a password with DCERPC/SAMR calls
878 * 1. connect to the SAMR pipe of users domain PDC (maybe a standalone server or workstation)
879 * is it correct to contact the the pdc of the domain of the user who's password should be set?
880 * 2. do a samr_Connect to get a policy handle
881 * 3. do a samr_LookupDomain to get the domain sid
882 * 4. do a samr_OpenDomain to get a domain handle
883 * 5. do a samr_LookupNames to get the users rid
884 * 6. do a samr_OpenUser to get a user handle
885 * 7 call libnet_SetPassword_samr_handle to set the password
887 static NTSTATUS
libnet_SetPassword_samr(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, union libnet_SetPassword
*r
)
890 struct libnet_RpcConnect c
;
891 struct samr_Connect sc
;
892 struct policy_handle p_handle
;
893 struct samr_LookupDomain ld
;
894 struct dom_sid2
*sid
= NULL
;
895 struct lsa_String d_name
;
896 struct samr_OpenDomain od
;
897 struct policy_handle d_handle
;
898 struct samr_LookupNames ln
;
899 struct samr_Ids rids
, types
;
900 struct samr_OpenUser ou
;
901 struct policy_handle u_handle
;
902 union libnet_SetPassword r2
;
905 /* prepare connect to the SAMR pipe of users domain PDC */
906 c
.level
= LIBNET_RPC_CONNECT_PDC
;
907 c
.in
.name
= r
->samr
.in
.domain_name
;
908 c
.in
.dcerpc_iface
= &ndr_table_samr
;
910 /* 1. connect to the SAMR pipe of users domain PDC (maybe a standalone server or workstation) */
911 status
= libnet_RpcConnect(ctx
, mem_ctx
, &c
);
912 if (!NT_STATUS_IS_OK(status
)) {
913 r
->samr
.out
.error_string
= talloc_asprintf(mem_ctx
,
914 "Connection to SAMR pipe of PDC of domain '%s' failed: %s",
915 r
->samr
.in
.domain_name
, nt_errstr(status
));
919 /* prepare samr_Connect */
920 ZERO_STRUCT(p_handle
);
921 sc
.in
.system_name
= NULL
;
922 sc
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
923 sc
.out
.connect_handle
= &p_handle
;
925 /* 2. do a samr_Connect to get a policy handle */
926 status
= dcerpc_samr_Connect_r(c
.out
.dcerpc_pipe
->binding_handle
, mem_ctx
, &sc
);
927 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(sc
.out
.result
)) {
928 status
= sc
.out
.result
;
930 if (!NT_STATUS_IS_OK(status
)) {
931 r
->samr
.out
.error_string
= talloc_asprintf(mem_ctx
,
932 "samr_Connect failed: %s",
937 /* prepare samr_LookupDomain */
938 d_name
.string
= r
->samr
.in
.domain_name
;
939 ld
.in
.connect_handle
= &p_handle
;
940 ld
.in
.domain_name
= &d_name
;
943 /* 3. do a samr_LookupDomain to get the domain sid */
944 status
= dcerpc_samr_LookupDomain_r(c
.out
.dcerpc_pipe
->binding_handle
, mem_ctx
, &ld
);
945 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(ld
.out
.result
)) {
946 status
= ld
.out
.result
;
948 if (!NT_STATUS_IS_OK(status
)) {
949 r
->samr
.out
.error_string
= talloc_asprintf(mem_ctx
,
950 "samr_LookupDomain for [%s] failed: %s",
951 r
->samr
.in
.domain_name
, nt_errstr(status
));
955 /* prepare samr_OpenDomain */
956 ZERO_STRUCT(d_handle
);
957 od
.in
.connect_handle
= &p_handle
;
958 od
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
959 od
.in
.sid
= *ld
.out
.sid
;
960 od
.out
.domain_handle
= &d_handle
;
962 /* 4. do a samr_OpenDomain to get a domain handle */
963 status
= dcerpc_samr_OpenDomain_r(c
.out
.dcerpc_pipe
->binding_handle
, mem_ctx
, &od
);
964 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(od
.out
.result
)) {
965 status
= od
.out
.result
;
967 if (!NT_STATUS_IS_OK(status
)) {
968 r
->samr
.out
.error_string
= talloc_asprintf(mem_ctx
,
969 "samr_OpenDomain for [%s] failed: %s",
970 r
->samr
.in
.domain_name
, nt_errstr(status
));
974 /* prepare samr_LookupNames */
975 ln
.in
.domain_handle
= &d_handle
;
977 ln
.in
.names
= talloc_array(mem_ctx
, struct lsa_String
, 1);
979 ln
.out
.types
= &types
;
981 r
->samr
.out
.error_string
= "Out of Memory";
982 return NT_STATUS_NO_MEMORY
;
984 ln
.in
.names
[0].string
= r
->samr
.in
.account_name
;
986 /* 5. do a samr_LookupNames to get the users rid */
987 status
= dcerpc_samr_LookupNames_r(c
.out
.dcerpc_pipe
->binding_handle
, mem_ctx
, &ln
);
988 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(ln
.out
.result
)) {
989 status
= ln
.out
.result
;
991 if (!NT_STATUS_IS_OK(status
)) {
992 r
->samr
.out
.error_string
= talloc_asprintf(mem_ctx
,
993 "samr_LookupNames for [%s] failed: %s",
994 r
->samr
.in
.account_name
, nt_errstr(status
));
998 /* check if we got one RID for the user */
999 if (ln
.out
.rids
->count
!= 1) {
1000 r
->samr
.out
.error_string
= talloc_asprintf(mem_ctx
,
1001 "samr_LookupNames for [%s] returns %d RIDs",
1002 r
->samr
.in
.account_name
, ln
.out
.rids
->count
);
1003 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1007 if (ln
.out
.types
->count
!= 1) {
1008 r
->samr
.out
.error_string
= talloc_asprintf(mem_ctx
,
1009 "samr_LookupNames for [%s] returns %d RID TYPEs",
1010 r
->samr
.in
.account_name
, ln
.out
.types
->count
);
1011 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
1015 /* prepare samr_OpenUser */
1016 ZERO_STRUCT(u_handle
);
1017 ou
.in
.domain_handle
= &d_handle
;
1018 ou
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
1019 ou
.in
.rid
= ln
.out
.rids
->ids
[0];
1020 ou
.out
.user_handle
= &u_handle
;
1022 /* 6. do a samr_OpenUser to get a user handle */
1023 status
= dcerpc_samr_OpenUser_r(c
.out
.dcerpc_pipe
->binding_handle
, mem_ctx
, &ou
);
1024 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(ou
.out
.result
)) {
1025 status
= ou
.out
.result
;
1027 if (!NT_STATUS_IS_OK(status
)) {
1028 r
->samr
.out
.error_string
= talloc_asprintf(mem_ctx
,
1029 "samr_OpenUser for [%s] failed: %s",
1030 r
->samr
.in
.account_name
, nt_errstr(status
));
1035 r2
.samr_handle
.level
= LIBNET_SET_PASSWORD_SAMR_HANDLE
;
1036 r2
.samr_handle
.samr_level
= r
->samr
.samr_level
;
1037 r2
.samr_handle
.in
.account_name
= r
->samr
.in
.account_name
;
1038 r2
.samr_handle
.in
.newpassword
= r
->samr
.in
.newpassword
;
1039 r2
.samr_handle
.in
.user_handle
= &u_handle
;
1040 r2
.samr_handle
.in
.dcerpc_pipe
= c
.out
.dcerpc_pipe
;
1041 r2
.samr_handle
.in
.info21
= NULL
;
1043 status
= libnet_SetPassword(ctx
, mem_ctx
, &r2
);
1045 r
->generic
.out
.error_string
= r2
.samr_handle
.out
.error_string
;
1048 /* close connection */
1049 talloc_unlink(ctx
, c
.out
.dcerpc_pipe
);
1054 static NTSTATUS
libnet_SetPassword_generic(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, union libnet_SetPassword
*r
)
1057 union libnet_SetPassword r2
;
1060 r2
.samr
.level
= LIBNET_SET_PASSWORD_SAMR
;
1061 r2
.samr
.samr_level
= r
->generic
.samr_level
;
1062 r2
.samr
.in
.account_name
= r
->generic
.in
.account_name
;
1063 r2
.samr
.in
.domain_name
= r
->generic
.in
.domain_name
;
1064 r2
.samr
.in
.newpassword
= r
->generic
.in
.newpassword
;
1066 r
->generic
.out
.error_string
= "Unknown Error";
1067 status
= libnet_SetPassword(ctx
, mem_ctx
, &r2
);
1069 r
->generic
.out
.error_string
= r2
.samr
.out
.error_string
;
1074 NTSTATUS
libnet_SetPassword(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, union libnet_SetPassword
*r
)
1076 enum smb_encryption_setting encryption_state
=
1077 cli_credentials_get_smb_encryption(ctx
->cred
);
1078 NTSTATUS status
= NT_STATUS_INVALID_LEVEL
;
1080 switch (r
->generic
.level
) {
1081 case LIBNET_SET_PASSWORD_GENERIC
:
1082 status
= libnet_SetPassword_generic(ctx
, mem_ctx
, r
);
1084 case LIBNET_SET_PASSWORD_SAMR
:
1085 status
= libnet_SetPassword_samr(ctx
, mem_ctx
, r
);
1087 case LIBNET_SET_PASSWORD_SAMR_HANDLE
:
1088 status
= libnet_SetPassword_samr_handle(ctx
, mem_ctx
, r
);
1090 case LIBNET_SET_PASSWORD_SAMR_HANDLE_26
:
1091 if (encryption_state
== SMB_ENCRYPTION_REQUIRED
) {
1092 GNUTLS_FIPS140_SET_LAX_MODE();
1094 status
= libnet_SetPassword_samr_handle_26(ctx
, mem_ctx
, r
);
1096 case LIBNET_SET_PASSWORD_SAMR_HANDLE_25
:
1097 if (encryption_state
== SMB_ENCRYPTION_REQUIRED
) {
1098 GNUTLS_FIPS140_SET_LAX_MODE();
1100 status
= libnet_SetPassword_samr_handle_25(ctx
, mem_ctx
, r
);
1102 case LIBNET_SET_PASSWORD_SAMR_HANDLE_24
:
1103 if (encryption_state
== SMB_ENCRYPTION_REQUIRED
) {
1104 GNUTLS_FIPS140_SET_LAX_MODE();
1106 status
= libnet_SetPassword_samr_handle_24(ctx
, mem_ctx
, r
);
1108 case LIBNET_SET_PASSWORD_SAMR_HANDLE_23
:
1109 if (encryption_state
== SMB_ENCRYPTION_REQUIRED
) {
1110 GNUTLS_FIPS140_SET_LAX_MODE();
1112 status
= libnet_SetPassword_samr_handle_23(ctx
, mem_ctx
, r
);
1114 case LIBNET_SET_PASSWORD_SAMR_HANDLE_18
:
1115 if (encryption_state
== SMB_ENCRYPTION_REQUIRED
) {
1116 GNUTLS_FIPS140_SET_LAX_MODE();
1118 status
= libnet_SetPassword_samr_handle_18(ctx
, mem_ctx
, r
);
1120 case LIBNET_SET_PASSWORD_KRB5
:
1121 status
= NT_STATUS_NOT_IMPLEMENTED
;
1123 case LIBNET_SET_PASSWORD_LDAP
:
1124 status
= NT_STATUS_NOT_IMPLEMENTED
;
1126 case LIBNET_SET_PASSWORD_RAP
:
1127 status
= NT_STATUS_NOT_IMPLEMENTED
;
1131 GNUTLS_FIPS140_SET_STRICT_MODE();