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