r23792: convert Samba4 to GPLv3
[ira/wip.git] / source / 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 libnet_ListShares s)
33 {
34         int i, j;
35
36         struct share_type {
37                 enum srvsvc_ShareType type;
38                 const char *desc;
39         } share_types[] = {
40                 { STYPE_DISKTREE, "STYPE_DISKTREE" },
41                 { STYPE_DISKTREE_TEMPORARY, "STYPE_DISKTREE_TEMPORARY" },
42                 { STYPE_DISKTREE_HIDDEN, "STYPE_DISKTREE_HIDDEN" },
43                 { STYPE_PRINTQ, "STYPE_PRINTQ" },
44                 { STYPE_PRINTQ_TEMPORARY, "STYPE_PRINTQ_TEMPORARY" },
45                 { STYPE_PRINTQ_HIDDEN, "STYPE_PRINTQ_HIDDEN" },
46                 { STYPE_DEVICE, "STYPE_DEVICE" },
47                 { STYPE_DEVICE_TEMPORARY, "STYPE_DEVICE_TEMPORARY" },
48                 { STYPE_DEVICE_HIDDEN, "STYPE_DEVICE_HIDDEN" },
49                 { STYPE_IPC, "STYPE_IPC" },
50                 { STYPE_IPC_TEMPORARY, "STYPE_IPC_TEMPORARY" },
51                 { STYPE_IPC_HIDDEN, "STYPE_IPC_HIDDEN" }
52         };
53
54         switch (s.in.level) {
55         case 0:
56                 for (i = 0; i < s.out.ctr.ctr0->count; i++) {
57                         struct srvsvc_NetShareInfo0 *info = &s.out.ctr.ctr0->array[i];
58                         d_printf("\t[%d] %s\n", i, info->name);
59                 }
60                 break;
61
62         case 1:
63                 for (i = 0; i < s.out.ctr.ctr1->count; i++) {
64                         struct srvsvc_NetShareInfo1 *info = &s.out.ctr.ctr1->array[i];
65                         for (j = 0; j < ARRAY_SIZE(share_types); j++) {
66                                 if (share_types[j].type == info->type) break;
67                         }
68                         d_printf("\t[%d] %s (%s)\t%s\n", i, info->name,
69                                info->comment, share_types[j].desc);
70                 }
71                 break;
72
73         case 2:
74                 for (i = 0; i < s.out.ctr.ctr2->count; i++) {
75                         struct srvsvc_NetShareInfo2 *info = &s.out.ctr.ctr2->array[i];
76                         for (j = 0; j < ARRAY_SIZE(share_types); j++) {
77                                 if (share_types[j].type == info->type) break;
78                         }
79                         d_printf("\t[%d] %s\t%s\n\t    %s\n\t    [perms=0x%08x, max_usr=%d, cur_usr=%d, path=%s, pass=%s]\n",
80                                  i, info->name, share_types[j].desc, info->comment,
81                                  info->permissions, info->max_users,
82                                  info->current_users, info->path,
83                                  info->password);
84                 }
85                 break;
86
87         case 501:
88                 for (i = 0; i < s.out.ctr.ctr501->count; i++) {
89                         struct srvsvc_NetShareInfo501 *info = &s.out.ctr.ctr501->array[i];
90                         for (j = 0; j < ARRAY_SIZE(share_types); j++) {
91                                 if (share_types[j].type == info->type) break;
92                         }
93                         d_printf("\t[%d] %s\t%s [csc_policy=0x%08x]\n\t    %s\n", i, info->name,
94                                  share_types[j].desc, info->csc_policy,
95                                  info->comment);
96                 }
97                 break;
98
99         case 502:
100                 for (i = 0; i < s.out.ctr.ctr502->count; i++) {
101                         struct srvsvc_NetShareInfo502 *info = &s.out.ctr.ctr502->array[i];
102                         for (j = 0; j < ARRAY_SIZE(share_types); j++) {
103                                 if (share_types[j].type == info->type) break;
104                         }
105                         d_printf("\t[%d] %s\t%s\n\t    %s\n\t    [perms=0x%08x, max_usr=%d, cur_usr=%d, path=%s, pass=%s, unknown=0x%08x]\n",
106                                  i, info->name, share_types[j].desc, info->comment,
107                                  info->permissions, info->max_users,
108                                  info->current_users, info->path,
109                                  info->password, info->unknown);
110                 }
111                 break;
112         }
113 }
114
115
116 BOOL torture_listshares(struct torture_context *torture)
117 {
118         struct libnet_ListShares share;
119         NTSTATUS  status;
120         uint32_t levels[] = { 0, 1, 2, 501, 502 };
121         int i;
122         BOOL ret = True;
123         struct libnet_context* libnetctx;
124         const char *binding;
125         struct dcerpc_binding *bind;
126         TALLOC_CTX *mem_ctx;
127
128         mem_ctx = talloc_init("test_listshares");
129         binding = torture_setting_string(torture, "binding", NULL);
130         status = dcerpc_parse_binding(mem_ctx, binding, &bind);
131         if (!NT_STATUS_IS_OK(status)) {
132                 printf("Error while parsing the binding string\n");
133                 ret = False;
134                 goto done;
135         }
136
137         libnetctx = libnet_context_init(NULL);
138         if (!libnetctx) {
139                 printf("Couldn't allocate libnet context\n");
140                 ret = False;
141                 goto done;
142         }
143
144         libnetctx->cred = cmdline_credentials;
145         
146         printf("Testing libnet_ListShare\n");
147         
148         share.in.server_name = talloc_asprintf(mem_ctx, "%s", bind->host);
149
150         for (i = 0; i < ARRAY_SIZE(levels); i++) {
151                 share.in.level = levels[i];
152                 printf("testing libnet_ListShare level %u\n", share.in.level);
153
154                 status = libnet_ListShares(libnetctx, mem_ctx, &share);
155                 if (!NT_STATUS_IS_OK(status)) {
156                         printf("libnet_ListShare level %u failed - %s\n", share.in.level, share.out.error_string);
157                         ret = False;
158                         goto done;
159                 }
160
161                 printf("listing shares:\n");
162                 test_displayshares(share);
163         }
164
165 done:
166         talloc_free(mem_ctx);
167         return ret;
168 }
169
170
171 static BOOL test_addshare(struct dcerpc_pipe *svc_pipe, TALLOC_CTX *mem_ctx, const char *host,
172                           const char* share)
173 {
174         NTSTATUS status;
175         struct srvsvc_NetShareAdd add;
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         add.in.server_unc = host;
187         add.in.level      = 2;
188         add.in.info.info2 = &i;
189
190         status = dcerpc_srvsvc_NetShareAdd(svc_pipe, mem_ctx, &add);
191         if (!NT_STATUS_IS_OK(status)) {
192                 printf("Failed to add a new share\n");
193                 return False;
194         }
195
196         printf("share added\n");
197         return True;
198 }
199
200
201 BOOL torture_delshare(struct torture_context *torture)
202 {
203         struct dcerpc_pipe *p;
204         struct dcerpc_binding *bind;
205         struct libnet_context* libnetctx;
206         const char *host, *binding;
207         TALLOC_CTX *mem_ctx;
208         NTSTATUS  status;
209         BOOL ret = True;
210         struct libnet_DelShare share;
211         
212         mem_ctx = talloc_init("test_listshares");
213         host = torture_setting_string(torture, "host", NULL);
214         binding = torture_setting_string(torture, "binding", NULL);
215         status = dcerpc_parse_binding(mem_ctx, binding, &bind);
216         if (!NT_STATUS_IS_OK(status)) {
217                 printf("Error while parsing the binding string\n");
218                 ret = False;
219                 goto done;
220         }
221
222         libnetctx = libnet_context_init(NULL);
223         libnetctx->cred = cmdline_credentials;
224
225         status = torture_rpc_connection(mem_ctx,
226                                         &p,
227                                         &dcerpc_table_srvsvc);
228
229         if (!test_addshare(p, mem_ctx, host, TEST_SHARENAME)) {
230                 ret = False;
231                 goto done;
232         }
233
234         share.in.server_name    = bind->host;
235         share.in.share_name     = TEST_SHARENAME;
236
237         status = libnet_DelShare(libnetctx, mem_ctx, &share);
238         if (!NT_STATUS_IS_OK(status)) {
239                 ret = False;
240                 goto done;
241         }
242
243
244 done:
245         talloc_free(mem_ctx);
246         return ret;
247 }