Use pidl for _svcctl_OpenServiceW().
[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  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 3 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
22  */
23
24 #include "includes.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_RPC_SRV
28
29 struct service_control_op {
30         const char *name;
31         SERVICE_CONTROL_OPS *ops;
32 };
33
34 #define SVCCTL_NUM_INTERNAL_SERVICES    4
35
36 /* handle external services */
37 extern SERVICE_CONTROL_OPS rcinit_svc_ops;
38
39 /* builtin services (see service_db.c and services/svc_*.c */
40 extern SERVICE_CONTROL_OPS spoolss_svc_ops;
41 extern SERVICE_CONTROL_OPS netlogon_svc_ops;
42 extern SERVICE_CONTROL_OPS winreg_svc_ops;
43 extern SERVICE_CONTROL_OPS wins_svc_ops;
44
45 /* make sure this number patches the number of builtin
46    SERVICE_CONTROL_OPS structure listed above */
47
48 #define SVCCTL_NUM_INTERNAL_SERVICES    4
49
50 struct service_control_op *svcctl_ops;
51
52 static const struct generic_mapping scm_generic_map =
53         { SC_MANAGER_READ_ACCESS, SC_MANAGER_WRITE_ACCESS, SC_MANAGER_EXECUTE_ACCESS, SC_MANAGER_ALL_ACCESS };
54 static const struct generic_mapping svc_generic_map =
55         { SERVICE_READ_ACCESS, SERVICE_WRITE_ACCESS, SERVICE_EXECUTE_ACCESS, SERVICE_ALL_ACCESS };
56
57
58 /********************************************************************
59 ********************************************************************/
60
61 bool init_service_op_table( void )
62 {
63         const char **service_list = lp_svcctl_list();
64         int num_services = SVCCTL_NUM_INTERNAL_SERVICES + str_list_count( service_list );
65         int i;
66
67         if ( !(svcctl_ops = TALLOC_ARRAY( NULL, struct service_control_op, num_services+1)) ) {
68                 DEBUG(0,("init_service_op_table: talloc() failed!\n"));
69                 return False;
70         }
71
72         /* services listed in smb.conf get the rc.init interface */
73
74         for ( i=0; service_list && service_list[i]; i++ ) {
75                 svcctl_ops[i].name = talloc_strdup( svcctl_ops, service_list[i] );
76                 svcctl_ops[i].ops  = &rcinit_svc_ops;
77         }
78
79         /* add builtin services */
80
81         svcctl_ops[i].name = talloc_strdup( svcctl_ops, "Spooler" );
82         svcctl_ops[i].ops  = &spoolss_svc_ops;
83         i++;
84
85         svcctl_ops[i].name = talloc_strdup( svcctl_ops, "NETLOGON" );
86         svcctl_ops[i].ops  = &netlogon_svc_ops;
87         i++;
88
89         svcctl_ops[i].name = talloc_strdup( svcctl_ops, "RemoteRegistry" );
90         svcctl_ops[i].ops  = &winreg_svc_ops;
91         i++;
92
93         svcctl_ops[i].name = talloc_strdup( svcctl_ops, "WINS" );
94         svcctl_ops[i].ops  = &wins_svc_ops;
95         i++;
96
97         /* NULL terminate the array */
98
99         svcctl_ops[i].name = NULL;
100         svcctl_ops[i].ops  = NULL;
101
102         return True;
103 }
104
105 /********************************************************************
106 ********************************************************************/
107
108 static struct service_control_op* find_service_by_name( const char *name )
109 {
110         int i;
111
112         for ( i=0; svcctl_ops[i].name; i++ ) {
113                 if ( strequal( name, svcctl_ops[i].name ) )
114                         return &svcctl_ops[i];
115         }
116
117         return NULL;
118 }
119 /********************************************************************
120 ********************************************************************/
121
122 static NTSTATUS svcctl_access_check( SEC_DESC *sec_desc, NT_USER_TOKEN *token,
123                                      uint32 access_desired, uint32 *access_granted )
124 {
125         NTSTATUS result;
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         se_access_check( sec_desc, token, access_desired, access_granted, &result );
133
134         return result;
135 }
136
137 /********************************************************************
138 ********************************************************************/
139
140 static SEC_DESC* construct_scm_sd( TALLOC_CTX *ctx )
141 {
142         SEC_ACE ace[2];
143         SEC_ACCESS mask;
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_access(&mask, SC_MANAGER_READ_ACCESS );
152         init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
153
154         /* Full Access 'BUILTIN\Administrators' */
155
156         init_sec_access(&mask,SC_MANAGER_ALL_ACCESS );
157         init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 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"));
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         return WERR_OK;
332 }
333
334 /********************************************************************
335 ********************************************************************/
336
337 WERROR _svcctl_get_display_name(pipes_struct *p, SVCCTL_Q_GET_DISPLAY_NAME *q_u, SVCCTL_R_GET_DISPLAY_NAME *r_u)
338 {
339         fstring service;
340         const char *display_name;
341         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
342
343         /* can only use an SCM handle here */
344
345         if ( !info || (info->type != SVC_HANDLE_IS_SCM) )
346                 return WERR_BADFID;
347
348         rpcstr_pull(service, q_u->servicename.buffer, sizeof(service), q_u->servicename.uni_str_len*2, 0);
349
350         display_name = svcctl_lookup_dispname(p->mem_ctx, service, p->pipe_user.nt_user_token );
351         init_svcctl_r_get_display_name( r_u, display_name ? display_name : "");
352
353         return WERR_OK;
354 }
355
356 /********************************************************************
357 ********************************************************************/
358
359 WERROR _svcctl_query_status(pipes_struct *p, SVCCTL_Q_QUERY_STATUS *q_u, SVCCTL_R_QUERY_STATUS *r_u)
360 {
361         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
362
363         /* perform access checks */
364
365         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
366                 return WERR_BADFID;
367
368         if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_STATUS) )
369                 return WERR_ACCESS_DENIED;
370
371         /* try the service specific status call */
372
373         return info->ops->service_status( info->name, &r_u->svc_status );
374 }
375
376 /********************************************************************
377 ********************************************************************/
378
379 static int enumerate_status( TALLOC_CTX *ctx, ENUM_SERVICES_STATUS **status, NT_USER_TOKEN *token )
380 {
381         int num_services = 0;
382         int i;
383         ENUM_SERVICES_STATUS *st;
384         const char *display_name;
385
386         /* just count */
387         while ( svcctl_ops[num_services].name )
388                 num_services++;
389
390         if ( !(st = TALLOC_ARRAY( ctx, ENUM_SERVICES_STATUS, num_services )) ) {
391                 DEBUG(0,("enumerate_status: talloc() failed!\n"));
392                 return -1;
393         }
394
395         for ( i=0; i<num_services; i++ ) {
396                 init_unistr( &st[i].servicename, svcctl_ops[i].name );
397
398                 display_name = svcctl_lookup_dispname(ctx, svcctl_ops[i].name, token );
399                 init_unistr( &st[i].displayname, display_name ? display_name : "");
400
401                 svcctl_ops[i].ops->service_status( svcctl_ops[i].name, &st[i].status );
402         }
403
404         *status = st;
405
406         return num_services;
407 }
408
409 /********************************************************************
410 ********************************************************************/
411
412 WERROR _svcctl_enum_services_status(pipes_struct *p, SVCCTL_Q_ENUM_SERVICES_STATUS *q_u, SVCCTL_R_ENUM_SERVICES_STATUS *r_u)
413 {
414         ENUM_SERVICES_STATUS *services = NULL;
415         int num_services;
416         int i = 0;
417         size_t buffer_size = 0;
418         WERROR result = WERR_OK;
419         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
420         NT_USER_TOKEN *token = p->pipe_user.nt_user_token;
421
422         /* perform access checks */
423
424         if ( !info || (info->type != SVC_HANDLE_IS_SCM) )
425                 return WERR_BADFID;
426
427         if ( !(info->access_granted & SC_RIGHT_MGR_ENUMERATE_SERVICE) ) {
428                 return WERR_ACCESS_DENIED;
429         }
430
431         num_services = enumerate_status( p->mem_ctx, &services, token );
432         if (num_services == -1 ) {
433                 return WERR_NOMEM;
434         }
435
436         for ( i=0; i<num_services; i++ ) {
437                 buffer_size += svcctl_sizeof_enum_services_status(&services[i]);
438         }
439
440         buffer_size += buffer_size % 4;
441
442         if (buffer_size > q_u->buffer_size ) {
443                 num_services = 0;
444                 result = WERR_MORE_DATA;
445         }
446
447         rpcbuf_init(&r_u->buffer, q_u->buffer_size, p->mem_ctx);
448
449         if ( W_ERROR_IS_OK(result) ) {
450                 for ( i=0; i<num_services; i++ )
451                         svcctl_io_enum_services_status( "", &services[i], &r_u->buffer, 0 );
452         }
453
454         r_u->needed      = (buffer_size > q_u->buffer_size) ? buffer_size : q_u->buffer_size;
455         r_u->returned    = (uint32)num_services;
456
457         if ( !(r_u->resume = TALLOC_P( p->mem_ctx, uint32 )) )
458                 return WERR_NOMEM;
459
460         *r_u->resume = 0x0;
461
462         return result;
463 }
464
465 /********************************************************************
466 ********************************************************************/
467
468 WERROR _svcctl_start_service(pipes_struct *p, SVCCTL_Q_START_SERVICE *q_u, SVCCTL_R_START_SERVICE *r_u)
469 {
470         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
471
472         /* perform access checks */
473
474         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
475                 return WERR_BADFID;
476
477         if ( !(info->access_granted & SC_RIGHT_SVC_START) )
478                 return WERR_ACCESS_DENIED;
479
480         return info->ops->start_service( info->name );
481 }
482
483 /********************************************************************
484 ********************************************************************/
485
486 WERROR _svcctl_control_service(pipes_struct *p, SVCCTL_Q_CONTROL_SERVICE *q_u, SVCCTL_R_CONTROL_SERVICE *r_u)
487 {
488         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
489
490         /* perform access checks */
491
492         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
493                 return WERR_BADFID;
494
495         switch ( q_u->control ) {
496         case SVCCTL_CONTROL_STOP:
497                 if ( !(info->access_granted & SC_RIGHT_SVC_STOP) )
498                         return WERR_ACCESS_DENIED;
499
500                 return info->ops->stop_service( info->name, &r_u->svc_status );
501
502         case SVCCTL_CONTROL_INTERROGATE:
503                 if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_STATUS) )
504                         return WERR_ACCESS_DENIED;
505
506                 return info->ops->service_status( info->name, &r_u->svc_status );
507         }
508
509         /* default control action */
510
511         return WERR_ACCESS_DENIED;
512 }
513
514 /********************************************************************
515 ********************************************************************/
516
517 WERROR _svcctl_enum_dependent_services( pipes_struct *p, SVCCTL_Q_ENUM_DEPENDENT_SERVICES *q_u, SVCCTL_R_ENUM_DEPENDENT_SERVICES *r_u )
518 {
519         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
520
521         /* perform access checks */
522
523         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
524                 return WERR_BADFID;
525
526         if ( !(info->access_granted & SC_RIGHT_SVC_ENUMERATE_DEPENDENTS) )
527                 return WERR_ACCESS_DENIED;
528
529         /* we have to set the outgoing buffer size to the same as the
530            incoming buffer size (even in the case of failure */
531
532         rpcbuf_init( &r_u->buffer, q_u->buffer_size, p->mem_ctx );
533
534         r_u->needed      = q_u->buffer_size;
535
536         /* no dependent services...basically a stub function */
537         r_u->returned    = 0;
538
539         return WERR_OK;
540 }
541
542 /********************************************************************
543 ********************************************************************/
544
545 WERROR _svcctl_query_service_status_ex( pipes_struct *p, SVCCTL_Q_QUERY_SERVICE_STATUSEX *q_u, SVCCTL_R_QUERY_SERVICE_STATUSEX *r_u )
546 {
547         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
548         uint32 buffer_size;
549
550         /* perform access checks */
551
552         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
553                 return WERR_BADFID;
554
555         if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_STATUS) )
556                 return WERR_ACCESS_DENIED;
557
558         /* we have to set the outgoing buffer size to the same as the
559            incoming buffer size (even in the case of failure) */
560
561         rpcbuf_init( &r_u->buffer, q_u->buffer_size, p->mem_ctx );
562         r_u->needed = q_u->buffer_size;
563
564         switch ( q_u->level ) {
565                 case SVC_STATUS_PROCESS_INFO:
566                 {
567                         SERVICE_STATUS_PROCESS svc_stat_proc;
568
569                         /* Get the status of the service.. */
570                         info->ops->service_status( info->name, &svc_stat_proc.status );
571                         svc_stat_proc.process_id     = sys_getpid();
572                         svc_stat_proc.service_flags  = 0x0;
573
574                         svcctl_io_service_status_process( "", &svc_stat_proc, &r_u->buffer, 0 );
575                         buffer_size = sizeof(SERVICE_STATUS_PROCESS);
576                         break;
577                 }
578
579                 default:
580                         return WERR_UNKNOWN_LEVEL;
581         }
582
583
584         buffer_size += buffer_size % 4;
585         r_u->needed = (buffer_size > q_u->buffer_size) ? buffer_size : q_u->buffer_size;
586
587         if (buffer_size > q_u->buffer_size )
588                 return WERR_MORE_DATA;
589
590         return WERR_OK;
591 }
592
593 /********************************************************************
594 ********************************************************************/
595
596 static WERROR fill_svc_config( TALLOC_CTX *ctx, const char *name, SERVICE_CONFIG *config, NT_USER_TOKEN *token )
597 {
598         REGVAL_CTR *values;
599         REGISTRY_VALUE *val;
600
601         /* retrieve the registry values for this service */
602
603         if ( !(values = svcctl_fetch_regvalues( name, token )) )
604                 return WERR_REG_CORRUPT;
605
606         /* now fill in the individual values */
607
608         config->displayname = TALLOC_ZERO_P( ctx, UNISTR2 );
609         if ( (val = regval_ctr_getvalue( values, "DisplayName" )) != NULL )
610                 init_unistr2( config->displayname, regval_sz( val ), UNI_STR_TERMINATE );
611         else
612                 init_unistr2( config->displayname, name, UNI_STR_TERMINATE );
613
614         if ( (val = regval_ctr_getvalue( values, "ObjectName" )) != NULL ) {
615                 config->startname = TALLOC_ZERO_P( ctx, UNISTR2 );
616                 init_unistr2( config->startname, regval_sz( val ), UNI_STR_TERMINATE );
617         }
618
619         if ( (val = regval_ctr_getvalue( values, "ImagePath" )) != NULL ) {
620                 config->executablepath = TALLOC_ZERO_P( ctx, UNISTR2 );
621                 init_unistr2( config->executablepath, regval_sz( val ), UNI_STR_TERMINATE );
622         }
623
624         /* a few hard coded values */
625         /* loadordergroup and dependencies are empty */
626
627         config->tag_id           = 0x00000000;                  /* unassigned loadorder group */
628         config->service_type     = SVCCTL_WIN32_OWN_PROC;
629         config->error_control    = SVCCTL_SVC_ERROR_NORMAL;
630
631         /* set the start type.  NetLogon and WINS are disabled to prevent
632            the client from showing the "Start" button (if of course the services
633            are not running */
634
635         if ( strequal( name, "NETLOGON" ) && ( lp_servicenumber(name) == -1 ) )
636                 config->start_type = SVCCTL_DISABLED;
637         else if ( strequal( name, "WINS" ) && ( !lp_wins_support() ))
638                 config->start_type = SVCCTL_DISABLED;
639         else
640                 config->start_type = SVCCTL_DEMAND_START;
641
642
643         TALLOC_FREE( values );
644
645         return WERR_OK;
646 }
647
648 /********************************************************************
649 ********************************************************************/
650
651 WERROR _svcctl_query_service_config( pipes_struct *p, SVCCTL_Q_QUERY_SERVICE_CONFIG *q_u, SVCCTL_R_QUERY_SERVICE_CONFIG *r_u )
652 {
653         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
654         uint32 buffer_size;
655         WERROR wresult;
656
657         /* perform access checks */
658
659         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
660                 return WERR_BADFID;
661
662         if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_CONFIG) )
663                 return WERR_ACCESS_DENIED;
664
665         /* we have to set the outgoing buffer size to the same as the
666            incoming buffer size (even in the case of failure */
667
668         r_u->needed      = q_u->buffer_size;
669
670         wresult = fill_svc_config( p->mem_ctx, info->name, &r_u->config, p->pipe_user.nt_user_token );
671         if ( !W_ERROR_IS_OK(wresult) )
672                 return wresult;
673
674         buffer_size = svcctl_sizeof_service_config( &r_u->config );
675         r_u->needed = (buffer_size > q_u->buffer_size) ? buffer_size : q_u->buffer_size;
676
677         if (buffer_size > q_u->buffer_size ) {
678                 ZERO_STRUCTP( &r_u->config );
679                 return WERR_INSUFFICIENT_BUFFER;
680         }
681
682         return WERR_OK;
683 }
684
685 /********************************************************************
686 ********************************************************************/
687
688 WERROR _svcctl_query_service_config2( pipes_struct *p, SVCCTL_Q_QUERY_SERVICE_CONFIG2 *q_u, SVCCTL_R_QUERY_SERVICE_CONFIG2 *r_u )
689 {
690         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
691         uint32 buffer_size;
692
693         /* perform access checks */
694
695         if ( !info || (info->type != SVC_HANDLE_IS_SERVICE) )
696                 return WERR_BADFID;
697
698         if ( !(info->access_granted & SC_RIGHT_SVC_QUERY_CONFIG) )
699                 return WERR_ACCESS_DENIED;
700
701         /* we have to set the outgoing buffer size to the same as the
702            incoming buffer size (even in the case of failure */
703
704         rpcbuf_init( &r_u->buffer, q_u->buffer_size, p->mem_ctx );
705         r_u->needed = q_u->buffer_size;
706
707         switch ( q_u->level ) {
708         case SERVICE_CONFIG_DESCRIPTION:
709                 {
710                         SERVICE_DESCRIPTION desc_buf;
711                         const char *description;
712
713                         description = svcctl_lookup_description(p->mem_ctx, info->name, p->pipe_user.nt_user_token );
714
715                         ZERO_STRUCTP( &desc_buf );
716
717                         init_service_description_buffer( &desc_buf, description ? description : "");
718                         svcctl_io_service_description( "", &desc_buf, &r_u->buffer, 0 );
719                         buffer_size = svcctl_sizeof_service_description( &desc_buf );
720
721                         break;
722                 }
723                 break;
724         case SERVICE_CONFIG_FAILURE_ACTIONS:
725                 {
726                         SERVICE_FAILURE_ACTIONS actions;
727
728                         /* nothing to say...just service the request */
729
730                         ZERO_STRUCTP( &actions );
731                         svcctl_io_service_fa( "", &actions, &r_u->buffer, 0 );
732                         buffer_size = svcctl_sizeof_service_fa( &actions );
733
734                         break;
735                 }
736                 break;
737
738         default:
739                 return WERR_UNKNOWN_LEVEL;
740         }
741
742         buffer_size += buffer_size % 4;
743         r_u->needed = (buffer_size > q_u->buffer_size) ? buffer_size : q_u->buffer_size;
744
745         if (buffer_size > q_u->buffer_size )
746                 return WERR_INSUFFICIENT_BUFFER;
747
748         return WERR_OK;
749 }
750
751 /********************************************************************
752 ********************************************************************/
753
754 WERROR _svcctl_lock_service_db( pipes_struct *p, SVCCTL_Q_LOCK_SERVICE_DB *q_u, SVCCTL_R_LOCK_SERVICE_DB *r_u )
755 {
756         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
757
758         /* perform access checks */
759
760         if ( !info || (info->type != SVC_HANDLE_IS_SCM) )
761                 return WERR_BADFID;
762
763         if ( !(info->access_granted & SC_RIGHT_MGR_LOCK) )
764                 return WERR_ACCESS_DENIED;
765
766         /* Just open a handle.  Doesn't actually lock anything */
767
768         return create_open_service_handle( p, &r_u->h_lock, SVC_HANDLE_IS_DBLOCK, NULL, 0 );
769 ;
770 }
771
772 /********************************************************************
773 ********************************************************************/
774
775 WERROR _svcctl_unlock_service_db( pipes_struct *p, SVCCTL_Q_UNLOCK_SERVICE_DB *q_u, SVCCTL_R_UNLOCK_SERVICE_DB *r_u )
776 {
777         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->h_lock );
778
779
780         if ( !info || (info->type != SVC_HANDLE_IS_DBLOCK) )
781                 return WERR_BADFID;
782
783         return close_policy_hnd( p, &q_u->h_lock) ? WERR_OK : WERR_BADFID;
784 }
785
786 /********************************************************************
787 ********************************************************************/
788
789 WERROR _svcctl_query_service_sec( pipes_struct *p, SVCCTL_Q_QUERY_SERVICE_SEC *q_u, SVCCTL_R_QUERY_SERVICE_SEC *r_u )
790 {
791         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
792         SEC_DESC *sec_desc;
793
794
795         /* only support the SCM and individual services */
796
797         if ( !info || !(info->type & (SVC_HANDLE_IS_SERVICE|SVC_HANDLE_IS_SCM)) )
798                 return WERR_BADFID;
799
800         /* check access reights (according to MSDN) */
801
802         if ( !(info->access_granted & STD_RIGHT_READ_CONTROL_ACCESS) )
803                 return WERR_ACCESS_DENIED;
804
805         /* TODO: handle something besides DACL_SECURITY_INFORMATION */
806
807         if ( (q_u->security_flags & DACL_SECURITY_INFORMATION) != DACL_SECURITY_INFORMATION )
808                 return WERR_INVALID_PARAM;
809
810         /* lookup the security descriptor and marshall it up for a reply */
811
812         if ( !(sec_desc = svcctl_get_secdesc( p->mem_ctx, info->name, get_root_nt_token() )) )
813                 return WERR_NOMEM;
814
815         r_u->needed = ndr_size_security_descriptor( sec_desc, 0 );
816
817         if ( r_u->needed > q_u->buffer_size ) {
818                 ZERO_STRUCTP( &r_u->buffer );
819                 return WERR_INSUFFICIENT_BUFFER;
820         }
821
822         rpcbuf_init( &r_u->buffer, q_u->buffer_size, p->mem_ctx );
823
824         if ( !sec_io_desc("", &sec_desc, &r_u->buffer.prs, 0 ) )
825                 return WERR_NOMEM;
826
827         return WERR_OK;
828 }
829
830 /********************************************************************
831 ********************************************************************/
832
833 WERROR _svcctl_set_service_sec( pipes_struct *p, SVCCTL_Q_SET_SERVICE_SEC *q_u, SVCCTL_R_SET_SERVICE_SEC *r_u )
834 {
835         SERVICE_INFO *info = find_service_info_by_hnd( p, &q_u->handle );
836         SEC_DESC *sec_desc = NULL;
837         uint32 required_access;
838
839         if ( !info || !(info->type & (SVC_HANDLE_IS_SERVICE|SVC_HANDLE_IS_SCM))  )
840                 return WERR_BADFID;
841
842         /* can't set the security de4scriptor on the ServiceControlManager */
843
844         if ( info->type == SVC_HANDLE_IS_SCM )
845                 return WERR_ACCESS_DENIED;
846
847         /* check the access on the open handle */
848
849         switch ( q_u->security_flags ) {
850                 case DACL_SECURITY_INFORMATION:
851                         required_access = STD_RIGHT_WRITE_DAC_ACCESS;
852                         break;
853
854                 case OWNER_SECURITY_INFORMATION:
855                 case GROUP_SECURITY_INFORMATION:
856                         required_access = STD_RIGHT_WRITE_OWNER_ACCESS;
857                         break;
858
859                 case SACL_SECURITY_INFORMATION:
860                         return WERR_INVALID_PARAM;
861                 default:
862                         return WERR_INVALID_PARAM;
863         }
864
865         if ( !(info->access_granted & required_access) )
866                 return WERR_ACCESS_DENIED;
867
868         /* read the security descfriptor */
869
870         if ( !sec_io_desc("", &sec_desc, &q_u->buffer.prs, 0 ) )
871                 return WERR_NOMEM;
872
873         /* store the new SD */
874
875         if ( !svcctl_set_secdesc( p->mem_ctx, info->name, sec_desc, p->pipe_user.nt_user_token ) )
876                 return WERR_ACCESS_DENIED;
877
878         return WERR_OK;
879 }
880
881
882 WERROR _svcctl_ControlService(pipes_struct *p, struct svcctl_ControlService *r)
883 {
884         p->rng_fault_state = True;
885         return WERR_NOT_SUPPORTED;
886 }
887
888 WERROR _svcctl_DeleteService(pipes_struct *p, struct svcctl_DeleteService *r)
889 {
890         p->rng_fault_state = True;
891         return WERR_NOT_SUPPORTED;
892 }
893
894 WERROR _svcctl_LockServiceDatabase(pipes_struct *p, struct svcctl_LockServiceDatabase *r)
895 {
896         p->rng_fault_state = True;
897         return WERR_NOT_SUPPORTED;
898 }
899
900 WERROR _svcctl_QueryServiceObjectSecurity(pipes_struct *p, struct svcctl_QueryServiceObjectSecurity *r)
901 {
902         p->rng_fault_state = True;
903         return WERR_NOT_SUPPORTED;
904 }
905
906 WERROR _svcctl_SetServiceObjectSecurity(pipes_struct *p, struct svcctl_SetServiceObjectSecurity *r)
907 {
908         p->rng_fault_state = True;
909         return WERR_NOT_SUPPORTED;
910 }
911
912 WERROR _svcctl_QueryServiceStatus(pipes_struct *p, struct svcctl_QueryServiceStatus *r)
913 {
914         p->rng_fault_state = True;
915         return WERR_NOT_SUPPORTED;
916 }
917
918 WERROR _svcctl_SetServiceStatus(pipes_struct *p, struct svcctl_SetServiceStatus *r)
919 {
920         p->rng_fault_state = True;
921         return WERR_NOT_SUPPORTED;
922 }
923
924 WERROR _svcctl_UnlockServiceDatabase(pipes_struct *p, struct svcctl_UnlockServiceDatabase *r)
925 {
926         p->rng_fault_state = True;
927         return WERR_NOT_SUPPORTED;
928 }
929
930 WERROR _svcctl_NotifyBootConfigStatus(pipes_struct *p, struct svcctl_NotifyBootConfigStatus *r)
931 {
932         p->rng_fault_state = True;
933         return WERR_NOT_SUPPORTED;
934 }
935
936 WERROR _svcctl_SCSetServiceBitsW(pipes_struct *p, struct svcctl_SCSetServiceBitsW *r)
937 {
938         p->rng_fault_state = True;
939         return WERR_NOT_SUPPORTED;
940 }
941
942 WERROR _svcctl_ChangeServiceConfigW(pipes_struct *p, struct svcctl_ChangeServiceConfigW *r)
943 {
944         p->rng_fault_state = True;
945         return WERR_NOT_SUPPORTED;
946 }
947
948 WERROR _svcctl_CreateServiceW(pipes_struct *p, struct svcctl_CreateServiceW *r)
949 {
950         p->rng_fault_state = True;
951         return WERR_NOT_SUPPORTED;
952 }
953
954 WERROR _svcctl_EnumDependentServicesW(pipes_struct *p, struct svcctl_EnumDependentServicesW *r)
955 {
956         p->rng_fault_state = True;
957         return WERR_NOT_SUPPORTED;
958 }
959
960 WERROR _svcctl_EnumServicesStatusW(pipes_struct *p, struct svcctl_EnumServicesStatusW *r)
961 {
962         p->rng_fault_state = True;
963         return WERR_NOT_SUPPORTED;
964 }
965
966 WERROR _svcctl_QueryServiceConfigW(pipes_struct *p, struct svcctl_QueryServiceConfigW *r)
967 {
968         p->rng_fault_state = True;
969         return WERR_NOT_SUPPORTED;
970 }
971
972 WERROR _svcctl_QueryServiceLockStatusW(pipes_struct *p, struct svcctl_QueryServiceLockStatusW *r)
973 {
974         p->rng_fault_state = True;
975         return WERR_NOT_SUPPORTED;
976 }
977
978 WERROR _svcctl_StartServiceW(pipes_struct *p, struct svcctl_StartServiceW *r)
979 {
980         p->rng_fault_state = True;
981         return WERR_NOT_SUPPORTED;
982 }
983
984 WERROR _svcctl_GetServiceDisplayNameW(pipes_struct *p, struct svcctl_GetServiceDisplayNameW *r)
985 {
986         p->rng_fault_state = True;
987         return WERR_NOT_SUPPORTED;
988 }
989
990 WERROR _svcctl_GetServiceKeyNameW(pipes_struct *p, struct svcctl_GetServiceKeyNameW *r)
991 {
992         p->rng_fault_state = True;
993         return WERR_NOT_SUPPORTED;
994 }
995
996 WERROR _svcctl_SCSetServiceBitsA(pipes_struct *p, struct svcctl_SCSetServiceBitsA *r)
997 {
998         p->rng_fault_state = True;
999         return WERR_NOT_SUPPORTED;
1000 }
1001
1002 WERROR _svcctl_ChangeServiceConfigA(pipes_struct *p, struct svcctl_ChangeServiceConfigA *r)
1003 {
1004         p->rng_fault_state = True;
1005         return WERR_NOT_SUPPORTED;
1006 }
1007
1008 WERROR _svcctl_CreateServiceA(pipes_struct *p, struct svcctl_CreateServiceA *r)
1009 {
1010         p->rng_fault_state = True;
1011         return WERR_NOT_SUPPORTED;
1012 }
1013
1014 WERROR _svcctl_EnumDependentServicesA(pipes_struct *p, struct svcctl_EnumDependentServicesA *r)
1015 {
1016         p->rng_fault_state = True;
1017         return WERR_NOT_SUPPORTED;
1018 }
1019
1020 WERROR _svcctl_EnumServicesStatusA(pipes_struct *p, struct svcctl_EnumServicesStatusA *r)
1021 {
1022         p->rng_fault_state = True;
1023         return WERR_NOT_SUPPORTED;
1024 }
1025
1026 WERROR _svcctl_OpenSCManagerA(pipes_struct *p, struct svcctl_OpenSCManagerA *r)
1027 {
1028         p->rng_fault_state = True;
1029         return WERR_NOT_SUPPORTED;
1030 }
1031
1032 WERROR _svcctl_OpenServiceA(pipes_struct *p, struct svcctl_OpenServiceA *r)
1033 {
1034         p->rng_fault_state = True;
1035         return WERR_NOT_SUPPORTED;
1036 }
1037
1038 WERROR _svcctl_QueryServiceConfigA(pipes_struct *p, struct svcctl_QueryServiceConfigA *r)
1039 {
1040         p->rng_fault_state = True;
1041         return WERR_NOT_SUPPORTED;
1042 }
1043
1044 WERROR _svcctl_QueryServiceLockStatusA(pipes_struct *p, struct svcctl_QueryServiceLockStatusA *r)
1045 {
1046         p->rng_fault_state = True;
1047         return WERR_NOT_SUPPORTED;
1048 }
1049
1050 WERROR _svcctl_StartServiceA(pipes_struct *p, struct svcctl_StartServiceA *r)
1051 {
1052         p->rng_fault_state = True;
1053         return WERR_NOT_SUPPORTED;
1054 }
1055
1056 WERROR _svcctl_GetServiceDisplayNameA(pipes_struct *p, struct svcctl_GetServiceDisplayNameA *r)
1057 {
1058         p->rng_fault_state = True;
1059         return WERR_NOT_SUPPORTED;
1060 }
1061
1062 WERROR _svcctl_GetServiceKeyNameA(pipes_struct *p, struct svcctl_GetServiceKeyNameA *r)
1063 {
1064         p->rng_fault_state = True;
1065         return WERR_NOT_SUPPORTED;
1066 }
1067
1068 WERROR _svcctl_GetCurrentGroupeStateW(pipes_struct *p, struct svcctl_GetCurrentGroupeStateW *r)
1069 {
1070         p->rng_fault_state = True;
1071         return WERR_NOT_SUPPORTED;
1072 }
1073
1074 WERROR _svcctl_EnumServiceGroupW(pipes_struct *p, struct svcctl_EnumServiceGroupW *r)
1075 {
1076         p->rng_fault_state = True;
1077         return WERR_NOT_SUPPORTED;
1078 }
1079
1080 WERROR _svcctl_ChangeServiceConfig2A(pipes_struct *p, struct svcctl_ChangeServiceConfig2A *r)
1081 {
1082         p->rng_fault_state = True;
1083         return WERR_NOT_SUPPORTED;
1084 }
1085
1086 WERROR _svcctl_ChangeServiceConfig2W(pipes_struct *p, struct svcctl_ChangeServiceConfig2W *r)
1087 {
1088         p->rng_fault_state = True;
1089         return WERR_NOT_SUPPORTED;
1090 }
1091
1092 WERROR _svcctl_QueryServiceConfig2A(pipes_struct *p, struct svcctl_QueryServiceConfig2A *r)
1093 {
1094         p->rng_fault_state = True;
1095         return WERR_NOT_SUPPORTED;
1096 }
1097
1098 WERROR _svcctl_QueryServiceConfig2W(pipes_struct *p, struct svcctl_QueryServiceConfig2W *r)
1099 {
1100         p->rng_fault_state = True;
1101         return WERR_NOT_SUPPORTED;
1102 }
1103
1104 WERROR _svcctl_QueryServiceStatusEx(pipes_struct *p, struct svcctl_QueryServiceStatusEx *r)
1105 {
1106         p->rng_fault_state = True;
1107         return WERR_NOT_SUPPORTED;
1108 }
1109
1110 WERROR _EnumServicesStatusExA(pipes_struct *p, struct EnumServicesStatusExA *r)
1111 {
1112         p->rng_fault_state = True;
1113         return WERR_NOT_SUPPORTED;
1114 }
1115
1116 WERROR _EnumServicesStatusExW(pipes_struct *p, struct EnumServicesStatusExW *r)
1117 {
1118         p->rng_fault_state = True;
1119         return WERR_NOT_SUPPORTED;
1120 }
1121
1122 WERROR _svcctl_SCSendTSMessage(pipes_struct *p, struct svcctl_SCSendTSMessage *r)
1123 {
1124         p->rng_fault_state = True;
1125         return WERR_NOT_SUPPORTED;
1126 }
1127