Merge branch 'v3-2-test' of ssh://git.samba.org/data/git/samba into v3-2-test
[kai/samba-autobuild/.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 3 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, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "rpcclient.h"
25
26 /* Display server query info */
27
28 static char *get_server_type_str(uint32 type)
29 {
30         static fstring typestr;
31         int i;
32
33         if (type == SV_TYPE_ALL) {
34                 fstrcpy(typestr, "All");
35                 return typestr;
36         }
37                 
38         typestr[0] = 0;
39
40         for (i = 0; i < 32; i++) {
41                 if (type & (1 << i)) {
42                         switch (1 << i) {
43                         case SV_TYPE_WORKSTATION:
44                                 fstrcat(typestr, "Wk ");
45                                 break;
46                         case SV_TYPE_SERVER:
47                                 fstrcat(typestr, "Sv ");
48                                 break;
49                         case SV_TYPE_SQLSERVER:
50                                 fstrcat(typestr, "Sql ");
51                                 break;
52                         case SV_TYPE_DOMAIN_CTRL:
53                                 fstrcat(typestr, "PDC ");
54                                 break;
55                         case SV_TYPE_DOMAIN_BAKCTRL:
56                                 fstrcat(typestr, "BDC ");
57                                 break;
58                         case SV_TYPE_TIME_SOURCE:
59                                 fstrcat(typestr, "Tim ");
60                                 break;
61                         case SV_TYPE_AFP:
62                                 fstrcat(typestr, "AFP ");
63                                 break;
64                         case SV_TYPE_NOVELL:
65                                 fstrcat(typestr, "Nov ");
66                                 break;
67                         case SV_TYPE_DOMAIN_MEMBER:
68                                 fstrcat(typestr, "Dom ");
69                                 break;
70                         case SV_TYPE_PRINTQ_SERVER:
71                                 fstrcat(typestr, "PrQ ");
72                                 break;
73                         case SV_TYPE_DIALIN_SERVER:
74                                 fstrcat(typestr, "Din ");
75                                 break;
76                         case SV_TYPE_SERVER_UNIX:
77                                 fstrcat(typestr, "Unx ");
78                                 break;
79                         case SV_TYPE_NT:
80                                 fstrcat(typestr, "NT ");
81                                 break;
82                         case SV_TYPE_WFW:
83                                 fstrcat(typestr, "Wfw ");
84                                 break;
85                         case SV_TYPE_SERVER_MFPN:
86                                 fstrcat(typestr, "Mfp ");
87                                 break;
88                         case SV_TYPE_SERVER_NT:
89                                 fstrcat(typestr, "SNT ");
90                                 break;
91                         case SV_TYPE_POTENTIAL_BROWSER:
92                                 fstrcat(typestr, "PtB ");
93                                 break;
94                         case SV_TYPE_BACKUP_BROWSER:
95                                 fstrcat(typestr, "BMB ");
96                                 break;
97                         case SV_TYPE_MASTER_BROWSER:
98                                 fstrcat(typestr, "LMB ");
99                                 break;
100                         case SV_TYPE_DOMAIN_MASTER:
101                                 fstrcat(typestr, "DMB ");
102                                 break;
103                         case SV_TYPE_SERVER_OSF:
104                                 fstrcat(typestr, "OSF ");
105                                 break;
106                         case SV_TYPE_SERVER_VMS:
107                                 fstrcat(typestr, "VMS ");
108                                 break;
109                         case SV_TYPE_WIN95_PLUS:
110                                 fstrcat(typestr, "W95 ");
111                                 break;
112                         case SV_TYPE_ALTERNATE_XPORT:
113                                 fstrcat(typestr, "Xpt ");
114                                 break;
115                         case SV_TYPE_LOCAL_LIST_ONLY:
116                                 fstrcat(typestr, "Dom ");
117                                 break;
118                         case SV_TYPE_DOMAIN_ENUM:
119                                 fstrcat(typestr, "Loc ");
120                                 break;
121                         }
122                 }
123         }
124
125         i = strlen(typestr) - 1;
126
127         if (typestr[i] == ' ')
128                 typestr[i] = 0;
129         
130         return typestr;
131 }
132
133 static void display_server(const char *sname, uint32 type, const char *comment)
134 {
135         printf("\t%-15.15s%-20s %s\n", sname, get_server_type_str(type), 
136                comment);
137 }
138
139 static void display_srv_info_101(struct srvsvc_NetSrvInfo101 *r)
140 {
141         display_server(r->server_name, r->server_type, r->comment);
142
143         printf("\tplatform_id     :\t%d\n", r->platform_id);
144         printf("\tos version      :\t%d.%d\n",
145                 r->version_major, r->version_minor);
146         printf("\tserver type     :\t0x%x\n", r->server_type);
147 }
148
149 static void display_srv_info_102(struct srvsvc_NetSrvInfo102 *r)
150 {
151         display_server(r->server_name, r->server_type, r->comment);
152
153         printf("\tplatform_id     :\t%d\n", r->platform_id);
154         printf("\tos version      :\t%d.%d\n",
155                 r->version_major, r->version_minor);
156         printf("\tserver type     :\t0x%x\n", r->server_type);
157
158         printf("\tusers           :\t%x\n", r->users);
159         printf("\tdisc, hidden    :\t%x, %x\n", r->disc, r->hidden);
160         printf("\tannounce, delta :\t%d, %d\n", r->announce,
161                r->anndelta);
162         printf("\tlicenses        :\t%d\n", r->licenses);
163         printf("\tuser path       :\t%s\n", r->userpath);
164 }
165
166 /* Server query info */
167 static WERROR cmd_srvsvc_srv_query_info(struct rpc_pipe_client *cli, 
168                                           TALLOC_CTX *mem_ctx,
169                                           int argc, const char **argv)
170 {
171         uint32 info_level = 101;
172         union srvsvc_NetSrvInfo info;
173         WERROR result;
174         NTSTATUS status;
175         const char *server_name;
176
177         if (argc > 2) {
178                 printf("Usage: %s [infolevel]\n", argv[0]);
179                 return WERR_OK;
180         }
181
182         if (argc == 2)
183                 info_level = atoi(argv[1]);
184
185         server_name = talloc_asprintf_strupper_m(mem_ctx, "\\\\%s",
186                                                  cli->cli->desthost);
187         W_ERROR_HAVE_NO_MEMORY(server_name);
188
189         status = rpccli_srvsvc_NetSrvGetInfo(cli, mem_ctx,
190                                              server_name,
191                                              info_level,
192                                              &info,
193                                              &result);
194         if (!NT_STATUS_IS_OK(status)) {
195                 return ntstatus_to_werror(status);
196         }
197
198         if (!W_ERROR_IS_OK(result)) {
199                 goto done;
200         }
201
202         /* Display results */
203
204         switch (info_level) {
205         case 101:
206                 display_srv_info_101(info.info101);
207                 break;
208         case 102:
209                 display_srv_info_102(info.info102);
210                 break;
211         default:
212                 printf("unsupported info level %d\n", info_level);
213                 break;
214         }
215
216  done:
217         return result;
218 }
219
220 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
221 {
222         fstring netname = "", remark = "";
223
224         rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
225         rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
226
227         printf("netname: %s\n", netname);
228         printf("\tremark:\t%s\n", remark);
229 }
230
231 static void display_share_info_2(SRV_SHARE_INFO_2 *info2)
232 {
233         fstring netname = "", remark = "", path = "", passwd = "";
234
235         rpcstr_pull_unistr2_fstring(netname, &info2->info_2_str.uni_netname);
236         rpcstr_pull_unistr2_fstring(remark, &info2->info_2_str.uni_remark);
237         rpcstr_pull_unistr2_fstring(path, &info2->info_2_str.uni_path);
238         rpcstr_pull_unistr2_fstring(passwd, &info2->info_2_str.uni_passwd);
239
240         printf("netname: %s\n", netname);
241         printf("\tremark:\t%s\n", remark);
242         printf("\tpath:\t%s\n", path);
243         printf("\tpassword:\t%s\n", passwd);
244 }
245
246 static void display_share_info_502(SRV_SHARE_INFO_502 *info502)
247 {
248         fstring netname = "", remark = "", path = "", passwd = "";
249
250         rpcstr_pull_unistr2_fstring(netname, &info502->info_502_str.uni_netname);
251         rpcstr_pull_unistr2_fstring(remark, &info502->info_502_str.uni_remark);
252         rpcstr_pull_unistr2_fstring(path, &info502->info_502_str.uni_path);
253         rpcstr_pull_unistr2_fstring(passwd, &info502->info_502_str.uni_passwd);
254
255         printf("netname: %s\n", netname);
256         printf("\tremark:\t%s\n", remark);
257         printf("\tpath:\t%s\n", path);
258         printf("\tpassword:\t%s\n", passwd);
259
260         printf("\ttype:\t0x%x\n", info502->info_502.type);
261         printf("\tperms:\t%d\n", info502->info_502.perms);
262         printf("\tmax_uses:\t%d\n", info502->info_502.max_uses);
263         printf("\tnum_uses:\t%d\n", info502->info_502.num_uses);
264         
265         if (info502->info_502_str.sd)
266                 display_sec_desc(info502->info_502_str.sd);
267
268 }
269
270 static WERROR cmd_srvsvc_net_share_enum(struct rpc_pipe_client *cli, 
271                                           TALLOC_CTX *mem_ctx,
272                                           int argc, const char **argv)
273 {
274         uint32 info_level = 2;
275         SRV_SHARE_INFO_CTR ctr;
276         WERROR result;
277         ENUM_HND hnd;
278         uint32 preferred_len = 0xffffffff, i;
279
280         if (argc > 2) {
281                 printf("Usage: %s [infolevel]\n", argv[0]);
282                 return WERR_OK;
283         }
284
285         if (argc == 2)
286                 info_level = atoi(argv[1]);
287
288         init_enum_hnd(&hnd, 0);
289
290         result = rpccli_srvsvc_net_share_enum(
291                 cli, mem_ctx, info_level, &ctr, preferred_len, &hnd);
292
293         if (!W_ERROR_IS_OK(result) || !ctr.num_entries)
294                 goto done;
295
296         /* Display results */
297
298         switch (info_level) {
299         case 1:
300                 for (i = 0; i < ctr.num_entries; i++)
301                         display_share_info_1(&ctr.share.info1[i]);
302                 break;
303         case 2:
304                 for (i = 0; i < ctr.num_entries; i++)
305                         display_share_info_2(&ctr.share.info2[i]);
306                 break;
307         case 502:
308                 for (i = 0; i < ctr.num_entries; i++)
309                         display_share_info_502(&ctr.share.info502[i]);
310                 break;
311         default:
312                 printf("unsupported info level %d\n", info_level);
313                 break;
314         }
315
316  done:
317         return result;
318 }
319
320 static WERROR cmd_srvsvc_net_share_get_info(struct rpc_pipe_client *cli, 
321                                             TALLOC_CTX *mem_ctx,
322                                             int argc, const char **argv)
323 {
324         uint32 info_level = 502;
325         SRV_SHARE_INFO info;
326         WERROR result;
327
328         if (argc > 3) {
329                 printf("Usage: %s [sharename] [infolevel]\n", argv[0]);
330                 return WERR_OK;
331         }
332
333         if (argc == 3)
334                 info_level = atoi(argv[2]);
335
336         result = rpccli_srvsvc_net_share_get_info(cli, mem_ctx, argv[1], info_level, &info);
337
338         if (!W_ERROR_IS_OK(result))
339                 goto done;
340
341         /* Display results */
342
343         switch (info_level) {
344         case 1:
345                 display_share_info_1(&info.share.info1);
346                 break;
347         case 2:
348                 display_share_info_2(&info.share.info2);
349                 break;
350         case 502:
351                 display_share_info_502(&info.share.info502);
352                 break;
353         default:
354                 printf("unsupported info level %d\n", info_level);
355                 break;
356         }
357
358  done:
359         return result;
360 }
361
362 static WERROR cmd_srvsvc_net_share_set_info(struct rpc_pipe_client *cli, 
363                                             TALLOC_CTX *mem_ctx,
364                                             int argc, const char **argv)
365 {
366         uint32 info_level = 502;
367         SRV_SHARE_INFO info_get;
368         WERROR result;
369
370         if (argc > 3) {
371                 printf("Usage: %s [sharename] [comment]\n", argv[0]);
372                 return WERR_OK;
373         }
374
375         /* retrieve share info */
376         result = rpccli_srvsvc_net_share_get_info(cli, mem_ctx, argv[1], info_level, &info_get);
377         if (!W_ERROR_IS_OK(result))
378                 goto done;
379
380         info_get.switch_value = info_level;
381         info_get.ptr_share_ctr = 1;
382         init_unistr2(&(info_get.share.info502.info_502_str.uni_remark), argv[2], UNI_STR_TERMINATE);
383         
384         /* set share info */
385         result = rpccli_srvsvc_net_share_set_info(cli, mem_ctx, argv[1], info_level, &info_get);
386
387         if (!W_ERROR_IS_OK(result))
388                 goto done;
389
390         /* re-retrieve share info and display */
391         result = rpccli_srvsvc_net_share_get_info(cli, mem_ctx, argv[1], info_level, &info_get);
392         if (!W_ERROR_IS_OK(result))
393                 goto done;
394
395         display_share_info_502(&info_get.share.info502);
396         
397  done:
398         return result;
399 }
400
401 static WERROR cmd_srvsvc_net_remote_tod(struct rpc_pipe_client *cli, 
402                                           TALLOC_CTX *mem_ctx,
403                                           int argc, const char **argv)
404 {
405         struct srvsvc_NetRemoteTODInfo *tod = NULL;
406         fstring srv_name_slash;
407         WERROR result;
408         NTSTATUS status;
409
410         if (argc > 1) {
411                 printf("Usage: %s\n", argv[0]);
412                 return WERR_OK;
413         }
414
415         fstr_sprintf(srv_name_slash, "\\\\%s", cli->cli->desthost);
416         status = rpccli_srvsvc_NetRemoteTOD(cli, mem_ctx,
417                                             srv_name_slash,
418                                             &tod,
419                                             &result);
420         if (!NT_STATUS_IS_OK(status)) {
421                 result = ntstatus_to_werror(status);
422                 goto done;
423         }
424
425         if (!W_ERROR_IS_OK(result))
426                 goto done;
427
428  done:
429         return result;
430 }
431
432 static WERROR cmd_srvsvc_net_file_enum(struct rpc_pipe_client *cli, 
433                                          TALLOC_CTX *mem_ctx,
434                                          int argc, const char **argv)
435 {
436         uint32 info_level = 3;
437         SRV_FILE_INFO_CTR ctr;
438         WERROR result;
439         ENUM_HND hnd;
440         uint32 preferred_len = 0xffff;
441
442         if (argc > 2) {
443                 printf("Usage: %s [infolevel]\n", argv[0]);
444                 return WERR_OK;
445         }
446
447         if (argc == 2)
448                 info_level = atoi(argv[1]);
449
450         init_enum_hnd(&hnd, 0);
451
452         ZERO_STRUCT(ctr);
453
454         result = rpccli_srvsvc_net_file_enum(
455                 cli, mem_ctx, info_level, NULL, &ctr, preferred_len, &hnd);
456
457         if (!W_ERROR_IS_OK(result))
458                 goto done;
459
460  done:
461         return result;
462 }
463
464 /* List of commands exported by this module */
465
466 struct cmd_set srvsvc_commands[] = {
467
468         { "SRVSVC" },
469
470         { "srvinfo",     RPC_RTYPE_WERROR, NULL, cmd_srvsvc_srv_query_info, PI_SRVSVC, NULL, "Server query info", "" },
471         { "netshareenum",RPC_RTYPE_WERROR, NULL, cmd_srvsvc_net_share_enum, PI_SRVSVC, NULL, "Enumerate shares", "" },
472         { "netsharegetinfo",RPC_RTYPE_WERROR, NULL, cmd_srvsvc_net_share_get_info, PI_SRVSVC, NULL, "Get Share Info", "" },
473         { "netsharesetinfo",RPC_RTYPE_WERROR, NULL, cmd_srvsvc_net_share_set_info, PI_SRVSVC, NULL, "Set Share Info", "" },
474         { "netfileenum", RPC_RTYPE_WERROR, NULL, cmd_srvsvc_net_file_enum,  PI_SRVSVC, NULL, "Enumerate open files", "" },
475         { "netremotetod",RPC_RTYPE_WERROR, NULL, cmd_srvsvc_net_remote_tod, PI_SRVSVC, NULL, "Fetch remote time of day", "" },
476
477         { NULL }
478 };