r9661: fallback to cn attribubte if displayName is not available
[Samba.git] / source / utils / net_rpc_service.c
blob94644f8dcf493deada2ec26f56f3e269befd99a3
1 /*
2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) Gerald (Jerry) Carter 2005
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.
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.
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. */
20 #include "includes.h"
21 #include "utils/net.h"
24 /********************************************************************
25 ********************************************************************/
27 static WERROR query_service_state( struct cli_state *cli, TALLOC_CTX *mem_ctx,
28 POLICY_HND *hSCM, const char *service, uint32 *state )
30 POLICY_HND hService;
31 SERVICE_STATUS service_status;
32 WERROR result = WERR_GENERAL_FAILURE;
34 /* now cycle until the status is actually 'watch_state' */
36 result = cli_svcctl_open_service( cli, mem_ctx, hSCM, &hService,
37 service, SC_RIGHT_SVC_QUERY_STATUS );
39 if ( !W_ERROR_IS_OK(result) ) {
40 d_printf("Failed to open service. [%s]\n", dos_errstr(result));
41 return result;
44 result = cli_svcctl_query_status( cli, mem_ctx, &hService, &service_status );
45 if ( W_ERROR_IS_OK(result) ) {
46 *state = service_status.state;
49 cli_svcctl_close_service( cli, mem_ctx, &hService );
51 return result;
54 /********************************************************************
55 ********************************************************************/
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 )
61 uint32 i;
62 uint32 state = 0;
63 WERROR result = WERR_GENERAL_FAILURE;
66 i = 0;
67 while ( (state != watch_state ) && i<30 ) {
68 /* get the status */
70 result = query_service_state( cli, mem_ctx, hSCM, service, &state );
71 if ( !W_ERROR_IS_OK(result) ) {
72 break;
75 d_printf(".");
76 i++;
77 usleep( 100 );
79 d_printf("\n");
81 *final_state = state;
83 return result;
86 /********************************************************************
87 ********************************************************************/
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 )
93 POLICY_HND hService;
94 WERROR result = WERR_GENERAL_FAILURE;
95 SERVICE_STATUS service_status;
96 uint32 state = 0;
98 /* Open the Service */
100 result = cli_svcctl_open_service( cli, mem_ctx, hSCM, &hService,
101 service, (SC_RIGHT_SVC_STOP|SC_RIGHT_SVC_PAUSE_CONTINUE) );
103 if ( !W_ERROR_IS_OK(result) ) {
104 d_printf("Failed to open service. [%s]\n", dos_errstr(result));
105 goto done;
108 /* get the status */
110 result = cli_svcctl_control_service( cli, mem_ctx, &hService,
111 control, &service_status );
113 if ( !W_ERROR_IS_OK(result) ) {
114 d_printf("Control service request failed. [%s]\n", dos_errstr(result));
115 goto done;
118 /* loop -- checking the state until we are where we want to be */
120 result = watch_service_state( cli, mem_ctx, hSCM, service, watch_state, &state );
122 d_printf("%s service is %s.\n", service, svc_status_string(state));
124 done:
125 cli_svcctl_close_service( cli, mem_ctx, &hService );
127 return result;
130 /********************************************************************
131 ********************************************************************/
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 )
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;
145 if (argc != 0 ) {
146 d_printf("Usage: net rpc service list\n");
147 return NT_STATUS_OK;
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);
156 result = cli_svcctl_enumerate_services( cli, mem_ctx, &hSCM, SVCCTL_TYPE_WIN32,
157 SVCCTL_STATE_ALL, &num_services, &services );
159 if ( !W_ERROR_IS_OK(result) ) {
160 d_printf("Failed to enumerate services. [%s]\n", dos_errstr(result));
161 goto done;
164 if ( num_services == 0 )
165 d_printf("No services returned\n");
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 );
171 d_printf("%-20s \"%s\"\n", servicename, displayname);
174 done:
175 cli_svcctl_close_service( cli, mem_ctx, &hSCM );
177 return werror_to_ntstatus(result);
180 /********************************************************************
181 ********************************************************************/
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 )
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;
194 if (argc != 1 ) {
195 d_printf("Usage: net rpc service status <service>\n");
196 return NT_STATUS_OK;
199 fstrcpy( servicename, argv[0] );
201 /* Open the Service Control Manager */
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);
209 /* Open the Service */
211 result = cli_svcctl_open_service( cli, mem_ctx, &hSCM, &hService, servicename,
212 (SC_RIGHT_SVC_QUERY_STATUS|SC_RIGHT_SVC_QUERY_CONFIG) );
214 if ( !W_ERROR_IS_OK(result) ) {
215 d_printf("Failed to open service. [%s]\n", dos_errstr(result));
216 goto done;
219 /* get the status */
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;
227 d_printf("%s service is %s.\n", servicename, svc_status_string(service_status.state));
229 /* get the config */
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;
237 /* print out the configuration information for the service */
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);
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);
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);
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);
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);
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);
270 done:
271 cli_svcctl_close_service( cli, mem_ctx, &hService );
272 cli_svcctl_close_service( cli, mem_ctx, &hSCM );
274 return werror_to_ntstatus(result);
278 /********************************************************************
279 ********************************************************************/
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 )
285 POLICY_HND hSCM;
286 WERROR result = WERR_GENERAL_FAILURE;
287 fstring servicename;
289 if (argc != 1 ) {
290 d_printf("Usage: net rpc service status <service>\n");
291 return NT_STATUS_OK;
294 fstrcpy( servicename, argv[0] );
296 /* Open the Service Control Manager */
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);
304 result = control_service( cli, mem_ctx, &hSCM, servicename,
305 SVCCTL_CONTROL_STOP, SVCCTL_STOPPED );
307 cli_svcctl_close_service( cli, mem_ctx, &hSCM );
309 return werror_to_ntstatus(result);
312 /********************************************************************
313 ********************************************************************/
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 )
319 POLICY_HND hSCM;
320 WERROR result = WERR_GENERAL_FAILURE;
321 fstring servicename;
323 if (argc != 1 ) {
324 d_printf("Usage: net rpc service status <service>\n");
325 return NT_STATUS_OK;
328 fstrcpy( servicename, argv[0] );
330 /* Open the Service Control Manager */
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);
338 result = control_service( cli, mem_ctx, &hSCM, servicename,
339 SVCCTL_CONTROL_PAUSE, SVCCTL_PAUSED );
341 cli_svcctl_close_service( cli, mem_ctx, &hSCM );
343 return werror_to_ntstatus(result);
346 /********************************************************************
347 ********************************************************************/
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 )
353 POLICY_HND hSCM;
354 WERROR result = WERR_GENERAL_FAILURE;
355 fstring servicename;
357 if (argc != 1 ) {
358 d_printf("Usage: net rpc service status <service>\n");
359 return NT_STATUS_OK;
362 fstrcpy( servicename, argv[0] );
364 /* Open the Service Control Manager */
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);
372 result = control_service( cli, mem_ctx, &hSCM, servicename,
373 SVCCTL_CONTROL_CONTINUE, SVCCTL_RUNNING );
375 cli_svcctl_close_service( cli, mem_ctx, &hSCM );
377 return werror_to_ntstatus(result);
380 /********************************************************************
381 ********************************************************************/
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 )
387 POLICY_HND hSCM, hService;
388 WERROR result = WERR_GENERAL_FAILURE;
389 fstring servicename;
390 uint32 state = 0;
392 if (argc != 1 ) {
393 d_printf("Usage: net rpc service status <service>\n");
394 return NT_STATUS_OK;
397 fstrcpy( servicename, argv[0] );
399 /* Open the Service Control Manager */
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);
407 /* Open the Service */
409 result = cli_svcctl_open_service( cli, mem_ctx, &hSCM, &hService,
410 servicename, SC_RIGHT_SVC_START );
412 if ( !W_ERROR_IS_OK(result) ) {
413 d_printf("Failed to open service. [%s]\n", dos_errstr(result));
414 goto done;
417 /* get the status */
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;
425 result = watch_service_state( cli, mem_ctx, &hSCM, servicename, SVCCTL_RUNNING, &state );
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) );
432 done:
433 cli_svcctl_close_service( cli, mem_ctx, &hService );
434 cli_svcctl_close_service( cli, mem_ctx, &hSCM );
436 return werror_to_ntstatus(result);
439 /********************************************************************
440 ********************************************************************/
442 static int rpc_service_list( int argc, const char **argv )
444 return run_rpc_command( NULL, PI_SVCCTL, 0,
445 rpc_service_list_internal, argc, argv );
448 /********************************************************************
449 ********************************************************************/
451 static int rpc_service_start( int argc, const char **argv )
453 return run_rpc_command( NULL, PI_SVCCTL, 0,
454 rpc_service_start_internal, argc, argv );
457 /********************************************************************
458 ********************************************************************/
460 static int rpc_service_stop( int argc, const char **argv )
462 return run_rpc_command( NULL, PI_SVCCTL, 0,
463 rpc_service_stop_internal, argc, argv );
466 /********************************************************************
467 ********************************************************************/
469 static int rpc_service_resume( int argc, const char **argv )
471 return run_rpc_command( NULL, PI_SVCCTL, 0,
472 rpc_service_resume_internal, argc, argv );
475 /********************************************************************
476 ********************************************************************/
478 static int rpc_service_pause( int argc, const char **argv )
480 return run_rpc_command( NULL, PI_SVCCTL, 0,
481 rpc_service_pause_internal, argc, argv );
484 /********************************************************************
485 ********************************************************************/
487 static int rpc_service_status( int argc, const char **argv )
489 return run_rpc_command( NULL, PI_SVCCTL, 0,
490 rpc_service_status_internal, argc, argv );
493 /********************************************************************
494 ********************************************************************/
496 static int net_help_service( int argc, const char **argv )
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 a service\n");
503 d_printf("net rpc service status <service> View the current status of a service\n");
505 return -1;
508 /********************************************************************
509 ********************************************************************/
511 int net_rpc_service(int argc, const char **argv)
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}
523 if ( argc )
524 return net_run_function( argc, argv, func, net_help_service );
526 return net_help_service( argc, argv );