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