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