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