r17930: Merge noinclude branch:
[gd/samba-autobuild/.git] / source4 / libnet / libnet_passwd.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    Copyright (C) Stefan Metzmacher      2004
5    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
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"
27
28 /*
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)
35  */
36 static NTSTATUS libnet_ChangePassword_samr(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_ChangePassword *r)
37 {
38         NTSTATUS status;
39         struct libnet_RpcConnect c;
40 #if 0
41         struct policy_handle user_handle;
42         struct samr_Password hash1, hash2, hash3, hash4, hash5, hash6;
43         struct samr_ChangePasswordUser pw;
44 #endif
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];
54
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;
59
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));
66                 return status;
67         }
68
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;
72
73         E_md4hash(r->samr.in.oldpassword, old_nt_hash);
74         E_md4hash(r->samr.in.newpassword, new_nt_hash);
75
76         E_deshash(r->samr.in.oldpassword, old_lm_hash);
77         E_deshash(r->samr.in.newpassword, new_lm_hash);
78
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);
83
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);
87
88         pw3.in.server = &server;
89         pw3.in.account = &account;
90         pw3.in.nt_password = &nt_pass;
91         pw3.in.nt_verifier = &nt_verifier;
92         pw3.in.lm_change = 1;
93         pw3.in.lm_password = &lm_pass;
94         pw3.in.lm_verifier = &lm_verifier;
95         pw3.in.password3 = NULL;
96
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",
102                                                 nt_errstr(status));
103                 goto ChangePasswordUser2;
104         }
105
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;
115                         goto disconnect;
116                 }
117                 goto ChangePasswordUser2;
118         }
119
120         goto disconnect;
121
122 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);
127
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);
131
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;
139
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",
145                                                 nt_errstr(status));
146                 goto OemChangePasswordUser2;
147         }
148
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;
157                         goto disconnect;
158                 }
159                 goto OemChangePasswordUser2;
160         }
161
162         goto disconnect;
163
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;
168
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);
172
173         oe2.in.server = &a_server;
174         oe2.in.account = &a_account;
175         oe2.in.password = &lm_pass;
176         oe2.in.hash = &lm_verifier;
177
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",
183                                                 nt_errstr(status));
184                 goto ChangePasswordUser;
185         }
186
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;
195                         goto disconnect;
196                 }
197                 goto ChangePasswordUser;
198         }
199
200         goto disconnect;
201
202 ChangePasswordUser:
203 #if 0
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);
211
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;
224
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",
230                                                 nt_errstr(status));
231                 goto disconnect;
232         }
233
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;
242                         goto disconnect;
243                 }
244                 goto disconnect;
245         }
246 #endif
247 disconnect:
248         /* close connection */
249         talloc_free(c.out.dcerpc_pipe);
250
251         return status;
252 }
253
254 static NTSTATUS libnet_ChangePassword_generic(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_ChangePassword *r)
255 {
256         NTSTATUS status;
257         union libnet_ChangePassword r2;
258
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;
264
265         status = libnet_ChangePassword(ctx, mem_ctx, &r2);
266
267         r->generic.out.error_string = r2.samr.out.error_string;
268
269         return status;
270 }
271
272 NTSTATUS libnet_ChangePassword(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_ChangePassword *r)
273 {
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;
285         }
286
287         return NT_STATUS_INVALID_LEVEL;
288 }
289
290 static NTSTATUS libnet_SetPassword_samr_handle_26(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_SetPassword *r)
291 {
292         NTSTATUS status;
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;
299
300         /* prepare samr_SetUserInfo level 26 */
301         ZERO_STRUCT(u_info);
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);
304         
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",
309                                                                   nt_errstr(status));
310                 return status;
311         }
312         
313         generate_random_buffer((uint8_t *)confounder, 16);
314         
315         MD5Init(&md5);
316         MD5Update(&md5, confounder, 16);
317         MD5Update(&md5, session_key.data, session_key.length);
318         MD5Final(confounded_session_key.data, &md5);
319         
320         arcfour_crypt_blob(u_info.info26.password.data, 516, &confounded_session_key);
321         memcpy(&u_info.info26.password.data[516], confounder, 16);
322         
323         sui.in.user_handle = r->samr_handle.in.user_handle;
324         sui.in.info = &u_info;
325         sui.in.level = 26;
326         
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));
335         }
336         return status;
337 }
338
339 static NTSTATUS libnet_SetPassword_samr_handle_25(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_SetPassword *r)
340 {
341         NTSTATUS status;
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;
348
349         /* prepare samr_SetUserInfo level 25 */
350         ZERO_STRUCT(u_info);
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);
353
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",
358                                                 nt_errstr(status));
359                 return status;
360         }
361
362         generate_random_buffer((uint8_t *)confounder, 16);
363
364         MD5Init(&md5);
365         MD5Update(&md5, confounder, 16);
366         MD5Update(&md5, session_key.data, session_key.length);
367         MD5Final(confounded_session_key.data, &md5);
368
369         arcfour_crypt_blob(u_info.info25.password.data, 516, &confounded_session_key);
370         memcpy(&u_info.info25.password.data[516], confounder, 16);
371
372         sui.in.user_handle = r->samr_handle.in.user_handle;
373         sui.in.info = &u_info;
374         sui.in.level = 25;
375
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));
383         }
384         return status;
385 }
386
387 static NTSTATUS libnet_SetPassword_samr_handle_24(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_SetPassword *r)
388 {
389         NTSTATUS status;
390         struct samr_SetUserInfo sui;
391         union samr_UserInfo u_info;
392         DATA_BLOB session_key;
393
394         /* prepare samr_SetUserInfo level 24 */
395         ZERO_STRUCT(u_info);
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;
399
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",
404                                                 nt_errstr(status));
405                 return status;
406         }
407
408         arcfour_crypt_blob(u_info.info24.password.data, 516, &session_key);
409
410         sui.in.user_handle = r->samr_handle.in.user_handle;
411         sui.in.info = &u_info;
412         sui.in.level = 24;
413
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));
421         }
422         return status;
423 }
424
425 static NTSTATUS libnet_SetPassword_samr_handle_23(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_SetPassword *r)
426 {
427         NTSTATUS status;
428         struct samr_SetUserInfo sui;
429         union samr_UserInfo u_info;
430         DATA_BLOB session_key;
431
432         /* prepare samr_SetUserInfo level 23 */
433         ZERO_STRUCT(u_info);
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);
436
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",
442                                           nt_errstr(status));
443                 return status;
444         }
445
446         arcfour_crypt_blob(u_info.info23.password.data, 516, &session_key);
447
448         sui.in.user_handle = r->samr_handle.in.user_handle;
449         sui.in.info = &u_info;
450         sui.in.level = 23;
451
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));
459         }
460         return status;
461 }
462
463 /*
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
468 */
469 static NTSTATUS libnet_SetPassword_samr_handle(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_SetPassword *r)
470 {
471         NTSTATUS status;
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,
477         };
478         int i;
479         
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 */
486                         continue;
487                 }
488                 break;
489         }
490         
491         return status;
492 }
493 /*
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
503  */
504 static NTSTATUS libnet_SetPassword_samr(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_SetPassword *r)
505 {
506         NTSTATUS status;
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;
518
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;
523
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));
530                 return status;
531         }
532
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;
538
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",
544                                                 nt_errstr(status));
545                 goto disconnect;
546         }
547
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;
552
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));
559                 goto disconnect;
560         }
561
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;
568
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));
575                 goto disconnect;
576         }
577
578         /* prepare samr_LookupNames */
579         ln.in.domain_handle = &d_handle;
580         ln.in.num_names = 1;
581         ln.in.names = talloc_array(mem_ctx, struct lsa_String, 1);
582         if (!ln.in.names) {
583                 r->samr.out.error_string = "Out of Memory";
584                 return NT_STATUS_NO_MEMORY;
585         }
586         ln.in.names[0].string = r->samr.in.account_name;
587
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));
594                 goto disconnect;
595         }
596
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;
603                 goto disconnect;        
604         }
605
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;
612
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));
619                 goto disconnect;
620         }
621
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;
627
628         status = libnet_SetPassword(ctx, mem_ctx, &r2);
629
630         r->generic.out.error_string = r2.samr_handle.out.error_string;
631
632 disconnect:
633         /* close connection */
634         talloc_free(c.out.dcerpc_pipe);
635
636         return status;
637 }
638
639 static NTSTATUS libnet_SetPassword_generic(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_SetPassword *r)
640 {
641         NTSTATUS status;
642         union libnet_SetPassword r2;
643
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;
648
649         r->generic.out.error_string = "Unknown Error";
650         status = libnet_SetPassword(ctx, mem_ctx, &r2);
651
652         r->generic.out.error_string = r2.samr.out.error_string;
653
654         return status;
655 }
656
657 NTSTATUS libnet_SetPassword(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_SetPassword *r)
658 {
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;
680         }
681
682         return NT_STATUS_INVALID_LEVEL;
683 }