Removed 'extern int DEBUGLEVEL' as it is now in the smb.h header.
[samba.git] / source3 / rpcclient / cmd_dfs.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.2
4    RPC pipe client
5
6    Copyright (C) Tim Potter 2000
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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 extern pstring server;
26
27 /* Check DFS is supported by the remote server */
28
29 static NTSTATUS cmd_dfs_exist(struct cli_state *cli, int argc, char **argv)
30 {
31         TALLOC_CTX *mem_ctx;
32         BOOL dfs_exists;
33         NTSTATUS result;
34
35         if (argc != 1) {
36                 printf("Usage: %s\n", argv[0]);
37                 return NT_STATUS_OK;
38         }
39
40         if (!(mem_ctx = talloc_init())) {
41                 DEBUG(0,("cmd_dfs_exist: talloc_init failed\n"));
42                 return NT_STATUS_UNSUCCESSFUL;
43         }
44
45         /* Initialise RPC connection */
46
47         if (!cli_nt_session_open (cli, PIPE_NETDFS)) {
48                 DEBUG(0, ("Could not initialize netdfs pipe!\n"));
49                 result = NT_STATUS_UNSUCCESSFUL;
50                 goto done;
51         }
52
53         result = cli_dfs_exist(cli, mem_ctx, &dfs_exists);
54
55         if (NT_STATUS_IS_OK(result))
56                 printf("dfs is %spresent\n", dfs_exists ? "" : "not ");
57
58         cli_nt_session_close(cli);
59
60 done:
61         talloc_destroy(mem_ctx);
62         return result;
63 }
64
65 static NTSTATUS cmd_dfs_add(struct cli_state *cli, int argc, char **argv)
66 {
67         TALLOC_CTX *mem_ctx;
68         NTSTATUS result;
69         char *entrypath, *servername, *sharename, *comment;
70         uint32 flags = 0;
71
72         if (argc != 5) {
73                 printf("Usage: %s entrypath servername sharename comment\n", 
74                        argv[0]);
75                 return NT_STATUS_OK;
76         }
77
78         entrypath = argv[1];
79         servername = argv[2];
80         sharename = argv[3];
81         comment = argv[4];
82
83         if (!(mem_ctx = talloc_init())) {
84                 DEBUG(0,("cmd_dfs_add: talloc_init failed\n"));
85                 return NT_STATUS_UNSUCCESSFUL;
86         }
87
88         /* Initialise RPC connection */
89
90         if (!cli_nt_session_open (cli, PIPE_NETDFS)) {
91                 DEBUG(0, ("Could not initialize netdfs pipe!\n"));
92                 result = NT_STATUS_UNSUCCESSFUL;
93                 goto done;
94         }
95
96         result = cli_dfs_add(cli, mem_ctx, entrypath, servername, 
97                              sharename, comment, flags);
98
99         cli_nt_session_close(cli);
100
101 done:
102         talloc_destroy(mem_ctx);
103         return result;
104 }
105
106 static NTSTATUS cmd_dfs_remove(struct cli_state *cli, int argc, char **argv)
107 {
108         TALLOC_CTX *mem_ctx;
109         NTSTATUS result;
110         char *entrypath, *servername, *sharename;
111
112         if (argc != 4) {
113                 printf("Usage: %s entrypath servername sharename\n", argv[0]);
114                 return NT_STATUS_OK;
115         }
116
117         entrypath = argv[1];
118         servername = argv[2];
119         sharename = argv[3];
120
121         if (!(mem_ctx = talloc_init())) {
122                 DEBUG(0,("cmd_dfs_remove: talloc_init failed\n"));
123                 return NT_STATUS_UNSUCCESSFUL;
124         }
125
126         /* Initialise RPC connection */
127
128         if (!cli_nt_session_open (cli, PIPE_NETDFS)) {
129                 DEBUG(0, ("Could not initialize netdfs pipe!\n"));
130                 result = NT_STATUS_UNSUCCESSFUL;
131                 goto done;
132         }
133
134         result = cli_dfs_remove(cli, mem_ctx, entrypath, servername, 
135                                 sharename);
136
137         cli_nt_session_close(cli);
138
139 done:
140         talloc_destroy(mem_ctx);
141         return result;
142 }
143
144 /* Display a DFS_INFO_1 structure */
145
146 static void display_dfs_info_1(DFS_INFO_1 *info1)
147 {
148         fstring temp;
149
150         unistr2_to_ascii(temp, &info1->entrypath, sizeof(temp) - 1);
151         printf("entrypath: %s\n", temp);
152 }
153
154 /* Display a DFS_INFO_2 structure */
155
156 static void display_dfs_info_2(DFS_INFO_2 *info2)
157 {
158         fstring temp;
159
160         unistr2_to_ascii(temp, &info2->entrypath, sizeof(temp) - 1);
161         printf("entrypath: %s\n", temp);
162
163         unistr2_to_ascii(temp, &info2->comment, sizeof(temp) - 1);
164         printf("\tcomment: %s\n", temp);
165
166         printf("\tstate: %d\n", info2->state);
167         printf("\tnum_storages: %d\n", info2->num_storages);
168 }
169
170 /* Display a DFS_INFO_3 structure */
171
172 static void display_dfs_info_3(DFS_INFO_3 *info3)
173 {
174         fstring temp;
175         int i;
176
177         unistr2_to_ascii(temp, &info3->entrypath, sizeof(temp) - 1);
178         printf("entrypath: %s\n", temp);
179
180         unistr2_to_ascii(temp, &info3->comment, sizeof(temp) - 1);
181         printf("\tcomment: %s\n", temp);
182
183         printf("\tstate: %d\n", info3->state);
184         printf("\tnum_storages: %d\n", info3->num_storages);
185
186         for (i = 0; i < info3->num_storages; i++) {
187                 DFS_STORAGE_INFO *dsi = &info3->storages[i];
188
189                 unistr2_to_ascii(temp, &dsi->servername, sizeof(temp) - 1);
190                 printf("\t\tstorage[%d] servername: %s\n", i, temp);
191
192                 unistr2_to_ascii(temp, &dsi->sharename, sizeof(temp) - 1);
193                 printf("\t\tstorage[%d] sharename: %s\n", i, temp);
194         }
195 }
196
197 /* Display a DFS_INFO_CTR structure */
198
199 static void display_dfs_info_ctr(DFS_INFO_CTR *ctr)
200 {
201         int i;
202
203         for (i = 0; i < ctr->num_entries; i++) {
204                 switch (ctr->switch_value) {
205                 case 0x01:
206                         display_dfs_info_1(&ctr->dfs.info1[i]);
207                         break;
208                 case 0x02:
209                         display_dfs_info_2(&ctr->dfs.info2[i]);
210                         break;
211                 case 0x03:
212                         display_dfs_info_3(&ctr->dfs.info3[i]);
213                         break;
214                 default:
215                         printf("unsupported info level %d\n", 
216                                ctr->switch_value);
217                         break;
218                 }
219         }
220 }
221
222 /* Enumerate dfs shares */
223
224 static NTSTATUS cmd_dfs_enum(struct cli_state *cli, int argc, char **argv)
225 {
226         TALLOC_CTX *mem_ctx;
227         DFS_INFO_CTR ctr;
228         NTSTATUS result;
229         uint32 info_level = 1;
230
231         if (argc > 2) {
232                 printf("Usage: %s [info_level]\n", argv[0]);
233                 return NT_STATUS_OK;
234         }
235
236         if (argc == 2)
237                 info_level = atoi(argv[1]);
238
239         if (!(mem_ctx = talloc_init())) {
240                 DEBUG(0,("cmd_dfs_enum: talloc_init failed\n"));
241                 return NT_STATUS_NO_MEMORY;
242         }
243
244         /* Initialise RPC connection */
245
246         if (!cli_nt_session_open (cli, PIPE_NETDFS)) {
247                 DEBUG(0, ("Could not initialize netdfs pipe!\n"));
248                 result = NT_STATUS_UNSUCCESSFUL;
249                 goto done;
250         }
251
252         /* Call RPC function */
253
254         result = cli_dfs_enum(cli, mem_ctx, info_level, &ctr);
255         if (NT_STATUS_IS_OK(result)) {
256                 /* Print results */
257                 display_dfs_info_ctr(&ctr);
258         }
259
260         cli_nt_session_close(cli);
261
262 done:
263         talloc_destroy(mem_ctx);
264         return result;
265 }
266
267 static NTSTATUS cmd_dfs_getinfo(struct cli_state *cli, int argc, char **argv)
268 {
269         TALLOC_CTX *mem_ctx;
270         NTSTATUS result;
271         char *entrypath, *servername, *sharename;
272         uint32 info_level = 1;
273         DFS_INFO_CTR ctr;
274
275         if (argc < 4 || argc > 5) {
276                 printf("Usage: %s entrypath servername sharename [info_level]\n", argv[0]);
277                 return NT_STATUS_OK;
278         }
279
280         entrypath = argv[1];
281         servername = argv[2];
282         sharename = argv[3];
283
284         if (argc == 5)
285                 info_level = atoi(argv[4]);
286
287         if (!(mem_ctx = talloc_init())) {
288                 DEBUG(0,("cmd_dfs_getinfo: talloc_init failed\n"));
289                 return NT_STATUS_UNSUCCESSFUL;
290         }
291
292         /* Initialise RPC connection */
293
294         if (!cli_nt_session_open (cli, PIPE_NETDFS)) {
295                 DEBUG(0, ("Could not initialize netdfs pipe!\n"));
296                 result = NT_STATUS_UNSUCCESSFUL;
297                 goto done;
298         }
299
300         /* Call RPC function */
301
302         result = cli_dfs_get_info(cli, mem_ctx, entrypath, servername, 
303                                   sharename, info_level, &ctr);
304         if (NT_STATUS_IS_OK(result)) {
305                 /* Print results */
306                 display_dfs_info_ctr(&ctr);
307         }
308
309         cli_nt_session_close(cli);
310
311 done:
312         talloc_destroy(mem_ctx);
313         return result;
314 }
315
316 /* List of commands exported by this module */
317
318 struct cmd_set dfs_commands[] = {
319
320         { "DFS" },
321
322         { "dfsexist",   cmd_dfs_exist,   "Query DFS support",    "" },
323         { "dfsadd",     cmd_dfs_add,     "Add a DFS share",      "" },
324         { "dfsremove",  cmd_dfs_remove,  "Remove a DFS share",   "" },
325         { "dfsgetinfo", cmd_dfs_getinfo, "Query DFS share info", "" },
326         { "dfsenum",    cmd_dfs_enum,    "Enumerate dfs shares", "" },
327
328         { NULL }
329 };