Update for new interface to netfileenum
[kai/samba.git] / source3 / rpcclient / cmd_srvsvc.c
1 /* 
2    Unix SMB/CIFS implementation.
3    RPC pipe client
4
5    Copyright (C) Andrew Tridgell 1992-1999
6    Copyright (C) Luke Kenneth Casson Leighton 1996 - 1999
7    Copyright (C) Tim Potter 2000,2002
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "rpcclient.h"
26
27 /* Display server query info */
28
29 static char *get_server_type_str(uint32 type)
30 {
31         static fstring typestr;
32         int i;
33
34         if (type == SV_TYPE_ALL) {
35                 fstrcpy(typestr, "All");
36                 return typestr;
37         }
38                 
39         typestr[0] = 0;
40
41         for (i = 0; i < 32; i++) {
42                 if (type & (1 << i)) {
43                         switch (1 << i) {
44                         case SV_TYPE_WORKSTATION:
45                                 fstrcat(typestr, "Wk ");
46                                 break;
47                         case SV_TYPE_SERVER:
48                                 fstrcat(typestr, "Sv ");
49                                 break;
50                         case SV_TYPE_SQLSERVER:
51                                 fstrcat(typestr, "Sql ");
52                                 break;
53                         case SV_TYPE_DOMAIN_CTRL:
54                                 fstrcat(typestr, "PDC ");
55                                 break;
56                         case SV_TYPE_DOMAIN_BAKCTRL:
57                                 fstrcat(typestr, "BDC ");
58                                 break;
59                         case SV_TYPE_TIME_SOURCE:
60                                 fstrcat(typestr, "Tim ");
61                                 break;
62                         case SV_TYPE_AFP:
63                                 fstrcat(typestr, "AFP ");
64                                 break;
65                         case SV_TYPE_NOVELL:
66                                 fstrcat(typestr, "Nov ");
67                                 break;
68                         case SV_TYPE_DOMAIN_MEMBER:
69                                 fstrcat(typestr, "Dom ");
70                                 break;
71                         case SV_TYPE_PRINTQ_SERVER:
72                                 fstrcat(typestr, "PrQ ");
73                                 break;
74                         case SV_TYPE_DIALIN_SERVER:
75                                 fstrcat(typestr, "Din ");
76                                 break;
77                         case SV_TYPE_SERVER_UNIX:
78                                 fstrcat(typestr, "Unx ");
79                                 break;
80                         case SV_TYPE_NT:
81                                 fstrcat(typestr, "NT ");
82                                 break;
83                         case SV_TYPE_WFW:
84                                 fstrcat(typestr, "Wfw ");
85                                 break;
86                         case SV_TYPE_SERVER_MFPN:
87                                 fstrcat(typestr, "Mfp ");
88                                 break;
89                         case SV_TYPE_SERVER_NT:
90                                 fstrcat(typestr, "SNT ");
91                                 break;
92                         case SV_TYPE_POTENTIAL_BROWSER:
93                                 fstrcat(typestr, "PtB ");
94                                 break;
95                         case SV_TYPE_BACKUP_BROWSER:
96                                 fstrcat(typestr, "BMB ");
97                                 break;
98                         case SV_TYPE_MASTER_BROWSER:
99                                 fstrcat(typestr, "LMB ");
100                                 break;
101                         case SV_TYPE_DOMAIN_MASTER:
102                                 fstrcat(typestr, "DMB ");
103                                 break;
104                         case SV_TYPE_SERVER_OSF:
105                                 fstrcat(typestr, "OSF ");
106                                 break;
107                         case SV_TYPE_SERVER_VMS:
108                                 fstrcat(typestr, "VMS ");
109                                 break;
110                         case SV_TYPE_WIN95_PLUS:
111                                 fstrcat(typestr, "W95 ");
112                                 break;
113                         case SV_TYPE_ALTERNATE_XPORT:
114                                 fstrcat(typestr, "Xpt ");
115                                 break;
116                         case SV_TYPE_LOCAL_LIST_ONLY:
117                                 fstrcat(typestr, "Dom ");
118                                 break;
119                         case SV_TYPE_DOMAIN_ENUM:
120                                 fstrcat(typestr, "Loc ");
121                                 break;
122                         }
123                 }
124         }
125
126         i = strlen(typestr) - 1;
127
128         if (typestr[i] == ' ')
129                 typestr[i] = 0;
130         
131         return typestr;
132 }
133
134 static void display_server(char *sname, uint32 type, const char *comment)
135 {
136         printf("\t%-15.15s%-20s %s\n", sname, get_server_type_str(type), 
137                comment);
138 }
139
140 static void display_srv_info_101(SRV_INFO_101 *sv101)
141 {
142         fstring name;
143         fstring comment;
144
145         unistr2_to_ascii(name, &sv101->uni_name, sizeof(name) - 1);
146         unistr2_to_ascii(comment, &sv101->uni_comment, sizeof(comment) - 1);
147
148         display_server(name, sv101->srv_type, comment);
149
150         printf("\tplatform_id     :\t%d\n", sv101->platform_id);
151         printf("\tos version      :\t%d.%d\n", sv101->ver_major, 
152                sv101->ver_minor);
153
154         printf("\tserver type     :\t0x%x\n", sv101->srv_type);
155 }
156
157 static void display_srv_info_102(SRV_INFO_102 *sv102)
158 {
159         fstring name;
160         fstring comment;
161         fstring usr_path;
162         
163         unistr2_to_ascii(name, &sv102->uni_name, sizeof(name) - 1);
164         unistr2_to_ascii(comment, &sv102->uni_comment, sizeof(comment) - 1);
165         unistr2_to_ascii(usr_path, &sv102->uni_usr_path, sizeof(usr_path) - 1);
166
167         display_server(name, sv102->srv_type, comment);
168
169         printf("\tplatform_id     :\t%d\n", sv102->platform_id);
170         printf("\tos version      :\t%d.%d\n", sv102->ver_major, 
171                sv102->ver_minor);
172
173         printf("\tusers           :\t%x\n", sv102->users);
174         printf("\tdisc, hidden    :\t%x, %x\n", sv102->disc, sv102->hidden);
175         printf("\tannounce, delta :\t%d, %d\n", sv102->announce, 
176                sv102->ann_delta);
177         printf("\tlicenses        :\t%d\n", sv102->licenses);
178         printf("\tuser path       :\t%s\n", usr_path);
179 }
180
181 /* Server query info */
182
183 static NTSTATUS cmd_srvsvc_srv_query_info(struct cli_state *cli, 
184                                           TALLOC_CTX *mem_ctx,
185                                           int argc, char **argv)
186 {
187         uint32 info_level = 101;
188         SRV_INFO_CTR ctr;
189         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
190
191         if (argc > 2) {
192                 printf("Usage: %s [infolevel]\n", argv[0]);
193                 return NT_STATUS_OK;
194         }
195
196         if (argc == 2)
197                 info_level = atoi(argv[1]);
198
199         result = cli_srvsvc_net_srv_get_info(cli, mem_ctx, info_level,
200                                              &ctr);
201
202         if (!NT_STATUS_IS_OK(result)) {
203                 goto done;
204         }
205
206         /* Display results */
207
208         switch (info_level) {
209         case 101:
210                 display_srv_info_101(&ctr.srv.sv101);
211                 break;
212         case 102:
213                 display_srv_info_102(&ctr.srv.sv102);
214                 break;
215         default:
216                 printf("unsupported info level %d\n", info_level);
217                 break;
218         }
219
220  done:
221         return result;
222 }
223
224 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
225 {
226         fstring netname = "", remark = "";
227
228         rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
229         rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
230
231         printf("netname: %s\n", netname);
232         printf("\tremark:\t%s\n", remark);
233 }
234
235 static void display_share_info_2(SRV_SHARE_INFO_2 *info2)
236 {
237         fstring netname = "", remark = "", path = "", passwd = "";
238
239         rpcstr_pull_unistr2_fstring(netname, &info2->info_2_str.uni_netname);
240         rpcstr_pull_unistr2_fstring(remark, &info2->info_2_str.uni_remark);
241         rpcstr_pull_unistr2_fstring(path, &info2->info_2_str.uni_path);
242         rpcstr_pull_unistr2_fstring(passwd, &info2->info_2_str.uni_passwd);
243
244         printf("netname: %s\n", netname);
245         printf("\tremark:\t%s\n", remark);
246         printf("\tpath:\t%s\n", path);
247         printf("\tpassword:\t%s\n", passwd);
248 }
249
250 static NTSTATUS cmd_srvsvc_net_share_enum(struct cli_state *cli, 
251                                           TALLOC_CTX *mem_ctx,
252                                           int argc, char **argv)
253 {
254         uint32 info_level = 2;
255         SRV_SHARE_INFO_CTR ctr;
256         WERROR result;
257         ENUM_HND hnd;
258         uint32 preferred_len = 0xffffffff, i;
259
260         if (argc > 2) {
261                 printf("Usage: %s [infolevel]\n", argv[0]);
262                 return NT_STATUS_OK;
263         }
264
265         if (argc == 2)
266                 info_level = atoi(argv[1]);
267
268         init_enum_hnd(&hnd, 0);
269
270         result = cli_srvsvc_net_share_enum(
271                 cli, mem_ctx, info_level, &ctr, preferred_len, &hnd);
272
273         if (!W_ERROR_IS_OK(result))
274                 goto done;
275
276         /* Display results */
277
278         switch (info_level) {
279         case 1:
280                 for (i = 0; i < ctr.num_entries; i++)
281                         display_share_info_1(&ctr.share.info1[i]);
282                 break;
283         case 2:
284                 for (i = 0; i < ctr.num_entries; i++)
285                         display_share_info_2(&ctr.share.info2[i]);
286                 break;
287         default:
288                 printf("unsupported info level %d\n", info_level);
289                 break;
290         }
291
292  done:
293         return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
294 }
295
296 static NTSTATUS cmd_srvsvc_net_remote_tod(struct cli_state *cli, 
297                                           TALLOC_CTX *mem_ctx,
298                                           int argc, char **argv)
299 {
300         TIME_OF_DAY_INFO tod;
301         WERROR result;
302
303         if (argc > 1) {
304                 printf("Usage: %s\n", argv[0]);
305                 return NT_STATUS_OK;
306         }
307
308         result = cli_srvsvc_net_remote_tod(
309                 cli, mem_ctx, cli->srv_name_slash, &tod);
310
311         if (!W_ERROR_IS_OK(result))
312                 goto done;
313
314  done:
315         return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
316 }
317
318 static NTSTATUS cmd_srvsvc_net_file_enum(struct cli_state *cli, 
319                                          TALLOC_CTX *mem_ctx,
320                                          int argc, char **argv)
321 {
322         uint32 info_level = 3;
323         SRV_FILE_INFO_CTR ctr;
324         WERROR result;
325         ENUM_HND hnd;
326         uint32 preferred_len = 0;
327
328         if (argc > 2) {
329                 printf("Usage: %s [infolevel]\n", argv[0]);
330                 return NT_STATUS_OK;
331         }
332
333         if (argc == 2)
334                 info_level = atoi(argv[1]);
335
336         init_enum_hnd(&hnd, 0);
337
338         ZERO_STRUCT(ctr);
339
340         result = cli_srvsvc_net_file_enum(
341                 cli, mem_ctx, info_level, NULL, &ctr, preferred_len, &hnd);
342
343         if (!W_ERROR_IS_OK(result))
344                 goto done;
345
346  done:
347         return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
348 }
349
350 /* List of commands exported by this module */
351
352 struct cmd_set srvsvc_commands[] = {
353
354         { "SRVSVC" },
355
356         { "srvinfo",    cmd_srvsvc_srv_query_info,  PIPE_SRVSVC, "Server query info", "" },
357         { "netshareenum", cmd_srvsvc_net_share_enum, PIPE_SRVSVC, "Enumerate shares", "" },
358         { "netfileenum", cmd_srvsvc_net_file_enum, PIPE_SRVSVC, "Enumerate open files", "" },
359         { "netremotetod", cmd_srvsvc_net_remote_tod, PIPE_SRVSVC, "Fetch remote time of day", "" },
360
361         { NULL }
362 };