r26639: librpc: Pass iconv convenience on from RPC connection to NDR library, so...
[bbaumbach/samba-autobuild/.git] / source4 / torture / rpc / samba3rpc.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    dcerpc torture tests, designed to walk Samba3 code paths
5
6    Copyright (C) Volker Lendecke 2006
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/rap/rap.h"
25 #include "torture/torture.h"
26 #include "torture/util.h"
27 #include "torture/rap/proto.h"
28 #include "librpc/gen_ndr/ndr_lsa.h"
29 #include "librpc/gen_ndr/ndr_lsa_c.h"
30 #include "librpc/gen_ndr/ndr_samr.h"
31 #include "librpc/gen_ndr/ndr_samr_c.h"
32 #include "librpc/gen_ndr/ndr_netlogon.h"
33 #include "librpc/gen_ndr/ndr_netlogon_c.h"
34 #include "librpc/gen_ndr/ndr_srvsvc.h"
35 #include "librpc/gen_ndr/ndr_srvsvc_c.h"
36 #include "librpc/gen_ndr/ndr_spoolss.h"
37 #include "librpc/gen_ndr/ndr_spoolss_c.h"
38 #include "librpc/gen_ndr/ndr_winreg.h"
39 #include "librpc/gen_ndr/ndr_winreg_c.h"
40 #include "librpc/gen_ndr/ndr_wkssvc.h"
41 #include "librpc/gen_ndr/ndr_wkssvc_c.h"
42 #include "lib/cmdline/popt_common.h"
43 #include "librpc/rpc/dcerpc.h"
44 #include "torture/rpc/rpc.h"
45 #include "libcli/libcli.h"
46 #include "libcli/composite/composite.h"
47 #include "libcli/smb_composite/smb_composite.h"
48 #include "libcli/auth/libcli_auth.h"
49 #include "lib/crypto/crypto.h"
50 #include "libcli/security/proto.h"
51 #include "param/param.h"
52 #include "lib/registry/registry.h"
53
54 /*
55  * This tests a RPC call using an invalid vuid
56  */
57
58 bool torture_bind_authcontext(struct torture_context *torture) 
59 {
60         TALLOC_CTX *mem_ctx;
61         NTSTATUS status;
62         bool ret = false;
63         struct lsa_ObjectAttribute objectattr;
64         struct lsa_OpenPolicy2 openpolicy;
65         struct policy_handle handle;
66         struct lsa_Close close_handle;
67         struct smbcli_session *tmp;
68         struct smbcli_session *session2;
69         struct smbcli_state *cli;
70         struct dcerpc_pipe *lsa_pipe;
71         struct cli_credentials *anon_creds;
72         struct smb_composite_sesssetup setup;
73
74         mem_ctx = talloc_init("torture_bind_authcontext");
75
76         if (mem_ctx == NULL) {
77                 d_printf("talloc_init failed\n");
78                 return false;
79         }
80
81         status = smbcli_full_connection(mem_ctx, &cli,
82                                         torture_setting_string(torture, "host", NULL),
83                                         lp_smb_ports(torture->lp_ctx),
84                                         "IPC$", NULL, cmdline_credentials,
85                                         NULL);
86         if (!NT_STATUS_IS_OK(status)) {
87                 d_printf("smbcli_full_connection failed: %s\n",
88                          nt_errstr(status));
89                 goto done;
90         }
91
92         lsa_pipe = dcerpc_pipe_init(mem_ctx, cli->transport->socket->event.ctx,
93                                     lp_iconv_convenience(torture->lp_ctx));
94         if (lsa_pipe == NULL) {
95                 d_printf("dcerpc_pipe_init failed\n");
96                 goto done;
97         }
98
99         status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
100         if (!NT_STATUS_IS_OK(status)) {
101                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
102                          nt_errstr(status));
103                 goto done;
104         }
105
106         status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
107         if (!NT_STATUS_IS_OK(status)) {
108                 d_printf("dcerpc_bind_auth_none failed: %s\n",
109                          nt_errstr(status));
110                 goto done;
111         }
112
113         openpolicy.in.system_name =talloc_asprintf(
114                 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
115         ZERO_STRUCT(objectattr);
116         openpolicy.in.attr = &objectattr;
117         openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
118         openpolicy.out.handle = &handle;
119
120         status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
121
122         if (!NT_STATUS_IS_OK(status)) {
123                 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
124                          nt_errstr(status));
125                 goto done;
126         }
127
128         close_handle.in.handle = &handle;
129         close_handle.out.handle = &handle;
130
131         status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
132         if (!NT_STATUS_IS_OK(status)) {
133                 d_printf("dcerpc_lsa_Close failed: %s\n",
134                          nt_errstr(status));
135                 goto done;
136         }
137
138         session2 = smbcli_session_init(cli->transport, mem_ctx, false);
139         if (session2 == NULL) {
140                 d_printf("smbcli_session_init failed\n");
141                 goto done;
142         }
143
144         if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
145                 d_printf("create_anon_creds failed\n");
146                 goto done;
147         }
148
149         setup.in.sesskey = cli->transport->negotiate.sesskey;
150         setup.in.capabilities = cli->transport->negotiate.capabilities;
151         setup.in.workgroup = "";
152         setup.in.credentials = anon_creds;
153
154         status = smb_composite_sesssetup(session2, &setup);
155         if (!NT_STATUS_IS_OK(status)) {
156                 d_printf("anon session setup failed: %s\n",
157                          nt_errstr(status));
158                 goto done;
159         }
160         session2->vuid = setup.out.vuid;
161
162         tmp = cli->tree->session;
163         cli->tree->session = session2;
164
165         status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
166
167         cli->tree->session = tmp;
168         talloc_free(lsa_pipe);
169         lsa_pipe = NULL;
170
171         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
172                 d_printf("dcerpc_lsa_OpenPolicy2 with wrong vuid gave %s, "
173                          "expected NT_STATUS_INVALID_HANDLE\n",
174                          nt_errstr(status));
175                 goto done;
176         }
177
178         ret = true;
179  done:
180         talloc_free(mem_ctx);
181         return ret;
182 }
183
184 /*
185  * Bind to lsa using a specific auth method
186  */
187
188 static bool bindtest(struct smbcli_state *cli,
189                      struct cli_credentials *credentials,
190                      struct loadparm_context *lp_ctx,
191                      uint8_t auth_type, uint8_t auth_level)
192 {
193         TALLOC_CTX *mem_ctx;
194         bool ret = false;
195         NTSTATUS status;
196
197         struct dcerpc_pipe *lsa_pipe;
198         struct lsa_ObjectAttribute objectattr;
199         struct lsa_OpenPolicy2 openpolicy;
200         struct lsa_QueryInfoPolicy query;
201         struct policy_handle handle;
202         struct lsa_Close close_handle;
203
204         if ((mem_ctx = talloc_init("bindtest")) == NULL) {
205                 d_printf("talloc_init failed\n");
206                 return false;
207         }
208
209         lsa_pipe = dcerpc_pipe_init(mem_ctx,
210                                     cli->transport->socket->event.ctx,
211                                     lp_iconv_convenience(lp_ctx));
212         if (lsa_pipe == NULL) {
213                 d_printf("dcerpc_pipe_init failed\n");
214                 goto done;
215         }
216
217         status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
218         if (!NT_STATUS_IS_OK(status)) {
219                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
220                          nt_errstr(status));
221                 goto done;
222         }
223
224         status = dcerpc_bind_auth(lsa_pipe, &ndr_table_lsarpc,
225                                   credentials, lp_ctx, auth_type, auth_level,
226                                   NULL);
227         if (!NT_STATUS_IS_OK(status)) {
228                 d_printf("dcerpc_bind_auth failed: %s\n", nt_errstr(status));
229                 goto done;
230         }
231
232         openpolicy.in.system_name =talloc_asprintf(
233                 mem_ctx, "\\\\%s", dcerpc_server_name(lsa_pipe));
234         ZERO_STRUCT(objectattr);
235         openpolicy.in.attr = &objectattr;
236         openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
237         openpolicy.out.handle = &handle;
238
239         status = dcerpc_lsa_OpenPolicy2(lsa_pipe, mem_ctx, &openpolicy);
240
241         if (!NT_STATUS_IS_OK(status)) {
242                 d_printf("dcerpc_lsa_OpenPolicy2 failed: %s\n",
243                          nt_errstr(status));
244                 goto done;
245         }
246
247         query.in.handle = &handle;
248         query.in.level = LSA_POLICY_INFO_DOMAIN;
249
250         status = dcerpc_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx, &query);
251         if (!NT_STATUS_IS_OK(status)) {
252                 d_printf("dcerpc_lsa_QueryInfoPolicy failed: %s\n",
253                          nt_errstr(status));
254                 goto done;
255         }
256
257         close_handle.in.handle = &handle;
258         close_handle.out.handle = &handle;
259
260         status = dcerpc_lsa_Close(lsa_pipe, mem_ctx, &close_handle);
261         if (!NT_STATUS_IS_OK(status)) {
262                 d_printf("dcerpc_lsa_Close failed: %s\n",
263                          nt_errstr(status));
264                 goto done;
265         }
266
267         ret = true;
268  done:
269         talloc_free(mem_ctx);
270         return ret;
271 }
272
273 /*
274  * test authenticated RPC binds with the variants Samba3 does support
275  */
276
277 bool torture_bind_samba3(struct torture_context *torture) 
278 {
279         TALLOC_CTX *mem_ctx;
280         NTSTATUS status;
281         bool ret = false;
282         struct smbcli_state *cli;
283
284         mem_ctx = talloc_init("torture_bind_authcontext");
285
286         if (mem_ctx == NULL) {
287                 d_printf("talloc_init failed\n");
288                 return false;
289         }
290
291         status = smbcli_full_connection(mem_ctx, &cli,
292                                         torture_setting_string(torture, "host", NULL),
293                                         lp_smb_ports(torture->lp_ctx),
294                                         "IPC$", NULL, cmdline_credentials,
295                                         NULL);
296         if (!NT_STATUS_IS_OK(status)) {
297                 d_printf("smbcli_full_connection failed: %s\n",
298                          nt_errstr(status));
299                 goto done;
300         }
301
302         ret = true;
303
304         ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_NTLMSSP,
305                         DCERPC_AUTH_LEVEL_INTEGRITY);
306         ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_NTLMSSP,
307                         DCERPC_AUTH_LEVEL_PRIVACY);
308         ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_SPNEGO,
309                         DCERPC_AUTH_LEVEL_INTEGRITY);
310         ret &= bindtest(cli, cmdline_credentials, torture->lp_ctx, DCERPC_AUTH_TYPE_SPNEGO,
311                         DCERPC_AUTH_LEVEL_PRIVACY);
312
313  done:
314         talloc_free(mem_ctx);
315         return ret;
316 }
317
318 /*
319  * Lookup or create a user and return all necessary info
320  */
321
322 static NTSTATUS get_usr_handle(struct smbcli_state *cli,
323                                TALLOC_CTX *mem_ctx,
324                                struct loadparm_context *lp_ctx,
325                                struct cli_credentials *admin_creds,
326                                uint8_t auth_type,
327                                uint8_t auth_level,
328                                const char *username,
329                                char **domain,
330                                struct dcerpc_pipe **result_pipe,
331                                struct policy_handle **result_handle,
332                                struct dom_sid **sid)
333 {
334         struct dcerpc_pipe *samr_pipe;
335         NTSTATUS status;
336         struct policy_handle conn_handle;
337         struct policy_handle domain_handle;
338         struct policy_handle *user_handle;
339         struct samr_Connect2 conn;
340         struct samr_EnumDomains enumdom;
341         uint32_t resume_handle = 0;
342         struct samr_LookupDomain l;
343         int dom_idx;
344         struct lsa_String domain_name;
345         struct lsa_String user_name;
346         struct samr_OpenDomain o;
347         struct samr_CreateUser2 c;
348         uint32_t user_rid,access_granted;
349
350         samr_pipe = dcerpc_pipe_init(mem_ctx,
351                                      cli->transport->socket->event.ctx,
352                                      lp_iconv_convenience(lp_ctx));
353         if (samr_pipe == NULL) {
354                 d_printf("dcerpc_pipe_init failed\n");
355                 status = NT_STATUS_NO_MEMORY;
356                 goto fail;
357         }
358
359         status = dcerpc_pipe_open_smb(samr_pipe, cli->tree, "\\samr");
360         if (!NT_STATUS_IS_OK(status)) {
361                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
362                          nt_errstr(status));
363                 goto fail;
364         }
365
366         if (admin_creds != NULL) {
367                 status = dcerpc_bind_auth(samr_pipe, &ndr_table_samr,
368                                           admin_creds, lp_ctx, auth_type, auth_level,
369                                           NULL);
370                 if (!NT_STATUS_IS_OK(status)) {
371                         d_printf("dcerpc_bind_auth failed: %s\n",
372                                  nt_errstr(status));
373                         goto fail;
374                 }
375         } else {
376                 /* We must have an authenticated SMB connection */
377                 status = dcerpc_bind_auth_none(samr_pipe, &ndr_table_samr);
378                 if (!NT_STATUS_IS_OK(status)) {
379                         d_printf("dcerpc_bind_auth_none failed: %s\n",
380                                  nt_errstr(status));
381                         goto fail;
382                 }
383         }
384
385         conn.in.system_name = talloc_asprintf(
386                 mem_ctx, "\\\\%s", dcerpc_server_name(samr_pipe));
387         conn.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
388         conn.out.connect_handle = &conn_handle;
389
390         status = dcerpc_samr_Connect2(samr_pipe, mem_ctx, &conn);
391         if (!NT_STATUS_IS_OK(status)) {
392                 d_printf("samr_Connect2 failed: %s\n", nt_errstr(status));
393                 goto fail;
394         }
395
396         enumdom.in.connect_handle = &conn_handle;
397         enumdom.in.resume_handle = &resume_handle;
398         enumdom.in.buf_size = (uint32_t)-1;
399         enumdom.out.resume_handle = &resume_handle;
400
401         status = dcerpc_samr_EnumDomains(samr_pipe, mem_ctx, &enumdom);
402         if (!NT_STATUS_IS_OK(status)) {
403                 d_printf("samr_EnumDomains failed: %s\n", nt_errstr(status));
404                 goto fail;
405         }
406
407         if (enumdom.out.num_entries != 2) {
408                 d_printf("samr_EnumDomains returned %d entries, expected 2\n",
409                          enumdom.out.num_entries);
410                 status = NT_STATUS_UNSUCCESSFUL;
411                 goto fail;
412         }
413
414         dom_idx = strequal(enumdom.out.sam->entries[0].name.string,
415                            "builtin") ? 1:0;
416
417         l.in.connect_handle = &conn_handle;
418         domain_name.string = enumdom.out.sam->entries[0].name.string;
419         *domain = talloc_strdup(mem_ctx, domain_name.string);
420         l.in.domain_name = &domain_name;
421
422         status = dcerpc_samr_LookupDomain(samr_pipe, mem_ctx, &l);
423         if (!NT_STATUS_IS_OK(status)) {
424                 d_printf("samr_LookupDomain failed: %s\n", nt_errstr(status));
425                 goto fail;
426         }
427
428         o.in.connect_handle = &conn_handle;
429         o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
430         o.in.sid = l.out.sid;
431         o.out.domain_handle = &domain_handle;
432
433         status = dcerpc_samr_OpenDomain(samr_pipe, mem_ctx, &o);
434         if (!NT_STATUS_IS_OK(status)) {
435                 d_printf("samr_OpenDomain failed: %s\n", nt_errstr(status));
436                 goto fail;
437         }
438
439         c.in.domain_handle = &domain_handle;
440         user_name.string = username;
441         c.in.account_name = &user_name;
442         c.in.acct_flags = ACB_NORMAL;
443         c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
444         user_handle = talloc(mem_ctx, struct policy_handle);
445         c.out.user_handle = user_handle;
446         c.out.access_granted = &access_granted;
447         c.out.rid = &user_rid;
448
449         status = dcerpc_samr_CreateUser2(samr_pipe, mem_ctx, &c);
450
451         if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
452                 struct samr_LookupNames ln;
453                 struct samr_OpenUser ou;
454
455                 ln.in.domain_handle = &domain_handle;
456                 ln.in.num_names = 1;
457                 ln.in.names = &user_name;
458
459                 status = dcerpc_samr_LookupNames(samr_pipe, mem_ctx, &ln);
460                 if (!NT_STATUS_IS_OK(status)) {
461                         d_printf("samr_LookupNames failed: %s\n",
462                                  nt_errstr(status));
463                         goto fail;
464                 }
465
466                 ou.in.domain_handle = &domain_handle;
467                 ou.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
468                 user_rid = ou.in.rid = ln.out.rids.ids[0];
469                 ou.out.user_handle = user_handle;
470
471                 status = dcerpc_samr_OpenUser(samr_pipe, mem_ctx, &ou);
472                 if (!NT_STATUS_IS_OK(status)) {
473                         d_printf("samr_OpenUser failed: %s\n",
474                                  nt_errstr(status));
475                         goto fail;
476                 }
477         }
478
479         if (!NT_STATUS_IS_OK(status)) {
480                 d_printf("samr_CreateUser failed: %s\n", nt_errstr(status));
481                 goto fail;
482         }
483
484         *result_pipe = samr_pipe;
485         *result_handle = user_handle;
486         if (sid != NULL) {
487                 *sid = dom_sid_add_rid(mem_ctx, l.out.sid, user_rid);
488         }
489         return NT_STATUS_OK;
490
491  fail:
492         return status;
493 }
494
495 /*
496  * Create a test user
497  */
498
499 static bool create_user(TALLOC_CTX *mem_ctx, struct smbcli_state *cli,
500                         struct loadparm_context *lp_ctx,
501                         struct cli_credentials *admin_creds,
502                         const char *username, const char *password,
503                         char **domain_name,
504                         struct dom_sid **user_sid)
505 {
506         TALLOC_CTX *tmp_ctx;
507         NTSTATUS status;
508         struct dcerpc_pipe *samr_pipe;
509         struct policy_handle *wks_handle;
510         bool ret = false;
511
512         if (!(tmp_ctx = talloc_new(mem_ctx))) {
513                 d_printf("talloc_init failed\n");
514                 return false;
515         }
516
517         status = get_usr_handle(cli, tmp_ctx, lp_ctx, admin_creds,
518                                 DCERPC_AUTH_TYPE_NTLMSSP,
519                                 DCERPC_AUTH_LEVEL_INTEGRITY,
520                                 username, domain_name, &samr_pipe, &wks_handle,
521                                 user_sid);
522         if (!NT_STATUS_IS_OK(status)) {
523                 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
524                 goto done;
525         }
526
527         {
528                 struct samr_SetUserInfo2 sui2;
529                 struct samr_SetUserInfo sui;
530                 struct samr_QueryUserInfo qui;
531                 union samr_UserInfo u_info;
532                 DATA_BLOB session_key;
533
534
535                 ZERO_STRUCT(u_info);
536                 encode_pw_buffer(u_info.info23.password.data, password,
537                                  STR_UNICODE);
538
539                 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
540                 if (!NT_STATUS_IS_OK(status)) {
541                         d_printf("dcerpc_fetch_session_key failed\n");
542                         goto done;
543                 }
544                 arcfour_crypt_blob(u_info.info23.password.data, 516,
545                                    &session_key);
546                 u_info.info23.info.password_expired = 0;
547                 u_info.info23.info.fields_present = SAMR_FIELD_PASSWORD | 
548                                                     SAMR_FIELD_PASSWORD2 |
549                                                     SAMR_FIELD_EXPIRED_FLAG;
550                 sui2.in.user_handle = wks_handle;
551                 sui2.in.info = &u_info;
552                 sui2.in.level = 23;
553
554                 status = dcerpc_samr_SetUserInfo2(samr_pipe, tmp_ctx, &sui2);
555                 if (!NT_STATUS_IS_OK(status)) {
556                         d_printf("samr_SetUserInfo(23) failed: %s\n",
557                                  nt_errstr(status));
558                         goto done;
559                 }
560
561                 u_info.info16.acct_flags = ACB_NORMAL;
562                 sui.in.user_handle = wks_handle;
563                 sui.in.info = &u_info;
564                 sui.in.level = 16;
565
566                 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
567                 if (!NT_STATUS_IS_OK(status)) {
568                         d_printf("samr_SetUserInfo(16) failed\n");
569                         goto done;
570                 }
571
572                 qui.in.user_handle = wks_handle;
573                 qui.in.level = 21;
574
575                 status = dcerpc_samr_QueryUserInfo(samr_pipe, tmp_ctx, &qui);
576                 if (!NT_STATUS_IS_OK(status)) {
577                         d_printf("samr_QueryUserInfo(21) failed\n");
578                         goto done;
579                 }
580
581                 qui.out.info->info21.allow_password_change = 0;
582                 qui.out.info->info21.force_password_change = 0;
583                 qui.out.info->info21.account_name.string = NULL;
584                 qui.out.info->info21.rid = 0;
585                 qui.out.info->info21.acct_expiry = 0;
586                 qui.out.info->info21.fields_present = 0x81827fa; /* copy usrmgr.exe */
587
588                 u_info.info21 = qui.out.info->info21;
589                 sui.in.user_handle = wks_handle;
590                 sui.in.info = &u_info;
591                 sui.in.level = 21;
592
593                 status = dcerpc_samr_SetUserInfo(samr_pipe, tmp_ctx, &sui);
594                 if (!NT_STATUS_IS_OK(status)) {
595                         d_printf("samr_SetUserInfo(21) failed\n");
596                         goto done;
597                 }
598         }
599
600         *domain_name= talloc_steal(mem_ctx, *domain_name);
601         *user_sid = talloc_steal(mem_ctx, *user_sid);
602         ret = true;
603  done:
604         talloc_free(tmp_ctx);
605         return ret;
606 }
607
608 /*
609  * Delete a test user
610  */
611
612 static bool delete_user(struct smbcli_state *cli,
613                         struct loadparm_context *lp_ctx,
614                         struct cli_credentials *admin_creds,
615                         const char *username)
616 {
617         TALLOC_CTX *mem_ctx;
618         NTSTATUS status;
619         char *dom_name;
620         struct dcerpc_pipe *samr_pipe;
621         struct policy_handle *user_handle;
622         bool ret = false;
623
624         if ((mem_ctx = talloc_init("leave")) == NULL) {
625                 d_printf("talloc_init failed\n");
626                 return false;
627         }
628
629         status = get_usr_handle(cli, mem_ctx, lp_ctx, admin_creds,
630                                 DCERPC_AUTH_TYPE_NTLMSSP,
631                                 DCERPC_AUTH_LEVEL_INTEGRITY,
632                                 username, &dom_name, &samr_pipe,
633                                 &user_handle, NULL);
634
635         if (!NT_STATUS_IS_OK(status)) {
636                 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
637                 goto done;
638         }
639
640         {
641                 struct samr_DeleteUser d;
642
643                 d.in.user_handle = user_handle;
644                 d.out.user_handle = user_handle;
645
646                 status = dcerpc_samr_DeleteUser(samr_pipe, mem_ctx, &d);
647                 if (!NT_STATUS_IS_OK(status)) {
648                         d_printf("samr_DeleteUser failed %s\n", nt_errstr(status));
649                         goto done;
650                 }
651         }
652
653         ret = true;
654
655  done:
656         talloc_free(mem_ctx);
657         return ret;
658 }
659
660 /*
661  * Do a Samba3-style join
662  */
663
664 static bool join3(struct smbcli_state *cli,
665                   struct loadparm_context *lp_ctx,
666                   bool use_level25,
667                   struct cli_credentials *admin_creds,
668                   struct cli_credentials *wks_creds)
669 {
670         TALLOC_CTX *mem_ctx;
671         NTSTATUS status;
672         char *dom_name;
673         struct dcerpc_pipe *samr_pipe;
674         struct policy_handle *wks_handle;
675         bool ret = false;
676
677         if ((mem_ctx = talloc_init("join3")) == NULL) {
678                 d_printf("talloc_init failed\n");
679                 return false;
680         }
681
682         status = get_usr_handle(
683                 cli, mem_ctx, lp_ctx, admin_creds,
684                 DCERPC_AUTH_TYPE_NTLMSSP,
685                 DCERPC_AUTH_LEVEL_PRIVACY,
686                 talloc_asprintf(mem_ctx, "%s$",
687                                 cli_credentials_get_workstation(wks_creds)),
688                 &dom_name, &samr_pipe, &wks_handle, NULL);
689
690         if (!NT_STATUS_IS_OK(status)) {
691                 d_printf("get_wks_handle failed: %s\n", nt_errstr(status));
692                 goto done;
693         }
694
695         cli_credentials_set_domain(wks_creds, dom_name, CRED_SPECIFIED);
696
697         if (use_level25) {
698                 struct samr_SetUserInfo2 sui2;
699                 union samr_UserInfo u_info;
700                 struct samr_UserInfo21 *i21 = &u_info.info25.info;
701                 DATA_BLOB session_key;
702                 DATA_BLOB confounded_session_key = data_blob_talloc(
703                         mem_ctx, NULL, 16);
704                 struct MD5Context ctx;
705                 uint8_t confounder[16];
706
707                 ZERO_STRUCT(u_info);
708
709                 i21->full_name.string = talloc_asprintf(
710                         mem_ctx, "%s$",
711                         cli_credentials_get_workstation(wks_creds));
712                 i21->acct_flags = ACB_WSTRUST;
713                 i21->fields_present = SAMR_FIELD_FULL_NAME |
714                         SAMR_FIELD_ACCT_FLAGS | SAMR_FIELD_PASSWORD;
715
716                 encode_pw_buffer(u_info.info25.password.data,
717                                  cli_credentials_get_password(wks_creds),
718                                  STR_UNICODE);
719                 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
720                 if (!NT_STATUS_IS_OK(status)) {
721                         d_printf("dcerpc_fetch_session_key failed: %s\n",
722                                  nt_errstr(status));
723                         goto done;
724                 }
725                 generate_random_buffer((uint8_t *)confounder, 16);
726
727                 MD5Init(&ctx);
728                 MD5Update(&ctx, confounder, 16);
729                 MD5Update(&ctx, session_key.data, session_key.length);
730                 MD5Final(confounded_session_key.data, &ctx);
731
732                 arcfour_crypt_blob(u_info.info25.password.data, 516,
733                                    &confounded_session_key);
734                 memcpy(&u_info.info25.password.data[516], confounder, 16);
735
736                 sui2.in.user_handle = wks_handle;
737                 sui2.in.level = 25;
738                 sui2.in.info = &u_info;
739
740                 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
741                 if (!NT_STATUS_IS_OK(status)) {
742                         d_printf("samr_SetUserInfo2(25) failed: %s\n",
743                                  nt_errstr(status));
744                         goto done;
745                 }
746         } else {
747                 struct samr_SetUserInfo2 sui2;
748                 struct samr_SetUserInfo sui;
749                 union samr_UserInfo u_info;
750                 DATA_BLOB session_key;
751
752                 encode_pw_buffer(u_info.info24.password.data,
753                                  cli_credentials_get_password(wks_creds),
754                                  STR_UNICODE);
755                 u_info.info24.pw_len =
756                         strlen_m(cli_credentials_get_password(wks_creds))*2;
757
758                 status = dcerpc_fetch_session_key(samr_pipe, &session_key);
759                 if (!NT_STATUS_IS_OK(status)) {
760                         d_printf("dcerpc_fetch_session_key failed\n");
761                         goto done;
762                 }
763                 arcfour_crypt_blob(u_info.info24.password.data, 516,
764                                    &session_key);
765                 sui2.in.user_handle = wks_handle;
766                 sui2.in.info = &u_info;
767                 sui2.in.level = 24;
768
769                 status = dcerpc_samr_SetUserInfo2(samr_pipe, mem_ctx, &sui2);
770                 if (!NT_STATUS_IS_OK(status)) {
771                         d_printf("samr_SetUserInfo(24) failed: %s\n",
772                                  nt_errstr(status));
773                         goto done;
774                 }
775
776                 u_info.info16.acct_flags = ACB_WSTRUST;
777                 sui.in.user_handle = wks_handle;
778                 sui.in.info = &u_info;
779                 sui.in.level = 16;
780
781                 status = dcerpc_samr_SetUserInfo(samr_pipe, mem_ctx, &sui);
782                 if (!NT_STATUS_IS_OK(status)) {
783                         d_printf("samr_SetUserInfo(16) failed\n");
784                         goto done;
785                 }
786         }
787
788         ret = true;
789
790  done:
791         talloc_free(mem_ctx);
792         return ret;
793 }
794
795 /*
796  * Do a ReqChallenge/Auth2 and get the wks creds
797  */
798
799 static bool auth2(struct smbcli_state *cli,
800                   struct cli_credentials *wks_cred)
801 {
802         TALLOC_CTX *mem_ctx;
803         struct dcerpc_pipe *net_pipe;
804         bool result = false;
805         NTSTATUS status;
806         struct netr_ServerReqChallenge r;
807         struct netr_Credential netr_cli_creds;
808         struct netr_Credential netr_srv_creds;
809         uint32_t negotiate_flags;
810         struct netr_ServerAuthenticate2 a;
811         struct creds_CredentialState *creds_state;
812         struct netr_Credential netr_cred;
813         struct samr_Password mach_pw;
814
815         mem_ctx = talloc_new(NULL);
816         if (mem_ctx == NULL) {
817                 d_printf("talloc_new failed\n");
818                 return false;
819         }
820
821         net_pipe = dcerpc_pipe_init(mem_ctx,
822                                     cli->transport->socket->event.ctx,
823                                     lp_iconv_convenience(global_loadparm));
824         if (net_pipe == NULL) {
825                 d_printf("dcerpc_pipe_init failed\n");
826                 goto done;
827         }
828
829         status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
830         if (!NT_STATUS_IS_OK(status)) {
831                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
832                          nt_errstr(status));
833                 goto done;
834         }
835
836         status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
837         if (!NT_STATUS_IS_OK(status)) {
838                 d_printf("dcerpc_bind_auth_none failed: %s\n",
839                          nt_errstr(status));
840                 goto done;
841         }
842
843         r.in.computer_name = cli_credentials_get_workstation(wks_cred);
844         r.in.server_name = talloc_asprintf(
845                 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
846         if (r.in.server_name == NULL) {
847                 d_printf("talloc_asprintf failed\n");
848                 goto done;
849         }
850         generate_random_buffer(netr_cli_creds.data,
851                                sizeof(netr_cli_creds.data));
852         r.in.credentials = &netr_cli_creds;
853         r.out.credentials = &netr_srv_creds;
854
855         status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
856         if (!NT_STATUS_IS_OK(status)) {
857                 d_printf("netr_ServerReqChallenge failed: %s\n",
858                          nt_errstr(status));
859                 goto done;
860         }
861
862         negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
863         E_md4hash(cli_credentials_get_password(wks_cred), mach_pw.hash);
864
865         creds_state = talloc(mem_ctx, struct creds_CredentialState);
866         creds_client_init(creds_state, r.in.credentials,
867                           r.out.credentials, &mach_pw,
868                           &netr_cred, negotiate_flags);
869
870         a.in.server_name = talloc_asprintf(
871                 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
872         a.in.account_name = talloc_asprintf(
873                 mem_ctx, "%s$", cli_credentials_get_workstation(wks_cred));
874         a.in.computer_name = cli_credentials_get_workstation(wks_cred);
875         a.in.secure_channel_type = SEC_CHAN_WKSTA;
876         a.in.negotiate_flags = &negotiate_flags;
877         a.out.negotiate_flags = &negotiate_flags;
878         a.in.credentials = &netr_cred;
879         a.out.credentials = &netr_cred;
880
881         status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
882         if (!NT_STATUS_IS_OK(status)) {
883                 d_printf("netr_ServerServerAuthenticate2 failed: %s\n",
884                          nt_errstr(status));
885                 goto done;
886         }
887
888         if (!creds_client_check(creds_state, a.out.credentials)) {
889                 d_printf("creds_client_check failed\n");
890                 goto done;
891         }
892
893         cli_credentials_set_netlogon_creds(wks_cred, creds_state);
894
895         result = true;
896
897  done:
898         talloc_free(mem_ctx);
899         return result;
900 }
901
902 /*
903  * Do a couple of schannel protected Netlogon ops: Interactive and Network
904  * login, and change the wks password
905  */
906
907 static bool schan(struct smbcli_state *cli,
908                   struct loadparm_context *lp_ctx,
909                   struct cli_credentials *wks_creds,
910                   struct cli_credentials *user_creds)
911 {
912         TALLOC_CTX *mem_ctx;
913         NTSTATUS status;
914         bool ret = false;
915         struct dcerpc_pipe *net_pipe;
916         int i;
917         
918         mem_ctx = talloc_new(NULL);
919         if (mem_ctx == NULL) {
920                 d_printf("talloc_new failed\n");
921                 return false;
922         }
923
924         net_pipe = dcerpc_pipe_init(mem_ctx,
925                                     cli->transport->socket->event.ctx,
926                                     lp_iconv_convenience(lp_ctx));
927         if (net_pipe == NULL) {
928                 d_printf("dcerpc_pipe_init failed\n");
929                 goto done;
930         }
931
932         status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
933         if (!NT_STATUS_IS_OK(status)) {
934                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
935                          nt_errstr(status));
936                 goto done;
937         }
938
939 #if 0
940         net_pipe->conn->flags |= DCERPC_DEBUG_PRINT_IN |
941                 DCERPC_DEBUG_PRINT_OUT;
942 #endif
943 #if 1
944         net_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
945         status = dcerpc_bind_auth(net_pipe, &ndr_table_netlogon,
946                                   wks_creds, lp_ctx, DCERPC_AUTH_TYPE_SCHANNEL,
947                                   DCERPC_AUTH_LEVEL_PRIVACY,
948                                   NULL);
949 #else
950         status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
951 #endif
952         if (!NT_STATUS_IS_OK(status)) {
953                 d_printf("schannel bind failed: %s\n", nt_errstr(status));
954                 goto done;
955         }
956
957
958         for (i=2; i<4; i++) {
959                 int flags;
960                 DATA_BLOB chal, nt_resp, lm_resp, names_blob, session_key;
961                 struct creds_CredentialState *creds_state;
962                 struct netr_Authenticator netr_auth, netr_auth2;
963                 struct netr_NetworkInfo ninfo;
964                 struct netr_PasswordInfo pinfo;
965                 struct netr_LogonSamLogon r;
966
967                 flags = CLI_CRED_LANMAN_AUTH | CLI_CRED_NTLM_AUTH |
968                         CLI_CRED_NTLMv2_AUTH;
969
970                 chal = data_blob_talloc(mem_ctx, NULL, 8);
971                 if (chal.data == NULL) {
972                         d_printf("data_blob_talloc failed\n");
973                         goto done;
974                 }
975
976                 generate_random_buffer(chal.data, chal.length);
977                 names_blob = NTLMv2_generate_names_blob(
978                         mem_ctx, cli_credentials_get_workstation(user_creds),
979                         cli_credentials_get_domain(user_creds));
980                 status = cli_credentials_get_ntlm_response(
981                         user_creds, mem_ctx, &flags, chal, names_blob,
982                         &lm_resp, &nt_resp, NULL, NULL);
983                 if (!NT_STATUS_IS_OK(status)) {
984                         d_printf("cli_credentials_get_ntlm_response failed:"
985                                  " %s\n", nt_errstr(status));
986                         goto done;
987                 }
988
989                 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
990                 creds_client_authenticator(creds_state, &netr_auth);
991
992                 ninfo.identity_info.account_name.string =
993                         cli_credentials_get_username(user_creds);
994                 ninfo.identity_info.domain_name.string =
995                         cli_credentials_get_domain(user_creds);
996                 ninfo.identity_info.parameter_control = 0;
997                 ninfo.identity_info.logon_id_low = 0;
998                 ninfo.identity_info.logon_id_high = 0;
999                 ninfo.identity_info.workstation.string =
1000                         cli_credentials_get_workstation(user_creds);
1001                 memcpy(ninfo.challenge, chal.data, sizeof(ninfo.challenge));
1002                 ninfo.nt.length = nt_resp.length;
1003                 ninfo.nt.data = nt_resp.data;
1004                 ninfo.lm.length = lm_resp.length;
1005                 ninfo.lm.data = lm_resp.data;
1006
1007                 r.in.server_name = talloc_asprintf(
1008                         mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1009                 ZERO_STRUCT(netr_auth2);
1010                 r.in.computer_name =
1011                         cli_credentials_get_workstation(wks_creds);
1012                 r.in.credential = &netr_auth;
1013                 r.in.return_authenticator = &netr_auth2;
1014                 r.in.logon_level = 2;
1015                 r.in.validation_level = i;
1016                 r.in.logon.network = &ninfo;
1017                 r.out.return_authenticator = NULL;
1018
1019                 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1020                 if (!NT_STATUS_IS_OK(status)) {
1021                         d_printf("netr_LogonSamLogon failed: %s\n",
1022                                  nt_errstr(status));
1023                         goto done;
1024                 }
1025
1026                 if ((r.out.return_authenticator == NULL) ||
1027                     (!creds_client_check(creds_state,
1028                                          &r.out.return_authenticator->cred))) {
1029                         d_printf("Credentials check failed!\n");
1030                         goto done;
1031                 }
1032
1033                 creds_client_authenticator(creds_state, &netr_auth);
1034
1035                 pinfo.identity_info = ninfo.identity_info;
1036                 ZERO_STRUCT(pinfo.lmpassword.hash);
1037                 E_md4hash(cli_credentials_get_password(user_creds),
1038                           pinfo.ntpassword.hash);
1039                 session_key = data_blob_talloc(mem_ctx,
1040                                                creds_state->session_key, 16);
1041                 arcfour_crypt_blob(pinfo.ntpassword.hash,
1042                                    sizeof(pinfo.ntpassword.hash),
1043                                    &session_key);
1044
1045                 r.in.logon_level = 1;
1046                 r.in.logon.password = &pinfo;
1047                 r.out.return_authenticator = NULL;
1048
1049                 status = dcerpc_netr_LogonSamLogon(net_pipe, mem_ctx, &r);
1050                 if (!NT_STATUS_IS_OK(status)) {
1051                         d_printf("netr_LogonSamLogon failed: %s\n",
1052                                  nt_errstr(status));
1053                         goto done;
1054                 }
1055
1056                 if ((r.out.return_authenticator == NULL) ||
1057                     (!creds_client_check(creds_state,
1058                                          &r.out.return_authenticator->cred))) {
1059                         d_printf("Credentials check failed!\n");
1060                         goto done;
1061                 }
1062         }
1063
1064         {
1065                 struct netr_ServerPasswordSet s;
1066                 char *password = generate_random_str(wks_creds, 8);
1067                 struct creds_CredentialState *creds_state;
1068
1069                 s.in.server_name = talloc_asprintf(
1070                         mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1071                 s.in.computer_name = cli_credentials_get_workstation(wks_creds);
1072                 s.in.account_name = talloc_asprintf(
1073                         mem_ctx, "%s$", s.in.computer_name);
1074                 s.in.secure_channel_type = SEC_CHAN_WKSTA;
1075                 E_md4hash(password, s.in.new_password.hash);
1076
1077                 creds_state = cli_credentials_get_netlogon_creds(wks_creds);
1078                 creds_des_encrypt(creds_state, &s.in.new_password);
1079                 creds_client_authenticator(creds_state, &s.in.credential);
1080
1081                 status = dcerpc_netr_ServerPasswordSet(net_pipe, mem_ctx, &s);
1082                 if (!NT_STATUS_IS_OK(status)) {
1083                         printf("ServerPasswordSet - %s\n", nt_errstr(status));
1084                         goto done;
1085                 }
1086
1087                 if (!creds_client_check(creds_state,
1088                                         &s.out.return_authenticator.cred)) {
1089                         printf("Credential chaining failed\n");
1090                 }
1091
1092                 cli_credentials_set_password(wks_creds, password,
1093                                              CRED_SPECIFIED);
1094         }
1095
1096         ret = true;
1097  done:
1098         talloc_free(mem_ctx);
1099         return ret;
1100 }
1101
1102 /*
1103  * Delete the wks account again
1104  */
1105
1106 static bool leave(struct smbcli_state *cli,
1107                   struct loadparm_context *lp_ctx,
1108                   struct cli_credentials *admin_creds,
1109                   struct cli_credentials *wks_creds)
1110 {
1111         char *wks_name = talloc_asprintf(
1112                 NULL, "%s$", cli_credentials_get_workstation(wks_creds));
1113         bool ret;
1114
1115         ret = delete_user(cli, lp_ctx, admin_creds, wks_name);
1116         talloc_free(wks_name);
1117         return ret;
1118 }
1119
1120 /*
1121  * Test the Samba3 DC code a bit. Join, do some schan netlogon ops, leave
1122  */
1123
1124 bool torture_netlogon_samba3(struct torture_context *torture)
1125 {
1126         TALLOC_CTX *mem_ctx;
1127         NTSTATUS status;
1128         bool ret = false;
1129         struct smbcli_state *cli;
1130         struct cli_credentials *anon_creds;
1131         struct cli_credentials *wks_creds;
1132         const char *wks_name;
1133         int i;
1134
1135         wks_name = torture_setting_string(torture, "wksname", NULL);
1136         if (wks_name == NULL) {
1137                 wks_name = get_myname();
1138         }
1139
1140         mem_ctx = talloc_init("torture_netlogon_samba3");
1141
1142         if (mem_ctx == NULL) {
1143                 d_printf("talloc_init failed\n");
1144                 return false;
1145         }
1146
1147         if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1148                 d_printf("create_anon_creds failed\n");
1149                 goto done;
1150         }
1151
1152         status = smbcli_full_connection(mem_ctx, &cli,
1153                                         torture_setting_string(torture, "host", NULL),
1154                                         lp_smb_ports(torture->lp_ctx),
1155                                         "IPC$", NULL, anon_creds, NULL);
1156         if (!NT_STATUS_IS_OK(status)) {
1157                 d_printf("smbcli_full_connection failed: %s\n",
1158                          nt_errstr(status));
1159                 goto done;
1160         }
1161
1162         wks_creds = cli_credentials_init(mem_ctx);
1163         if (wks_creds == NULL) {
1164                 d_printf("cli_credentials_init failed\n");
1165                 goto done;
1166         }
1167
1168         cli_credentials_set_conf(wks_creds, torture->lp_ctx);
1169         cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1170         cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1171         cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1172         cli_credentials_set_password(wks_creds,
1173                                      generate_random_str(wks_creds, 8),
1174                                      CRED_SPECIFIED);
1175
1176         if (!join3(cli, torture->lp_ctx, false, cmdline_credentials, wks_creds)) {
1177                 d_printf("join failed\n");
1178                 goto done;
1179         }
1180
1181         cli_credentials_set_domain(
1182                 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1183                 CRED_SPECIFIED);
1184
1185         for (i=0; i<2; i++) {
1186
1187                 /* Do this more than once, the routine "schan" changes
1188                  * the workstation password using the netlogon
1189                  * password change routine */
1190
1191                 int j;
1192
1193                 if (!auth2(cli, wks_creds)) {
1194                         d_printf("auth2 failed\n");
1195                         goto done;
1196                 }
1197
1198                 for (j=0; j<2; j++) {
1199                         if (!schan(cli, torture->lp_ctx, wks_creds, cmdline_credentials)) {
1200                                 d_printf("schan failed\n");
1201                                 goto done;
1202                         }
1203                 }
1204         }
1205
1206         if (!leave(cli, torture->lp_ctx, cmdline_credentials, wks_creds)) {
1207                 d_printf("leave failed\n");
1208                 goto done;
1209         }
1210
1211         ret = true;
1212
1213  done:
1214         talloc_free(mem_ctx);
1215         return ret;
1216 }
1217
1218 /*
1219  * Do a simple join, testjoin and leave using specified smb and samr
1220  * credentials
1221  */
1222
1223 static bool test_join3(struct torture_context *tctx,
1224                        bool use_level25,
1225                        struct cli_credentials *smb_creds,
1226                        struct cli_credentials *samr_creds,
1227                        const char *wks_name)
1228 {
1229         NTSTATUS status;
1230         bool ret = false;
1231         struct smbcli_state *cli;
1232         struct cli_credentials *wks_creds;
1233
1234         status = smbcli_full_connection(tctx, &cli,
1235                                         torture_setting_string(tctx, "host", NULL),
1236                                         lp_smb_ports(tctx->lp_ctx),
1237                                         "IPC$", NULL, smb_creds, NULL);
1238         if (!NT_STATUS_IS_OK(status)) {
1239                 d_printf("smbcli_full_connection failed: %s\n",
1240                          nt_errstr(status));
1241                 goto done;
1242         }
1243
1244         wks_creds = cli_credentials_init(cli);
1245         if (wks_creds == NULL) {
1246                 d_printf("cli_credentials_init failed\n");
1247                 goto done;
1248         }
1249
1250         cli_credentials_set_conf(wks_creds, tctx->lp_ctx);
1251         cli_credentials_set_secure_channel_type(wks_creds, SEC_CHAN_WKSTA);
1252         cli_credentials_set_username(wks_creds, wks_name, CRED_SPECIFIED);
1253         cli_credentials_set_workstation(wks_creds, wks_name, CRED_SPECIFIED);
1254         cli_credentials_set_password(wks_creds,
1255                                      generate_random_str(wks_creds, 8),
1256                                      CRED_SPECIFIED);
1257
1258         if (!join3(cli, tctx->lp_ctx, use_level25, samr_creds, wks_creds)) {
1259                 d_printf("join failed\n");
1260                 goto done;
1261         }
1262
1263         cli_credentials_set_domain(
1264                 cmdline_credentials, cli_credentials_get_domain(wks_creds),
1265                 CRED_SPECIFIED);
1266
1267         if (!auth2(cli, wks_creds)) {
1268                 d_printf("auth2 failed\n");
1269                 goto done;
1270         }
1271
1272         if (!leave(cli, tctx->lp_ctx, samr_creds, wks_creds)) {
1273                 d_printf("leave failed\n");
1274                 goto done;
1275         }
1276
1277         talloc_free(cli);
1278
1279         ret = true;
1280
1281  done:
1282         return ret;
1283 }
1284
1285 /*
1286  * Test the different session key variants. Do it by joining, this uses the
1287  * session key in the setpassword routine. Test the join by doing the auth2.
1288  */
1289
1290 bool torture_samba3_sessionkey(struct torture_context *torture)
1291 {
1292         bool ret = false;
1293         struct cli_credentials *anon_creds;
1294         const char *wks_name;
1295
1296         wks_name = torture_setting_string(torture, "wksname", get_myname());
1297
1298         if (!(anon_creds = cli_credentials_init_anon(torture))) {
1299                 d_printf("create_anon_creds failed\n");
1300                 goto done;
1301         }
1302
1303         ret = true;
1304
1305         if (!torture_setting_bool(torture, "samba3", false)) {
1306
1307                 /* Samba3 in the build farm right now does this happily. Need
1308                  * to fix :-) */
1309
1310                 if (test_join3(torture, false, anon_creds, NULL, wks_name)) {
1311                         d_printf("join using anonymous bind on an anonymous smb "
1312                                  "connection succeeded -- HUH??\n");
1313                         ret = false;
1314                 }
1315         }
1316
1317         if (!test_join3(torture, false, anon_creds, cmdline_credentials,
1318                         wks_name)) {
1319                 d_printf("join using ntlmssp bind on an anonymous smb "
1320                          "connection failed\n");
1321                 ret = false;
1322         }
1323
1324         if (!test_join3(torture, false, cmdline_credentials, NULL, wks_name)) {
1325                 d_printf("join using anonymous bind on an authenticated smb "
1326                          "connection failed\n");
1327                 ret = false;
1328         }
1329
1330         if (!test_join3(torture, false, cmdline_credentials,
1331                         cmdline_credentials,
1332                         wks_name)) {
1333                 d_printf("join using ntlmssp bind on an authenticated smb "
1334                          "connection failed\n");
1335                 ret = false;
1336         }
1337
1338         /*
1339          * The following two are tests for setuserinfolevel 25
1340          */
1341
1342         if (!test_join3(torture, true, anon_creds, cmdline_credentials,
1343                         wks_name)) {
1344                 d_printf("join using ntlmssp bind on an anonymous smb "
1345                          "connection failed\n");
1346                 ret = false;
1347         }
1348
1349         if (!test_join3(torture, true, cmdline_credentials, NULL, wks_name)) {
1350                 d_printf("join using anonymous bind on an authenticated smb "
1351                          "connection failed\n");
1352                 ret = false;
1353         }
1354
1355  done:
1356
1357         return ret;
1358 }
1359
1360 /*
1361  * open pipe and bind, given an IPC$ context
1362  */
1363
1364 static NTSTATUS pipe_bind_smb(TALLOC_CTX *mem_ctx,
1365                               struct smbcli_tree *tree,
1366                               const char *pipe_name,
1367                               const struct ndr_interface_table *iface,
1368                               struct dcerpc_pipe **p)
1369 {
1370         struct dcerpc_pipe *result;
1371         NTSTATUS status;
1372
1373         if (!(result = dcerpc_pipe_init(
1374                       mem_ctx, tree->session->transport->socket->event.ctx, 
1375                       lp_iconv_convenience(global_loadparm)))) {
1376                 return NT_STATUS_NO_MEMORY;
1377         }
1378
1379         status = dcerpc_pipe_open_smb(result, tree, pipe_name);
1380         if (!NT_STATUS_IS_OK(status)) {
1381                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1382                          nt_errstr(status));
1383                 talloc_free(result);
1384                 return status;
1385         }
1386
1387         status = dcerpc_bind_auth_none(result, iface);
1388         if (!NT_STATUS_IS_OK(status)) {
1389                 d_printf("schannel bind failed: %s\n", nt_errstr(status));
1390                 talloc_free(result);
1391                 return status;
1392         }
1393
1394         *p = result;
1395         return NT_STATUS_OK;
1396 }
1397
1398 /*
1399  * Sane wrapper around lsa_LookupNames
1400  */
1401
1402 static struct dom_sid *name2sid(TALLOC_CTX *mem_ctx,
1403                                 struct dcerpc_pipe *p,
1404                                 const char *name,
1405                                 const char *domain)
1406 {
1407         struct lsa_ObjectAttribute attr;
1408         struct lsa_QosInfo qos;
1409         struct lsa_OpenPolicy2 r;
1410         struct lsa_Close c;
1411         NTSTATUS status;
1412         struct policy_handle handle;
1413         struct lsa_LookupNames l;
1414         struct lsa_TransSidArray sids;
1415         struct lsa_String lsa_name;
1416         uint32_t count = 0;
1417         struct dom_sid *result;
1418         TALLOC_CTX *tmp_ctx;
1419
1420         if (!(tmp_ctx = talloc_new(mem_ctx))) {
1421                 return NULL;
1422         }
1423
1424         qos.len = 0;
1425         qos.impersonation_level = 2;
1426         qos.context_mode = 1;
1427         qos.effective_only = 0;
1428
1429         attr.len = 0;
1430         attr.root_dir = NULL;
1431         attr.object_name = NULL;
1432         attr.attributes = 0;
1433         attr.sec_desc = NULL;
1434         attr.sec_qos = &qos;
1435
1436         r.in.system_name = "\\";
1437         r.in.attr = &attr;
1438         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1439         r.out.handle = &handle;
1440
1441         status = dcerpc_lsa_OpenPolicy2(p, tmp_ctx, &r);
1442         if (!NT_STATUS_IS_OK(status)) {
1443                 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1444                 talloc_free(tmp_ctx);
1445                 return NULL;
1446         }
1447
1448         sids.count = 0;
1449         sids.sids = NULL;
1450
1451         lsa_name.string = talloc_asprintf(tmp_ctx, "%s\\%s", domain, name);
1452
1453         l.in.handle = &handle;
1454         l.in.num_names = 1;
1455         l.in.names = &lsa_name;
1456         l.in.sids = &sids;
1457         l.in.level = 1;
1458         l.in.count = &count;
1459         l.out.count = &count;
1460         l.out.sids = &sids;
1461
1462         status = dcerpc_lsa_LookupNames(p, tmp_ctx, &l);
1463         if (!NT_STATUS_IS_OK(status)) {
1464                 printf("LookupNames of %s failed - %s\n", lsa_name.string, 
1465                        nt_errstr(status));
1466                 talloc_free(tmp_ctx);
1467                 return NULL;
1468         }
1469
1470         result = dom_sid_add_rid(mem_ctx, l.out.domains->domains[0].sid,
1471                                  l.out.sids->sids[0].rid);
1472
1473         c.in.handle = &handle;
1474         c.out.handle = &handle;
1475
1476         status = dcerpc_lsa_Close(p, tmp_ctx, &c);
1477         if (!NT_STATUS_IS_OK(status)) {
1478                 printf("dcerpc_lsa_Close failed - %s\n", nt_errstr(status));
1479                 talloc_free(tmp_ctx);
1480                 return NULL;
1481         }
1482         
1483         talloc_free(tmp_ctx);
1484         return result;
1485 }
1486
1487 /*
1488  * Find out the user SID on this connection
1489  */
1490
1491 static struct dom_sid *whoami(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree)
1492 {
1493         struct dcerpc_pipe *lsa;
1494         struct lsa_GetUserName r;
1495         NTSTATUS status;
1496         struct lsa_StringPointer authority_name_p;
1497         struct dom_sid *result;
1498
1499         status = pipe_bind_smb(mem_ctx, tree, "\\pipe\\lsarpc",
1500                                &ndr_table_lsarpc, &lsa);
1501         if (!NT_STATUS_IS_OK(status)) {
1502                 d_printf("(%s) Could not bind to LSA: %s\n",
1503                          __location__, nt_errstr(status));
1504                 return NULL;
1505         }
1506
1507         r.in.system_name = "\\";
1508         r.in.account_name = NULL;
1509         authority_name_p.string = NULL;
1510         r.in.authority_name = &authority_name_p;
1511
1512         status = dcerpc_lsa_GetUserName(lsa, mem_ctx, &r);
1513
1514         if (!NT_STATUS_IS_OK(status)) {
1515                 printf("(%s) GetUserName failed - %s\n",
1516                        __location__, nt_errstr(status));
1517                 talloc_free(lsa);
1518                 return NULL;
1519         }
1520
1521         result = name2sid(mem_ctx, lsa, r.out.account_name->string,
1522                           r.out.authority_name->string->string);
1523
1524         talloc_free(lsa);
1525         return result;
1526 }
1527
1528 static int destroy_tree(struct smbcli_tree *tree)
1529 {
1530         smb_tree_disconnect(tree);
1531         return 0;
1532 }
1533
1534 /*
1535  * Do a tcon, given a session
1536  */
1537
1538 NTSTATUS secondary_tcon(TALLOC_CTX *mem_ctx,
1539                         struct smbcli_session *session,
1540                         const char *sharename,
1541                         struct smbcli_tree **res)
1542 {
1543         struct smbcli_tree *result;
1544         TALLOC_CTX *tmp_ctx;
1545         union smb_tcon tcon;
1546         NTSTATUS status;
1547
1548         if (!(tmp_ctx = talloc_new(mem_ctx))) {
1549                 return NT_STATUS_NO_MEMORY;
1550         }
1551
1552         if (!(result = smbcli_tree_init(session, mem_ctx, false))) {
1553                 talloc_free(tmp_ctx);
1554                 return NT_STATUS_NO_MEMORY;
1555         }
1556
1557         tcon.generic.level = RAW_TCON_TCONX;
1558         tcon.tconx.in.flags = 0;
1559         tcon.tconx.in.password = data_blob(NULL, 0);
1560         tcon.tconx.in.path = sharename;
1561         tcon.tconx.in.device = "?????";
1562
1563         status = smb_raw_tcon(result, tmp_ctx, &tcon);
1564         if (!NT_STATUS_IS_OK(status)) {
1565                 d_printf("(%s) smb_raw_tcon failed: %s\n", __location__,
1566                          nt_errstr(status));
1567                 talloc_free(tmp_ctx);
1568                 return status;
1569         }
1570
1571         result->tid = tcon.tconx.out.tid;
1572         result = talloc_steal(mem_ctx, result);
1573         talloc_set_destructor(result, destroy_tree);
1574         talloc_free(tmp_ctx);
1575         *res = result;
1576         return NT_STATUS_OK;
1577 }
1578
1579 /*
1580  * Test the getusername behaviour
1581  */
1582
1583 bool torture_samba3_rpc_getusername(struct torture_context *torture)
1584 {
1585         NTSTATUS status;
1586         struct smbcli_state *cli;
1587         TALLOC_CTX *mem_ctx;
1588         bool ret = true;
1589         struct dom_sid *user_sid;
1590         struct dom_sid *created_sid;
1591         struct cli_credentials *anon_creds;
1592         struct cli_credentials *user_creds;
1593         char *domain_name;
1594
1595         if (!(mem_ctx = talloc_new(torture))) {
1596                 return false;
1597         }
1598
1599         status = smbcli_full_connection(
1600                 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1601                 lp_smb_ports(torture->lp_ctx),
1602                 "IPC$", NULL, cmdline_credentials, NULL);
1603         if (!NT_STATUS_IS_OK(status)) {
1604                 d_printf("(%s) smbcli_full_connection failed: %s\n",
1605                          __location__, nt_errstr(status));
1606                 ret = false;
1607                 goto done;
1608         }
1609
1610         if (!(user_sid = whoami(mem_ctx, cli->tree))) {
1611                 d_printf("(%s) whoami on auth'ed connection failed\n",
1612                          __location__);
1613                 ret = false;
1614         }
1615
1616         talloc_free(cli);
1617
1618         if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
1619                 d_printf("(%s) create_anon_creds failed\n", __location__);
1620                 ret = false;
1621                 goto done;
1622         }
1623
1624         status = smbcli_full_connection(
1625                 mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
1626                 lp_smb_ports(torture->lp_ctx),
1627                 "IPC$", NULL, anon_creds, NULL);
1628         if (!NT_STATUS_IS_OK(status)) {
1629                 d_printf("(%s) anon smbcli_full_connection failed: %s\n",
1630                          __location__, nt_errstr(status));
1631                 ret = false;
1632                 goto done;
1633         }
1634
1635         if (!(user_sid = whoami(mem_ctx, cli->tree))) {
1636                 d_printf("(%s) whoami on anon connection failed\n",
1637                          __location__);
1638                 ret = false;
1639                 goto done;
1640         }
1641
1642         if (!dom_sid_equal(user_sid,
1643                            dom_sid_parse_talloc(mem_ctx, "s-1-5-7"))) {
1644                 d_printf("(%s) Anon lsa_GetUserName returned %s, expected "
1645                          "S-1-5-7", __location__,
1646                          dom_sid_string(mem_ctx, user_sid));
1647                 ret = false;
1648         }
1649
1650         if (!(user_creds = cli_credentials_init(mem_ctx))) {
1651                 d_printf("(%s) cli_credentials_init failed\n", __location__);
1652                 ret = false;
1653                 goto done;
1654         }
1655
1656         cli_credentials_set_conf(user_creds, torture->lp_ctx);
1657         cli_credentials_set_username(user_creds, "torture_username",
1658                                      CRED_SPECIFIED);
1659         cli_credentials_set_password(user_creds,
1660                                      generate_random_str(user_creds, 8),
1661                                      CRED_SPECIFIED);
1662
1663         if (!create_user(mem_ctx, cli, torture->lp_ctx, cmdline_credentials,
1664                          cli_credentials_get_username(user_creds),
1665                          cli_credentials_get_password(user_creds),
1666                          &domain_name, &created_sid)) {
1667                 d_printf("(%s) create_user failed\n", __location__);
1668                 ret = false;
1669                 goto done;
1670         }
1671
1672         cli_credentials_set_domain(user_creds, domain_name,
1673                                    CRED_SPECIFIED);
1674
1675         {
1676                 struct smbcli_session *session2;
1677                 struct smb_composite_sesssetup setup;
1678                 struct smbcli_tree *tree;
1679
1680                 session2 = smbcli_session_init(cli->transport, mem_ctx, false);
1681                 if (session2 == NULL) {
1682                         d_printf("(%s) smbcli_session_init failed\n",
1683                                  __location__);
1684                         goto done;
1685                 }
1686
1687                 setup.in.sesskey = cli->transport->negotiate.sesskey;
1688                 setup.in.capabilities = cli->transport->negotiate.capabilities;
1689                 setup.in.workgroup = "";
1690                 setup.in.credentials = user_creds;
1691
1692                 status = smb_composite_sesssetup(session2, &setup);
1693                 if (!NT_STATUS_IS_OK(status)) {
1694                         d_printf("(%s) session setup with new user failed: "
1695                                  "%s\n", __location__, nt_errstr(status));
1696                         ret = false;
1697                         goto done;
1698                 }
1699                 session2->vuid = setup.out.vuid;
1700
1701                 if (!NT_STATUS_IS_OK(secondary_tcon(mem_ctx, session2,
1702                                                     "IPC$", &tree))) {
1703                         d_printf("(%s) secondary_tcon failed\n",
1704                                  __location__);
1705                         ret = false;
1706                         goto done;
1707                 }
1708
1709                 if (!(user_sid = whoami(mem_ctx, tree))) {
1710                         d_printf("(%s) whoami on user connection failed\n",
1711                                  __location__);
1712                         ret = false;
1713                         goto delete;
1714                 }
1715
1716                 talloc_free(tree);
1717         }
1718
1719         d_printf("Created %s, found %s\n",
1720                  dom_sid_string(mem_ctx, created_sid),
1721                  dom_sid_string(mem_ctx, user_sid));
1722
1723         if (!dom_sid_equal(created_sid, user_sid)) {
1724                 ret = false;
1725         }
1726
1727  delete:
1728         if (!delete_user(cli, torture->lp_ctx, 
1729                          cmdline_credentials,
1730                          cli_credentials_get_username(user_creds))) {
1731                 d_printf("(%s) delete_user failed\n", __location__);
1732                 ret = false;
1733         }
1734
1735  done:
1736         talloc_free(mem_ctx);
1737         return ret;
1738 }
1739
1740 static bool test_NetShareGetInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1741                                  const char *sharename)
1742 {
1743         NTSTATUS status;
1744         struct srvsvc_NetShareGetInfo r;
1745         uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007, 1501 };
1746         int i;
1747         bool ret = true;
1748
1749         r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
1750                                           dcerpc_server_name(p));
1751         r.in.share_name = sharename;
1752
1753         for (i=0;i<ARRAY_SIZE(levels);i++) {
1754                 r.in.level = levels[i];
1755
1756                 ZERO_STRUCT(r.out);
1757
1758                 printf("testing NetShareGetInfo level %u on share '%s'\n", 
1759                        r.in.level, r.in.share_name);
1760
1761                 status = dcerpc_srvsvc_NetShareGetInfo(p, mem_ctx, &r);
1762                 if (!NT_STATUS_IS_OK(status)) {
1763                         printf("NetShareGetInfo level %u on share '%s' failed"
1764                                " - %s\n", r.in.level, r.in.share_name,
1765                                nt_errstr(status));
1766                         ret = false;
1767                         continue;
1768                 }
1769                 if (!W_ERROR_IS_OK(r.out.result)) {
1770                         printf("NetShareGetInfo level %u on share '%s' failed "
1771                                "- %s\n", r.in.level, r.in.share_name,
1772                                win_errstr(r.out.result));
1773                         ret = false;
1774                         continue;
1775                 }
1776         }
1777
1778         return ret;
1779 }
1780
1781 static bool test_NetShareEnum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1782                               const char **one_sharename)
1783 {
1784         NTSTATUS status;
1785         struct srvsvc_NetShareEnum r;
1786         struct srvsvc_NetShareCtr0 c0;
1787         uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007 };
1788         int i;
1789         bool ret = true;
1790
1791         r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
1792         r.in.ctr.ctr0 = &c0;
1793         r.in.ctr.ctr0->count = 0;
1794         r.in.ctr.ctr0->array = NULL;
1795         r.in.max_buffer = (uint32_t)-1;
1796         r.in.resume_handle = NULL;
1797
1798         for (i=0;i<ARRAY_SIZE(levels);i++) {
1799                 r.in.level = levels[i];
1800
1801                 ZERO_STRUCT(r.out);
1802
1803                 printf("testing NetShareEnum level %u\n", r.in.level);
1804                 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
1805                 if (!NT_STATUS_IS_OK(status)) {
1806                         printf("NetShareEnum level %u failed - %s\n",
1807                                r.in.level, nt_errstr(status));
1808                         ret = false;
1809                         continue;
1810                 }
1811                 if (!W_ERROR_IS_OK(r.out.result)) {
1812                         printf("NetShareEnum level %u failed - %s\n",
1813                                r.in.level, win_errstr(r.out.result));
1814                         continue;
1815                 }
1816                 if (r.in.level == 0) {
1817                         struct srvsvc_NetShareCtr0 *ctr = r.out.ctr.ctr0;
1818                         if (ctr->count > 0) {
1819                                 *one_sharename = ctr->array[0].name;
1820                         }
1821                 }
1822         }
1823
1824         return ret;
1825 }
1826
1827 bool torture_samba3_rpc_srvsvc(struct torture_context *torture)
1828 {
1829         struct dcerpc_pipe *p;
1830         TALLOC_CTX *mem_ctx;
1831         bool ret = true;
1832         const char *sharename = NULL;
1833         struct smbcli_state *cli;
1834         NTSTATUS status;
1835
1836         if (!(mem_ctx = talloc_new(torture))) {
1837                 return false;
1838         }
1839
1840         if (!(torture_open_connection_share(
1841                       mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
1842                       "IPC$", NULL))) {
1843                 talloc_free(mem_ctx);
1844                 return false;
1845         }
1846
1847         status = pipe_bind_smb(mem_ctx, cli->tree, "\\pipe\\srvsvc",
1848                                &ndr_table_srvsvc, &p);
1849         if (!NT_STATUS_IS_OK(status)) {
1850                 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
1851                          __location__, nt_errstr(status));
1852                 ret = false;
1853                 goto done;
1854         }
1855
1856         ret &= test_NetShareEnum(p, mem_ctx, &sharename);
1857         if (sharename == NULL) {
1858                 printf("did not get sharename\n");
1859         } else {
1860                 ret &= test_NetShareGetInfo(p, mem_ctx, sharename);
1861         }
1862
1863  done:
1864         talloc_free(mem_ctx);
1865         return ret;
1866 }
1867
1868 /*
1869  * Do a ReqChallenge/Auth2 with a random wks name, make sure it returns
1870  * NT_STATUS_NO_SAM_ACCOUNT
1871  */
1872
1873 bool torture_samba3_rpc_randomauth2(struct torture_context *torture)
1874 {
1875         TALLOC_CTX *mem_ctx;
1876         struct dcerpc_pipe *net_pipe;
1877         char *wksname;
1878         bool result = false;
1879         NTSTATUS status;
1880         struct netr_ServerReqChallenge r;
1881         struct netr_Credential netr_cli_creds;
1882         struct netr_Credential netr_srv_creds;
1883         uint32_t negotiate_flags;
1884         struct netr_ServerAuthenticate2 a;
1885         struct creds_CredentialState *creds_state;
1886         struct netr_Credential netr_cred;
1887         struct samr_Password mach_pw;
1888         struct smbcli_state *cli;
1889
1890         if (!(mem_ctx = talloc_new(torture))) {
1891                 d_printf("talloc_new failed\n");
1892                 return false;
1893         }
1894
1895         if (!(wksname = generate_random_str_list(
1896                       mem_ctx, 14, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))) {
1897                 d_printf("generate_random_str_list failed\n");
1898                 goto done;
1899         }
1900
1901         if (!(torture_open_connection_share(
1902                       mem_ctx, &cli,
1903                       torture, torture_setting_string(torture, "host", NULL),
1904                       "IPC$", NULL))) {
1905                 d_printf("IPC$ connection failed\n");
1906                 goto done;
1907         }
1908
1909         if (!(net_pipe = dcerpc_pipe_init(
1910                       mem_ctx, cli->transport->socket->event.ctx,
1911                       lp_iconv_convenience(torture->lp_ctx)))) {
1912                 d_printf("dcerpc_pipe_init failed\n");
1913                 goto done;
1914         }
1915
1916         status = dcerpc_pipe_open_smb(net_pipe, cli->tree, "\\netlogon");
1917         if (!NT_STATUS_IS_OK(status)) {
1918                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
1919                          nt_errstr(status));
1920                 goto done;
1921         }
1922
1923         status = dcerpc_bind_auth_none(net_pipe, &ndr_table_netlogon);
1924         if (!NT_STATUS_IS_OK(status)) {
1925                 d_printf("dcerpc_bind_auth_none failed: %s\n",
1926                          nt_errstr(status));
1927                 goto done;
1928         }
1929
1930         r.in.computer_name = wksname;
1931         r.in.server_name = talloc_asprintf(
1932                 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1933         if (r.in.server_name == NULL) {
1934                 d_printf("talloc_asprintf failed\n");
1935                 goto done;
1936         }
1937         generate_random_buffer(netr_cli_creds.data,
1938                                sizeof(netr_cli_creds.data));
1939         r.in.credentials = &netr_cli_creds;
1940         r.out.credentials = &netr_srv_creds;
1941
1942         status = dcerpc_netr_ServerReqChallenge(net_pipe, mem_ctx, &r);
1943         if (!NT_STATUS_IS_OK(status)) {
1944                 d_printf("netr_ServerReqChallenge failed: %s\n",
1945                          nt_errstr(status));
1946                 goto done;
1947         }
1948
1949         negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
1950         E_md4hash("foobar", mach_pw.hash);
1951
1952         creds_state = talloc(mem_ctx, struct creds_CredentialState);
1953         creds_client_init(creds_state, r.in.credentials,
1954                           r.out.credentials, &mach_pw,
1955                           &netr_cred, negotiate_flags);
1956
1957         a.in.server_name = talloc_asprintf(
1958                 mem_ctx, "\\\\%s", dcerpc_server_name(net_pipe));
1959         a.in.account_name = talloc_asprintf(
1960                 mem_ctx, "%s$", wksname);
1961         a.in.computer_name = wksname;
1962         a.in.secure_channel_type = SEC_CHAN_WKSTA;
1963         a.in.negotiate_flags = &negotiate_flags;
1964         a.out.negotiate_flags = &negotiate_flags;
1965         a.in.credentials = &netr_cred;
1966         a.out.credentials = &netr_cred;
1967
1968         status = dcerpc_netr_ServerAuthenticate2(net_pipe, mem_ctx, &a);
1969
1970         if (!NT_STATUS_EQUAL(status, NT_STATUS_NO_TRUST_SAM_ACCOUNT)) {
1971                 d_printf("dcerpc_netr_ServerAuthenticate2 returned %s, "
1972                          "expected NT_STATUS_NO_TRUST_SAM_ACCOUNT\n",
1973                          nt_errstr(status));
1974                 goto done;
1975         }
1976
1977         result = true;
1978  done:
1979         talloc_free(mem_ctx);
1980         return result;
1981 }
1982
1983 static struct security_descriptor *get_sharesec(TALLOC_CTX *mem_ctx,
1984                                                 struct smbcli_session *sess,
1985                                                 const char *sharename)
1986 {
1987         struct smbcli_tree *tree;
1988         TALLOC_CTX *tmp_ctx;
1989         struct dcerpc_pipe *p;
1990         NTSTATUS status;
1991         struct srvsvc_NetShareGetInfo r;
1992         struct security_descriptor *result;
1993
1994         if (!(tmp_ctx = talloc_new(mem_ctx))) {
1995                 d_printf("talloc_new failed\n");
1996                 return NULL;
1997         }
1998
1999         if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2000                 d_printf("secondary_tcon failed\n");
2001                 talloc_free(tmp_ctx);
2002                 return NULL;
2003         }
2004
2005         status = pipe_bind_smb(mem_ctx, tree, "\\pipe\\srvsvc",
2006                                &ndr_table_srvsvc, &p);
2007         if (!NT_STATUS_IS_OK(status)) {
2008                 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2009                          __location__, nt_errstr(status));
2010                 talloc_free(tmp_ctx);
2011                 return NULL;
2012         }
2013
2014 #if 0
2015         p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2016 #endif
2017
2018         r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2019                                           dcerpc_server_name(p));
2020         r.in.share_name = sharename;
2021         r.in.level = 502;
2022
2023         status = dcerpc_srvsvc_NetShareGetInfo(p, tmp_ctx, &r);
2024         if (!NT_STATUS_IS_OK(status)) {
2025                 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
2026                          nt_errstr(status));
2027                 talloc_free(tmp_ctx);
2028                 return NULL;
2029         }
2030
2031         result = talloc_steal(mem_ctx, r.out.info.info502->sd);
2032         talloc_free(tmp_ctx);
2033         return result;
2034 }
2035
2036 static NTSTATUS set_sharesec(TALLOC_CTX *mem_ctx,
2037                              struct smbcli_session *sess,
2038                              const char *sharename,
2039                              struct security_descriptor *sd)
2040 {
2041         struct smbcli_tree *tree;
2042         TALLOC_CTX *tmp_ctx;
2043         struct dcerpc_pipe *p;
2044         NTSTATUS status;
2045         struct sec_desc_buf i;
2046         struct srvsvc_NetShareSetInfo r;
2047         uint32_t error = 0;
2048
2049         if (!(tmp_ctx = talloc_new(mem_ctx))) {
2050                 d_printf("talloc_new failed\n");
2051                 return NT_STATUS_NO_MEMORY;
2052         }
2053
2054         if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
2055                 d_printf("secondary_tcon failed\n");
2056                 talloc_free(tmp_ctx);
2057                 return NT_STATUS_UNSUCCESSFUL;
2058         }
2059
2060         status = pipe_bind_smb(mem_ctx, tree, "\\pipe\\srvsvc",
2061                                &ndr_table_srvsvc, &p);
2062         if (!NT_STATUS_IS_OK(status)) {
2063                 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
2064                          __location__, nt_errstr(status));
2065                 talloc_free(tmp_ctx);
2066                 return NT_STATUS_UNSUCCESSFUL;
2067         }
2068
2069 #if 0
2070         p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2071 #endif
2072
2073         r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
2074                                           dcerpc_server_name(p));
2075         r.in.share_name = sharename;
2076         r.in.level = 1501;
2077         i.sd = sd;
2078         r.in.info.info1501 = &i;
2079         r.in.parm_error = &error;
2080
2081         status = dcerpc_srvsvc_NetShareSetInfo(p, tmp_ctx, &r);
2082         if (!NT_STATUS_IS_OK(status)) {
2083                 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
2084                          nt_errstr(status));
2085         }
2086
2087         talloc_free(tmp_ctx);
2088         return status;
2089 }
2090
2091 bool try_tcon(TALLOC_CTX *mem_ctx,
2092               struct security_descriptor *orig_sd,
2093               struct smbcli_session *session,
2094               const char *sharename, const struct dom_sid *user_sid,
2095               unsigned int access_mask, NTSTATUS expected_tcon,
2096               NTSTATUS expected_mkdir)
2097 {
2098         TALLOC_CTX *tmp_ctx;
2099         struct smbcli_tree *rmdir_tree, *tree;
2100         struct dom_sid *domain_sid;
2101         uint32_t rid;
2102         struct security_descriptor *sd;
2103         NTSTATUS status;
2104         bool ret = true;
2105
2106         if (!(tmp_ctx = talloc_new(mem_ctx))) {
2107                 d_printf("talloc_new failed\n");
2108                 return false;
2109         }
2110
2111         status = secondary_tcon(tmp_ctx, session, sharename, &rmdir_tree);
2112         if (!NT_STATUS_IS_OK(status)) {
2113                 d_printf("first tcon to delete dir failed\n");
2114                 talloc_free(tmp_ctx);
2115                 return false;
2116         }
2117
2118         smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2119
2120         if (!NT_STATUS_IS_OK(dom_sid_split_rid(tmp_ctx, user_sid,
2121                                                &domain_sid, &rid))) {
2122                 d_printf("dom_sid_split_rid failed\n");
2123                 talloc_free(tmp_ctx);
2124                 return false;
2125         }
2126
2127         sd = security_descriptor_dacl_create(
2128                 tmp_ctx, 0, "S-1-5-32-544",
2129                 dom_sid_string(mem_ctx, dom_sid_add_rid(mem_ctx, domain_sid,
2130                                                         DOMAIN_RID_USERS)),
2131                 dom_sid_string(mem_ctx, user_sid),
2132                 SEC_ACE_TYPE_ACCESS_ALLOWED, access_mask, 0, NULL);
2133         if (sd == NULL) {
2134                 d_printf("security_descriptor_dacl_create failed\n");
2135                 talloc_free(tmp_ctx);
2136                 return false;
2137         }
2138
2139         status = set_sharesec(mem_ctx, session, sharename, sd);
2140         if (!NT_STATUS_IS_OK(status)) {
2141                 d_printf("custom set_sharesec failed: %s\n",
2142                          nt_errstr(status));
2143                 talloc_free(tmp_ctx);
2144                 return false;
2145         }
2146
2147         status = secondary_tcon(tmp_ctx, session, sharename, &tree);
2148         if (!NT_STATUS_EQUAL(status, expected_tcon)) {
2149                 d_printf("Expected %s, got %s\n", nt_errstr(expected_tcon),
2150                          nt_errstr(status));
2151                 ret = false;
2152                 goto done;
2153         }
2154
2155         if (!NT_STATUS_IS_OK(status)) {
2156                 /* An expected non-access, no point in trying to write */
2157                 goto done;
2158         }
2159
2160         status = smbcli_mkdir(tree, "sharesec_testdir");
2161         if (!NT_STATUS_EQUAL(status, expected_mkdir)) {
2162                 d_printf("(%s) Expected %s, got %s\n", __location__,
2163                          nt_errstr(expected_mkdir), nt_errstr(status));
2164                 ret = false;
2165         }
2166
2167  done:
2168         smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2169
2170         status = set_sharesec(mem_ctx, session, sharename, orig_sd);
2171         if (!NT_STATUS_IS_OK(status)) {
2172                 d_printf("custom set_sharesec failed: %s\n",
2173                          nt_errstr(status));
2174                 talloc_free(tmp_ctx);
2175                 return false;
2176         }
2177
2178         talloc_free(tmp_ctx);
2179         return ret;
2180 }
2181
2182 bool torture_samba3_rpc_sharesec(struct torture_context *torture)
2183 {
2184         TALLOC_CTX *mem_ctx;
2185         bool ret = true;
2186         struct smbcli_state *cli;
2187         struct security_descriptor *sd;
2188         struct dom_sid *user_sid;
2189
2190         if (!(mem_ctx = talloc_new(torture))) {
2191                 return false;
2192         }
2193
2194         if (!(torture_open_connection_share(
2195                       mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2196                       "IPC$", NULL))) {
2197                 d_printf("IPC$ connection failed\n");
2198                 talloc_free(mem_ctx);
2199                 return false;
2200         }
2201
2202         if (!(user_sid = whoami(mem_ctx, cli->tree))) {
2203                 d_printf("whoami failed\n");
2204                 talloc_free(mem_ctx);
2205                 return false;
2206         }
2207
2208         sd = get_sharesec(mem_ctx, cli->session, torture_setting_string(torture,
2209                                                                 "share", NULL));
2210
2211         ret &= try_tcon(mem_ctx, sd, cli->session,
2212                         torture_setting_string(torture, "share", NULL),
2213                         user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK);
2214
2215         ret &= try_tcon(mem_ctx, sd, cli->session,
2216                         torture_setting_string(torture, "share", NULL),
2217                         user_sid, SEC_FILE_READ_DATA, NT_STATUS_OK,
2218                         NT_STATUS_MEDIA_WRITE_PROTECTED);
2219
2220         ret &= try_tcon(mem_ctx, sd, cli->session,
2221                         torture_setting_string(torture, "share", NULL),
2222                         user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK);
2223
2224         talloc_free(mem_ctx);
2225         return ret;
2226 }
2227
2228 bool torture_samba3_rpc_lsa(struct torture_context *torture)
2229 {
2230         TALLOC_CTX *mem_ctx;
2231         bool ret = true;
2232         struct smbcli_state *cli;
2233         struct dcerpc_pipe *p;
2234         struct policy_handle lsa_handle;
2235         NTSTATUS status;
2236         struct dom_sid *domain_sid;
2237
2238         if (!(mem_ctx = talloc_new(torture))) {
2239                 return false;
2240         }
2241
2242         if (!(torture_open_connection_share(
2243                       mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2244                       "IPC$", NULL))) {
2245                 d_printf("IPC$ connection failed\n");
2246                 talloc_free(mem_ctx);
2247                 return false;
2248         }
2249
2250         status = pipe_bind_smb(mem_ctx, cli->tree, "\\lsarpc",
2251                                &ndr_table_lsarpc, &p);
2252         if (!NT_STATUS_IS_OK(status)) {
2253                 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2254                          nt_errstr(status));
2255                 talloc_free(mem_ctx);
2256                 return false;
2257         }
2258
2259         {
2260                 struct lsa_ObjectAttribute attr;
2261                 struct lsa_OpenPolicy2 o;
2262                 o.in.system_name = talloc_asprintf(
2263                         mem_ctx, "\\\\%s", dcerpc_server_name(p));
2264                 ZERO_STRUCT(attr);
2265                 o.in.attr = &attr;
2266                 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2267                 o.out.handle = &lsa_handle;
2268                 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &o);
2269                 if (!NT_STATUS_IS_OK(status)) {
2270                         d_printf("(%s) dcerpc_lsa_OpenPolicy2 failed: %s\n",
2271                                  __location__, nt_errstr(status));
2272                         talloc_free(mem_ctx);
2273                         return false;
2274                 }
2275         }
2276
2277 #if 0
2278         p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2279 #endif
2280
2281         {
2282                 int i;
2283                 int levels[] = { 2,3,5,6 };
2284
2285                 for (i=0; i<ARRAY_SIZE(levels); i++) {
2286                         struct lsa_QueryInfoPolicy r;
2287                         r.in.handle = &lsa_handle;
2288                         r.in.level = levels[i];
2289                         status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
2290                         if (!NT_STATUS_IS_OK(status)) {
2291                                 d_printf("(%s) dcerpc_lsa_QueryInfoPolicy %d "
2292                                          "failed: %s\n", __location__,
2293                                          levels[i], nt_errstr(status));
2294                                 talloc_free(mem_ctx);
2295                                 return false;
2296                         }
2297                         if (levels[i] == 5) {
2298                                 domain_sid = r.out.info->account_domain.sid;
2299                         }
2300                 }
2301         }
2302
2303         return ret;
2304 }
2305
2306 static NTSTATUS get_servername(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree,
2307                                char **name)
2308 {
2309         struct rap_WserverGetInfo r;
2310         NTSTATUS status;
2311         char servername[17];
2312
2313         r.in.level = 0;
2314         r.in.bufsize = 0xffff;
2315
2316         status = smbcli_rap_netservergetinfo(tree, mem_ctx, &r);
2317         if (!NT_STATUS_IS_OK(status)) {
2318                 return status;
2319         }
2320
2321         memcpy(servername, r.out.info.info0.name, 16);
2322         servername[16] = '\0';
2323
2324         if (pull_ascii_talloc(mem_ctx, lp_iconv_convenience(global_loadparm), 
2325                               name, servername) < 0) {
2326                 return NT_STATUS_NO_MEMORY;
2327         }
2328
2329         return NT_STATUS_OK;
2330 }
2331
2332
2333 static NTSTATUS find_printers(TALLOC_CTX *ctx, struct smbcli_tree *tree,
2334                               const char ***printers, int *num_printers)
2335 {
2336         TALLOC_CTX *mem_ctx;
2337         NTSTATUS status;
2338         struct dcerpc_pipe *p;
2339         struct srvsvc_NetShareEnum r;
2340         struct srvsvc_NetShareCtr1 c1_in;
2341         struct srvsvc_NetShareCtr1 *c1;
2342         int i;
2343
2344         mem_ctx = talloc_new(ctx);
2345         if (mem_ctx == NULL) {
2346                 return NT_STATUS_NO_MEMORY;
2347         }
2348
2349         status = pipe_bind_smb(mem_ctx, tree, "\\srvsvc", &ndr_table_srvsvc,
2350                                &p);
2351         if (!NT_STATUS_IS_OK(status)) {
2352                 d_printf("could not bind to srvsvc pipe\n");
2353                 talloc_free(mem_ctx);
2354                 return status;
2355         }
2356
2357         r.in.server_unc = talloc_asprintf(
2358                 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2359         r.in.level = 1;
2360         ZERO_STRUCT(c1_in);
2361         r.in.ctr.ctr1 = &c1_in;
2362         r.in.max_buffer = (uint32_t)-1;
2363         r.in.resume_handle = NULL;
2364
2365         status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
2366         if (!NT_STATUS_IS_OK(status)) {
2367                 d_printf("NetShareEnum level %u failed - %s\n",
2368                          r.in.level, nt_errstr(status));
2369                 talloc_free(mem_ctx);
2370                 return status;
2371         }
2372
2373         *printers = NULL;
2374         *num_printers = 0;
2375         c1 = r.out.ctr.ctr1;
2376         for (i=0; i<c1->count; i++) {
2377                 if (c1->array[i].type != STYPE_PRINTQ) {
2378                         continue;
2379                 }
2380                 if (!add_string_to_array(ctx, c1->array[i].name,
2381                                          printers, num_printers)) {
2382                         talloc_free(ctx);
2383                         return NT_STATUS_NO_MEMORY;
2384                 }
2385         }
2386
2387         talloc_free(mem_ctx);
2388         return NT_STATUS_OK;
2389 }
2390
2391 static bool enumprinters(TALLOC_CTX *mem_ctx, struct dcerpc_pipe *pipe,
2392                          const char *servername, int level, int *num_printers)
2393 {
2394         struct spoolss_EnumPrinters r;
2395         NTSTATUS status;
2396         DATA_BLOB blob;
2397
2398         r.in.flags = PRINTER_ENUM_LOCAL;
2399         r.in.server = talloc_asprintf(mem_ctx, "\\\\%s", servername);
2400         r.in.level = level;
2401         r.in.buffer = NULL;
2402         r.in.offered = 0;
2403
2404         status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2405         if (!NT_STATUS_IS_OK(status)) {
2406                 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s\n",
2407                          __location__, nt_errstr(status));
2408                 return false;
2409         }
2410
2411         if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2412                 d_printf("(%s) EnumPrinters unexpected return code %s, should "
2413                          "be WERR_INSUFFICIENT_BUFFER\n", __location__,
2414                          win_errstr(r.out.result));
2415                 return false;
2416         }
2417
2418         blob = data_blob_talloc_zero(mem_ctx, r.out.needed);
2419         if (blob.data == NULL) {
2420                 d_printf("(%s) data_blob_talloc failed\n", __location__);
2421                 return false;
2422         }
2423
2424         r.in.buffer = &blob;
2425         r.in.offered = r.out.needed;
2426
2427         status = dcerpc_spoolss_EnumPrinters(pipe, mem_ctx, &r);
2428         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2429                 d_printf("(%s) dcerpc_spoolss_EnumPrinters failed: %s, "
2430                          "%s\n", __location__, nt_errstr(status),
2431                          win_errstr(r.out.result));
2432                 return false;
2433         }
2434
2435         *num_printers = r.out.count;
2436
2437         return true;
2438 }
2439
2440 static NTSTATUS getprinterinfo(TALLOC_CTX *ctx, struct dcerpc_pipe *pipe,
2441                                struct policy_handle *handle, int level,
2442                                union spoolss_PrinterInfo **res)
2443 {
2444         TALLOC_CTX *mem_ctx;
2445         struct spoolss_GetPrinter r;
2446         DATA_BLOB blob;
2447         NTSTATUS status;
2448
2449         mem_ctx = talloc_new(ctx);
2450         if (mem_ctx == NULL) {
2451                 return NT_STATUS_NO_MEMORY;
2452         }
2453
2454         r.in.handle = handle;
2455         r.in.level = level;
2456         r.in.buffer = NULL;
2457         r.in.offered = 0;
2458
2459         status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2460         if (!NT_STATUS_IS_OK(status)) {
2461                 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s\n",
2462                          __location__, nt_errstr(status));
2463                 talloc_free(mem_ctx);
2464                 return status;
2465         }
2466
2467         if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2468                 printf("GetPrinter unexpected return code %s, should "
2469                        "be WERR_INSUFFICIENT_BUFFER\n",
2470                        win_errstr(r.out.result));
2471                 talloc_free(mem_ctx);
2472                 return NT_STATUS_UNSUCCESSFUL;
2473         }
2474
2475         r.in.handle = handle;
2476         r.in.level = level;
2477         blob = data_blob_talloc(mem_ctx, NULL, r.out.needed);
2478         if (blob.data == NULL) {
2479                 talloc_free(mem_ctx);
2480                 return NT_STATUS_NO_MEMORY;
2481         }
2482         memset(blob.data, 0, blob.length);
2483         r.in.buffer = &blob;
2484         r.in.offered = r.out.needed;
2485
2486         status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2487         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2488                 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s, "
2489                          "%s\n", __location__, nt_errstr(status),
2490                          win_errstr(r.out.result));
2491                 talloc_free(mem_ctx);
2492                 return NT_STATUS_IS_OK(status) ?
2493                         NT_STATUS_UNSUCCESSFUL : status;
2494         }
2495
2496         if (res != NULL) {
2497                 *res = talloc_steal(ctx, r.out.info);
2498         }
2499
2500         talloc_free(mem_ctx);
2501         return NT_STATUS_OK;
2502 }
2503
2504 bool torture_samba3_rpc_spoolss(struct torture_context *torture)
2505 {
2506         TALLOC_CTX *mem_ctx;
2507         bool ret = true;
2508         struct smbcli_state *cli;
2509         struct dcerpc_pipe *p;
2510         NTSTATUS status;
2511         struct policy_handle server_handle, printer_handle;
2512         const char **printers;
2513         int num_printers;
2514         struct spoolss_UserLevel1 userlevel1;
2515         char *servername;
2516
2517         if (!(mem_ctx = talloc_new(torture))) {
2518                 return false;
2519         }
2520
2521         if (!(torture_open_connection_share(
2522                       mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2523                       "IPC$", NULL))) {
2524                 d_printf("IPC$ connection failed\n");
2525                 talloc_free(mem_ctx);
2526                 return false;
2527         }
2528
2529         status = get_servername(mem_ctx, cli->tree, &servername);
2530         if (!NT_STATUS_IS_OK(status)) {
2531                 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2532                           __location__, nt_errstr(status));
2533                 talloc_free(mem_ctx);
2534                 return false;
2535         }
2536
2537         if (!NT_STATUS_IS_OK(find_printers(mem_ctx, cli->tree,
2538                                            &printers, &num_printers))) {
2539                 talloc_free(mem_ctx);
2540                 return false;
2541         }
2542
2543         if (num_printers == 0) {
2544                 d_printf("Did not find printers\n");
2545                 talloc_free(mem_ctx);
2546                 return true;
2547         }
2548
2549         status = pipe_bind_smb(mem_ctx, cli->tree, "\\spoolss",
2550                                &ndr_table_spoolss, &p);
2551         if (!NT_STATUS_IS_OK(status)) {
2552                 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2553                          nt_errstr(status));
2554                 talloc_free(mem_ctx);
2555                 return false;
2556         }
2557
2558         ZERO_STRUCT(userlevel1);
2559         userlevel1.client = talloc_asprintf(
2560                 mem_ctx, "\\\\%s", lp_netbios_name(torture->lp_ctx));
2561         userlevel1.user = cli_credentials_get_username(cmdline_credentials);
2562         userlevel1.build = 2600;
2563         userlevel1.major = 3;
2564         userlevel1.minor = 0;
2565         userlevel1.processor = 0;
2566
2567         {
2568                 struct spoolss_OpenPrinterEx r;
2569
2570                 ZERO_STRUCT(r);
2571                 r.in.printername = talloc_asprintf(mem_ctx, "\\\\%s",
2572                                                    servername);
2573                 r.in.datatype = NULL;
2574                 r.in.access_mask = 0;
2575                 r.in.level = 1;
2576                 r.in.userlevel.level1 = &userlevel1;
2577                 r.out.handle = &server_handle;
2578
2579                 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2580                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2581                         d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2582                                  "%s, %s\n", __location__, nt_errstr(status),
2583                                  win_errstr(r.out.result));
2584                         talloc_free(mem_ctx);
2585                         return false;
2586                 }
2587         }
2588
2589         {
2590                 struct spoolss_ClosePrinter r;
2591
2592                 r.in.handle = &server_handle;
2593                 r.out.handle = &server_handle;
2594
2595                 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2596                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2597                         d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2598                                  "%s, %s\n", __location__, nt_errstr(status),
2599                                  win_errstr(r.out.result));
2600                         talloc_free(mem_ctx);
2601                         return false;
2602                 }
2603         }
2604
2605         {
2606                 struct spoolss_OpenPrinterEx r;
2607
2608                 ZERO_STRUCT(r);
2609                 r.in.printername = talloc_asprintf(
2610                         mem_ctx, "\\\\%s\\%s", servername, printers[0]);
2611                 r.in.datatype = NULL;
2612                 r.in.access_mask = 0;
2613                 r.in.level = 1;
2614                 r.in.userlevel.level1 = &userlevel1;
2615                 r.out.handle = &printer_handle;
2616
2617                 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2618                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2619                         d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2620                                  "%s, %s\n", __location__, nt_errstr(status),
2621                                  win_errstr(r.out.result));
2622                         talloc_free(mem_ctx);
2623                         return false;
2624                 }
2625         }
2626
2627         {
2628                 int i;
2629
2630                 for (i=0; i<8; i++) {
2631                         status = getprinterinfo(mem_ctx, p, &printer_handle,
2632                                                 i, NULL);
2633                         if (!NT_STATUS_IS_OK(status)) {
2634                                 d_printf("(%s) getprinterinfo %d failed: %s\n",
2635                                          __location__, i, nt_errstr(status));
2636                                 ret = false;
2637                         }
2638                 }
2639         }
2640
2641         {
2642                 struct spoolss_ClosePrinter r;
2643
2644                 r.in.handle = &printer_handle;
2645                 r.out.handle = &printer_handle;
2646
2647                 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2648                 if (!NT_STATUS_IS_OK(status)) {
2649                         d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2650                                  "%s\n", __location__, nt_errstr(status));
2651                         talloc_free(mem_ctx);
2652                         return false;
2653                 }
2654         }
2655
2656         {
2657                 int num_enumerated;
2658                 if (!enumprinters(mem_ctx, p, servername, 1,
2659                                   &num_enumerated)) {
2660                         d_printf("(%s) enumprinters failed\n", __location__);
2661                         talloc_free(mem_ctx);
2662                         return false;
2663                 }
2664                 if (num_printers != num_enumerated) {
2665                         d_printf("(%s) netshareenum gave %d printers, "
2666                                  "enumprinters lvl 1 gave %d\n", __location__,
2667                                  num_printers, num_enumerated);
2668                         talloc_free(mem_ctx);
2669                         return false;
2670                 }
2671         }
2672
2673         {
2674                 int num_enumerated;
2675                 if (!enumprinters(mem_ctx, p, servername, 2,
2676                                   &num_enumerated)) {
2677                         d_printf("(%s) enumprinters failed\n", __location__);
2678                         talloc_free(mem_ctx);
2679                         return false;
2680                 }
2681                 if (num_printers != num_enumerated) {
2682                         d_printf("(%s) netshareenum gave %d printers, "
2683                                  "enumprinters lvl 2 gave %d\n", __location__,
2684                                  num_printers, num_enumerated);
2685                         talloc_free(mem_ctx);
2686                         return false;
2687                 }
2688         }
2689
2690         talloc_free(mem_ctx);
2691
2692         return ret;
2693 }
2694
2695 bool torture_samba3_rpc_wkssvc(struct torture_context *torture)
2696 {
2697         TALLOC_CTX *mem_ctx;
2698         struct smbcli_state *cli;
2699         struct dcerpc_pipe *p;
2700         NTSTATUS status;
2701         char *servername;
2702
2703         if (!(mem_ctx = talloc_new(torture))) {
2704                 return false;
2705         }
2706
2707         if (!(torture_open_connection_share(
2708                       mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
2709                       "IPC$", NULL))) {
2710                 d_printf("IPC$ connection failed\n");
2711                 talloc_free(mem_ctx);
2712                 return false;
2713         }
2714
2715         status = get_servername(mem_ctx, cli->tree, &servername);
2716         if (!NT_STATUS_IS_OK(status)) {
2717                 d_fprintf(stderr, "(%s) get_servername returned %s\n",
2718                           __location__, nt_errstr(status));
2719                 talloc_free(mem_ctx);
2720                 return false;
2721         }
2722
2723         status = pipe_bind_smb(mem_ctx, cli->tree, "\\wkssvc",
2724                                &ndr_table_wkssvc, &p);
2725         if (!NT_STATUS_IS_OK(status)) {
2726                 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2727                          nt_errstr(status));
2728                 talloc_free(mem_ctx);
2729                 return false;
2730         }
2731
2732         {
2733                 struct wkssvc_NetWkstaInfo100 wks100;
2734                 union wkssvc_NetWkstaInfo info;
2735                 struct wkssvc_NetWkstaGetInfo r;
2736
2737                 r.in.server_name = "\\foo";
2738                 r.in.level = 100;
2739                 info.info100 = &wks100;
2740                 r.out.info = &info;
2741
2742                 status = dcerpc_wkssvc_NetWkstaGetInfo(p, mem_ctx, &r);
2743                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2744                         d_printf("(%s) dcerpc_wkssvc_NetWksGetInfo failed: "
2745                                  "%s, %s\n", __location__, nt_errstr(status),
2746                                  win_errstr(r.out.result));
2747                         talloc_free(mem_ctx);
2748                         return false;
2749                 }
2750
2751                 if (strcmp(servername,
2752                            r.out.info->info100->server_name) != 0) {
2753                         d_printf("(%s) servername inconsistency: RAP=%s, "
2754                                  "dcerpc_wkssvc_NetWksGetInfo=%s",
2755                                  __location__, servername,
2756                                  r.out.info->info100->server_name);
2757                         talloc_free(mem_ctx);
2758                         return false;
2759                 }
2760         }
2761
2762         talloc_free(mem_ctx);
2763         return true;
2764 }
2765
2766 static NTSTATUS winreg_close(struct dcerpc_pipe *p,
2767                              struct policy_handle *handle)
2768 {
2769         struct winreg_CloseKey c;
2770         NTSTATUS status;
2771         TALLOC_CTX *mem_ctx;
2772
2773         c.in.handle = c.out.handle = handle;
2774
2775         if (!(mem_ctx = talloc_new(p))) {
2776                 return NT_STATUS_NO_MEMORY;
2777         }
2778
2779         status = dcerpc_winreg_CloseKey(p, mem_ctx, &c);
2780         talloc_free(mem_ctx);
2781
2782         if (!NT_STATUS_IS_OK(status)) {
2783                 return status;
2784         }
2785
2786         if (!W_ERROR_IS_OK(c.out.result)) {
2787                 return werror_to_ntstatus(c.out.result);
2788         }
2789
2790         return NT_STATUS_OK;
2791 }
2792
2793 static NTSTATUS enumvalues(struct dcerpc_pipe *p, struct policy_handle *handle,
2794                            TALLOC_CTX *mem_ctx)
2795 {
2796         uint32_t enum_index = 0;
2797
2798         while (1) {
2799                 struct winreg_EnumValue r;
2800                 struct winreg_StringBuf name;
2801                 enum winreg_Type type = 0;
2802                 uint8_t buf8[1024];
2803                 NTSTATUS status;
2804                 uint32_t size, length;
2805                 
2806                 r.in.handle = handle;
2807                 r.in.enum_index = enum_index;
2808                 name.name = "";
2809                 name.size = 1024;
2810                 r.in.name = r.out.name = &name;
2811                 size = 1024;
2812                 length = 5;
2813                 r.in.type = &type;
2814                 r.in.value = buf8;
2815                 r.in.size = &size;
2816                 r.in.length = &length;
2817
2818                 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
2819                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2820                         return NT_STATUS_OK;
2821                 }
2822                 enum_index += 1;
2823         }
2824 }
2825
2826 static NTSTATUS enumkeys(struct dcerpc_pipe *p, struct policy_handle *handle, 
2827                          TALLOC_CTX *mem_ctx, int depth)
2828 {
2829         struct winreg_EnumKey r;
2830         struct winreg_StringBuf class, name;
2831         NTSTATUS status;
2832         NTTIME t = 0;
2833
2834         if (depth <= 0) {
2835                 return NT_STATUS_OK;
2836         }
2837
2838         class.name   = "";
2839         class.size   = 1024;
2840
2841         r.in.handle = handle;
2842         r.in.enum_index = 0;
2843         r.in.name = &name;
2844         r.in.keyclass = &class;
2845         r.out.name = &name;
2846         r.in.last_changed_time = &t;
2847
2848         do {
2849                 TALLOC_CTX *tmp_ctx;
2850                 struct winreg_OpenKey o;
2851                 struct policy_handle key_handle;
2852                 int i;
2853
2854                 if (!(tmp_ctx = talloc_new(mem_ctx))) {
2855                         return NT_STATUS_NO_MEMORY;
2856                 }
2857
2858                 name.name = NULL;
2859                 name.size = 1024;
2860
2861                 status = dcerpc_winreg_EnumKey(p, tmp_ctx, &r);
2862                 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2863                         /* We're done enumerating */
2864                         talloc_free(tmp_ctx);
2865                         return NT_STATUS_OK;
2866                 }
2867
2868                 for (i=0; i<10-depth; i++)
2869                         printf(" ");
2870                 printf("%s\n", r.out.name->name);
2871                         
2872
2873                 o.in.parent_handle = handle;
2874                 o.in.keyname.name = r.out.name->name;
2875                 o.in.unknown = 0;
2876                 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2877                 o.out.handle = &key_handle;
2878
2879                 status = dcerpc_winreg_OpenKey(p, tmp_ctx, &o);
2880                 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(o.out.result)) {
2881                         enumkeys(p, &key_handle, tmp_ctx, depth-1);
2882                         enumvalues(p, &key_handle, tmp_ctx);
2883                         status = winreg_close(p, &key_handle);
2884                         if (!NT_STATUS_IS_OK(status)) {
2885                                 return status;
2886                         }
2887                 }
2888
2889                 talloc_free(tmp_ctx);
2890
2891                 r.in.enum_index += 1;
2892         } while(true);
2893
2894         return NT_STATUS_OK;
2895 }
2896
2897 typedef NTSTATUS (*winreg_open_fn)(struct dcerpc_pipe *, TALLOC_CTX *, void *);
2898
2899 static bool test_Open3(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
2900                        const char *name, winreg_open_fn open_fn)
2901 {
2902         struct policy_handle handle;
2903         struct winreg_OpenHKLM r;
2904         NTSTATUS status;
2905
2906         r.in.system_name = 0;
2907         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2908         r.out.handle = &handle;
2909         
2910         status = open_fn(p, mem_ctx, &r);
2911         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2912                 d_printf("(%s) %s failed: %s, %s\n", __location__, name,
2913                          nt_errstr(status), win_errstr(r.out.result));
2914                 return false;
2915         }
2916
2917         enumkeys(p, &handle, mem_ctx, 4);
2918
2919         status = winreg_close(p, &handle);
2920         if (!NT_STATUS_IS_OK(status)) {
2921                 d_printf("(%s) dcerpc_CloseKey failed: %s\n",
2922                          __location__, nt_errstr(status));
2923                 return false;
2924         }
2925
2926         return true;
2927 }
2928
2929 bool torture_samba3_rpc_winreg(struct torture_context *torture)
2930 {
2931         NTSTATUS status;
2932         struct dcerpc_pipe *p;
2933         TALLOC_CTX *mem_ctx;
2934         bool ret = true;
2935         struct {
2936                 const char *name;
2937                 winreg_open_fn fn;
2938         } open_fns[] = {
2939                 {"OpenHKLM", (winreg_open_fn)dcerpc_winreg_OpenHKLM },
2940                 {"OpenHKU",  (winreg_open_fn)dcerpc_winreg_OpenHKU },
2941                 {"OpenHKPD", (winreg_open_fn)dcerpc_winreg_OpenHKPD },
2942                 {"OpenHKPT", (winreg_open_fn)dcerpc_winreg_OpenHKPT },
2943                 {"OpenHKCR", (winreg_open_fn)dcerpc_winreg_OpenHKCR }};
2944 #if 0
2945         int i;
2946 #endif
2947
2948         mem_ctx = talloc_init("torture_rpc_winreg");
2949
2950         status = torture_rpc_connection(torture, &p, &ndr_table_winreg);
2951
2952         if (!NT_STATUS_IS_OK(status)) {
2953                 talloc_free(mem_ctx);
2954                 return false;
2955         }
2956
2957 #if 1
2958         ret = test_Open3(p, mem_ctx, open_fns[0].name, open_fns[0].fn);
2959 #else
2960         for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
2961                 if (!test_Open3(p, mem_ctx, open_fns[i].name, open_fns[i].fn))
2962                         ret = false;
2963         }
2964 #endif
2965
2966         talloc_free(mem_ctx);
2967
2968         return ret;
2969 }
2970
2971 static NTSTATUS get_shareinfo(TALLOC_CTX *mem_ctx,
2972                               struct smbcli_state *cli,
2973                               const char *share,
2974                               struct srvsvc_NetShareInfo502 **info)
2975 {
2976         struct smbcli_tree *ipc;
2977         struct dcerpc_pipe *p;
2978         struct srvsvc_NetShareGetInfo r;
2979         NTSTATUS status;
2980
2981         if (!(p = dcerpc_pipe_init(cli,
2982                                    cli->transport->socket->event.ctx,
2983                                    lp_iconv_convenience(global_loadparm)))) {
2984                 status = NT_STATUS_NO_MEMORY;
2985                 goto fail;
2986         }
2987
2988         status = secondary_tcon(p, cli->session, "IPC$", &ipc);
2989         if (!NT_STATUS_IS_OK(status)) {
2990                 goto fail;
2991         }
2992
2993         status = dcerpc_pipe_open_smb(p, ipc, "\\pipe\\srvsvc");
2994         if (!NT_STATUS_IS_OK(status)) {
2995                 d_printf("dcerpc_pipe_open_smb failed: %s\n",
2996                          nt_errstr(status));
2997                 goto fail;
2998         }
2999
3000         status = dcerpc_bind_auth_none(p, &ndr_table_srvsvc);
3001         if (!NT_STATUS_IS_OK(status)) {
3002                 d_printf("dcerpc_bind_auth_none failed: %s\n",
3003                          nt_errstr(status));
3004                 goto fail;