8f93ab3d060842f6f45f01c806f25a0e9adcf946
[nivanova/samba-autobuild/.git] / source3 / utils / net_rpc_service.c
1 /* 
2    Samba Unix/Linux SMB client library 
3    Distributed SMB/CIFS Server Management Utility 
4    Copyright (C) Gerald (Jerry) Carter          2005
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19  
20 #include "includes.h"
21 #include "utils/net.h"
22
23
24 /********************************************************************
25 ********************************************************************/
26
27 static WERROR query_service_state( struct cli_state *cli, TALLOC_CTX *mem_ctx, 
28                              POLICY_HND *hSCM, const char *service, uint32 *state )
29 {
30         POLICY_HND hService;
31         SERVICE_STATUS service_status;
32         WERROR result = WERR_GENERAL_FAILURE;
33         
34         /* now cycle until the status is actually 'watch_state' */
35         
36         result = cli_svcctl_open_service( cli, mem_ctx, hSCM, &hService, 
37                 service, SC_RIGHT_SVC_QUERY_STATUS );
38
39         if ( !W_ERROR_IS_OK(result) ) {
40                 d_printf("Failed to open service.  [%s]\n", dos_errstr(result));
41                 return result;
42         }
43
44         result = cli_svcctl_query_status( cli, mem_ctx, &hService, &service_status  );
45         if ( W_ERROR_IS_OK(result) ) {
46                 *state = service_status.state;
47         }
48         
49         cli_svcctl_close_service( cli, mem_ctx, &hService );
50         
51         return result;
52 }
53
54 /********************************************************************
55 ********************************************************************/
56
57 static WERROR watch_service_state( struct cli_state *cli, TALLOC_CTX *mem_ctx, 
58                                    POLICY_HND *hSCM, const char *service, 
59                                    uint32 watch_state, uint32 *final_state )
60 {
61         uint32 i;
62         uint32 state = 0;
63         WERROR result = WERR_GENERAL_FAILURE;
64         
65         
66         i = 0;
67         while ( (state != watch_state ) && i<30 ) {
68                 /* get the status */
69
70                 result = query_service_state( cli, mem_ctx, hSCM, service, &state  );
71                 if ( !W_ERROR_IS_OK(result) ) {
72                         break;
73                 }
74                 
75                 d_printf(".");
76                 i++;
77                 usleep( 100 );
78         }
79         d_printf("\n");
80         
81         *final_state = state;
82         
83         return result;
84 }
85
86 /********************************************************************
87 ********************************************************************/
88
89 static WERROR control_service( struct cli_state *cli, TALLOC_CTX *mem_ctx, 
90                              POLICY_HND *hSCM, const char *service, 
91                              uint32 control, uint32 watch_state )
92 {
93         POLICY_HND hService;
94         WERROR result = WERR_GENERAL_FAILURE;
95         SERVICE_STATUS service_status;
96         uint32 state = 0;
97         
98         /* Open the Service */
99         
100         result = cli_svcctl_open_service( cli, mem_ctx, hSCM, &hService, 
101                 service, (SC_RIGHT_SVC_STOP|SC_RIGHT_SVC_PAUSE_CONTINUE) );
102
103         if ( !W_ERROR_IS_OK(result) ) {
104                 d_printf("Failed to open service.  [%s]\n", dos_errstr(result));
105                 goto done;
106         }
107         
108         /* get the status */
109
110         result = cli_svcctl_control_service( cli, mem_ctx, &hService, 
111                 control, &service_status  );
112                 
113         if ( !W_ERROR_IS_OK(result) ) {
114                 d_printf("Control service request failed.  [%s]\n", dos_errstr(result));
115                 goto done;
116         }
117         
118         /* loop -- checking the state until we are where we want to be */
119         
120         result = watch_service_state( cli, mem_ctx, hSCM, service, watch_state, &state );
121                 
122         d_printf("%s service is %s.\n", service, svc_status_string(state));
123
124 done:   
125         cli_svcctl_close_service( cli, mem_ctx, &hService  );
126                 
127         return result;
128 }       
129
130 /********************************************************************
131 ********************************************************************/
132
133 static NTSTATUS rpc_service_list_internal( const DOM_SID *domain_sid, const char *domain_name, 
134                                            struct cli_state *cli, TALLOC_CTX *mem_ctx, 
135                                            int argc, const char **argv )
136 {
137         POLICY_HND hSCM;
138         ENUM_SERVICES_STATUS *services;
139         WERROR result = WERR_GENERAL_FAILURE;
140         fstring servicename;
141         fstring displayname;
142         uint32 num_services = 0;
143         int i;
144         
145         if (argc != 0 ) {
146                 d_printf("Usage: net rpc service list\n");
147                 return NT_STATUS_OK;
148         }
149
150         result = cli_svcctl_open_scm( cli, mem_ctx, &hSCM, SC_RIGHT_MGR_ENUMERATE_SERVICE  );
151         if ( !W_ERROR_IS_OK(result) ) {
152                 d_printf("Failed to open Service Control Manager.  [%s]\n", dos_errstr(result));
153                 return werror_to_ntstatus(result);
154         }
155         
156         result = cli_svcctl_enumerate_services( cli, mem_ctx, &hSCM, SVCCTL_TYPE_WIN32,
157                 SVCCTL_STATE_ALL, &num_services, &services );
158         
159         if ( !W_ERROR_IS_OK(result) ) {
160                 d_printf("Failed to enumerate services.  [%s]\n", dos_errstr(result));
161                 goto done;
162         }
163         
164         if ( num_services == 0 )
165                 d_printf("No services returned\n");
166         
167         for ( i=0; i<num_services; i++ ) {
168                 rpcstr_pull( servicename, services[i].servicename.buffer, sizeof(servicename), -1, STR_TERMINATE );
169                 rpcstr_pull( displayname, services[i].displayname.buffer, sizeof(displayname), -1, STR_TERMINATE );
170                 
171                 d_printf("%-20s    \"%s\"\n", servicename, displayname);
172         }
173
174 done:   
175         cli_svcctl_close_service( cli, mem_ctx, &hSCM  );
176                 
177         return werror_to_ntstatus(result);
178 }       
179
180 /********************************************************************
181 ********************************************************************/
182
183 static NTSTATUS rpc_service_status_internal( const DOM_SID *domain_sid, const char *domain_name, 
184                                            struct cli_state *cli, TALLOC_CTX *mem_ctx, 
185                                            int argc, const char **argv )
186 {
187         POLICY_HND hSCM, hService;
188         WERROR result = WERR_GENERAL_FAILURE;
189         fstring servicename;
190         SERVICE_STATUS service_status;
191         SERVICE_CONFIG config;
192         fstring ascii_string;
193         
194         if (argc != 1 ) {
195                 d_printf("Usage: net rpc service status <service>\n");
196                 return NT_STATUS_OK;
197         }
198
199         fstrcpy( servicename, argv[0] );
200
201         /* Open the Service Control Manager */
202         
203         result = cli_svcctl_open_scm( cli, mem_ctx, &hSCM, SC_RIGHT_MGR_ENUMERATE_SERVICE  );
204         if ( !W_ERROR_IS_OK(result) ) {
205                 d_printf("Failed to open Service Control Manager.  [%s]\n", dos_errstr(result));
206                 return werror_to_ntstatus(result);
207         }
208         
209         /* Open the Service */
210         
211         result = cli_svcctl_open_service( cli, mem_ctx, &hSCM, &hService, servicename, 
212                 (SC_RIGHT_SVC_QUERY_STATUS|SC_RIGHT_SVC_QUERY_CONFIG) );
213
214         if ( !W_ERROR_IS_OK(result) ) {
215                 d_printf("Failed to open service.  [%s]\n", dos_errstr(result));
216                 goto done;
217         }
218         
219         /* get the status */
220
221         result = cli_svcctl_query_status( cli, mem_ctx, &hService, &service_status  );
222         if ( !W_ERROR_IS_OK(result) ) {
223                 d_printf("Query status request failed.  [%s]\n", dos_errstr(result));
224                 goto done;
225         }
226         
227         d_printf("%s service is %s.\n", servicename, svc_status_string(service_status.state));
228
229         /* get the config */
230
231         result = cli_svcctl_query_config( cli, mem_ctx, &hService, &config  );
232         if ( !W_ERROR_IS_OK(result) ) {
233                 d_printf("Query config request failed.  [%s]\n", dos_errstr(result));
234                 goto done;
235         }
236
237         /* print out the configuration information for the service */
238
239         d_printf("Configuration details:\n");
240         d_printf("\tService Type         = 0x%x\n", config.service_type);
241         d_printf("\tStart Type           = 0x%x\n", config.start_type);
242         d_printf("\tError Control        = 0x%x\n", config.error_control);
243         d_printf("\tTag ID               = 0x%x\n", config.tag_id);
244
245         if ( config.executablepath ) {
246                 rpcstr_pull( ascii_string, config.executablepath->buffer, sizeof(ascii_string), -1, STR_TERMINATE );
247                 d_printf("\tExecutable Path      = %s\n", ascii_string);
248         }
249
250         if ( config.loadordergroup ) {
251                 rpcstr_pull( ascii_string, config.loadordergroup->buffer, sizeof(ascii_string), -1, STR_TERMINATE );
252                 d_printf("\tLoad Order Group     = %s\n", ascii_string);
253         }
254
255         if ( config.dependencies ) {
256                 rpcstr_pull( ascii_string, config.dependencies->buffer, sizeof(ascii_string), -1, STR_TERMINATE );
257                 d_printf("\tDependencies         = %s\n", ascii_string);
258         }
259
260         if ( config.startname ) {
261                 rpcstr_pull( ascii_string, config.startname->buffer, sizeof(ascii_string), -1, STR_TERMINATE );
262                 d_printf("\tStart Name           = %s\n", ascii_string);
263         }
264
265         if ( config.displayname ) {
266                 rpcstr_pull( ascii_string, config.displayname->buffer, sizeof(ascii_string), -1, STR_TERMINATE );
267                 d_printf("\tDisplay Name         = %s\n", ascii_string);
268         }
269
270 done:   
271         cli_svcctl_close_service( cli, mem_ctx, &hService  );
272         cli_svcctl_close_service( cli, mem_ctx, &hSCM  );
273                 
274         return werror_to_ntstatus(result);
275 }       
276
277
278 /********************************************************************
279 ********************************************************************/
280
281 static NTSTATUS rpc_service_stop_internal( const DOM_SID *domain_sid, const char *domain_name, 
282                                            struct cli_state *cli, TALLOC_CTX *mem_ctx, 
283                                            int argc, const char **argv )
284 {
285         POLICY_HND hSCM;
286         WERROR result = WERR_GENERAL_FAILURE;
287         fstring servicename;
288         
289         if (argc != 1 ) {
290                 d_printf("Usage: net rpc service status <service>\n");
291                 return NT_STATUS_OK;
292         }
293
294         fstrcpy( servicename, argv[0] );
295
296         /* Open the Service Control Manager */
297         
298         result = cli_svcctl_open_scm( cli, mem_ctx, &hSCM, SC_RIGHT_MGR_ENUMERATE_SERVICE  );
299         if ( !W_ERROR_IS_OK(result) ) {
300                 d_printf("Failed to open Service Control Manager.  [%s]\n", dos_errstr(result));
301                 return werror_to_ntstatus(result);
302         }
303         
304         result = control_service( cli, mem_ctx, &hSCM, servicename, 
305                 SVCCTL_CONTROL_STOP, SVCCTL_STOPPED );
306                 
307         cli_svcctl_close_service( cli, mem_ctx, &hSCM  );
308                 
309         return werror_to_ntstatus(result);
310 }       
311
312 /********************************************************************
313 ********************************************************************/
314
315 static NTSTATUS rpc_service_pause_internal( const DOM_SID *domain_sid, const char *domain_name, 
316                                            struct cli_state *cli, TALLOC_CTX *mem_ctx, 
317                                            int argc, const char **argv )
318 {
319         POLICY_HND hSCM;
320         WERROR result = WERR_GENERAL_FAILURE;
321         fstring servicename;
322         
323         if (argc != 1 ) {
324                 d_printf("Usage: net rpc service status <service>\n");
325                 return NT_STATUS_OK;
326         }
327
328         fstrcpy( servicename, argv[0] );
329
330         /* Open the Service Control Manager */
331         
332         result = cli_svcctl_open_scm( cli, mem_ctx, &hSCM, SC_RIGHT_MGR_ENUMERATE_SERVICE  );
333         if ( !W_ERROR_IS_OK(result) ) {
334                 d_printf("Failed to open Service Control Manager.  [%s]\n", dos_errstr(result));
335                 return werror_to_ntstatus(result);
336         }
337         
338         result = control_service( cli, mem_ctx, &hSCM, servicename, 
339                 SVCCTL_CONTROL_PAUSE, SVCCTL_PAUSED );
340                 
341         cli_svcctl_close_service( cli, mem_ctx, &hSCM  );
342                 
343         return werror_to_ntstatus(result);
344 }       
345
346 /********************************************************************
347 ********************************************************************/
348
349 static NTSTATUS rpc_service_resume_internal( const DOM_SID *domain_sid, const char *domain_name, 
350                                            struct cli_state *cli, TALLOC_CTX *mem_ctx, 
351                                            int argc, const char **argv )
352 {
353         POLICY_HND hSCM;
354         WERROR result = WERR_GENERAL_FAILURE;
355         fstring servicename;
356         
357         if (argc != 1 ) {
358                 d_printf("Usage: net rpc service status <service>\n");
359                 return NT_STATUS_OK;
360         }
361
362         fstrcpy( servicename, argv[0] );
363
364         /* Open the Service Control Manager */
365         
366         result = cli_svcctl_open_scm( cli, mem_ctx, &hSCM, SC_RIGHT_MGR_ENUMERATE_SERVICE  );
367         if ( !W_ERROR_IS_OK(result) ) {
368                 d_printf("Failed to open Service Control Manager.  [%s]\n", dos_errstr(result));
369                 return werror_to_ntstatus(result);
370         }
371         
372         result = control_service( cli, mem_ctx, &hSCM, servicename, 
373                 SVCCTL_CONTROL_CONTINUE, SVCCTL_RUNNING );
374                 
375         cli_svcctl_close_service( cli, mem_ctx, &hSCM  );
376                 
377         return werror_to_ntstatus(result);
378 }       
379
380 /********************************************************************
381 ********************************************************************/
382
383 static NTSTATUS rpc_service_start_internal( const DOM_SID *domain_sid, const char *domain_name, 
384                                            struct cli_state *cli, TALLOC_CTX *mem_ctx, 
385                                            int argc, const char **argv )
386 {
387         POLICY_HND hSCM, hService;
388         WERROR result = WERR_GENERAL_FAILURE;
389         fstring servicename;
390         uint32 state = 0;
391         
392         if (argc != 1 ) {
393                 d_printf("Usage: net rpc service status <service>\n");
394                 return NT_STATUS_OK;
395         }
396
397         fstrcpy( servicename, argv[0] );
398
399         /* Open the Service Control Manager */
400         
401         result = cli_svcctl_open_scm( cli, mem_ctx, &hSCM, SC_RIGHT_MGR_ENUMERATE_SERVICE  );
402         if ( !W_ERROR_IS_OK(result) ) {
403                 d_printf("Failed to open Service Control Manager.  [%s]\n", dos_errstr(result));
404                 return werror_to_ntstatus(result);
405         }
406         
407         /* Open the Service */
408         
409         result = cli_svcctl_open_service( cli, mem_ctx, &hSCM, &hService, 
410                 servicename, SC_RIGHT_SVC_START );
411
412         if ( !W_ERROR_IS_OK(result) ) {
413                 d_printf("Failed to open service.  [%s]\n", dos_errstr(result));
414                 goto done;
415         }
416         
417         /* get the status */
418
419         result = cli_svcctl_start_service( cli, mem_ctx, &hService, NULL, 0 );
420         if ( !W_ERROR_IS_OK(result) ) {
421                 d_printf("Query status request failed.  [%s]\n", dos_errstr(result));
422                 goto done;
423         }
424         
425         result = watch_service_state( cli, mem_ctx, &hSCM, servicename, SVCCTL_RUNNING, &state  );
426         
427         if ( W_ERROR_IS_OK(result) && (state == SVCCTL_RUNNING) )
428                 d_printf("Successfully started service: %s\n", servicename );
429         else
430                 d_printf("Failed to start service: %s [%s]\n", servicename, dos_errstr(result) );
431         
432 done:   
433         cli_svcctl_close_service( cli, mem_ctx, &hService  );
434         cli_svcctl_close_service( cli, mem_ctx, &hSCM  );
435                 
436         return werror_to_ntstatus(result);
437 }
438
439 /********************************************************************
440 ********************************************************************/
441
442 static int rpc_service_list( int argc, const char **argv )
443 {
444         return run_rpc_command( NULL, PI_SVCCTL, 0, 
445                 rpc_service_list_internal, argc, argv );
446 }
447
448 /********************************************************************
449 ********************************************************************/
450
451 static int rpc_service_start( int argc, const char **argv )
452 {
453         return run_rpc_command( NULL, PI_SVCCTL, 0, 
454                 rpc_service_start_internal, argc, argv );
455 }
456
457 /********************************************************************
458 ********************************************************************/
459
460 static int rpc_service_stop( int argc, const char **argv )
461 {
462         return run_rpc_command( NULL, PI_SVCCTL, 0, 
463                 rpc_service_stop_internal, argc, argv );
464 }
465
466 /********************************************************************
467 ********************************************************************/
468
469 static int rpc_service_resume( int argc, const char **argv )
470 {
471         return run_rpc_command( NULL, PI_SVCCTL, 0, 
472                 rpc_service_resume_internal, argc, argv );
473 }
474
475 /********************************************************************
476 ********************************************************************/
477
478 static int rpc_service_pause( int argc, const char **argv )
479 {
480         return run_rpc_command( NULL, PI_SVCCTL, 0, 
481                 rpc_service_pause_internal, argc, argv );
482 }
483
484 /********************************************************************
485 ********************************************************************/
486
487 static int rpc_service_status( int argc, const char **argv )
488 {
489         return run_rpc_command( NULL, PI_SVCCTL, 0, 
490                 rpc_service_status_internal, argc, argv );
491 }
492
493 /********************************************************************
494 ********************************************************************/
495
496 static int net_help_service( int argc, const char **argv )
497 {
498         d_printf("net rpc service list               View configured Win32 services\n");
499         d_printf("net rpc service start <service>    Start a service\n");
500         d_printf("net rpc service stop <service>     Stop a service\n");
501         d_printf("net rpc service pause <service>    Pause a service\n");
502         d_printf("net rpc service resume <service>   Resume a paused service\n");
503         d_printf("net rpc service status <service>   View the current status of a service\n");
504         
505         return -1;
506 }
507
508 /********************************************************************
509 ********************************************************************/
510
511 int net_rpc_service(int argc, const char **argv) 
512 {
513         struct functable func[] = {
514                 {"list", rpc_service_list},
515                 {"start", rpc_service_start},
516                 {"stop", rpc_service_stop},
517                 {"pause", rpc_service_pause},
518                 {"resume", rpc_service_resume},
519                 {"status", rpc_service_status},
520                 {NULL, NULL}
521         };
522         
523         if ( argc )
524                 return net_run_function( argc, argv, func, net_help_service );
525                 
526         return net_help_service( argc, argv );
527 }
528
529