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