rpcclient "Service Control Manager" svcenum [-i] command.
[samba.git] / source / rpcclient / cmd_svcctl.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    NT Domain Authentication SMB / MSRPC client
5    Copyright (C) Andrew Tridgell 1994-1997
6    Copyright (C) Luke Kenneth Casson Leighton 1996-1997
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23
24
25 #ifdef SYSLOG
26 #undef SYSLOG
27 #endif
28
29 #include "includes.h"
30 #include "nterr.h"
31
32 extern int DEBUGLEVEL;
33
34 extern struct cli_state *smb_cli;
35 extern int smb_tidx;
36
37 extern FILE* out_hnd;
38
39 /****************************************************************************
40 nt svcistry enum
41 ****************************************************************************/
42 void cmd_svc_enum(struct client_info *info)
43 {
44         uint16 fnum;
45         BOOL res = True;
46         BOOL res1 = True;
47         int i;
48         uint32 resume_hnd = 0;
49         uint32 buf_size = 0;
50         uint32 dos_error = 0;
51         ENUM_SRVC_STATUS *svcs = NULL;
52         uint32 num_svcs = 0;
53         fstring tmp;
54         BOOL request_info = False;
55
56         POLICY_HND sc_man_pol;
57         
58         fstring srv_name;
59
60         fstrcpy(srv_name, "\\\\");
61         fstrcat(srv_name, info->myhostname);
62         strupper(srv_name);
63
64         DEBUG(4,("cmd_svc_enum: server:%s\n", srv_name));
65
66         if (next_token(NULL, tmp, NULL, sizeof(tmp)))
67         {
68                 request_info = strequal(tmp, "-i");
69         }
70
71         /* open SVCCTL session. */
72         res = res ? cli_nt_session_open(smb_cli, PIPE_SVCCTL, &fnum) : False;
73
74         /* open service control manager receive a policy handle */
75         res = res ? svc_open_sc_man(smb_cli, fnum,
76                                 srv_name, NULL, 0x80000004,
77                                 &sc_man_pol) : False;
78
79         do
80         {
81                 buf_size += 0x800;
82
83                 /* enumerate services */
84                 res1 = res ? svc_enum_svcs(smb_cli, fnum,
85                                         &sc_man_pol,
86                                         0x00000030, 0x00000003,
87                                         &buf_size, &resume_hnd, &dos_error,
88                                         &svcs, &num_svcs) : False;
89
90         } while (dos_error == ERRmoredata);
91
92         if (res1 && dos_error == 0x0 && num_svcs > 0 && svcs != NULL)
93         {
94                 fprintf(out_hnd,"Services\n");
95                 fprintf(out_hnd,"--------\n");
96         }
97
98         for (i = 0; i < num_svcs && svcs != NULL && res1; i++)
99         {
100                 BOOL res2 = request_info;
101                 BOOL res3;
102                 POLICY_HND svc_pol;
103                 fstring svc_name;
104                 QUERY_SERVICE_CONFIG cfg;
105                 uint32 svc_buf_size = 0x800;
106
107                 fstrcpy(svc_name, unistr2(svcs[i].uni_srvc_name.buffer));
108
109                 res2 = res2 ? svc_open_service(smb_cli, fnum,
110                                                &sc_man_pol,
111                                                svc_name, 0x80000001,
112                                                &svc_pol) : False;
113                 res3 = res2 ? svc_query_svc_cfg(smb_cli, fnum,
114                                                &svc_pol, &cfg,
115                                                &svc_buf_size) : False;
116
117                 if (res3)
118                 {
119                         display_query_svc_cfg(out_hnd, ACTION_HEADER   , &cfg);
120                         display_query_svc_cfg(out_hnd, ACTION_ENUMERATE, &cfg);
121                         display_query_svc_cfg(out_hnd, ACTION_FOOTER   , &cfg);
122                 }
123                 else
124                 {
125                         display_svc_info(out_hnd, ACTION_HEADER   , &svcs[i]);
126                         display_svc_info(out_hnd, ACTION_ENUMERATE, &svcs[i]);
127                         display_svc_info(out_hnd, ACTION_FOOTER   , &svcs[i]);
128                 }
129
130                 res2 = res2 ? svc_close(smb_cli, fnum, &svc_pol) : False;
131         }
132
133         if (svcs != NULL)
134         {
135                 free(svcs);
136         }
137
138         res = res ? svc_close(smb_cli, fnum, &sc_man_pol) : False;
139
140         /* close the session */
141         cli_nt_session_close(smb_cli, fnum);
142
143         if (res && res1)
144         {
145                 DEBUG(5,("cmd_svc_enum: query succeeded\n"));
146         }
147         else
148         {
149                 DEBUG(5,("cmd_svc_enum: query failed\n"));
150         }
151 }
152