9f61b421298d1db4c5583765cf40bce143d4a66c
[tprouty/samba.git] / source / libmsrpc / cac_svcctl.c
1
2 /* 
3  *  Unix SMB/CIFS implementation.
4  *  MS-RPC client library implementation (SVCCTL pipe)
5  *  Copyright (C) Chris Nicholls              2005.
6  *  
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.
11  *  
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.
16  *  
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.
20  */
21
22 #include "libmsrpc.h"
23 #include "libsmb_internal.h"
24
25 #define WAIT_SLEEP_TIME 300000
26
27 int cac_SvcOpenScm( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
28                     struct SvcOpenScm *op )
29 {
30         SMBCSRV *srv = NULL;
31         struct rpc_pipe_client *pipe_hnd = NULL;
32         WERROR err;
33
34         POLICY_HND *scm_out = NULL;
35
36         if ( !hnd )
37                 return CAC_FAILURE;
38
39         if ( !hnd->_internal.ctx ) {
40                 hnd->status = NT_STATUS_INVALID_HANDLE;
41                 return CAC_FAILURE;
42         }
43
44         if ( !op || op->in.access == 0 || !mem_ctx ) {
45                 hnd->status = NT_STATUS_INVALID_PARAMETER;
46                 return CAC_FAILURE;
47         }
48
49         srv = cac_GetServer( hnd );
50         if ( !srv ) {
51                 hnd->status = NT_STATUS_INVALID_CONNECTION;
52                 return CAC_FAILURE;
53         }
54
55         /*initialize for samr pipe if we have to */
56         if ( !hnd->_internal.pipes[PI_SVCCTL] ) {
57                 if ( !
58                      ( pipe_hnd =
59                        cli_rpc_pipe_open_noauth( srv->cli, PI_SVCCTL,
60                                                  &( hnd->status ) ) ) ) {
61                         hnd->status = NT_STATUS_UNSUCCESSFUL;
62                         return CAC_FAILURE;
63                 }
64
65                 hnd->_internal.pipes[PI_SVCCTL] = True;
66         }
67
68         scm_out = talloc( mem_ctx, POLICY_HND );
69         if ( !scm_out ) {
70                 hnd->status = NT_STATUS_NO_MEMORY;
71                 return CAC_FAILURE;
72         }
73
74         err = rpccli_svcctl_open_scm( pipe_hnd, mem_ctx, scm_out,
75                                       op->in.access );
76         hnd->status = werror_to_ntstatus( err );
77
78         if ( !NT_STATUS_IS_OK( hnd->status ) )
79                 return CAC_FAILURE;
80
81         op->out.scm_hnd = scm_out;
82
83         return CAC_SUCCESS;
84 }
85
86 int cac_SvcClose( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
87                   POLICY_HND * scm_hnd )
88 {
89         struct rpc_pipe_client *pipe_hnd = NULL;
90         WERROR err;
91
92         if ( !hnd )
93                 return CAC_FAILURE;
94
95         if ( !hnd->_internal.ctx ) {
96                 hnd->status = NT_STATUS_INVALID_HANDLE;
97                 return CAC_FAILURE;
98         }
99
100         if ( !scm_hnd || !mem_ctx ) {
101                 hnd->status = NT_STATUS_INVALID_PARAMETER;
102                 return CAC_FAILURE;
103         }
104
105         pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
106         if ( !pipe_hnd ) {
107                 hnd->status = NT_STATUS_INVALID_HANDLE;
108                 return CAC_FAILURE;
109         }
110
111         err = rpccli_svcctl_close_service( pipe_hnd, mem_ctx, scm_hnd );
112         hnd->status = werror_to_ntstatus( err );
113
114         if ( !NT_STATUS_IS_OK( hnd->status ) )
115                 return CAC_FAILURE;
116
117         return CAC_SUCCESS;
118 }
119
120 int cac_SvcEnumServices( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
121                          struct SvcEnumServices *op )
122 {
123         struct rpc_pipe_client *pipe_hnd = NULL;
124         WERROR err;
125
126         uint32 type_buf = 0;
127         uint32 state_buf = 0;
128
129         uint32 num_svc_out = 0;
130
131         ENUM_SERVICES_STATUS *svc_buf = NULL;
132
133         if ( !hnd )
134                 return CAC_FAILURE;
135
136         if ( !hnd->_internal.ctx ) {
137                 hnd->status = NT_STATUS_INVALID_HANDLE;
138                 return CAC_FAILURE;
139         }
140
141         if ( !op || !op->in.scm_hnd || !mem_ctx ) {
142                 hnd->status = NT_STATUS_INVALID_PARAMETER;
143                 return CAC_FAILURE;
144         }
145
146         pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
147         if ( !pipe_hnd ) {
148                 hnd->status = NT_STATUS_INVALID_HANDLE;
149                 return CAC_FAILURE;
150         }
151
152         type_buf =
153                 ( op->in.type !=
154                   0 ) ? op->in.
155                 type : ( SVCCTL_TYPE_DRIVER | SVCCTL_TYPE_WIN32 );
156         state_buf = ( op->in.state != 0 ) ? op->in.state : SVCCTL_STATE_ALL;
157
158         err = rpccli_svcctl_enumerate_services( pipe_hnd, mem_ctx,
159                                                 op->in.scm_hnd, type_buf,
160                                                 state_buf, &num_svc_out,
161                                                 &svc_buf );
162         hnd->status = werror_to_ntstatus( err );
163
164         if ( !NT_STATUS_IS_OK( hnd->status ) )
165                 return CAC_FAILURE;
166
167         op->out.services =
168                 cac_MakeServiceArray( mem_ctx, svc_buf, num_svc_out );
169
170         if ( !op->out.services ) {
171                 hnd->status = NT_STATUS_NO_MEMORY;
172                 return CAC_FAILURE;
173         }
174
175         TALLOC_FREE( svc_buf );
176
177         op->out.num_services = num_svc_out;
178
179         return CAC_SUCCESS;
180 }
181
182 int cac_SvcOpenService( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
183                         struct SvcOpenService *op )
184 {
185         struct rpc_pipe_client *pipe_hnd = NULL;
186         WERROR err;
187
188         POLICY_HND *svc_hnd_out = NULL;
189
190         if ( !hnd )
191                 return CAC_FAILURE;
192
193         if ( !hnd->_internal.ctx ) {
194                 hnd->status = NT_STATUS_INVALID_HANDLE;
195                 return CAC_FAILURE;
196         }
197
198         if ( !op || !op->in.scm_hnd || !op->in.name || !mem_ctx ) {
199                 hnd->status = NT_STATUS_INVALID_PARAMETER;
200                 return CAC_FAILURE;
201         }
202
203         pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
204         if ( !pipe_hnd ) {
205                 hnd->status = NT_STATUS_INVALID_HANDLE;
206                 return CAC_FAILURE;
207         }
208
209         svc_hnd_out = talloc( mem_ctx, POLICY_HND );
210         if ( !svc_hnd_out ) {
211                 hnd->status = NT_STATUS_NO_MEMORY;
212                 return CAC_FAILURE;
213         }
214
215         err = rpccli_svcctl_open_service( pipe_hnd, mem_ctx, op->in.scm_hnd,
216                                           svc_hnd_out, op->in.name,
217                                           op->in.access );
218         hnd->status = werror_to_ntstatus( err );
219
220         if ( !NT_STATUS_IS_OK( hnd->status ) )
221                 return CAC_FAILURE;
222
223         op->out.svc_hnd = svc_hnd_out;
224
225         return CAC_SUCCESS;
226 }
227
228 int cac_SvcControlService( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
229                            struct SvcControlService *op )
230 {
231         struct rpc_pipe_client *pipe_hnd = NULL;
232         WERROR err;
233
234         SERVICE_STATUS status_out;
235
236         if ( !hnd )
237                 return CAC_FAILURE;
238
239         if ( !hnd->_internal.ctx ) {
240                 hnd->status = NT_STATUS_INVALID_HANDLE;
241                 return CAC_FAILURE;
242         }
243
244         if ( !op || !op->in.svc_hnd || !mem_ctx ) {
245                 hnd->status = NT_STATUS_INVALID_PARAMETER;
246                 return CAC_FAILURE;
247         }
248
249         if ( op->in.control < SVCCTL_CONTROL_STOP
250              || op->in.control > SVCCTL_CONTROL_SHUTDOWN ) {
251                 hnd->status = NT_STATUS_INVALID_PARAMETER;
252                 return CAC_FAILURE;
253         }
254
255         pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
256         if ( !pipe_hnd ) {
257                 hnd->status = NT_STATUS_INVALID_HANDLE;
258                 return CAC_FAILURE;
259         }
260
261         err = rpccli_svcctl_control_service( pipe_hnd, mem_ctx,
262                                              op->in.svc_hnd, op->in.control,
263                                              &status_out );
264         hnd->status = werror_to_ntstatus( err );
265
266         if ( !NT_STATUS_IS_OK( hnd->status ) )
267                 return CAC_FAILURE;
268
269         return CAC_SUCCESS;
270 }
271
272 int cac_SvcGetStatus( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
273                       struct SvcGetStatus *op )
274 {
275         struct rpc_pipe_client *pipe_hnd = NULL;
276         WERROR err;
277
278         SERVICE_STATUS status_out;
279
280         if ( !hnd )
281                 return CAC_FAILURE;
282
283         if ( !hnd->_internal.ctx ) {
284                 hnd->status = NT_STATUS_INVALID_HANDLE;
285                 return CAC_FAILURE;
286         }
287
288         if ( !op || !op->in.svc_hnd || !mem_ctx ) {
289                 hnd->status = NT_STATUS_INVALID_PARAMETER;
290                 return CAC_FAILURE;
291         }
292
293         pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
294         if ( !pipe_hnd ) {
295                 hnd->status = NT_STATUS_INVALID_HANDLE;
296                 return CAC_FAILURE;
297         }
298
299         err = rpccli_svcctl_query_status( pipe_hnd, mem_ctx, op->in.svc_hnd,
300                                           &status_out );
301         hnd->status = werror_to_ntstatus( err );
302
303         if ( !NT_STATUS_IS_OK( hnd->status ) )
304                 return CAC_FAILURE;
305
306         op->out.status = status_out;
307
308         return CAC_SUCCESS;
309 }
310
311
312
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
320  */
321 int cac_WaitForService( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
322                         POLICY_HND * svc_hnd, uint32 state, uint32 timeout,
323                         SERVICE_STATUS * status )
324 {
325         struct rpc_pipe_client *pipe_hnd = NULL;
326
327         /*number of milliseconds we have spent */
328         uint32 time_spent = 0;
329         WERROR err;
330
331         if ( !hnd || !mem_ctx || !svc_hnd || !status )
332                 return CAC_FAILURE;
333
334         pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
335         if ( !pipe_hnd ) {
336                 hnd->status = NT_STATUS_INVALID_HANDLE;
337                 return CAC_FAILURE;
338         }
339
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;
345
346                 err = rpccli_svcctl_query_status( pipe_hnd, mem_ctx, svc_hnd,
347                                                   status );
348                 hnd->status = werror_to_ntstatus( err );
349         }
350
351         if ( status->state == state )
352                 return CAC_SUCCESS;
353
354         return CAC_FAILURE;
355 }
356
357 int cac_SvcStartService( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
358                          struct SvcStartService *op )
359 {
360         struct rpc_pipe_client *pipe_hnd = NULL;
361         WERROR err;
362
363         SERVICE_STATUS status_buf;
364
365         if ( !hnd )
366                 return CAC_FAILURE;
367
368         if ( !hnd->_internal.ctx ) {
369                 hnd->status = NT_STATUS_INVALID_HANDLE;
370                 return CAC_FAILURE;
371         }
372
373         if ( !op || !op->in.svc_hnd || !mem_ctx ) {
374                 hnd->status = NT_STATUS_INVALID_PARAMETER;
375                 return CAC_FAILURE;
376         }
377
378         if ( op->in.num_parms != 0 && op->in.parms == NULL ) {
379                 hnd->status = NT_STATUS_INVALID_PARAMETER;
380                 return CAC_FAILURE;
381         }
382
383         pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
384         if ( !pipe_hnd ) {
385                 hnd->status = NT_STATUS_INVALID_HANDLE;
386                 return CAC_FAILURE;
387         }
388
389         err = rpccli_svcctl_start_service( pipe_hnd, mem_ctx, op->in.svc_hnd,
390                                            ( const char ** ) op->in.parms,
391                                            op->in.num_parms );
392         hnd->status = werror_to_ntstatus( err );
393
394         if ( !NT_STATUS_IS_OK( hnd->status ) )
395                 return CAC_FAILURE;
396
397         if ( op->in.timeout == 0 )
398                 return CAC_SUCCESS;
399
400         return cac_WaitForService( hnd, mem_ctx, op->in.svc_hnd,
401                                    SVCCTL_RUNNING, op->in.timeout,
402                                    &status_buf );
403 }
404
405 int cac_SvcStopService( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
406                         struct SvcStopService *op )
407 {
408         struct rpc_pipe_client *pipe_hnd = NULL;
409         WERROR err;
410
411         SERVICE_STATUS status_out;
412
413         if ( !hnd )
414                 return CAC_FAILURE;
415
416         if ( !hnd->_internal.ctx ) {
417                 hnd->status = NT_STATUS_INVALID_HANDLE;
418                 return CAC_FAILURE;
419         }
420
421         if ( !op || !op->in.svc_hnd || !mem_ctx ) {
422                 hnd->status = NT_STATUS_INVALID_PARAMETER;
423                 return CAC_FAILURE;
424         }
425
426         pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
427         if ( !pipe_hnd ) {
428                 hnd->status = NT_STATUS_INVALID_HANDLE;
429                 return CAC_FAILURE;
430         }
431
432         err = rpccli_svcctl_control_service( pipe_hnd, mem_ctx,
433                                              op->in.svc_hnd,
434                                              SVCCTL_CONTROL_STOP,
435                                              &status_out );
436         hnd->status = werror_to_ntstatus( err );
437
438         if ( !NT_STATUS_IS_OK( hnd->status ) )
439                 return CAC_FAILURE;
440
441         op->out.status = status_out;
442
443         if ( op->in.timeout == 0 )
444                 return CAC_SUCCESS;
445
446         return cac_WaitForService( hnd, mem_ctx, op->in.svc_hnd,
447                                    SVCCTL_STOPPED, op->in.timeout,
448                                    &op->out.status );
449 }
450
451 int cac_SvcPauseService( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
452                          struct SvcPauseService *op )
453 {
454         struct rpc_pipe_client *pipe_hnd = NULL;
455         WERROR err;
456
457         SERVICE_STATUS status_out;
458
459         if ( !hnd )
460                 return CAC_FAILURE;
461
462         if ( !hnd->_internal.ctx ) {
463                 hnd->status = NT_STATUS_INVALID_HANDLE;
464                 return CAC_FAILURE;
465         }
466
467         if ( !op || !op->in.svc_hnd || !mem_ctx ) {
468                 hnd->status = NT_STATUS_INVALID_PARAMETER;
469                 return CAC_FAILURE;
470         }
471
472         pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
473         if ( !pipe_hnd ) {
474                 hnd->status = NT_STATUS_INVALID_HANDLE;
475                 return CAC_FAILURE;
476         }
477
478         err = rpccli_svcctl_control_service( pipe_hnd, mem_ctx,
479                                              op->in.svc_hnd,
480                                              SVCCTL_CONTROL_PAUSE,
481                                              &status_out );
482         hnd->status = werror_to_ntstatus( err );
483
484         if ( !NT_STATUS_IS_OK( hnd->status ) )
485                 return CAC_FAILURE;
486
487         op->out.status = status_out;
488
489         if ( op->in.timeout == 0 )
490                 return CAC_SUCCESS;
491
492         return cac_WaitForService( hnd, mem_ctx, op->in.svc_hnd,
493                                    SVCCTL_PAUSED, op->in.timeout,
494                                    &op->out.status );
495 }
496
497 int cac_SvcContinueService( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
498                             struct SvcContinueService *op )
499 {
500         struct rpc_pipe_client *pipe_hnd = NULL;
501         WERROR err;
502
503         SERVICE_STATUS status_out;
504
505         if ( !hnd )
506                 return CAC_FAILURE;
507
508         if ( !hnd->_internal.ctx ) {
509                 hnd->status = NT_STATUS_INVALID_HANDLE;
510                 return CAC_FAILURE;
511         }
512
513         if ( !op || !op->in.svc_hnd || !mem_ctx ) {
514                 hnd->status = NT_STATUS_INVALID_PARAMETER;
515                 return CAC_FAILURE;
516         }
517
518         pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
519         if ( !pipe_hnd ) {
520                 hnd->status = NT_STATUS_INVALID_HANDLE;
521                 return CAC_FAILURE;
522         }
523
524         err = rpccli_svcctl_control_service( pipe_hnd, mem_ctx,
525                                              op->in.svc_hnd,
526                                              SVCCTL_CONTROL_CONTINUE,
527                                              &status_out );
528         hnd->status = werror_to_ntstatus( err );
529
530         if ( !NT_STATUS_IS_OK( hnd->status ) )
531                 return CAC_FAILURE;
532
533         op->out.status = status_out;
534
535         if ( op->in.timeout == 0 )
536                 return CAC_SUCCESS;
537
538         return cac_WaitForService( hnd, mem_ctx, op->in.svc_hnd,
539                                    SVCCTL_RUNNING, op->in.timeout,
540                                    &op->out.status );
541 }
542
543 int cac_SvcGetDisplayName( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
544                            struct SvcGetDisplayName *op )
545 {
546         struct rpc_pipe_client *pipe_hnd = NULL;
547         WERROR err;
548
549         fstring disp_name_out;
550
551         if ( !hnd )
552                 return CAC_FAILURE;
553
554         if ( !hnd->_internal.ctx ) {
555                 hnd->status = NT_STATUS_INVALID_HANDLE;
556                 return CAC_FAILURE;
557         }
558
559         if ( !op || !op->in.svc_hnd || !mem_ctx ) {
560                 hnd->status = NT_STATUS_INVALID_PARAMETER;
561                 return CAC_FAILURE;
562         }
563
564         pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
565         if ( !pipe_hnd ) {
566                 hnd->status = NT_STATUS_INVALID_HANDLE;
567                 return CAC_FAILURE;
568         }
569
570         err = rpccli_svcctl_get_dispname( pipe_hnd, mem_ctx, op->in.svc_hnd,
571                                           disp_name_out );
572         hnd->status = werror_to_ntstatus( err );
573
574         if ( !NT_STATUS_IS_OK( hnd->status ) )
575                 return CAC_FAILURE;
576
577         op->out.display_name = talloc_strdup( mem_ctx, disp_name_out );
578
579         if ( !op->out.display_name ) {
580                 hnd->status = NT_STATUS_NO_MEMORY;
581                 return CAC_FAILURE;
582         }
583
584         return CAC_SUCCESS;
585 }
586
587
588 int cac_SvcGetServiceConfig( CacServerHandle * hnd, TALLOC_CTX * mem_ctx,
589                              struct SvcGetServiceConfig *op )
590 {
591         struct rpc_pipe_client *pipe_hnd = NULL;
592         WERROR err;
593
594         SERVICE_CONFIG config_out;
595
596         if ( !hnd )
597                 return CAC_FAILURE;
598
599         if ( !hnd->_internal.ctx ) {
600                 hnd->status = NT_STATUS_INVALID_HANDLE;
601                 return CAC_FAILURE;
602         }
603
604         if ( !op || !op->in.svc_hnd || !mem_ctx ) {
605                 hnd->status = NT_STATUS_INVALID_PARAMETER;
606                 return CAC_FAILURE;
607         }
608
609         pipe_hnd = cac_GetPipe( hnd, PI_SVCCTL );
610         if ( !pipe_hnd ) {
611                 hnd->status = NT_STATUS_INVALID_HANDLE;
612                 return CAC_FAILURE;
613         }
614
615         err = rpccli_svcctl_query_config( pipe_hnd, mem_ctx, op->in.svc_hnd,
616                                           &config_out );
617         hnd->status = werror_to_ntstatus( err );
618
619         if ( !NT_STATUS_IS_OK( hnd->status ) )
620                 return CAC_FAILURE;
621
622         if ( !cac_InitCacServiceConfig
623              ( mem_ctx, &config_out, &op->out.config ) ) {
624                 hnd->status = NT_STATUS_NO_MEMORY;
625                 return CAC_FAILURE;
626         }
627
628         return CAC_SUCCESS;
629
630 }