rpcclient: Fix ncacn_ip_tcp:<ip-address>
[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 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 #include "../librpc/gen_ndr/ndr_srvsvc.h"
26 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
27 #include "../libcli/security/display_sec.h"
28 #include "lib/util/string_wrappers.h"
29
30 /* Display server query info */
31
32 static char *get_server_type_str(uint32_t type)
33 {
34         static fstring typestr;
35         int i;
36
37         if (type == SV_TYPE_ALL) {
38                 fstrcpy(typestr, "All");
39                 return typestr;
40         }
41                 
42         typestr[0] = 0;
43
44         for (i = 0; i < 32; i++) {
45                 if (type & ((uint32_t)1 << i)) {
46                         switch (1 << i) {
47                         case SV_TYPE_WORKSTATION:
48                                 fstrcat(typestr, "Wk ");
49                                 break;
50                         case SV_TYPE_SERVER:
51                                 fstrcat(typestr, "Sv ");
52                                 break;
53                         case SV_TYPE_SQLSERVER:
54                                 fstrcat(typestr, "Sql ");
55                                 break;
56                         case SV_TYPE_DOMAIN_CTRL:
57                                 fstrcat(typestr, "PDC ");
58                                 break;
59                         case SV_TYPE_DOMAIN_BAKCTRL:
60                                 fstrcat(typestr, "BDC ");
61                                 break;
62                         case SV_TYPE_TIME_SOURCE:
63                                 fstrcat(typestr, "Tim ");
64                                 break;
65                         case SV_TYPE_AFP:
66                                 fstrcat(typestr, "AFP ");
67                                 break;
68                         case SV_TYPE_NOVELL:
69                                 fstrcat(typestr, "Nov ");
70                                 break;
71                         case SV_TYPE_DOMAIN_MEMBER:
72                                 fstrcat(typestr, "Dom ");
73                                 break;
74                         case SV_TYPE_PRINTQ_SERVER:
75                                 fstrcat(typestr, "PrQ ");
76                                 break;
77                         case SV_TYPE_DIALIN_SERVER:
78                                 fstrcat(typestr, "Din ");
79                                 break;
80                         case SV_TYPE_SERVER_UNIX:
81                                 fstrcat(typestr, "Unx ");
82                                 break;
83                         case SV_TYPE_NT:
84                                 fstrcat(typestr, "NT ");
85                                 break;
86                         case SV_TYPE_WFW:
87                                 fstrcat(typestr, "Wfw ");
88                                 break;
89                         case SV_TYPE_SERVER_MFPN:
90                                 fstrcat(typestr, "Mfp ");
91                                 break;
92                         case SV_TYPE_SERVER_NT:
93                                 fstrcat(typestr, "SNT ");
94                                 break;
95                         case SV_TYPE_POTENTIAL_BROWSER:
96                                 fstrcat(typestr, "PtB ");
97                                 break;
98                         case SV_TYPE_BACKUP_BROWSER:
99                                 fstrcat(typestr, "BMB ");
100                                 break;
101                         case SV_TYPE_MASTER_BROWSER:
102                                 fstrcat(typestr, "LMB ");
103                                 break;
104                         case SV_TYPE_DOMAIN_MASTER:
105                                 fstrcat(typestr, "DMB ");
106                                 break;
107                         case SV_TYPE_SERVER_OSF:
108                                 fstrcat(typestr, "OSF ");
109                                 break;
110                         case SV_TYPE_SERVER_VMS:
111                                 fstrcat(typestr, "VMS ");
112                                 break;
113                         case SV_TYPE_WIN95_PLUS:
114                                 fstrcat(typestr, "W95 ");
115                                 break;
116                         case SV_TYPE_ALTERNATE_XPORT:
117                                 fstrcat(typestr, "Xpt ");
118                                 break;
119                         case SV_TYPE_LOCAL_LIST_ONLY:
120                                 fstrcat(typestr, "Dom ");
121                                 break;
122                         case SV_TYPE_DOMAIN_ENUM:
123                                 fstrcat(typestr, "Loc ");
124                                 break;
125                         }
126                 }
127         }
128
129         i = strlen(typestr) - 1;
130
131         if (typestr[i] == ' ')
132                 typestr[i] = 0;
133         
134         return typestr;
135 }
136
137 static void display_server(const char *sname, uint32_t type, const char *comment)
138 {
139         printf("\t%-15.15s%-20s %s\n", sname, get_server_type_str(type), 
140                comment);
141 }
142
143 static void display_srv_info_101(struct srvsvc_NetSrvInfo101 *r)
144 {
145         display_server(r->server_name, r->server_type, r->comment);
146
147         printf("\tplatform_id     :\t%d\n", r->platform_id);
148         printf("\tos version      :\t%d.%d\n",
149                 r->version_major, r->version_minor);
150         printf("\tserver type     :\t0x%x\n", r->server_type);
151 }
152
153 static void display_srv_info_102(struct srvsvc_NetSrvInfo102 *r)
154 {
155         display_server(r->server_name, r->server_type, r->comment);
156
157         printf("\tplatform_id     :\t%d\n", r->platform_id);
158         printf("\tos version      :\t%d.%d\n",
159                 r->version_major, r->version_minor);
160         printf("\tserver type     :\t0x%x\n", r->server_type);
161
162         printf("\tusers           :\t%x\n", r->users);
163         printf("\tdisc, hidden    :\t%x, %x\n", r->disc, r->hidden);
164         printf("\tannounce, delta :\t%d, %d\n", r->announce,
165                r->anndelta);
166         printf("\tlicenses        :\t%d\n", r->licenses);
167         printf("\tuser path       :\t%s\n", r->userpath);
168 }
169
170 /* Server query info */
171 static WERROR cmd_srvsvc_srv_query_info(struct rpc_pipe_client *cli, 
172                                           TALLOC_CTX *mem_ctx,
173                                           int argc, const char **argv)
174 {
175         uint32_t info_level = 101;
176         union srvsvc_NetSrvInfo info;
177         WERROR result;
178         NTSTATUS status;
179         const char *server_unc = cli->srv_name_slash;
180         struct dcerpc_binding_handle *b = cli->binding_handle;
181
182         if (argc > 3) {
183                 printf("Usage: %s [infolevel] [server_unc]\n", argv[0]);
184                 return WERR_OK;
185         }
186
187         if (argc >= 2) {
188                 info_level = atoi(argv[1]);
189         }
190
191         if (argc >= 3) {
192                 server_unc = argv[2];
193         }
194
195         status = dcerpc_srvsvc_NetSrvGetInfo(b, mem_ctx,
196                                              server_unc,
197                                              info_level,
198                                              &info,
199                                              &result);
200         if (!NT_STATUS_IS_OK(status)) {
201                 return ntstatus_to_werror(status);
202         }
203
204         if (!W_ERROR_IS_OK(result)) {
205                 goto done;
206         }
207
208         /* Display results */
209
210         switch (info_level) {
211         case 101:
212                 display_srv_info_101(info.info101);
213                 break;
214         case 102:
215                 display_srv_info_102(info.info102);
216                 break;
217         default:
218                 printf("unsupported info level %d\n", info_level);
219                 break;
220         }
221
222  done:
223         return result;
224 }
225
226 static void display_share_info_1(struct srvsvc_NetShareInfo1 *r)
227 {
228         printf("netname: %s\n", r->name);
229         printf("\tremark:\t%s\n", r->comment);
230 }
231
232 static void display_share_info_2(struct srvsvc_NetShareInfo2 *r)
233 {
234         printf("netname: %s\n", r->name);
235         printf("\tremark:\t%s\n", r->comment);
236         printf("\tpath:\t%s\n", r->path);
237         printf("\tpassword:\t%s\n", r->password);
238 }
239
240 static void display_share_info_502(struct srvsvc_NetShareInfo502 *r)
241 {
242         printf("netname: %s\n", r->name);
243         printf("\tremark:\t%s\n", r->comment);
244         printf("\tpath:\t%s\n", r->path);
245         printf("\tpassword:\t%s\n", r->password);
246
247         printf("\ttype:\t0x%x\n", r->type);
248         printf("\tperms:\t%d\n", r->permissions);
249         printf("\tmax_uses:\t%d\n", r->max_users);
250         printf("\tnum_uses:\t%d\n", r->current_users);
251
252         if (r->sd_buf.sd)
253                 display_sec_desc(r->sd_buf.sd);
254
255 }
256
257 static void display_share_info_1005(struct srvsvc_NetShareInfo1005 *r)
258 {
259         printf("flags: 0x%x\n", r->dfs_flags);
260         printf("csc caching: %u\n",
261                (r->dfs_flags & SHARE_1005_CSC_POLICY_MASK) >>
262                SHARE_1005_CSC_POLICY_SHIFT);
263 }
264
265 static WERROR cmd_srvsvc_net_share_enum_int(struct rpc_pipe_client *cli,
266                                             TALLOC_CTX *mem_ctx,
267                                             int argc, const char **argv,
268                                             uint32_t opcode)
269 {
270         uint32_t info_level = 2;
271         struct srvsvc_NetShareInfoCtr info_ctr;
272         struct srvsvc_NetShareCtr0 ctr0;
273         struct srvsvc_NetShareCtr1 ctr1;
274         struct srvsvc_NetShareCtr2 ctr2;
275         struct srvsvc_NetShareCtr501 ctr501;
276         struct srvsvc_NetShareCtr502 ctr502;
277         struct srvsvc_NetShareCtr1004 ctr1004;
278         struct srvsvc_NetShareCtr1005 ctr1005;
279         struct srvsvc_NetShareCtr1006 ctr1006;
280         struct srvsvc_NetShareCtr1007 ctr1007;
281         struct srvsvc_NetShareCtr1501 ctr1501;
282         WERROR result;
283         NTSTATUS status;
284         uint32_t totalentries = 0;
285         uint32_t count = 0;
286         uint32_t resume_handle = 0;
287         uint32_t *resume_handle_p = NULL;
288         uint32_t preferred_len = 0xffffffff, i;
289         struct dcerpc_binding_handle *b = cli->binding_handle;
290
291         if (argc > 3) {
292                 printf("Usage: %s [infolevel] [resume_handle]\n", argv[0]);
293                 return WERR_OK;
294         }
295
296         if (argc >= 2) {
297                 info_level = atoi(argv[1]);
298         }
299
300         if (argc == 3) {
301                 resume_handle = atoi(argv[2]);
302                 resume_handle_p = &resume_handle;
303         }
304
305         ZERO_STRUCT(info_ctr);
306
307         info_ctr.level = info_level;
308
309         switch (info_level) {
310         case 0:
311                 ZERO_STRUCT(ctr0);
312                 info_ctr.ctr.ctr0 = &ctr0;
313                 break;
314         case 1:
315                 ZERO_STRUCT(ctr1);
316                 info_ctr.ctr.ctr1 = &ctr1;
317                 break;
318         case 2:
319                 ZERO_STRUCT(ctr2);
320                 info_ctr.ctr.ctr2 = &ctr2;
321                 break;
322         case 501:
323                 ZERO_STRUCT(ctr501);
324                 info_ctr.ctr.ctr501 = &ctr501;
325                 break;
326         case 502:
327                 ZERO_STRUCT(ctr502);
328                 info_ctr.ctr.ctr502 = &ctr502;
329                 break;
330         case 1004:
331                 ZERO_STRUCT(ctr1004);
332                 info_ctr.ctr.ctr1004 = &ctr1004;
333                 break;
334         case 1005:
335                 ZERO_STRUCT(ctr1005);
336                 info_ctr.ctr.ctr1005 = &ctr1005;
337                 break;
338         case 1006:
339                 ZERO_STRUCT(ctr1006);
340                 info_ctr.ctr.ctr1006 = &ctr1006;
341                 break;
342         case 1007:
343                 ZERO_STRUCT(ctr1007);
344                 info_ctr.ctr.ctr1007 = &ctr1007;
345                 break;
346         case 1501:
347                 ZERO_STRUCT(ctr1501);
348                 info_ctr.ctr.ctr1501 = &ctr1501;
349                 break;
350         }
351
352         switch (opcode) {
353                 case NDR_SRVSVC_NETSHAREENUM:
354                         status = dcerpc_srvsvc_NetShareEnum(b, mem_ctx,
355                                                             cli->desthost,
356                                                             &info_ctr,
357                                                             preferred_len,
358                                                             &totalentries,
359                                                             resume_handle_p,
360                                                             &result);
361                         break;
362                 case NDR_SRVSVC_NETSHAREENUMALL:
363                         status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
364                                                                cli->desthost,
365                                                                &info_ctr,
366                                                                preferred_len,
367                                                                &totalentries,
368                                                                resume_handle_p,
369                                                                &result);
370                         break;
371                 default:
372                         return WERR_INVALID_PARAMETER;
373         }
374
375         if (!NT_STATUS_IS_OK(status)) {
376                 result = ntstatus_to_werror(status);
377                 goto done;
378         }
379         if (!W_ERROR_IS_OK(result)) {
380                 goto done;
381         }
382
383         /* Display results */
384
385         switch (info_level) {
386         case 1:
387                 count = info_ctr.ctr.ctr1->count;
388                 for (i = 0; i < count; i++)
389                         display_share_info_1(&info_ctr.ctr.ctr1->array[i]);
390                 break;
391         case 2:
392                 count = info_ctr.ctr.ctr2->count;
393                 for (i = 0; i < count; i++)
394                         display_share_info_2(&info_ctr.ctr.ctr2->array[i]);
395                 break;
396         case 502:
397                 count = info_ctr.ctr.ctr502->count;
398                 for (i = 0; i < count; i++)
399                         display_share_info_502(&info_ctr.ctr.ctr502->array[i]);
400                 break;
401         default:
402                 printf("unsupported info level %d\n", info_level);
403                 break;
404         }
405
406  done:
407         return result;
408 }
409
410 static WERROR cmd_srvsvc_net_share_enum(struct rpc_pipe_client *cli,
411                                         TALLOC_CTX *mem_ctx,
412                                         int argc, const char **argv)
413 {
414         return cmd_srvsvc_net_share_enum_int(cli, mem_ctx,
415                                              argc, argv,
416                                              NDR_SRVSVC_NETSHAREENUM);
417 }
418
419 static WERROR cmd_srvsvc_net_share_enum_all(struct rpc_pipe_client *cli,
420                                             TALLOC_CTX *mem_ctx,
421                                             int argc, const char **argv)
422 {
423         return cmd_srvsvc_net_share_enum_int(cli, mem_ctx,
424                                              argc, argv,
425                                              NDR_SRVSVC_NETSHAREENUMALL);
426 }
427
428 static WERROR cmd_srvsvc_net_share_get_info(struct rpc_pipe_client *cli, 
429                                             TALLOC_CTX *mem_ctx,
430                                             int argc, const char **argv)
431 {
432         uint32_t info_level = 502;
433         union srvsvc_NetShareInfo info;
434         WERROR result;
435         NTSTATUS status;
436         struct dcerpc_binding_handle *b = cli->binding_handle;
437
438         if (argc < 2 || argc > 3) {
439                 printf("Usage: %s sharename [infolevel 1|2|502|1005]\n",
440                        argv[0]);
441                 return WERR_OK;
442         }
443
444         if (argc == 3)
445                 info_level = atoi(argv[2]);
446
447         status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
448                                                cli->desthost,
449                                                argv[1],
450                                                info_level,
451                                                &info,
452                                                &result);
453
454         if (!NT_STATUS_IS_OK(status)) {
455                 result = ntstatus_to_werror(status);
456                 goto done;
457         }
458         if (!W_ERROR_IS_OK(result)) {
459                 goto done;
460         }
461
462         /* Display results */
463
464         switch (info_level) {
465         case 1:
466                 display_share_info_1(info.info1);
467                 break;
468         case 2:
469                 display_share_info_2(info.info2);
470                 break;
471         case 502:
472                 display_share_info_502(info.info502);
473                 break;
474         case 1005:
475                 display_share_info_1005(info.info1005);
476                 break;
477         default:
478                 printf("unsupported info level %d\n", info_level);
479                 break;
480         }
481
482  done:
483         return result;
484 }
485
486 static WERROR cmd_srvsvc_net_share_set_info(struct rpc_pipe_client *cli, 
487                                             TALLOC_CTX *mem_ctx,
488                                             int argc, const char **argv)
489 {
490         uint32_t info_level = 502;
491         union srvsvc_NetShareInfo info_get;
492         WERROR result;
493         NTSTATUS status;
494         uint32_t parm_err = 0;
495         struct dcerpc_binding_handle *b = cli->binding_handle;
496
497         if (argc > 3) {
498                 printf("Usage: %s [sharename] [comment]\n", argv[0]);
499                 return WERR_OK;
500         }
501
502         /* retrieve share info */
503         status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
504                                                cli->desthost,
505                                                argv[1],
506                                                info_level,
507                                                &info_get,
508                                                &result);
509
510         if (!NT_STATUS_IS_OK(status)) {
511                 result = ntstatus_to_werror(status);
512                 goto done;
513         }
514         if (!W_ERROR_IS_OK(result)) {
515                 goto done;
516         }
517
518         info_get.info502->comment = argv[2];
519
520         /* set share info */
521         status = dcerpc_srvsvc_NetShareSetInfo(b, mem_ctx,
522                                                cli->desthost,
523                                                argv[1],
524                                                info_level,
525                                                &info_get,
526                                                &parm_err,
527                                                &result);
528
529         if (!NT_STATUS_IS_OK(status)) {
530                 result = ntstatus_to_werror(status);
531                 goto done;
532         }
533         if (!W_ERROR_IS_OK(result)) {
534                 goto done;
535         }
536
537         /* re-retrieve share info and display */
538         status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
539                                                cli->desthost,
540                                                argv[1],
541                                                info_level,
542                                                &info_get,
543                                                &result);
544         if (!NT_STATUS_IS_OK(status)) {
545                 result = ntstatus_to_werror(status);
546                 goto done;
547         }
548         if (!W_ERROR_IS_OK(result)) {
549                 goto done;
550         }
551
552         display_share_info_502(info_get.info502);
553
554  done:
555         return result;
556 }
557
558 static WERROR cmd_srvsvc_net_share_set_dfs_flags(struct rpc_pipe_client *cli,
559                                             TALLOC_CTX *mem_ctx,
560                                             int argc, const char **argv)
561 {
562         struct srvsvc_NetShareInfo1005 info1005 = { 0 };
563         union srvsvc_NetShareInfo info = { .info1005 = &info1005 };
564         WERROR result;
565         NTSTATUS status;
566         uint32_t parm_err = 0;
567         struct dcerpc_binding_handle *b = cli->binding_handle;
568
569         if (argc > 3) {
570                 printf("Usage: %s [sharename] [dfsflags]\n", argv[0]);
571                 return WERR_OK;
572         }
573
574         if (argc > 2) {
575                 info.info1005->dfs_flags = strtol(argv[2], NULL, 0);
576         }
577
578         /* set share info */
579         status = dcerpc_srvsvc_NetShareSetInfo(b, mem_ctx,
580                                                cli->desthost,
581                                                argv[1],
582                                                1005,
583                                                &info,
584                                                &parm_err,
585                                                &result);
586
587         if (!NT_STATUS_IS_OK(status)) {
588                 return ntstatus_to_werror(status);
589         }
590         if (!W_ERROR_IS_OK(result)) {
591                 return result;
592         }
593
594         /* re-retrieve share info and display */
595         status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
596                                                cli->desthost,
597                                                argv[1],
598                                                1005,
599                                                &info,
600                                                &result);
601         if (!NT_STATUS_IS_OK(status)) {
602                 return ntstatus_to_werror(status);
603         }
604         if (!W_ERROR_IS_OK(result)) {
605                 return result;
606         }
607
608         display_share_info_1005(info.info1005);
609
610         return result;
611 }
612
613 static WERROR cmd_srvsvc_net_remote_tod(struct rpc_pipe_client *cli, 
614                                           TALLOC_CTX *mem_ctx,
615                                           int argc, const char **argv)
616 {
617         struct srvsvc_NetRemoteTODInfo *tod = NULL;
618         WERROR result;
619         NTSTATUS status;
620         struct dcerpc_binding_handle *b = cli->binding_handle;
621
622         if (argc > 1) {
623                 printf("Usage: %s\n", argv[0]);
624                 return WERR_OK;
625         }
626
627         status = dcerpc_srvsvc_NetRemoteTOD(b, mem_ctx,
628                                             cli->srv_name_slash,
629                                             &tod,
630                                             &result);
631         if (!NT_STATUS_IS_OK(status)) {
632                 result = ntstatus_to_werror(status);
633                 goto done;
634         }
635
636         if (!W_ERROR_IS_OK(result))
637                 goto done;
638
639  done:
640         return result;
641 }
642
643 static WERROR cmd_srvsvc_net_file_enum(struct rpc_pipe_client *cli, 
644                                          TALLOC_CTX *mem_ctx,
645                                          int argc, const char **argv)
646 {
647         struct srvsvc_NetFileCtr3 ctr3 = { 0 };
648         struct srvsvc_NetFileInfoCtr info_ctr = {
649                 .level = 3,
650                 .ctr = {
651                         .ctr3 = &ctr3,
652                 },
653         };
654         WERROR result;
655         NTSTATUS status;
656         uint32_t preferred_len = 0xffff;
657         uint32_t total_entries = 0;
658         uint32_t resume_handle = 0;
659         struct dcerpc_binding_handle *b = cli->binding_handle;
660
661         if (argc > 2) {
662                 printf("Usage: %s [infolevel]\n", argv[0]);
663                 return WERR_OK;
664         }
665
666         if (argc == 2) {
667                 info_ctr.level = atoi(argv[1]);
668         }
669
670         status = dcerpc_srvsvc_NetFileEnum(b, mem_ctx,
671                                            cli->desthost,
672                                            NULL,
673                                            NULL,
674                                            &info_ctr,
675                                            preferred_len,
676                                            &total_entries,
677                                            &resume_handle,
678                                            &result);
679         if (!NT_STATUS_IS_OK(status)) {
680                 result = ntstatus_to_werror(status);
681                 goto done;
682         }
683
684         if (!W_ERROR_IS_OK(result)) {
685                 goto done;
686         }
687
688         if (info_ctr.level == 3) {
689                 struct srvsvc_NetFileCtr3 *ret = info_ctr.ctr.ctr3;
690                 uint32_t i;
691
692                 for (i=0; i<ret->count; i++) {
693                         printf("%s\n", ret->array[i].path);
694                 }
695         }
696  done:
697         return result;
698 }
699
700 static WERROR cmd_srvsvc_net_name_validate(struct rpc_pipe_client *cli,
701                                            TALLOC_CTX *mem_ctx,
702                                            int argc, const char **argv)
703 {
704         WERROR result;
705         NTSTATUS status;
706         uint32_t name_type = 9;
707         uint32_t flags = 0;
708         struct dcerpc_binding_handle *b = cli->binding_handle;
709
710         if (argc < 2 || argc > 3) {
711                 printf("Usage: %s [sharename] [type]\n", argv[0]);
712                 return WERR_OK;
713         }
714
715         if (argc == 3) {
716                 name_type = atoi(argv[2]);
717         }
718
719         status = dcerpc_srvsvc_NetNameValidate(b, mem_ctx,
720                                                cli->desthost,
721                                                argv[1],
722                                                name_type,
723                                                flags,
724                                                &result);
725         if (!NT_STATUS_IS_OK(status)) {
726                 result = ntstatus_to_werror(status);
727                 goto done;
728         }
729
730         if (!W_ERROR_IS_OK(result))
731                 goto done;
732
733  done:
734         return result;
735 }
736
737 static WERROR cmd_srvsvc_net_file_get_sec(struct rpc_pipe_client *cli,
738                                           TALLOC_CTX *mem_ctx,
739                                           int argc, const char **argv)
740 {
741         WERROR result;
742         NTSTATUS status;
743         struct sec_desc_buf *sd_buf = NULL;
744         struct dcerpc_binding_handle *b = cli->binding_handle;
745
746         if (argc < 2 || argc > 4) {
747                 printf("Usage: %s [sharename] [file]\n", argv[0]);
748                 return WERR_OK;
749         }
750
751         status = dcerpc_srvsvc_NetGetFileSecurity(b, mem_ctx,
752                                                   cli->desthost,
753                                                   argv[1],
754                                                   argv[2],
755                                                   SECINFO_DACL,
756                                                   &sd_buf,
757                                                   &result);
758         if (!NT_STATUS_IS_OK(status)) {
759                 result = ntstatus_to_werror(status);
760                 goto done;
761         }
762
763         if (!W_ERROR_IS_OK(result)) {
764                 goto done;
765         }
766
767         display_sec_desc(sd_buf->sd);
768
769  done:
770         return result;
771 }
772
773 static WERROR cmd_srvsvc_net_sess_del(struct rpc_pipe_client *cli,
774                                       TALLOC_CTX *mem_ctx,
775                                       int argc, const char **argv)
776 {
777         WERROR result;
778         NTSTATUS status;
779         struct dcerpc_binding_handle *b = cli->binding_handle;
780
781         if (argc < 2 || argc > 4) {
782                 printf("Usage: %s [client] [user]\n", argv[0]);
783                 return WERR_OK;
784         }
785
786         status = dcerpc_srvsvc_NetSessDel(b, mem_ctx,
787                                           cli->desthost,
788                                           argv[1],
789                                           argv[2],
790                                           &result);
791         if (!NT_STATUS_IS_OK(status)) {
792                 result = ntstatus_to_werror(status);
793                 goto done;
794         }
795
796         if (!W_ERROR_IS_OK(result)) {
797                 goto done;
798         }
799
800  done:
801         return result;
802 }
803
804 static WERROR cmd_srvsvc_net_sess_enum(struct rpc_pipe_client *cli,
805                                        TALLOC_CTX *mem_ctx,
806                                        int argc, const char **argv)
807 {
808         WERROR result;
809         NTSTATUS status;
810         struct srvsvc_NetSessInfoCtr info_ctr;
811         struct srvsvc_NetSessCtr0 ctr0;
812         struct srvsvc_NetSessCtr1 ctr1;
813         struct srvsvc_NetSessCtr2 ctr2;
814         struct srvsvc_NetSessCtr10 ctr10;
815         struct srvsvc_NetSessCtr502 ctr502;
816         uint32_t total_entries = 0;
817         uint32_t resume_handle = 0;
818         uint32_t *resume_handle_p = NULL;
819         uint32_t level = 1;
820         const char *client = NULL;
821         const char *user = NULL;
822         struct dcerpc_binding_handle *b = cli->binding_handle;
823
824         if (argc > 6) {
825                 printf("Usage: %s [client] [user] [level] [resume_handle]\n", argv[0]);
826                 return WERR_OK;
827         }
828
829         if (argc >= 2) {
830                 client = argv[1];
831         }
832
833         if (argc >= 3) {
834                 user = argv[2];
835         }
836
837         if (argc >= 4) {
838                 level = atoi(argv[3]);
839         }
840
841         if (argc >= 5) {
842                 resume_handle = atoi(argv[4]);
843                 resume_handle_p = &resume_handle;
844         }
845
846         ZERO_STRUCT(info_ctr);
847
848         info_ctr.level = level;
849
850         d_printf("trying level: %d\n", level);
851
852         switch (level) {
853         case 0:
854                 ZERO_STRUCT(ctr0);
855                 info_ctr.ctr.ctr0 = &ctr0;
856                 break;
857         case 1:
858                 ZERO_STRUCT(ctr1);
859                 info_ctr.ctr.ctr1 = &ctr1;
860                 break;
861         case 2:
862                 ZERO_STRUCT(ctr2);
863                 info_ctr.ctr.ctr2 = &ctr2;
864                 break;
865         case 10:
866                 ZERO_STRUCT(ctr10);
867                 info_ctr.ctr.ctr10 = &ctr10;
868                 break;
869         case 502:
870                 ZERO_STRUCT(ctr502);
871                 info_ctr.ctr.ctr502 = &ctr502;
872                 break;
873         }
874
875         status = dcerpc_srvsvc_NetSessEnum(b, mem_ctx,
876                                           cli->desthost,
877                                           client,
878                                           user,
879                                           &info_ctr,
880                                           0xffffffff,
881                                           &total_entries,
882                                           resume_handle_p,
883                                           &result);
884
885         if (!NT_STATUS_IS_OK(status)) {
886                 result = ntstatus_to_werror(status);
887                 goto done;
888         }
889
890         if (!W_ERROR_IS_OK(result)) {
891                 goto done;
892         }
893
894         d_printf("Received %d entries.\n", total_entries);
895
896  done:
897         return result;
898 }
899
900 static WERROR cmd_srvsvc_net_disk_enum(struct rpc_pipe_client *cli,
901                                        TALLOC_CTX *mem_ctx,
902                                        int argc, const char **argv)
903 {
904         struct srvsvc_NetDiskInfo info;
905         WERROR result;
906         NTSTATUS status;
907         uint32_t total_entries = 0;
908         uint32_t resume_handle = 0;
909         uint32_t level = 0;
910         struct dcerpc_binding_handle *b = cli->binding_handle;
911
912         if (argc > 4) {
913                 printf("Usage: %s [level] [resume_handle]\n", argv[0]);
914                 return WERR_OK;
915         }
916
917         if (argc >= 2) {
918                 level = atoi(argv[1]);
919         }
920
921         if (argc >= 3) {
922                 resume_handle = atoi(argv[2]);
923         }
924
925         ZERO_STRUCT(info);
926
927         status = dcerpc_srvsvc_NetDiskEnum(b, mem_ctx,
928                                            cli->desthost,
929                                            level,
930                                            &info,
931                                            0xffffffff,
932                                            &total_entries,
933                                            &resume_handle,
934                                            &result);
935         if (!NT_STATUS_IS_OK(status)) {
936                 result = ntstatus_to_werror(status);
937                 goto done;
938         }
939
940         if (!W_ERROR_IS_OK(result)) {
941                 goto done;
942         }
943
944  done:
945         return result;
946 }
947
948 static WERROR cmd_srvsvc_net_conn_enum(struct rpc_pipe_client *cli,
949                                        TALLOC_CTX *mem_ctx,
950                                        int argc, const char **argv)
951 {
952         struct srvsvc_NetConnInfoCtr info_ctr;
953         struct srvsvc_NetConnCtr0 ctr0;
954         struct srvsvc_NetConnCtr1 ctr1;
955         WERROR result;
956         NTSTATUS status;
957         uint32_t total_entries = 0;
958         uint32_t resume_handle = 0;
959         uint32_t *resume_handle_p = NULL;
960         uint32_t level = 1;
961         const char *path = "IPC$";
962         struct dcerpc_binding_handle *b = cli->binding_handle;
963
964         if (argc > 4) {
965                 printf("Usage: %s [level] [path] [resume_handle]\n", argv[0]);
966                 return WERR_OK;
967         }
968
969         if (argc >= 2) {
970                 level = atoi(argv[1]);
971         }
972
973         if (argc >= 3) {
974                 path = argv[2];
975         }
976
977         if (argc >= 4) {
978                 resume_handle = atoi(argv[3]);
979                 resume_handle_p = &resume_handle;
980         }
981
982         ZERO_STRUCT(info_ctr);
983
984         info_ctr.level = level;
985
986         switch (level) {
987                 case 0:
988                         ZERO_STRUCT(ctr0);
989                         info_ctr.ctr.ctr0 = &ctr0;
990                         break;
991                 case 1:
992                         ZERO_STRUCT(ctr1);
993                         info_ctr.ctr.ctr1 = &ctr1;
994                         break;
995                 default:
996                         return WERR_INVALID_PARAMETER;
997         }
998
999         status = dcerpc_srvsvc_NetConnEnum(b, mem_ctx,
1000                                            cli->desthost,
1001                                            path,
1002                                            &info_ctr,
1003                                            0xffffffff,
1004                                            &total_entries,
1005                                            resume_handle_p,
1006                                            &result);
1007
1008         if (!NT_STATUS_IS_OK(status)) {
1009                 result = ntstatus_to_werror(status);
1010                 goto done;
1011         }
1012
1013         if (!W_ERROR_IS_OK(result)) {
1014                 goto done;
1015         }
1016
1017  done:
1018         return result;
1019 }
1020
1021 static WERROR cmd_srvsvc_net_share_add(struct rpc_pipe_client *cli,
1022                                        TALLOC_CTX *mem_ctx,
1023                                        int argc, const char **argv)
1024 {
1025         struct srvsvc_NetShareInfo502 info502 = { 0 };
1026         union srvsvc_NetShareInfo info = { .info502 = &info502 };
1027         WERROR result;
1028         NTSTATUS status;
1029         uint32_t max_users = -1, parm_error;
1030         struct sec_desc_buf sd_buf = { 0 };
1031         const char *path, *share_name, *comment = NULL;
1032         struct dcerpc_binding_handle *b = cli->binding_handle;
1033
1034         if (argc < 3 || argc > 5) {
1035                 printf("Usage: %s path share_name [max_users] [comment]\n",
1036                        argv[0]);
1037                 return WERR_OK;
1038         }
1039
1040         path = argv[1];
1041         share_name = argv[2];
1042
1043         if (argc >= 4) {
1044                 max_users = atoi(argv[3]);
1045         }
1046
1047         if (argc >= 5) {
1048                 comment = argv[4];
1049         }
1050
1051         info.info502->name = share_name;
1052         info.info502->type = STYPE_DISKTREE;
1053         info.info502->comment = comment;
1054         info.info502->max_users = max_users;
1055         info.info502->path = path;
1056         info.info502->sd_buf = sd_buf;
1057
1058         status = dcerpc_srvsvc_NetShareAdd(b, mem_ctx, cli->desthost,
1059                                            502, &info, &parm_error, &result);
1060         if (!NT_STATUS_IS_OK(status)) {
1061                 result = ntstatus_to_werror(status);
1062         }
1063
1064         return result;
1065 }
1066
1067 static WERROR cmd_srvsvc_net_share_del(struct rpc_pipe_client *cli,
1068                                        TALLOC_CTX *mem_ctx,
1069                                        int argc, const char **argv)
1070 {
1071         const char *share_name;
1072         WERROR result;
1073         NTSTATUS status;
1074         struct dcerpc_binding_handle *b = cli->binding_handle;
1075
1076         if (argc < 2) {
1077                 printf("Usage: %s share_name\n", argv[0]);
1078                 return WERR_OK;
1079         }
1080
1081         share_name = argv[1];
1082
1083         status = dcerpc_srvsvc_NetShareDel(b, mem_ctx, cli->desthost,
1084                                            share_name, 0, &result);
1085
1086         if (!NT_STATUS_IS_OK(status)) {
1087                 result = ntstatus_to_werror(status);
1088         }
1089
1090         return result;
1091 }
1092
1093
1094 /* List of commands exported by this module */
1095
1096 struct cmd_set srvsvc_commands[] = {
1097
1098         {
1099                 .name = "SRVSVC",
1100         },
1101
1102         {
1103                 .name               = "srvinfo",
1104                 .returntype         = RPC_RTYPE_WERROR,
1105                 .ntfn               = NULL,
1106                 .wfn                = cmd_srvsvc_srv_query_info,
1107                 .table              = &ndr_table_srvsvc,
1108                 .rpc_pipe           = NULL,
1109                 .description        = "Server query info",
1110                 .usage              = "",
1111         },
1112         {
1113                 .name               = "netshareenum",
1114                 .returntype         = RPC_RTYPE_WERROR,
1115                 .ntfn               = NULL,
1116                 .wfn                = cmd_srvsvc_net_share_enum,
1117                 .table              = &ndr_table_srvsvc,
1118                 .rpc_pipe           = NULL,
1119                 .description        = "Enumerate shares",
1120                 .usage              = "",
1121         },
1122         {
1123                 .name               = "netshareenumall",
1124                 .returntype         = RPC_RTYPE_WERROR,
1125                 .ntfn               = NULL,
1126                 .wfn                = cmd_srvsvc_net_share_enum_all,
1127                 .table              = &ndr_table_srvsvc,
1128                 .rpc_pipe           = NULL,
1129                 .description        = "Enumerate all shares",
1130                 .usage              = "",
1131         },
1132         {
1133                 .name               = "netsharegetinfo",
1134                 .returntype         = RPC_RTYPE_WERROR,
1135                 .ntfn               = NULL,
1136                 .wfn                = cmd_srvsvc_net_share_get_info,
1137                 .table              = &ndr_table_srvsvc,
1138                 .rpc_pipe           = NULL,
1139                 .description        = "Get Share Info",
1140                 .usage              = "",
1141         },
1142         {
1143                 .name               = "netsharesetinfo",
1144                 .returntype         = RPC_RTYPE_WERROR,
1145                 .ntfn               = NULL,
1146                 .wfn                = cmd_srvsvc_net_share_set_info,
1147                 .table              = &ndr_table_srvsvc,
1148                 .rpc_pipe           = NULL,
1149                 .description        = "Set Share Info",
1150                 .usage              = "",
1151         },
1152         {
1153                 .name               = "netsharesetdfsflags",
1154                 .returntype         = RPC_RTYPE_WERROR,
1155                 .ntfn               = NULL,
1156                 .wfn                = cmd_srvsvc_net_share_set_dfs_flags,
1157                 .table              = &ndr_table_srvsvc,
1158                 .rpc_pipe           = NULL,
1159                 .description        = "Set DFS flags",
1160                 .usage              = "",
1161         },
1162         {
1163                 .name               = "netfileenum",
1164                 .returntype         = RPC_RTYPE_WERROR,
1165                 .ntfn               = NULL,
1166                 .wfn                = cmd_srvsvc_net_file_enum,
1167                 .table              =  &ndr_table_srvsvc,
1168                 .rpc_pipe           = NULL,
1169                 .description        = "Enumerate open files",
1170                 .usage              = "",
1171         },
1172         {
1173                 .name               = "netremotetod",
1174                 .returntype         = RPC_RTYPE_WERROR,
1175                 .ntfn               = NULL,
1176                 .wfn                = cmd_srvsvc_net_remote_tod,
1177                 .table              = &ndr_table_srvsvc,
1178                 .rpc_pipe           = NULL,
1179                 .description        = "Fetch remote time of day",
1180                 .usage              = "",
1181         },
1182         {
1183                 .name               = "netnamevalidate",
1184                 .returntype         = RPC_RTYPE_WERROR,
1185                 .ntfn               = NULL,
1186                 .wfn                = cmd_srvsvc_net_name_validate,
1187                 .table              = &ndr_table_srvsvc,
1188                 .rpc_pipe           = NULL,
1189                 .description        = "Validate sharename",
1190                 .usage              = "",
1191         },
1192         {
1193                 .name               = "netfilegetsec",
1194                 .returntype         = RPC_RTYPE_WERROR,
1195                 .ntfn               = NULL,
1196                 .wfn                = cmd_srvsvc_net_file_get_sec,
1197                 .table              = &ndr_table_srvsvc,
1198                 .rpc_pipe           = NULL,
1199                 .description        = "Get File security",
1200                 .usage              = "",
1201         },
1202         {
1203                 .name               = "netsessdel",
1204                 .returntype         = RPC_RTYPE_WERROR,
1205                 .ntfn               = NULL,
1206                 .wfn                = cmd_srvsvc_net_sess_del,
1207                 .table              = &ndr_table_srvsvc,
1208                 .rpc_pipe           = NULL,
1209                 .description        = "Delete Session",
1210                 .usage              = "",
1211         },
1212         {
1213                 .name               = "netsessenum",
1214                 .returntype         = RPC_RTYPE_WERROR,
1215                 .ntfn               = NULL,
1216                 .wfn                = cmd_srvsvc_net_sess_enum,
1217                 .table              = &ndr_table_srvsvc,
1218                 .rpc_pipe           = NULL,
1219                 .description        = "Enumerate Sessions",
1220                 .usage              = "",
1221         },
1222         {
1223                 .name               = "netdiskenum",
1224                 .returntype         = RPC_RTYPE_WERROR,
1225                 .ntfn               = NULL,
1226                 .wfn                = cmd_srvsvc_net_disk_enum,
1227                 .table              = &ndr_table_srvsvc,
1228                 .rpc_pipe           = NULL,
1229                 .description        = "Enumerate Disks",
1230                 .usage              = "",
1231         },
1232         {
1233                 .name               = "netconnenum",
1234                 .returntype         = RPC_RTYPE_WERROR,
1235                 .ntfn               = NULL,
1236                 .wfn                = cmd_srvsvc_net_conn_enum,
1237                 .table              = &ndr_table_srvsvc,
1238                 .rpc_pipe           = NULL,
1239                 .description        = "Enumerate Connections",
1240                 .usage              = "",
1241         },
1242         {
1243                 .name               = "netshareadd",
1244                 .returntype         = RPC_RTYPE_WERROR,
1245                 .ntfn               = NULL,
1246                 .wfn                = cmd_srvsvc_net_share_add,
1247                 .table              = &ndr_table_srvsvc,
1248                 .rpc_pipe           = NULL,
1249                 .description        = "Add share",
1250                 .usage              = "",
1251         },
1252         {
1253                 .name               = "netsharedel",
1254                 .returntype         = RPC_RTYPE_WERROR,
1255                 .ntfn               = NULL,
1256                 .wfn                = cmd_srvsvc_net_share_del,
1257                 .table              = &ndr_table_srvsvc,
1258                 .rpc_pipe           = NULL,
1259                 .description        = "Delete share",
1260                 .usage              = "",
1261         },
1262
1263         {
1264                 .name = NULL,
1265         }
1266 };