3 * Unix SMB/CIFS implementation.
4 * MS-RPC client library implementation (SVCCTL pipe)
5 * Copyright (C) Chris Nicholls 2005.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "libsmb_internal.h"
25 #define WAIT_SLEEP_TIME 300000
27 int cac_SvcOpenScm( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
28 struct SvcOpenScm *op )
31 struct rpc_pipe_client *pipe_hnd = NULL;
34 POLICY_HND *scm_out = NULL;
39 if ( !hnd->_internal.ctx ) {
40 hnd->status = NT_STATUS_INVALID_HANDLE;
44 if ( !op || op->in.access == 0 || !mem_ctx ) {
45 hnd->status = NT_STATUS_INVALID_PARAMETER;
49 srv = cac_GetServer( hnd );
51 hnd->status = NT_STATUS_INVALID_CONNECTION;
55 /*initialize for samr pipe if we have to */
56 if ( !hnd->_internal.pipes[PI_SVCCTL] ) {
59 cli_rpc_pipe_open_noauth( srv->cli, PI_SVCCTL,
60 &( hnd->status ) ) ) ) {
61 hnd->status = NT_STATUS_UNSUCCESSFUL;
65 hnd->_internal.pipes[PI_SVCCTL] = True;
68 scm_out = talloc( mem_ctx, POLICY_HND );
70 hnd->status = NT_STATUS_NO_MEMORY;
74 err = rpccli_svcctl_open_scm( pipe_hnd, mem_ctx, scm_out,
76 hnd->status = werror_to_ntstatus( err );
78 if ( !NT_STATUS_IS_OK( hnd->status ) )
81 op->out.scm_hnd = scm_out;
86 int cac_SvcClose( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
87 POLICY_HND * scm_hnd )
89 struct rpc_pipe_client *pipe_hnd = NULL;
95 if ( !hnd->_internal.ctx ) {
96 hnd->status = NT_STATUS_INVALID_HANDLE;
100 if ( !scm_hnd || !mem_ctx ) {
101 hnd->status = NT_STATUS_INVALID_PARAMETER;
105 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
107 hnd->status = NT_STATUS_INVALID_HANDLE;
111 err = rpccli_svcctl_close_service( pipe_hnd, mem_ctx, scm_hnd );
112 hnd->status = werror_to_ntstatus( err );
114 if ( !NT_STATUS_IS_OK( hnd->status ) )
120 int cac_SvcEnumServices( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
121 struct SvcEnumServices *op )
123 struct rpc_pipe_client *pipe_hnd = NULL;
127 uint32 state_buf = 0;
129 uint32 num_svc_out = 0;
131 ENUM_SERVICES_STATUS *svc_buf = NULL;
136 if ( !hnd->_internal.ctx ) {
137 hnd->status = NT_STATUS_INVALID_HANDLE;
141 if ( !op || !op->in.scm_hnd || !mem_ctx ) {
142 hnd->status = NT_STATUS_INVALID_PARAMETER;
146 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
148 hnd->status = NT_STATUS_INVALID_HANDLE;
155 type : ( SVCCTL_TYPE_DRIVER | SVCCTL_TYPE_WIN32 );
156 state_buf = ( op->in.state != 0 ) ? op->in.state : SVCCTL_STATE_ALL;
158 err = rpccli_svcctl_enumerate_services( pipe_hnd, mem_ctx,
159 op->in.scm_hnd, type_buf,
160 state_buf, &num_svc_out,
162 hnd->status = werror_to_ntstatus( err );
164 if ( !NT_STATUS_IS_OK( hnd->status ) )
168 cac_MakeServiceArray( mem_ctx, svc_buf, num_svc_out );
170 if ( !op->out.services ) {
171 hnd->status = NT_STATUS_NO_MEMORY;
175 TALLOC_FREE( svc_buf );
177 op->out.num_services = num_svc_out;
182 int cac_SvcOpenService( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
183 struct SvcOpenService *op )
185 struct rpc_pipe_client *pipe_hnd = NULL;
188 POLICY_HND *svc_hnd_out = NULL;
193 if ( !hnd->_internal.ctx ) {
194 hnd->status = NT_STATUS_INVALID_HANDLE;
198 if ( !op || !op->in.scm_hnd || !op->in.name || !mem_ctx ) {
199 hnd->status = NT_STATUS_INVALID_PARAMETER;
203 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
205 hnd->status = NT_STATUS_INVALID_HANDLE;
209 svc_hnd_out = talloc( mem_ctx, POLICY_HND );
210 if ( !svc_hnd_out ) {
211 hnd->status = NT_STATUS_NO_MEMORY;
215 err = rpccli_svcctl_open_service( pipe_hnd, mem_ctx, op->in.scm_hnd,
216 svc_hnd_out, op->in.name,
218 hnd->status = werror_to_ntstatus( err );
220 if ( !NT_STATUS_IS_OK( hnd->status ) )
223 op->out.svc_hnd = svc_hnd_out;
228 int cac_SvcControlService( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
229 struct SvcControlService *op )
231 struct rpc_pipe_client *pipe_hnd = NULL;
234 SERVICE_STATUS status_out;
239 if ( !hnd->_internal.ctx ) {
240 hnd->status = NT_STATUS_INVALID_HANDLE;
244 if ( !op || !op->in.svc_hnd || !mem_ctx ) {
245 hnd->status = NT_STATUS_INVALID_PARAMETER;
249 if ( op->in.control < SVCCTL_CONTROL_STOP
250 || op->in.control > SVCCTL_CONTROL_SHUTDOWN ) {
251 hnd->status = NT_STATUS_INVALID_PARAMETER;
255 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
257 hnd->status = NT_STATUS_INVALID_HANDLE;
261 err = rpccli_svcctl_control_service( pipe_hnd, mem_ctx,
262 op->in.svc_hnd, op->in.control,
264 hnd->status = werror_to_ntstatus( err );
266 if ( !NT_STATUS_IS_OK( hnd->status ) )
272 int cac_SvcGetStatus( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
273 struct SvcGetStatus *op )
275 struct rpc_pipe_client *pipe_hnd = NULL;
278 SERVICE_STATUS status_out;
283 if ( !hnd->_internal.ctx ) {
284 hnd->status = NT_STATUS_INVALID_HANDLE;
288 if ( !op || !op->in.svc_hnd || !mem_ctx ) {
289 hnd->status = NT_STATUS_INVALID_PARAMETER;
293 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
295 hnd->status = NT_STATUS_INVALID_HANDLE;
299 err = rpccli_svcctl_query_status( pipe_hnd, mem_ctx, op->in.svc_hnd,
301 hnd->status = werror_to_ntstatus( err );
303 if ( !NT_STATUS_IS_OK( hnd->status ) )
306 op->out.status = status_out;
313 /*Internal function - similar to code found in utils/net_rpc_service.c
314 * Waits for a service to reach a specific state.
315 * svc_hnd - Handle to the service
316 * state - the state we are waiting for
317 * timeout - number of seconds to wait
318 * returns CAC_FAILURE if the state is never reached
319 * or CAC_SUCCESS if the state is reached
321 int cac_WaitForService( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
322 POLICY_HND * svc_hnd, uint32 state, uint32 timeout,
323 SERVICE_STATUS * status )
325 struct rpc_pipe_client *pipe_hnd = NULL;
327 /*number of milliseconds we have spent */
328 uint32 time_spent = 0;
331 if ( !hnd || !mem_ctx || !svc_hnd || !status )
334 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
336 hnd->status = NT_STATUS_INVALID_HANDLE;
340 while ( status->state != state && time_spent < ( timeout * 1000000 )
341 && NT_STATUS_IS_OK( hnd->status ) ) {
342 /*if this is the first call, then we _just_ got the status.. sleep now */
343 usleep( WAIT_SLEEP_TIME );
344 time_spent += WAIT_SLEEP_TIME;
346 err = rpccli_svcctl_query_status( pipe_hnd, mem_ctx, svc_hnd,
348 hnd->status = werror_to_ntstatus( err );
351 if ( status->state == state )
357 int cac_SvcStartService( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
358 struct SvcStartService *op )
360 struct rpc_pipe_client *pipe_hnd = NULL;
363 SERVICE_STATUS status_buf;
368 if ( !hnd->_internal.ctx ) {
369 hnd->status = NT_STATUS_INVALID_HANDLE;
373 if ( !op || !op->in.svc_hnd || !mem_ctx ) {
374 hnd->status = NT_STATUS_INVALID_PARAMETER;
378 if ( op->in.num_parms != 0 && op->in.parms == NULL ) {
379 hnd->status = NT_STATUS_INVALID_PARAMETER;
383 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
385 hnd->status = NT_STATUS_INVALID_HANDLE;
389 err = rpccli_svcctl_start_service( pipe_hnd, mem_ctx, op->in.svc_hnd,
390 ( const char ** ) op->in.parms,
392 hnd->status = werror_to_ntstatus( err );
394 if ( !NT_STATUS_IS_OK( hnd->status ) )
397 if ( op->in.timeout == 0 )
400 return cac_WaitForService( hnd, mem_ctx, op->in.svc_hnd,
401 SVCCTL_RUNNING, op->in.timeout,
405 int cac_SvcStopService( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
406 struct SvcStopService *op )
408 struct rpc_pipe_client *pipe_hnd = NULL;
411 SERVICE_STATUS status_out;
416 if ( !hnd->_internal.ctx ) {
417 hnd->status = NT_STATUS_INVALID_HANDLE;
421 if ( !op || !op->in.svc_hnd || !mem_ctx ) {
422 hnd->status = NT_STATUS_INVALID_PARAMETER;
426 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
428 hnd->status = NT_STATUS_INVALID_HANDLE;
432 err = rpccli_svcctl_control_service( pipe_hnd, mem_ctx,
436 hnd->status = werror_to_ntstatus( err );
438 if ( !NT_STATUS_IS_OK( hnd->status ) )
441 op->out.status = status_out;
443 if ( op->in.timeout == 0 )
446 return cac_WaitForService( hnd, mem_ctx, op->in.svc_hnd,
447 SVCCTL_STOPPED, op->in.timeout,
451 int cac_SvcPauseService( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
452 struct SvcPauseService *op )
454 struct rpc_pipe_client *pipe_hnd = NULL;
457 SERVICE_STATUS status_out;
462 if ( !hnd->_internal.ctx ) {
463 hnd->status = NT_STATUS_INVALID_HANDLE;
467 if ( !op || !op->in.svc_hnd || !mem_ctx ) {
468 hnd->status = NT_STATUS_INVALID_PARAMETER;
472 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
474 hnd->status = NT_STATUS_INVALID_HANDLE;
478 err = rpccli_svcctl_control_service( pipe_hnd, mem_ctx,
480 SVCCTL_CONTROL_PAUSE,
482 hnd->status = werror_to_ntstatus( err );
484 if ( !NT_STATUS_IS_OK( hnd->status ) )
487 op->out.status = status_out;
489 if ( op->in.timeout == 0 )
492 return cac_WaitForService( hnd, mem_ctx, op->in.svc_hnd,
493 SVCCTL_PAUSED, op->in.timeout,
497 int cac_SvcContinueService( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
498 struct SvcContinueService *op )
500 struct rpc_pipe_client *pipe_hnd = NULL;
503 SERVICE_STATUS status_out;
508 if ( !hnd->_internal.ctx ) {
509 hnd->status = NT_STATUS_INVALID_HANDLE;
513 if ( !op || !op->in.svc_hnd || !mem_ctx ) {
514 hnd->status = NT_STATUS_INVALID_PARAMETER;
518 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
520 hnd->status = NT_STATUS_INVALID_HANDLE;
524 err = rpccli_svcctl_control_service( pipe_hnd, mem_ctx,
526 SVCCTL_CONTROL_CONTINUE,
528 hnd->status = werror_to_ntstatus( err );
530 if ( !NT_STATUS_IS_OK( hnd->status ) )
533 op->out.status = status_out;
535 if ( op->in.timeout == 0 )
538 return cac_WaitForService( hnd, mem_ctx, op->in.svc_hnd,
539 SVCCTL_RUNNING, op->in.timeout,
543 int cac_SvcGetDisplayName( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
544 struct SvcGetDisplayName *op )
546 struct rpc_pipe_client *pipe_hnd = NULL;
549 fstring disp_name_out;
554 if ( !hnd->_internal.ctx ) {
555 hnd->status = NT_STATUS_INVALID_HANDLE;
559 if ( !op || !op->in.svc_hnd || !mem_ctx ) {
560 hnd->status = NT_STATUS_INVALID_PARAMETER;
564 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
566 hnd->status = NT_STATUS_INVALID_HANDLE;
570 err = rpccli_svcctl_get_dispname( pipe_hnd, mem_ctx, op->in.svc_hnd,
572 hnd->status = werror_to_ntstatus( err );
574 if ( !NT_STATUS_IS_OK( hnd->status ) )
577 op->out.display_name = talloc_strdup( mem_ctx, disp_name_out );
579 if ( !op->out.display_name ) {
580 hnd->status = NT_STATUS_NO_MEMORY;
588 int cac_SvcGetServiceConfig( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
589 struct SvcGetServiceConfig *op )
591 struct rpc_pipe_client *pipe_hnd = NULL;
594 SERVICE_CONFIG config_out;
599 if ( !hnd->_internal.ctx ) {
600 hnd->status = NT_STATUS_INVALID_HANDLE;
604 if ( !op || !op->in.svc_hnd || !mem_ctx ) {
605 hnd->status = NT_STATUS_INVALID_PARAMETER;
609 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
611 hnd->status = NT_STATUS_INVALID_HANDLE;
615 err = rpccli_svcctl_query_config( pipe_hnd, mem_ctx, op->in.svc_hnd,
617 hnd->status = werror_to_ntstatus( err );
619 if ( !NT_STATUS_IS_OK( hnd->status ) )
622 if ( !cac_InitCacServiceConfig
623 ( mem_ctx, &config_out, &op->out.config ) ) {
624 hnd->status = NT_STATUS_NO_MEMORY;