b6b0b37d016fc443f774f81398b58843e4c5973f
[kai/samba.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 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         struct dcerpc_binding *bind;
125         TALLOC_CTX *mem_ctx;
126
127         mem_ctx = talloc_init("test_listshares");
128         status = torture_rpc_binding(torture, &bind);
129         if (!NT_STATUS_IS_OK(status)) {
130                 ret = False;
131                 goto done;
132         }
133
134         libnetctx = libnet_context_init(NULL);
135         if (!libnetctx) {
136                 printf("Couldn't allocate libnet context\n");
137                 ret = False;
138                 goto done;
139         }
140
141         libnetctx->cred = cmdline_credentials;
142         
143         printf("Testing libnet_ListShare\n");
144         
145         share.in.server_name = talloc_asprintf(mem_ctx, "%s", bind->host);
146
147         for (i = 0; i < ARRAY_SIZE(levels); i++) {
148                 share.in.level = levels[i];
149                 printf("testing libnet_ListShare level %u\n", share.in.level);
150
151                 status = libnet_ListShares(libnetctx, mem_ctx, &share);
152                 if (!NT_STATUS_IS_OK(status)) {
153                         printf("libnet_ListShare level %u failed - %s\n", share.in.level, share.out.error_string);
154                         ret = False;
155                         goto done;
156                 }
157
158                 printf("listing shares:\n");
159                 test_displayshares(share);
160         }
161
162 done:
163         talloc_free(mem_ctx);
164         return ret;
165 }
166
167
168 static BOOL test_addshare(struct dcerpc_pipe *svc_pipe, TALLOC_CTX *mem_ctx, const char *host,
169                           const char* share)
170 {
171         NTSTATUS status;
172         struct srvsvc_NetShareAdd add;
173         struct srvsvc_NetShareInfo2 i;
174         
175         i.name         = share;
176         i.type         = STYPE_DISKTREE;
177         i.path         = "C:\\WINDOWS\\TEMP";
178         i.max_users    = 5;
179         i.comment      = "Comment to the test share";
180         i.password     = NULL;
181         i.permissions  = 0x0;
182
183         add.in.server_unc = host;
184         add.in.level      = 2;
185         add.in.info.info2 = &i;
186
187         status = dcerpc_srvsvc_NetShareAdd(svc_pipe, mem_ctx, &add);
188         if (!NT_STATUS_IS_OK(status)) {
189                 printf("Failed to add a new share\n");
190                 return False;
191         }
192
193         printf("share added\n");
194         return True;
195 }
196
197
198 BOOL torture_delshare(struct torture_context *torture)
199 {
200         struct dcerpc_pipe *p;
201         struct dcerpc_binding *bind;
202         struct libnet_context* libnetctx;
203         const char *host;
204         TALLOC_CTX *mem_ctx;
205         NTSTATUS  status;
206         BOOL ret = True;
207         struct libnet_DelShare share;
208         
209         mem_ctx = talloc_init("test_listshares");
210         host = torture_setting_string(torture, "host", NULL);
211         status = torture_rpc_binding(torture, &bind);
212         if (!NT_STATUS_IS_OK(status)) {
213                 ret = False;
214                 goto done;
215         }
216
217         libnetctx = libnet_context_init(NULL);
218         libnetctx->cred = cmdline_credentials;
219
220         status = torture_rpc_connection(torture,
221                                         &p,
222                                         &ndr_table_srvsvc);
223
224         if (!test_addshare(p, mem_ctx, host, TEST_SHARENAME)) {
225                 ret = False;
226                 goto done;
227         }
228
229         share.in.server_name    = bind->host;
230         share.in.share_name     = TEST_SHARENAME;
231
232         status = libnet_DelShare(libnetctx, mem_ctx, &share);
233         if (!NT_STATUS_IS_OK(status)) {
234                 ret = False;
235                 goto done;
236         }
237
238
239 done:
240         talloc_free(mem_ctx);
241         return ret;
242 }