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