s4: popt: Global replace of cmdline_credentials -> popt_get_cmdline_credentials().
[gd/samba-autobuild/.git] / source4 / torture / libnet / libnet_share.c
1 /*
2    Unix SMB/CIFS implementation.
3    Test suite for libnet calls.
4
5    Copyright (C) Gregory LEOCADIE <gleocadie@idealx.com> 2005
6    Copyright (C) Rafal Szczesniak  2005
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 "torture/rpc/torture_rpc.h"
24 #include "libnet/libnet.h"
25 #include "lib/cmdline/popt_common.h"
26 #include "librpc/gen_ndr/ndr_srvsvc_c.h"
27 #include "torture/libnet/proto.h"
28
29
30 #define TEST_SHARENAME "libnetsharetest"
31
32
33 static void test_displayshares(struct torture_context *tctx,
34                                struct libnet_ListShares s)
35 {
36         int i, j;
37
38         struct share_type {
39                 enum srvsvc_ShareType type;
40                 const char *desc;
41         } share_types[] = {
42                 { STYPE_DISKTREE, "STYPE_DISKTREE" },
43                 { STYPE_DISKTREE_TEMPORARY, "STYPE_DISKTREE_TEMPORARY" },
44                 { STYPE_DISKTREE_HIDDEN, "STYPE_DISKTREE_HIDDEN" },
45                 { STYPE_PRINTQ, "STYPE_PRINTQ" },
46                 { STYPE_PRINTQ_TEMPORARY, "STYPE_PRINTQ_TEMPORARY" },
47                 { STYPE_PRINTQ_HIDDEN, "STYPE_PRINTQ_HIDDEN" },
48                 { STYPE_DEVICE, "STYPE_DEVICE" },
49                 { STYPE_DEVICE_TEMPORARY, "STYPE_DEVICE_TEMPORARY" },
50                 { STYPE_DEVICE_HIDDEN, "STYPE_DEVICE_HIDDEN" },
51                 { STYPE_IPC, "STYPE_IPC" },
52                 { STYPE_IPC_TEMPORARY, "STYPE_IPC_TEMPORARY" },
53                 { STYPE_IPC_HIDDEN, "STYPE_IPC_HIDDEN" }
54         };
55
56         switch (s.in.level) {
57         case 0:
58                 for (i = 0; i < s.out.ctr.ctr0->count; i++) {
59                         struct srvsvc_NetShareInfo0 *info = &s.out.ctr.ctr0->array[i];
60                         torture_comment(tctx, "\t[%d] %s\n", i, info->name);
61                 }
62                 break;
63
64         case 1:
65                 for (i = 0; i < s.out.ctr.ctr1->count; i++) {
66                         struct srvsvc_NetShareInfo1 *info = &s.out.ctr.ctr1->array[i];
67                         for (j = 0; j < ARRAY_SIZE(share_types); j++) {
68                                 if (share_types[j].type == info->type) break;
69                         }
70                         torture_comment(tctx, "\t[%d] %s (%s)\t%s\n", i, info->name,
71                                info->comment, share_types[j].desc);
72                 }
73                 break;
74
75         case 2:
76                 for (i = 0; i < s.out.ctr.ctr2->count; i++) {
77                         struct srvsvc_NetShareInfo2 *info = &s.out.ctr.ctr2->array[i];
78                         for (j = 0; j < ARRAY_SIZE(share_types); j++) {
79                                 if (share_types[j].type == info->type) break;
80                         }
81                         torture_comment(tctx, "\t[%d] %s\t%s\n\t    %s\n\t    [perms=0x%08x, max_usr=%d, cur_usr=%d, path=%s, pass=%s]\n",
82                                  i, info->name, share_types[j].desc, info->comment,
83                                  info->permissions, info->max_users,
84                                  info->current_users, info->path,
85                                  info->password);
86                 }
87                 break;
88
89         case 501:
90                 for (i = 0; i < s.out.ctr.ctr501->count; i++) {
91                         struct srvsvc_NetShareInfo501 *info = &s.out.ctr.ctr501->array[i];
92                         for (j = 0; j < ARRAY_SIZE(share_types); j++) {
93                                 if (share_types[j].type == info->type) break;
94                         }
95                         torture_comment(tctx, "\t[%d] %s\t%s [csc_policy=0x%08x]\n\t    %s\n", i, info->name,
96                                  share_types[j].desc, info->csc_policy,
97                                  info->comment);
98                 }
99                 break;
100
101         case 502:
102                 for (i = 0; i < s.out.ctr.ctr502->count; i++) {
103                         struct srvsvc_NetShareInfo502 *info = &s.out.ctr.ctr502->array[i];
104                         for (j = 0; j < ARRAY_SIZE(share_types); j++) {
105                                 if (share_types[j].type == info->type) break;
106                         }
107                         torture_comment(tctx, "\t[%d] %s\t%s\n\t    %s\n\t    [perms=0x%08x, max_usr=%d, cur_usr=%d, path=%s, pass=%s]\n",
108                                  i, info->name, share_types[j].desc, info->comment,
109                                  info->permissions, info->max_users,
110                                  info->current_users, info->path,
111                                  info->password);
112                 }
113                 break;
114         }
115 }
116
117
118 bool torture_listshares(struct torture_context *torture)
119 {
120         struct libnet_ListShares share;
121         NTSTATUS  status;
122         uint32_t levels[] = { 0, 1, 2, 501, 502 };
123         int i;
124         bool ret = true;
125         struct libnet_context* libnetctx;
126         struct dcerpc_binding *binding;
127         TALLOC_CTX *mem_ctx;
128
129         mem_ctx = talloc_init("test_listshares");
130         status = torture_rpc_binding(torture, &binding);
131         if (!NT_STATUS_IS_OK(status)) {
132                 ret = false;
133                 goto done;
134         }
135
136         libnetctx = libnet_context_init(torture->ev, torture->lp_ctx);
137         if (!libnetctx) {
138                 torture_comment(torture, "Couldn't allocate libnet context\n");
139                 ret = false;
140                 goto done;
141         }
142
143         libnetctx->cred = popt_get_cmdline_credentials();
144
145         torture_comment(torture, "Testing libnet_ListShare\n");
146
147         share.in.server_name = dcerpc_binding_get_string_option(binding, "host");
148
149         for (i = 0; i < ARRAY_SIZE(levels); i++) {
150                 share.in.level = levels[i];
151                 torture_comment(torture, "Testing libnet_ListShare level %u\n", share.in.level);
152
153                 status = libnet_ListShares(libnetctx, mem_ctx, &share);
154                 if (!NT_STATUS_IS_OK(status)) {
155                         torture_comment(torture, "libnet_ListShare level %u failed - %s\n", share.in.level, share.out.error_string);
156                         ret = false;
157                         goto done;
158                 }
159
160                 torture_comment(torture, "listing shares:\n");
161                 test_displayshares(torture, share);
162         }
163
164 done:
165         talloc_free(mem_ctx);
166         return ret;
167 }
168
169
170 static bool test_addshare(struct torture_context *tctx,
171                           struct dcerpc_binding_handle *b, TALLOC_CTX *mem_ctx, const char *host,
172                           const char* share)
173 {
174         NTSTATUS status;
175         struct srvsvc_NetShareAdd add;
176         union srvsvc_NetShareInfo info;
177         struct srvsvc_NetShareInfo2 i;
178
179         ZERO_STRUCT(i);
180         ZERO_STRUCT(info);
181         ZERO_STRUCT(add);
182
183         i.name         = share;
184         i.type         = STYPE_DISKTREE;
185         i.path         = "C:\\WINDOWS\\TEMP";
186         i.max_users    = 5;
187         i.comment      = "Comment to the test share";
188         i.password     = NULL;
189         i.permissions  = 0x0;
190
191         info.info2 = &i;
192
193         add.in.server_unc = host;
194         add.in.level      = 2;
195         add.in.info       = &info;
196         add.in.parm_error = NULL;
197
198         status = dcerpc_srvsvc_NetShareAdd_r(b, mem_ctx, &add);
199         if (!NT_STATUS_IS_OK(status)) {
200                 torture_comment(tctx, "Failed to add a new share\n");
201                 return false;
202         }
203
204         torture_comment(tctx, "share added\n");
205         return true;
206 }
207
208
209 bool torture_delshare(struct torture_context *torture)
210 {
211         struct dcerpc_pipe *p;
212         struct dcerpc_binding *binding;
213         struct libnet_context* libnetctx;
214         const char *host;
215         NTSTATUS  status;
216         bool ret = true;
217         struct libnet_DelShare share;
218
219         host = torture_setting_string(torture, "host", NULL);
220         status = torture_rpc_binding(torture, &binding);
221         torture_assert_ntstatus_ok(torture, status, "Failed to get binding");
222
223         libnetctx = libnet_context_init(torture->ev, torture->lp_ctx);
224         libnetctx->cred = popt_get_cmdline_credentials();
225
226         status = torture_rpc_connection(torture,
227                                         &p,
228                                         &ndr_table_srvsvc);
229
230         torture_assert_ntstatus_ok(torture, status, "Failed to get rpc connection");
231
232         if (!test_addshare(torture, p->binding_handle, torture, host, TEST_SHARENAME)) {
233                 return false;
234         }
235
236         share.in.server_name    = dcerpc_binding_get_string_option(binding, "host");
237         share.in.share_name     = TEST_SHARENAME;
238
239         status = libnet_DelShare(libnetctx, torture, &share);
240         torture_assert_ntstatus_ok(torture, status, "Failed to delete share");
241
242         return ret;
243 }