Now that all policy_handle free_fn's are just TALLOC_FREE, dump free_fn
[ira/wip.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 *acl;
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 ( !(acl = 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                                   acl, &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, POLICY_HND *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, POLICY_HND *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, ENUM_SERVICES_STATUS **status, NT_USER_TOKEN *token )
384 {
385         int num_services = 0;
386         int i;
387         ENUM_SERVICES_STATUS *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, ENUM_SERVICES_STATUS, num_services )) ) {
395                 DEBUG(0,("enumerate_status: talloc() failed!\n"));
396                 return -1;
397         }
398
399         for ( i=0; i<num_services; i++ ) {
400                 init_unistr( &st[i].servicename, svcctl_ops[i].name );
401
402                 display_name = svcctl_lookup_dispname(ctx, svcctl_ops[i].name, token );
403                 init_unistr( &st[i].displayname, 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 ********************************************************************/
415
416 WERROR _svcctl_enum_services_status(pipes_struct *p, SVCCTL_Q_ENUM_SERVICES_STATUS *q_u, SVCCTL_R_ENUM_SERVICES_STATUS *r_u)
417 {
418         ENUM_SERVICES_STATUS *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, &q_u->handle );
424         NT_USER_TOKEN *token = p->server_info->ptok;
425
426         /* perform access checks */
427
428         if ( !info || (info->type != SVC_HANDLE_IS_SCM) )
429                 return WERR_BADFID;
430
431         if ( !(info->access_granted & SC_RIGHT_MGR_ENUMERATE_SERVICE) ) {
432                 return WERR_ACCESS_DENIED;
433         }
434
435         num_services = enumerate_status( p->mem_ctx, &services, token );
436         if (num_services == -1 ) {
437                 return WERR_NOMEM;
438         }
439
440         for ( i=0; i<num_services; i++ ) {
441                 buffer_size += svcctl_sizeof_enum_services_status(&services[i]);
442         }
443
444         buffer_size += buffer_size % 4;
445
446         if (buffer_size > q_u->buffer_size ) {
447                 num_services = 0;
448                 result = WERR_MORE_DATA;
449         }
450
451         rpcbuf_init(&r_u->buffer, q_u->buffer_size, p->mem_ctx);
452
453         if ( W_ERROR_IS_OK(result) ) {
454                 for ( i=0; i<num_services; i++ )
455                         svcctl_io_enum_services_status( "", &services[i], &r_u->buffer, 0 );
456         }
457
458         r_u->needed      = (buffer_size > q_u->buffer_size) ? buffer_size : q_u->buffer_size;
459         r_u->returned    = (uint32)num_services;
460
461         if ( !(r_u->resume = TALLOC_P( p->mem_ctx, uint32 )) )
462                 return WERR_NOMEM;
463
464         *r_u->resume = 0x0;
465
466         return result;
467 }
468
469 /********************************************************************
470  _svcctl_StartServiceW
471 ********************************************************************/
472
473 WERROR _svcctl_StartServiceW(pipes_struct *p,
474                              struct svcctl_StartServiceW *r)
475 {
476         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
477
478         /* perform access checks */
479
480         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
481                 return WERR_BADFID;
482
483         if ( !(info->access_granted & SC_RIGHT_SVC_START) )
484                 return WERR_ACCESS_DENIED;
485
486         return info->ops->start_service( info->name );
487 }
488
489 /********************************************************************
490  _svcctl_ControlService
491 ********************************************************************/
492
493 WERROR _svcctl_ControlService(pipes_struct *p,
494                               struct svcctl_ControlService *r)
495 {
496         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
497
498         /* perform access checks */
499
500         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
501                 return WERR_BADFID;
502
503         switch ( r->in.control ) {
504         case SVCCTL_CONTROL_STOP:
505                 if ( !(info->access_granted & SC_RIGHT_SVC_STOP) )
506                         return WERR_ACCESS_DENIED;
507
508                 return info->ops->stop_service( info->name,
509                                                 r->out.service_status );
510
511         case SVCCTL_CONTROL_INTERROGATE:
512                 if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_STATUS) )
513                         return WERR_ACCESS_DENIED;
514
515                 return info->ops->service_status( info->name,
516                                                   r->out.service_status );
517         default:
518                 return WERR_ACCESS_DENIED;
519         }
520 }
521
522 /********************************************************************
523  _svcctl_EnumDependentServicesW
524 ********************************************************************/
525
526 WERROR _svcctl_EnumDependentServicesW(pipes_struct *p,
527                                       struct svcctl_EnumDependentServicesW *r)
528 {
529         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.service );
530
531         /* perform access checks */
532
533         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
534                 return WERR_BADFID;
535
536         if ( !(info->access_granted & SC_RIGHT_SVC_ENUMERATE_DEPENDENTS) )
537                 return WERR_ACCESS_DENIED;
538
539         /* we have to set the outgoing buffer size to the same as the
540            incoming buffer size (even in the case of failure */
541         /* this is done in the autogenerated server already - gd */
542
543         *r->out.bytes_needed = r->in.buf_size;
544
545         /* no dependent services...basically a stub function */
546         *r->out.services_returned = 0;
547
548         return WERR_OK;
549 }
550
551 /********************************************************************
552  _svcctl_QueryServiceStatusEx
553 ********************************************************************/
554
555 WERROR _svcctl_QueryServiceStatusEx(pipes_struct *p,
556                                     struct svcctl_QueryServiceStatusEx *r)
557 {
558         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
559         uint32 buffer_size;
560
561         /* perform access checks */
562
563         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
564                 return WERR_BADFID;
565
566         if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_STATUS) )
567                 return WERR_ACCESS_DENIED;
568
569         /* we have to set the outgoing buffer size to the same as the
570            incoming buffer size (even in the case of failure) */
571
572         *r->out.bytes_needed = r->in.buf_size;
573
574         switch ( r->in.info_level ) {
575                 case SVC_STATUS_PROCESS_INFO:
576                 {
577                         struct SERVICE_STATUS_PROCESS svc_stat_proc;
578                         enum ndr_err_code ndr_err;
579                         DATA_BLOB blob;
580
581                         /* Get the status of the service.. */
582                         info->ops->service_status( info->name, &svc_stat_proc.status );
583                         svc_stat_proc.process_id     = sys_getpid();
584                         svc_stat_proc.service_flags  = 0x0;
585
586                         ndr_err = ndr_push_struct_blob(&blob, p->mem_ctx, NULL,
587                                                        &svc_stat_proc,
588                                                        (ndr_push_flags_fn_t)ndr_push_SERVICE_STATUS_PROCESS);
589                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
590                                 return WERR_INVALID_PARAM;
591                         }
592
593                         r->out.buffer = blob.data;
594                         buffer_size = sizeof(struct SERVICE_STATUS_PROCESS);
595                         break;
596                 }
597
598                 default:
599                         return WERR_UNKNOWN_LEVEL;
600         }
601
602
603         buffer_size += buffer_size % 4;
604         *r->out.bytes_needed = (buffer_size > r->in.buf_size) ? buffer_size : r->in.buf_size;
605
606         if (buffer_size > r->in.buf_size ) {
607                 return WERR_INSUFFICIENT_BUFFER;
608         }
609
610         return WERR_OK;
611 }
612
613 /********************************************************************
614 ********************************************************************/
615
616 static WERROR fill_svc_config( TALLOC_CTX *ctx, const char *name,
617                                struct QUERY_SERVICE_CONFIG *config,
618                                NT_USER_TOKEN *token )
619 {
620         REGVAL_CTR *values;
621         REGISTRY_VALUE *val;
622
623         /* retrieve the registry values for this service */
624
625         if ( !(values = svcctl_fetch_regvalues( name, token )) )
626                 return WERR_REG_CORRUPT;
627
628         /* now fill in the individual values */
629
630         if ( (val = regval_ctr_getvalue( values, "DisplayName" )) != NULL )
631                 config->displayname = regval_sz(val);
632         else
633                 config->displayname = name;
634
635         if ( (val = regval_ctr_getvalue( values, "ObjectName" )) != NULL ) {
636                 config->startname = regval_sz(val);
637         }
638
639         if ( (val = regval_ctr_getvalue( values, "ImagePath" )) != NULL ) {
640                 config->executablepath = regval_sz(val);
641         }
642
643         /* a few hard coded values */
644         /* loadordergroup and dependencies are empty */
645
646         config->tag_id           = 0x00000000;                  /* unassigned loadorder group */
647         config->service_type     = SERVICE_TYPE_WIN32_OWN_PROCESS;
648         config->error_control    = SVCCTL_SVC_ERROR_NORMAL;
649
650         /* set the start type.  NetLogon and WINS are disabled to prevent
651            the client from showing the "Start" button (if of course the services
652            are not running */
653
654         if ( strequal( name, "NETLOGON" ) && ( lp_servicenumber(name) == -1 ) )
655                 config->start_type = SVCCTL_DISABLED;
656         else if ( strequal( name, "WINS" ) && ( !lp_wins_support() ))
657                 config->start_type = SVCCTL_DISABLED;
658         else
659                 config->start_type = SVCCTL_DEMAND_START;
660
661
662         TALLOC_FREE( values );
663
664         return WERR_OK;
665 }
666
667 /********************************************************************
668  _svcctl_QueryServiceConfigW
669 ********************************************************************/
670
671 WERROR _svcctl_QueryServiceConfigW(pipes_struct *p,
672                                    struct svcctl_QueryServiceConfigW *r)
673 {
674         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
675         uint32 buffer_size;
676         WERROR wresult;
677
678         /* perform access checks */
679
680         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
681                 return WERR_BADFID;
682
683         if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_CONFIG) )
684                 return WERR_ACCESS_DENIED;
685
686         /* we have to set the outgoing buffer size to the same as the
687            incoming buffer size (even in the case of failure */
688
689         *r->out.bytes_needed = r->in.buf_size;
690
691         wresult = fill_svc_config( p->mem_ctx, info->name, r->out.query,
692                                    p->server_info->ptok);
693         if ( !W_ERROR_IS_OK(wresult) )
694                 return wresult;
695
696         buffer_size = ndr_size_QUERY_SERVICE_CONFIG(r->out.query, NULL, 0);
697         *r->out.bytes_needed = (buffer_size > r->in.buf_size) ? buffer_size : r->in.buf_size;
698
699         if (buffer_size > r->in.buf_size ) {
700                 ZERO_STRUCTP(r->out.query);
701                 return WERR_INSUFFICIENT_BUFFER;
702         }
703
704         return WERR_OK;
705 }
706
707 /********************************************************************
708  _svcctl_QueryServiceConfig2W
709 ********************************************************************/
710
711 WERROR _svcctl_QueryServiceConfig2W(pipes_struct *p,
712                                     struct svcctl_QueryServiceConfig2W *r)
713 {
714         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
715         uint32 buffer_size;
716
717         /* perform access checks */
718
719         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
720                 return WERR_BADFID;
721
722         if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_CONFIG) )
723                 return WERR_ACCESS_DENIED;
724
725         /* we have to set the outgoing buffer size to the same as the
726            incoming buffer size (even in the case of failure */
727
728         *r->out.bytes_needed = r->in.buf_size;
729
730         switch ( r->in.info_level ) {
731         case SERVICE_CONFIG_DESCRIPTION:
732                 {
733                         struct SERVICE_DESCRIPTION desc_buf;
734                         const char *description;
735                         enum ndr_err_code ndr_err;
736                         DATA_BLOB blob;
737
738                         description = svcctl_lookup_description(
739                                 p->mem_ctx, info->name, p->server_info->ptok);
740
741                         desc_buf.description = description;
742
743                         ndr_err = ndr_push_struct_blob(&blob, p->mem_ctx, NULL,
744                                                        &desc_buf,
745                                                        (ndr_push_flags_fn_t)ndr_push_SERVICE_DESCRIPTION);
746                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
747                                 return WERR_INVALID_PARAM;
748                         }
749
750                         buffer_size = ndr_size_SERVICE_DESCRIPTION(&desc_buf, NULL, 0);
751                         r->out.buffer = blob.data;
752
753                         break;
754                 }
755                 break;
756         case SERVICE_CONFIG_FAILURE_ACTIONS:
757                 {
758                         struct SERVICE_FAILURE_ACTIONS actions;
759                         enum ndr_err_code ndr_err;
760                         DATA_BLOB blob;
761
762                         /* nothing to say...just service the request */
763
764                         ZERO_STRUCT( actions );
765
766                         ndr_err = ndr_push_struct_blob(&blob, p->mem_ctx, NULL,
767                                                        &actions,
768                                                        (ndr_push_flags_fn_t)ndr_push_SERVICE_FAILURE_ACTIONS);
769                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
770                                 return WERR_INVALID_PARAM;
771                         }
772
773                         buffer_size = ndr_size_SERVICE_FAILURE_ACTIONS(&actions, NULL, 0);
774                         r->out.buffer = blob.data;
775
776                         break;
777                 }
778                 break;
779
780         default:
781                 return WERR_UNKNOWN_LEVEL;
782         }
783
784         buffer_size += buffer_size % 4;
785         *r->out.bytes_needed = (buffer_size > r->in.buf_size) ? buffer_size : r->in.buf_size;
786
787         if (buffer_size > r->in.buf_size )
788                 return WERR_INSUFFICIENT_BUFFER;
789
790         return WERR_OK;
791 }
792
793 /********************************************************************
794  _svcctl_LockServiceDatabase
795 ********************************************************************/
796
797 WERROR _svcctl_LockServiceDatabase(pipes_struct *p,
798                                    struct svcctl_LockServiceDatabase *r)
799 {
800         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
801
802         /* perform access checks */
803
804         if ( !info || (info->type != SVC_HANDLE_IS_SCM) )
805                 return WERR_BADFID;
806
807         if ( !(info->access_granted & SC_RIGHT_MGR_LOCK) )
808                 return WERR_ACCESS_DENIED;
809
810         /* Just open a handle.  Doesn't actually lock anything */
811
812         return create_open_service_handle( p, r->out.lock, SVC_HANDLE_IS_DBLOCK, NULL, 0 );
813 }
814
815 /********************************************************************
816  _svcctl_UnlockServiceDatabase
817 ********************************************************************/
818
819 WERROR _svcctl_UnlockServiceDatabase(pipes_struct *p,
820                                      struct svcctl_UnlockServiceDatabase *r)
821 {
822         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.lock );
823
824
825         if ( !info || (info->type != SVC_HANDLE_IS_DBLOCK) )
826                 return WERR_BADFID;
827
828         return close_policy_hnd( p, r->out.lock) ? WERR_OK : WERR_BADFID;
829 }
830
831 /********************************************************************
832  _svcctl_QueryServiceObjectSecurity
833 ********************************************************************/
834
835 WERROR _svcctl_QueryServiceObjectSecurity(pipes_struct *p,
836                                           struct svcctl_QueryServiceObjectSecurity *r)
837 {
838         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
839         SEC_DESC *sec_desc;
840         NTSTATUS status;
841         uint8_t *buffer = NULL;
842         size_t len = 0;
843
844
845         /* only support the SCM and individual services */
846
847         if ( !info || !(info->type & (SVC_HANDLE_IS_SERVICE|SVC_HANDLE_IS_SCM)) )
848                 return WERR_BADFID;
849
850         /* check access reights (according to MSDN) */
851
852         if ( !(info->access_granted & STD_RIGHT_READ_CONTROL_ACCESS) )
853                 return WERR_ACCESS_DENIED;
854
855         /* TODO: handle something besides DACL_SECURITY_INFORMATION */
856
857         if ( (r->in.security_flags & DACL_SECURITY_INFORMATION) != DACL_SECURITY_INFORMATION )
858                 return WERR_INVALID_PARAM;
859
860         /* lookup the security descriptor and marshall it up for a reply */
861
862         if ( !(sec_desc = svcctl_get_secdesc( p->mem_ctx, info->name, get_root_nt_token() )) )
863                 return WERR_NOMEM;
864
865         *r->out.needed = ndr_size_security_descriptor( sec_desc, NULL, 0 );
866
867         if ( *r->out.needed > r->in.buffer_size ) {
868                 ZERO_STRUCTP( &r->out.buffer );
869                 return WERR_INSUFFICIENT_BUFFER;
870         }
871
872         status = marshall_sec_desc(p->mem_ctx, sec_desc, &buffer, &len);
873         if (!NT_STATUS_IS_OK(status)) {
874                 return ntstatus_to_werror(status);
875         }
876
877         *r->out.needed = len;
878         r->out.buffer = buffer;
879
880         return WERR_OK;
881 }
882
883 /********************************************************************
884  _svcctl_SetServiceObjectSecurity
885 ********************************************************************/
886
887 WERROR _svcctl_SetServiceObjectSecurity(pipes_struct *p,
888                                         struct svcctl_SetServiceObjectSecurity *r)
889 {
890         SERVICE_INFO *info = find_service_info_by_hnd( p, r->in.handle );
891         SEC_DESC *sec_desc = NULL;
892         uint32 required_access;
893         NTSTATUS status;
894
895         if ( !info || !(info->type & (SVC_HANDLE_IS_SERVICE|SVC_HANDLE_IS_SCM))  )
896                 return WERR_BADFID;
897
898         /* can't set the security de4scriptor on the ServiceControlManager */
899
900         if ( info->type == SVC_HANDLE_IS_SCM )
901                 return WERR_ACCESS_DENIED;
902
903         /* check the access on the open handle */
904
905         switch ( r->in.security_flags ) {
906                 case DACL_SECURITY_INFORMATION:
907                         required_access = STD_RIGHT_WRITE_DAC_ACCESS;
908                         break;
909
910                 case OWNER_SECURITY_INFORMATION:
911                 case GROUP_SECURITY_INFORMATION:
912                         required_access = STD_RIGHT_WRITE_OWNER_ACCESS;
913                         break;
914
915                 case SACL_SECURITY_INFORMATION:
916                         return WERR_INVALID_PARAM;
917                 default:
918                         return WERR_INVALID_PARAM;
919         }
920
921         if ( !(info->access_granted & required_access) )
922                 return WERR_ACCESS_DENIED;
923
924         /* read the security descfriptor */
925
926         status = unmarshall_sec_desc(p->mem_ctx,
927                                      r->in.buffer, r->in.buffer_size,
928                                      &sec_desc);
929         if (!NT_STATUS_IS_OK(status)) {
930                 return ntstatus_to_werror(status);
931         }
932
933         /* store the new SD */
934
935         if ( !svcctl_set_secdesc( p->mem_ctx, info->name, sec_desc,
936                                   p->server_info->ptok) )
937                 return WERR_ACCESS_DENIED;
938
939         return WERR_OK;
940 }
941
942
943 WERROR _svcctl_DeleteService(pipes_struct *p, struct svcctl_DeleteService *r)
944 {
945         p->rng_fault_state = True;
946         return WERR_NOT_SUPPORTED;
947 }
948
949 WERROR _svcctl_SetServiceStatus(pipes_struct *p, struct svcctl_SetServiceStatus *r)
950 {
951         p->rng_fault_state = True;
952         return WERR_NOT_SUPPORTED;
953 }
954
955 WERROR _svcctl_NotifyBootConfigStatus(pipes_struct *p, struct svcctl_NotifyBootConfigStatus *r)
956 {
957         p->rng_fault_state = True;
958         return WERR_NOT_SUPPORTED;
959 }
960
961 WERROR _svcctl_SCSetServiceBitsW(pipes_struct *p, struct svcctl_SCSetServiceBitsW *r)
962 {
963         p->rng_fault_state = True;
964         return WERR_NOT_SUPPORTED;
965 }
966
967 WERROR _svcctl_ChangeServiceConfigW(pipes_struct *p, struct svcctl_ChangeServiceConfigW *r)
968 {
969         p->rng_fault_state = True;
970         return WERR_NOT_SUPPORTED;
971 }
972
973 WERROR _svcctl_CreateServiceW(pipes_struct *p, struct svcctl_CreateServiceW *r)
974 {
975         p->rng_fault_state = True;
976         return WERR_NOT_SUPPORTED;
977 }
978
979 WERROR _svcctl_EnumServicesStatusW(pipes_struct *p, struct svcctl_EnumServicesStatusW *r)
980 {
981         p->rng_fault_state = True;
982         return WERR_NOT_SUPPORTED;
983 }
984
985 WERROR _svcctl_QueryServiceLockStatusW(pipes_struct *p, struct svcctl_QueryServiceLockStatusW *r)
986 {
987         p->rng_fault_state = True;
988         return WERR_NOT_SUPPORTED;
989 }
990
991 WERROR _svcctl_GetServiceKeyNameW(pipes_struct *p, struct svcctl_GetServiceKeyNameW *r)
992 {
993         p->rng_fault_state = True;
994         return WERR_NOT_SUPPORTED;
995 }
996
997 WERROR _svcctl_SCSetServiceBitsA(pipes_struct *p, struct svcctl_SCSetServiceBitsA *r)
998 {
999         p->rng_fault_state = True;
1000         return WERR_NOT_SUPPORTED;
1001 }
1002
1003 WERROR _svcctl_ChangeServiceConfigA(pipes_struct *p, struct svcctl_ChangeServiceConfigA *r)
1004 {
1005         p->rng_fault_state = True;
1006         return WERR_NOT_SUPPORTED;
1007 }
1008
1009 WERROR _svcctl_CreateServiceA(pipes_struct *p, struct svcctl_CreateServiceA *r)
1010 {
1011         p->rng_fault_state = True;
1012         return WERR_NOT_SUPPORTED;
1013 }
1014
1015 WERROR _svcctl_EnumDependentServicesA(pipes_struct *p, struct svcctl_EnumDependentServicesA *r)
1016 {
1017         p->rng_fault_state = True;
1018         return WERR_NOT_SUPPORTED;
1019 }
1020
1021 WERROR _svcctl_EnumServicesStatusA(pipes_struct *p, struct svcctl_EnumServicesStatusA *r)
1022 {
1023         p->rng_fault_state = True;
1024         return WERR_NOT_SUPPORTED;
1025 }
1026
1027 WERROR _svcctl_OpenSCManagerA(pipes_struct *p, struct svcctl_OpenSCManagerA *r)
1028 {
1029         p->rng_fault_state = True;
1030         return WERR_NOT_SUPPORTED;
1031 }
1032
1033 WERROR _svcctl_OpenServiceA(pipes_struct *p, struct svcctl_OpenServiceA *r)
1034 {
1035         p->rng_fault_state = True;
1036         return WERR_NOT_SUPPORTED;
1037 }
1038
1039 WERROR _svcctl_QueryServiceConfigA(pipes_struct *p, struct svcctl_QueryServiceConfigA *r)
1040 {
1041         p->rng_fault_state = True;
1042         return WERR_NOT_SUPPORTED;
1043 }
1044
1045 WERROR _svcctl_QueryServiceLockStatusA(pipes_struct *p, struct svcctl_QueryServiceLockStatusA *r)
1046 {
1047         p->rng_fault_state = True;
1048         return WERR_NOT_SUPPORTED;
1049 }
1050
1051 WERROR _svcctl_StartServiceA(pipes_struct *p, struct svcctl_StartServiceA *r)
1052 {
1053         p->rng_fault_state = True;
1054         return WERR_NOT_SUPPORTED;
1055 }
1056
1057 WERROR _svcctl_GetServiceDisplayNameA(pipes_struct *p, struct svcctl_GetServiceDisplayNameA *r)
1058 {
1059         p->rng_fault_state = True;
1060         return WERR_NOT_SUPPORTED;
1061 }
1062
1063 WERROR _svcctl_GetServiceKeyNameA(pipes_struct *p, struct svcctl_GetServiceKeyNameA *r)
1064 {
1065         p->rng_fault_state = True;
1066         return WERR_NOT_SUPPORTED;
1067 }
1068
1069 WERROR _svcctl_GetCurrentGroupeStateW(pipes_struct *p, struct svcctl_GetCurrentGroupeStateW *r)
1070 {
1071         p->rng_fault_state = True;
1072         return WERR_NOT_SUPPORTED;
1073 }
1074
1075 WERROR _svcctl_EnumServiceGroupW(pipes_struct *p, struct svcctl_EnumServiceGroupW *r)
1076 {
1077         p->rng_fault_state = True;
1078         return WERR_NOT_SUPPORTED;
1079 }
1080
1081 WERROR _svcctl_ChangeServiceConfig2A(pipes_struct *p, struct svcctl_ChangeServiceConfig2A *r)
1082 {
1083         p->rng_fault_state = True;
1084         return WERR_NOT_SUPPORTED;
1085 }
1086
1087 WERROR _svcctl_ChangeServiceConfig2W(pipes_struct *p, struct svcctl_ChangeServiceConfig2W *r)
1088 {
1089         p->rng_fault_state = True;
1090         return WERR_NOT_SUPPORTED;
1091 }
1092
1093 WERROR _svcctl_QueryServiceConfig2A(pipes_struct *p, struct svcctl_QueryServiceConfig2A *r)
1094 {
1095         p->rng_fault_state = True;
1096         return WERR_NOT_SUPPORTED;
1097 }
1098
1099 WERROR _EnumServicesStatusExA(pipes_struct *p, struct EnumServicesStatusExA *r)
1100 {
1101         p->rng_fault_state = True;
1102         return WERR_NOT_SUPPORTED;
1103 }
1104
1105 WERROR _EnumServicesStatusExW(pipes_struct *p, struct EnumServicesStatusExW *r)
1106 {
1107         p->rng_fault_state = True;
1108         return WERR_NOT_SUPPORTED;
1109 }
1110
1111 WERROR _svcctl_SCSendTSMessage(pipes_struct *p, struct svcctl_SCSendTSMessage *r)
1112 {
1113         p->rng_fault_state = True;
1114         return WERR_NOT_SUPPORTED;
1115 }
1116