r17205: Even if this makes me look foolish, at least start to scratch on the surface
[jelmer/samba4-debian.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 False;
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("GetUserName failed - %s\n", nt_errstr(status));
1510                 talloc_free(lsa);
1511                 return NULL;
1512         }
1513
1514         result = name2sid(mem_ctx, lsa, r.out.account_name->string,
1515                           r.out.authority_name->string->string);
1516
1517         talloc_free(lsa);
1518         return result;
1519 }
1520
1521 /*
1522  * Do a tcon, given a session
1523  */
1524
1525 NTSTATUS secondary_tcon(TALLOC_CTX *mem_ctx,
1526                         struct smbcli_session *session,
1527                         const char *sharename,
1528                         struct smbcli_tree **res)
1529 {
1530         struct smbcli_tree *result;
1531         TALLOC_CTX *tmp_ctx;
1532         union smb_tcon tcon;
1533         NTSTATUS status;
1534
1535         if (!(tmp_ctx = talloc_new(mem_ctx))) {
1536                 return NT_STATUS_NO_MEMORY;
1537         }
1538
1539         if (!(result = smbcli_tree_init(session, mem_ctx, False))) {
1540                 talloc_free(tmp_ctx);
1541                 return NT_STATUS_NO_MEMORY;
1542         }
1543
1544         tcon.generic.level = RAW_TCON_TCONX;
1545         tcon.tconx.in.flags = 0;
1546         tcon.tconx.in.password = data_blob(NULL, 0);
1547         tcon.tconx.in.path = sharename;
1548         tcon.tconx.in.device = "?????";
1549
1550         status = smb_raw_tcon(result, tmp_ctx, &tcon);
1551         if (!NT_STATUS_IS_OK(status)) {
1552                 d_printf("smb_raw_tcon failed: %s\n", nt_errstr(status));
1553                 talloc_free(tmp_ctx);
1554                 return status;
1555         }
1556
1557         result->tid = tcon.tconx.out.tid;
1558         result = talloc_steal(mem_ctx, result);
1559         talloc_free(tmp_ctx);
1560         *res = result;
1561         return NT_STATUS_OK;
1562 }
1563
1564 /*
1565  * Test the getusername behaviour
1566  */
1567
1568 BOOL torture_samba3_rpc_getusername(struct torture_context *torture)
1569 {
1570         NTSTATUS status;
1571         struct smbcli_state *cli;
1572         TALLOC_CTX *mem_ctx;
1573         BOOL ret = True;
1574         struct dom_sid *user_sid;
1575         struct dom_sid *created_sid;
1576         struct cli_credentials *anon_creds;
1577         struct cli_credentials *user_creds;
1578         char *domain_name;
1579
1580         if (!(mem_ctx = talloc_new(torture))) {
1581                 return False;
1582         }
1583
1584         status = smbcli_full_connection(
1585                 mem_ctx, &cli, lp_parm_string(-1, "torture", "host"),
1586                 "IPC$", NULL, cmdline_credentials, NULL);
1587         if (!NT_STATUS_IS_OK(status)) {
1588                 d_printf("smbcli_full_connection failed: %s\n",
1589                          nt_errstr(status));
1590                 ret = False;
1591                 goto done;
1592         }
1593
1594         if (!(user_sid = whoami(mem_ctx, cli->tree))) {
1595                 d_printf("whoami on auth'ed connection failed\n");
1596                 ret = False;
1597         }
1598
1599         talloc_free(cli);
1600
1601         if (!(anon_creds = create_anon_creds(mem_ctx))) {
1602                 d_printf("create_anon_creds failed\n");
1603                 ret = False;
1604                 goto done;
1605         }
1606
1607         status = smbcli_full_connection(
1608                 mem_ctx, &cli, lp_parm_string(-1, "torture", "host"),
1609                 "IPC$", NULL, anon_creds, NULL);
1610         if (!NT_STATUS_IS_OK(status)) {
1611                 d_printf("anon smbcli_full_connection failed: %s\n",
1612                          nt_errstr(status));
1613                 ret = False;
1614                 goto done;
1615         }
1616
1617         if (!(user_sid = whoami(mem_ctx, cli->tree))) {
1618                 d_printf("whoami on anon connection failed\n");
1619                 ret = False;
1620                 goto done;
1621         }
1622
1623         if (!dom_sid_equal(user_sid,
1624                            dom_sid_parse_talloc(mem_ctx, "s-1-5-7"))) {
1625                 d_printf("Anon lsa_GetUserName returned %s, expected S-1-5-7",
1626                          dom_sid_string(mem_ctx, user_sid));
1627                 ret = False;
1628         }
1629
1630         if (!(user_creds = cli_credentials_init(mem_ctx))) {
1631                 d_printf("cli_credentials_init failed\n");
1632                 ret = False;
1633                 goto done;
1634         }
1635
1636         cli_credentials_set_conf(user_creds);
1637         cli_credentials_set_username(user_creds, "torture_username",
1638                                      CRED_SPECIFIED);
1639         cli_credentials_set_password(user_creds,
1640                                      generate_random_str(user_creds, 8),
1641                                      CRED_SPECIFIED);
1642
1643         if (!create_user(mem_ctx, cli, cmdline_credentials,
1644                          cli_credentials_get_username(user_creds),
1645                          cli_credentials_get_password(user_creds),
1646                          &domain_name, &created_sid)) {
1647                 d_printf("create_user failed\n");
1648                 ret = False;
1649                 goto done;
1650         }
1651
1652         cli_credentials_set_domain(user_creds, domain_name,
1653                                    CRED_SPECIFIED);
1654
1655         {
1656                 struct smbcli_session *session2;
1657                 struct smb_composite_sesssetup setup;
1658                 struct smbcli_tree *tree;
1659
1660                 session2 = smbcli_session_init(cli->transport, mem_ctx, False);
1661                 if (session2 == NULL) {
1662                         d_printf("smbcli_session_init failed\n");
1663                         goto done;
1664                 }
1665
1666                 setup.in.sesskey = cli->transport->negotiate.sesskey;
1667                 setup.in.capabilities = cli->transport->negotiate.capabilities;
1668                 setup.in.workgroup = "";
1669                 setup.in.credentials = user_creds;
1670
1671                 status = smb_composite_sesssetup(session2, &setup);
1672                 if (!NT_STATUS_IS_OK(status)) {
1673                         d_printf("anon session setup failed: %s\n",
1674                                  nt_errstr(status));
1675                         ret = False;
1676                         goto done;
1677                 }
1678
1679                 if (!NT_STATUS_IS_OK(secondary_tcon(mem_ctx, session2,
1680                                                     "IPC$", &tree))) {
1681                         d_printf("secondary_tcon failed\n");
1682                         ret = False;
1683                         goto done;
1684                 }
1685
1686                 if (!(user_sid = whoami(mem_ctx, tree))) {
1687                         d_printf("whoami on user connection failed\n");
1688                         ret = False;
1689                         goto delete;
1690                 }
1691
1692                 talloc_free(tree);
1693         }
1694
1695         d_printf("Created %s, found %s\n",
1696                  dom_sid_string(mem_ctx, created_sid),
1697                  dom_sid_string(mem_ctx, user_sid));
1698
1699         if (!dom_sid_equal(created_sid, user_sid)) {
1700                 ret = False;
1701         }
1702
1703  delete:
1704         if (!delete_user(cli, cmdline_credentials,
1705                          cli_credentials_get_username(user_creds))) {
1706                 d_printf("delete_user failed\n");
1707                 ret = False;
1708         }
1709
1710  done:
1711         talloc_free(mem_ctx);
1712         return ret;
1713 }
1714
1715 static BOOL test_NetShareGetInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1716                                  const char *sharename)
1717 {
1718         NTSTATUS status;
1719         struct srvsvc_NetShareGetInfo r;
1720         uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007, 1501 };
1721         int i;
1722         BOOL ret = True;
1723
1724         r.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s",
1725                                           dcerpc_server_name(p));
1726         r.in.share_name = sharename;
1727
1728         for (i=0;i<ARRAY_SIZE(levels);i++) {
1729                 r.in.level = levels[i];
1730
1731                 ZERO_STRUCT(r.out);
1732
1733                 printf("testing NetShareGetInfo level %u on share '%s'\n", 
1734                        r.in.level, r.in.share_name);
1735
1736                 status = dcerpc_srvsvc_NetShareGetInfo(p, mem_ctx, &r);
1737                 if (!NT_STATUS_IS_OK(status)) {
1738                         printf("NetShareGetInfo level %u on share '%s' failed"
1739                                " - %s\n", r.in.level, r.in.share_name,
1740                                nt_errstr(status));
1741                         ret = False;
1742                         continue;
1743                 }
1744                 if (!W_ERROR_IS_OK(r.out.result)) {
1745                         printf("NetShareGetInfo level %u on share '%s' failed "
1746                                "- %s\n", r.in.level, r.in.share_name,
1747                                win_errstr(r.out.result));
1748                         ret = False;
1749                         continue;
1750                 }
1751         }
1752
1753         return ret;
1754 }
1755
1756 static BOOL test_NetShareEnum(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1757                               const char **one_sharename)
1758 {
1759         NTSTATUS status;
1760         struct srvsvc_NetShareEnum r;
1761         struct srvsvc_NetShareCtr0 c0;
1762         uint32_t levels[] = { 0, 1, 2, 501, 502, 1004, 1005, 1006, 1007 };
1763         int i;
1764         BOOL ret = True;
1765
1766         r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
1767         r.in.ctr.ctr0 = &c0;
1768         r.in.ctr.ctr0->count = 0;
1769         r.in.ctr.ctr0->array = NULL;
1770         r.in.max_buffer = (uint32_t)-1;
1771         r.in.resume_handle = NULL;
1772
1773         for (i=0;i<ARRAY_SIZE(levels);i++) {
1774                 r.in.level = levels[i];
1775
1776                 ZERO_STRUCT(r.out);
1777
1778                 printf("testing NetShareEnum level %u\n", r.in.level);
1779                 status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
1780                 if (!NT_STATUS_IS_OK(status)) {
1781                         printf("NetShareEnum level %u failed - %s\n",
1782                                r.in.level, nt_errstr(status));
1783                         ret = False;
1784                         continue;
1785                 }
1786                 if (!W_ERROR_IS_OK(r.out.result)) {
1787                         printf("NetShareEnum level %u failed - %s\n",
1788                                r.in.level, win_errstr(r.out.result));
1789                         continue;
1790                 }
1791                 if (r.in.level == 0) {
1792                         struct srvsvc_NetShareCtr0 *ctr = r.out.ctr.ctr0;
1793                         if (ctr->count > 0) {
1794                                 *one_sharename = ctr->array[0].name;
1795                         }
1796                 }
1797         }
1798
1799         return ret;
1800 }
1801
1802 BOOL torture_samba3_rpc_srvsvc(struct torture_context *torture)
1803 {
1804         struct dcerpc_pipe *p;
1805         TALLOC_CTX *mem_ctx;
1806         BOOL ret = True;
1807         const char *sharename = NULL;
1808         struct smbcli_state *cli;
1809         NTSTATUS status;
1810
1811         if (!(mem_ctx = talloc_new(torture))) {
1812                 return False;
1813         }
1814
1815         if (!(torture_open_connection_share(
1816                       mem_ctx, &cli, lp_parm_string(-1, "torture", "host"),
1817                       "IPC$", NULL))) {
1818                 talloc_free(mem_ctx);
1819                 return False;
1820         }
1821
1822         status = pipe_bind_smb(mem_ctx, cli->tree, "\\pipe\\srvsvc",
1823                                &dcerpc_table_srvsvc, &p);
1824         if (!NT_STATUS_IS_OK(status)) {
1825                 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
1826                          __location__, nt_errstr(status));
1827                 ret = False;
1828                 goto done;
1829         }
1830
1831         ret &= test_NetShareEnum(p, mem_ctx, &sharename);
1832         if (sharename == NULL) {
1833                 printf("did not get sharename\n");
1834         } else {
1835                 ret &= test_NetShareGetInfo(p, mem_ctx, sharename);
1836         }
1837
1838  done:
1839         talloc_free(mem_ctx);
1840         return ret;
1841 }
1842
1843 static struct security_descriptor *get_sharesec(TALLOC_CTX *mem_ctx,
1844                                                 struct smbcli_session *sess,
1845                                                 const char *sharename)
1846 {
1847         struct smbcli_tree *tree;
1848         TALLOC_CTX *tmp_ctx;
1849         struct dcerpc_pipe *p;
1850         NTSTATUS status;
1851         struct srvsvc_NetShareGetInfo r;
1852         struct security_descriptor *result;
1853
1854         if (!(tmp_ctx = talloc_new(mem_ctx))) {
1855                 d_printf("talloc_new failed\n");
1856                 return NULL;
1857         }
1858
1859         if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
1860                 d_printf("secondary_tcon failed\n");
1861                 talloc_free(tmp_ctx);
1862                 return NULL;
1863         }
1864
1865         status = pipe_bind_smb(mem_ctx, tree, "\\pipe\\srvsvc",
1866                                &dcerpc_table_srvsvc, &p);
1867         if (!NT_STATUS_IS_OK(status)) {
1868                 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
1869                          __location__, nt_errstr(status));
1870                 talloc_free(tmp_ctx);
1871                 return NULL;
1872         }
1873
1874 #if 0
1875         p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
1876 #endif
1877
1878         r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
1879                                           dcerpc_server_name(p));
1880         r.in.share_name = sharename;
1881         r.in.level = 502;
1882
1883         status = dcerpc_srvsvc_NetShareGetInfo(p, tmp_ctx, &r);
1884         if (!NT_STATUS_IS_OK(status)) {
1885                 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
1886                          nt_errstr(status));
1887                 talloc_free(tmp_ctx);
1888                 return NULL;
1889         }
1890
1891         result = talloc_steal(mem_ctx, r.out.info.info502->sd);
1892         talloc_free(tmp_ctx);
1893         return result;
1894 }
1895
1896 static NTSTATUS set_sharesec(TALLOC_CTX *mem_ctx,
1897                              struct smbcli_session *sess,
1898                              const char *sharename,
1899                              struct security_descriptor *sd)
1900 {
1901         struct smbcli_tree *tree;
1902         TALLOC_CTX *tmp_ctx;
1903         struct dcerpc_pipe *p;
1904         NTSTATUS status;
1905         struct sec_desc_buf i;
1906         struct srvsvc_NetShareSetInfo r;
1907         uint32_t error = 0;
1908
1909         if (!(tmp_ctx = talloc_new(mem_ctx))) {
1910                 d_printf("talloc_new failed\n");
1911                 return NT_STATUS_NO_MEMORY;
1912         }
1913
1914         if (!NT_STATUS_IS_OK(secondary_tcon(tmp_ctx, sess, "IPC$", &tree))) {
1915                 d_printf("secondary_tcon failed\n");
1916                 talloc_free(tmp_ctx);
1917                 return NT_STATUS_UNSUCCESSFUL;
1918         }
1919
1920         status = pipe_bind_smb(mem_ctx, tree, "\\pipe\\srvsvc",
1921                                &dcerpc_table_srvsvc, &p);
1922         if (!NT_STATUS_IS_OK(status)) {
1923                 d_printf("(%s) could not bind to srvsvc pipe: %s\n",
1924                          __location__, nt_errstr(status));
1925                 talloc_free(tmp_ctx);
1926                 return NT_STATUS_UNSUCCESSFUL;
1927         }
1928
1929 #if 0
1930         p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
1931 #endif
1932
1933         r.in.server_unc = talloc_asprintf(tmp_ctx, "\\\\%s",
1934                                           dcerpc_server_name(p));
1935         r.in.share_name = sharename;
1936         r.in.level = 1501;
1937         i.sd = sd;
1938         r.in.info.info1501 = &i;
1939         r.in.parm_error = &error;
1940
1941         status = dcerpc_srvsvc_NetShareSetInfo(p, tmp_ctx, &r);
1942         if (!NT_STATUS_IS_OK(status)) {
1943                 d_printf("srvsvc_NetShareGetInfo failed: %s\n",
1944                          nt_errstr(status));
1945         }
1946
1947         talloc_free(tmp_ctx);
1948         return status;
1949 }
1950
1951 BOOL try_tcon(TALLOC_CTX *mem_ctx,
1952               struct security_descriptor *orig_sd,
1953               struct smbcli_session *session,
1954               const char *sharename, const struct dom_sid *user_sid,
1955               unsigned int access_mask, NTSTATUS expected_tcon,
1956               NTSTATUS expected_mkdir)
1957 {
1958         TALLOC_CTX *tmp_ctx;
1959         struct smbcli_tree *rmdir_tree, *tree;
1960         struct dom_sid *domain_sid;
1961         uint32_t rid;
1962         struct security_descriptor *sd;
1963         NTSTATUS status;
1964         BOOL ret = True;
1965
1966         if (!(tmp_ctx = talloc_new(mem_ctx))) {
1967                 d_printf("talloc_new failed\n");
1968                 return False;
1969         }
1970
1971         status = secondary_tcon(tmp_ctx, session, sharename, &rmdir_tree);
1972         if (!NT_STATUS_IS_OK(status)) {
1973                 d_printf("first tcon to delete dir failed\n");
1974                 talloc_free(tmp_ctx);
1975                 return False;
1976         }
1977
1978         smbcli_rmdir(rmdir_tree, "sharesec_testdir");
1979
1980         if (!NT_STATUS_IS_OK(dom_sid_split_rid(tmp_ctx, user_sid,
1981                                                &domain_sid, &rid))) {
1982                 d_printf("dom_sid_split_rid failed\n");
1983                 talloc_free(tmp_ctx);
1984                 return False;
1985         }
1986
1987         sd = security_descriptor_create(
1988                 tmp_ctx, "S-1-5-32-544",
1989                 dom_sid_string(mem_ctx, dom_sid_add_rid(mem_ctx, domain_sid,
1990                                                         DOMAIN_RID_USERS)),
1991                 dom_sid_string(mem_ctx, user_sid),
1992                 SEC_ACE_TYPE_ACCESS_ALLOWED, access_mask, 0, NULL);
1993         if (sd == NULL) {
1994                 d_printf("security_descriptor_create failed\n");
1995                 talloc_free(tmp_ctx);
1996                 return False;
1997         }
1998
1999         status = set_sharesec(mem_ctx, session, sharename, sd);
2000         if (!NT_STATUS_IS_OK(status)) {
2001                 d_printf("custom set_sharesec failed: %s\n",
2002                          nt_errstr(status));
2003                 talloc_free(tmp_ctx);
2004                 return False;
2005         }
2006
2007         status = secondary_tcon(tmp_ctx, session, sharename, &tree);
2008         if (!NT_STATUS_EQUAL(status, expected_tcon)) {
2009                 d_printf("Expected %s, got %s\n", nt_errstr(expected_tcon),
2010                          nt_errstr(status));
2011                 ret = False;
2012                 goto done;
2013         }
2014
2015         if (!NT_STATUS_IS_OK(status)) {
2016                 /* An expected non-access, no point in trying to write */
2017                 goto done;
2018         }
2019
2020         status = smbcli_mkdir(tree, "sharesec_testdir");
2021         if (!NT_STATUS_EQUAL(status, expected_mkdir)) {
2022                 d_printf("Expected %s, got %s\n", nt_errstr(expected_mkdir),
2023                          nt_errstr(status));
2024                 ret = False;
2025         }
2026
2027  done:
2028         smbcli_rmdir(rmdir_tree, "sharesec_testdir");
2029
2030         status = set_sharesec(mem_ctx, session, sharename, orig_sd);
2031         if (!NT_STATUS_IS_OK(status)) {
2032                 d_printf("custom set_sharesec failed: %s\n",
2033                          nt_errstr(status));
2034                 talloc_free(tmp_ctx);
2035                 return False;
2036         }
2037
2038         talloc_free(tmp_ctx);
2039         return ret;
2040 }
2041
2042 BOOL torture_samba3_rpc_sharesec(struct torture_context *torture)
2043 {
2044         TALLOC_CTX *mem_ctx;
2045         BOOL ret = True;
2046         struct smbcli_state *cli;
2047         struct security_descriptor *sd;
2048         struct dom_sid *user_sid;
2049
2050         if (!(mem_ctx = talloc_new(torture))) {
2051                 return False;
2052         }
2053
2054         if (!(torture_open_connection_share(
2055                       mem_ctx, &cli, lp_parm_string(-1, "torture", "host"),
2056                       "IPC$", NULL))) {
2057                 d_printf("IPC$ connection failed\n");
2058                 talloc_free(mem_ctx);
2059                 return False;
2060         }
2061
2062         if (!(user_sid = whoami(mem_ctx, cli->tree))) {
2063                 d_printf("whoami failed\n");
2064                 talloc_free(mem_ctx);
2065                 return False;
2066         }
2067
2068         sd = get_sharesec(mem_ctx, cli->session, lp_parm_string(-1, "torture",
2069                                                                 "share"));
2070
2071         ret &= try_tcon(mem_ctx, sd, cli->session,
2072                         lp_parm_string(-1, "torture", "share"),
2073                         user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK);
2074
2075         ret &= try_tcon(mem_ctx, sd, cli->session,
2076                         lp_parm_string(-1, "torture", "share"),
2077                         user_sid, SEC_FILE_READ_DATA, NT_STATUS_OK,
2078                         NT_STATUS_NETWORK_ACCESS_DENIED);
2079
2080         ret &= try_tcon(mem_ctx, sd, cli->session,
2081                         lp_parm_string(-1, "torture", "share"),
2082                         user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK);
2083
2084         talloc_free(mem_ctx);
2085         return ret;
2086 }
2087
2088 BOOL torture_samba3_rpc_lsa(struct torture_context *torture)
2089 {
2090         TALLOC_CTX *mem_ctx;
2091         BOOL ret = True;
2092         struct smbcli_state *cli;
2093         struct dcerpc_pipe *p;
2094         struct policy_handle lsa_handle;
2095         NTSTATUS status;
2096         struct dom_sid *domain_sid;
2097
2098         if (!(mem_ctx = talloc_new(torture))) {
2099                 return False;
2100         }
2101
2102         if (!(torture_open_connection_share(
2103                       mem_ctx, &cli, lp_parm_string(-1, "torture", "host"),
2104                       "IPC$", NULL))) {
2105                 d_printf("IPC$ connection failed\n");
2106                 talloc_free(mem_ctx);
2107                 return False;
2108         }
2109
2110         status = pipe_bind_smb(mem_ctx, cli->tree, "\\lsarpc",
2111                                &dcerpc_table_lsarpc, &p);
2112         if (!NT_STATUS_IS_OK(status)) {
2113                 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2114                          nt_errstr(status));
2115                 talloc_free(mem_ctx);
2116                 return False;
2117         }
2118
2119         {
2120                 struct lsa_ObjectAttribute attr;
2121                 struct lsa_OpenPolicy2 o;
2122                 o.in.system_name = talloc_asprintf(
2123                         mem_ctx, "\\\\%s", dcerpc_server_name(p));
2124                 ZERO_STRUCT(attr);
2125                 o.in.attr = &attr;
2126                 o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2127                 o.out.handle = &lsa_handle;
2128                 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &o);
2129                 if (!NT_STATUS_IS_OK(status)) {
2130                         d_printf("(%s) dcerpc_lsa_OpenPolicy2 failed: %s\n",
2131                                  __location__, nt_errstr(status));
2132                         talloc_free(mem_ctx);
2133                         return False;
2134                 }
2135         }
2136
2137 #if 0
2138         p->conn->flags |= DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT;
2139 #endif
2140
2141         {
2142                 int i;
2143                 int levels[] = { 2,3,5,6 };
2144
2145                 for (i=0; i<ARRAY_SIZE(levels); i++) {
2146                         struct lsa_QueryInfoPolicy r;
2147                         r.in.handle = &lsa_handle;
2148                         r.in.level = levels[i];
2149                         status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
2150                         if (!NT_STATUS_IS_OK(status)) {
2151                                 d_printf("(%s) dcerpc_lsa_QueryInfoPolicy %d "
2152                                          "failed: %s\n", __location__,
2153                                          levels[i], nt_errstr(status));
2154                                 talloc_free(mem_ctx);
2155                                 return False;
2156                         }
2157                         if (levels[i] == 5) {
2158                                 domain_sid = r.out.info->account_domain.sid;
2159                         }
2160                 }
2161         }
2162
2163         return ret;
2164 }
2165
2166 static NTSTATUS find_printers(TALLOC_CTX *ctx, struct smbcli_tree *tree,
2167                               const char ***printers, int *num_printers)
2168 {
2169         TALLOC_CTX *mem_ctx;
2170         NTSTATUS status;
2171         struct dcerpc_pipe *p;
2172         struct srvsvc_NetShareEnum r;
2173         struct srvsvc_NetShareCtr1 c1_in;
2174         struct srvsvc_NetShareCtr1 *c1;
2175         int i;
2176
2177         mem_ctx = talloc_new(ctx);
2178         if (mem_ctx == NULL) {
2179                 return NT_STATUS_NO_MEMORY;
2180         }
2181
2182         status = pipe_bind_smb(mem_ctx, tree, "\\srvsvc", &dcerpc_table_srvsvc,
2183                                &p);
2184         if (!NT_STATUS_IS_OK(status)) {
2185                 d_printf("could not bind to srvsvc pipe\n");
2186                 talloc_free(mem_ctx);
2187                 return status;
2188         }
2189
2190         r.in.server_unc = talloc_asprintf(
2191                 mem_ctx, "\\\\%s", dcerpc_server_name(p));
2192         r.in.level = 1;
2193         ZERO_STRUCT(c1_in);
2194         r.in.ctr.ctr1 = &c1_in;
2195         r.in.max_buffer = (uint32_t)-1;
2196         r.in.resume_handle = NULL;
2197
2198         status = dcerpc_srvsvc_NetShareEnum(p, mem_ctx, &r);
2199         if (!NT_STATUS_IS_OK(status)) {
2200                 d_printf("NetShareEnum level %u failed - %s\n",
2201                          r.in.level, nt_errstr(status));
2202                 talloc_free(mem_ctx);
2203                 return status;
2204         }
2205
2206         *printers = NULL;
2207         *num_printers = 0;
2208         c1 = r.out.ctr.ctr1;
2209         for (i=0; i<c1->count; i++) {
2210                 if (c1->array[i].type != STYPE_PRINTQ) {
2211                         continue;
2212                 }
2213                 if (!add_string_to_array(ctx, c1->array[i].name,
2214                                          printers, num_printers)) {
2215                         talloc_free(ctx);
2216                         return NT_STATUS_NO_MEMORY;
2217                 }
2218         }
2219
2220         talloc_free(mem_ctx);
2221         return NT_STATUS_OK;
2222 }
2223
2224 static NTSTATUS getprinterinfo(TALLOC_CTX *ctx, struct dcerpc_pipe *pipe,
2225                                struct policy_handle *handle, int level,
2226                                union spoolss_PrinterInfo **res)
2227 {
2228         TALLOC_CTX *mem_ctx;
2229         struct spoolss_GetPrinter r;
2230         DATA_BLOB blob;
2231         NTSTATUS status;
2232
2233         mem_ctx = talloc_new(ctx);
2234         if (mem_ctx == NULL) {
2235                 return NT_STATUS_NO_MEMORY;
2236         }
2237
2238         r.in.handle = handle;
2239         r.in.level = level;
2240         r.in.buffer = NULL;
2241         r.in.offered = 0;
2242
2243         status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2244         if (!NT_STATUS_IS_OK(status)) {
2245                 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s\n",
2246                          __location__, nt_errstr(status));
2247                 talloc_free(mem_ctx);
2248                 return status;
2249         }
2250
2251         if (!W_ERROR_EQUAL(r.out.result, WERR_INSUFFICIENT_BUFFER)) {
2252                 printf("GetPrinter unexpected return code %s, should "
2253                        "be WERR_INSUFFICIENT_BUFFER\n",
2254                        win_errstr(r.out.result));
2255                 talloc_free(mem_ctx);
2256                 return NT_STATUS_UNSUCCESSFUL;
2257         }
2258
2259         r.in.handle = handle;
2260         r.in.level = level;
2261         blob = data_blob_talloc(mem_ctx, NULL, r.out.needed);
2262         if (blob.data == NULL) {
2263                 talloc_free(mem_ctx);
2264                 return NT_STATUS_NO_MEMORY;
2265         }
2266         memset(blob.data, 0, blob.length);
2267         r.in.buffer = &blob;
2268         r.in.offered = r.out.needed;
2269
2270         status = dcerpc_spoolss_GetPrinter(pipe, mem_ctx, &r);
2271         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2272                 d_printf("(%s) dcerpc_spoolss_GetPrinter failed: %s, "
2273                          "%s\n", __location__, nt_errstr(status),
2274                          win_errstr(r.out.result));
2275                 talloc_free(mem_ctx);
2276                 return NT_STATUS_IS_OK(status) ?
2277                         NT_STATUS_UNSUCCESSFUL : status;
2278         }
2279
2280         if (res != NULL) {
2281                 *res = talloc_steal(ctx, r.out.info);
2282         }
2283
2284         talloc_free(mem_ctx);
2285         return NT_STATUS_OK;
2286 }
2287         
2288
2289 BOOL torture_samba3_rpc_spoolss(struct torture_context *torture)
2290 {
2291         TALLOC_CTX *mem_ctx;
2292         BOOL ret = True;
2293         struct smbcli_state *cli;
2294         struct dcerpc_pipe *p;
2295         NTSTATUS status;
2296         struct policy_handle server_handle, printer_handle;
2297         const char **printers;
2298         int num_printers;
2299         struct spoolss_UserLevel1 userlevel1;
2300
2301         if (!(mem_ctx = talloc_new(torture))) {
2302                 return False;
2303         }
2304
2305         if (!(torture_open_connection_share(
2306                       mem_ctx, &cli, lp_parm_string(-1, "torture", "host"),
2307                       "IPC$", NULL))) {
2308                 d_printf("IPC$ connection failed\n");
2309                 talloc_free(mem_ctx);
2310                 return False;
2311         }
2312
2313         if (!NT_STATUS_IS_OK(find_printers(mem_ctx, cli->tree,
2314                                            &printers, &num_printers))) {
2315                 talloc_free(mem_ctx);
2316                 return False;
2317         }
2318
2319         if (num_printers == 0) {
2320                 d_printf("Did not find printers\n");
2321                 talloc_free(mem_ctx);
2322                 return True;
2323         }
2324
2325         status = pipe_bind_smb(mem_ctx, cli->tree, "\\spoolss",
2326                                &dcerpc_table_spoolss, &p);
2327         if (!NT_STATUS_IS_OK(status)) {
2328                 d_printf("(%s) pipe_bind_smb failed: %s\n", __location__,
2329                          nt_errstr(status));
2330                 talloc_free(mem_ctx);
2331                 return False;
2332         }
2333
2334         ZERO_STRUCT(userlevel1);
2335         userlevel1.client = talloc_asprintf(
2336                 mem_ctx, "\\\\%s", lp_netbios_name());
2337         userlevel1.user = cli_credentials_get_username(cmdline_credentials);
2338         userlevel1.build = 2600;
2339         userlevel1.major = 3;
2340         userlevel1.minor = 0;
2341         userlevel1.processor = 0;
2342
2343         {
2344                 struct spoolss_OpenPrinterEx r;
2345
2346                 ZERO_STRUCT(r);
2347                 r.in.printername = talloc_asprintf(
2348                         mem_ctx, "\\\\%s", dcerpc_server_name(p));
2349                 r.in.datatype = NULL;
2350                 r.in.access_mask = 0;
2351                 r.in.level = 1;
2352                 r.in.userlevel.level1 = &userlevel1;
2353                 r.out.handle = &server_handle;
2354
2355                 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2356                 if (!NT_STATUS_IS_OK(status)) {
2357                         d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2358                                  "%s\n", __location__, nt_errstr(status));
2359                         talloc_free(mem_ctx);
2360                         return False;
2361                 }
2362         }
2363
2364         {
2365                 struct spoolss_ClosePrinter r;
2366
2367                 r.in.handle = &server_handle;
2368                 r.out.handle = &server_handle;
2369
2370                 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2371                 if (!NT_STATUS_IS_OK(status)) {
2372                         d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2373                                  "%s\n", __location__, nt_errstr(status));
2374                         talloc_free(mem_ctx);
2375                         return False;
2376                 }
2377         }
2378
2379         {
2380                 struct spoolss_OpenPrinterEx r;
2381
2382                 ZERO_STRUCT(r);
2383                 r.in.printername = talloc_asprintf(
2384                         mem_ctx, "\\\\%s\\%s", dcerpc_server_name(p),
2385                         printers[0]);
2386                 r.in.datatype = NULL;
2387                 r.in.access_mask = 0;
2388                 r.in.level = 1;
2389                 r.in.userlevel.level1 = &userlevel1;
2390                 r.out.handle = &printer_handle;
2391
2392                 status = dcerpc_spoolss_OpenPrinterEx(p, mem_ctx, &r);
2393                 if (!NT_STATUS_IS_OK(status)) {
2394                         d_printf("(%s) dcerpc_spoolss_OpenPrinterEx failed: "
2395                                  "%s\n", __location__, nt_errstr(status));
2396                         talloc_free(mem_ctx);
2397                         return False;
2398                 }
2399         }
2400
2401         {
2402                 int i;
2403
2404                 for (i=0; i<8; i++) {
2405                         status = getprinterinfo(mem_ctx, p, &printer_handle,
2406                                                 i, NULL);
2407                         if (!NT_STATUS_IS_OK(status)) {
2408                                 d_printf("(%s) getprinterinfo %d failed: %s\n",
2409                                          __location__, i, nt_errstr(status));
2410                                 ret = False;
2411                         }
2412                 }
2413         }
2414
2415         {
2416                 struct spoolss_ClosePrinter r;
2417
2418                 r.in.handle = &printer_handle;
2419                 r.out.handle = &printer_handle;
2420
2421                 status = dcerpc_spoolss_ClosePrinter(p, mem_ctx, &r);
2422                 if (!NT_STATUS_IS_OK(status)) {
2423                         d_printf("(%s) dcerpc_spoolss_ClosePrinter failed: "
2424                                  "%s\n", __location__, nt_errstr(status));
2425                         talloc_free(mem_ctx);
2426                         return False;
2427                 }
2428         }
2429
2430         talloc_free(mem_ctx);
2431
2432         return ret;
2433 }