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