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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "libnet/libnet.h"
24 #include "lib/crypto/crypto.h"
25 #include "libcli/auth/libcli_auth.h"
26 #include "librpc/gen_ndr/ndr_samr_c.h"
29 * do a password change using DCERPC/SAMR calls
30 * 1. connect to the SAMR pipe of users domain PDC (maybe a standalone server or workstation)
31 * 2. try samr_ChangePasswordUser3
32 * 3. try samr_ChangePasswordUser2
33 * 4. try samr_OemChangePasswordUser2
34 * (not yet: 5. try samr_ChangePasswordUser)
36 static NTSTATUS libnet_ChangePassword_samr(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_ChangePassword *r)
39 struct libnet_RpcConnect c;
41 struct policy_handle user_handle;
42 struct samr_Password hash1, hash2, hash3, hash4, hash5, hash6;
43 struct samr_ChangePasswordUser pw;
45 struct samr_OemChangePasswordUser2 oe2;
46 struct samr_ChangePasswordUser2 pw2;
47 struct samr_ChangePasswordUser3 pw3;
48 struct lsa_String server, account;
49 struct lsa_AsciiString a_server, a_account;
50 struct samr_CryptPassword nt_pass, lm_pass;
51 struct samr_Password nt_verifier, lm_verifier;
52 uint8_t old_nt_hash[16], new_nt_hash[16];
53 uint8_t old_lm_hash[16], new_lm_hash[16];
55 /* prepare connect to the SAMR pipe of the users domain PDC */
56 c.level = LIBNET_RPC_CONNECT_PDC;
57 c.in.name = r->samr.in.domain_name;
58 c.in.dcerpc_iface = &dcerpc_table_samr;
60 /* 1. connect to the SAMR pipe of users domain PDC (maybe a standalone server or workstation) */
61 status = libnet_RpcConnect(ctx, mem_ctx, &c);
62 if (!NT_STATUS_IS_OK(status)) {
63 r->samr.out.error_string = talloc_asprintf(mem_ctx,
64 "Connection to SAMR pipe of PDC of domain '%s' failed: %s\n",
65 r->samr.in.domain_name, nt_errstr(status));
69 /* prepare password change for account */
70 server.string = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(c.out.dcerpc_pipe));
71 account.string = r->samr.in.account_name;
73 E_md4hash(r->samr.in.oldpassword, old_nt_hash);
74 E_md4hash(r->samr.in.newpassword, new_nt_hash);
76 E_deshash(r->samr.in.oldpassword, old_lm_hash);
77 E_deshash(r->samr.in.newpassword, new_lm_hash);
79 /* prepare samr_ChangePasswordUser3 */
80 encode_pw_buffer(lm_pass.data, r->samr.in.newpassword, STR_UNICODE);
81 arcfour_crypt(lm_pass.data, old_nt_hash, 516);
82 E_old_pw_hash(new_lm_hash, old_lm_hash, lm_verifier.hash);
84 encode_pw_buffer(nt_pass.data, r->samr.in.newpassword, STR_UNICODE);
85 arcfour_crypt(nt_pass.data, old_nt_hash, 516);
86 E_old_pw_hash(new_nt_hash, old_nt_hash, nt_verifier.hash);
88 pw3.in.server = &server;
89 pw3.in.account = &account;
90 pw3.in.nt_password = &nt_pass;
91 pw3.in.nt_verifier = &nt_verifier;
93 pw3.in.lm_password = &lm_pass;
94 pw3.in.lm_verifier = &lm_verifier;
95 pw3.in.password3 = NULL;
97 /* 2. try samr_ChangePasswordUser3 */
98 status = dcerpc_samr_ChangePasswordUser3(c.out.dcerpc_pipe, mem_ctx, &pw3);
99 if (!NT_STATUS_IS_OK(status)) {
100 r->samr.out.error_string = talloc_asprintf(mem_ctx,
101 "samr_ChangePasswordUser3 failed: %s\n",
103 goto ChangePasswordUser2;
106 /* check result of samr_ChangePasswordUser3 */
107 if (!NT_STATUS_IS_OK(pw3.out.result)) {
108 r->samr.out.error_string = talloc_asprintf(mem_ctx,
109 "samr_ChangePasswordUser3 for '%s\\%s' failed: %s\n",
110 r->samr.in.domain_name, r->samr.in.account_name,
111 nt_errstr(pw3.out.result));
112 /* TODO: give the reason of the reject */
113 if (NT_STATUS_EQUAL(pw3.out.result, NT_STATUS_PASSWORD_RESTRICTION)) {
114 status = pw3.out.result;
117 goto ChangePasswordUser2;
123 /* prepare samr_ChangePasswordUser2 */
124 encode_pw_buffer(lm_pass.data, r->samr.in.newpassword, STR_ASCII|STR_TERMINATE);
125 arcfour_crypt(lm_pass.data, old_lm_hash, 516);
126 E_old_pw_hash(new_lm_hash, old_lm_hash, lm_verifier.hash);
128 encode_pw_buffer(nt_pass.data, r->samr.in.newpassword, STR_UNICODE);
129 arcfour_crypt(nt_pass.data, old_nt_hash, 516);
130 E_old_pw_hash(new_nt_hash, old_nt_hash, nt_verifier.hash);
132 pw2.in.server = &server;
133 pw2.in.account = &account;
134 pw2.in.nt_password = &nt_pass;
135 pw2.in.nt_verifier = &nt_verifier;
136 pw2.in.lm_change = 1;
137 pw2.in.lm_password = &lm_pass;
138 pw2.in.lm_verifier = &lm_verifier;
140 /* 3. try samr_ChangePasswordUser2 */
141 status = dcerpc_samr_ChangePasswordUser2(c.out.dcerpc_pipe, mem_ctx, &pw2);
142 if (!NT_STATUS_IS_OK(status)) {
143 r->samr.out.error_string = talloc_asprintf(mem_ctx,
144 "samr_ChangePasswordUser2 failed: %s\n",
146 goto OemChangePasswordUser2;
149 /* check result of samr_ChangePasswordUser2 */
150 if (!NT_STATUS_IS_OK(pw2.out.result)) {
151 r->samr.out.error_string = talloc_asprintf(mem_ctx,
152 "samr_ChangePasswordUser2 for '%s\\%s' failed: %s\n",
153 r->samr.in.domain_name, r->samr.in.account_name,
154 nt_errstr(pw2.out.result));
155 if (NT_STATUS_EQUAL(pw2.out.result, NT_STATUS_PASSWORD_RESTRICTION)) {
156 status = pw2.out.result;
159 goto OemChangePasswordUser2;
164 OemChangePasswordUser2:
165 /* prepare samr_OemChangePasswordUser2 */
166 a_server.string = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(c.out.dcerpc_pipe));
167 a_account.string = r->samr.in.account_name;
169 encode_pw_buffer(lm_pass.data, r->samr.in.newpassword, STR_ASCII);
170 arcfour_crypt(lm_pass.data, old_lm_hash, 516);
171 E_old_pw_hash(new_lm_hash, old_lm_hash, lm_verifier.hash);
173 oe2.in.server = &a_server;
174 oe2.in.account = &a_account;
175 oe2.in.password = &lm_pass;
176 oe2.in.hash = &lm_verifier;
178 /* 4. try samr_OemChangePasswordUser2 */
179 status = dcerpc_samr_OemChangePasswordUser2(c.out.dcerpc_pipe, mem_ctx, &oe2);
180 if (!NT_STATUS_IS_OK(status)) {
181 r->samr.out.error_string = talloc_asprintf(mem_ctx,
182 "samr_OemChangePasswordUser2 failed: %s\n",
184 goto ChangePasswordUser;
187 /* check result of samr_OemChangePasswordUser2 */
188 if (!NT_STATUS_IS_OK(oe2.out.result)) {
189 r->samr.out.error_string = talloc_asprintf(mem_ctx,
190 "samr_OemChangePasswordUser2 for '%s\\%s' failed: %s\n",
191 r->samr.in.domain_name, r->samr.in.account_name,
192 nt_errstr(oe2.out.result));
193 if (NT_STATUS_EQUAL(oe2.out.result, NT_STATUS_PASSWORD_RESTRICTION)) {
194 status = oe2.out.result;
197 goto ChangePasswordUser;
204 /* prepare samr_ChangePasswordUser */
205 E_old_pw_hash(new_lm_hash, old_lm_hash, hash1.hash);
206 E_old_pw_hash(old_lm_hash, new_lm_hash, hash2.hash);
207 E_old_pw_hash(new_nt_hash, old_nt_hash, hash3.hash);
208 E_old_pw_hash(old_nt_hash, new_nt_hash, hash4.hash);
209 E_old_pw_hash(old_lm_hash, new_nt_hash, hash5.hash);
210 E_old_pw_hash(old_nt_hash, new_lm_hash, hash6.hash);
212 /* TODO: ask for a user_handle */
213 pw.in.handle = &user_handle;
214 pw.in.lm_present = 1;
215 pw.in.old_lm_crypted = &hash1;
216 pw.in.new_lm_crypted = &hash2;
217 pw.in.nt_present = 1;
218 pw.in.old_nt_crypted = &hash3;
219 pw.in.new_nt_crypted = &hash4;
220 pw.in.cross1_present = 1;
221 pw.in.nt_cross = &hash5;
222 pw.in.cross2_present = 1;
223 pw.in.lm_cross = &hash6;
225 /* 5. try samr_ChangePasswordUser */
226 status = dcerpc_samr_ChangePasswordUser(c.pdc.out.dcerpc_pipe, mem_ctx, &pw);
227 if (!NT_STATUS_IS_OK(status)) {
228 r->samr.out.error_string = talloc_asprintf(mem_ctx,
229 "samr_ChangePasswordUser failed: %s\n",
234 /* check result of samr_ChangePasswordUser */
235 if (!NT_STATUS_IS_OK(pw.out.result)) {
236 r->samr.out.error_string = talloc_asprintf(mem_ctx,
237 "samr_ChangePasswordUser for '%s\\%s' failed: %s\n",
238 r->samr.in.domain_name, r->samr.in.account_name,
239 nt_errstr(pw.out.result));
240 if (NT_STATUS_EQUAL(pw.out.result, NT_STATUS_PASSWORD_RESTRICTION)) {
241 status = pw.out.result;
248 /* close connection */
249 talloc_free(c.out.dcerpc_pipe);
254 static NTSTATUS libnet_ChangePassword_generic(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_ChangePassword *r)
257 union libnet_ChangePassword r2;
259 r2.samr.level = LIBNET_CHANGE_PASSWORD_SAMR;
260 r2.samr.in.account_name = r->generic.in.account_name;
261 r2.samr.in.domain_name = r->generic.in.domain_name;
262 r2.samr.in.oldpassword = r->generic.in.oldpassword;
263 r2.samr.in.newpassword = r->generic.in.newpassword;
265 status = libnet_ChangePassword(ctx, mem_ctx, &r2);
267 r->generic.out.error_string = r2.samr.out.error_string;
272 NTSTATUS libnet_ChangePassword(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_ChangePassword *r)
274 switch (r->generic.level) {
275 case LIBNET_CHANGE_PASSWORD_GENERIC:
276 return libnet_ChangePassword_generic(ctx, mem_ctx, r);
277 case LIBNET_CHANGE_PASSWORD_SAMR:
278 return libnet_ChangePassword_samr(ctx, mem_ctx, r);
279 case LIBNET_CHANGE_PASSWORD_KRB5:
280 return NT_STATUS_NOT_IMPLEMENTED;
281 case LIBNET_CHANGE_PASSWORD_LDAP:
282 return NT_STATUS_NOT_IMPLEMENTED;
283 case LIBNET_CHANGE_PASSWORD_RAP:
284 return NT_STATUS_NOT_IMPLEMENTED;
287 return NT_STATUS_INVALID_LEVEL;
290 static NTSTATUS libnet_SetPassword_samr_handle_26(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_SetPassword *r)
293 struct samr_SetUserInfo sui;
294 union samr_UserInfo u_info;
295 DATA_BLOB session_key;
296 DATA_BLOB confounded_session_key = data_blob_talloc(mem_ctx, NULL, 16);
297 uint8_t confounder[16];
298 struct MD5Context md5;
300 /* prepare samr_SetUserInfo level 26 */
302 encode_pw_buffer(u_info.info26.password.data, r->samr_handle.in.newpassword, STR_UNICODE);
303 u_info.info26.pw_len = strlen(r->samr_handle.in.newpassword);
305 status = dcerpc_fetch_session_key(r->samr_handle.in.dcerpc_pipe, &session_key);
306 if (!NT_STATUS_IS_OK(status)) {
307 r->samr_handle.out.error_string = talloc_asprintf(mem_ctx,
308 "dcerpc_fetch_session_key failed: %s\n",
313 generate_random_buffer((uint8_t *)confounder, 16);
316 MD5Update(&md5, confounder, 16);
317 MD5Update(&md5, session_key.data, session_key.length);
318 MD5Final(confounded_session_key.data, &md5);
320 arcfour_crypt_blob(u_info.info26.password.data, 516, &confounded_session_key);
321 memcpy(&u_info.info26.password.data[516], confounder, 16);
323 sui.in.user_handle = r->samr_handle.in.user_handle;
324 sui.in.info = &u_info;
327 /* 7. try samr_SetUserInfo level 26 to set the password */
328 status = dcerpc_samr_SetUserInfo(r->samr_handle.in.dcerpc_pipe, mem_ctx, &sui);
329 /* check result of samr_SetUserInfo level 26 */
330 if (!NT_STATUS_IS_OK(status)) {
331 r->samr_handle.out.error_string
332 = talloc_asprintf(mem_ctx,
333 "SetUserInfo level 26 for [%s] failed: %s\n",
334 r->samr_handle.in.account_name, nt_errstr(status));
339 static NTSTATUS libnet_SetPassword_samr_handle_25(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_SetPassword *r)
342 struct samr_SetUserInfo sui;
343 union samr_UserInfo u_info;
344 DATA_BLOB session_key;
345 DATA_BLOB confounded_session_key = data_blob_talloc(mem_ctx, NULL, 16);
346 uint8_t confounder[16];
347 struct MD5Context md5;
349 /* prepare samr_SetUserInfo level 25 */
351 u_info.info25.info.fields_present = SAMR_FIELD_PASSWORD;
352 encode_pw_buffer(u_info.info25.password.data, r->samr_handle.in.newpassword, STR_UNICODE);
354 status = dcerpc_fetch_session_key(r->samr_handle.in.dcerpc_pipe, &session_key);
355 if (!NT_STATUS_IS_OK(status)) {
356 r->samr_handle.out.error_string = talloc_asprintf(mem_ctx,
357 "dcerpc_fetch_session_key failed: %s\n",
362 generate_random_buffer((uint8_t *)confounder, 16);
365 MD5Update(&md5, confounder, 16);
366 MD5Update(&md5, session_key.data, session_key.length);
367 MD5Final(confounded_session_key.data, &md5);
369 arcfour_crypt_blob(u_info.info25.password.data, 516, &confounded_session_key);
370 memcpy(&u_info.info25.password.data[516], confounder, 16);
372 sui.in.user_handle = r->samr_handle.in.user_handle;
373 sui.in.info = &u_info;
376 /* 8. try samr_SetUserInfo level 25 to set the password */
377 status = dcerpc_samr_SetUserInfo(r->samr_handle.in.dcerpc_pipe, mem_ctx, &sui);
378 if (!NT_STATUS_IS_OK(status)) {
379 r->samr_handle.out.error_string
380 = talloc_asprintf(mem_ctx,
381 "SetUserInfo level 25 for [%s] failed: %s\n",
382 r->samr_handle.in.account_name, nt_errstr(status));
387 static NTSTATUS libnet_SetPassword_samr_handle_24(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_SetPassword *r)
390 struct samr_SetUserInfo sui;
391 union samr_UserInfo u_info;
392 DATA_BLOB session_key;
394 /* prepare samr_SetUserInfo level 24 */
396 encode_pw_buffer(u_info.info24.password.data, r->samr_handle.in.newpassword, STR_UNICODE);
397 /* w2k3 ignores this length */
398 u_info.info24.pw_len = strlen_m(r->samr_handle.in.newpassword)*2;
400 status = dcerpc_fetch_session_key(r->samr_handle.in.dcerpc_pipe, &session_key);
401 if (!NT_STATUS_IS_OK(status)) {
402 r->samr_handle.out.error_string = talloc_asprintf(mem_ctx,
403 "dcerpc_fetch_session_key failed: %s\n",
408 arcfour_crypt_blob(u_info.info24.password.data, 516, &session_key);
410 sui.in.user_handle = r->samr_handle.in.user_handle;
411 sui.in.info = &u_info;
414 /* 9. try samr_SetUserInfo level 24 to set the password */
415 status = dcerpc_samr_SetUserInfo(r->samr_handle.in.dcerpc_pipe, mem_ctx, &sui);
416 if (!NT_STATUS_IS_OK(status)) {
417 r->samr_handle.out.error_string
418 = talloc_asprintf(mem_ctx,
419 "SetUserInfo level 24 for [%s] failed: %s\n",
420 r->samr_handle.in.account_name, nt_errstr(status));
425 static NTSTATUS libnet_SetPassword_samr_handle_23(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_SetPassword *r)
428 struct samr_SetUserInfo sui;
429 union samr_UserInfo u_info;
430 DATA_BLOB session_key;
432 /* prepare samr_SetUserInfo level 23 */
434 u_info.info23.info.fields_present = SAMR_FIELD_PASSWORD;
435 encode_pw_buffer(u_info.info23.password.data, r->samr_handle.in.newpassword, STR_UNICODE);
437 status = dcerpc_fetch_session_key(r->samr_handle.in.dcerpc_pipe, &session_key);
438 if (!NT_STATUS_IS_OK(status)) {
439 r->samr_handle.out.error_string
440 = talloc_asprintf(mem_ctx,
441 "dcerpc_fetch_session_key failed: %s\n",
446 arcfour_crypt_blob(u_info.info23.password.data, 516, &session_key);
448 sui.in.user_handle = r->samr_handle.in.user_handle;
449 sui.in.info = &u_info;
452 /* 10. try samr_SetUserInfo level 23 to set the password */
453 status = dcerpc_samr_SetUserInfo(r->samr_handle.in.dcerpc_pipe, mem_ctx, &sui);
454 if (!NT_STATUS_IS_OK(status)) {
455 r->samr_handle.out.error_string
456 = talloc_asprintf(mem_ctx,
457 "SetUserInfo level 23 for [%s] failed: %s\n",
458 r->samr_handle.in.account_name, nt_errstr(status));
464 * 1. try samr_SetUserInfo level 26 to set the password
465 * 2. try samr_SetUserInfo level 25 to set the password
466 * 3. try samr_SetUserInfo level 24 to set the password
467 * 4. try samr_SetUserInfo level 23 to set the password
469 static NTSTATUS libnet_SetPassword_samr_handle(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_SetPassword *r)
472 enum libnet_SetPassword_level levels[] = {
473 LIBNET_SET_PASSWORD_SAMR_HANDLE_26,
474 LIBNET_SET_PASSWORD_SAMR_HANDLE_25,
475 LIBNET_SET_PASSWORD_SAMR_HANDLE_24,
476 LIBNET_SET_PASSWORD_SAMR_HANDLE_23,
480 for (i=0; i < ARRAY_SIZE(levels); i++) {
481 r->generic.level = levels[i];
482 status = libnet_SetPassword(ctx, mem_ctx, r);
483 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS)
484 || NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
485 /* Try another password set mechanism */
494 * set a password with DCERPC/SAMR calls
495 * 1. connect to the SAMR pipe of users domain PDC (maybe a standalone server or workstation)
496 * is it correct to contact the the pdc of the domain of the user who's password should be set?
497 * 2. do a samr_Connect to get a policy handle
498 * 3. do a samr_LookupDomain to get the domain sid
499 * 4. do a samr_OpenDomain to get a domain handle
500 * 5. do a samr_LookupNames to get the users rid
501 * 6. do a samr_OpenUser to get a user handle
502 * 7 call libnet_SetPassword_samr_handle to set the password
504 static NTSTATUS libnet_SetPassword_samr(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_SetPassword *r)
507 struct libnet_RpcConnect c;
508 struct samr_Connect sc;
509 struct policy_handle p_handle;
510 struct samr_LookupDomain ld;
511 struct lsa_String d_name;
512 struct samr_OpenDomain od;
513 struct policy_handle d_handle;
514 struct samr_LookupNames ln;
515 struct samr_OpenUser ou;
516 struct policy_handle u_handle;
517 union libnet_SetPassword r2;
519 /* prepare connect to the SAMR pipe of users domain PDC */
520 c.level = LIBNET_RPC_CONNECT_PDC;
521 c.in.name = r->samr.in.domain_name;
522 c.in.dcerpc_iface = &dcerpc_table_samr;
524 /* 1. connect to the SAMR pipe of users domain PDC (maybe a standalone server or workstation) */
525 status = libnet_RpcConnect(ctx, mem_ctx, &c);
526 if (!NT_STATUS_IS_OK(status)) {
527 r->samr.out.error_string = talloc_asprintf(mem_ctx,
528 "Connection to SAMR pipe of PDC of domain '%s' failed: %s\n",
529 r->samr.in.domain_name, nt_errstr(status));
533 /* prepare samr_Connect */
534 ZERO_STRUCT(p_handle);
535 sc.in.system_name = NULL;
536 sc.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
537 sc.out.connect_handle = &p_handle;
539 /* 2. do a samr_Connect to get a policy handle */
540 status = dcerpc_samr_Connect(c.out.dcerpc_pipe, mem_ctx, &sc);
541 if (!NT_STATUS_IS_OK(status)) {
542 r->samr.out.error_string = talloc_asprintf(mem_ctx,
543 "samr_Connect failed: %s\n",
548 /* prepare samr_LookupDomain */
549 d_name.string = r->samr.in.domain_name;
550 ld.in.connect_handle = &p_handle;
551 ld.in.domain_name = &d_name;
553 /* 3. do a samr_LookupDomain to get the domain sid */
554 status = dcerpc_samr_LookupDomain(c.out.dcerpc_pipe, mem_ctx, &ld);
555 if (!NT_STATUS_IS_OK(status)) {
556 r->samr.out.error_string = talloc_asprintf(mem_ctx,
557 "samr_LookupDomain for [%s] failed: %s\n",
558 r->samr.in.domain_name, nt_errstr(status));
562 /* prepare samr_OpenDomain */
563 ZERO_STRUCT(d_handle);
564 od.in.connect_handle = &p_handle;
565 od.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
566 od.in.sid = ld.out.sid;
567 od.out.domain_handle = &d_handle;
569 /* 4. do a samr_OpenDomain to get a domain handle */
570 status = dcerpc_samr_OpenDomain(c.out.dcerpc_pipe, mem_ctx, &od);
571 if (!NT_STATUS_IS_OK(status)) {
572 r->samr.out.error_string = talloc_asprintf(mem_ctx,
573 "samr_OpenDomain for [%s] failed: %s\n",
574 r->samr.in.domain_name, nt_errstr(status));
578 /* prepare samr_LookupNames */
579 ln.in.domain_handle = &d_handle;
581 ln.in.names = talloc_array(mem_ctx, struct lsa_String, 1);
583 r->samr.out.error_string = "Out of Memory";
584 return NT_STATUS_NO_MEMORY;
586 ln.in.names[0].string = r->samr.in.account_name;
588 /* 5. do a samr_LookupNames to get the users rid */
589 status = dcerpc_samr_LookupNames(c.out.dcerpc_pipe, mem_ctx, &ln);
590 if (!NT_STATUS_IS_OK(status)) {
591 r->samr.out.error_string = talloc_asprintf(mem_ctx,
592 "samr_LookupNames for [%s] failed: %s\n",
593 r->samr.in.account_name, nt_errstr(status));
597 /* check if we got one RID for the user */
598 if (ln.out.rids.count != 1) {
599 r->samr.out.error_string = talloc_asprintf(mem_ctx,
600 "samr_LookupNames for [%s] returns %d RIDs\n",
601 r->samr.in.account_name, ln.out.rids.count);
602 status = NT_STATUS_INVALID_PARAMETER;
606 /* prepare samr_OpenUser */
607 ZERO_STRUCT(u_handle);
608 ou.in.domain_handle = &d_handle;
609 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
610 ou.in.rid = ln.out.rids.ids[0];
611 ou.out.user_handle = &u_handle;
613 /* 6. do a samr_OpenUser to get a user handle */
614 status = dcerpc_samr_OpenUser(c.out.dcerpc_pipe, mem_ctx, &ou);
615 if (!NT_STATUS_IS_OK(status)) {
616 r->samr.out.error_string = talloc_asprintf(mem_ctx,
617 "samr_OpenUser for [%s] failed: %s\n",
618 r->samr.in.account_name, nt_errstr(status));
622 r2.samr_handle.level = LIBNET_SET_PASSWORD_SAMR_HANDLE;
623 r2.samr_handle.in.account_name = r->samr.in.account_name;
624 r2.samr_handle.in.newpassword = r->samr.in.newpassword;
625 r2.samr_handle.in.user_handle = &u_handle;
626 r2.samr_handle.in.dcerpc_pipe = c.out.dcerpc_pipe;
628 status = libnet_SetPassword(ctx, mem_ctx, &r2);
630 r->generic.out.error_string = r2.samr_handle.out.error_string;
633 /* close connection */
634 talloc_free(c.out.dcerpc_pipe);
639 static NTSTATUS libnet_SetPassword_generic(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_SetPassword *r)
642 union libnet_SetPassword r2;
644 r2.samr.level = LIBNET_SET_PASSWORD_SAMR;
645 r2.samr.in.account_name = r->generic.in.account_name;
646 r2.samr.in.domain_name = r->generic.in.domain_name;
647 r2.samr.in.newpassword = r->generic.in.newpassword;
649 r->generic.out.error_string = "Unknown Error";
650 status = libnet_SetPassword(ctx, mem_ctx, &r2);
652 r->generic.out.error_string = r2.samr.out.error_string;
657 NTSTATUS libnet_SetPassword(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_SetPassword *r)
659 switch (r->generic.level) {
660 case LIBNET_SET_PASSWORD_GENERIC:
661 return libnet_SetPassword_generic(ctx, mem_ctx, r);
662 case LIBNET_SET_PASSWORD_SAMR:
663 return libnet_SetPassword_samr(ctx, mem_ctx, r);
664 case LIBNET_SET_PASSWORD_SAMR_HANDLE:
665 return libnet_SetPassword_samr_handle(ctx, mem_ctx, r);
666 case LIBNET_SET_PASSWORD_SAMR_HANDLE_26:
667 return libnet_SetPassword_samr_handle_26(ctx, mem_ctx, r);
668 case LIBNET_SET_PASSWORD_SAMR_HANDLE_25:
669 return libnet_SetPassword_samr_handle_25(ctx, mem_ctx, r);
670 case LIBNET_SET_PASSWORD_SAMR_HANDLE_24:
671 return libnet_SetPassword_samr_handle_24(ctx, mem_ctx, r);
672 case LIBNET_SET_PASSWORD_SAMR_HANDLE_23:
673 return libnet_SetPassword_samr_handle_23(ctx, mem_ctx, r);
674 case LIBNET_SET_PASSWORD_KRB5:
675 return NT_STATUS_NOT_IMPLEMENTED;
676 case LIBNET_SET_PASSWORD_LDAP:
677 return NT_STATUS_NOT_IMPLEMENTED;
678 case LIBNET_SET_PASSWORD_RAP:
679 return NT_STATUS_NOT_IMPLEMENTED;
682 return NT_STATUS_INVALID_LEVEL;