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