s3-net: restructure "net rpc service" and add open_service().
authorGünther Deschner <gd@samba.org>
Wed, 12 Jan 2011 18:00:16 +0000 (19:00 +0100)
committerAndreas Schneider <asn@samba.org>
Thu, 13 Jan 2011 11:17:30 +0000 (12:17 +0100)
Guenther

Signed-off-by: Andreas Schneider <asn@samba.org>
source3/utils/net_rpc_service.c

index 8968c46b70da7353078a6b8b46a5d053bf7fdf59..22394ca3a06d4cd6b460c17635b5783b73cc006f 100644 (file)
@@ -60,25 +60,21 @@ const char *svc_status_string( uint32 state )
 /********************************************************************
 ********************************************************************/
 
-static WERROR query_service_state(struct rpc_pipe_client *pipe_hnd,
-                               TALLOC_CTX *mem_ctx,
-                               struct policy_handle *hSCM,
-                               const char *service,
-                               uint32 *state )
+static WERROR open_service(struct dcerpc_binding_handle *b,
+                          TALLOC_CTX *mem_ctx,
+                          struct policy_handle *hSCM,
+                          const char *service,
+                          uint32_t access_mask,
+                          struct policy_handle *hService)
 {
-       struct policy_handle hService;
-       struct SERVICE_STATUS service_status;
-       WERROR result = WERR_GENERAL_FAILURE;
        NTSTATUS status;
-       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
-
-       /* now cycle until the status is actually 'watch_state' */
+       WERROR result;
 
        status = dcerpc_svcctl_OpenServiceW(b, mem_ctx,
                                            hSCM,
                                            service,
-                                           SC_RIGHT_SVC_QUERY_STATUS,
-                                           &hService,
+                                           access_mask,
+                                           hService,
                                            &result);
        if (!NT_STATUS_IS_OK(status)) {
                result = ntstatus_to_werror(status);
@@ -92,6 +88,33 @@ static WERROR query_service_state(struct rpc_pipe_client *pipe_hnd,
                return result;
        }
 
+       return WERR_OK;
+}
+
+/********************************************************************
+********************************************************************/
+
+static WERROR query_service_state(struct rpc_pipe_client *pipe_hnd,
+                               TALLOC_CTX *mem_ctx,
+                               struct policy_handle *hSCM,
+                               const char *service,
+                               uint32 *state )
+{
+       struct policy_handle hService;
+       struct SERVICE_STATUS service_status;
+       WERROR result = WERR_GENERAL_FAILURE;
+       NTSTATUS status;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
+
+       /* now cycle until the status is actually 'watch_state' */
+
+       result = open_service(b, mem_ctx, hSCM, service,
+                             SC_RIGHT_SVC_QUERY_STATUS,
+                             &hService);
+       if (!W_ERROR_IS_OK(result) ) {
+               return result;
+       }
+
        status = dcerpc_svcctl_QueryServiceStatus(b, mem_ctx,
                                                  &hService,
                                                  &service_status,
@@ -169,23 +192,11 @@ static WERROR control_service(struct rpc_pipe_client *pipe_hnd,
 
        /* Open the Service */
 
-       status = dcerpc_svcctl_OpenServiceW(b, mem_ctx,
-                                           hSCM,
-                                           service,
-                                           (SC_RIGHT_SVC_STOP|SC_RIGHT_SVC_PAUSE_CONTINUE),
-                                           &hService,
-                                           &result);
-       if (!NT_STATUS_IS_OK(status)) {
-               result = ntstatus_to_werror(status);
-               d_fprintf(stderr, _("Failed to open service.  [%s]\n"),
-                         nt_errstr(status));
-               goto done;
-       }
-
+       result = open_service(b, mem_ctx, hSCM, service,
+                             (SC_RIGHT_SVC_STOP|SC_RIGHT_SVC_PAUSE_CONTINUE),
+                             &hService);
        if (!W_ERROR_IS_OK(result) ) {
-               d_fprintf(stderr, _("Failed to open service.  [%s]\n"),
-                         win_errstr(result));
-               goto done;
+               return result;
        }
 
        /* get the status */
@@ -402,22 +413,10 @@ static NTSTATUS rpc_service_status_internal(struct net_context *c,
 
        /* Open the Service */
 
-       status = dcerpc_svcctl_OpenServiceW(b, mem_ctx,
-                                           &hSCM,
-                                           argv[0],
-                                           (SC_RIGHT_SVC_QUERY_STATUS|SC_RIGHT_SVC_QUERY_CONFIG),
-                                           &hService,
-                                           &result);
-
-       if (!NT_STATUS_IS_OK(status)) {
-               result = ntstatus_to_werror(status);
-               d_fprintf(stderr, _("Failed to open service.  [%s]\n"),
-                         nt_errstr(status));
-               goto done;
-       }
+       result = open_service(b, mem_ctx, &hSCM, argv[0],
+                             (SC_RIGHT_SVC_QUERY_STATUS|SC_RIGHT_SVC_QUERY_CONFIG),
+                             &hService);
        if (!W_ERROR_IS_OK(result) ) {
-               d_fprintf(stderr, _("Failed to open service.  [%s]\n"),
-                         win_errstr(result));
                goto done;
        }
 
@@ -741,22 +740,10 @@ static NTSTATUS rpc_service_start_internal(struct net_context *c,
 
        /* Open the Service */
 
-       status = dcerpc_svcctl_OpenServiceW(b, mem_ctx,
-                                           &hSCM,
-                                           argv[0],
-                                           SC_RIGHT_SVC_START,
-                                           &hService,
-                                           &result);
-       if (!NT_STATUS_IS_OK(status)) {
-               result = ntstatus_to_werror(status);
-               d_fprintf(stderr, _("Failed to open service.  [%s]\n"),
-                         nt_errstr(status));
-               goto done;
-       }
-
+       result = open_service(b, mem_ctx, &hSCM, argv[0],
+                             SC_RIGHT_SVC_START,
+                             &hService);
        if (!W_ERROR_IS_OK(result) ) {
-               d_fprintf(stderr, _("Failed to open service.  [%s]\n"),
-                         win_errstr(result));
                goto done;
        }
 
@@ -849,23 +836,10 @@ static NTSTATUS rpc_service_delete_internal(struct net_context *c,
 
        /* Open the Service */
 
-       status = dcerpc_svcctl_OpenServiceW(b, mem_ctx,
-                                           &hSCM,
-                                           argv[0],
-                                           SERVICE_ALL_ACCESS,
-                                           &hService,
-                                           &result);
-
-       if (!NT_STATUS_IS_OK(status)) {
-               result = ntstatus_to_werror(status);
-               d_fprintf(stderr, _("Failed to open service.  [%s]\n"),
-                         nt_errstr(status));
-               goto done;
-       }
-
+       result = open_service(b, mem_ctx, &hSCM, argv[0],
+                             SERVICE_ALL_ACCESS,
+                             &hService);
        if (!W_ERROR_IS_OK(result) ) {
-               d_fprintf(stderr, _("Failed to open service.  [%s]\n"),
-                         win_errstr(result));
                goto done;
        }