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
)
504 struct samr_SetUserInfo2 sui
;
505 union samr_UserInfo u_info
;
506 DATA_BLOB session_key
;
508 if (r
->samr_handle
.in
.info21
) {
509 return NT_STATUS_INVALID_PARAMETER_MIX
;
512 /* prepare samr_SetUserInfo2 level 26 */
514 u_info
.info26
.password_expired
= 0;
516 status
= dcerpc_fetch_session_key(r
->samr_handle
.in
.dcerpc_pipe
, &session_key
);
517 if (!NT_STATUS_IS_OK(status
)) {
518 r
->samr_handle
.out
.error_string
= talloc_asprintf(mem_ctx
,
519 "dcerpc_fetch_session_key failed: %s",
524 status
= encode_rc4_passwd_buffer(r
->samr_handle
.in
.newpassword
,
526 &u_info
.info26
.password
);
527 if (!NT_STATUS_IS_OK(status
)) {
528 r
->samr_handle
.out
.error_string
=
529 talloc_asprintf(mem_ctx
,
530 "encode_rc4_passwd_buffer failed: %s",
535 sui
.in
.user_handle
= r
->samr_handle
.in
.user_handle
;
536 sui
.in
.info
= &u_info
;
539 /* 7. try samr_SetUserInfo2 level 26 to set the password */
540 status
= dcerpc_samr_SetUserInfo2_r(r
->samr_handle
.in
.dcerpc_pipe
->binding_handle
, mem_ctx
, &sui
);
541 /* check result of samr_SetUserInfo2 level 26 */
542 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(sui
.out
.result
)) {
543 status
= sui
.out
.result
;
545 if (!NT_STATUS_IS_OK(status
)) {
546 r
->samr_handle
.out
.error_string
547 = talloc_asprintf(mem_ctx
,
548 "SetUserInfo2 level 26 for [%s] failed: %s",
549 r
->samr_handle
.in
.account_name
, nt_errstr(status
));
555 static NTSTATUS
libnet_SetPassword_samr_handle_25(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, union libnet_SetPassword
*r
)
558 struct samr_SetUserInfo2 sui
;
559 union samr_UserInfo u_info
;
560 DATA_BLOB session_key
;
562 if (!r
->samr_handle
.in
.info21
) {
563 return NT_STATUS_INVALID_PARAMETER_MIX
;
566 /* prepare samr_SetUserInfo2 level 25 */
568 u_info
.info25
.info
= *r
->samr_handle
.in
.info21
;
569 u_info
.info25
.info
.fields_present
|= SAMR_FIELD_NT_PASSWORD_PRESENT
;
571 status
= dcerpc_fetch_session_key(r
->samr_handle
.in
.dcerpc_pipe
, &session_key
);
572 if (!NT_STATUS_IS_OK(status
)) {
573 r
->samr_handle
.out
.error_string
= talloc_asprintf(mem_ctx
,
574 "dcerpc_fetch_session_key failed: %s",
579 status
= encode_rc4_passwd_buffer(r
->samr_handle
.in
.newpassword
,
581 &u_info
.info25
.password
);
582 if (!NT_STATUS_IS_OK(status
)) {
583 r
->samr_handle
.out
.error_string
=
584 talloc_asprintf(mem_ctx
,
585 "encode_rc4_passwd_buffer failed: %s",
591 sui
.in
.user_handle
= r
->samr_handle
.in
.user_handle
;
592 sui
.in
.info
= &u_info
;
595 /* 8. try samr_SetUserInfo2 level 25 to set the password */
596 status
= dcerpc_samr_SetUserInfo2_r(r
->samr_handle
.in
.dcerpc_pipe
->binding_handle
, mem_ctx
, &sui
);
597 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(sui
.out
.result
)) {
598 status
= sui
.out
.result
;
600 if (!NT_STATUS_IS_OK(status
)) {
601 r
->samr_handle
.out
.error_string
602 = talloc_asprintf(mem_ctx
,
603 "SetUserInfo2 level 25 for [%s] failed: %s",
604 r
->samr_handle
.in
.account_name
, nt_errstr(status
));
610 static NTSTATUS
libnet_SetPassword_samr_handle_24(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, union libnet_SetPassword
*r
)
613 struct samr_SetUserInfo2 sui
;
614 union samr_UserInfo u_info
;
615 DATA_BLOB session_key
;
616 gnutls_cipher_hd_t cipher_hnd
= NULL
;
617 gnutls_datum_t enc_session_key
;
620 if (r
->samr_handle
.in
.info21
) {
621 return NT_STATUS_INVALID_PARAMETER_MIX
;
624 /* prepare samr_SetUserInfo2 level 24 */
626 encode_pw_buffer(u_info
.info24
.password
.data
, r
->samr_handle
.in
.newpassword
, STR_UNICODE
);
627 u_info
.info24
.password_expired
= 0;
629 status
= dcerpc_fetch_session_key(r
->samr_handle
.in
.dcerpc_pipe
, &session_key
);
630 if (!NT_STATUS_IS_OK(status
)) {
631 r
->samr_handle
.out
.error_string
= talloc_asprintf(mem_ctx
,
632 "dcerpc_fetch_session_key failed: %s",
637 enc_session_key
= (gnutls_datum_t
) {
638 .data
= session_key
.data
,
639 .size
= session_key
.length
,
642 rc
= gnutls_cipher_init(&cipher_hnd
,
643 GNUTLS_CIPHER_ARCFOUR_128
,
647 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
651 rc
= gnutls_cipher_encrypt(cipher_hnd
,
652 u_info
.info24
.password
.data
,
654 gnutls_cipher_deinit(cipher_hnd
);
656 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
660 sui
.in
.user_handle
= r
->samr_handle
.in
.user_handle
;
661 sui
.in
.info
= &u_info
;
664 /* 9. try samr_SetUserInfo2 level 24 to set the password */
665 status
= dcerpc_samr_SetUserInfo2_r(r
->samr_handle
.in
.dcerpc_pipe
->binding_handle
, mem_ctx
, &sui
);
666 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(sui
.out
.result
)) {
667 status
= sui
.out
.result
;
669 if (!NT_STATUS_IS_OK(status
)) {
670 r
->samr_handle
.out
.error_string
671 = talloc_asprintf(mem_ctx
,
672 "SetUserInfo2 level 24 for [%s] failed: %s",
673 r
->samr_handle
.in
.account_name
, nt_errstr(status
));
677 data_blob_clear(&session_key
);
681 static NTSTATUS
libnet_SetPassword_samr_handle_23(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, union libnet_SetPassword
*r
)
684 struct samr_SetUserInfo2 sui
;
685 union samr_UserInfo u_info
;
686 DATA_BLOB session_key
;
687 gnutls_cipher_hd_t cipher_hnd
= NULL
;
688 gnutls_datum_t _session_key
;
691 if (!r
->samr_handle
.in
.info21
) {
692 return NT_STATUS_INVALID_PARAMETER_MIX
;
695 /* prepare samr_SetUserInfo2 level 23 */
697 u_info
.info23
.info
= *r
->samr_handle
.in
.info21
;
698 u_info
.info23
.info
.fields_present
|= SAMR_FIELD_NT_PASSWORD_PRESENT
;
699 encode_pw_buffer(u_info
.info23
.password
.data
, r
->samr_handle
.in
.newpassword
, STR_UNICODE
);
701 status
= dcerpc_fetch_session_key(r
->samr_handle
.in
.dcerpc_pipe
, &session_key
);
702 if (!NT_STATUS_IS_OK(status
)) {
703 r
->samr_handle
.out
.error_string
704 = talloc_asprintf(mem_ctx
,
705 "dcerpc_fetch_session_key failed: %s",
710 _session_key
= (gnutls_datum_t
) {
711 .data
= session_key
.data
,
712 .size
= session_key
.length
,
715 rc
= gnutls_cipher_init(&cipher_hnd
,
716 GNUTLS_CIPHER_ARCFOUR_128
,
720 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
724 rc
= gnutls_cipher_encrypt(cipher_hnd
,
725 u_info
.info23
.password
.data
,
727 data_blob_clear_free(&session_key
);
728 gnutls_cipher_deinit(cipher_hnd
);
730 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
734 sui
.in
.user_handle
= r
->samr_handle
.in
.user_handle
;
735 sui
.in
.info
= &u_info
;
738 /* 10. try samr_SetUserInfo2 level 23 to set the password */
739 status
= dcerpc_samr_SetUserInfo2_r(r
->samr_handle
.in
.dcerpc_pipe
->binding_handle
, mem_ctx
, &sui
);
740 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(sui
.out
.result
)) {
741 status
= sui
.out
.result
;
743 if (!NT_STATUS_IS_OK(status
)) {
744 r
->samr_handle
.out
.error_string
745 = talloc_asprintf(mem_ctx
,
746 "SetUserInfo2 level 23 for [%s] failed: %s",
747 r
->samr_handle
.in
.account_name
, nt_errstr(status
));
754 static NTSTATUS
libnet_SetPassword_samr_handle_18(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, union libnet_SetPassword
*r
)
757 struct samr_SetUserInfo2 sui
;
758 union samr_UserInfo u_info
;
759 struct samr_Password ntpwd
;
762 DATA_BLOB session_key
;
765 if (r
->samr_handle
.in
.info21
) {
766 return NT_STATUS_INVALID_PARAMETER_MIX
;
769 /* prepare samr_SetUserInfo2 level 18 (nt_hash) */
771 E_md4hash(r
->samr_handle
.in
.newpassword
, ntpwd
.hash
);
772 ntpwd_in
= data_blob_const(ntpwd
.hash
, sizeof(ntpwd
.hash
));
773 ntpwd_out
= data_blob_const(u_info
.info18
.nt_pwd
.hash
,
774 sizeof(u_info
.info18
.nt_pwd
.hash
));
775 u_info
.info18
.nt_pwd_active
= 1;
776 u_info
.info18
.password_expired
= 0;
778 status
= dcerpc_fetch_session_key(r
->samr_handle
.in
.dcerpc_pipe
, &session_key
);
779 if (!NT_STATUS_IS_OK(status
)) {
780 r
->samr_handle
.out
.error_string
= talloc_asprintf(mem_ctx
,
781 "dcerpc_fetch_session_key failed: %s",
786 rc
= sess_crypt_blob(&ntpwd_out
, &ntpwd_in
,
787 &session_key
, SAMBA_GNUTLS_ENCRYPT
);
789 status
= gnutls_error_to_ntstatus(rc
, NT_STATUS_CRYPTO_SYSTEM_INVALID
);
793 sui
.in
.user_handle
= r
->samr_handle
.in
.user_handle
;
794 sui
.in
.info
= &u_info
;
797 /* 9. try samr_SetUserInfo2 level 18 to set the password */
798 status
= dcerpc_samr_SetUserInfo2_r(r
->samr_handle
.in
.dcerpc_pipe
->binding_handle
, mem_ctx
, &sui
);
799 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(sui
.out
.result
)) {
800 status
= sui
.out
.result
;
802 if (!NT_STATUS_IS_OK(status
)) {
803 r
->samr_handle
.out
.error_string
804 = talloc_asprintf(mem_ctx
,
805 "SetUserInfo2 level 18 for [%s] failed: %s",
806 r
->samr_handle
.in
.account_name
, nt_errstr(status
));
810 data_blob_clear(&session_key
);
815 * 1. try samr_SetUserInfo2 level 26 to set the password
816 * 2. try samr_SetUserInfo2 level 25 to set the password
817 * 3. try samr_SetUserInfo2 level 24 to set the password
818 * 4. try samr_SetUserInfo2 level 23 to set the password
820 static NTSTATUS
libnet_SetPassword_samr_handle(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, union libnet_SetPassword
*r
)
824 enum libnet_SetPassword_level levels
[] = {
825 LIBNET_SET_PASSWORD_SAMR_HANDLE_26
,
826 LIBNET_SET_PASSWORD_SAMR_HANDLE_25
,
827 LIBNET_SET_PASSWORD_SAMR_HANDLE_24
,
828 LIBNET_SET_PASSWORD_SAMR_HANDLE_23
,
832 if (r
->samr_handle
.samr_level
!= 0) {
833 r
->generic
.level
= r
->samr_handle
.samr_level
;
834 return libnet_SetPassword(ctx
, mem_ctx
, r
);
837 for (i
=0; i
< ARRAY_SIZE(levels
); i
++) {
838 r
->generic
.level
= levels
[i
];
839 status
= libnet_SetPassword(ctx
, mem_ctx
, r
);
840 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_INFO_CLASS
)
841 || NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER_MIX
)
842 || NT_STATUS_EQUAL(status
, NT_STATUS_RPC_ENUM_VALUE_OUT_OF_RANGE
)) {
843 /* Try another password set mechanism */
852 * set a password with DCERPC/SAMR calls
853 * 1. connect to the SAMR pipe of users domain PDC (maybe a standalone server or workstation)
854 * is it correct to contact the the pdc of the domain of the user who's password should be set?
855 * 2. do a samr_Connect to get a policy handle
856 * 3. do a samr_LookupDomain to get the domain sid
857 * 4. do a samr_OpenDomain to get a domain handle
858 * 5. do a samr_LookupNames to get the users rid
859 * 6. do a samr_OpenUser to get a user handle
860 * 7 call libnet_SetPassword_samr_handle to set the password
862 static NTSTATUS
libnet_SetPassword_samr(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, union libnet_SetPassword
*r
)
865 struct libnet_RpcConnect c
;
866 struct samr_Connect sc
;
867 struct policy_handle p_handle
;
868 struct samr_LookupDomain ld
;
869 struct dom_sid2
*sid
= NULL
;
870 struct lsa_String d_name
;
871 struct samr_OpenDomain od
;
872 struct policy_handle d_handle
;
873 struct samr_LookupNames ln
;
874 struct samr_Ids rids
, types
;
875 struct samr_OpenUser ou
;
876 struct policy_handle u_handle
;
877 union libnet_SetPassword r2
;
880 /* prepare connect to the SAMR pipe of users domain PDC */
881 c
.level
= LIBNET_RPC_CONNECT_PDC
;
882 c
.in
.name
= r
->samr
.in
.domain_name
;
883 c
.in
.dcerpc_iface
= &ndr_table_samr
;
885 /* 1. connect to the SAMR pipe of users domain PDC (maybe a standalone server or workstation) */
886 status
= libnet_RpcConnect(ctx
, mem_ctx
, &c
);
887 if (!NT_STATUS_IS_OK(status
)) {
888 r
->samr
.out
.error_string
= talloc_asprintf(mem_ctx
,
889 "Connection to SAMR pipe of PDC of domain '%s' failed: %s",
890 r
->samr
.in
.domain_name
, nt_errstr(status
));
894 /* prepare samr_Connect */
895 ZERO_STRUCT(p_handle
);
896 sc
.in
.system_name
= NULL
;
897 sc
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
898 sc
.out
.connect_handle
= &p_handle
;
900 /* 2. do a samr_Connect to get a policy handle */
901 status
= dcerpc_samr_Connect_r(c
.out
.dcerpc_pipe
->binding_handle
, mem_ctx
, &sc
);
902 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(sc
.out
.result
)) {
903 status
= sc
.out
.result
;
905 if (!NT_STATUS_IS_OK(status
)) {
906 r
->samr
.out
.error_string
= talloc_asprintf(mem_ctx
,
907 "samr_Connect failed: %s",
912 /* prepare samr_LookupDomain */
913 d_name
.string
= r
->samr
.in
.domain_name
;
914 ld
.in
.connect_handle
= &p_handle
;
915 ld
.in
.domain_name
= &d_name
;
918 /* 3. do a samr_LookupDomain to get the domain sid */
919 status
= dcerpc_samr_LookupDomain_r(c
.out
.dcerpc_pipe
->binding_handle
, mem_ctx
, &ld
);
920 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(ld
.out
.result
)) {
921 status
= ld
.out
.result
;
923 if (!NT_STATUS_IS_OK(status
)) {
924 r
->samr
.out
.error_string
= talloc_asprintf(mem_ctx
,
925 "samr_LookupDomain for [%s] failed: %s",
926 r
->samr
.in
.domain_name
, nt_errstr(status
));
930 /* prepare samr_OpenDomain */
931 ZERO_STRUCT(d_handle
);
932 od
.in
.connect_handle
= &p_handle
;
933 od
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
934 od
.in
.sid
= *ld
.out
.sid
;
935 od
.out
.domain_handle
= &d_handle
;
937 /* 4. do a samr_OpenDomain to get a domain handle */
938 status
= dcerpc_samr_OpenDomain_r(c
.out
.dcerpc_pipe
->binding_handle
, mem_ctx
, &od
);
939 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(od
.out
.result
)) {
940 status
= od
.out
.result
;
942 if (!NT_STATUS_IS_OK(status
)) {
943 r
->samr
.out
.error_string
= talloc_asprintf(mem_ctx
,
944 "samr_OpenDomain for [%s] failed: %s",
945 r
->samr
.in
.domain_name
, nt_errstr(status
));
949 /* prepare samr_LookupNames */
950 ln
.in
.domain_handle
= &d_handle
;
952 ln
.in
.names
= talloc_array(mem_ctx
, struct lsa_String
, 1);
954 ln
.out
.types
= &types
;
956 r
->samr
.out
.error_string
= "Out of Memory";
957 return NT_STATUS_NO_MEMORY
;
959 ln
.in
.names
[0].string
= r
->samr
.in
.account_name
;
961 /* 5. do a samr_LookupNames to get the users rid */
962 status
= dcerpc_samr_LookupNames_r(c
.out
.dcerpc_pipe
->binding_handle
, mem_ctx
, &ln
);
963 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(ln
.out
.result
)) {
964 status
= ln
.out
.result
;
966 if (!NT_STATUS_IS_OK(status
)) {
967 r
->samr
.out
.error_string
= talloc_asprintf(mem_ctx
,
968 "samr_LookupNames for [%s] failed: %s",
969 r
->samr
.in
.account_name
, nt_errstr(status
));
973 /* check if we got one RID for the user */
974 if (ln
.out
.rids
->count
!= 1) {
975 r
->samr
.out
.error_string
= talloc_asprintf(mem_ctx
,
976 "samr_LookupNames for [%s] returns %d RIDs",
977 r
->samr
.in
.account_name
, ln
.out
.rids
->count
);
978 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
982 if (ln
.out
.types
->count
!= 1) {
983 r
->samr
.out
.error_string
= talloc_asprintf(mem_ctx
,
984 "samr_LookupNames for [%s] returns %d RID TYPEs",
985 r
->samr
.in
.account_name
, ln
.out
.types
->count
);
986 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
990 /* prepare samr_OpenUser */
991 ZERO_STRUCT(u_handle
);
992 ou
.in
.domain_handle
= &d_handle
;
993 ou
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
994 ou
.in
.rid
= ln
.out
.rids
->ids
[0];
995 ou
.out
.user_handle
= &u_handle
;
997 /* 6. do a samr_OpenUser to get a user handle */
998 status
= dcerpc_samr_OpenUser_r(c
.out
.dcerpc_pipe
->binding_handle
, mem_ctx
, &ou
);
999 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_OK(ou
.out
.result
)) {
1000 status
= ou
.out
.result
;
1002 if (!NT_STATUS_IS_OK(status
)) {
1003 r
->samr
.out
.error_string
= talloc_asprintf(mem_ctx
,
1004 "samr_OpenUser for [%s] failed: %s",
1005 r
->samr
.in
.account_name
, nt_errstr(status
));
1010 r2
.samr_handle
.level
= LIBNET_SET_PASSWORD_SAMR_HANDLE
;
1011 r2
.samr_handle
.samr_level
= r
->samr
.samr_level
;
1012 r2
.samr_handle
.in
.account_name
= r
->samr
.in
.account_name
;
1013 r2
.samr_handle
.in
.newpassword
= r
->samr
.in
.newpassword
;
1014 r2
.samr_handle
.in
.user_handle
= &u_handle
;
1015 r2
.samr_handle
.in
.dcerpc_pipe
= c
.out
.dcerpc_pipe
;
1016 r2
.samr_handle
.in
.info21
= NULL
;
1018 status
= libnet_SetPassword(ctx
, mem_ctx
, &r2
);
1020 r
->generic
.out
.error_string
= r2
.samr_handle
.out
.error_string
;
1023 /* close connection */
1024 talloc_unlink(ctx
, c
.out
.dcerpc_pipe
);
1029 static NTSTATUS
libnet_SetPassword_generic(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, union libnet_SetPassword
*r
)
1032 union libnet_SetPassword r2
;
1035 r2
.samr
.level
= LIBNET_SET_PASSWORD_SAMR
;
1036 r2
.samr
.samr_level
= r
->generic
.samr_level
;
1037 r2
.samr
.in
.account_name
= r
->generic
.in
.account_name
;
1038 r2
.samr
.in
.domain_name
= r
->generic
.in
.domain_name
;
1039 r2
.samr
.in
.newpassword
= r
->generic
.in
.newpassword
;
1041 r
->generic
.out
.error_string
= "Unknown Error";
1042 status
= libnet_SetPassword(ctx
, mem_ctx
, &r2
);
1044 r
->generic
.out
.error_string
= r2
.samr
.out
.error_string
;
1049 NTSTATUS
libnet_SetPassword(struct libnet_context
*ctx
, TALLOC_CTX
*mem_ctx
, union libnet_SetPassword
*r
)
1051 enum smb_encryption_setting encryption_state
=
1052 cli_credentials_get_smb_encryption(ctx
->cred
);
1053 NTSTATUS status
= NT_STATUS_INVALID_LEVEL
;
1055 switch (r
->generic
.level
) {
1056 case LIBNET_SET_PASSWORD_GENERIC
:
1057 status
= libnet_SetPassword_generic(ctx
, mem_ctx
, r
);
1059 case LIBNET_SET_PASSWORD_SAMR
:
1060 status
= libnet_SetPassword_samr(ctx
, mem_ctx
, r
);
1062 case LIBNET_SET_PASSWORD_SAMR_HANDLE
:
1063 status
= libnet_SetPassword_samr_handle(ctx
, mem_ctx
, r
);
1065 case LIBNET_SET_PASSWORD_SAMR_HANDLE_26
:
1066 if (encryption_state
== SMB_ENCRYPTION_REQUIRED
) {
1067 GNUTLS_FIPS140_SET_LAX_MODE();
1069 status
= libnet_SetPassword_samr_handle_26(ctx
, mem_ctx
, r
);
1071 case LIBNET_SET_PASSWORD_SAMR_HANDLE_25
:
1072 if (encryption_state
== SMB_ENCRYPTION_REQUIRED
) {
1073 GNUTLS_FIPS140_SET_LAX_MODE();
1075 status
= libnet_SetPassword_samr_handle_25(ctx
, mem_ctx
, r
);
1077 case LIBNET_SET_PASSWORD_SAMR_HANDLE_24
:
1078 if (encryption_state
== SMB_ENCRYPTION_REQUIRED
) {
1079 GNUTLS_FIPS140_SET_LAX_MODE();
1081 status
= libnet_SetPassword_samr_handle_24(ctx
, mem_ctx
, r
);
1083 case LIBNET_SET_PASSWORD_SAMR_HANDLE_23
:
1084 if (encryption_state
== SMB_ENCRYPTION_REQUIRED
) {
1085 GNUTLS_FIPS140_SET_LAX_MODE();
1087 status
= libnet_SetPassword_samr_handle_23(ctx
, mem_ctx
, r
);
1089 case LIBNET_SET_PASSWORD_SAMR_HANDLE_18
:
1090 if (encryption_state
== SMB_ENCRYPTION_REQUIRED
) {
1091 GNUTLS_FIPS140_SET_LAX_MODE();
1093 status
= libnet_SetPassword_samr_handle_18(ctx
, mem_ctx
, r
);
1095 case LIBNET_SET_PASSWORD_KRB5
:
1096 status
= NT_STATUS_NOT_IMPLEMENTED
;
1098 case LIBNET_SET_PASSWORD_LDAP
:
1099 status
= NT_STATUS_NOT_IMPLEMENTED
;
1101 case LIBNET_SET_PASSWORD_RAP
:
1102 status
= NT_STATUS_NOT_IMPLEMENTED
;
1106 GNUTLS_FIPS140_SET_STRICT_MODE();