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