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