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