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