LCOV - code coverage report
Current view: top level - source3/utils - net_rpc_service.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 0 425 0.0 %
Date: 2024-04-21 15:09:00 Functions: 0 23 0.0 %

          Line data    Source code
       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 3 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, see <http://www.gnu.org/licenses/>.  */
      18             : 
      19             : #include "includes.h"
      20             : #include "utils/net.h"
      21             : #include "rpc_client/rpc_client.h"
      22             : #include "../librpc/gen_ndr/ndr_svcctl.h"
      23             : #include "../librpc/gen_ndr/ndr_svcctl_c.h"
      24             : #include "lib/util/string_wrappers.h"
      25             : 
      26             : struct svc_state_msg {
      27             :         uint32_t flag;
      28             :         const char *message;
      29             : };
      30             : 
      31             : static struct svc_state_msg state_msg_table[] = {
      32             :         { SVCCTL_STOPPED,            N_("stopped") },
      33             :         { SVCCTL_START_PENDING,      N_("start pending") },
      34             :         { SVCCTL_STOP_PENDING,       N_("stop pending") },
      35             :         { SVCCTL_RUNNING,            N_("running") },
      36             :         { SVCCTL_CONTINUE_PENDING,   N_("resume pending") },
      37             :         { SVCCTL_PAUSE_PENDING,      N_("pause pending") },
      38             :         { SVCCTL_PAUSED,             N_("paused") },
      39             :         { 0,                          NULL }
      40             : };
      41             : 
      42             : 
      43             : /********************************************************************
      44             : ********************************************************************/
      45           0 : const char *svc_status_string( uint32_t state )
      46             : {
      47           0 :         fstring msg;
      48           0 :         int i;
      49             : 
      50           0 :         fstr_sprintf( msg, _("Unknown State [%d]"), state );
      51             : 
      52           0 :         for ( i=0; state_msg_table[i].message; i++ ) {
      53           0 :                 if ( state_msg_table[i].flag == state ) {
      54           0 :                         fstrcpy( msg, state_msg_table[i].message );
      55           0 :                         break;
      56             :                 }
      57             :         }
      58             : 
      59           0 :         return talloc_strdup(talloc_tos(), msg);
      60             : }
      61             : 
      62             : /********************************************************************
      63             : ********************************************************************/
      64             : 
      65           0 : static WERROR open_service(struct dcerpc_binding_handle *b,
      66             :                            TALLOC_CTX *mem_ctx,
      67             :                            struct policy_handle *hSCM,
      68             :                            const char *service,
      69             :                            uint32_t access_mask,
      70             :                            struct policy_handle *hService)
      71             : {
      72           0 :         NTSTATUS status;
      73           0 :         WERROR result;
      74             : 
      75           0 :         status = dcerpc_svcctl_OpenServiceW(b, mem_ctx,
      76             :                                             hSCM,
      77             :                                             service,
      78             :                                             access_mask,
      79             :                                             hService,
      80             :                                             &result);
      81           0 :         if (!NT_STATUS_IS_OK(status)) {
      82           0 :                 result = ntstatus_to_werror(status);
      83           0 :                 d_fprintf(stderr, _("Failed to open service.  [%s]\n"),
      84             :                           nt_errstr(status));
      85           0 :                 return result;
      86             :         }
      87           0 :         if (!W_ERROR_IS_OK(result) ) {
      88           0 :                 d_fprintf(stderr, _("Failed to open service.  [%s]\n"),
      89             :                           win_errstr(result));
      90           0 :                 return result;
      91             :         }
      92             : 
      93           0 :         return WERR_OK;
      94             : }
      95             : 
      96             : /********************************************************************
      97             : ********************************************************************/
      98             : 
      99           0 : static WERROR open_scm(struct dcerpc_binding_handle *b,
     100             :                        TALLOC_CTX *mem_ctx,
     101             :                        const char *server_name,
     102             :                        uint32_t access_mask,
     103             :                        struct policy_handle *hSCM)
     104             : {
     105           0 :         NTSTATUS status;
     106           0 :         WERROR result;
     107             : 
     108           0 :         status = dcerpc_svcctl_OpenSCManagerW(b, mem_ctx,
     109             :                                               server_name,
     110             :                                               NULL,
     111             :                                               access_mask,
     112             :                                               hSCM,
     113             :                                               &result);
     114           0 :         if (!NT_STATUS_IS_OK(status)) {
     115           0 :                 result = ntstatus_to_werror(status);
     116           0 :                 d_fprintf(stderr,
     117           0 :                           _("Failed to open Service Control Manager. [%s]\n"),
     118             :                           nt_errstr(status));
     119           0 :                 return result;
     120             :         }
     121           0 :         if (!W_ERROR_IS_OK(result)) {
     122           0 :                 d_fprintf(stderr,
     123           0 :                           _("Failed to open Service Control Manager. [%s]\n"),
     124             :                           win_errstr(result));
     125           0 :                 return result;
     126             :         }
     127             : 
     128           0 :         return WERR_OK;
     129             : }
     130             : 
     131             : /********************************************************************
     132             : ********************************************************************/
     133             : 
     134           0 : static WERROR query_service_state(struct rpc_pipe_client *pipe_hnd,
     135             :                                 TALLOC_CTX *mem_ctx,
     136             :                                 struct policy_handle *hSCM,
     137             :                                 const char *service,
     138             :                                 uint32_t *state )
     139             : {
     140           0 :         struct policy_handle hService;
     141           0 :         struct SERVICE_STATUS service_status;
     142           0 :         WERROR result = WERR_GEN_FAILURE;
     143           0 :         NTSTATUS status;
     144           0 :         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
     145             : 
     146             :         /* now cycle until the status is actually 'watch_state' */
     147             : 
     148           0 :         result = open_service(b, mem_ctx, hSCM, service,
     149             :                               SC_RIGHT_SVC_QUERY_STATUS,
     150             :                               &hService);
     151           0 :         if (!W_ERROR_IS_OK(result) ) {
     152           0 :                 return result;
     153             :         }
     154             : 
     155           0 :         status = dcerpc_svcctl_QueryServiceStatus(b, mem_ctx,
     156             :                                                   &hService,
     157             :                                                   &service_status,
     158             :                                                   &result);
     159           0 :         if (!NT_STATUS_IS_OK(status)) {
     160           0 :                 result = ntstatus_to_werror(status);
     161           0 :                 goto done;
     162             :         }
     163           0 :         if (!W_ERROR_IS_OK(result)) {
     164           0 :                 goto done;
     165             :         }
     166             : 
     167           0 :         *state = service_status.state;
     168             : 
     169           0 :  done:
     170           0 :         if (is_valid_policy_hnd(&hService)) {
     171           0 :                 WERROR _result;
     172           0 :                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
     173             :         }
     174             : 
     175           0 :         return result;
     176             : }
     177             : 
     178             : /********************************************************************
     179             : ********************************************************************/
     180             : 
     181           0 : static WERROR watch_service_state(struct rpc_pipe_client *pipe_hnd,
     182             :                                 TALLOC_CTX *mem_ctx,
     183             :                                 struct policy_handle *hSCM,
     184             :                                 const char *service,
     185             :                                 uint32_t watch_state,
     186             :                                 uint32_t *final_state )
     187             : {
     188           0 :         uint32_t i;
     189           0 :         uint32_t state = 0;
     190           0 :         WERROR result = WERR_GEN_FAILURE;
     191             : 
     192             : 
     193           0 :         i = 0;
     194           0 :         while ( (state != watch_state ) && i<30 ) {
     195             :                 /* get the status */
     196             : 
     197           0 :                 result = query_service_state(pipe_hnd, mem_ctx, hSCM, service, &state  );
     198           0 :                 if ( !W_ERROR_IS_OK(result) ) {
     199           0 :                         break;
     200             :                 }
     201             : 
     202           0 :                 d_printf(".");
     203           0 :                 i++;
     204           0 :                 usleep( 100 );
     205             :         }
     206           0 :         d_printf("\n");
     207             : 
     208           0 :         *final_state = state;
     209             : 
     210           0 :         return result;
     211             : }
     212             : 
     213             : /********************************************************************
     214             : ********************************************************************/
     215             : 
     216           0 : static WERROR control_service(struct rpc_pipe_client *pipe_hnd,
     217             :                                 TALLOC_CTX *mem_ctx,
     218             :                                 struct policy_handle *hSCM,
     219             :                                 const char *service,
     220             :                                 uint32_t control,
     221             :                                 uint32_t watch_state )
     222             : {
     223           0 :         struct policy_handle hService;
     224           0 :         WERROR result = WERR_GEN_FAILURE;
     225           0 :         NTSTATUS status;
     226           0 :         struct SERVICE_STATUS service_status;
     227           0 :         uint32_t state = 0;
     228           0 :         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
     229             : 
     230             :         /* Open the Service */
     231             : 
     232           0 :         result = open_service(b, mem_ctx, hSCM, service,
     233             :                               (SC_RIGHT_SVC_STOP|SC_RIGHT_SVC_PAUSE_CONTINUE),
     234             :                               &hService);
     235           0 :         if (!W_ERROR_IS_OK(result) ) {
     236           0 :                 return result;
     237             :         }
     238             : 
     239             :         /* get the status */
     240             : 
     241           0 :         status = dcerpc_svcctl_ControlService(b, mem_ctx,
     242             :                                               &hService,
     243             :                                               control,
     244             :                                               &service_status,
     245             :                                               &result);
     246             : 
     247           0 :         if (!NT_STATUS_IS_OK(status)) {
     248           0 :                 result = ntstatus_to_werror(status);
     249           0 :                 d_fprintf(stderr, _("Control service request failed.  [%s]\n"),
     250             :                           nt_errstr(status));
     251           0 :                 goto done;
     252             :         }
     253           0 :         if (!W_ERROR_IS_OK(result) ) {
     254           0 :                 d_fprintf(stderr, _("Control service request failed.  [%s]\n"),
     255             :                           win_errstr(result));
     256           0 :                 goto done;
     257             :         }
     258             : 
     259             :         /* loop -- checking the state until we are where we want to be */
     260             : 
     261           0 :         result = watch_service_state(pipe_hnd, mem_ctx, hSCM, service, watch_state, &state );
     262             : 
     263           0 :         d_printf(_("%s service is %s.\n"), service, svc_status_string(state));
     264             : 
     265           0 : done:
     266           0 :         if (is_valid_policy_hnd(&hService)) {
     267           0 :                 WERROR _result;
     268           0 :                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
     269             :         }
     270             : 
     271           0 :         return result;
     272             : }
     273             : 
     274             : /********************************************************************
     275             : ********************************************************************/
     276             : 
     277           0 : static NTSTATUS rpc_service_list_internal(struct net_context *c,
     278             :                                         const struct dom_sid *domain_sid,
     279             :                                         const char *domain_name,
     280             :                                         struct cli_state *cli,
     281             :                                         struct rpc_pipe_client *pipe_hnd,
     282             :                                         TALLOC_CTX *mem_ctx,
     283             :                                         int argc,
     284             :                                         const char **argv )
     285             : {
     286           0 :         struct policy_handle hSCM;
     287           0 :         struct ENUM_SERVICE_STATUSW *services = NULL;
     288           0 :         WERROR result = WERR_GEN_FAILURE;
     289           0 :         NTSTATUS status;
     290           0 :         int i;
     291           0 :         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
     292             : 
     293           0 :         uint8_t *buffer;
     294           0 :         uint32_t buf_size = 0;
     295           0 :         uint32_t bytes_needed = 0;
     296           0 :         uint32_t num_services = 0;
     297           0 :         uint32_t resume_handle = 0;
     298             : 
     299           0 :         if (argc != 0 ) {
     300           0 :                 d_printf("%s net rpc service list\n", _("Usage:"));
     301           0 :                 return NT_STATUS_OK;
     302             :         }
     303             : 
     304           0 :         result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
     305             :                           SC_RIGHT_MGR_ENUMERATE_SERVICE,
     306             :                           &hSCM);
     307           0 :         if (!W_ERROR_IS_OK(result)) {
     308           0 :                 return werror_to_ntstatus(result);
     309             :         }
     310             : 
     311           0 :         buffer = talloc_array(mem_ctx, uint8_t, buf_size);
     312           0 :         if (buffer == NULL) {
     313           0 :                 status = NT_STATUS_NO_MEMORY;
     314           0 :                 goto done;
     315             :         }
     316             : 
     317           0 :         do {
     318           0 :                 status = dcerpc_svcctl_EnumServicesStatusW(b, mem_ctx,
     319             :                                                            &hSCM,
     320             :                                                            SERVICE_TYPE_WIN32,
     321             :                                                            SERVICE_STATE_ALL,
     322             :                                                            buffer,
     323             :                                                            buf_size,
     324             :                                                            &bytes_needed,
     325             :                                                            &num_services,
     326             :                                                            &resume_handle,
     327             :                                                            &result);
     328             : 
     329           0 :                 if (!NT_STATUS_IS_OK(status)) {
     330           0 :                         d_fprintf(stderr,
     331           0 :                                 _("Failed to enumerate services.  [%s]\n"),
     332             :                                 nt_errstr(status));
     333           0 :                         break;
     334             :                 }
     335             : 
     336           0 :                 if (W_ERROR_EQUAL(result, WERR_MORE_DATA) && bytes_needed > 0) {
     337           0 :                         buf_size = bytes_needed;
     338           0 :                         buffer = talloc_realloc(mem_ctx, buffer, uint8_t, bytes_needed);
     339           0 :                         if (buffer == NULL) {
     340           0 :                                 status = NT_STATUS_NO_MEMORY;
     341           0 :                                 break;
     342             :                         }
     343           0 :                         continue;
     344             :                 }
     345             : 
     346           0 :                 if (!W_ERROR_IS_OK(result)) {
     347           0 :                         status = werror_to_ntstatus(result);
     348           0 :                         d_fprintf(stderr,
     349           0 :                                 _("Failed to enumerate services.  [%s]\n"),
     350             :                                 win_errstr(result));
     351           0 :                         break;
     352             :                 }
     353             : 
     354           0 :                 if ( num_services == 0 ) {
     355           0 :                         d_printf(_("No services returned\n"));
     356           0 :                         break;
     357             :                 }
     358             : 
     359             :                 {
     360           0 :                         enum ndr_err_code ndr_err;
     361           0 :                         DATA_BLOB blob;
     362           0 :                         struct ndr_pull *ndr;
     363             : 
     364           0 :                         blob.length = buf_size;
     365           0 :                         blob.data = talloc_steal(mem_ctx, buffer);
     366             : 
     367           0 :                         services = talloc_array(mem_ctx, struct ENUM_SERVICE_STATUSW, num_services);
     368           0 :                         if (!services) {
     369           0 :                                 status = NT_STATUS_NO_MEMORY;
     370           0 :                                 break;
     371             :                         }
     372             : 
     373           0 :                         ndr = ndr_pull_init_blob(&blob, mem_ctx);
     374           0 :                         if (ndr == NULL) {
     375           0 :                                 status = NT_STATUS_NO_MEMORY;
     376           0 :                                 break;
     377             :                         }
     378             : 
     379           0 :                         ndr_err = ndr_pull_ENUM_SERVICE_STATUSW_array(
     380             :                                 ndr, num_services, services);
     381           0 :                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
     382           0 :                                 status = ndr_map_error2ntstatus(ndr_err);
     383           0 :                                 break;
     384             :                         }
     385             : 
     386           0 :                         for ( i=0; i<num_services; i++ ) {
     387           0 :                                 d_printf("%-20s    \"%s\"\n",
     388           0 :                                         services[i].service_name,
     389           0 :                                         services[i].display_name);
     390             :                         }
     391             :                 }
     392             : 
     393           0 :         } while (W_ERROR_EQUAL(result, WERR_MORE_DATA));
     394             : 
     395           0 : done:
     396           0 :         if (is_valid_policy_hnd(&hSCM)) {
     397           0 :                 WERROR _result;
     398           0 :                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
     399             :         }
     400             : 
     401           0 :         return status;
     402             : }
     403             : 
     404             : /********************************************************************
     405             : ********************************************************************/
     406             : 
     407           0 : static NTSTATUS rpc_service_status_internal(struct net_context *c,
     408             :                                                 const struct dom_sid *domain_sid,
     409             :                                                 const char *domain_name,
     410             :                                                 struct cli_state *cli,
     411             :                                                 struct rpc_pipe_client *pipe_hnd,
     412             :                                                 TALLOC_CTX *mem_ctx,
     413             :                                                 int argc,
     414             :                                                 const char **argv )
     415             : {
     416           0 :         struct policy_handle hSCM, hService;
     417           0 :         WERROR result = WERR_GEN_FAILURE;
     418           0 :         NTSTATUS status;
     419           0 :         struct SERVICE_STATUS service_status;
     420           0 :         struct QUERY_SERVICE_CONFIG config;
     421           0 :         uint32_t buf_size = sizeof(config);
     422           0 :         uint32_t ret_size = 0;
     423           0 :         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
     424             : 
     425           0 :         if (argc != 1 ) {
     426           0 :                 d_printf("%s net rpc service status <service>\n", _("Usage:"));
     427           0 :                 return NT_STATUS_OK;
     428             :         }
     429             : 
     430             :         /* Open the Service Control Manager */
     431           0 :         result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
     432             :                           SC_RIGHT_MGR_ENUMERATE_SERVICE,
     433             :                           &hSCM);
     434           0 :         if (!W_ERROR_IS_OK(result)) {
     435           0 :                 return werror_to_ntstatus(result);
     436             :         }
     437             : 
     438             :         /* Open the Service */
     439             : 
     440           0 :         result = open_service(b, mem_ctx, &hSCM, argv[0],
     441             :                               (SC_RIGHT_SVC_QUERY_STATUS|SC_RIGHT_SVC_QUERY_CONFIG),
     442             :                               &hService);
     443           0 :         if (!W_ERROR_IS_OK(result) ) {
     444           0 :                 goto done;
     445             :         }
     446             : 
     447             :         /* get the status */
     448             : 
     449           0 :         status = dcerpc_svcctl_QueryServiceStatus(b, mem_ctx,
     450             :                                                   &hService,
     451             :                                                   &service_status,
     452             :                                                   &result);
     453           0 :         if (!NT_STATUS_IS_OK(status)) {
     454           0 :                 result = ntstatus_to_werror(status);
     455           0 :                 d_fprintf(stderr, _("Query status request failed.  [%s]\n"),
     456             :                           nt_errstr(status));
     457           0 :                 goto done;
     458             :         }
     459             : 
     460           0 :         if (!W_ERROR_IS_OK(result) ) {
     461           0 :                 d_fprintf(stderr, _("Query status request failed.  [%s]\n"),
     462             :                           win_errstr(result));
     463           0 :                 goto done;
     464             :         }
     465             : 
     466           0 :         d_printf(_("%s service is %s.\n"), argv[0],
     467           0 :                  svc_status_string(service_status.state));
     468             : 
     469             :         /* get the config */
     470             : 
     471           0 :         status = dcerpc_svcctl_QueryServiceConfigW(b, mem_ctx,
     472             :                                                    &hService,
     473             :                                                    &config,
     474             :                                                    buf_size,
     475             :                                                    &ret_size,
     476             :                                                    &result);
     477           0 :         if (!NT_STATUS_IS_OK(status)) {
     478           0 :                 result = ntstatus_to_werror(status);
     479           0 :                 d_fprintf(stderr, _("Query config request failed.  [%s]\n"),
     480             :                           nt_errstr(status));
     481           0 :                 goto done;
     482             :         }
     483             : 
     484           0 :         if (W_ERROR_EQUAL(result, WERR_INSUFFICIENT_BUFFER)) {
     485           0 :                 buf_size = ret_size;
     486           0 :                 status = dcerpc_svcctl_QueryServiceConfigW(b, mem_ctx,
     487             :                                                            &hService,
     488             :                                                            &config,
     489             :                                                            buf_size,
     490             :                                                            &ret_size,
     491             :                                                            &result);
     492           0 :                 if (!NT_STATUS_IS_OK(status)) {
     493           0 :                         result = ntstatus_to_werror(status);
     494           0 :                         d_fprintf(stderr, _("Query config request failed.  [%s]\n"),
     495             :                                   nt_errstr(status));
     496           0 :                         goto done;
     497             :                 }
     498             :         }
     499             : 
     500           0 :         if (!W_ERROR_IS_OK(result) ) {
     501           0 :                 d_fprintf(stderr, _("Query config request failed.  [%s]\n"),
     502             :                           win_errstr(result));
     503           0 :                 goto done;
     504             :         }
     505             : 
     506             :         /* print out the configuration information for the service */
     507             : 
     508           0 :         d_printf(_("Configuration details:\n"));
     509           0 :         d_printf(_("\tControls Accepted    = 0x%x\n"),
     510             :                  service_status.controls_accepted);
     511           0 :         d_printf(_("\tService Type         = 0x%x\n"), config.service_type);
     512           0 :         d_printf(_("\tStart Type           = 0x%x\n"), config.start_type);
     513           0 :         d_printf(_("\tError Control        = 0x%x\n"), config.error_control);
     514           0 :         d_printf(_("\tTag ID               = 0x%x\n"), config.tag_id);
     515             : 
     516           0 :         if (config.executablepath) {
     517           0 :                 d_printf(_("\tExecutable Path      = %s\n"),
     518             :                          config.executablepath);
     519             :         }
     520             : 
     521           0 :         if (config.loadordergroup) {
     522           0 :                 d_printf(_("\tLoad Order Group     = %s\n"),
     523             :                          config.loadordergroup);
     524             :         }
     525             : 
     526           0 :         if (config.dependencies) {
     527           0 :                 d_printf(_("\tDependencies         = %s\n"),
     528             :                          config.dependencies);
     529             :         }
     530             : 
     531           0 :         if (config.startname) {
     532           0 :                 d_printf(_("\tStart Name           = %s\n"), config.startname);
     533             :         }
     534             : 
     535           0 :         if (config.displayname) {
     536           0 :                 d_printf(_("\tDisplay Name         = %s\n"),
     537             :                          config.displayname);
     538             :         }
     539             : 
     540           0 : done:
     541           0 :         if (is_valid_policy_hnd(&hService)) {
     542           0 :                 WERROR _result;
     543           0 :                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
     544             :         }
     545           0 :         if (is_valid_policy_hnd(&hSCM)) {
     546           0 :                 WERROR _result;
     547           0 :                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
     548             :         }
     549             : 
     550           0 :         return werror_to_ntstatus(result);
     551             : }
     552             : 
     553             : /********************************************************************
     554             : ********************************************************************/
     555             : 
     556           0 : static NTSTATUS rpc_service_stop_internal(struct net_context *c,
     557             :                                         const struct dom_sid *domain_sid,
     558             :                                         const char *domain_name,
     559             :                                         struct cli_state *cli,
     560             :                                         struct rpc_pipe_client *pipe_hnd,
     561             :                                         TALLOC_CTX *mem_ctx,
     562             :                                         int argc,
     563             :                                         const char **argv )
     564             : {
     565           0 :         struct policy_handle hSCM;
     566           0 :         WERROR result = WERR_GEN_FAILURE;
     567           0 :         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
     568             : 
     569           0 :         if (argc != 1 ) {
     570           0 :                 d_printf("%s net rpc service status <service>\n", _("Usage:"));
     571           0 :                 return NT_STATUS_OK;
     572             :         }
     573             : 
     574             :         /* Open the Service Control Manager */
     575           0 :         result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
     576             :                           SC_RIGHT_MGR_ENUMERATE_SERVICE,
     577             :                           &hSCM);
     578           0 :         if (!W_ERROR_IS_OK(result)) {
     579           0 :                 return werror_to_ntstatus(result);
     580             :         }
     581             : 
     582           0 :         result = control_service(pipe_hnd, mem_ctx, &hSCM, argv[0],
     583             :                 SVCCTL_CONTROL_STOP, SVCCTL_STOPPED );
     584             : 
     585           0 :         if (is_valid_policy_hnd(&hSCM)) {
     586           0 :                 WERROR _result;
     587           0 :                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
     588             :         }
     589             : 
     590           0 :         return werror_to_ntstatus(result);
     591             : }
     592             : 
     593             : /********************************************************************
     594             : ********************************************************************/
     595             : 
     596           0 : static NTSTATUS rpc_service_pause_internal(struct net_context *c,
     597             :                                         const struct dom_sid *domain_sid,
     598             :                                         const char *domain_name,
     599             :                                         struct cli_state *cli,
     600             :                                         struct rpc_pipe_client *pipe_hnd,
     601             :                                         TALLOC_CTX *mem_ctx,
     602             :                                         int argc,
     603             :                                         const char **argv )
     604             : {
     605           0 :         struct policy_handle hSCM;
     606           0 :         WERROR result = WERR_GEN_FAILURE;
     607           0 :         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
     608             : 
     609           0 :         if (argc != 1 ) {
     610           0 :                 d_printf("%s net rpc service status <service>\n", _("Usage:"));
     611           0 :                 return NT_STATUS_OK;
     612             :         }
     613             : 
     614             :         /* Open the Service Control Manager */
     615           0 :         result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
     616             :                           SC_RIGHT_MGR_ENUMERATE_SERVICE,
     617             :                           &hSCM);
     618           0 :         if (!W_ERROR_IS_OK(result)) {
     619           0 :                 return werror_to_ntstatus(result);
     620             :         }
     621             : 
     622           0 :         result = control_service(pipe_hnd, mem_ctx, &hSCM, argv[0],
     623             :                 SVCCTL_CONTROL_PAUSE, SVCCTL_PAUSED );
     624             : 
     625           0 :         if (is_valid_policy_hnd(&hSCM)) {
     626           0 :                 WERROR _result;
     627           0 :                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
     628             :         }
     629             : 
     630           0 :         return werror_to_ntstatus(result);
     631             : }
     632             : 
     633             : /********************************************************************
     634             : ********************************************************************/
     635             : 
     636           0 : static NTSTATUS rpc_service_resume_internal(struct net_context *c,
     637             :                                         const struct dom_sid *domain_sid,
     638             :                                         const char *domain_name,
     639             :                                         struct cli_state *cli,
     640             :                                         struct rpc_pipe_client *pipe_hnd,
     641             :                                         TALLOC_CTX *mem_ctx,
     642             :                                         int argc,
     643             :                                         const char **argv )
     644             : {
     645           0 :         struct policy_handle hSCM;
     646           0 :         WERROR result = WERR_GEN_FAILURE;
     647           0 :         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
     648             : 
     649           0 :         if (argc != 1 ) {
     650           0 :                 d_printf("%s net rpc service status <service>\n", _("Usage:"));
     651           0 :                 return NT_STATUS_OK;
     652             :         }
     653             : 
     654             :         /* Open the Service Control Manager */
     655           0 :         result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
     656             :                           SC_RIGHT_MGR_ENUMERATE_SERVICE,
     657             :                           &hSCM);
     658           0 :         if (!W_ERROR_IS_OK(result)) {
     659           0 :                 return werror_to_ntstatus(result);
     660             :         }
     661             : 
     662           0 :         result = control_service(pipe_hnd, mem_ctx, &hSCM, argv[0],
     663             :                 SVCCTL_CONTROL_CONTINUE, SVCCTL_RUNNING );
     664             : 
     665           0 :         if (is_valid_policy_hnd(&hSCM)) {
     666           0 :                 WERROR _result;
     667           0 :                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
     668             :         }
     669             : 
     670           0 :         return werror_to_ntstatus(result);
     671             : }
     672             : 
     673             : /********************************************************************
     674             : ********************************************************************/
     675             : 
     676           0 : static NTSTATUS rpc_service_start_internal(struct net_context *c,
     677             :                                         const struct dom_sid *domain_sid,
     678             :                                         const char *domain_name,
     679             :                                         struct cli_state *cli,
     680             :                                         struct rpc_pipe_client *pipe_hnd,
     681             :                                         TALLOC_CTX *mem_ctx,
     682             :                                         int argc,
     683             :                                         const char **argv )
     684             : {
     685           0 :         struct policy_handle hSCM, hService;
     686           0 :         WERROR result = WERR_GEN_FAILURE;
     687           0 :         NTSTATUS status;
     688           0 :         uint32_t state = 0;
     689           0 :         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
     690             : 
     691           0 :         if (argc != 1 ) {
     692           0 :                 d_printf("%s net rpc service status <service>\n", _("Usage:"));
     693           0 :                 return NT_STATUS_OK;
     694             :         }
     695             : 
     696             :         /* Open the Service Control Manager */
     697           0 :         result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
     698             :                           SC_RIGHT_MGR_ENUMERATE_SERVICE,
     699             :                           &hSCM);
     700           0 :         if (!W_ERROR_IS_OK(result)) {
     701           0 :                 return werror_to_ntstatus(result);
     702             :         }
     703             : 
     704             : 
     705             :         /* Open the Service */
     706             : 
     707           0 :         result = open_service(b, mem_ctx, &hSCM, argv[0],
     708             :                               SC_RIGHT_SVC_START,
     709             :                               &hService);
     710           0 :         if (!W_ERROR_IS_OK(result) ) {
     711           0 :                 goto done;
     712             :         }
     713             : 
     714             :         /* get the status */
     715             : 
     716           0 :         status = dcerpc_svcctl_StartServiceW(b, mem_ctx,
     717             :                                              &hService,
     718             :                                              0,
     719             :                                              NULL,
     720             :                                              &result);
     721             : 
     722           0 :         if (!NT_STATUS_IS_OK(status)) {
     723           0 :                 result = ntstatus_to_werror(status);
     724           0 :                 d_fprintf(stderr, _("Query status request failed.  [%s]\n"),
     725             :                           nt_errstr(status));
     726           0 :                 goto done;
     727             :         }
     728           0 :         if (!W_ERROR_IS_OK(result) ) {
     729           0 :                 d_fprintf(stderr, _("Query status request failed.  [%s]\n"),
     730             :                           win_errstr(result));
     731           0 :                 goto done;
     732             :         }
     733             : 
     734           0 :         result = watch_service_state(pipe_hnd, mem_ctx, &hSCM, argv[0], SVCCTL_RUNNING, &state  );
     735             : 
     736           0 :         if ( W_ERROR_IS_OK(result) && (state == SVCCTL_RUNNING) )
     737           0 :                 d_printf(_("Successfully started service: %s\n"),
     738             :                          argv[0] );
     739             :         else
     740           0 :                 d_fprintf(stderr,_("Failed to start service: %s [%s]\n"),
     741             :                           argv[0], win_errstr(result) );
     742             : 
     743           0 : done:
     744           0 :         if (is_valid_policy_hnd(&hService)) {
     745           0 :                 WERROR _result;
     746           0 :                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
     747             :         }
     748           0 :         if (is_valid_policy_hnd(&hSCM)) {
     749           0 :                 WERROR _result;
     750           0 :                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
     751             :         }
     752             : 
     753           0 :         return werror_to_ntstatus(result);
     754             : }
     755             : 
     756             : /********************************************************************
     757             : ********************************************************************/
     758             : 
     759           0 : static NTSTATUS rpc_service_delete_internal(struct net_context *c,
     760             :                                             const struct dom_sid *domain_sid,
     761             :                                             const char *domain_name,
     762             :                                             struct cli_state *cli,
     763             :                                             struct rpc_pipe_client *pipe_hnd,
     764             :                                             TALLOC_CTX *mem_ctx,
     765             :                                             int argc,
     766             :                                             const char **argv)
     767             : {
     768           0 :         struct policy_handle hSCM, hService;
     769           0 :         WERROR result = WERR_GEN_FAILURE;
     770           0 :         NTSTATUS status;
     771           0 :         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
     772             : 
     773           0 :         if (argc != 1 ) {
     774           0 :                 d_printf("%s net rpc service delete <service>\n", _("Usage:"));
     775           0 :                 return NT_STATUS_OK;
     776             :         }
     777             : 
     778           0 :         ZERO_STRUCT(hSCM);
     779           0 :         ZERO_STRUCT(hService);
     780             : 
     781             :         /* Open the Service Control Manager */
     782           0 :         result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
     783             :                           SC_RIGHT_MGR_ENUMERATE_SERVICE,
     784             :                           &hSCM);
     785           0 :         if (!W_ERROR_IS_OK(result)) {
     786           0 :                 return werror_to_ntstatus(result);
     787             :         }
     788             : 
     789             :         /* Open the Service */
     790             : 
     791           0 :         result = open_service(b, mem_ctx, &hSCM, argv[0],
     792             :                               SERVICE_ALL_ACCESS,
     793             :                               &hService);
     794           0 :         if (!W_ERROR_IS_OK(result) ) {
     795           0 :                 goto done;
     796             :         }
     797             : 
     798             :         /* Delete the Service */
     799             : 
     800           0 :         status = dcerpc_svcctl_DeleteService(b, mem_ctx,
     801             :                                              &hService,
     802             :                                              &result);
     803             : 
     804           0 :         if (!NT_STATUS_IS_OK(status)) {
     805           0 :                 result = ntstatus_to_werror(status);
     806           0 :                 d_fprintf(stderr, _("Delete service request failed.  [%s]\n"),
     807             :                         nt_errstr(status));
     808           0 :                 goto done;
     809             :         }
     810           0 :         if (!W_ERROR_IS_OK(result)) {
     811           0 :                 d_fprintf(stderr, _("Delete service request failed.  [%s]\n"),
     812             :                         win_errstr(result));
     813           0 :                 goto done;
     814             :         }
     815             : 
     816           0 :         d_printf(_("Successfully deleted Service: %s\n"), argv[0]);
     817             : 
     818           0 :  done:
     819           0 :         if (is_valid_policy_hnd(&hService)) {
     820           0 :                 WERROR _result;
     821           0 :                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
     822             :         }
     823           0 :         if (is_valid_policy_hnd(&hSCM)) {
     824           0 :                 WERROR _result;
     825           0 :                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
     826             :         }
     827             : 
     828           0 :         return werror_to_ntstatus(result);
     829             : }
     830             : 
     831             : /********************************************************************
     832             : ********************************************************************/
     833             : 
     834           0 : static NTSTATUS rpc_service_create_internal(struct net_context *c,
     835             :                                             const struct dom_sid *domain_sid,
     836             :                                             const char *domain_name,
     837             :                                             struct cli_state *cli,
     838             :                                             struct rpc_pipe_client *pipe_hnd,
     839             :                                             TALLOC_CTX *mem_ctx,
     840             :                                             int argc,
     841             :                                             const char **argv)
     842             : {
     843           0 :         struct policy_handle hSCM, hService;
     844           0 :         WERROR result = WERR_GEN_FAILURE;
     845           0 :         NTSTATUS status;
     846           0 :         const char *ServiceName;
     847           0 :         const char *DisplayName;
     848           0 :         const char *binary_path;
     849           0 :         struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
     850             : 
     851           0 :         if (argc != 3) {
     852           0 :                 d_printf("%s net rpc service create <service> "
     853             :                          "<displayname> <binarypath>\n", _("Usage:"));
     854           0 :                 return NT_STATUS_OK;
     855             :         }
     856             : 
     857           0 :         ZERO_STRUCT(hSCM);
     858           0 :         ZERO_STRUCT(hService);
     859             : 
     860             :         /* Open the Service Control Manager */
     861           0 :         result = open_scm(b, mem_ctx, pipe_hnd->srv_name_slash,
     862             :                           SC_RIGHT_MGR_CREATE_SERVICE,
     863             :                           &hSCM);
     864           0 :         if (!W_ERROR_IS_OK(result)) {
     865           0 :                 return werror_to_ntstatus(result);
     866             :         }
     867             : 
     868             :         /* Create the service */
     869             : 
     870           0 :         ServiceName = argv[0];
     871           0 :         DisplayName = argv[1];
     872           0 :         binary_path = argv[2];
     873             : 
     874           0 :         status = dcerpc_svcctl_CreateServiceW(b, mem_ctx,
     875             :                                               &hSCM,
     876             :                                               ServiceName,
     877             :                                               DisplayName,
     878             :                                               SERVICE_ALL_ACCESS,
     879             :                                               SERVICE_TYPE_WIN32_OWN_PROCESS,
     880             :                                               SVCCTL_DEMAND_START,
     881             :                                               SVCCTL_SVC_ERROR_NORMAL,
     882             :                                               binary_path,
     883             :                                               NULL, /* LoadOrderGroupKey */
     884             :                                               NULL, /* TagId */
     885             :                                               NULL, /* dependencies */
     886             :                                               0, /* dependencies_size */
     887             :                                               NULL, /* service_start_name */
     888             :                                               NULL, /* password */
     889             :                                               0, /* password_size */
     890             :                                               &hService,
     891             :                                               &result);
     892           0 :         if (!NT_STATUS_IS_OK(status)) {
     893           0 :                 result = ntstatus_to_werror(status);
     894           0 :                 d_fprintf(stderr, _("Create service request failed.  [%s]\n"),
     895             :                           nt_errstr(status));
     896           0 :                 goto done;
     897             :         }
     898           0 :         if (!W_ERROR_IS_OK(result)) {
     899           0 :                 d_fprintf(stderr, _("Create service request failed.  [%s]\n"),
     900             :                           win_errstr(result));
     901           0 :                 goto done;
     902             :         }
     903             : 
     904           0 :         d_printf(_("Successfully created Service: %s\n"), argv[0]);
     905             : 
     906           0 :  done:
     907           0 :         if (is_valid_policy_hnd(&hService)) {
     908           0 :                 WERROR _result;
     909           0 :                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hService, &_result);
     910             :         }
     911           0 :         if (is_valid_policy_hnd(&hSCM)) {
     912           0 :                 WERROR _result;
     913           0 :                 dcerpc_svcctl_CloseServiceHandle(b, mem_ctx, &hSCM, &_result);
     914             :         }
     915             : 
     916           0 :         return werror_to_ntstatus(result);
     917             : }
     918             : 
     919             : /********************************************************************
     920             : ********************************************************************/
     921             : 
     922           0 : static int rpc_service_list(struct net_context *c, int argc, const char **argv )
     923             : {
     924           0 :         if (c->display_usage) {
     925           0 :                 d_printf(  "%s\n"
     926             :                            "net rpc service list\n"
     927             :                            "    %s\n",
     928             :                          _("Usage:"),
     929             :                          _("View configured Win32 services"));
     930           0 :                 return 0;
     931             :         }
     932             : 
     933           0 :         return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
     934             :                 rpc_service_list_internal, argc, argv );
     935             : }
     936             : 
     937             : /********************************************************************
     938             : ********************************************************************/
     939             : 
     940           0 : static int rpc_service_start(struct net_context *c, int argc, const char **argv )
     941             : {
     942           0 :         if (c->display_usage) {
     943           0 :                 d_printf(  "%s\n"
     944             :                            "net rpc service start <service>\n"
     945             :                            "    %s\n",
     946             :                          _("Usage:"),
     947             :                          _("Start a Win32 service"));
     948           0 :                 return 0;
     949             :         }
     950             : 
     951           0 :         return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
     952             :                 rpc_service_start_internal, argc, argv );
     953             : }
     954             : 
     955             : /********************************************************************
     956             : ********************************************************************/
     957             : 
     958           0 : static int rpc_service_stop(struct net_context *c, int argc, const char **argv )
     959             : {
     960           0 :         if (c->display_usage) {
     961           0 :                 d_printf(  "%s\n"
     962             :                            "net rpc service stop <service>\n"
     963             :                            "    %s\n",
     964             :                          _("Usage:"),
     965             :                          _("Stop a Win32 service"));
     966           0 :                 return 0;
     967             :         }
     968             : 
     969           0 :         return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
     970             :                 rpc_service_stop_internal, argc, argv );
     971             : }
     972             : 
     973             : /********************************************************************
     974             : ********************************************************************/
     975             : 
     976           0 : static int rpc_service_resume(struct net_context *c, int argc, const char **argv )
     977             : {
     978           0 :         if (c->display_usage) {
     979           0 :                 d_printf(  "%s\n"
     980             :                            "net rpc service resume <service>\n"
     981             :                            "    %s\n",
     982             :                          _("Usage:"),
     983             :                          _("Resume a Win32 service"));
     984           0 :                 return 0;
     985             :         }
     986             : 
     987           0 :         return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
     988             :                 rpc_service_resume_internal, argc, argv );
     989             : }
     990             : 
     991             : /********************************************************************
     992             : ********************************************************************/
     993             : 
     994           0 : static int rpc_service_pause(struct net_context *c, int argc, const char **argv )
     995             : {
     996           0 :         if (c->display_usage) {
     997           0 :                 d_printf(  "%s\n"
     998             :                            "net rpc service pause <service>\n"
     999             :                            "    %s\n",
    1000             :                          _("Usage:"),
    1001             :                          _("Pause a Win32 service"));
    1002           0 :                 return 0;
    1003             :         }
    1004             : 
    1005           0 :         return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
    1006             :                 rpc_service_pause_internal, argc, argv );
    1007             : }
    1008             : 
    1009             : /********************************************************************
    1010             : ********************************************************************/
    1011             : 
    1012           0 : static int rpc_service_status(struct net_context *c, int argc, const char **argv )
    1013             : {
    1014           0 :         if (c->display_usage) {
    1015           0 :                 d_printf(  "%s\n"
    1016             :                            "net rpc service status <service>\n"
    1017             :                            "     %s\n",
    1018             :                          _("Usage:"),
    1019             :                          _("Show the current status of a service"));
    1020           0 :                 return 0;
    1021             :         }
    1022             : 
    1023           0 :         return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
    1024             :                 rpc_service_status_internal, argc, argv );
    1025             : }
    1026             : 
    1027             : /********************************************************************
    1028             : ********************************************************************/
    1029             : 
    1030           0 : static int rpc_service_delete(struct net_context *c, int argc, const char **argv)
    1031             : {
    1032           0 :         if (c->display_usage) {
    1033           0 :                 d_printf(  "%s\n"
    1034             :                            "net rpc service delete <service>\n"
    1035             :                            "    %s\n",
    1036             :                          _("Usage:"),
    1037             :                          _("Delete a Win32 service"));
    1038           0 :                 return 0;
    1039             :         }
    1040             : 
    1041           0 :         return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
    1042             :                 rpc_service_delete_internal, argc, argv);
    1043             : }
    1044             : 
    1045             : /********************************************************************
    1046             : ********************************************************************/
    1047             : 
    1048           0 : static int rpc_service_create(struct net_context *c, int argc, const char **argv)
    1049             : {
    1050           0 :         if (c->display_usage) {
    1051           0 :                 d_printf(  "%s\n"
    1052             :                            "net rpc service create <service>\n"
    1053             :                            "    %s\n",
    1054             :                          _("Usage:"),
    1055             :                          _("Create a Win32 service"));
    1056           0 :                 return 0;
    1057             :         }
    1058             : 
    1059           0 :         return run_rpc_command(c, NULL, &ndr_table_svcctl, 0,
    1060             :                 rpc_service_create_internal, argc, argv);
    1061             : }
    1062             : 
    1063             : /********************************************************************
    1064             : ********************************************************************/
    1065             : 
    1066           0 : int net_rpc_service(struct net_context *c, int argc, const char **argv)
    1067             : {
    1068           0 :         struct functable func[] = {
    1069             :                 {
    1070             :                         "list",
    1071             :                         rpc_service_list,
    1072             :                         NET_TRANSPORT_RPC,
    1073             :                         N_("View configured Win32 services"),
    1074             :                         N_("net rpc service list\n"
    1075             :                            "    View configured Win32 services")
    1076             :                 },
    1077             :                 {
    1078             :                         "start",
    1079             :                         rpc_service_start,
    1080             :                         NET_TRANSPORT_RPC,
    1081             :                         N_("Start a service"),
    1082             :                         N_("net rpc service start\n"
    1083             :                            "    Start a service")
    1084             :                 },
    1085             :                 {
    1086             :                         "stop",
    1087             :                         rpc_service_stop,
    1088             :                         NET_TRANSPORT_RPC,
    1089             :                         N_("Stop a service"),
    1090             :                         N_("net rpc service stop\n"
    1091             :                            "    Stop a service")
    1092             :                 },
    1093             :                 {
    1094             :                         "pause",
    1095             :                         rpc_service_pause,
    1096             :                         NET_TRANSPORT_RPC,
    1097             :                         N_("Pause a service"),
    1098             :                         N_("net rpc service pause\n"
    1099             :                            "    Pause a service")
    1100             :                 },
    1101             :                 {
    1102             :                         "resume",
    1103             :                         rpc_service_resume,
    1104             :                         NET_TRANSPORT_RPC,
    1105             :                         N_("Resume a paused service"),
    1106             :                         N_("net rpc service resume\n"
    1107             :                            "    Resume a service")
    1108             :                 },
    1109             :                 {
    1110             :                         "status",
    1111             :                         rpc_service_status,
    1112             :                         NET_TRANSPORT_RPC,
    1113             :                         N_("View current status of a service"),
    1114             :                         N_("net rpc service status\n"
    1115             :                            "    View current status of a service")
    1116             :                 },
    1117             :                 {
    1118             :                         "delete",
    1119             :                         rpc_service_delete,
    1120             :                         NET_TRANSPORT_RPC,
    1121             :                         N_("Delete a service"),
    1122             :                         N_("net rpc service delete\n"
    1123             :                            "    Deletes a service")
    1124             :                 },
    1125             :                 {
    1126             :                         "create",
    1127             :                         rpc_service_create,
    1128             :                         NET_TRANSPORT_RPC,
    1129             :                         N_("Create a service"),
    1130             :                         N_("net rpc service create\n"
    1131             :                            "    Creates a service")
    1132             :                 },
    1133             : 
    1134             :                 {NULL, NULL, 0, NULL, NULL}
    1135             :         };
    1136             : 
    1137           0 :         return net_run_function(c, argc, argv, "net rpc service",func);
    1138             : }

Generated by: LCOV version 1.14