s3-ntlm_auth: use manage_gensec_request for squid-2.5-ntlmssp
[kai/samba.git] / source3 / utils / net_rpc_service.c
index 9ab82b5adf33dfaecbe719ac68a57134447f57fd..523eafd6533a37381e708c93352f16619e552c0c 100644 (file)
@@ -18,7 +18,9 @@
 
 #include "includes.h"
 #include "utils/net.h"
-#include "../librpc/gen_ndr/cli_svcctl.h"
+#include "rpc_client/rpc_client.h"
+#include "../librpc/gen_ndr/ndr_svcctl.h"
+#include "../librpc/gen_ndr/ndr_svcctl_c.h"
 
 struct svc_state_msg {
        uint32 flag;
@@ -59,6 +61,75 @@ const char *svc_status_string( 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)
+{
+       NTSTATUS status;
+       WERROR result;
+
+       status = dcerpc_svcctl_OpenServiceW(b, mem_ctx,
+                                           hSCM,
+                                           service,
+                                           access_mask,
+                                           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));
+               return result;
+       }
+       if (!W_ERROR_IS_OK(result) ) {
+               d_fprintf(stderr, _("Failed to open service.  [%s]\n"),
+                         win_errstr(result));
+               return result;
+       }
+
+       return WERR_OK;
+}
+
+/********************************************************************
+********************************************************************/
+
+static WERROR open_scm(struct dcerpc_binding_handle *b,
+                      TALLOC_CTX *mem_ctx,
+                      const char *server_name,
+                      uint32_t access_mask,
+                      struct policy_handle *hSCM)
+{
+       NTSTATUS status;
+       WERROR result;
+
+       status = dcerpc_svcctl_OpenSCManagerW(b, mem_ctx,
+                                             server_name,
+                                             NULL,
+                                             access_mask,
+                                             hSCM,
+                                             &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               result = ntstatus_to_werror(status);
+               d_fprintf(stderr,
+                         _("Failed to open Service Control Manager. [%s]\n"),
+                         nt_errstr(status));
+               return result;
+       }
+       if (!W_ERROR_IS_OK(result)) {
+               d_fprintf(stderr,
+                         _("Failed to open Service Control Manager. [%s]\n"),
+                         win_errstr(result));
+               return result;
+       }
+
+       return WERR_OK;
+}
+
+/********************************************************************
+********************************************************************/
+
 static WERROR query_service_state(struct rpc_pipe_client *pipe_hnd,
                                TALLOC_CTX *mem_ctx,
                                struct policy_handle *hSCM,
@@ -69,31 +140,36 @@ static WERROR query_service_state(struct rpc_pipe_client *pipe_hnd,
        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' */
 
-       status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
-                                           hSCM,
-                                           service,
-                                           SC_RIGHT_SVC_QUERY_STATUS,
-                                           &hService,
-                                           &result);
-       if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
-               d_fprintf(stderr, _("Failed to open service.  [%s]\n"),
-                         win_errstr(result));
+       result = open_service(b, mem_ctx, hSCM, service,
+                             SC_RIGHT_SVC_QUERY_STATUS,
+                             &hService);
+       if (!W_ERROR_IS_OK(result) ) {
                return result;
        }
 
-       status = rpccli_svcctl_QueryServiceStatus(pipe_hnd, mem_ctx,
+       status = dcerpc_svcctl_QueryServiceStatus(b, mem_ctx,
                                                  &hService,
                                                  &service_status,
                                                  &result);
-
-       if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
-               *state = service_status.state;
+       if (!NT_STATUS_IS_OK(status)) {
+               result = ntstatus_to_werror(status);
+               goto done;
+       }
+       if (!W_ERROR_IS_OK(result)) {
+               goto done;
        }
 
-       rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
+       *state = service_status.state;
+
+ done:
+       if (is_valid_policy_hnd(&hService)) {
+               WERROR _result;
+               dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
+       }
 
        return result;
 }
@@ -124,7 +200,7 @@ static WERROR watch_service_state(struct rpc_pipe_client *pipe_hnd,
 
                d_printf(".");
                i++;
-               sys_usleep( 100 );
+               usleep( 100 );
        }
        d_printf("\n");
 
@@ -148,31 +224,32 @@ static WERROR control_service(struct rpc_pipe_client *pipe_hnd,
        NTSTATUS status;
        struct SERVICE_STATUS service_status;
        uint32 state = 0;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        /* Open the Service */
 
-       status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
-                                           hSCM,
-                                           service,
-                                           (SC_RIGHT_SVC_STOP|SC_RIGHT_SVC_PAUSE_CONTINUE),
-                                           &hService,
-                                           &result);
-
-       if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
-               d_fprintf(stderr, _("Failed to open service.  [%s]\n"),
-                         win_errstr(result));
-               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) ) {
+               return result;
        }
 
        /* get the status */
 
-       status = rpccli_svcctl_ControlService(pipe_hnd, mem_ctx,
+       status = dcerpc_svcctl_ControlService(b, mem_ctx,
                                              &hService,
                                              control,
                                              &service_status,
                                              &result);
 
-       if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
+       if (!NT_STATUS_IS_OK(status)) {
+               result = ntstatus_to_werror(status);
+               d_fprintf(stderr, _("Control service request failed.  [%s]\n"),
+                         nt_errstr(status));
+               goto done;
+       }
+       if (!W_ERROR_IS_OK(result) ) {
                d_fprintf(stderr, _("Control service request failed.  [%s]\n"),
                          win_errstr(result));
                goto done;
@@ -185,7 +262,10 @@ static WERROR control_service(struct rpc_pipe_client *pipe_hnd,
        d_printf(_("%s service is %s.\n"), service, svc_status_string(state));
 
 done:
-       rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
+       if (is_valid_policy_hnd(&hService)) {
+               WERROR _result;
+               dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
+       }
 
        return result;
 }
@@ -207,6 +287,7 @@ static NTSTATUS rpc_service_list_internal(struct net_context *c,
        WERROR result = WERR_GENERAL_FAILURE;
        NTSTATUS status;
        int i;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        uint8_t *buffer = NULL;
        uint32_t buf_size = 0;
@@ -219,21 +300,15 @@ static NTSTATUS rpc_service_list_internal(struct net_context *c,
                return NT_STATUS_OK;
        }
 
-       status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
-                                             pipe_hnd->srv_name_slash,
-                                             NULL,
-                                             SC_RIGHT_MGR_ENUMERATE_SERVICE,
-                                             &hSCM,
-                                             &result);
-       if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
-               d_fprintf(stderr,
-                         _("Failed to open Service Control Manager. [%s]\n"),
-                         win_errstr(result));
+       result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
+                         SC_RIGHT_MGR_ENUMERATE_SERVICE,
+                         &hSCM);
+       if (!W_ERROR_IS_OK(result)) {
                return werror_to_ntstatus(result);
        }
 
        do {
-               status = rpccli_svcctl_EnumServicesStatusW(pipe_hnd, mem_ctx,
+               status = dcerpc_svcctl_EnumServicesStatusW(b, mem_ctx,
                                                           &hSCM,
                                                           SERVICE_TYPE_WIN32,
                                                           SERVICE_STATE_ALL,
@@ -244,10 +319,10 @@ static NTSTATUS rpc_service_list_internal(struct net_context *c,
                                                           &resume_handle,
                                                           &result);
 
-               if (NT_STATUS_IS_ERR(status)) {
+               if (!NT_STATUS_IS_OK(status)) {
                        d_fprintf(stderr,
                                _("Failed to enumerate services.  [%s]\n"),
-                               win_errstr(result));
+                               nt_errstr(status));
                        break;
                }
 
@@ -257,6 +332,14 @@ static NTSTATUS rpc_service_list_internal(struct net_context *c,
                        continue;
                }
 
+               if (!W_ERROR_IS_OK(result)) {
+                       status = werror_to_ntstatus(result);
+                       d_fprintf(stderr,
+                               _("Failed to enumerate services.  [%s]\n"),
+                               win_errstr(result));
+                       break;
+               }
+
                if ( num_services == 0 ) {
                        d_printf(_("No services returned\n"));
                        break;
@@ -298,7 +381,10 @@ static NTSTATUS rpc_service_list_internal(struct net_context *c,
 
        } while (W_ERROR_EQUAL(result, WERR_MORE_DATA));
 
-       rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
+       if (is_valid_policy_hnd(&hSCM)) {
+               WERROR _result;
+               dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
+       }
 
        return status;
 }
@@ -322,6 +408,7 @@ static NTSTATUS rpc_service_status_internal(struct net_context *c,
        struct QUERY_SERVICE_CONFIG config;
        uint32_t buf_size = sizeof(config);
        uint32_t ret_size = 0;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        if (argc != 1 ) {
                d_printf("%s net rpc service status <service>\n", _("Usage:"));
@@ -329,42 +416,36 @@ static NTSTATUS rpc_service_status_internal(struct net_context *c,
        }
 
        /* Open the Service Control Manager */
-       status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
-                                             pipe_hnd->srv_name_slash,
-                                             NULL,
-                                             SC_RIGHT_MGR_ENUMERATE_SERVICE,
-                                             &hSCM,
-                                             &result);
-       if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
-               d_fprintf(stderr,
-                         _("Failed to open Service Control Manager. [%s]\n"),
-                         win_errstr(result));
+       result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
+                         SC_RIGHT_MGR_ENUMERATE_SERVICE,
+                         &hSCM);
+       if (!W_ERROR_IS_OK(result)) {
                return werror_to_ntstatus(result);
        }
 
        /* Open the Service */
 
-       status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
-                                           &hSCM,
-                                           argv[0],
-                                           (SC_RIGHT_SVC_QUERY_STATUS|SC_RIGHT_SVC_QUERY_CONFIG),
-                                           &hService,
-                                           &result);
-
-       if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
-               d_fprintf(stderr, _("Failed to open service.  [%s]\n"),
-                         win_errstr(result));
+       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) ) {
                goto done;
        }
 
        /* get the status */
 
-       status = rpccli_svcctl_QueryServiceStatus(pipe_hnd, mem_ctx,
+       status = dcerpc_svcctl_QueryServiceStatus(b, mem_ctx,
                                                  &hService,
                                                  &service_status,
                                                  &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               result = ntstatus_to_werror(status);
+               d_fprintf(stderr, _("Query status request failed.  [%s]\n"),
+                         nt_errstr(status));
+               goto done;
+       }
 
-       if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
+       if (!W_ERROR_IS_OK(result) ) {
                d_fprintf(stderr, _("Query status request failed.  [%s]\n"),
                          win_errstr(result));
                goto done;
@@ -375,23 +456,36 @@ static NTSTATUS rpc_service_status_internal(struct net_context *c,
 
        /* get the config */
 
-       status = rpccli_svcctl_QueryServiceConfigW(pipe_hnd, mem_ctx,
+       status = dcerpc_svcctl_QueryServiceConfigW(b, mem_ctx,
                                                   &hService,
                                                   &config,
                                                   buf_size,
                                                   &ret_size,
                                                   &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               result = ntstatus_to_werror(status);
+               d_fprintf(stderr, _("Query config request failed.  [%s]\n"),
+                         nt_errstr(status));
+               goto done;
+       }
+
        if (W_ERROR_EQUAL(result, WERR_INSUFFICIENT_BUFFER)) {
                buf_size = ret_size;
-               status = rpccli_svcctl_QueryServiceConfigW(pipe_hnd, mem_ctx,
+               status = dcerpc_svcctl_QueryServiceConfigW(b, mem_ctx,
                                                           &hService,
                                                           &config,
                                                           buf_size,
                                                           &ret_size,
                                                           &result);
+               if (!NT_STATUS_IS_OK(status)) {
+                       result = ntstatus_to_werror(status);
+                       d_fprintf(stderr, _("Query config request failed.  [%s]\n"),
+                                 nt_errstr(status));
+                       goto done;
+               }
        }
 
-       if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
+       if (!W_ERROR_IS_OK(result) ) {
                d_fprintf(stderr, _("Query config request failed.  [%s]\n"),
                          win_errstr(result));
                goto done;
@@ -432,8 +526,14 @@ static NTSTATUS rpc_service_status_internal(struct net_context *c,
        }
 
 done:
-        rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
-       rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
+       if (is_valid_policy_hnd(&hService)) {
+               WERROR _result;
+               dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
+       }
+       if (is_valid_policy_hnd(&hSCM)) {
+               WERROR _result;
+               dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
+       }
 
        return werror_to_ntstatus(result);
 }
@@ -452,34 +552,28 @@ static NTSTATUS rpc_service_stop_internal(struct net_context *c,
 {
        struct policy_handle hSCM;
        WERROR result = WERR_GENERAL_FAILURE;
-       NTSTATUS status;
-       fstring servicename;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        if (argc != 1 ) {
                d_printf("%s net rpc service status <service>\n", _("Usage:"));
                return NT_STATUS_OK;
        }
 
-       fstrcpy( servicename, argv[0] );
-
        /* Open the Service Control Manager */
-       status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
-                                             pipe_hnd->srv_name_slash,
-                                             NULL,
-                                             SC_RIGHT_MGR_ENUMERATE_SERVICE,
-                                             &hSCM,
-                                             &result);
-       if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
-               d_fprintf(stderr,
-                         _("Failed to open Service Control Manager.  [%s]\n"),
-                         win_errstr(result));
+       result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
+                         SC_RIGHT_MGR_ENUMERATE_SERVICE,
+                         &hSCM);
+       if (!W_ERROR_IS_OK(result)) {
                return werror_to_ntstatus(result);
        }
 
-       result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
+       result = control_service(pipe_hnd, mem_ctx, &hSCM, argv[0],
                SVCCTL_CONTROL_STOP, SVCCTL_STOPPED );
 
-       rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
+       if (is_valid_policy_hnd(&hSCM)) {
+               WERROR _result;
+               dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
+       }
 
        return werror_to_ntstatus(result);
 }
@@ -498,34 +592,28 @@ static NTSTATUS rpc_service_pause_internal(struct net_context *c,
 {
        struct policy_handle hSCM;
        WERROR result = WERR_GENERAL_FAILURE;
-       NTSTATUS status;
-       fstring servicename;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        if (argc != 1 ) {
                d_printf("%s net rpc service status <service>\n", _("Usage:"));
                return NT_STATUS_OK;
        }
 
-       fstrcpy( servicename, argv[0] );
-
        /* Open the Service Control Manager */
-       status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
-                                             pipe_hnd->srv_name_slash,
-                                             NULL,
-                                             SC_RIGHT_MGR_ENUMERATE_SERVICE,
-                                             &hSCM,
-                                             &result);
-       if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
-               d_fprintf(stderr,
-                         _("Failed to open Service Control Manager.  [%s]\n"),
-                         win_errstr(result));
+       result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
+                         SC_RIGHT_MGR_ENUMERATE_SERVICE,
+                         &hSCM);
+       if (!W_ERROR_IS_OK(result)) {
                return werror_to_ntstatus(result);
        }
 
-       result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
+       result = control_service(pipe_hnd, mem_ctx, &hSCM, argv[0],
                SVCCTL_CONTROL_PAUSE, SVCCTL_PAUSED );
 
-       rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
+       if (is_valid_policy_hnd(&hSCM)) {
+               WERROR _result;
+               dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
+       }
 
        return werror_to_ntstatus(result);
 }
@@ -544,34 +632,28 @@ static NTSTATUS rpc_service_resume_internal(struct net_context *c,
 {
        struct policy_handle hSCM;
        WERROR result = WERR_GENERAL_FAILURE;
-       NTSTATUS status;
-       fstring servicename;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        if (argc != 1 ) {
                d_printf("%s net rpc service status <service>\n", _("Usage:"));
                return NT_STATUS_OK;
        }
 
-       fstrcpy( servicename, argv[0] );
-
        /* Open the Service Control Manager */
-       status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
-                                             pipe_hnd->srv_name_slash,
-                                             NULL,
-                                             SC_RIGHT_MGR_ENUMERATE_SERVICE,
-                                             &hSCM,
-                                             &result);
-       if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
-               d_fprintf(stderr,
-                         _("Failed to open Service Control Manager.  [%s]\n"),
-                         win_errstr(result));
+       result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
+                         SC_RIGHT_MGR_ENUMERATE_SERVICE,
+                         &hSCM);
+       if (!W_ERROR_IS_OK(result)) {
                return werror_to_ntstatus(result);
        }
 
-       result = control_service(pipe_hnd, mem_ctx, &hSCM, servicename,
+       result = control_service(pipe_hnd, mem_ctx, &hSCM, argv[0],
                SVCCTL_CONTROL_CONTINUE, SVCCTL_RUNNING );
 
-       rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
+       if (is_valid_policy_hnd(&hSCM)) {
+               WERROR _result;
+               dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
+       }
 
        return werror_to_ntstatus(result);
 }
@@ -592,6 +674,7 @@ static NTSTATUS rpc_service_start_internal(struct net_context *c,
        WERROR result = WERR_GENERAL_FAILURE;
        NTSTATUS status;
        uint32 state = 0;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        if (argc != 1 ) {
                d_printf("%s net rpc service status <service>\n", _("Usage:"));
@@ -599,43 +682,38 @@ static NTSTATUS rpc_service_start_internal(struct net_context *c,
        }
 
        /* Open the Service Control Manager */
-       status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
-                                             pipe_hnd->srv_name_slash,
-                                             NULL,
-                                             SC_RIGHT_MGR_ENUMERATE_SERVICE,
-                                             &hSCM,
-                                             &result);
-       if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
-               d_fprintf(stderr,
-                         _("Failed to open Service Control Manager.  [%s]\n"),
-                         win_errstr(result));
+       result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
+                         SC_RIGHT_MGR_ENUMERATE_SERVICE,
+                         &hSCM);
+       if (!W_ERROR_IS_OK(result)) {
                return werror_to_ntstatus(result);
        }
 
-       /* Open the Service */
 
-       status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
-                                           &hSCM,
-                                           argv[0],
-                                           SC_RIGHT_SVC_START,
-                                           &hService,
-                                           &result);
+       /* Open the Service */
 
-       if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
-               d_fprintf(stderr, _("Failed to open service.  [%s]\n"),
-                         win_errstr(result));
+       result = open_service(b, mem_ctx, &hSCM, argv[0],
+                             SC_RIGHT_SVC_START,
+                             &hService);
+       if (!W_ERROR_IS_OK(result) ) {
                goto done;
        }
 
        /* get the status */
 
-       status = rpccli_svcctl_StartServiceW(pipe_hnd, mem_ctx,
+       status = dcerpc_svcctl_StartServiceW(b, mem_ctx,
                                             &hService,
                                             0,
                                             NULL,
                                             &result);
 
-       if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
+       if (!NT_STATUS_IS_OK(status)) {
+               result = ntstatus_to_werror(status);
+               d_fprintf(stderr, _("Query status request failed.  [%s]\n"),
+                         nt_errstr(status));
+               goto done;
+       }
+       if (!W_ERROR_IS_OK(result) ) {
                d_fprintf(stderr, _("Query status request failed.  [%s]\n"),
                          win_errstr(result));
                goto done;
@@ -651,8 +729,14 @@ static NTSTATUS rpc_service_start_internal(struct net_context *c,
                          argv[0], win_errstr(result) );
 
 done:
-       rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
-       rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
+       if (is_valid_policy_hnd(&hService)) {
+               WERROR _result;
+               dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
+       }
+       if (is_valid_policy_hnd(&hSCM)) {
+               WERROR _result;
+               dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
+       }
 
        return werror_to_ntstatus(result);
 }
@@ -672,48 +756,46 @@ static NTSTATUS rpc_service_delete_internal(struct net_context *c,
        struct policy_handle hSCM, hService;
        WERROR result = WERR_GENERAL_FAILURE;
        NTSTATUS status;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        if (argc != 1 ) {
                d_printf("%s net rpc service delete <service>\n", _("Usage:"));
                return NT_STATUS_OK;
        }
 
+       ZERO_STRUCT(hSCM);
+       ZERO_STRUCT(hService);
+
        /* Open the Service Control Manager */
-       status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
-                                             pipe_hnd->srv_name_slash,
-                                             NULL,
-                                             SC_RIGHT_MGR_ENUMERATE_SERVICE,
-                                             &hSCM,
-                                             &result);
-       if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
-               d_fprintf(stderr,
-                       _("Failed to open Service Control Manager.  [%s]\n"),
-                       win_errstr(result));
+       result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
+                         SC_RIGHT_MGR_ENUMERATE_SERVICE,
+                         &hSCM);
+       if (!W_ERROR_IS_OK(result)) {
                return werror_to_ntstatus(result);
        }
 
        /* Open the Service */
 
-       status = rpccli_svcctl_OpenServiceW(pipe_hnd, mem_ctx,
-                                           &hSCM,
-                                           argv[0],
-                                           SERVICE_ALL_ACCESS,
-                                           &hService,
-                                           &result);
-
-       if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
-               d_fprintf(stderr, _("Failed to open service.  [%s]\n"),
-                       win_errstr(result));
+       result = open_service(b, mem_ctx, &hSCM, argv[0],
+                             SERVICE_ALL_ACCESS,
+                             &hService);
+       if (!W_ERROR_IS_OK(result) ) {
                goto done;
        }
 
        /* Delete the Service */
 
-       status = rpccli_svcctl_DeleteService(pipe_hnd, mem_ctx,
+       status = dcerpc_svcctl_DeleteService(b, mem_ctx,
                                             &hService,
                                             &result);
 
-       if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
+       if (!NT_STATUS_IS_OK(status)) {
+               result = ntstatus_to_werror(status);
+               d_fprintf(stderr, _("Delete service request failed.  [%s]\n"),
+                       nt_errstr(status));
+               goto done;
+       }
+       if (!W_ERROR_IS_OK(result)) {
                d_fprintf(stderr, _("Delete service request failed.  [%s]\n"),
                        win_errstr(result));
                goto done;
@@ -723,10 +805,12 @@ static NTSTATUS rpc_service_delete_internal(struct net_context *c,
 
  done:
        if (is_valid_policy_hnd(&hService)) {
-               rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
+               WERROR _result;
+               dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
        }
        if (is_valid_policy_hnd(&hSCM)) {
-               rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
+               WERROR _result;
+               dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
        }
 
        return werror_to_ntstatus(result);
@@ -750,6 +834,7 @@ static NTSTATUS rpc_service_create_internal(struct net_context *c,
        const char *ServiceName;
        const char *DisplayName;
        const char *binary_path;
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
 
        if (argc != 3) {
                d_printf("%s net rpc service create <service> "
@@ -757,17 +842,14 @@ static NTSTATUS rpc_service_create_internal(struct net_context *c,
                return NT_STATUS_OK;
        }
 
+       ZERO_STRUCT(hSCM);
+       ZERO_STRUCT(hService);
+
        /* Open the Service Control Manager */
-       status = rpccli_svcctl_OpenSCManagerW(pipe_hnd, mem_ctx,
-                                             pipe_hnd->srv_name_slash,
-                                             NULL,
-                                             SC_RIGHT_MGR_CREATE_SERVICE,
-                                             &hSCM,
-                                             &result);
-       if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
-               d_fprintf(stderr,
-                       _("Failed to open Service Control Manager.  [%s]\n"),
-                       win_errstr(result));
+       result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
+                         SC_RIGHT_MGR_CREATE_SERVICE,
+                         &hSCM);
+       if (!W_ERROR_IS_OK(result)) {
                return werror_to_ntstatus(result);
        }
 
@@ -777,7 +859,7 @@ static NTSTATUS rpc_service_create_internal(struct net_context *c,
        DisplayName = argv[1];
        binary_path = argv[2];
 
-       status = rpccli_svcctl_CreateServiceW(pipe_hnd, mem_ctx,
+       status = dcerpc_svcctl_CreateServiceW(b, mem_ctx,
                                              &hSCM,
                                              ServiceName,
                                              DisplayName,
@@ -795,10 +877,15 @@ static NTSTATUS rpc_service_create_internal(struct net_context *c,
                                              0, /* password_size */
                                              &hService,
                                              &result);
-
-       if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result) ) {
+       if (!NT_STATUS_IS_OK(status)) {
+               result = ntstatus_to_werror(status);
                d_fprintf(stderr, _("Create service request failed.  [%s]\n"),
-                       win_errstr(result));
+                         nt_errstr(status));
+               goto done;
+       }
+       if (!W_ERROR_IS_OK(result)) {
+               d_fprintf(stderr, _("Create service request failed.  [%s]\n"),
+                         win_errstr(result));
                goto done;
        }
 
@@ -806,10 +893,12 @@ static NTSTATUS rpc_service_create_internal(struct net_context *c,
 
  done:
        if (is_valid_policy_hnd(&hService)) {
-               rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hService, NULL);
+               WERROR _result;
+               dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
        }
        if (is_valid_policy_hnd(&hSCM)) {
-               rpccli_svcctl_CloseServiceHandle(pipe_hnd, mem_ctx, &hSCM, NULL);
+               WERROR _result;
+               dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
        }
 
        return werror_to_ntstatus(result);
@@ -829,7 +918,7 @@ static int rpc_service_list(struct net_context *c, int argc, const char **argv )
                return 0;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
                rpc_service_list_internal, argc, argv );
 }
 
@@ -847,7 +936,7 @@ static int rpc_service_start(struct net_context *c, int argc, const char **argv
                return 0;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
                rpc_service_start_internal, argc, argv );
 }
 
@@ -865,7 +954,7 @@ static int rpc_service_stop(struct net_context *c, int argc, const char **argv )
                return 0;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
                rpc_service_stop_internal, argc, argv );
 }
 
@@ -883,7 +972,7 @@ static int rpc_service_resume(struct net_context *c, int argc, const char **argv
                return 0;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
                rpc_service_resume_internal, argc, argv );
 }
 
@@ -901,7 +990,7 @@ static int rpc_service_pause(struct net_context *c, int argc, const char **argv
                return 0;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
                rpc_service_pause_internal, argc, argv );
 }
 
@@ -919,7 +1008,7 @@ static int rpc_service_status(struct net_context *c, int argc, const char **argv
                return 0;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
                rpc_service_status_internal, argc, argv );
 }
 
@@ -937,7 +1026,7 @@ static int rpc_service_delete(struct net_context *c, int argc, const char **argv
                return 0;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
                rpc_service_delete_internal, argc, argv);
 }
 
@@ -955,7 +1044,7 @@ static int rpc_service_create(struct net_context *c, int argc, const char **argv
                return 0;
        }
 
-       return run_rpc_command(c, NULL, &ndr_table_svcctl.syntax_id, 0,
+       return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
                rpc_service_create_internal, argc, argv);
 }