printing: Add new parameter "cups timeout".
[Samba.git] / source / utils / net_rpc_service.c
blobd1349a903d0e4e8c70f8db8fb8548c8a3d764336
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 3 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, see <http://www.gnu.org/licenses/>. */
19 #include "includes.h"
20 #include "utils/net.h"
23 struct svc_state_msg {
24 uint32 flag;
25 const char *message;
28 static struct svc_state_msg state_msg_table[] = {
29 { SVCCTL_STOPPED, "stopped" },
30 { SVCCTL_START_PENDING, "start pending" },
31 { SVCCTL_STOP_PENDING, "stop pending" },
32 { SVCCTL_RUNNING, "running" },
33 { SVCCTL_CONTINUE_PENDING, "resume pending" },
34 { SVCCTL_PAUSE_PENDING, "pause pending" },
35 { SVCCTL_PAUSED, "paused" },
36 { 0, NULL }
40 /********************************************************************
41 ********************************************************************/
42 const char *svc_status_string( uint32 state )
44 fstring msg;
45 int i;
47 fstr_sprintf( msg, "Unknown State [%d]", state );
49 for ( i=0; state_msg_table[i].message; i++ ) {
50 if ( state_msg_table[i].flag == state ) {
51 fstrcpy( msg, state_msg_table[i].message );
52 break;
56 return talloc_strdup(talloc_tos(), msg);
59 /********************************************************************
60 ********************************************************************/
62 static WERROR query_service_state(struct rpc_pipe_client *pipe_hnd,
63 TALLOC_CTX *mem_ctx,
64 POLICY_HND *hSCM,
65 const char *service,
66 uint32 *state )
68 POLICY_HND hService;
69 SERVICE_STATUS service_status;
70 WERROR result = WERR_GENERAL_FAILURE;
71 NTSTATUS status;
73 /* now cycle until the status is actually 'watch_state' */
75 status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
76 hSCM,
77 service,
78 SC_RIGHT_SVC_QUERY_STATUS,
79 &hService,
80 &result);
81 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
82 d_fprintf(stderr, "Failed to open service. [%s]\n", dos_errstr(result));
83 return result;
86 status = rpccli_svcctl_QueryServiceStatus(pipe_hnd, mem_ctx,
87 &hService,
88 &service_status,
89 &result);
91 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
92 *state = service_status.state;
95 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
97 return result;
100 /********************************************************************
101 ********************************************************************/
103 static WERROR watch_service_state(struct rpc_pipe_client *pipe_hnd,
104 TALLOC_CTX *mem_ctx,
105 POLICY_HND *hSCM,
106 const char *service,
107 uint32 watch_state,
108 uint32 *final_state )
110 uint32 i;
111 uint32 state = 0;
112 WERROR result = WERR_GENERAL_FAILURE;
115 i = 0;
116 while ( (state != watch_state ) && i<30 ) {
117 /* get the status */
119 result = query_service_state(pipe_hnd, mem_ctx, hSCM, service, &state );
120 if ( !W_ERROR_IS_OK(result) ) {
121 break;
124 d_printf(".");
125 i++;
126 sys_usleep( 100 );
128 d_printf("\n");
130 *final_state = state;
132 return result;
135 /********************************************************************
136 ********************************************************************/
138 static WERROR control_service(struct rpc_pipe_client *pipe_hnd,
139 TALLOC_CTX *mem_ctx,
140 POLICY_HND *hSCM,
141 const char *service,
142 uint32 control,
143 uint32 watch_state )
145 POLICY_HND hService;
146 WERROR result = WERR_GENERAL_FAILURE;
147 NTSTATUS status;
148 SERVICE_STATUS service_status;
149 uint32 state = 0;
151 /* Open the Service */
153 status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
154 hSCM,
155 service,
156 (SC_RIGHT_SVC_STOP|SC_RIGHT_SVC_PAUSE_CONTINUE),
157 &hService,
158 &result);
160 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
161 d_fprintf(stderr, "Failed to open service. [%s]\n", dos_errstr(result));
162 goto done;
165 /* get the status */
167 status = rpccli_svcctl_ControlService(pipe_hnd, mem_ctx,
168 &hService,
169 control,
170 &service_status,
171 &result);
173 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
174 d_fprintf(stderr, "Control service request failed. [%s]\n", dos_errstr(result));
175 goto done;
178 /* loop -- checking the state until we are where we want to be */
180 result = watch_service_state(pipe_hnd, mem_ctx, hSCM, service, watch_state, &state );
182 d_printf("%s service is %s.\n", service, svc_status_string(state));
184 done:
185 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
187 return result;
190 /********************************************************************
191 ********************************************************************/
193 static NTSTATUS rpc_service_list_internal(struct net_context *c,
194 const DOM_SID *domain_sid,
195 const char *domain_name,
196 struct cli_state *cli,
197 struct rpc_pipe_client *pipe_hnd,
198 TALLOC_CTX *mem_ctx,
199 int argc,
200 const char **argv )
202 POLICY_HND hSCM;
203 ENUM_SERVICES_STATUS *services;
204 WERROR result = WERR_GENERAL_FAILURE;
205 NTSTATUS status;
206 fstring servicename;
207 fstring displayname;
208 uint32 num_services = 0;
209 int i;
211 if (argc != 0 ) {
212 d_printf("Usage: net rpc service list\n");
213 return NT_STATUS_OK;
216 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
217 pipe_hnd->srv_name_slash,
218 NULL,
219 SC_RIGHT_MGR_ENUMERATE_SERVICE,
220 &hSCM,
221 &result);
222 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
223 d_fprintf(stderr, "Failed to open Service Control Manager. [%s]\n", dos_errstr(result));
224 return werror_to_ntstatus(result);
227 result = rpccli_svcctl_enumerate_services(pipe_hnd, mem_ctx, &hSCM, SVCCTL_TYPE_WIN32,
228 SVCCTL_STATE_ALL, &num_services, &services );
230 if ( !W_ERROR_IS_OK(result) ) {
231 d_fprintf(stderr, "Failed to enumerate services. [%s]\n", dos_errstr(result));
232 goto done;
235 if ( num_services == 0 )
236 d_printf("No services returned\n");
238 for ( i=0; i<num_services; i++ ) {
239 rpcstr_pull( servicename, services[i].servicename.buffer, sizeof(servicename), -1, STR_TERMINATE );
240 rpcstr_pull( displayname, services[i].displayname.buffer, sizeof(displayname), -1, STR_TERMINATE );
242 d_printf("%-20s \"%s\"\n", servicename, displayname);
245 done:
246 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
248 return werror_to_ntstatus(result);
251 /********************************************************************
252 ********************************************************************/
254 static NTSTATUS rpc_service_status_internal(struct net_context *c,
255 const DOM_SID *domain_sid,
256 const char *domain_name,
257 struct cli_state *cli,
258 struct rpc_pipe_client *pipe_hnd,
259 TALLOC_CTX *mem_ctx,
260 int argc,
261 const char **argv )
263 POLICY_HND hSCM, hService;
264 WERROR result = WERR_GENERAL_FAILURE;
265 NTSTATUS status;
266 SERVICE_STATUS service_status;
267 SERVICE_CONFIG config;
268 fstring ascii_string;
270 if (argc != 1 ) {
271 d_printf("Usage: net rpc service status <service>\n");
272 return NT_STATUS_OK;
275 /* Open the Service Control Manager */
276 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
277 pipe_hnd->srv_name_slash,
278 NULL,
279 SC_RIGHT_MGR_ENUMERATE_SERVICE,
280 &hSCM,
281 &result);
282 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
283 d_fprintf(stderr, "Failed to open Service Control Manager. [%s]\n", dos_errstr(result));
284 return werror_to_ntstatus(result);
287 /* Open the Service */
289 status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
290 &hSCM,
291 argv[0],
292 (SC_RIGHT_SVC_QUERY_STATUS|SC_RIGHT_SVC_QUERY_CONFIG),
293 &hService,
294 &result);
296 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
297 d_fprintf(stderr, "Failed to open service. [%s]\n", dos_errstr(result));
298 goto done;
301 /* get the status */
303 status = rpccli_svcctl_QueryServiceStatus(pipe_hnd, mem_ctx,
304 &hService,
305 &service_status,
306 &result);
308 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
309 d_fprintf(stderr, "Query status request failed. [%s]\n", dos_errstr(result));
310 goto done;
313 d_printf("%s service is %s.\n", argv[0], svc_status_string(service_status.state));
315 /* get the config */
317 result = rpccli_svcctl_query_config(pipe_hnd, mem_ctx, &hService, &config );
318 if ( !W_ERROR_IS_OK(result) ) {
319 d_fprintf(stderr, "Query config request failed. [%s]\n", dos_errstr(result));
320 goto done;
323 /* print out the configuration information for the service */
325 d_printf("Configuration details:\n");
326 d_printf("\tControls Accepted = 0x%x\n", service_status.controls_accepted);
327 d_printf("\tService Type = 0x%x\n", config.service_type);
328 d_printf("\tStart Type = 0x%x\n", config.start_type);
329 d_printf("\tError Control = 0x%x\n", config.error_control);
330 d_printf("\tTag ID = 0x%x\n", config.tag_id);
332 if ( config.executablepath ) {
333 rpcstr_pull( ascii_string, config.executablepath->buffer, sizeof(ascii_string), -1, STR_TERMINATE );
334 d_printf("\tExecutable Path = %s\n", ascii_string);
337 if ( config.loadordergroup ) {
338 rpcstr_pull( ascii_string, config.loadordergroup->buffer, sizeof(ascii_string), -1, STR_TERMINATE );
339 d_printf("\tLoad Order Group = %s\n", ascii_string);
342 if ( config.dependencies ) {
343 rpcstr_pull( ascii_string, config.dependencies->buffer, sizeof(ascii_string), -1, STR_TERMINATE );
344 d_printf("\tDependencies = %s\n", ascii_string);
347 if ( config.startname ) {
348 rpcstr_pull( ascii_string, config.startname->buffer, sizeof(ascii_string), -1, STR_TERMINATE );
349 d_printf("\tStart Name = %s\n", ascii_string);
352 if ( config.displayname ) {
353 rpcstr_pull( ascii_string, config.displayname->buffer, sizeof(ascii_string), -1, STR_TERMINATE );
354 d_printf("\tDisplay Name = %s\n", ascii_string);
357 done:
358 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
359 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
361 return werror_to_ntstatus(result);
364 /********************************************************************
365 ********************************************************************/
367 static NTSTATUS rpc_service_stop_internal(struct net_context *c,
368 const DOM_SID *domain_sid,
369 const char *domain_name,
370 struct cli_state *cli,
371 struct rpc_pipe_client *pipe_hnd,
372 TALLOC_CTX *mem_ctx,
373 int argc,
374 const char **argv )
376 POLICY_HND hSCM;
377 WERROR result = WERR_GENERAL_FAILURE;
378 NTSTATUS status;
379 fstring servicename;
381 if (argc != 1 ) {
382 d_printf("Usage: net rpc service status <service>\n");
383 return NT_STATUS_OK;
386 fstrcpy( servicename, argv[0] );
388 /* Open the Service Control Manager */
389 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
390 pipe_hnd->srv_name_slash,
391 NULL,
392 SC_RIGHT_MGR_ENUMERATE_SERVICE,
393 &hSCM,
394 &result);
395 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
396 d_fprintf(stderr, "Failed to open Service Control Manager. [%s]\n", dos_errstr(result));
397 return werror_to_ntstatus(result);
400 result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
401 SVCCTL_CONTROL_STOP, SVCCTL_STOPPED );
403 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
405 return werror_to_ntstatus(result);
408 /********************************************************************
409 ********************************************************************/
411 static NTSTATUS rpc_service_pause_internal(struct net_context *c,
412 const DOM_SID *domain_sid,
413 const char *domain_name,
414 struct cli_state *cli,
415 struct rpc_pipe_client *pipe_hnd,
416 TALLOC_CTX *mem_ctx,
417 int argc,
418 const char **argv )
420 POLICY_HND hSCM;
421 WERROR result = WERR_GENERAL_FAILURE;
422 NTSTATUS status;
423 fstring servicename;
425 if (argc != 1 ) {
426 d_printf("Usage: net rpc service status <service>\n");
427 return NT_STATUS_OK;
430 fstrcpy( servicename, argv[0] );
432 /* Open the Service Control Manager */
433 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
434 pipe_hnd->srv_name_slash,
435 NULL,
436 SC_RIGHT_MGR_ENUMERATE_SERVICE,
437 &hSCM,
438 &result);
439 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
440 d_fprintf(stderr, "Failed to open Service Control Manager. [%s]\n", dos_errstr(result));
441 return werror_to_ntstatus(result);
444 result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
445 SVCCTL_CONTROL_PAUSE, SVCCTL_PAUSED );
447 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
449 return werror_to_ntstatus(result);
452 /********************************************************************
453 ********************************************************************/
455 static NTSTATUS rpc_service_resume_internal(struct net_context *c,
456 const DOM_SID *domain_sid,
457 const char *domain_name,
458 struct cli_state *cli,
459 struct rpc_pipe_client *pipe_hnd,
460 TALLOC_CTX *mem_ctx,
461 int argc,
462 const char **argv )
464 POLICY_HND hSCM;
465 WERROR result = WERR_GENERAL_FAILURE;
466 NTSTATUS status;
467 fstring servicename;
469 if (argc != 1 ) {
470 d_printf("Usage: net rpc service status <service>\n");
471 return NT_STATUS_OK;
474 fstrcpy( servicename, argv[0] );
476 /* Open the Service Control Manager */
477 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
478 pipe_hnd->srv_name_slash,
479 NULL,
480 SC_RIGHT_MGR_ENUMERATE_SERVICE,
481 &hSCM,
482 &result);
483 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
484 d_fprintf(stderr, "Failed to open Service Control Manager. [%s]\n", dos_errstr(result));
485 return werror_to_ntstatus(result);
488 result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
489 SVCCTL_CONTROL_CONTINUE, SVCCTL_RUNNING );
491 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
493 return werror_to_ntstatus(result);
496 /********************************************************************
497 ********************************************************************/
499 static NTSTATUS rpc_service_start_internal(struct net_context *c,
500 const DOM_SID *domain_sid,
501 const char *domain_name,
502 struct cli_state *cli,
503 struct rpc_pipe_client *pipe_hnd,
504 TALLOC_CTX *mem_ctx,
505 int argc,
506 const char **argv )
508 POLICY_HND hSCM, hService;
509 WERROR result = WERR_GENERAL_FAILURE;
510 NTSTATUS status;
511 uint32 state = 0;
513 if (argc != 1 ) {
514 d_printf("Usage: net rpc service status <service>\n");
515 return NT_STATUS_OK;
518 /* Open the Service Control Manager */
519 status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
520 pipe_hnd->srv_name_slash,
521 NULL,
522 SC_RIGHT_MGR_ENUMERATE_SERVICE,
523 &hSCM,
524 &result);
525 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
526 d_fprintf(stderr, "Failed to open Service Control Manager. [%s]\n", dos_errstr(result));
527 return werror_to_ntstatus(result);
530 /* Open the Service */
532 status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
533 &hSCM,
534 argv[0],
535 SC_RIGHT_SVC_START,
536 &hService,
537 &result);
539 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
540 d_fprintf(stderr, "Failed to open service. [%s]\n", dos_errstr(result));
541 goto done;
544 /* get the status */
546 status = rpccli_svcctl_StartServiceW(pipe_hnd, mem_ctx,
547 &hService,
549 NULL,
550 &result);
552 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
553 d_fprintf(stderr, "Query status request failed. [%s]\n", dos_errstr(result));
554 goto done;
557 result = watch_service_state(pipe_hnd, mem_ctx, &hSCM, argv[0], SVCCTL_RUNNING, &state );
559 if ( W_ERROR_IS_OK(result) && (state == SVCCTL_RUNNING) )
560 d_printf("Successfully started service: %s\n", argv[0] );
561 else
562 d_fprintf(stderr, "Failed to start service: %s [%s]\n", argv[0], dos_errstr(result) );
564 done:
565 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
566 rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
568 return werror_to_ntstatus(result);
571 /********************************************************************
572 ********************************************************************/
574 static int rpc_service_list(struct net_context *c, int argc, const char **argv )
576 if (c->display_usage) {
577 d_printf("Usage:\n"
578 "net rpc service list\n"
579 " View configured Win32 services\n");
580 return 0;
583 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
584 rpc_service_list_internal, argc, argv );
587 /********************************************************************
588 ********************************************************************/
590 static int rpc_service_start(struct net_context *c, int argc, const char **argv )
592 if (c->display_usage) {
593 d_printf("Usage:\n"
594 "net rpc service start <service>\n"
595 " Start a Win32 service\n");
596 return 0;
599 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
600 rpc_service_start_internal, argc, argv );
603 /********************************************************************
604 ********************************************************************/
606 static int rpc_service_stop(struct net_context *c, int argc, const char **argv )
608 if (c->display_usage) {
609 d_printf("Usage:\n"
610 "net rpc service stop <service>\n"
611 " Stop a Win32 service\n");
612 return 0;
615 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
616 rpc_service_stop_internal, argc, argv );
619 /********************************************************************
620 ********************************************************************/
622 static int rpc_service_resume(struct net_context *c, int argc, const char **argv )
624 if (c->display_usage) {
625 d_printf("Usage:\n"
626 "net rpc service resume <service>\n"
627 " Resume a Win32 service\n");
628 return 0;
631 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
632 rpc_service_resume_internal, argc, argv );
635 /********************************************************************
636 ********************************************************************/
638 static int rpc_service_pause(struct net_context *c, int argc, const char **argv )
640 if (c->display_usage) {
641 d_printf("Usage:\n"
642 "net rpc service pause <service>\n"
643 " Pause a Win32 service\n");
644 return 0;
647 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
648 rpc_service_pause_internal, argc, argv );
651 /********************************************************************
652 ********************************************************************/
654 static int rpc_service_status(struct net_context *c, int argc, const char **argv )
656 if (c->display_usage) {
657 d_printf("Usage:\n"
658 "net rpc service status <service>\n"
659 " Show the current status of a service\n");
660 return 0;
663 return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
664 rpc_service_status_internal, argc, argv );
667 /********************************************************************
668 ********************************************************************/
670 int net_rpc_service(struct net_context *c, int argc, const char **argv)
672 struct functable func[] = {
674 "list",
675 rpc_service_list,
676 NET_TRANSPORT_RPC,
677 "View configured Win32 services",
678 "net rpc service list\n"
679 " View configured Win32 services"
682 "start",
683 rpc_service_start,
684 NET_TRANSPORT_RPC,
685 "Start a service",
686 "net rpc service start\n"
687 " Start a service"
690 "stop",
691 rpc_service_stop,
692 NET_TRANSPORT_RPC,
693 "Stop a service",
694 "net rpc service stop\n"
695 " Stop a service"
698 "pause",
699 rpc_service_pause,
700 NET_TRANSPORT_RPC,
701 "Pause a service",
702 "net rpc service pause\n"
703 " Pause a service"
706 "resume",
707 rpc_service_resume,
708 NET_TRANSPORT_RPC,
709 "Resume a paused service",
710 "net rpc service resume\n"
711 " Resume a service"
714 "status",
715 rpc_service_status,
716 NET_TRANSPORT_RPC,
717 "View current status of a service",
718 "net rpc service status\n"
719 " View current status of a service"
721 {NULL, NULL, 0, NULL, NULL}
724 return net_run_function(c, argc, argv, "net rpc service",func);