rpcclient: Use struct initializers in cmd_srvsvc_net_file_enum()
[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 & ((uint32_t)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         struct srvsvc_NetFileCtr3 ctr3 = { 0 };
647         struct srvsvc_NetFileInfoCtr info_ctr = {
648                 .level = 3,
649                 .ctr = {
650                         .ctr3 = &ctr3,
651                 },
652         };
653         WERROR result;
654         NTSTATUS status;
655         uint32_t preferred_len = 0xffff;
656         uint32_t total_entries = 0;
657         uint32_t resume_handle = 0;
658         struct dcerpc_binding_handle *b = cli->binding_handle;
659
660         if (argc > 2) {
661                 printf("Usage: %s [infolevel]\n", argv[0]);
662                 return WERR_OK;
663         }
664
665         if (argc == 2) {
666                 info_ctr.level = atoi(argv[1]);
667         }
668
669         status = dcerpc_srvsvc_NetFileEnum(b, mem_ctx,
670                                            cli->desthost,
671                                            NULL,
672                                            NULL,
673                                            &info_ctr,
674                                            preferred_len,
675                                            &total_entries,
676                                            &resume_handle,
677                                            &result);
678         if (!NT_STATUS_IS_OK(status)) {
679                 result = ntstatus_to_werror(status);
680                 goto done;
681         }
682
683         if (!W_ERROR_IS_OK(result)) {
684                 goto done;
685         }
686
687  done:
688         return result;
689 }
690
691 static WERROR cmd_srvsvc_net_name_validate(struct rpc_pipe_client *cli,
692                                            TALLOC_CTX *mem_ctx,
693                                            int argc, const char **argv)
694 {
695         WERROR result;
696         NTSTATUS status;
697         uint32_t name_type = 9;
698         uint32_t flags = 0;
699         struct dcerpc_binding_handle *b = cli->binding_handle;
700
701         if (argc < 2 || argc > 3) {
702                 printf("Usage: %s [sharename] [type]\n", argv[0]);
703                 return WERR_OK;
704         }
705
706         if (argc == 3) {
707                 name_type = atoi(argv[2]);
708         }
709
710         status = dcerpc_srvsvc_NetNameValidate(b, mem_ctx,
711                                                cli->desthost,
712                                                argv[1],
713                                                name_type,
714                                                flags,
715                                                &result);
716         if (!NT_STATUS_IS_OK(status)) {
717                 result = ntstatus_to_werror(status);
718                 goto done;
719         }
720
721         if (!W_ERROR_IS_OK(result))
722                 goto done;
723
724  done:
725         return result;
726 }
727
728 static WERROR cmd_srvsvc_net_file_get_sec(struct rpc_pipe_client *cli,
729                                           TALLOC_CTX *mem_ctx,
730                                           int argc, const char **argv)
731 {
732         WERROR result;
733         NTSTATUS status;
734         struct sec_desc_buf *sd_buf = NULL;
735         struct dcerpc_binding_handle *b = cli->binding_handle;
736
737         if (argc < 2 || argc > 4) {
738                 printf("Usage: %s [sharename] [file]\n", argv[0]);
739                 return WERR_OK;
740         }
741
742         status = dcerpc_srvsvc_NetGetFileSecurity(b, mem_ctx,
743                                                   cli->desthost,
744                                                   argv[1],
745                                                   argv[2],
746                                                   SECINFO_DACL,
747                                                   &sd_buf,
748                                                   &result);
749         if (!NT_STATUS_IS_OK(status)) {
750                 result = ntstatus_to_werror(status);
751                 goto done;
752         }
753
754         if (!W_ERROR_IS_OK(result)) {
755                 goto done;
756         }
757
758         display_sec_desc(sd_buf->sd);
759
760  done:
761         return result;
762 }
763
764 static WERROR cmd_srvsvc_net_sess_del(struct rpc_pipe_client *cli,
765                                       TALLOC_CTX *mem_ctx,
766                                       int argc, const char **argv)
767 {
768         WERROR result;
769         NTSTATUS status;
770         struct dcerpc_binding_handle *b = cli->binding_handle;
771
772         if (argc < 2 || argc > 4) {
773                 printf("Usage: %s [client] [user]\n", argv[0]);
774                 return WERR_OK;
775         }
776
777         status = dcerpc_srvsvc_NetSessDel(b, mem_ctx,
778                                           cli->desthost,
779                                           argv[1],
780                                           argv[2],
781                                           &result);
782         if (!NT_STATUS_IS_OK(status)) {
783                 result = ntstatus_to_werror(status);
784                 goto done;
785         }
786
787         if (!W_ERROR_IS_OK(result)) {
788                 goto done;
789         }
790
791  done:
792         return result;
793 }
794
795 static WERROR cmd_srvsvc_net_sess_enum(struct rpc_pipe_client *cli,
796                                        TALLOC_CTX *mem_ctx,
797                                        int argc, const char **argv)
798 {
799         WERROR result;
800         NTSTATUS status;
801         struct srvsvc_NetSessInfoCtr info_ctr;
802         struct srvsvc_NetSessCtr0 ctr0;
803         struct srvsvc_NetSessCtr1 ctr1;
804         struct srvsvc_NetSessCtr2 ctr2;
805         struct srvsvc_NetSessCtr10 ctr10;
806         struct srvsvc_NetSessCtr502 ctr502;
807         uint32_t total_entries = 0;
808         uint32_t resume_handle = 0;
809         uint32_t *resume_handle_p = NULL;
810         uint32_t level = 1;
811         const char *client = NULL;
812         const char *user = NULL;
813         struct dcerpc_binding_handle *b = cli->binding_handle;
814
815         if (argc > 6) {
816                 printf("Usage: %s [client] [user] [level] [resume_handle]\n", argv[0]);
817                 return WERR_OK;
818         }
819
820         if (argc >= 2) {
821                 client = argv[1];
822         }
823
824         if (argc >= 3) {
825                 user = argv[2];
826         }
827
828         if (argc >= 4) {
829                 level = atoi(argv[3]);
830         }
831
832         if (argc >= 5) {
833                 resume_handle = atoi(argv[4]);
834                 resume_handle_p = &resume_handle;
835         }
836
837         ZERO_STRUCT(info_ctr);
838
839         info_ctr.level = level;
840
841         d_printf("trying level: %d\n", level);
842
843         switch (level) {
844         case 0:
845                 ZERO_STRUCT(ctr0);
846                 info_ctr.ctr.ctr0 = &ctr0;
847                 break;
848         case 1:
849                 ZERO_STRUCT(ctr1);
850                 info_ctr.ctr.ctr1 = &ctr1;
851                 break;
852         case 2:
853                 ZERO_STRUCT(ctr2);
854                 info_ctr.ctr.ctr2 = &ctr2;
855                 break;
856         case 10:
857                 ZERO_STRUCT(ctr10);
858                 info_ctr.ctr.ctr10 = &ctr10;
859                 break;
860         case 502:
861                 ZERO_STRUCT(ctr502);
862                 info_ctr.ctr.ctr502 = &ctr502;
863                 break;
864         }
865
866         status = dcerpc_srvsvc_NetSessEnum(b, mem_ctx,
867                                           cli->desthost,
868                                           client,
869                                           user,
870                                           &info_ctr,
871                                           0xffffffff,
872                                           &total_entries,
873                                           resume_handle_p,
874                                           &result);
875
876         if (!NT_STATUS_IS_OK(status)) {
877                 result = ntstatus_to_werror(status);
878                 goto done;
879         }
880
881         if (!W_ERROR_IS_OK(result)) {
882                 goto done;
883         }
884
885         d_printf("Received %d entries.\n", total_entries);
886
887  done:
888         return result;
889 }
890
891 static WERROR cmd_srvsvc_net_disk_enum(struct rpc_pipe_client *cli,
892                                        TALLOC_CTX *mem_ctx,
893                                        int argc, const char **argv)
894 {
895         struct srvsvc_NetDiskInfo info;
896         WERROR result;
897         NTSTATUS status;
898         uint32_t total_entries = 0;
899         uint32_t resume_handle = 0;
900         uint32_t level = 0;
901         struct dcerpc_binding_handle *b = cli->binding_handle;
902
903         if (argc > 4) {
904                 printf("Usage: %s [level] [resume_handle]\n", argv[0]);
905                 return WERR_OK;
906         }
907
908         if (argc >= 2) {
909                 level = atoi(argv[1]);
910         }
911
912         if (argc >= 3) {
913                 resume_handle = atoi(argv[2]);
914         }
915
916         ZERO_STRUCT(info);
917
918         status = dcerpc_srvsvc_NetDiskEnum(b, mem_ctx,
919                                            cli->desthost,
920                                            level,
921                                            &info,
922                                            0xffffffff,
923                                            &total_entries,
924                                            &resume_handle,
925                                            &result);
926         if (!NT_STATUS_IS_OK(status)) {
927                 result = ntstatus_to_werror(status);
928                 goto done;
929         }
930
931         if (!W_ERROR_IS_OK(result)) {
932                 goto done;
933         }
934
935  done:
936         return result;
937 }
938
939 static WERROR cmd_srvsvc_net_conn_enum(struct rpc_pipe_client *cli,
940                                        TALLOC_CTX *mem_ctx,
941                                        int argc, const char **argv)
942 {
943         struct srvsvc_NetConnInfoCtr info_ctr;
944         struct srvsvc_NetConnCtr0 ctr0;
945         struct srvsvc_NetConnCtr1 ctr1;
946         WERROR result;
947         NTSTATUS status;
948         uint32_t total_entries = 0;
949         uint32_t resume_handle = 0;
950         uint32_t *resume_handle_p = NULL;
951         uint32_t level = 1;
952         const char *path = "IPC$";
953         struct dcerpc_binding_handle *b = cli->binding_handle;
954
955         if (argc > 4) {
956                 printf("Usage: %s [level] [path] [resume_handle]\n", argv[0]);
957                 return WERR_OK;
958         }
959
960         if (argc >= 2) {
961                 level = atoi(argv[1]);
962         }
963
964         if (argc >= 3) {
965                 path = argv[2];
966         }
967
968         if (argc >= 4) {
969                 resume_handle = atoi(argv[3]);
970                 resume_handle_p = &resume_handle;
971         }
972
973         ZERO_STRUCT(info_ctr);
974
975         info_ctr.level = level;
976
977         switch (level) {
978                 case 0:
979                         ZERO_STRUCT(ctr0);
980                         info_ctr.ctr.ctr0 = &ctr0;
981                         break;
982                 case 1:
983                         ZERO_STRUCT(ctr1);
984                         info_ctr.ctr.ctr1 = &ctr1;
985                         break;
986                 default:
987                         return WERR_INVALID_PARAMETER;
988         }
989
990         status = dcerpc_srvsvc_NetConnEnum(b, mem_ctx,
991                                            cli->desthost,
992                                            path,
993                                            &info_ctr,
994                                            0xffffffff,
995                                            &total_entries,
996                                            resume_handle_p,
997                                            &result);
998
999         if (!NT_STATUS_IS_OK(status)) {
1000                 result = ntstatus_to_werror(status);
1001                 goto done;
1002         }
1003
1004         if (!W_ERROR_IS_OK(result)) {
1005                 goto done;
1006         }
1007
1008  done:
1009         return result;
1010 }
1011
1012 static WERROR cmd_srvsvc_net_share_add(struct rpc_pipe_client *cli,
1013                                        TALLOC_CTX *mem_ctx,
1014                                        int argc, const char **argv)
1015 {
1016         struct srvsvc_NetShareInfo502 info502 = { 0 };
1017         union srvsvc_NetShareInfo info = { .info502 = &info502 };
1018         WERROR result;
1019         NTSTATUS status;
1020         uint32_t max_users = -1, parm_error;
1021         struct sec_desc_buf sd_buf = { 0 };
1022         const char *path, *share_name, *comment = NULL;
1023         struct dcerpc_binding_handle *b = cli->binding_handle;
1024
1025         if (argc < 3 || argc > 5) {
1026                 printf("Usage: %s path share_name [max_users] [comment]\n",
1027                        argv[0]);
1028                 return WERR_OK;
1029         }
1030
1031         path = argv[1];
1032         share_name = argv[2];
1033
1034         if (argc >= 4) {
1035                 max_users = atoi(argv[3]);
1036         }
1037
1038         if (argc >= 5) {
1039                 comment = argv[4];
1040         }
1041
1042         info.info502->name = share_name;
1043         info.info502->type = STYPE_DISKTREE;
1044         info.info502->comment = comment;
1045         info.info502->max_users = max_users;
1046         info.info502->path = path;
1047         info.info502->sd_buf = sd_buf;
1048
1049         status = dcerpc_srvsvc_NetShareAdd(b, mem_ctx, cli->desthost,
1050                                            502, &info, &parm_error, &result);
1051         if (!NT_STATUS_IS_OK(status)) {
1052                 result = ntstatus_to_werror(status);
1053         }
1054
1055         return result;
1056 }
1057
1058 static WERROR cmd_srvsvc_net_share_del(struct rpc_pipe_client *cli,
1059                                        TALLOC_CTX *mem_ctx,
1060                                        int argc, const char **argv)
1061 {
1062         const char *share_name;
1063         WERROR result;
1064         NTSTATUS status;
1065         struct dcerpc_binding_handle *b = cli->binding_handle;
1066
1067         if (argc < 2) {
1068                 printf("Usage: %s share_name\n", argv[0]);
1069                 return WERR_OK;
1070         }
1071
1072         share_name = argv[1];
1073
1074         status = dcerpc_srvsvc_NetShareDel(b, mem_ctx, cli->desthost,
1075                                            share_name, 0, &result);
1076
1077         if (!NT_STATUS_IS_OK(status)) {
1078                 result = ntstatus_to_werror(status);
1079         }
1080
1081         return result;
1082 }
1083
1084
1085 /* List of commands exported by this module */
1086
1087 struct cmd_set srvsvc_commands[] = {
1088
1089         {
1090                 .name = "SRVSVC",
1091         },
1092
1093         {
1094                 .name               = "srvinfo",
1095                 .returntype         = RPC_RTYPE_WERROR,
1096                 .ntfn               = NULL,
1097                 .wfn                = cmd_srvsvc_srv_query_info,
1098                 .table              = &ndr_table_srvsvc,
1099                 .rpc_pipe           = NULL,
1100                 .description        = "Server query info",
1101                 .usage              = "",
1102         },
1103         {
1104                 .name               = "netshareenum",
1105                 .returntype         = RPC_RTYPE_WERROR,
1106                 .ntfn               = NULL,
1107                 .wfn                = cmd_srvsvc_net_share_enum,
1108                 .table              = &ndr_table_srvsvc,
1109                 .rpc_pipe           = NULL,
1110                 .description        = "Enumerate shares",
1111                 .usage              = "",
1112         },
1113         {
1114                 .name               = "netshareenumall",
1115                 .returntype         = RPC_RTYPE_WERROR,
1116                 .ntfn               = NULL,
1117                 .wfn                = cmd_srvsvc_net_share_enum_all,
1118                 .table              = &ndr_table_srvsvc,
1119                 .rpc_pipe           = NULL,
1120                 .description        = "Enumerate all shares",
1121                 .usage              = "",
1122         },
1123         {
1124                 .name               = "netsharegetinfo",
1125                 .returntype         = RPC_RTYPE_WERROR,
1126                 .ntfn               = NULL,
1127                 .wfn                = cmd_srvsvc_net_share_get_info,
1128                 .table              = &ndr_table_srvsvc,
1129                 .rpc_pipe           = NULL,
1130                 .description        = "Get Share Info",
1131                 .usage              = "",
1132         },
1133         {
1134                 .name               = "netsharesetinfo",
1135                 .returntype         = RPC_RTYPE_WERROR,
1136                 .ntfn               = NULL,
1137                 .wfn                = cmd_srvsvc_net_share_set_info,
1138                 .table              = &ndr_table_srvsvc,
1139                 .rpc_pipe           = NULL,
1140                 .description        = "Set Share Info",
1141                 .usage              = "",
1142         },
1143         {
1144                 .name               = "netsharesetdfsflags",
1145                 .returntype         = RPC_RTYPE_WERROR,
1146                 .ntfn               = NULL,
1147                 .wfn                = cmd_srvsvc_net_share_set_dfs_flags,
1148                 .table              = &ndr_table_srvsvc,
1149                 .rpc_pipe           = NULL,
1150                 .description        = "Set DFS flags",
1151                 .usage              = "",
1152         },
1153         {
1154                 .name               = "netfileenum",
1155                 .returntype         = RPC_RTYPE_WERROR,
1156                 .ntfn               = NULL,
1157                 .wfn                = cmd_srvsvc_net_file_enum,
1158                 .table              =  &ndr_table_srvsvc,
1159                 .rpc_pipe           = NULL,
1160                 .description        = "Enumerate open files",
1161                 .usage              = "",
1162         },
1163         {
1164                 .name               = "netremotetod",
1165                 .returntype         = RPC_RTYPE_WERROR,
1166                 .ntfn               = NULL,
1167                 .wfn                = cmd_srvsvc_net_remote_tod,
1168                 .table              = &ndr_table_srvsvc,
1169                 .rpc_pipe           = NULL,
1170                 .description        = "Fetch remote time of day",
1171                 .usage              = "",
1172         },
1173         {
1174                 .name               = "netnamevalidate",
1175                 .returntype         = RPC_RTYPE_WERROR,
1176                 .ntfn               = NULL,
1177                 .wfn                = cmd_srvsvc_net_name_validate,
1178                 .table              = &ndr_table_srvsvc,
1179                 .rpc_pipe           = NULL,
1180                 .description        = "Validate sharename",
1181                 .usage              = "",
1182         },
1183         {
1184                 .name               = "netfilegetsec",
1185                 .returntype         = RPC_RTYPE_WERROR,
1186                 .ntfn               = NULL,
1187                 .wfn                = cmd_srvsvc_net_file_get_sec,
1188                 .table              = &ndr_table_srvsvc,
1189                 .rpc_pipe           = NULL,
1190                 .description        = "Get File security",
1191                 .usage              = "",
1192         },
1193         {
1194                 .name               = "netsessdel",
1195                 .returntype         = RPC_RTYPE_WERROR,
1196                 .ntfn               = NULL,
1197                 .wfn                = cmd_srvsvc_net_sess_del,
1198                 .table              = &ndr_table_srvsvc,
1199                 .rpc_pipe           = NULL,
1200                 .description        = "Delete Session",
1201                 .usage              = "",
1202         },
1203         {
1204                 .name               = "netsessenum",
1205                 .returntype         = RPC_RTYPE_WERROR,
1206                 .ntfn               = NULL,
1207                 .wfn                = cmd_srvsvc_net_sess_enum,
1208                 .table              = &ndr_table_srvsvc,
1209                 .rpc_pipe           = NULL,
1210                 .description        = "Enumerate Sessions",
1211                 .usage              = "",
1212         },
1213         {
1214                 .name               = "netdiskenum",
1215                 .returntype         = RPC_RTYPE_WERROR,
1216                 .ntfn               = NULL,
1217                 .wfn                = cmd_srvsvc_net_disk_enum,
1218                 .table              = &ndr_table_srvsvc,
1219                 .rpc_pipe           = NULL,
1220                 .description        = "Enumerate Disks",
1221                 .usage              = "",
1222         },
1223         {
1224                 .name               = "netconnenum",
1225                 .returntype         = RPC_RTYPE_WERROR,
1226                 .ntfn               = NULL,
1227                 .wfn                = cmd_srvsvc_net_conn_enum,
1228                 .table              = &ndr_table_srvsvc,
1229                 .rpc_pipe           = NULL,
1230                 .description        = "Enumerate Connections",
1231                 .usage              = "",
1232         },
1233         {
1234                 .name               = "netshareadd",
1235                 .returntype         = RPC_RTYPE_WERROR,
1236                 .ntfn               = NULL,
1237                 .wfn                = cmd_srvsvc_net_share_add,
1238                 .table              = &ndr_table_srvsvc,
1239                 .rpc_pipe           = NULL,
1240                 .description        = "Add share",
1241                 .usage              = "",
1242         },
1243         {
1244                 .name               = "netsharedel",
1245                 .returntype         = RPC_RTYPE_WERROR,
1246                 .ntfn               = NULL,
1247                 .wfn                = cmd_srvsvc_net_share_del,
1248                 .table              = &ndr_table_srvsvc,
1249                 .rpc_pipe           = NULL,
1250                 .description        = "Delete share",
1251                 .usage              = "",
1252         },
1253
1254         {
1255                 .name = NULL,
1256         }
1257 };