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 3 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_WaitForService( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
28 POLICY_HND * svc_hnd, uint32 state, uint32 timeout,
29 SERVICE_STATUS * status );
31 int cac_SvcOpenScm( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
32 struct SvcOpenScm *op )
35 struct rpc_pipe_client *pipe_hnd = NULL;
38 POLICY_HND *scm_out = NULL;
43 if ( !hnd->_internal.ctx ) {
44 hnd->status = NT_STATUS_INVALID_HANDLE;
48 if ( !op || op->in.access == 0 || !mem_ctx ) {
49 hnd->status = NT_STATUS_INVALID_PARAMETER;
53 srv = cac_GetServer( hnd );
55 hnd->status = NT_STATUS_INVALID_CONNECTION;
59 /*initialize for samr pipe if we have to */
60 if ( !hnd->_internal.pipes[PI_SVCCTL] ) {
63 cli_rpc_pipe_open_noauth( srv->cli, PI_SVCCTL,
64 &( hnd->status ) ) ) ) {
65 hnd->status = NT_STATUS_UNSUCCESSFUL;
69 hnd->_internal.pipes[PI_SVCCTL] = True;
72 scm_out = talloc( mem_ctx, POLICY_HND );
74 hnd->status = NT_STATUS_NO_MEMORY;
78 err = rpccli_svcctl_open_scm( pipe_hnd, mem_ctx, scm_out,
80 hnd->status = werror_to_ntstatus( err );
82 if ( !NT_STATUS_IS_OK( hnd->status ) )
85 op->out.scm_hnd = scm_out;
90 int cac_SvcClose( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
91 POLICY_HND * scm_hnd )
93 struct rpc_pipe_client *pipe_hnd = NULL;
98 if ( !hnd->_internal.ctx ) {
99 hnd->status = NT_STATUS_INVALID_HANDLE;
103 if ( !scm_hnd || !mem_ctx ) {
104 hnd->status = NT_STATUS_INVALID_PARAMETER;
108 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
110 hnd->status = NT_STATUS_INVALID_HANDLE;
114 hnd->status = rpccli_svcctl_CloseServiceHandle( pipe_hnd, mem_ctx, scm_hnd );
116 if ( !NT_STATUS_IS_OK( hnd->status ) )
122 int cac_SvcEnumServices( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
123 struct SvcEnumServices *op )
125 struct rpc_pipe_client *pipe_hnd = NULL;
129 uint32 state_buf = 0;
131 uint32 num_svc_out = 0;
133 ENUM_SERVICES_STATUS *svc_buf = NULL;
138 if ( !hnd->_internal.ctx ) {
139 hnd->status = NT_STATUS_INVALID_HANDLE;
143 if ( !op || !op->in.scm_hnd || !mem_ctx ) {
144 hnd->status = NT_STATUS_INVALID_PARAMETER;
148 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
150 hnd->status = NT_STATUS_INVALID_HANDLE;
157 type : ( SVCCTL_TYPE_DRIVER | SVCCTL_TYPE_WIN32 );
158 state_buf = ( op->in.state != 0 ) ? op->in.state : SVCCTL_STATE_ALL;
160 err = rpccli_svcctl_enumerate_services( pipe_hnd, mem_ctx,
161 op->in.scm_hnd, type_buf,
162 state_buf, &num_svc_out,
164 hnd->status = werror_to_ntstatus( err );
166 if ( !NT_STATUS_IS_OK( hnd->status ) )
170 cac_MakeServiceArray( mem_ctx, svc_buf, num_svc_out );
172 if ( !op->out.services ) {
173 hnd->status = NT_STATUS_NO_MEMORY;
177 TALLOC_FREE( svc_buf );
179 op->out.num_services = num_svc_out;
184 int cac_SvcOpenService( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
185 struct SvcOpenService *op )
187 struct rpc_pipe_client *pipe_hnd = NULL;
190 POLICY_HND *svc_hnd_out = NULL;
195 if ( !hnd->_internal.ctx ) {
196 hnd->status = NT_STATUS_INVALID_HANDLE;
200 if ( !op || !op->in.scm_hnd || !op->in.name || !mem_ctx ) {
201 hnd->status = NT_STATUS_INVALID_PARAMETER;
205 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
207 hnd->status = NT_STATUS_INVALID_HANDLE;
211 svc_hnd_out = talloc( mem_ctx, POLICY_HND );
212 if ( !svc_hnd_out ) {
213 hnd->status = NT_STATUS_NO_MEMORY;
217 err = rpccli_svcctl_open_service( pipe_hnd, mem_ctx, op->in.scm_hnd,
218 svc_hnd_out, op->in.name,
220 hnd->status = werror_to_ntstatus( err );
222 if ( !NT_STATUS_IS_OK( hnd->status ) )
225 op->out.svc_hnd = svc_hnd_out;
230 int cac_SvcControlService( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
231 struct SvcControlService *op )
233 struct rpc_pipe_client *pipe_hnd = NULL;
236 SERVICE_STATUS status_out;
241 if ( !hnd->_internal.ctx ) {
242 hnd->status = NT_STATUS_INVALID_HANDLE;
246 if ( !op || !op->in.svc_hnd || !mem_ctx ) {
247 hnd->status = NT_STATUS_INVALID_PARAMETER;
251 if ( op->in.control < SVCCTL_CONTROL_STOP
252 || op->in.control > SVCCTL_CONTROL_SHUTDOWN ) {
253 hnd->status = NT_STATUS_INVALID_PARAMETER;
257 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
259 hnd->status = NT_STATUS_INVALID_HANDLE;
263 err = rpccli_svcctl_control_service( pipe_hnd, mem_ctx,
264 op->in.svc_hnd, op->in.control,
266 hnd->status = werror_to_ntstatus( err );
268 if ( !NT_STATUS_IS_OK( hnd->status ) )
274 int cac_SvcGetStatus( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
275 struct SvcGetStatus *op )
277 struct rpc_pipe_client *pipe_hnd = NULL;
280 SERVICE_STATUS status_out;
285 if ( !hnd->_internal.ctx ) {
286 hnd->status = NT_STATUS_INVALID_HANDLE;
290 if ( !op || !op->in.svc_hnd || !mem_ctx ) {
291 hnd->status = NT_STATUS_INVALID_PARAMETER;
295 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
297 hnd->status = NT_STATUS_INVALID_HANDLE;
301 err = rpccli_svcctl_query_status( pipe_hnd, mem_ctx, op->in.svc_hnd,
303 hnd->status = werror_to_ntstatus( err );
305 if ( !NT_STATUS_IS_OK( hnd->status ) )
308 op->out.status = status_out;
315 /*Internal function - similar to code found in utils/net_rpc_service.c
316 * Waits for a service to reach a specific state.
317 * svc_hnd - Handle to the service
318 * state - the state we are waiting for
319 * timeout - number of seconds to wait
320 * returns CAC_FAILURE if the state is never reached
321 * or CAC_SUCCESS if the state is reached
323 int cac_WaitForService( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
324 POLICY_HND * svc_hnd, uint32 state, uint32 timeout,
325 SERVICE_STATUS * status )
327 struct rpc_pipe_client *pipe_hnd = NULL;
329 /*number of milliseconds we have spent */
330 uint32 time_spent = 0;
333 if ( !hnd || !mem_ctx || !svc_hnd || !status )
336 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
338 hnd->status = NT_STATUS_INVALID_HANDLE;
342 while ( status->state != state && time_spent < ( timeout * 1000000 )
343 && NT_STATUS_IS_OK( hnd->status ) ) {
344 /*if this is the first call, then we _just_ got the status.. sleep now */
345 usleep( WAIT_SLEEP_TIME );
346 time_spent += WAIT_SLEEP_TIME;
348 err = rpccli_svcctl_query_status( pipe_hnd, mem_ctx, svc_hnd,
350 hnd->status = werror_to_ntstatus( err );
353 if ( status->state == state )
359 int cac_SvcStartService( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
360 struct SvcStartService *op )
362 struct rpc_pipe_client *pipe_hnd = NULL;
365 SERVICE_STATUS status_buf;
370 if ( !hnd->_internal.ctx ) {
371 hnd->status = NT_STATUS_INVALID_HANDLE;
375 if ( !op || !op->in.svc_hnd || !mem_ctx ) {
376 hnd->status = NT_STATUS_INVALID_PARAMETER;
380 if ( op->in.num_parms != 0 && op->in.parms == NULL ) {
381 hnd->status = NT_STATUS_INVALID_PARAMETER;
385 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
387 hnd->status = NT_STATUS_INVALID_HANDLE;
391 err = rpccli_svcctl_start_service( pipe_hnd, mem_ctx, op->in.svc_hnd,
392 ( const char ** ) op->in.parms,
394 hnd->status = werror_to_ntstatus( err );
396 if ( !NT_STATUS_IS_OK( hnd->status ) )
399 if ( op->in.timeout == 0 )
402 return cac_WaitForService( hnd, mem_ctx, op->in.svc_hnd,
403 SVCCTL_RUNNING, op->in.timeout,
407 int cac_SvcStopService( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
408 struct SvcStopService *op )
410 struct rpc_pipe_client *pipe_hnd = NULL;
413 SERVICE_STATUS status_out;
418 if ( !hnd->_internal.ctx ) {
419 hnd->status = NT_STATUS_INVALID_HANDLE;
423 if ( !op || !op->in.svc_hnd || !mem_ctx ) {
424 hnd->status = NT_STATUS_INVALID_PARAMETER;
428 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
430 hnd->status = NT_STATUS_INVALID_HANDLE;
434 err = rpccli_svcctl_control_service( pipe_hnd, mem_ctx,
438 hnd->status = werror_to_ntstatus( err );
440 if ( !NT_STATUS_IS_OK( hnd->status ) )
443 op->out.status = status_out;
445 if ( op->in.timeout == 0 )
448 return cac_WaitForService( hnd, mem_ctx, op->in.svc_hnd,
449 SVCCTL_STOPPED, op->in.timeout,
453 int cac_SvcPauseService( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
454 struct SvcPauseService *op )
456 struct rpc_pipe_client *pipe_hnd = NULL;
459 SERVICE_STATUS status_out;
464 if ( !hnd->_internal.ctx ) {
465 hnd->status = NT_STATUS_INVALID_HANDLE;
469 if ( !op || !op->in.svc_hnd || !mem_ctx ) {
470 hnd->status = NT_STATUS_INVALID_PARAMETER;
474 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
476 hnd->status = NT_STATUS_INVALID_HANDLE;
480 err = rpccli_svcctl_control_service( pipe_hnd, mem_ctx,
482 SVCCTL_CONTROL_PAUSE,
484 hnd->status = werror_to_ntstatus( err );
486 if ( !NT_STATUS_IS_OK( hnd->status ) )
489 op->out.status = status_out;
491 if ( op->in.timeout == 0 )
494 return cac_WaitForService( hnd, mem_ctx, op->in.svc_hnd,
495 SVCCTL_PAUSED, op->in.timeout,
499 int cac_SvcContinueService( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
500 struct SvcContinueService *op )
502 struct rpc_pipe_client *pipe_hnd = NULL;
505 SERVICE_STATUS status_out;
510 if ( !hnd->_internal.ctx ) {
511 hnd->status = NT_STATUS_INVALID_HANDLE;
515 if ( !op || !op->in.svc_hnd || !mem_ctx ) {
516 hnd->status = NT_STATUS_INVALID_PARAMETER;
520 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
522 hnd->status = NT_STATUS_INVALID_HANDLE;
526 err = rpccli_svcctl_control_service( pipe_hnd, mem_ctx,
528 SVCCTL_CONTROL_CONTINUE,
530 hnd->status = werror_to_ntstatus( err );
532 if ( !NT_STATUS_IS_OK( hnd->status ) )
535 op->out.status = status_out;
537 if ( op->in.timeout == 0 )
540 return cac_WaitForService( hnd, mem_ctx, op->in.svc_hnd,
541 SVCCTL_RUNNING, op->in.timeout,
545 int cac_SvcGetDisplayName( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
546 struct SvcGetDisplayName *op )
548 struct rpc_pipe_client *pipe_hnd = NULL;
551 fstring disp_name_out;
556 if ( !hnd->_internal.ctx ) {
557 hnd->status = NT_STATUS_INVALID_HANDLE;
561 if ( !op || !op->in.svc_hnd || !mem_ctx ) {
562 hnd->status = NT_STATUS_INVALID_PARAMETER;
566 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
568 hnd->status = NT_STATUS_INVALID_HANDLE;
572 err = rpccli_svcctl_get_dispname( pipe_hnd, mem_ctx, op->in.svc_hnd,
574 hnd->status = werror_to_ntstatus( err );
576 if ( !NT_STATUS_IS_OK( hnd->status ) )
579 op->out.display_name = talloc_strdup( mem_ctx, disp_name_out );
581 if ( !op->out.display_name ) {
582 hnd->status = NT_STATUS_NO_MEMORY;
590 int cac_SvcGetServiceConfig( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
591 struct SvcGetServiceConfig *op )
593 struct rpc_pipe_client *pipe_hnd = NULL;
596 SERVICE_CONFIG config_out;
601 if ( !hnd->_internal.ctx ) {
602 hnd->status = NT_STATUS_INVALID_HANDLE;
606 if ( !op || !op->in.svc_hnd || !mem_ctx ) {
607 hnd->status = NT_STATUS_INVALID_PARAMETER;
611 pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
613 hnd->status = NT_STATUS_INVALID_HANDLE;
617 err = rpccli_svcctl_query_config( pipe_hnd, mem_ctx, op->in.svc_hnd,
619 hnd->status = werror_to_ntstatus( err );
621 if ( !NT_STATUS_IS_OK( hnd->status ) )
624 if ( !cac_InitCacServiceConfig
625 ( mem_ctx, &config_out, &op->out.config ) ) {
626 hnd->status = NT_STATUS_NO_MEMORY;