Merge branch 'master' of ssh://git.samba.org/data/git/samba into libcli-auth-merge...
[kai/samba.git] / source3 / rpc_server / srv_svcctl_nt.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *
5  *  Copyright (C) Marcin Krzysztof Porwit           2005.
6  *
7  *  Largely Rewritten (Again) by:
8  *  Copyright (C) Gerald (Jerry) Carter             2005.
9  *  Copyright (C) Guenther Deschner                 2008,2009.
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 3 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
23  */
24
25 #include "includes.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_RPC_SRV
29
30 struct service_control_op {
31         const char *name;
32         SERVICE_CONTROL_OPS *ops;
33 };
34
35 /* handle external services */
36 extern SERVICE_CONTROL_OPS rcinit_svc_ops;
37
38 /* builtin services (see service_db.c and services/svc_*.c */
39 extern SERVICE_CONTROL_OPS spoolss_svc_ops;
40 extern SERVICE_CONTROL_OPS netlogon_svc_ops;
41 extern SERVICE_CONTROL_OPS winreg_svc_ops;
42 extern SERVICE_CONTROL_OPS wins_svc_ops;
43
44 /* make sure this number patches the number of builtin
45    SERVICE_CONTROL_OPS structure listed above */
46
47 #define SVCCTL_NUM_INTERNAL_SERVICES    4
48
49 struct service_control_op *svcctl_ops;
50
51 static const struct generic_mapping scm_generic_map =
52         { SC_MANAGER_READ_ACCESS, SC_MANAGER_WRITE_ACCESS, SC_MANAGER_EXECUTE_ACCESS, SC_MANAGER_ALL_ACCESS };
53 static const struct generic_mapping svc_generic_map =
54         { SERVICE_READ_ACCESS, SERVICE_WRITE_ACCESS, SERVICE_EXECUTE_ACCESS, SERVICE_ALL_ACCESS };
55
56
57 /********************************************************************
58 ********************************************************************/
59
60 bool init_service_op_table( void )
61 {
62         const char **service_list = lp_svcctl_list();
63         int num_services = SVCCTL_NUM_INTERNAL_SERVICES + str_list_length( service_list );
64         int i;
65
66         if ( !(svcctl_ops = TALLOC_ARRAY( NULL, struct service_control_op, num_services+1)) ) {
67                 DEBUG(0,("init_service_op_table: talloc() failed!\n"));
68                 return False;
69         }
70
71         /* services listed in smb.conf get the rc.init interface */
72
73         for ( i=0; service_list && service_list[i]; i++ ) {
74                 svcctl_ops[i].name = talloc_strdup( svcctl_ops, service_list[i] );
75                 svcctl_ops[i].ops  = &rcinit_svc_ops;
76         }
77
78         /* add builtin services */
79
80         svcctl_ops[i].name = talloc_strdup( svcctl_ops, "Spooler" );
81         svcctl_ops[i].ops  = &spoolss_svc_ops;
82         i++;
83
84         svcctl_ops[i].name = talloc_strdup( svcctl_ops, "NETLOGON" );
85         svcctl_ops[i].ops  = &netlogon_svc_ops;
86         i++;
87
88         svcctl_ops[i].name = talloc_strdup( svcctl_ops, "RemoteRegistry" );
89         svcctl_ops[i].ops  = &winreg_svc_ops;
90         i++;
91
92         svcctl_ops[i].name = talloc_strdup( svcctl_ops, "WINS" );
93         svcctl_ops[i].ops  = &wins_svc_ops;
94         i++;
95
96         /* NULL terminate the array */
97
98         svcctl_ops[i].name = NULL;
99         svcctl_ops[i].ops  = NULL;
100
101         return True;
102 }
103
104 /********************************************************************
105 ********************************************************************/
106
107 static struct service_control_op* find_service_by_name( const char *name )
108 {
109         int i;
110
111         for ( i=0; svcctl_ops[i].name; i++ ) {
112                 if ( strequal( name, svcctl_ops[i].name ) )
113                         return &svcctl_ops[i];
114         }
115
116         return NULL;
117 }
118 /********************************************************************
119 ********************************************************************/
120
121 static NTSTATUS svcctl_access_check( SEC_DESC *sec_desc, NT_USER_TOKEN *token,
122                                      uint32 access_desired, uint32 *access_granted )
123 {
124         if ( geteuid() == sec_initial_uid() ) {
125                 DEBUG(5,("svcctl_access_check: using root's token\n"));
126                 token = get_root_nt_token();
127         }
128
129         return se_access_check( sec_desc, token, access_desired, access_granted);
130 }
131
132 /********************************************************************
133 ********************************************************************/
134
135 static SEC_DESC* construct_scm_sd( TALLOC_CTX *ctx )
136 {
137         SEC_ACE ace[2];
138         size_t i = 0;
139         SEC_DESC *sd;
140         SEC_ACL *theacl;
141         size_t sd_size;
142
143         /* basic access for Everyone */
144
145         init_sec_ace(&ace[i++], &global_sid_World,
146                 SEC_ACE_TYPE_ACCESS_ALLOWED, SC_MANAGER_READ_ACCESS, 0);
147
148         /* Full Access 'BUILTIN\Administrators' */
149
150         init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
151                 SEC_ACE_TYPE_ACCESS_ALLOWED, SC_MANAGER_ALL_ACCESS, 0);
152
153
154         /* create the security descriptor */
155
156         if ( !(theacl = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) )
157                 return NULL;
158
159         if ( !(sd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
160                                   SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
161                                   theacl, &sd_size)) )
162                 return NULL;
163
164         return sd;
165 }
166
167 /******************************************************************
168  Find a registry key handle and return a SERVICE_INFO
169  *****************************************************************/
170
171 static SERVICE_INFO *find_service_info_by_hnd(pipes_struct *p, struct policy_handle *hnd)
172 {
173         SERVICE_INFO *service_info = NULL;
174
175         if( !find_policy_by_hnd( p, hnd, (void **)(void *)&service_info) ) {
176                 DEBUG(2,("find_service_info_by_hnd: handle not found\n"));
177                 return NULL;
178         }
179
180         return service_info;
181 }
182
183 /******************************************************************
184  *****************************************************************/
185
186 static WERROR create_open_service_handle( pipes_struct *p, struct policy_handle *handle, uint32 type,
187                                           const char *service, uint32 access_granted )
188 {
189         SERVICE_INFO *info = NULL;
190         WERROR result = WERR_OK;
191         struct service_control_op *s_op;
192
193         if ( !(info = TALLOC_ZERO_P( NULL, SERVICE_INFO )) )
194                 return WERR_NOMEM;
195
196         /* the Service Manager has a NULL name */
197
198         info->type = SVC_HANDLE_IS_SCM;
199
200         switch ( type ) {
201         case SVC_HANDLE_IS_SCM:
202                 info->type = SVC_HANDLE_IS_SCM;
203                 break;
204
205         case SVC_HANDLE_IS_DBLOCK:
206                 info->type = SVC_HANDLE_IS_DBLOCK;
207                 break;
208
209         case SVC_HANDLE_IS_SERVICE:
210                 info->type = SVC_HANDLE_IS_SERVICE;
211
212                 /* lookup the SERVICE_CONTROL_OPS */
213
214                 if ( !(s_op = find_service_by_name( service )) ) {
215                         result = WERR_NO_SUCH_SERVICE;
216                         goto done;
217                 }
218
219                 info->ops = s_op->ops;
220
221                 if ( !(info->name  = talloc_strdup( info, s_op->name )) ) {
222                         result = WERR_NOMEM;
223                         goto done;
224                 }
225                 break;
226
227         default:
228                 result = WERR_NO_SUCH_SERVICE;
229                 goto done;
230         }
231
232         info->access_granted = access_granted;
233
234         /* store the SERVICE_INFO and create an open handle */
235
236         if ( !create_policy_hnd( p, handle, info ) ) {
237                 result = WERR_ACCESS_DENIED;
238                 goto done;
239         }
240
241 done:
242         if ( !W_ERROR_IS_OK(result) )
243                 TALLOC_FREE(info);
244
245         return result;
246 }
247
248 /********************************************************************
249  _svcctl_OpenSCManagerW
250 ********************************************************************/
251
252 WERROR _svcctl_OpenSCManagerW(pipes_struct *p,
253                               struct svcctl_OpenSCManagerW *r)
254 {
255         SEC_DESC *sec_desc;
256         uint32 access_granted = 0;
257         NTSTATUS status;
258
259         /* perform access checks */
260
261         if ( !(sec_desc = construct_scm_sd( p->mem_ctx )) )
262                 return WERR_NOMEM;
263
264         se_map_generic( &r->in.access_mask, &scm_generic_map );
265         status = svcctl_access_check( sec_desc, p->server_info->ptok,
266                                       r->in.access_mask, &access_granted );
267         if ( !NT_STATUS_IS_OK(status) )
268                 return ntstatus_to_werror( status );
269
270         return create_open_service_handle( p, r->out.handle, SVC_HANDLE_IS_SCM, NULL, access_granted );
271 }
272
273 /********************************************************************
274  _svcctl_OpenServiceW
275 ********************************************************************/
276
277 WERROR _svcctl_OpenServiceW(pipes_struct *p,
278                             struct svcctl_OpenServiceW *r)
279 {
280         SEC_DESC *sec_desc;
281         uint32 access_granted = 0;
282         NTSTATUS status;
283         const char *service = NULL;
284
285         service = r->in.ServiceName;
286         if (!service) {
287                 return WERR_NOMEM;
288         }
289         DEBUG(5, ("_svcctl_OpenServiceW: Attempting to open Service [%s], \n", service));
290
291         /* based on my tests you can open a service if you have a valid scm handle */
292
293         if ( !find_service_info_by_hnd( p, r->in.scmanager_handle) )
294                 return WERR_BADFID;
295
296         /* perform access checks.  Use the root token in order to ensure that we
297            retrieve the security descriptor */
298
299         if ( !(sec_desc = svcctl_get_secdesc( p->mem_ctx, service, get_root_nt_token() )) )
300                 return WERR_NOMEM;
301
302         se_map_generic( &r->in.access_mask, &svc_generic_map );
303         status = svcctl_access_check( sec_desc, p->server_info->ptok,
304                                       r->in.access_mask, &access_granted );
305         if ( !NT_STATUS_IS_OK(status) )
306                 return ntstatus_to_werror( status );
307
308         return create_open_service_handle( p, r->out.handle, SVC_HANDLE_IS_SERVICE, service, access_granted );
309 }
310
311 /********************************************************************
312  _svcctl_CloseServiceHandle
313 ********************************************************************/
314
315 WERROR _svcctl_CloseServiceHandle(pipes_struct *p,
316                                   struct svcctl_CloseServiceHandle *r)
317 {
318         if ( !close_policy_hnd( p, r->in.handle ) )
319                 return  WERR_BADFID;
320
321         ZERO_STRUCTP(r->out.handle);
322
323         return WERR_OK;
324 }
325
326 /********************************************************************
327  _svcctl_GetServiceDisplayNameW
328 ********************************************************************/
329
330 WERROR _svcctl_GetServiceDisplayNameW(pipes_struct *p,
331                                       struct svcctl_GetServiceDisplayNameW *r)
332 {
333         const char *service;
334         const char *display_name;
335         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
336
337         /* can only use an SCM handle here */
338
339         if ( !info || (info->type != SVC_HANDLE_IS_SCM) )
340                 return WERR_BADFID;
341
342         service = r->in.service_name;
343
344         display_name = svcctl_lookup_dispname(p->mem_ctx, service,
345                                               p->server_info->ptok);
346         if (!display_name) {
347                 display_name = "";
348         }
349
350         *r->out.display_name = display_name;
351         *r->out.display_name_length = strlen(display_name);
352
353         return WERR_OK;
354 }
355
356 /********************************************************************
357  _svcctl_QueryServiceStatus
358 ********************************************************************/
359
360 WERROR _svcctl_QueryServiceStatus(pipes_struct *p,
361                                   struct svcctl_QueryServiceStatus *r)
362 {
363         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
364
365         /* perform access checks */
366
367         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
368                 return WERR_BADFID;
369
370         if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_STATUS) )
371                 return WERR_ACCESS_DENIED;
372
373         /* try the service specific status call */
374
375         return info->ops->service_status( info->name, r->out.service_status );
376 }
377
378 /********************************************************************
379 ********************************************************************/
380
381 static int enumerate_status( TALLOC_CTX *ctx, struct ENUM_SERVICE_STATUSW **status, NT_USER_TOKEN *token )
382 {
383         int num_services = 0;
384         int i;
385         struct ENUM_SERVICE_STATUSW *st;
386         const char *display_name;
387
388         /* just count */
389         while ( svcctl_ops[num_services].name )
390                 num_services++;
391
392         if ( !(st = TALLOC_ARRAY( ctx, struct ENUM_SERVICE_STATUSW, num_services )) ) {
393                 DEBUG(0,("enumerate_status: talloc() failed!\n"));
394                 return -1;
395         }
396
397         for ( i=0; i<num_services; i++ ) {
398                 st[i].service_name = talloc_strdup(st, svcctl_ops[i].name );
399
400                 display_name = svcctl_lookup_dispname(ctx, svcctl_ops[i].name, token );
401                 st[i].display_name = talloc_strdup(st, display_name ? display_name : "");
402
403                 svcctl_ops[i].ops->service_status( svcctl_ops[i].name, &st[i].status );
404         }
405
406         *status = st;
407
408         return num_services;
409 }
410
411 /********************************************************************
412  _svcctl_EnumServicesStatusW
413 ********************************************************************/
414
415 WERROR _svcctl_EnumServicesStatusW(pipes_struct *p,
416                                    struct svcctl_EnumServicesStatusW *r)
417 {
418         struct ENUM_SERVICE_STATUSW *services = NULL;
419         int num_services;
420         int i = 0;
421         size_t buffer_size = 0;
422         WERROR result = WERR_OK;
423         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
424         NT_USER_TOKEN *token = p->server_info->ptok;
425         DATA_BLOB blob = data_blob_null;
426
427         /* perform access checks */
428
429         if ( !info || (info->type != SVC_HANDLE_IS_SCM) )
430                 return WERR_BADFID;
431
432         if ( !(info->access_granted & SC_RIGHT_MGR_ENUMERATE_SERVICE) ) {
433                 return WERR_ACCESS_DENIED;
434         }
435
436         num_services = enumerate_status( p->mem_ctx, &services, token );
437         if (num_services == -1 ) {
438                 return WERR_NOMEM;
439         }
440
441         for ( i=0; i<num_services; i++ ) {
442                 buffer_size += ndr_size_ENUM_SERVICE_STATUSW(&services[i], NULL, 0);
443         }
444
445         buffer_size += buffer_size % 4;
446
447         if (buffer_size > r->in.offered) {
448                 num_services = 0;
449                 result = WERR_MORE_DATA;
450         }
451
452         if ( W_ERROR_IS_OK(result) ) {
453
454                 enum ndr_err_code ndr_err;
455                 struct ndr_push *ndr;
456
457                 ndr = ndr_push_init_ctx(p->mem_ctx, NULL);
458                 if (ndr == NULL) {
459                         return WERR_INVALID_PARAM;
460                 }
461
462                 ndr_err = ndr_push_ENUM_SERVICE_STATUSW_array(
463                         ndr, num_services, services);
464                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
465                         return ntstatus_to_werror(ndr_map_error2ntstatus(ndr_err));
466                 }
467                 blob = ndr_push_blob(ndr);
468                 memcpy(r->out.service, blob.data, MIN(blob.length, r->in.offered));
469         }
470
471         *r->out.needed                  = (buffer_size > r->in.offered) ? buffer_size : r->in.offered;
472         *r->out.services_returned       = (uint32)num_services;
473         *r->out.resume_handle           = 0x0;
474
475         return result;
476 }
477
478 /********************************************************************
479  _svcctl_StartServiceW
480 ********************************************************************/
481
482 WERROR _svcctl_StartServiceW(pipes_struct *p,
483                              struct svcctl_StartServiceW *r)
484 {
485         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
486
487         /* perform access checks */
488
489         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
490                 return WERR_BADFID;
491
492         if ( !(info->access_granted & SC_RIGHT_SVC_START) )
493                 return WERR_ACCESS_DENIED;
494
495         return info->ops->start_service( info->name );
496 }
497
498 /********************************************************************
499  _svcctl_ControlService
500 ********************************************************************/
501
502 WERROR _svcctl_ControlService(pipes_struct *p,
503                               struct svcctl_ControlService *r)
504 {
505         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
506
507         /* perform access checks */
508
509         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
510                 return WERR_BADFID;
511
512         switch ( r->in.control ) {
513         case SVCCTL_CONTROL_STOP:
514                 if ( !(info->access_granted & SC_RIGHT_SVC_STOP) )
515                         return WERR_ACCESS_DENIED;
516
517                 return info->ops->stop_service( info->name,
518                                                 r->out.service_status );
519
520         case SVCCTL_CONTROL_INTERROGATE:
521                 if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_STATUS) )
522                         return WERR_ACCESS_DENIED;
523
524                 return info->ops->service_status( info->name,
525                                                   r->out.service_status );
526         default:
527                 return WERR_ACCESS_DENIED;
528         }
529 }
530
531 /********************************************************************
532  _svcctl_EnumDependentServicesW
533 ********************************************************************/
534
535 WERROR _svcctl_EnumDependentServicesW(pipes_struct *p,
536                                       struct svcctl_EnumDependentServicesW *r)
537 {
538         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.service );
539
540         /* perform access checks */
541
542         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
543                 return WERR_BADFID;
544
545         if ( !(info->access_granted & SC_RIGHT_SVC_ENUMERATE_DEPENDENTS) )
546                 return WERR_ACCESS_DENIED;
547
548         switch (r->in.state) {
549         case SERVICE_STATE_ACTIVE:
550         case SERVICE_STATE_INACTIVE:
551         case SERVICE_STATE_ALL:
552                 break;
553         default:
554                 return WERR_INVALID_PARAM;
555         }
556
557         /* we have to set the outgoing buffer size to the same as the
558            incoming buffer size (even in the case of failure */
559         /* this is done in the autogenerated server already - gd */
560
561         *r->out.needed = r->in.offered;
562
563         /* no dependent services...basically a stub function */
564         *r->out.services_returned = 0;
565
566         return WERR_OK;
567 }
568
569 /********************************************************************
570  _svcctl_QueryServiceStatusEx
571 ********************************************************************/
572
573 WERROR _svcctl_QueryServiceStatusEx(pipes_struct *p,
574                                     struct svcctl_QueryServiceStatusEx *r)
575 {
576         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
577         uint32 buffer_size;
578
579         /* perform access checks */
580
581         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
582                 return WERR_BADFID;
583
584         if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_STATUS) )
585                 return WERR_ACCESS_DENIED;
586
587         /* we have to set the outgoing buffer size to the same as the
588            incoming buffer size (even in the case of failure) */
589         *r->out.needed = r->in.offered;
590
591         switch ( r->in.info_level ) {
592                 case SVC_STATUS_PROCESS_INFO:
593                 {
594                         struct SERVICE_STATUS_PROCESS svc_stat_proc;
595                         enum ndr_err_code ndr_err;
596                         DATA_BLOB blob;
597
598                         /* Get the status of the service.. */
599                         info->ops->service_status( info->name, &svc_stat_proc.status );
600                         svc_stat_proc.process_id     = sys_getpid();
601                         svc_stat_proc.service_flags  = 0x0;
602
603                         ndr_err = ndr_push_struct_blob(&blob, p->mem_ctx, NULL,
604                                                        &svc_stat_proc,
605                                                        (ndr_push_flags_fn_t)ndr_push_SERVICE_STATUS_PROCESS);
606                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
607                                 return WERR_INVALID_PARAM;
608                         }
609
610                         r->out.buffer = blob.data;
611                         buffer_size = sizeof(struct SERVICE_STATUS_PROCESS);
612                         break;
613                 }
614
615                 default:
616                         return WERR_UNKNOWN_LEVEL;
617         }
618
619
620         buffer_size += buffer_size % 4;
621         *r->out.needed = (buffer_size > r->in.offered) ? buffer_size : r->in.offered;
622
623         if (buffer_size > r->in.offered ) {
624                 return WERR_INSUFFICIENT_BUFFER;
625         }
626
627         return WERR_OK;
628 }
629
630 /********************************************************************
631 ********************************************************************/
632
633 static WERROR fill_svc_config( TALLOC_CTX *ctx, const char *name,
634                                struct QUERY_SERVICE_CONFIG *config,
635                                NT_USER_TOKEN *token )
636 {
637         REGVAL_CTR *values;
638         REGISTRY_VALUE *val;
639
640         /* retrieve the registry values for this service */
641
642         if ( !(values = svcctl_fetch_regvalues( name, token )) )
643                 return WERR_REG_CORRUPT;
644
645         /* now fill in the individual values */
646
647         if ( (val = regval_ctr_getvalue( values, "DisplayName" )) != NULL )
648                 config->displayname = regval_sz(val);
649         else
650                 config->displayname = name;
651
652         if ( (val = regval_ctr_getvalue( values, "ObjectName" )) != NULL ) {
653                 config->startname = regval_sz(val);
654         }
655
656         if ( (val = regval_ctr_getvalue( values, "ImagePath" )) != NULL ) {
657                 config->executablepath = regval_sz(val);
658         }
659
660         /* a few hard coded values */
661         /* loadordergroup and dependencies are empty */
662
663         config->tag_id           = 0x00000000;                  /* unassigned loadorder group */
664         config->service_type     = SERVICE_TYPE_WIN32_OWN_PROCESS;
665         config->error_control    = SVCCTL_SVC_ERROR_NORMAL;
666
667         /* set the start type.  NetLogon and WINS are disabled to prevent
668            the client from showing the "Start" button (if of course the services
669            are not running */
670
671         if ( strequal( name, "NETLOGON" ) && ( lp_servicenumber(name) == -1 ) )
672                 config->start_type = SVCCTL_DISABLED;
673         else if ( strequal( name, "WINS" ) && ( !lp_wins_support() ))
674                 config->start_type = SVCCTL_DISABLED;
675         else
676                 config->start_type = SVCCTL_DEMAND_START;
677
678
679         TALLOC_FREE( values );
680
681         return WERR_OK;
682 }
683
684 /********************************************************************
685  _svcctl_QueryServiceConfigW
686 ********************************************************************/
687
688 WERROR _svcctl_QueryServiceConfigW(pipes_struct *p,
689                                    struct svcctl_QueryServiceConfigW *r)
690 {
691         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
692         uint32 buffer_size;
693         WERROR wresult;
694
695         /* perform access checks */
696
697         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
698                 return WERR_BADFID;
699
700         if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_CONFIG) )
701                 return WERR_ACCESS_DENIED;
702
703         /* we have to set the outgoing buffer size to the same as the
704            incoming buffer size (even in the case of failure */
705
706         *r->out.needed = r->in.offered;
707
708         wresult = fill_svc_config( p->mem_ctx, info->name, r->out.query,
709                                    p->server_info->ptok);
710         if ( !W_ERROR_IS_OK(wresult) )
711                 return wresult;
712
713         buffer_size = ndr_size_QUERY_SERVICE_CONFIG(r->out.query, NULL, 0);
714         *r->out.needed = (buffer_size > r->in.offered) ? buffer_size : r->in.offered;
715
716         if (buffer_size > r->in.offered ) {
717                 ZERO_STRUCTP(r->out.query);
718                 return WERR_INSUFFICIENT_BUFFER;
719         }
720
721         return WERR_OK;
722 }
723
724 /********************************************************************
725  _svcctl_QueryServiceConfig2W
726 ********************************************************************/
727
728 WERROR _svcctl_QueryServiceConfig2W(pipes_struct *p,
729                                     struct svcctl_QueryServiceConfig2W *r)
730 {
731         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
732         uint32 buffer_size;
733
734         /* perform access checks */
735
736         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
737                 return WERR_BADFID;
738
739         if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_CONFIG) )
740                 return WERR_ACCESS_DENIED;
741
742         /* we have to set the outgoing buffer size to the same as the
743            incoming buffer size (even in the case of failure */
744         *r->out.needed = r->in.offered;
745
746         switch ( r->in.info_level ) {
747         case SERVICE_CONFIG_DESCRIPTION:
748                 {
749                         struct SERVICE_DESCRIPTION desc_buf;
750                         const char *description;
751                         enum ndr_err_code ndr_err;
752                         DATA_BLOB blob;
753
754                         description = svcctl_lookup_description(
755                                 p->mem_ctx, info->name, p->server_info->ptok);
756
757                         desc_buf.description = description;
758
759                         ndr_err = ndr_push_struct_blob(&blob, p->mem_ctx, NULL,
760                                                        &desc_buf,
761                                                        (ndr_push_flags_fn_t)ndr_push_SERVICE_DESCRIPTION);
762                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
763                                 return WERR_INVALID_PARAM;
764                         }
765
766                         buffer_size = ndr_size_SERVICE_DESCRIPTION(&desc_buf, NULL, 0);
767                         r->out.buffer = blob.data;
768
769                         break;
770                 }
771                 break;
772         case SERVICE_CONFIG_FAILURE_ACTIONS:
773                 {
774                         struct SERVICE_FAILURE_ACTIONS actions;
775                         enum ndr_err_code ndr_err;
776                         DATA_BLOB blob;
777
778                         /* nothing to say...just service the request */
779
780                         ZERO_STRUCT( actions );
781
782                         ndr_err = ndr_push_struct_blob(&blob, p->mem_ctx, NULL,
783                                                        &actions,
784                                                        (ndr_push_flags_fn_t)ndr_push_SERVICE_FAILURE_ACTIONS);
785                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
786                                 return WERR_INVALID_PARAM;
787                         }
788
789                         buffer_size = ndr_size_SERVICE_FAILURE_ACTIONS(&actions, NULL, 0);
790                         r->out.buffer = blob.data;
791
792                         break;
793                 }
794                 break;
795
796         default:
797                 return WERR_UNKNOWN_LEVEL;
798         }
799
800         buffer_size += buffer_size % 4;
801         *r->out.needed = (buffer_size > r->in.offered) ? buffer_size : r->in.offered;
802
803         if (buffer_size > r->in.offered)
804                 return WERR_INSUFFICIENT_BUFFER;
805
806         return WERR_OK;
807 }
808
809 /********************************************************************
810  _svcctl_LockServiceDatabase
811 ********************************************************************/
812
813 WERROR _svcctl_LockServiceDatabase(pipes_struct *p,
814                                    struct svcctl_LockServiceDatabase *r)
815 {
816         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
817
818         /* perform access checks */
819
820         if ( !info || (info->type != SVC_HANDLE_IS_SCM) )
821                 return WERR_BADFID;
822
823         if ( !(info->access_granted & SC_RIGHT_MGR_LOCK) )
824                 return WERR_ACCESS_DENIED;
825
826         /* Just open a handle.  Doesn't actually lock anything */
827
828         return create_open_service_handle( p, r->out.lock, SVC_HANDLE_IS_DBLOCK, NULL, 0 );
829 }
830
831 /********************************************************************
832  _svcctl_UnlockServiceDatabase
833 ********************************************************************/
834
835 WERROR _svcctl_UnlockServiceDatabase(pipes_struct *p,
836                                      struct svcctl_UnlockServiceDatabase *r)
837 {
838         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.lock );
839
840
841         if ( !info || (info->type != SVC_HANDLE_IS_DBLOCK) )
842                 return WERR_BADFID;
843
844         return close_policy_hnd( p, r->out.lock) ? WERR_OK : WERR_BADFID;
845 }
846
847 /********************************************************************
848  _svcctl_QueryServiceObjectSecurity
849 ********************************************************************/
850
851 WERROR _svcctl_QueryServiceObjectSecurity(pipes_struct *p,
852                                           struct svcctl_QueryServiceObjectSecurity *r)
853 {
854         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
855         SEC_DESC *sec_desc;
856         NTSTATUS status;
857         uint8_t *buffer = NULL;
858         size_t len = 0;
859
860
861         /* only support the SCM and individual services */
862
863         if ( !info || !(info->type & (SVC_HANDLE_IS_SERVICE|SVC_HANDLE_IS_SCM)) )
864                 return WERR_BADFID;
865
866         /* check access reights (according to MSDN) */
867
868         if ( !(info->access_granted & STD_RIGHT_READ_CONTROL_ACCESS) )
869                 return WERR_ACCESS_DENIED;
870
871         /* TODO: handle something besides DACL_SECURITY_INFORMATION */
872
873         if ( (r->in.security_flags & DACL_SECURITY_INFORMATION) != DACL_SECURITY_INFORMATION )
874                 return WERR_INVALID_PARAM;
875
876         /* lookup the security descriptor and marshall it up for a reply */
877
878         if ( !(sec_desc = svcctl_get_secdesc( p->mem_ctx, info->name, get_root_nt_token() )) )
879                 return WERR_NOMEM;
880
881         *r->out.needed = ndr_size_security_descriptor( sec_desc, NULL, 0 );
882
883         if ( *r->out.needed > r->in.offered) {
884                 return WERR_INSUFFICIENT_BUFFER;
885         }
886
887         status = marshall_sec_desc(p->mem_ctx, sec_desc, &buffer, &len);
888         if (!NT_STATUS_IS_OK(status)) {
889                 return ntstatus_to_werror(status);
890         }
891
892         *r->out.needed = len;
893         r->out.buffer = buffer;
894
895         return WERR_OK;
896 }
897
898 /********************************************************************
899  _svcctl_SetServiceObjectSecurity
900 ********************************************************************/
901
902 WERROR _svcctl_SetServiceObjectSecurity(pipes_struct *p,
903                                         struct svcctl_SetServiceObjectSecurity *r)
904 {
905         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
906         SEC_DESC *sec_desc = NULL;
907         uint32 required_access;
908         NTSTATUS status;
909
910         if ( !info || !(info->type & (SVC_HANDLE_IS_SERVICE|SVC_HANDLE_IS_SCM))  )
911                 return WERR_BADFID;
912
913         /* can't set the security de4scriptor on the ServiceControlManager */
914
915         if ( info->type == SVC_HANDLE_IS_SCM )
916                 return WERR_ACCESS_DENIED;
917
918         /* check the access on the open handle */
919
920         switch ( r->in.security_flags ) {
921                 case DACL_SECURITY_INFORMATION:
922                         required_access = STD_RIGHT_WRITE_DAC_ACCESS;
923                         break;
924
925                 case OWNER_SECURITY_INFORMATION:
926                 case GROUP_SECURITY_INFORMATION:
927                         required_access = STD_RIGHT_WRITE_OWNER_ACCESS;
928                         break;
929
930                 case SACL_SECURITY_INFORMATION:
931                         return WERR_INVALID_PARAM;
932                 default:
933                         return WERR_INVALID_PARAM;
934         }
935
936         if ( !(info->access_granted & required_access) )
937                 return WERR_ACCESS_DENIED;
938
939         /* read the security descfriptor */
940
941         status = unmarshall_sec_desc(p->mem_ctx,
942                                      r->in.buffer,
943                                      r->in.offered,
944                                      &sec_desc);
945         if (!NT_STATUS_IS_OK(status)) {
946                 return ntstatus_to_werror(status);
947         }
948
949         /* store the new SD */
950
951         if ( !svcctl_set_secdesc( p->mem_ctx, info->name, sec_desc,
952                                   p->server_info->ptok) )
953                 return WERR_ACCESS_DENIED;
954
955         return WERR_OK;
956 }
957
958
959 WERROR _svcctl_DeleteService(pipes_struct *p, struct svcctl_DeleteService *r)
960 {
961         p->rng_fault_state = True;
962         return WERR_NOT_SUPPORTED;
963 }
964
965 WERROR _svcctl_SetServiceStatus(pipes_struct *p, struct svcctl_SetServiceStatus *r)
966 {
967         p->rng_fault_state = True;
968         return WERR_NOT_SUPPORTED;
969 }
970
971 WERROR _svcctl_NotifyBootConfigStatus(pipes_struct *p, struct svcctl_NotifyBootConfigStatus *r)
972 {
973         p->rng_fault_state = True;
974         return WERR_NOT_SUPPORTED;
975 }
976
977 WERROR _svcctl_SCSetServiceBitsW(pipes_struct *p, struct svcctl_SCSetServiceBitsW *r)
978 {
979         p->rng_fault_state = True;
980         return WERR_NOT_SUPPORTED;
981 }
982
983 WERROR _svcctl_ChangeServiceConfigW(pipes_struct *p, struct svcctl_ChangeServiceConfigW *r)
984 {
985         p->rng_fault_state = True;
986         return WERR_NOT_SUPPORTED;
987 }
988
989 WERROR _svcctl_CreateServiceW(pipes_struct *p, struct svcctl_CreateServiceW *r)
990 {
991         p->rng_fault_state = True;
992         return WERR_NOT_SUPPORTED;
993 }
994
995 WERROR _svcctl_QueryServiceLockStatusW(pipes_struct *p, struct svcctl_QueryServiceLockStatusW *r)
996 {
997         p->rng_fault_state = True;
998         return WERR_NOT_SUPPORTED;
999 }
1000
1001 WERROR _svcctl_GetServiceKeyNameW(pipes_struct *p, struct svcctl_GetServiceKeyNameW *r)
1002 {
1003         p->rng_fault_state = True;
1004         return WERR_NOT_SUPPORTED;
1005 }
1006
1007 WERROR _svcctl_SCSetServiceBitsA(pipes_struct *p, struct svcctl_SCSetServiceBitsA *r)
1008 {
1009         p->rng_fault_state = True;
1010         return WERR_NOT_SUPPORTED;
1011 }
1012
1013 WERROR _svcctl_ChangeServiceConfigA(pipes_struct *p, struct svcctl_ChangeServiceConfigA *r)
1014 {
1015         p->rng_fault_state = True;
1016         return WERR_NOT_SUPPORTED;
1017 }
1018
1019 WERROR _svcctl_CreateServiceA(pipes_struct *p, struct svcctl_CreateServiceA *r)
1020 {
1021         p->rng_fault_state = True;
1022         return WERR_NOT_SUPPORTED;
1023 }
1024
1025 WERROR _svcctl_EnumDependentServicesA(pipes_struct *p, struct svcctl_EnumDependentServicesA *r)
1026 {
1027         p->rng_fault_state = True;
1028         return WERR_NOT_SUPPORTED;
1029 }
1030
1031 WERROR _svcctl_EnumServicesStatusA(pipes_struct *p, struct svcctl_EnumServicesStatusA *r)
1032 {
1033         p->rng_fault_state = True;
1034         return WERR_NOT_SUPPORTED;
1035 }
1036
1037 WERROR _svcctl_OpenSCManagerA(pipes_struct *p, struct svcctl_OpenSCManagerA *r)
1038 {
1039         p->rng_fault_state = True;
1040         return WERR_NOT_SUPPORTED;
1041 }
1042
1043 WERROR _svcctl_OpenServiceA(pipes_struct *p, struct svcctl_OpenServiceA *r)
1044 {
1045         p->rng_fault_state = True;
1046         return WERR_NOT_SUPPORTED;
1047 }
1048
1049 WERROR _svcctl_QueryServiceConfigA(pipes_struct *p, struct svcctl_QueryServiceConfigA *r)
1050 {
1051         p->rng_fault_state = True;
1052         return WERR_NOT_SUPPORTED;
1053 }
1054
1055 WERROR _svcctl_QueryServiceLockStatusA(pipes_struct *p, struct svcctl_QueryServiceLockStatusA *r)
1056 {
1057         p->rng_fault_state = True;
1058         return WERR_NOT_SUPPORTED;
1059 }
1060
1061 WERROR _svcctl_StartServiceA(pipes_struct *p, struct svcctl_StartServiceA *r)
1062 {
1063         p->rng_fault_state = True;
1064         return WERR_NOT_SUPPORTED;
1065 }
1066
1067 WERROR _svcctl_GetServiceDisplayNameA(pipes_struct *p, struct svcctl_GetServiceDisplayNameA *r)
1068 {
1069         p->rng_fault_state = True;
1070         return WERR_NOT_SUPPORTED;
1071 }
1072
1073 WERROR _svcctl_GetServiceKeyNameA(pipes_struct *p, struct svcctl_GetServiceKeyNameA *r)
1074 {
1075         p->rng_fault_state = True;
1076         return WERR_NOT_SUPPORTED;
1077 }
1078
1079 WERROR _svcctl_GetCurrentGroupeStateW(pipes_struct *p, struct svcctl_GetCurrentGroupeStateW *r)
1080 {
1081         p->rng_fault_state = True;
1082         return WERR_NOT_SUPPORTED;
1083 }
1084
1085 WERROR _svcctl_EnumServiceGroupW(pipes_struct *p, struct svcctl_EnumServiceGroupW *r)
1086 {
1087         p->rng_fault_state = True;
1088         return WERR_NOT_SUPPORTED;
1089 }
1090
1091 WERROR _svcctl_ChangeServiceConfig2A(pipes_struct *p, struct svcctl_ChangeServiceConfig2A *r)
1092 {
1093         p->rng_fault_state = True;
1094         return WERR_NOT_SUPPORTED;
1095 }
1096
1097 WERROR _svcctl_ChangeServiceConfig2W(pipes_struct *p, struct svcctl_ChangeServiceConfig2W *r)
1098 {
1099         p->rng_fault_state = True;
1100         return WERR_NOT_SUPPORTED;
1101 }
1102
1103 WERROR _svcctl_QueryServiceConfig2A(pipes_struct *p, struct svcctl_QueryServiceConfig2A *r)
1104 {
1105         p->rng_fault_state = True;
1106         return WERR_NOT_SUPPORTED;
1107 }
1108
1109 WERROR _EnumServicesStatusExA(pipes_struct *p, struct EnumServicesStatusExA *r)
1110 {
1111         p->rng_fault_state = True;
1112         return WERR_NOT_SUPPORTED;
1113 }
1114
1115 WERROR _EnumServicesStatusExW(pipes_struct *p, struct EnumServicesStatusExW *r)
1116 {
1117         p->rng_fault_state = True;
1118         return WERR_NOT_SUPPORTED;
1119 }
1120
1121 WERROR _svcctl_SCSendTSMessage(pipes_struct *p, struct svcctl_SCSendTSMessage *r)
1122 {
1123         p->rng_fault_state = True;
1124         return WERR_NOT_SUPPORTED;
1125 }
1126